OSDN Git Service

Using content provider to update launcher settings
authorSunny Goyal <sunnygoyal@google.com>
Thu, 11 Jun 2015 23:18:39 +0000 (16:18 -0700)
committerSunny Goyal <sunnygoyal@google.com>
Fri, 12 Jun 2015 18:16:20 +0000 (11:16 -0700)
> Removing cross process preference file
> Removed broadcast listener management for settings changes
> Defining content provider method to get/set laucnehr preferences

Change-Id: Ida36eac0ab17c1d48fedc9404817a53a89b36c4f

AndroidManifest.xml
res/values/strings.xml
src/com/android/launcher3/Launcher.java
src/com/android/launcher3/LauncherAppState.java
src/com/android/launcher3/LauncherFiles.java
src/com/android/launcher3/LauncherProvider.java
src/com/android/launcher3/LauncherProviderChangeListener.java
src/com/android/launcher3/LauncherSettings.java
src/com/android/launcher3/SettingsActivity.java
src/com/android/launcher3/Utilities.java

index 5f9d7f4..03e9bbf 100644 (file)
         android:protectionLevel="signature"
         />
     <permission
-        android:name="com.android.launcher3.permission.RECEIVE_UPDATE_ORIENTATION_BROADCASTS"
-        android:protectionLevel="signature"
-        />
-    <permission
         android:name="com.android.launcher3.permission.RECEIVE_FIRST_LOAD_BROADCAST"
         android:protectionLevel="signatureOrSystem" />
 
@@ -66,7 +62,6 @@
     <uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS" />
     <uses-permission android:name="com.android.launcher3.permission.WRITE_SETTINGS" />
     <uses-permission android:name="com.android.launcher3.permission.RECEIVE_LAUNCH_BROADCASTS" />
-    <uses-permission android:name="com.android.launcher3.permission.RECEIVE_UPDATE_ORIENTATION_BROADCASTS" />
     <uses-permission android:name="com.android.launcher3.permission.RECEIVE_FIRST_LOAD_BROADCAST" />
 
     <application
index 305c310..440a537 100644 (file)
@@ -27,9 +27,6 @@
     <!-- Permission to receive the com.android.launcher3.action.LAUNCH intent -->
     <string name="receive_launch_broadcasts_permission" translatable="false">com.android.launcher3.permission.RECEIVE_LAUNCH_BROADCASTS</string>
 
-    <!-- Permission to receive the com.android.launcher3.SCREEN_ORIENTATION_PREF_CHANGED intent -->
-    <string name="receive_update_orientation_broadcasts_permission" translatable="false">com.android.launcher3.permission.RECEIVE_UPDATE_ORIENTATION_BROADCASTS</string>
-
     <!-- Permission to receive the com.android.launcher3.action.FIRST_LOAD_COMPLETE intent -->
     <string name="receive_first_load_broadcast_permission" translatable="false">com.android.launcher3.permission.RECEIVE_FIRST_LOAD_BROADCAST</string>
 
index 076a6e6..ba9a96e 100644 (file)
@@ -399,29 +399,8 @@ public class Launcher extends Activity
     }
 
     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 @@ public class Launcher extends Activity
         }
     }
 
+    private Runnable mUpdateOrientationRunnable = new Runnable() {
+        public void run() {
+            setOrientation();
+        }
+    };
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         if (DEBUG_STRICT_MODE) {
@@ -531,12 +516,6 @@ public class Launcher extends Activity
         // 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 @@ public class Launcher extends Activity
         }
     }
 
+    @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 class Launcher extends Activity
     public void onDestroy() {
         super.onDestroy();
 
-        if (mScreenOrientationSettingReceiverRegistered) {
-            unregisterReceiver(mScreenOrientationSettingReceiver);
-            mScreenOrientationSettingReceiverRegistered = false;
-        }
-
         // Remove all pending runnables
         mHandler.removeMessages(ADVANCE_MSG);
         mHandler.removeMessages(0);
index 76ad8c1..540bdf8 100644 (file)
@@ -21,10 +21,8 @@ import android.content.ComponentName;
 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 @@ public class LauncherAppState {
     private static LauncherAppState INSTANCE;
 
     private InvariantDeviceProfile mInvariantDeviceProfile;
+
     private LauncherAccessibilityDelegate mAccessibilityDelegate;
 
     public static LauncherAppState getInstance() {
index ec4e4f9..c08cd0b 100644 (file)
@@ -26,8 +26,6 @@ public class LauncherFiles {
     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 @@ public class LauncherFiles {
             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(
index b590126..cb808c2 100644 (file)
@@ -39,8 +39,10 @@ import android.database.sqlite.SQLiteOpenHelper;
 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 @@ public class LauncherProvider extends ContentProvider {
         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());
index 0de96fb..5b5c6c5 100644 (file)
@@ -8,4 +8,6 @@ package com.android.launcher3;
 public interface LauncherProviderChangeListener {
 
     public void onLauncherProviderChange();
+
+    public void onSettingsChanged(String settings, boolean value);
 }
index 90e60e4..afdb3dd 100644 (file)
@@ -305,4 +305,19 @@ public class LauncherSettings {
          */
         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";
+    }
 }
index 27763f5..dab71c8 100644 (file)
 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 @@ public class SettingsActivity extends Activity {
     /**
      * 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;
         }
     }
index 0cd980c..0f52cba 100644 (file)
@@ -92,17 +92,14 @@ public final class Utilities {
     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;
     }