Using content provider to update launcher settings

> Removing cross process preference file
> Removed broadcast listener management for settings changes
> Defining content provider method to get/set laucnehr preferences

Change-Id: Ida36eac0ab17c1d48fedc9404817a53a89b36c4f
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 076a6e6..ba9a96e 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -399,29 +399,8 @@
     }
 
     private Stats mStats;
-
     FocusIndicatorView mFocusHandler;
-
-    @Thunk boolean mRotationEnabled = false;
-    private boolean mScreenOrientationSettingReceiverRegistered = false;
-
-    final private BroadcastReceiver mScreenOrientationSettingReceiver =
-            new BroadcastReceiver() {
-                @Thunk Runnable mUpdateOrientationRunnable = new Runnable() {
-                    public void run() {
-                        setOrientation();
-                    }
-                };
-
-                @Override
-                public void onReceive(Context context, Intent intent) {
-                    mRotationEnabled = intent.getBooleanExtra(
-                            Utilities.SCREEN_ROTATION_SETTING_EXTRA, false);
-                    if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
-                        setOrientation();
-                    }
-                }
-            };
+    private boolean mRotationEnabled = false;
 
     @Thunk void setOrientation() {
         if (mRotationEnabled) {
@@ -432,6 +411,12 @@
         }
     }
 
+    private Runnable mUpdateOrientationRunnable = new Runnable() {
+        public void run() {
+            setOrientation();
+        }
+    };
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         if (DEBUG_STRICT_MODE) {
@@ -531,12 +516,6 @@
         // In case we are on a device with locked rotation, we should look at preferences to check
         // if the user has specifically allowed rotation.
         if (!mRotationEnabled) {
-            String updateOrientationBroadcastPermission = getResources().getString(
-                    R.string.receive_update_orientation_broadcasts_permission);
-            registerReceiver(mScreenOrientationSettingReceiver,
-                    new IntentFilter(Utilities.SCREEN_ROTATION_SETTING_INTENT),
-                    updateOrientationBroadcastPermission, null);
-            mScreenOrientationSettingReceiverRegistered = true;
             mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
         }
 
@@ -563,6 +542,16 @@
         }
     }
 
