Merge "Import translations. DO NOT MERGE" into ub-launcher3-calgary
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 9ae311e..e7b703c 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -85,8 +85,11 @@
         <activity
             android:name="com.android.launcher3.SettingsActivity"
             android:label="@string/settings_button_text"
-            android:autoRemoveFromRecents="true"
-            android:process=":settings_process">
+            android:autoRemoveFromRecents="true">
+            <intent-filter>
+                <action android:name="android.intent.action.APPLICATION_PREFERENCES" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
         </activity>
 
         <!-- Intent received used to install shortcuts from other applications -->
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index a58cddd..d2d1d02 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -43,6 +43,7 @@
 import android.content.IntentFilter;
 import android.content.IntentSender;
 import android.content.SharedPreferences;
+import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
 import android.content.pm.ActivityInfo;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
@@ -374,11 +375,7 @@
         }
     }
 
-    private Runnable mUpdateOrientationRunnable = new Runnable() {
-        public void run() {
-            setOrientation();
-        }
-    };
+    private RotationPrefChangeHandler mRotationPrefChangeHandler;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -479,6 +476,8 @@
         // if the user has specifically allowed rotation.
         if (!mRotationEnabled) {
             mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
+            mRotationPrefChangeHandler = new RotationPrefChangeHandler();
+            mSharedPrefs.registerOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
         }
 
         // On large interfaces, or on devices that a user has specifically enabled screen rotation,
@@ -498,16 +497,6 @@
     }
 
     @Override
-    public void onSettingsChanged(String settings, boolean value) {
-        if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
-            mRotationEnabled = value;
-            if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
-                mUpdateOrientationRunnable.run();
-            }
-        }
-    }
-
-    @Override
     public void onExtractedColorsChanged() {
         loadExtractedColorsAndColorItems();
     }
@@ -1997,6 +1986,10 @@
             LauncherAppState.getInstance().setLauncher(null);
         }
 
