diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/launcher3/LauncherProvider.java | 22 | ||||
-rw-r--r-- | src/com/android/launcher3/LauncherSettings.java | 5 | ||||
-rw-r--r-- | src/com/android/launcher3/SettingsActivity.java | 97 | ||||
-rw-r--r-- | src/com/android/launcher3/Utilities.java | 11 |
4 files changed, 63 insertions, 72 deletions
diff --git a/src/com/android/launcher3/LauncherProvider.java b/src/com/android/launcher3/LauncherProvider.java index 5cc5aa6ec..7ebee31e4 100644 --- a/src/com/android/launcher3/LauncherProvider.java +++ b/src/com/android/launcher3/LauncherProvider.java @@ -323,28 +323,6 @@ public class LauncherProvider extends ContentProvider { createDbIfNotExists(); switch (method) { - case LauncherSettings.Settings.METHOD_GET_BOOLEAN: { - Bundle result = new Bundle(); - if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(arg)) { - result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, - Utilities.isAllowRotationPrefEnabled(getContext())); - } else { - result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, - Utilities.getPrefs(getContext()).getBoolean(arg, extras.getBoolean( - LauncherSettings.Settings.EXTRA_DEFAULT_VALUE))); - } - return result; - } - case LauncherSettings.Settings.METHOD_SET_BOOLEAN: { - final boolean value = extras.getBoolean(LauncherSettings.Settings.EXTRA_VALUE); - Utilities.getPrefs(getContext()).edit().putBoolean(arg, value).apply(); - if (extras.getBoolean(LauncherSettings.Settings.NOTIFY_BACKUP)) { - LauncherBackupAgentHelper.dataChanged(getContext()); - } - Bundle result = new Bundle(); - result.putBoolean(LauncherSettings.Settings.EXTRA_VALUE, value); - return result; - } case LauncherSettings.Settings.METHOD_SET_EXTRACTED_COLORS_AND_WALLPAPER_ID: { String extractedColors = extras.getString( LauncherSettings.Settings.EXTRA_EXTRACTED_COLORS); diff --git a/src/com/android/launcher3/LauncherSettings.java b/src/com/android/launcher3/LauncherSettings.java index 095670dc9..45a87cc02 100644 --- a/src/com/android/launcher3/LauncherSettings.java +++ b/src/com/android/launcher3/LauncherSettings.java @@ -277,8 +277,6 @@ public class LauncherSettings { 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 METHOD_CLEAR_EMPTY_DB_FLAG = "clear_empty_db_flag"; public static final String METHOD_DELETE_EMPTY_FOLDERS = "delete_empty_folders"; @@ -301,9 +299,6 @@ public class LauncherSettings { public static final String EXTRA_WALLPAPER_ID = "extra_wallpaperId"; public static final String EXTRA_VALUE = "value"; - public static final String EXTRA_DEFAULT_VALUE = "default_value"; - // Extra for set_boolean method to also notify the backup manager of the change. - public static final String NOTIFY_BACKUP = "notify_backup"; public static Bundle call(ContentResolver cr, String method) { return cr.call(CONTENT_URI, method, null, null); diff --git a/src/com/android/launcher3/SettingsActivity.java b/src/com/android/launcher3/SettingsActivity.java index 4135d5be8..5ef6dd531 100644 --- a/src/com/android/launcher3/SettingsActivity.java +++ b/src/com/android/launcher3/SettingsActivity.java @@ -17,13 +17,14 @@ package com.android.launcher3; import android.app.Activity; -import android.os.AsyncTask; +import android.content.ContentResolver; +import android.database.ContentObserver; import android.os.Bundle; +import android.os.Handler; import android.preference.Preference; -import android.preference.Preference.OnPreferenceChangeListener; import android.preference.PreferenceFragment; -import android.preference.PreferenceScreen; -import android.preference.TwoStatePreference; +import android.provider.Settings; +import android.provider.Settings.System; /** * Settings activity for Launcher. Currently implements the following setting: Allow rotation @@ -42,56 +43,70 @@ public class SettingsActivity extends Activity { /** * This fragment shows the launcher preferences. */ - public static class LauncherSettingsFragment extends PreferenceFragment - implements OnPreferenceChangeListener { + public static class LauncherSettingsFragment extends PreferenceFragment { + + private SystemDisplayRotationLockObserver mRotationLockObserver; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + getPreferenceManager().setSharedPreferencesName(LauncherFiles.SHARED_PREFERENCES_KEY); addPreferencesFromResource(R.xml.launcher_preferences); - PreferenceScreen screen = getPreferenceScreen(); - for (int i = screen.getPreferenceCount() - 1; i >= 0; i--) { - Preference pref = screen.getPreference(i); - if (pref instanceof TwoStatePreference) { - setBooleanPrefUsingContentProvider((TwoStatePreference) pref); - } + // Setup allow rotation preference + Preference rotationPref = findPreference(Utilities.ALLOW_ROTATION_PREFERENCE_KEY); + if (getResources().getBoolean(R.bool.allow_rotation)) { + // Launcher supports rotation by default. No need to show this setting. + getPreferenceScreen().removePreference(rotationPref); + } else { + ContentResolver resolver = getContext().getContentResolver(); + mRotationLockObserver = new SystemDisplayRotationLockObserver(rotationPref, resolver); + + // Register a content observer to listen for system setting changes while + // this UI is active. + resolver.registerContentObserver( + Settings.System.getUriFor(System.ACCELEROMETER_ROTATION), + false, mRotationLockObserver); + + // Initialize the UI once + mRotationLockObserver.onChange(true); + rotationPref.setDefaultValue(Utilities.getAllowRotationDefaultValue(getContext())); } } @Override - 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; + public void onDestroy() { + if (mRotationLockObserver != null) { + getContext().getContentResolver().unregisterContentObserver(mRotationLockObserver); + mRotationLockObserver = null; + } + super.onDestroy(); } + } - private void setBooleanPrefUsingContentProvider(final TwoStatePreference pref) { - pref.setPersistent(false); - pref.setEnabled(false); + /** + * Content observer which listens for system auto-rotate setting changes, and enables/disables + * the launcher rotation setting accordingly. + */ + private static class SystemDisplayRotationLockObserver extends ContentObserver { - 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); - } + private final Preference mRotationPref; + private final ContentResolver mResolver; - @Override - protected void onPostExecute(Boolean aBoolean) { - pref.setChecked(aBoolean); - pref.setEnabled(true); - pref.setOnPreferenceChangeListener(LauncherSettingsFragment.this); - } - }.execute(); + public SystemDisplayRotationLockObserver( + Preference rotationPref, ContentResolver resolver) { + super(new Handler()); + mRotationPref = rotationPref; + mResolver = resolver; + } + + @Override + public void onChange(boolean selfChange) { + boolean enabled = Settings.System.getInt(mResolver, + Settings.System.ACCELEROMETER_ROTATION, 1) == 1; + mRotationPref.setEnabled(enabled); + mRotationPref.setSummary(enabled + ? R.string.allow_rotation_desc : R.string.allow_rotation_blocked_desc); } } } diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java index c5f601dcd..53522fb83 100644 --- a/src/com/android/launcher3/Utilities.java +++ b/src/com/android/launcher3/Utilities.java @@ -142,7 +142,11 @@ public final class Utilities { } public static boolean isAllowRotationPrefEnabled(Context context) { - boolean allowRotationPref = false; + return getPrefs(context).getBoolean(ALLOW_ROTATION_PREFERENCE_KEY, + getAllowRotationDefaultValue(context)); + } + + public static boolean getAllowRotationDefaultValue(Context context) { if (isNycOrAbove()) { // If the device was scaled, used the original dimensions to determine if rotation // is allowed of not. @@ -153,13 +157,12 @@ public final class Utilities { Resources res = context.getResources(); int originalSmallestWidth = res.getConfiguration().smallestScreenWidthDp * res.getDisplayMetrics().densityDpi / originalDensity; - allowRotationPref = originalSmallestWidth >= 600; + return originalSmallestWidth >= 600; } catch (Exception e) { // Ignore } } - - return getPrefs(context).getBoolean(ALLOW_ROTATION_PREFERENCE_KEY, allowRotationPref); + return false; } public static boolean isNycOrAbove() { |