Use a SwitchPreference for auto-rotate instead

- Change the UI to SwitchPreference
- Remove old string used by DropDownPreference
- Attach listener to monitor setting change

Change-Id: If42cceb74296814311eb0eff8e26b4a48a1c4d29
Fix: 35959797
Test: robotests
This commit is contained in:
Fan Zhang
2017-05-05 17:38:50 -07:00
parent e1c68df27e
commit 670410c04e
6 changed files with 233 additions and 48 deletions

View File

@@ -7075,17 +7075,6 @@
<!-- Summary Title for saying that the preference is experimental and will evolve over time due to User feedback. [CHAR LIMIT=NONE] --> <!-- Summary Title for saying that the preference is experimental and will evolve over time due to User feedback. [CHAR LIMIT=NONE] -->
<string name="experimental_preference">(Experimental)</string> <string name="experimental_preference">(Experimental)</string>
<!-- [CHAR LIMIT=45] Auto-rotate setting title -->
<string name="display_auto_rotate_title">Device rotation</string>
<!-- [CHAR LIMIT=70] Rotate when screen is turned option -->
<string name="display_auto_rotate_rotate">Rotate the contents of the screen</string>
<!-- [CHAR LIMIT=70] Keep the screen in portrait when rotated -->
<string name="display_auto_rotate_stay_in_portrait">Stay in portrait view</string>
<!-- [CHAR LIMIT=70] Keep the screen in landscape when rotated -->
<string name="display_auto_rotate_stay_in_landscape">Stay in landscape view</string>
<!-- [CHAR LIMIT=70] Don't rotate when screen is turned option -->
<string name="display_auto_rotate_stay_in_current">Stay in current orientation</string>
<!-- Encryption interstitial title. This screen asks the user whether the device will ask for a PIN / pattern / password before the device starts up. [CHAR LIMIT=30] --> <!-- Encryption interstitial title. This screen asks the user whether the device will ask for a PIN / pattern / password before the device starts up. [CHAR LIMIT=30] -->
<string name="encryption_interstitial_header">Secure start-up</string> <string name="encryption_interstitial_header">Secure start-up</string>

View File

@@ -57,10 +57,9 @@
android:entries="@array/screen_timeout_entries" android:entries="@array/screen_timeout_entries"
android:entryValues="@array/screen_timeout_values" /> android:entryValues="@array/screen_timeout_values" />
<DropDownPreference <SwitchPreference
android:key="auto_rotate" android:key="auto_rotate"
android:summary="%s" android:title="@string/accelerometer_title" />
android:title="@string/display_auto_rotate_title" />
<Preference <Preference
android:key="font_size" android:key="font_size"

View File

@@ -91,7 +91,7 @@ public class DisplaySettings extends DashboardFragment {
Context context, Lifecycle lifecycle) { Context context, Lifecycle lifecycle) {
final List<PreferenceController> controllers = new ArrayList<>(); final List<PreferenceController> controllers = new ArrayList<>();
controllers.add(new AutoBrightnessPreferenceController(context, KEY_AUTO_BRIGHTNESS)); controllers.add(new AutoBrightnessPreferenceController(context, KEY_AUTO_BRIGHTNESS));
controllers.add(new AutoRotatePreferenceController(context)); controllers.add(new AutoRotatePreferenceController(context, lifecycle));
controllers.add(new CameraGesturePreferenceController(context)); controllers.add(new CameraGesturePreferenceController(context));
controllers.add(new DozePreferenceController(context)); controllers.add(new DozePreferenceController(context));
controllers.add(new FontSizePreferenceController(context)); controllers.add(new FontSizePreferenceController(context));

View File

@@ -14,26 +14,33 @@
package com.android.settings.display; package com.android.settings.display;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration;
import android.support.v7.preference.DropDownPreference;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.TwoStatePreference;
import com.android.internal.logging.nano.MetricsProto; import com.android.internal.logging.nano.MetricsProto;
import com.android.internal.view.RotationPolicy; import com.android.internal.view.RotationPolicy;
import com.android.settings.R;
import com.android.settings.core.PreferenceController; import com.android.settings.core.PreferenceController;
import com.android.settings.core.instrumentation.MetricsFeatureProvider; import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.core.lifecycle.LifecycleObserver;
import com.android.settings.core.lifecycle.events.OnPause;
import com.android.settings.core.lifecycle.events.OnResume;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
public class AutoRotatePreferenceController extends PreferenceController implements public class AutoRotatePreferenceController extends PreferenceController implements
Preference.OnPreferenceChangeListener { Preference.OnPreferenceChangeListener, LifecycleObserver, OnResume, OnPause {
private static final String KEY_AUTO_ROTATE = "auto_rotate"; private static final String KEY_AUTO_ROTATE = "auto_rotate";
private final MetricsFeatureProvider mMetricsFeatureProvider; private final MetricsFeatureProvider mMetricsFeatureProvider;
private TwoStatePreference mPreference;
private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
public AutoRotatePreferenceController(Context context) { public AutoRotatePreferenceController(Context context, Lifecycle lifecycle) {
super(context); super(context);
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
if (lifecycle != null) {
lifecycle.addObserver(this);
}
} }
@Override @Override
@@ -43,28 +50,8 @@ public class AutoRotatePreferenceController extends PreferenceController impleme
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
final DropDownPreference rotatePreference = (DropDownPreference) preference; mPreference = (TwoStatePreference) preference;
final int rotateLockedResourceId; updatePreference();
// The following block sets the string used when rotation is locked.
// If the device locks specifically to portrait or landscape (rather than current
// rotation), then we use a different string to include this information.
if (allowAllRotations()) {
rotateLockedResourceId = R.string.display_auto_rotate_stay_in_current;
} else {
if (RotationPolicy.getRotationLockOrientation(mContext)
== Configuration.ORIENTATION_PORTRAIT) {
rotateLockedResourceId = R.string.display_auto_rotate_stay_in_portrait;
} else {
rotateLockedResourceId = R.string.display_auto_rotate_stay_in_landscape;
}
}
rotatePreference.setEntries(new CharSequence[]{
mContext.getString(R.string.display_auto_rotate_rotate),
mContext.getString(rotateLockedResourceId),
});
rotatePreference.setEntryValues(new CharSequence[]{"0", "1"});
rotatePreference.setValueIndex(RotationPolicy.isRotationLocked(mContext) ?
1 : 0);
} }
@Override @Override
@@ -72,17 +59,40 @@ public class AutoRotatePreferenceController extends PreferenceController impleme
return RotationPolicy.isRotationLockToggleVisible(mContext); return RotationPolicy.isRotationLockToggleVisible(mContext);
} }
private boolean allowAllRotations() {
return mContext.getResources().getBoolean(
com.android.internal.R.bool.config_allowAllRotations);
}
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean locked = Integer.parseInt((String) newValue) != 0; final boolean locked = !(boolean) newValue;
mMetricsFeatureProvider.action(mContext, MetricsProto.MetricsEvent.ACTION_ROTATION_LOCK, mMetricsFeatureProvider.action(mContext, MetricsProto.MetricsEvent.ACTION_ROTATION_LOCK,
locked); locked);
RotationPolicy.setRotationLock(mContext, locked); RotationPolicy.setRotationLock(mContext, locked);
return true; return true;
} }
@Override
public void onResume() {
if (mRotationPolicyListener == null) {
mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
@Override
public void onChange() {
updatePreference();
}
};
}
RotationPolicy.registerRotationPolicyListener(mContext,
mRotationPolicyListener);
}
@Override
public void onPause() {
if (mRotationPolicyListener != null) {
RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
}
}
private void updatePreference() {
if (mPreference == null) {
return;
}
mPreference.setChecked(!RotationPolicy.isRotationLocked(mContext));
}
} }

