Making the 'Allow rotation' setting dependent on system state

The setting will not be available on a tablet, where rotation is
always enabled. On mobiles, it will be disabled when auto-rotate
is disabled in display settings.
Also removing content provider dependency from settings, as its in the
same process as launcher.

Bug: 28704055
Change-Id: Ibe6b1e67411fb0e4b2e36446710f463e4a3d6883
This commit is contained in:
Sunny Goyal
2016-05-12 15:36:20 -07:00
parent f898b970cc
commit f48e59268b
5 changed files with 70 additions and 75 deletions
+5 -1
View File
@@ -169,7 +169,11 @@
<!-- Strings for settings -->
<!-- Title for Allow Rotation setting. [CHAR LIMIT=50] -->
<string name="allow_rotation_title">Allow rotation</string>
<string name="allow_rotation_title">Allow homescreen rotation</string>
<!-- Text explaining when the home screen will get rotated. [CHAR LIMIT=100] -->
<string name="allow_rotation_desc">When device is rotated</string>
<!-- Text explaining that rotation is disabled in Display settings. 'Display' refers to the Display section in system settings [CHAR LIMIT=100] -->
<string name="allow_rotation_blocked_desc">Current Display setting doesn\'t permit rotation</string>
<!-- Label on an icon that references an uninstalled package, for which we have no information about when it might be installed. [CHAR_LIMIT=15] -->
<string name="package_state_unknown">Unknown</string>
@@ -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);
@@ -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);
+58 -43
View File
@@ -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();
}
}
/**
* Content observer which listens for system auto-rotate setting changes, and enables/disables
* the launcher rotation setting accordingly.
*/
private static class SystemDisplayRotationLockObserver extends ContentObserver {
private final Preference mRotationPref;
private final ContentResolver mResolver;
public SystemDisplayRotationLockObserver(
Preference rotationPref, ContentResolver resolver) {
super(new Handler());
mRotationPref = rotationPref;
mResolver = resolver;
}
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();
@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);
}
}
}
+7 -4
View File
@@ -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() {