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.settings.R;
23 
24 /**
25  * A page with 3 radio buttons to choose the location mode.
26  *
27  * There are 3 location modes when location access is enabled:
28  *
29  * High accuracy: use both GPS and network location.
30  *
31  * Battery saving: use network location only to reduce the power consumption.
32  *
33  * Sensors only: use GPS location only.
34  */
35 public class LocationMode extends LocationSettingsBase
36         implements RadioButtonPreference.OnClickListener {
37     private static final String KEY_HIGH_ACCURACY = "high_accuracy";
38     private RadioButtonPreference mHighAccuracy;
39     private static final String KEY_BATTERY_SAVING = "battery_saving";
40     private RadioButtonPreference mBatterySaving;
41     private static final String KEY_SENSORS_ONLY = "sensors_only";
42     private RadioButtonPreference mSensorsOnly;
43 
44     @Override
onResume()45     public void onResume() {
46         super.onResume();
47         createPreferenceHierarchy();
48     }
49 
50     @Override
onPause()51     public void onPause() {
52         super.onPause();
53     }
54 
createPreferenceHierarchy()55     private PreferenceScreen createPreferenceHierarchy() {
56         PreferenceScreen root = getPreferenceScreen();
57         if (root != null) {
58             root.removeAll();
59         }
60         addPreferencesFromResource(R.xml.location_mode);
61         root = getPreferenceScreen();
62 
63         mHighAccuracy = (RadioButtonPreference) root.findPreference(KEY_HIGH_ACCURACY);
64         mBatterySaving = (RadioButtonPreference) root.findPreference(KEY_BATTERY_SAVING);
65         mSensorsOnly = (RadioButtonPreference) root.findPreference(KEY_SENSORS_ONLY);
66         mHighAccuracy.setOnClickListener(this);
67         mBatterySaving.setOnClickListener(this);
68         mSensorsOnly.setOnClickListener(this);
69 
70         refreshLocationMode();
71         return root;
72     }
73 
updateRadioButtons(RadioButtonPreference activated)74     private void updateRadioButtons(RadioButtonPreference activated) {
75         if (activated == null) {
76             mHighAccuracy.setChecked(false);
77             mBatterySaving.setChecked(false);
78             mSensorsOnly.setChecked(false);
79         } else if (activated == mHighAccuracy) {
80             mHighAccuracy.setChecked(true);
81             mBatterySaving.setChecked(false);
82             mSensorsOnly.setChecked(false);
83         } else if (activated == mBatterySaving) {
84             mHighAccuracy.setChecked(false);
85             mBatterySaving.setChecked(true);
86             mSensorsOnly.setChecked(false);
87         } else if (activated == mSensorsOnly) {
88             mHighAccuracy.setChecked(false);
89             mBatterySaving.setChecked(false);
90             mSensorsOnly.setChecked(true);
91         }
92     }
93 
94     @Override
onRadioButtonClicked(RadioButtonPreference emiter)95     public void onRadioButtonClicked(RadioButtonPreference emiter) {
96         int mode = Settings.Secure.LOCATION_MODE_OFF;
97         if (emiter == mHighAccuracy) {
98             mode = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
99         } else if (emiter == mBatterySaving) {
100             mode = Settings.Secure.LOCATION_MODE_BATTERY_SAVING;
101         } else if (emiter == mSensorsOnly) {
102             mode = Settings.Secure.LOCATION_MODE_SENSORS_ONLY;
103         }
104         setLocationMode(mode);
105     }
106 
107     @Override
onModeChanged(int mode, boolean restricted)108     public void onModeChanged(int mode, boolean restricted) {
109         switch (mode) {
110             case Settings.Secure.LOCATION_MODE_OFF:
111                 updateRadioButtons(null);
112                 break;
113             case Settings.Secure.LOCATION_MODE_SENSORS_ONLY:
114                 updateRadioButtons(mSensorsOnly);
115                 break;
116             case Settings.Secure.LOCATION_MODE_BATTERY_SAVING:
117                 updateRadioButtons(mBatterySaving);
118                 break;
119             case Settings.Secure.LOCATION_MODE_HIGH_ACCURACY:
120                 updateRadioButtons(mHighAccuracy);
121                 break;
122             default:
123                 break;
124         }
125 
126         boolean enabled = (mode != Settings.Secure.LOCATION_MODE_OFF) && !restricted;
127         mHighAccuracy.setEnabled(enabled);
128         mBatterySaving.setEnabled(enabled);
129         mSensorsOnly.setEnabled(enabled);
130     }
131 
132     @Override
getHelpResource()133     public int getHelpResource() {
134         return R.string.help_url_location_access;
135     }
136 }
137