+        if (mRotationPrefChangeHandler != null) {
+            mSharedPrefs.unregisterOnSharedPreferenceChangeListener(mRotationPrefChangeHandler);
+        }
+
         try {
             mAppWidgetHost.stopListening();
         } catch (NullPointerException ex) {
@@ -2744,13 +2737,10 @@
      * Event handler for a click on the settings button that appears after a long press
      * on the home screen.
      */
-    protected void onClickSettingsButton(View v) {
+    private void onClickSettingsButton(View v) {
         if (LOGD) Log.d(TAG, "onClickSettingsButton");
-        if (mLauncherCallbacks != null) {
-            mLauncherCallbacks.onClickSettingsButton(v);
-        } else {
-            startActivity(new Intent(this, SettingsActivity.class));
-        }
+        startActivity(new Intent(Utilities.ACTION_APPLICATION_PREFERENCES)
+                .setPackage(getPackageName()));
     }
 
     public View.OnTouchListener getHapticFeedbackTouchListener() {
@@ -4698,6 +4688,25 @@
             return Collections.EMPTY_LIST;
         }
     }
+
+    private class RotationPrefChangeHandler implements OnSharedPreferenceChangeListener, Runnable {
+
+        @Override
+        public void onSharedPreferenceChanged(
+                SharedPreferences sharedPreferences, String key) {
+            if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
+                mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
+                if (!waitUntilResume(this, true)) {
+                    run();
+                }
+            }
+        }
+
+        @Override
+        public void run() {
+            setOrientation();
+        }
+    }
 }
 
 interface DebugIntents {
diff --git a/src/com/android/launcher3/LauncherCallbacks.java b/src/com/android/launcher3/LauncherCallbacks.java
index 34117b6..e971631 100644
--- a/src/com/android/launcher3/LauncherCallbacks.java
+++ b/src/com/android/launcher3/LauncherCallbacks.java
@@ -77,8 +77,6 @@
     public void onInteractionEnd();
 
     @Deprecated
-    public void onClickSettingsButton(View v);
-    @Deprecated
     public void onWorkspaceLockedChanged();
 
     public boolean providesSearch();
diff --git a/src/com/android/launcher3/LauncherProvider.java b/src/com/android/launcher3/LauncherProvider.java
index a62d1e7..53026ac 100644
--- a/src/com/android/launcher3/LauncherProvider.java
+++ b/src/com/android/launcher3/LauncherProvider.java
@@ -280,17 +280,6 @@
             case LauncherSettings.Settings.METHOD_SET_BOOLEAN: {
                 final boolean value = extras.getBoolean(LauncherSettings.Settings.EXTRA_VALUE);
                 Utilities.getPrefs(getContext()).edit().putBoolean(arg, value).apply();
-                new MainThreadExecutor().execute(new Runnable() {
-                    @Override
-                    public void run() {
-                        synchronized (LISTENER_LOCK) {
-                            if (mListener != null) {
-                                mListener.onSettingsChanged(arg, value);
-                            }
-                        }
-
-                    }
-                });
                 if (extras.getBoolean(LauncherSettings.Settings.NOTIFY_BACKUP)) {
                     LauncherBackupAgentHelper.dataChanged(getContext());
                 }
diff --git a/src/com/android/launcher3/LauncherProviderChangeListener.java b/src/com/android/launcher3/LauncherProviderChangeListener.java
index 2d2da6e..524befc 100644
--- a/src/com/android/launcher3/LauncherProviderChangeListener.java
+++ b/src/com/android/launcher3/LauncherProviderChangeListener.java
@@ -9,8 +9,6 @@
 
     public void onLauncherProviderChange();
 
-    public void onSettingsChanged(String settings, boolean value);
-
     public void onExtractedColorsChanged();
 
     public void onAppWidgetHostReset();
diff --git a/src/com/android/launcher3/SettingsActivity.java b/src/com/android/launcher3/SettingsActivity.java
index dab71c8..4135d5b 100644
--- a/src/com/android/launcher3/SettingsActivity.java
+++ b/src/com/android/launcher3/SettingsActivity.java
@@ -17,11 +17,13 @@
 package com.android.launcher3;
 
 import android.app.Activity;
+import android.os.AsyncTask;
 import android.os.Bundle;
 import android.preference.Preference;
 import android.preference.Preference.OnPreferenceChangeListener;
 import android.preference.PreferenceFragment;
-import android.preference.SwitchPreference;
+import android.preference.PreferenceScreen;
+import android.preference.TwoStatePreference;
 
 /**
  * Settings activity for Launcher. Currently implements the following setting: Allow rotation
@@ -47,19 +49,13 @@
             super.onCreate(savedInstanceState);
             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);
+            PreferenceScreen screen = getPreferenceScreen();
+            for (int i = screen.getPreferenceCount() - 1; i >= 0; i--) {
+                Preference pref = screen.getPreference(i);
+                if (pref instanceof TwoStatePreference) {
+                    setBooleanPrefUsingContentProvider((TwoStatePreference) pref);
+                }
+            }
         }
 
         @Override
@@ -72,5 +68,30 @@
                     preference.getKey(), extras);
             return true;
         }
+
+        private void setBooleanPrefUsingContentProvider(final TwoStatePreference pref) {
+            pref.setPersistent(false);
+            pref.setEnabled(false);
+
+            new AsyncTask<Void, Void, Boolean>() {
+                @Override
+                protected Boolean doInBackground(Void... params) {
+                    Bundle extras = new Bundle();
+                    extras.putBoolean(LauncherSettings.Settings.EXTRA_DEFAULT_VALUE, true);
+                    Bundle value = pref.getContext().getContentResolver().call(
+                            LauncherSettings.Settings.CONTENT_URI,
+                            LauncherSettings.Settings.METHOD_GET_BOOLEAN,
+                            pref.getKey(), extras);
+                    return value.getBoolean(LauncherSettings.Settings.EXTRA_VALUE);
+                }
+
+                @Override
+                protected void onPostExecute(Boolean aBoolean) {
+                    pref.setChecked(aBoolean);
+                    pref.setEnabled(true);
+                    pref.setOnPreferenceChangeListener(LauncherSettingsFragment.this);
+                }
+            }.execute();
+        }
     }
 }
diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java
index d6d3517..1acbfc1 100644
--- a/src/com/android/launcher3/Utilities.java
+++ b/src/com/android/launcher3/Utilities.java
@@ -173,6 +173,10 @@
         }
     }
 
+    // TODO: Use Intent.ACTION_APPLICATION_PREFERENCES when N SDK is available.
+    public static final String ACTION_APPLICATION_PREFERENCES
+            = "android.intent.action.APPLICATION_PREFERENCES";
+
     public static Bitmap createIconBitmap(Cursor c, int iconIndex, Context context) {
         byte[] data = c.getBlob(iconIndex);
         try {
diff --git a/src/com/android/launcher3/testing/LauncherExtension.java b/src/com/android/launcher3/testing/LauncherExtension.java
index 8b6f5cd..c62ce0e 100644
--- a/src/com/android/launcher3/testing/LauncherExtension.java
+++ b/src/com/android/launcher3/testing/LauncherExtension.java
@@ -127,10 +127,6 @@
         }
 
         @Override
-        public void onClickSettingsButton(View v) {
-        }
-
-        @Override
         public void onWorkspaceLockedChanged() {
         }