+    @Override
+    public void onSettingsChanged(String settings, boolean value) {
+        if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
+            mRotationEnabled = value;
+            if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
+                mUpdateOrientationRunnable.run();
+            }
+        }
+    }
+
     private LauncherCallbacks mLauncherCallbacks;
 
     public void onPostCreate(Bundle savedInstanceState) {
@@ -2031,11 +2020,6 @@
     public void onDestroy() {
         super.onDestroy();
 
-        if (mScreenOrientationSettingReceiverRegistered) {
-            unregisterReceiver(mScreenOrientationSettingReceiver);
-            mScreenOrientationSettingReceiverRegistered = false;
-        }
-
         // Remove all pending runnables
         mHandler.removeMessages(ADVANCE_MSG);
         mHandler.removeMessages(0);
diff --git a/src/com/android/launcher3/LauncherAppState.java b/src/com/android/launcher3/LauncherAppState.java
index 76ad8c1..540bdf8 100644
--- a/src/com/android/launcher3/LauncherAppState.java
+++ b/src/com/android/launcher3/LauncherAppState.java
@@ -21,10 +21,8 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.content.res.Resources;
 import android.util.Log;
 
-import android.view.ViewConfiguration;
 import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
 import com.android.launcher3.compat.LauncherAppsCompat;
 import com.android.launcher3.compat.PackageInstallerCompat;
@@ -48,6 +46,7 @@
     private static LauncherAppState INSTANCE;
 
     private InvariantDeviceProfile mInvariantDeviceProfile;
+
     private LauncherAccessibilityDelegate mAccessibilityDelegate;
 
     public static LauncherAppState getInstance() {
diff --git a/src/com/android/launcher3/LauncherFiles.java b/src/com/android/launcher3/LauncherFiles.java
index ec4e4f9..c08cd0b 100644
--- a/src/com/android/launcher3/LauncherFiles.java
+++ b/src/com/android/launcher3/LauncherFiles.java
@@ -26,8 +26,6 @@
     public static final String WIDGET_PREVIEWS_DB = "widgetpreviews.db";
     public static final String APP_ICONS_DB = "app_icons.db";
 
-    public static final String ROTATION_PREF_FILE = "com.android.launcher3.rotation.prefs";
-
     public static final List<String> ALL_FILES = Collections.unmodifiableList(Arrays.asList(
             DEFAULT_WALLPAPER_THUMBNAIL,
             DEFAULT_WALLPAPER_THUMBNAIL_OLD,
@@ -37,8 +35,7 @@
             WALLPAPER_IMAGES_DB,
             WIDGET_PREVIEWS_DB,
             MANAGED_USER_PREFERENCES_KEY,
-            APP_ICONS_DB,
-            ROTATION_PREF_FILE));
+            APP_ICONS_DB));
 
     // TODO: Delete these files on upgrade
     public static final List<String> OBSOLETE_FILES = Collections.unmodifiableList(Arrays.asList(
diff --git a/src/com/android/launcher3/LauncherProvider.java b/src/com/android/launcher3/LauncherProvider.java
index b590126..cb808c2 100644
--- a/src/com/android/launcher3/LauncherProvider.java
+++ b/src/com/android/launcher3/LauncherProvider.java
@@ -39,8 +39,10 @@
 import android.database.sqlite.SQLiteQueryBuilder;
 import android.database.sqlite.SQLiteStatement;
 import android.net.Uri;
+import android.os.Binder;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Process;
 import android.os.StrictMode;
 import android.os.UserManager;
 import android.text.TextUtils;
@@ -237,6 +239,38 @@
         return count;
     }
 
+    @Override
+    public Bundle call(String method, String arg, Bundle extras) {
+        if (Binder.getCallingUid() != Process.myUid()) {
+            return null;
+        }
+
+        switch (method) {
+            case LauncherSettings.Settings.METHOD_GET_BOOLEAN: {
+                Bundle result = new Bundle();
+                result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE,
+                        getContext().getSharedPreferences(
+                                LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE)
+                                .getBoolean(arg, extras.getBoolean(
+                                        LauncherSettings.Settings.EXTRA_DEFAULT_VALUE)));
+                return result;
+            }
+            case LauncherSettings.Settings.METHOD_SET_BOOLEAN: {
+                boolean value = extras.getBoolean(LauncherSettings.Settings.EXTRA_VALUE);
+                getContext().getSharedPreferences(
+                        LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE)
+                        .edit().putBoolean(arg, value).apply();
+                if (mListener != null) {
+                    mListener.onSettingsChanged(arg, value);
+                }
+                Bundle result = new Bundle();
+                result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, value);
+                return result;
+            }
+        }
+        return null;
+    }
+
     private void notifyListeners() {
         // always notify the backup agent
         LauncherBackupAgentHelper.dataChanged(getContext());
diff --git a/src/com/android/launcher3/LauncherProviderChangeListener.java b/src/com/android/launcher3/LauncherProviderChangeListener.java
index 0de96fb..5b5c6c5 100644
--- a/src/com/android/launcher3/LauncherProviderChangeListener.java
+++ b/src/com/android/launcher3/LauncherProviderChangeListener.java
@@ -8,4 +8,6 @@
 public interface LauncherProviderChangeListener {
 
     public void onLauncherProviderChange();
+
+    public void onSettingsChanged(String settings, boolean value);
 }
diff --git a/src/com/android/launcher3/LauncherSettings.java b/src/com/android/launcher3/LauncherSettings.java
index 90e60e4..afdb3dd 100644
--- a/src/com/android/launcher3/LauncherSettings.java
+++ b/src/com/android/launcher3/LauncherSettings.java
@@ -305,4 +305,19 @@
          */
         static final String OPTIONS = "options";
     }
+
+    /**
+     * Launcher settings
+     */
+    public static final class Settings {
+
+        public static final Uri CONTENT_URI = Uri.parse("content://" +
+                ProviderConfig.AUTHORITY + "/settings");
+
+        public static final String METHOD_GET_BOOLEAN = "get_boolean_setting";
+        public static final String METHOD_SET_BOOLEAN = "set_boolean_setting";
+
+        public static final String EXTRA_VALUE = "value";
+        public static final String EXTRA_DEFAULT_VALUE = "default_value";
+    }
 }
diff --git a/src/com/android/launcher3/SettingsActivity.java b/src/com/android/launcher3/SettingsActivity.java
index 27763f5..dab71c8 100644
--- a/src/com/android/launcher3/SettingsActivity.java
+++ b/src/com/android/launcher3/SettingsActivity.java
@@ -17,12 +17,11 @@
 package com.android.launcher3;
 
 import android.app.Activity;
