OSDN Git Service

AI 144415: am: CL 144372 Cleanup Settings support for enabling and disabling location...
authorMike Lockwood <>
Fri, 3 Apr 2009 06:41:34 +0000 (23:41 -0700)
committerThe Android Open Source Project <initial-contribution@android.com>
Fri, 3 Apr 2009 06:41:34 +0000 (23:41 -0700)
  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
  Original author: lockwood

Automated import of CL 144415

src/com/android/settings/SecuritySettings.java

index 08e4a58..b7b382c 100644 (file)
@@ -64,7 +64,6 @@ public class SecuritySettings extends PreferenceActivity
 
     private CheckBoxPreference mNetwork;
     private CheckBoxPreference mGps;
-    private LocationManager mLocationManager;
     
     // To track whether Agree was clicked in the Network location warning dialog
     private boolean mOkClicked;
@@ -77,10 +76,6 @@ public class SecuritySettings extends PreferenceActivity
         mLockPatternUtils = new LockPatternUtils(getContentResolver());
 
         createPreferenceHierarchy();
-        
-        // Get the available location providers
-        mLocationManager = (LocationManager)
-            getSystemService(Context.LOCATION_SERVICE);
 
         mNetwork = (CheckBoxPreference) getPreferenceScreen().findPreference(LOCATION_NETWORK);
         mGps = (CheckBoxPreference) getPreferenceScreen().findPreference(LOCATION_GPS);
@@ -202,10 +197,12 @@ public class SecuritySettings extends PreferenceActivity
                         .show()
                         .setOnDismissListener(this);
             } else {
-                updateProviders();
+                Settings.Secure.setLocationProviderEnabled(getContentResolver(),
+                        LocationManager.NETWORK_PROVIDER, false);
             }
         } else if (preference == mGps) {
-            updateProviders();
+            Settings.Secure.setLocationProviderEnabled(getContentResolver(),
+                    LocationManager.GPS_PROVIDER, mGps.isChecked());
         }
 
         return false;
@@ -213,7 +210,8 @@ public class SecuritySettings extends PreferenceActivity
 
     public void onClick(DialogInterface dialog, int which) {
         if (which == DialogInterface.BUTTON_POSITIVE) {
-            updateProviders();
+            Settings.Secure.setLocationProviderEnabled(getContentResolver(),
+                    LocationManager.NETWORK_PROVIDER, true);
             mOkClicked = true;
         } else {
             // Reset the toggle
@@ -232,44 +230,10 @@ public class SecuritySettings extends PreferenceActivity
      * Creates toggles for each available location provider
      */
     private void updateToggles() {
-        String providers = getAllowedProviders();
-        mNetwork.setChecked(providers.contains(LocationManager.NETWORK_PROVIDER));
-        mGps.setChecked(providers.contains(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;
+        mNetwork.setChecked(Settings.Secure.isLocationProviderEnabled(
+                getContentResolver(), LocationManager.NETWORK_PROVIDER));
+        mGps.setChecked(Settings.Secure.isLocationProviderEnabled(
+                getContentResolver(), LocationManager.GPS_PROVIDER));
     }
 
     private boolean isToggled(Preference pref) {