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.Context; 23 import android.content.Intent; 24 import android.database.Cursor; 25 import android.location.LocationManager; 26 import android.os.UserManager; 27 import android.preference.CheckBoxPreference; 28 import android.preference.Preference; 29 import android.preference.PreferenceScreen; 30 import android.preference.SwitchPreference; 31 import android.provider.Settings; 32 import android.util.AttributeSet; 33 import android.view.View; 34 import android.widget.TextView; 35 36 import java.util.Observable; 37 import java.util.Observer; 38 39 /** 40 * Gesture lock pattern settings. 41 */ 42 public class LocationSettings extends SettingsPreferenceFragment 43 implements Preference.OnPreferenceChangeListener { 44 45 // Location Settings 46 private static final String KEY_LOCATION_TOGGLE = "location_toggle"; 47 private static final String KEY_LOCATION_NETWORK = "location_network"; 48 private static final String KEY_LOCATION_GPS = "location_gps"; 49 private static final String KEY_ASSISTED_GPS = "assisted_gps"; 50 51 private CheckBoxPreference mNetwork; 52 private CheckBoxPreference mGps; 53 private CheckBoxPreference mAssistedGps; 54 private SwitchPreference mLocationAccess; 55 56 // These provide support for receiving notification when Location Manager settings change. 57 // This is necessary because the Network Location Provider can change settings 58 // if the user does not confirm enabling the provider. 59 private ContentQueryMap mContentQueryMap; 60 61 private Observer mSettingsObserver; 62 63 @Override onStart()64 public void onStart() { 65 super.onStart(); 66 // listen for Location Manager settings changes 67 Cursor settingsCursor = getContentResolver().query(Settings.Secure.CONTENT_URI, null, 68 "(" + Settings.System.NAME + "=?)", 69 new String[]{Settings.Secure.LOCATION_PROVIDERS_ALLOWED}, 70 null); 71 mContentQueryMap = new ContentQueryMap(settingsCursor, Settings.System.NAME, true, null); 72 } 73 74 @Override onStop()75 public void onStop() { 76 super.onStop(); 77 if (mSettingsObserver != null) { 78 mContentQueryMap.deleteObserver(mSettingsObserver); 79 } 80 mContentQueryMap.close(); 81 } 82 createPreferenceHierarchy()83 private PreferenceScreen createPreferenceHierarchy() { 84 PreferenceScreen root = getPreferenceScreen(); 85 if (root != null) { 86 root.removeAll(); 87 } 88 addPreferencesFromResource(R.xml.location_settings); 89 root = getPreferenceScreen(); 90 91 mLocationAccess = (SwitchPreference) root.findPreference(KEY_LOCATION_TOGGLE); 92 mNetwork = (CheckBoxPreference) root.findPreference(KEY_LOCATION_NETWORK); 93 mGps = (CheckBoxPreference) root.findPreference(KEY_LOCATION_GPS); 94 mAssistedGps = (CheckBoxPreference) root.findPreference(KEY_ASSISTED_GPS); 95 96 // Only enable these controls if this user is allowed to change location 97 // sharing settings. 98 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE); 99 boolean isToggleAllowed = !um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION); 100 if (mLocationAccess != null) mLocationAccess.setEnabled(isToggleAllowed); 101 if (mNetwork != null) mNetwork.setEnabled(isToggleAllowed); 102 if (mGps != null) mGps.setEnabled(isToggleAllowed); 103 if (mAssistedGps != null) mAssistedGps.setEnabled(isToggleAllowed); 104 105 mLocationAccess.setOnPreferenceChangeListener(this); 106 return root; 107 } 108 109 @Override onResume()110 public void onResume() { 111 super.onResume(); 112 113 // Make sure we reload the preference hierarchy since some of these settings 114 // depend on others... 115 createPreferenceHierarchy(); 116 updateLocationToggles(); 117 118 if (mSettingsObserver == null) { 119 mSettingsObserver = new Observer() { 120 @Override 121 public void update(Observable o, Object arg) { 122 updateLocationToggles(); 123 } 124 }; 125 } 126 127 mContentQueryMap.addObserver(mSettingsObserver); 128 } 129 130 @Override onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)131 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { 132 final ContentResolver cr = getContentResolver(); 133 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE); 134 if (preference == mNetwork) { 135 if (!um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) { 136 Settings.Secure.setLocationProviderEnabled(cr, 137 LocationManager.NETWORK_PROVIDER, mNetwork.isChecked()); 138 } 139 } else if (preference == mGps) { 140 boolean enabled = mGps.isChecked(); 141 if (!um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) { 142 Settings.Secure.setLocationProviderEnabled(cr, 143 LocationManager.GPS_PROVIDER, enabled); 144 if (mAssistedGps != null) { 145 mAssistedGps.setEnabled(enabled); 146 } 147 } 148 } else if (preference == mAssistedGps) { 149 Settings.Global.putInt(cr, Settings.Global.ASSISTED_GPS_ENABLED, 150 mAssistedGps.isChecked() ? 1 : 0); 151 } else { 152 // If we didn't handle it, let preferences handle it. 153 return super.onPreferenceTreeClick(preferenceScreen, preference); 154 } 155 156 return true; 157 } 158 159 /* 160 * Creates toggles for each available location provider 161 */ updateLocationToggles()162 private void updateLocationToggles() { 163 ContentResolver res = getContentResolver(); 164 boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled( 165 res, LocationManager.GPS_PROVIDER); 166 boolean networkEnabled = Settings.Secure.isLocationProviderEnabled( 167 res, LocationManager.NETWORK_PROVIDER); 168 mGps.setChecked(gpsEnabled); 169 mNetwork.setChecked(networkEnabled); 170 mLocationAccess.setChecked(gpsEnabled || networkEnabled); 171 if (mAssistedGps != null) { 172 mAssistedGps.setChecked(Settings.Global.getInt(res, 173 Settings.Global.ASSISTED_GPS_ENABLED, 2) == 1); 174 mAssistedGps.setEnabled(gpsEnabled); 175 } 176 } 177 178 /** 179 * see confirmPatternThenDisableAndClear 180 */ 181 @Override onActivityResult(int requestCode, int resultCode, Intent data)182 public void onActivityResult(int requestCode, int resultCode, Intent data) { 183 super.onActivityResult(requestCode, resultCode, data); 184 createPreferenceHierarchy(); 185 } 186 187 /** Enable or disable all providers when the master toggle is changed. */ onToggleLocationAccess(boolean checked)188 private void onToggleLocationAccess(boolean checked) { 189 final UserManager um = (UserManager) getActivity().getSystemService(Context.USER_SERVICE); 190 if (um.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION)) { 191 return; 192 } 193 final ContentResolver cr = getContentResolver(); 194 Settings.Secure.setLocationProviderEnabled(cr, 195 LocationManager.GPS_PROVIDER, checked); 196 Settings.Secure.setLocationProviderEnabled(cr, 197 LocationManager.NETWORK_PROVIDER, checked); 198 updateLocationToggles(); 199 } 200 201 @Override onPreferenceChange(Preference pref, Object newValue)202 public boolean onPreferenceChange(Preference pref, Object newValue) { 203 if (pref.getKey().equals(KEY_LOCATION_TOGGLE)) { 204 onToggleLocationAccess((Boolean) newValue); 205 } 206 return true; 207 } 208 209 @Override getHelpResource()210 public int getHelpResource() { 211 return R.string.help_url_location_access; 212 } 213 } 214 215 class WrappingSwitchPreference extends SwitchPreference { 216 WrappingSwitchPreference(Context context, AttributeSet attrs, int defStyle)217 public WrappingSwitchPreference(Context context, AttributeSet attrs, int defStyle) { 218 super(context, attrs, defStyle); 219 } 220 WrappingSwitchPreference(Context context, AttributeSet attrs)221 public WrappingSwitchPreference(Context context, AttributeSet attrs) { 222 super(context, attrs); 223 } 224 225 @Override onBindView(View view)226 protected void onBindView(View view) { 227 super.onBindView(view); 228 229 TextView title = (TextView) view.findViewById(android.R.id.title); 230 if (title != null) { 231 title.setSingleLine(false); 232 title.setMaxLines(3); 233 } 234 } 235 } 236 237 class WrappingCheckBoxPreference extends CheckBoxPreference { 238 WrappingCheckBoxPreference(Context context, AttributeSet attrs, int defStyle)239 public WrappingCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) { 240 super(context, attrs, defStyle); 241 } 242 WrappingCheckBoxPreference(Context context, AttributeSet attrs)243 public WrappingCheckBoxPreference(Context context, AttributeSet attrs) { 244 super(context, attrs); 245 } 246 247 @Override onBindView(View view)248 protected void onBindView(View view) { 249 super.onBindView(view); 250 251 TextView title = (TextView) view.findViewById(android.R.id.title); 252 if (title != null) { 253 title.setSingleLine(false); 254 title.setMaxLines(3); 255 } 256 } 257 } 258