AI 144372: Cleanup Settings support for enabling and disabling location providers:

LocationManagerService now listens for changes to settings,
  making LocationManager.updateProviders() unnecessary.
  Removed LocationManager.updateProviders()
  Added Settings.Secure.setLocationProviderEnabled(), which is a thread-safe way
  of enabling or disabling a single location provider.
  This is safer than reading, modifying and writing the LOCATION_PROVIDERS_ALLOWED directly.
  BUG=1729031

Automated import of CL 144372
This commit is contained in:
Mike Lockwood
2009-04-02 21:41:57 -07:00
committed by The Android Open Source Project
parent e8302ee00e
commit 074149da80

View File

@@ -64,7 +64,6 @@ public class SecuritySettings extends PreferenceActivity
private CheckBoxPreference mNetwork; private CheckBoxPreference mNetwork;
private CheckBoxPreference mGps; private CheckBoxPreference mGps;
private LocationManager mLocationManager;
// To track whether Agree was clicked in the Network location warning dialog // To track whether Agree was clicked in the Network location warning dialog
private boolean mOkClicked; private boolean mOkClicked;
@@ -78,10 +77,6 @@ public class SecuritySettings extends PreferenceActivity
createPreferenceHierarchy(); createPreferenceHierarchy();
// Get the available location providers
mLocationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
mNetwork = (CheckBoxPreference) getPreferenceScreen().findPreference(LOCATION_NETWORK); mNetwork = (CheckBoxPreference) getPreferenceScreen().findPreference(LOCATION_NETWORK);
mGps = (CheckBoxPreference) getPreferenceScreen().findPreference(LOCATION_GPS); mGps = (CheckBoxPreference) getPreferenceScreen().findPreference(LOCATION_GPS);
updateToggles(); updateToggles();
@@ -202,10 +197,12 @@ public class SecuritySettings extends PreferenceActivity
.show() .show()
.setOnDismissListener(this); .setOnDismissListener(this);
} else { } else {
updateProviders(); Settings.Secure.setLocationProviderEnabled(getContentResolver(),
LocationManager.NETWORK_PROVIDER, false);
} }
} else if (preference == mGps) { } else if (preference == mGps) {
updateProviders(); Settings.Secure.setLocationProviderEnabled(getContentResolver(),
LocationManager.GPS_PROVIDER, mGps.isChecked());
} }
return false; return false;
@@ -213,7 +210,8 @@ public class SecuritySettings extends PreferenceActivity
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) { if (which == DialogInterface.BUTTON_POSITIVE) {
updateProviders(); Settings.Secure.setLocationProviderEnabled(getContentResolver(),
LocationManager.NETWORK_PROVIDER, true);
mOkClicked = true; mOkClicked = true;
} else { } else {
// Reset the toggle // Reset the toggle
@@ -232,44 +230,10 @@ public class SecuritySettings extends PreferenceActivity
* Creates toggles for each available location provider * Creates toggles for each available location provider
*/ */
private void updateToggles() { private void updateToggles() {
String providers = getAllowedProviders(); mNetwork.setChecked(Settings.Secure.isLocationProviderEnabled(
mNetwork.setChecked(providers.contains(LocationManager.NETWORK_PROVIDER)); getContentResolver(), LocationManager.NETWORK_PROVIDER));
mGps.setChecked(providers.contains(LocationManager.GPS_PROVIDER)); mGps.setChecked(Settings.Secure.isLocationProviderEnabled(
} getContentResolver(), LocationManager.GPS_PROVIDER));
private void updateProviders() {
String preferredProviders = "";
if (mNetwork.isChecked()) {
preferredProviders += LocationManager.NETWORK_PROVIDER;
}
if (mGps.isChecked()) {
preferredProviders += "," + LocationManager.GPS_PROVIDER;
}
setProviders(preferredProviders);
}
private void setProviders(String providers) {
// Update the secure setting LOCATION_PROVIDERS_ALLOWED
Settings.Secure.putString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED, providers);
if (Config.LOGV) {
Log.v("Location Accuracy", "Setting LOCATION_PROVIDERS_ALLOWED = " + providers);
}
// Inform the location manager about the changes
mLocationManager.updateProviders();
}
/**
* @return string containing a list of providers that have been enabled for use
*/
private String getAllowedProviders() {
String allowedProviders =
Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (allowedProviders == null) {
allowedProviders = "";
}
return allowedProviders;
} }
private boolean isToggled(Preference pref) { private boolean isToggled(Preference pref) {