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