Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit db3720d4 authored by David Christie's avatar David Christie
Browse files

Add helper methods to set/query the location master switch.

Consolidates the logic of exactly what the master switch means
so that if we change in the future, we don't have to find
all the different parts of the code that use it (location
settings, location quick settings, power widget, etc).

Change-Id: Ib918143b82e16541f0873c4d9141adb11fb815fc
parent 40f5b63b
Loading
Loading
Loading
Loading
+47 −9
Original line number Original line Diff line number Diff line
@@ -33,6 +33,7 @@ import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.Cursor;
import android.database.SQLException;
import android.database.SQLException;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager;
@@ -737,6 +738,10 @@ public final class Settings {
    private static final String TAG = "Settings";
    private static final String TAG = "Settings";
    private static final boolean LOCAL_LOGV = false;
    private static final boolean LOCAL_LOGV = false;


    // Lock ensures that when enabling/disabling the master location switch, we don't end up
    // with a partial enable/disable state in multi-threaded situations.
    private static final Object mLocationSettingsLock = new Object();

    public static class SettingNotFoundException extends AndroidException {
    public static class SettingNotFoundException extends AndroidException {
        public SettingNotFoundException(String msg) {
        public SettingNotFoundException(String msg) {
            super(msg);
            super(msg);
@@ -4302,6 +4307,20 @@ public final class Settings {
            return isLocationProviderEnabledForUser(cr, provider, UserHandle.myUserId());
            return isLocationProviderEnabledForUser(cr, provider, UserHandle.myUserId());
        }
        }


        /**
         * Helper method for determining if the location master switch is enabled.
         * @param cr the content resolver to use
         * @return true if the master switch is enabled
         * @hide
         */
        public static final boolean isLocationMasterSwitchEnabled(ContentResolver cr) {
            int uid = UserHandle.myUserId();
            synchronized (mLocationSettingsLock) {
                return isLocationProviderEnabledForUser(cr, LocationManager.NETWORK_PROVIDER, uid)
                        || isLocationProviderEnabledForUser(cr, LocationManager.GPS_PROVIDER, uid);
            }
        }

        /**
        /**
         * Helper method for determining if a location provider is enabled.
         * Helper method for determining if a location provider is enabled.
         * @param cr the content resolver to use
         * @param cr the content resolver to use
@@ -4327,6 +4346,23 @@ public final class Settings {
            setLocationProviderEnabledForUser(cr, provider, enabled, UserHandle.myUserId());
            setLocationProviderEnabledForUser(cr, provider, enabled, UserHandle.myUserId());
        }
        }


        /**
         * Thread-safe method for enabling or disabling the location master switch.
         *
         * @param cr the content resolver to use
         * @param enabled true if master switch should be enabled
         * @hide
         */
        public static final void setLocationMasterSwitchEnabled(ContentResolver cr,
                boolean enabled) {
            int uid = UserHandle.myUserId();
            synchronized (mLocationSettingsLock) {
                setLocationProviderEnabledForUser(cr, LocationManager.GPS_PROVIDER, enabled, uid);
                setLocationProviderEnabledForUser(cr, LocationManager.NETWORK_PROVIDER, enabled,
                        uid);
            }
        }

        /**
        /**
         * Thread-safe method for enabling or disabling a single location provider.
         * Thread-safe method for enabling or disabling a single location provider.
         * @param cr the content resolver to use
         * @param cr the content resolver to use
@@ -4337,6 +4373,7 @@ public final class Settings {
         */
         */
        public static final void setLocationProviderEnabledForUser(ContentResolver cr,
        public static final void setLocationProviderEnabledForUser(ContentResolver cr,
                String provider, boolean enabled, int userId) {
                String provider, boolean enabled, int userId) {
            synchronized (mLocationSettingsLock) {
                // to ensure thread safety, we write the provider name with a '+' or '-'
                // to ensure thread safety, we write the provider name with a '+' or '-'
                // and let the SettingsProvider handle it rather than reading and modifying
                // and let the SettingsProvider handle it rather than reading and modifying
                // the list of enabled providers.
                // the list of enabled providers.
@@ -4349,6 +4386,7 @@ public final class Settings {
                        userId);
                        userId);
            }
            }
        }
        }
    }


    /**
    /**
     * Global system settings, containing preferences that always apply identically
     * Global system settings, containing preferences that always apply identically