• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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;
18 
19 
20 import android.content.ContentQueryMap;
21 import android.content.ContentResolver;
22 import android.content.Intent;
23 import android.database.Cursor;
24 import android.location.LocationManager;
25 import android.preference.CheckBoxPreference;
26 import android.preference.Preference;
27 import android.preference.Preference.OnPreferenceChangeListener;
28 import android.preference.PreferenceScreen;
29 import android.provider.Settings;
30 
31 import java.util.Observable;
32 import java.util.Observer;
33 
34 /**
35  * Gesture lock pattern settings.
36  */
37 public class LocationSettings extends SettingsPreferenceFragment
38         implements OnPreferenceChangeListener {
39 
40     // Location Settings
41     private static final String KEY_LOCATION_NETWORK = "location_network";
42     private static final String KEY_LOCATION_GPS = "location_gps";
43     private static final String KEY_ASSISTED_GPS = "assisted_gps";
44     private static final String KEY_USE_LOCATION = "location_use_for_services";
45 
46     private CheckBoxPreference mNetwork;
47     private CheckBoxPreference mGps;
48     private CheckBoxPreference mAssistedGps;
49     private CheckBoxPreference mUseLocation;
50 
51     // These provide support for receiving notification when Location Manager settings change.
52     // This is necessary because the Network Location Provider can change settings
53     // if the user does not confirm enabling the provider.
54     private ContentQueryMap mContentQueryMap;
55 
56     private Observer mSettingsObserver;
57 
58     @Override
onStart()59     public void onStart() {
60         super.onStart();
61         // listen for Location Manager settings changes
62         Cursor settingsCursor = getContentResolver().query(Settings.Secure.CONTENT_URI, null,
63                 "(" + Settings.System.NAME + "=?)",
64                 new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED},
65                 null);
66         mContentQueryMap = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, null);
67     }
68 
69     @Override
onStop()70     public void onStop() {
71         super.onStop();
72         if (mSettingsObserver != null) {
73             mContentQueryMap.deleteObserver(mSettingsObserver);
74         }
75     }
76 
createPreferenceHierarchy()77     private PreferenceScreen createPreferenceHierarchy() {
78         PreferenceScreen root = getPreferenceScreen();
79         if (root != null) {
80             root.removeAll();
81         }
82         addPreferencesFromResource(R.xml.location_settings);
83         root = getPreferenceScreen();
84 
85         mNetwork = (CheckBoxPreference) root.findPreference(KEY_LOCATION_NETWORK);
86         mGps = (CheckBoxPreference) root.findPreference(KEY_LOCATION_GPS);
87         mAssistedGps = (CheckBoxPreference) root.findPreference(KEY_ASSISTED_GPS);
88         if (GoogleLocationSettingHelper.isAvailable(getActivity())) {
89             // GSF present, Add setting for 'Use My Location'
90             CheckBoxPreference useLocation = new CheckBoxPreference(getActivity());
91             useLocation.setKey(KEY_USE_LOCATION);
92             useLocation.setTitle(R.string.use_location_title);
93             useLocation.setSummary(R.string.use_location_summary);
94             useLocation.setChecked(
95                     GoogleLocationSettingHelper.getUseLocationForServices(getActivity())
96                     == GoogleLocationSettingHelper.USE_LOCATION_FOR_SERVICES_ON);
97             useLocation.setPersistent(false);
98             useLocation.setOnPreferenceChangeListener(this);
99             getPreferenceScreen().addPreference(useLocation);
100             mUseLocation = useLocation;
101         }
102 
103         return root;
104     }
105 
106     @Override
onResume()107     public void onResume() {
108         super.onResume();
109 
110         // Make sure we reload the preference hierarchy since some of these settings
111         // depend on others...
112         createPreferenceHierarchy();
113         updateLocationToggles();
114 
115         if (mSettingsObserver == null) {
116             mSettingsObserver = new Observer() {
117                 public void update(Observable o, Object arg) {
118                     updateLocationToggles();
119                 }
120             };
121         }
122 
123         mContentQueryMap.addObserver(mSettingsObserver);
124     }
125 
126     @Override
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)127     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
128 
129         if (preference == mNetwork) {
130             Settings.Secure.setLocationProviderEnabled(getContentResolver(),
131                     LocationManager.NETWORK_PROVIDER, mNetwork.isChecked());
132         } else if (preference == mGps) {
133             boolean enabled = mGps.isChecked();
134             Settings.Secure.setLocationProviderEnabled(getContentResolver(),
135                     LocationManager.GPS_PROVIDER, enabled);
136             if (mAssistedGps != null) {
137                 mAssistedGps.setEnabled(enabled);
138             }
139         } else if (preference == mAssistedGps) {
140             Settings.Secure.putInt(getContentResolver(), Settings.Secure.ASSISTED_GPS_ENABLED,
141                     mAssistedGps.isChecked() ? 1 : 0);
142         } else {
143             // If we didn't handle it, let preferences handle it.
144             return super.onPreferenceTreeClick(preferenceScreen, preference);
145         }
146 
147         return true;
148     }
149 
150     /*
151      * Creates toggles for each available location provider
152      */
updateLocationToggles()153     private void updateLocationToggles() {
154         ContentResolver res = getContentResolver();
155         boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(
156                 res, LocationManager.GPS_PROVIDER);
157         mNetwork.setChecked(Settings.Secure.isLocationProviderEnabled(
158                 res, LocationManager.NETWORK_PROVIDER));
159         mGps.setChecked(gpsEnabled);
160         if (mAssistedGps != null) {
161             mAssistedGps.setChecked(Settings.Secure.getInt(res,
162                     Settings.Secure.ASSISTED_GPS_ENABLED, 2) == 1);
163             mAssistedGps.setEnabled(gpsEnabled);
164         }
165     }
166 
167     /**
168      * see confirmPatternThenDisableAndClear
169      */
170     @Override
onActivityResult(int requestCode, int resultCode, Intent data)171     public void onActivityResult(int requestCode, int resultCode, Intent data) {
172         super.onActivityResult(requestCode, resultCode, data);
173         createPreferenceHierarchy();
174     }
175 
onPreferenceChange(Preference preference, Object value)176     public boolean onPreferenceChange(Preference preference, Object value) {
177         if (preference == mUseLocation) {
178             boolean newValue = (value == null ? false : (Boolean) value);
179             GoogleLocationSettingHelper.setUseLocationForServices(getActivity(), newValue);
180             // We don't want to change the value immediately here, since the user may click
181             // disagree in the dialog that pops up. When the activity we just launched exits, this
182             // activity will be restated and the new value re-read, so the checkbox will get its
183             // new value then.
184             return false;
185         }
186         return true;
187     }
188 }
189