View File

@@ -0,0 +1,111 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.display;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowSystemSettings;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class AutoRotatePreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
@Mock
private PackageManager mPackageManager;
private Lifecycle mLifecycle;
private SwitchPreference mPreference;
private ContentResolver mContentResolver;
private AutoRotatePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest(mContext);
mContentResolver = RuntimeEnvironment.application.getContentResolver();
mLifecycle = new Lifecycle();
mPreference = new SwitchPreference(RuntimeEnvironment.application);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mContext.getContentResolver()).thenReturn(mContentResolver);
mController = new AutoRotatePreferenceController(mContext, mLifecycle);
}
@After
public void tearDown() {
ShadowSystemSettings.reset();
}
@Test
public void isAvailableWhenPolicyAllows() {
assertThat(mController.isAvailable()).isFalse();
when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
when(mContext.getResources().getBoolean(anyInt())).thenReturn(true);
Settings.System.putInt(mContentResolver,
Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0);
assertThat(mController.isAvailable()).isTrue();
}
@Test
@Config(shadows = ShadowSystemSettings.class)
public void updatePreference_settingsIsOff_shouldTurnOffToggle() {
Settings.System.putIntForUser(mContentResolver,
Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isFalse();
}
@Test
@Config(shadows = ShadowSystemSettings.class)
public void updatePreference_settingsIsOn_shouldTurnOnToggle() {
Settings.System.putIntForUser(mContentResolver,
Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT);
mController.updateState(mPreference);
assertThat(mPreference.isChecked()).isTrue();
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.testutils.shadow;
import android.content.ContentResolver;
import android.provider.Settings;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import java.util.HashMap;
import java.util.Map;
@Implements(Settings.System.class)
public class ShadowSystemSettings {
private static final Map<String, Object> sValueMap = new HashMap<>();
@Implementation
public static boolean putInt(ContentResolver resolver, String name, int value) {
sValueMap.put(name, value);
return true;
}
@Implementation
public static boolean putString(ContentResolver resolver, String name, String value) {
sValueMap.put(name, value);
return true;
}
@Implementation
public static String getString(ContentResolver resolver, String name) {
return (String) sValueMap.get(name);
}
@Implementation
public static String getStringForUser(ContentResolver resolver, String name, int userHandle) {
return getString(resolver, name);
}
@Implementation
public static boolean putIntForUser(ContentResolver cr, String name, int value,
int userHandle) {
return putInt(cr, name, value);
}
@Implementation
public static int getIntForUser(ContentResolver cr, String name, int def, int userHandle) {
return getInt(cr, name, def);
}
@Implementation
public static int getInt(ContentResolver resolver, String name, int defaultValue) {
Integer value = (Integer) sValueMap.get(name);
return value == null ? defaultValue : value;
}
public static void reset() {
sValueMap.clear();
}
}