LocationMode.java revision 4fb17c1c9847d8a1a7c2386f3f28b91616e73f4b
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
17package com.android.settings.location;
18
19import android.app.Activity;
20import android.content.Intent;
21import android.preference.PreferenceActivity;
22import android.preference.PreferenceScreen;
23import android.provider.Settings;
24
25import com.android.settings.R;
26
27/**
28 * A page with 3 radio buttons to choose the location mode.
29 *
30 * There are 3 location modes when location access is enabled:
31 *
32 * High accuracy: use both GPS and network location.
33 *
34 * Battery saving: use network location only to reduce the power consumption.
35 *
36 * Sensors only: use GPS location only.
37 */
38public class LocationMode extends LocationSettingsBase
39        implements RadioButtonPreference.OnClickListener {
40    private static final String KEY_HIGH_ACCURACY = "high_accuracy";
41    private RadioButtonPreference mHighAccuracy;
42    private static final String KEY_BATTERY_SAVING = "battery_saving";
43    private RadioButtonPreference mBatterySaving;
44    private static final String KEY_SENSORS_ONLY = "sensors_only";
45    private RadioButtonPreference mSensorsOnly;
46
47    @Override
48    public void onActivityResult(int requestCode, int resultCode, Intent data) {
49        super.onActivityResult(requestCode, resultCode, data);
50        createPreferenceHierarchy();
51    }
52
53    @Override
54    public void onResume() {
55        super.onResume();
56        createPreferenceHierarchy();
57    }
58
59    @Override
60    public void onPause() {
61        super.onPause();
62    }
63
64    private PreferenceScreen createPreferenceHierarchy() {
65        PreferenceScreen root = getPreferenceScreen();
66        if (root != null) {
67            root.removeAll();
68        }
69        addPreferencesFromResource(R.xml.location_mode);
70        root = getPreferenceScreen();
71
72        mHighAccuracy = (RadioButtonPreference) root.findPreference(KEY_HIGH_ACCURACY);
73        mBatterySaving = (RadioButtonPreference) root.findPreference(KEY_BATTERY_SAVING);
74        mSensorsOnly = (RadioButtonPreference) root.findPreference(KEY_SENSORS_ONLY);
75        mHighAccuracy.setOnClickListener(this);
76        mBatterySaving.setOnClickListener(this);
77        mSensorsOnly.setOnClickListener(this);
78
79        refreshLocationMode();
80        return root;
81    }
82
83    private void updateRadioButtons(RadioButtonPreference activated) {
84        if (activated == mHighAccuracy) {
85            mHighAccuracy.setChecked(true);
86            mBatterySaving.setChecked(false);
87            mSensorsOnly.setChecked(false);
88        } else if (activated == mBatterySaving) {
89            mHighAccuracy.setChecked(false);
90            mBatterySaving.setChecked(true);
91            mSensorsOnly.setChecked(false);
92        } else if (activated == mSensorsOnly) {
93            mHighAccuracy.setChecked(false);
94            mBatterySaving.setChecked(false);
95            mSensorsOnly.setChecked(true);
96        }
97    }
98
99    @Override
100    public void onRadioButtonClicked(RadioButtonPreference emiter) {
101        int mode = Settings.Secure.LOCATION_MODE_OFF;
102        if (emiter == mHighAccuracy) {
103            mode = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
104        } else if (emiter == mBatterySaving) {
105            mode = Settings.Secure.LOCATION_MODE_BATTERY_SAVING;
106        } else if (emiter == mSensorsOnly) {
107            mode = Settings.Secure.LOCATION_MODE_SENSORS_ONLY;
108        }
109        setLocationMode(mode);
110    }
111
112    @Override
113    public void onModeChanged(int mode, boolean restricted) {
114        switch (mode) {
115            case Settings.Secure.LOCATION_MODE_OFF:
116                Intent intent = new Intent();
117                PreferenceActivity pa = (PreferenceActivity) getActivity();
118                pa.finishPreferencePanel(LocationMode.this, Activity.RESULT_OK, intent);
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
140    public int getHelpResource() {
141        return R.string.help_url_location_access;
142    }
143}
144