Home | History | Annotate | Download | only in location
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings.location;
     18 
     19 import android.preference.PreferenceScreen;
     20 import android.provider.Settings;
     21 
     22 import com.android.internal.logging.MetricsLogger;
     23 import com.android.settings.R;
     24 
     25 /**
     26  * A page with 3 radio buttons to choose the location mode.
     27  *
     28  * There are 3 location modes when location access is enabled:
     29  *
     30  * High accuracy: use both GPS and network location.
     31  *
     32  * Battery saving: use network location only to reduce the power consumption.
     33  *
     34  * Sensors only: use GPS location only.
     35  */
     36 public class LocationMode extends LocationSettingsBase
     37         implements RadioButtonPreference.OnClickListener {
     38     private static final String KEY_HIGH_ACCURACY = "high_accuracy";
     39     private RadioButtonPreference mHighAccuracy;
     40     private static final String KEY_BATTERY_SAVING = "battery_saving";
     41     private RadioButtonPreference mBatterySaving;
     42     private static final String KEY_SENSORS_ONLY = "sensors_only";
     43     private RadioButtonPreference mSensorsOnly;
     44 
     45     @Override
     46     protected int getMetricsCategory() {
     47         return MetricsLogger.LOCATION_MODE;
     48     }
     49 
     50     @Override
     51     public void onResume() {
     52         super.onResume();
     53         createPreferenceHierarchy();
     54     }
     55 
     56     @Override
     57     public void onPause() {
     58         super.onPause();
     59     }
     60 
     61     private PreferenceScreen createPreferenceHierarchy() {
     62         PreferenceScreen root = getPreferenceScreen();
     63         if (root != null) {
     64             root.removeAll();
     65         }
     66         addPreferencesFromResource(R.xml.location_mode);
     67         root = getPreferenceScreen();
     68 
     69         mHighAccuracy = (RadioButtonPreference) root.findPreference(KEY_HIGH_ACCURACY);
     70         mBatterySaving = (RadioButtonPreference) root.findPreference(KEY_BATTERY_SAVING);
     71         mSensorsOnly = (RadioButtonPreference) root.findPreference(KEY_SENSORS_ONLY);
     72         mHighAccuracy.setOnClickListener(this);
     73         mBatterySaving.setOnClickListener(this);
     74         mSensorsOnly.setOnClickListener(this);
     75 
     76         refreshLocationMode();
     77         return root;
     78     }
     79 
     80     private void updateRadioButtons(RadioButtonPreference activated) {
     81         if (activated == null) {
     82             mHighAccuracy.setChecked(false);
     83             mBatterySaving.setChecked(false);
     84             mSensorsOnly.setChecked(false);
     85         } else if (activated == mHighAccuracy) {
     86             mHighAccuracy.setChecked(true);
     87             mBatterySaving.setChecked(false);
     88             mSensorsOnly.setChecked(false);
     89         } else if (activated == mBatterySaving) {
     90             mHighAccuracy.setChecked(false);
     91             mBatterySaving.setChecked(true);
     92             mSensorsOnly.setChecked(false);
     93         } else if (activated == mSensorsOnly) {
     94             mHighAccuracy.setChecked(false);
     95             mBatterySaving.setChecked(false);
     96             mSensorsOnly.setChecked(true);
     97         }
     98     }
     99 
    100     @Override
    101     public void onRadioButtonClicked(RadioButtonPreference emiter) {
    102         int mode = Settings.Secure.LOCATION_MODE_OFF;
    103         if (emiter == mHighAccuracy) {
    104             mode = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
    105         } else if (emiter == mBatterySaving) {
    106             mode = Settings.Secure.LOCATION_MODE_BATTERY_SAVING;
    107         } else if (emiter == mSensorsOnly) {
    108             mode = Settings.Secure.LOCATION_MODE_SENSORS_ONLY;
    109         }
    110         setLocationMode(mode);
    111     }
    112 
    113     @Override
    114     public void onModeChanged(int mode, boolean restricted) {
    115         switch (mode) {
    116             case Settings.Secure.LOCATION_MODE_OFF:
    117                 updateRadioButtons(null);
    118                 break;
    119             case Settings.Secure.LOCATION_MODE_SENSORS_ONLY:
    120                 updateRadioButtons(mSensorsOnly);
    121                 break;
    122             case Settings.Secure.LOCATION_MODE_BATTERY_SAVING:
    123                 updateRadioButtons(mBatterySaving);
    124                 break;
    125             case Settings.Secure.LOCATION_MODE_HIGH_ACCURACY:
    126                 updateRadioButtons(mHighAccuracy);
    127                 break;
    128             default:
    129                 break;
    130         }
    131 
    132         boolean enabled = (mode != Settings.Secure.LOCATION_MODE_OFF) && !restricted;
    133         mHighAccuracy.setEnabled(enabled);
    134         mBatterySaving.setEnabled(enabled);
    135         mSensorsOnly.setEnabled(enabled);
    136     }
    137 
    138     @Override
    139     public int getHelpResource() {
    140         return R.string.help_url_location_access;
    141     }
    142 }
    143