-import android.content.Context;
-import android.content.Intent;
 import android.os.Bundle;
 import android.preference.Preference;
+import android.preference.Preference.OnPreferenceChangeListener;
 import android.preference.PreferenceFragment;
-import android.preference.PreferenceScreen;
+import android.preference.SwitchPreference;
 
 /**
  * Settings activity for Launcher. Currently implements the following setting: Allow rotation
@@ -41,26 +40,36 @@
     /**
      * This fragment shows the launcher preferences.
      */
-    @SuppressWarnings("WeakerAccess")
-    public static class LauncherSettingsFragment extends PreferenceFragment {
+    public static class LauncherSettingsFragment extends PreferenceFragment
+            implements OnPreferenceChangeListener {
         @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
-            getPreferenceManager().setSharedPreferencesMode(Context.MODE_MULTI_PROCESS);
-            getPreferenceManager().setSharedPreferencesName(LauncherFiles.ROTATION_PREF_FILE);
             addPreferencesFromResource(R.xml.launcher_preferences);
+
+            SwitchPreference pref = (SwitchPreference) findPreference(
+                    Utilities.ALLOW_ROTATION_PREFERENCE_KEY);
+            pref.setPersistent(false);
+
+            Bundle extras = new Bundle();
+            extras.putBoolean(LauncherSettings.Settings.EXTRA_DEFAULT_VALUE, false);
+            Bundle value = getActivity().getContentResolver().call(
+                    LauncherSettings.Settings.CONTENT_URI,
+                    LauncherSettings.Settings.METHOD_GET_BOOLEAN,
+                    Utilities.ALLOW_ROTATION_PREFERENCE_KEY, extras);
+            pref.setChecked(value.getBoolean(LauncherSettings.Settings.EXTRA_VALUE));
+
+            pref.setOnPreferenceChangeListener(this);
         }
 
         @Override
-        public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
-                                             Preference preference) {
-            boolean allowRotation = getPreferenceManager().getSharedPreferences().getBoolean(
-                    Utilities.ALLOW_ROTATION_PREFERENCE_KEY, false);
-            Intent rotationSetting = new Intent(Utilities.SCREEN_ROTATION_SETTING_INTENT);
-            String launchBroadcastPermission = getResources().getString(
-                            R.string.receive_update_orientation_broadcasts_permission);
-            rotationSetting.putExtra(Utilities.SCREEN_ROTATION_SETTING_EXTRA, allowRotation);
-            getActivity().sendBroadcast(rotationSetting, launchBroadcastPermission);
+        public boolean onPreferenceChange(Preference preference, Object newValue) {
+            Bundle extras = new Bundle();
+            extras.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, (Boolean) newValue);
+            getActivity().getContentResolver().call(
+                    LauncherSettings.Settings.CONTENT_URI,
+                    LauncherSettings.Settings.METHOD_SET_BOOLEAN,
+                    preference.getKey(), extras);
             return true;
         }
     }
diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java
index 0cd980c..0f52cba 100644
--- a/src/com/android/launcher3/Utilities.java
+++ b/src/com/android/launcher3/Utilities.java
@@ -92,17 +92,14 @@
     private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
 
     public static final String ALLOW_ROTATION_PREFERENCE_KEY = "pref_allowRotation";
-    public static final String SCREEN_ROTATION_SETTING_INTENT =
-            "come.android.launcher3.SCREEN_ORIENTATION_PREF_CHANGED";
-    public static final String SCREEN_ROTATION_SETTING_EXTRA = "screenRotationPref";
 
     public static boolean isPropertyEnabled(String propertyName) {
         return Log.isLoggable(propertyName, Log.VERBOSE);
     }
 
     public static boolean isAllowRotationPrefEnabled(Context context) {
-        SharedPreferences sharedPrefs = context.getSharedPreferences(LauncherFiles.ROTATION_PREF_FILE,
-                Context.MODE_MULTI_PROCESS);
+        SharedPreferences sharedPrefs = context.getSharedPreferences(
+                LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE);
         boolean allowRotationPref = sharedPrefs.getBoolean(ALLOW_ROTATION_PREFERENCE_KEY, false);
         return sForceEnableRotation || allowRotationPref;
     }