Support Screen Rotate in the Universal Settings API
Change extend from AbstractPreferenceController to TogglePreferenceControllear Bug: 67997439 Test: make RunSettingsRoboTests Change-Id: I86e59d00384fb131f2f2b92186e8536324db2e1b
This commit is contained in:
@@ -71,7 +71,8 @@
|
|||||||
<SwitchPreference
|
<SwitchPreference
|
||||||
android:key="auto_rotate"
|
android:key="auto_rotate"
|
||||||
android:title="@string/accelerometer_title"
|
android:title="@string/accelerometer_title"
|
||||||
settings:keywords="@string/keywords_auto_rotate"/>
|
settings:keywords="@string/keywords_auto_rotate"
|
||||||
|
settings:controller="com.android.settings.display.AutoRotatePreferenceController"/>
|
||||||
|
|
||||||
<Preference
|
<Preference
|
||||||
android:key="color_mode"
|
android:key="color_mode"
|
||||||
|
@@ -83,7 +83,6 @@ public class DisplaySettings extends DashboardFragment {
|
|||||||
private static List<AbstractPreferenceController> buildPreferenceControllers(
|
private static List<AbstractPreferenceController> buildPreferenceControllers(
|
||||||
Context context, Lifecycle lifecycle) {
|
Context context, Lifecycle lifecycle) {
|
||||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||||
controllers.add(new AutoRotatePreferenceController(context, lifecycle));
|
|
||||||
controllers.add(new CameraGesturePreferenceController(context));
|
controllers.add(new CameraGesturePreferenceController(context));
|
||||||
controllers.add(new LiftToWakePreferenceController(context));
|
controllers.add(new LiftToWakePreferenceController(context));
|
||||||
controllers.add(new NightDisplayPreferenceController(context));
|
controllers.add(new NightDisplayPreferenceController(context));
|
||||||
|
@@ -48,7 +48,7 @@ public abstract class TogglePreferenceController extends BasePreferenceControlle
|
|||||||
public abstract boolean setChecked(boolean isChecked);
|
public abstract boolean setChecked(boolean isChecked);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void updateState(Preference preference) {
|
public void updateState(Preference preference) {
|
||||||
if (preference instanceof TwoStatePreference) {
|
if (preference instanceof TwoStatePreference) {
|
||||||
((TwoStatePreference) preference).setChecked(isChecked());
|
((TwoStatePreference) preference).setChecked(isChecked());
|
||||||
} if (preference instanceof MasterSwitchPreference) {
|
} if (preference instanceof MasterSwitchPreference) {
|
||||||
|
@@ -15,59 +15,34 @@ package com.android.settings.display;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
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.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
|
import com.android.settings.core.TogglePreferenceController;
|
||||||
import com.android.settings.overlay.FeatureFactory;
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
|
||||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
|
||||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnResume;
|
import com.android.settingslib.core.lifecycle.events.OnResume;
|
||||||
|
|
||||||
public class AutoRotatePreferenceController extends AbstractPreferenceController implements
|
public class AutoRotatePreferenceController extends TogglePreferenceController implements
|
||||||
PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver,
|
PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver,
|
||||||
OnResume, OnPause {
|
OnResume, OnPause {
|
||||||
|
|
||||||
private static final String KEY_AUTO_ROTATE = "auto_rotate";
|
|
||||||
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||||
private TwoStatePreference mPreference;
|
private Preference mPreference;
|
||||||
private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
|
private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
|
||||||
|
|
||||||
public AutoRotatePreferenceController(Context context, Lifecycle lifecycle) {
|
public AutoRotatePreferenceController(Context context, String key) {
|
||||||
super(context);
|
super(context, key);
|
||||||
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
||||||
if (lifecycle != null) {
|
|
||||||
lifecycle.addObserver(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getPreferenceKey() {
|
|
||||||
return KEY_AUTO_ROTATE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateState(Preference preference) {
|
public void updateState(Preference preference) {
|
||||||
mPreference = (TwoStatePreference) preference;
|
mPreference = preference;
|
||||||
updatePreference();
|
super.updateState(preference);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isAvailable() {
|
|
||||||
return RotationPolicy.isRotationLockToggleVisible(mContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
|
||||||
final boolean locked = !(boolean) newValue;
|
|
||||||
mMetricsFeatureProvider.action(mContext, MetricsProto.MetricsEvent.ACTION_ROTATION_LOCK,
|
|
||||||
locked);
|
|
||||||
RotationPolicy.setRotationLock(mContext, locked);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -76,7 +51,9 @@ public class AutoRotatePreferenceController extends AbstractPreferenceController
|
|||||||
mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
|
mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onChange() {
|
public void onChange() {
|
||||||
updatePreference();
|
if (mPreference != null) {
|
||||||
|
updateState(mPreference);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -91,10 +68,23 @@ public class AutoRotatePreferenceController extends AbstractPreferenceController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updatePreference() {
|
@Override
|
||||||
if (mPreference == null) {
|
public int getAvailabilityStatus() {
|
||||||
return;
|
return RotationPolicy.isRotationLockToggleVisible(mContext)
|
||||||
|
? AVAILABLE : DISABLED_UNSUPPORTED;
|
||||||
}
|
}
|
||||||
mPreference.setChecked(!RotationPolicy.isRotationLocked(mContext));
|
|
||||||
|
@Override
|
||||||
|
public boolean isChecked() {
|
||||||
|
return !RotationPolicy.isRotationLocked(mContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setChecked(boolean isChecked) {
|
||||||
|
final boolean isLocked = !isChecked;
|
||||||
|
mMetricsFeatureProvider.action(mContext, MetricsProto.MetricsEvent.ACTION_ROTATION_LOCK,
|
||||||
|
isLocked);
|
||||||
|
RotationPolicy.setRotationLock(mContext, isLocked);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -21,7 +21,6 @@ import static org.mockito.Matchers.anyInt;
|
|||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.arch.lifecycle.LifecycleOwner;
|
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
@@ -29,10 +28,12 @@ import android.os.UserHandle;
|
|||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.support.v14.preference.SwitchPreference;
|
import android.support.v14.preference.SwitchPreference;
|
||||||
|
|
||||||
|
import com.android.internal.view.RotationPolicy;
|
||||||
|
import com.android.settings.core.BasePreferenceController;
|
||||||
import com.android.settings.testutils.FakeFeatureFactory;
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowRotationPolicy;
|
||||||
import com.android.settings.testutils.shadow.ShadowSystemSettings;
|
import com.android.settings.testutils.shadow.ShadowSystemSettings;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -52,8 +53,6 @@ public class AutoRotatePreferenceControllerTest {
|
|||||||
private Context mContext;
|
private Context mContext;
|
||||||
@Mock
|
@Mock
|
||||||
private PackageManager mPackageManager;
|
private PackageManager mPackageManager;
|
||||||
private LifecycleOwner mLifecycleOwner;
|
|
||||||
private Lifecycle mLifecycle;
|
|
||||||
private SwitchPreference mPreference;
|
private SwitchPreference mPreference;
|
||||||
private ContentResolver mContentResolver;
|
private ContentResolver mContentResolver;
|
||||||
private AutoRotatePreferenceController mController;
|
private AutoRotatePreferenceController mController;
|
||||||
@@ -63,13 +62,11 @@ public class AutoRotatePreferenceControllerTest {
|
|||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
FakeFeatureFactory.setupForTest();
|
FakeFeatureFactory.setupForTest();
|
||||||
mContentResolver = RuntimeEnvironment.application.getContentResolver();
|
mContentResolver = RuntimeEnvironment.application.getContentResolver();
|
||||||
mLifecycleOwner = () -> mLifecycle;
|
|
||||||
mLifecycle = new Lifecycle(mLifecycleOwner);
|
|
||||||
mPreference = new SwitchPreference(RuntimeEnvironment.application);
|
mPreference = new SwitchPreference(RuntimeEnvironment.application);
|
||||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
||||||
when(mContext.getContentResolver()).thenReturn(mContentResolver);
|
when(mContext.getContentResolver()).thenReturn(mContentResolver);
|
||||||
|
|
||||||
mController = new AutoRotatePreferenceController(mContext, mLifecycle);
|
mController = new AutoRotatePreferenceController(mContext, "auto_rotate");
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
@@ -81,18 +78,14 @@ public class AutoRotatePreferenceControllerTest {
|
|||||||
public void isAvailableWhenPolicyAllows() {
|
public void isAvailableWhenPolicyAllows() {
|
||||||
assertThat(mController.isAvailable()).isFalse();
|
assertThat(mController.isAvailable()).isFalse();
|
||||||
|
|
||||||
when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
|
enableAutoRotationPreference();
|
||||||
when(mContext.getResources().getBoolean(anyInt())).thenReturn(true);
|
|
||||||
Settings.System.putInt(mContentResolver,
|
|
||||||
Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0);
|
|
||||||
|
|
||||||
assertThat(mController.isAvailable()).isTrue();
|
assertThat(mController.isAvailable()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updatePreference_settingsIsOff_shouldTurnOffToggle() {
|
public void updatePreference_settingsIsOff_shouldTurnOffToggle() {
|
||||||
Settings.System.putIntForUser(mContentResolver,
|
disableAutoRotation();
|
||||||
Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT);
|
|
||||||
|
|
||||||
mController.updateState(mPreference);
|
mController.updateState(mPreference);
|
||||||
|
|
||||||
@@ -101,11 +94,77 @@ public class AutoRotatePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updatePreference_settingsIsOn_shouldTurnOnToggle() {
|
public void updatePreference_settingsIsOn_shouldTurnOnToggle() {
|
||||||
Settings.System.putIntForUser(mContentResolver,
|
enableAutoRotation();
|
||||||
Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT);
|
|
||||||
|
|
||||||
mController.updateState(mPreference);
|
mController.updateState(mPreference);
|
||||||
|
|
||||||
assertThat(mPreference.isChecked()).isTrue();
|
assertThat(mPreference.isChecked()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAvailabilityStatus() {
|
||||||
|
assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController
|
||||||
|
.DISABLED_UNSUPPORTED);
|
||||||
|
|
||||||
|
enableAutoRotationPreference();
|
||||||
|
|
||||||
|
assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController
|
||||||
|
.AVAILABLE);
|
||||||
|
|
||||||
|
disableAutoRotationPreference();
|
||||||
|
|
||||||
|
assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController
|
||||||
|
.DISABLED_UNSUPPORTED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsCheck() {
|
||||||
|
assertThat(mController.isChecked()).isFalse();
|
||||||
|
|
||||||
|
enableAutoRotation();
|
||||||
|
|
||||||
|
assertThat(mController.isChecked()).isTrue();
|
||||||
|
|
||||||
|
disableAutoRotation();
|
||||||
|
|
||||||
|
assertThat(mController.isChecked()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Config(shadows = {ShadowRotationPolicy.class})
|
||||||
|
public void testSetCheck() {
|
||||||
|
ShadowRotationPolicy.setRotationSupported(true);
|
||||||
|
|
||||||
|
mController.setChecked(false);
|
||||||
|
assertThat(mController.isChecked()).isFalse();
|
||||||
|
assertThat(RotationPolicy.isRotationLocked(mContext)).isTrue();
|
||||||
|
|
||||||
|
mController.setChecked(true);
|
||||||
|
assertThat(mController.isChecked()).isTrue();
|
||||||
|
assertThat(RotationPolicy.isRotationLocked(mContext)).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void enableAutoRotationPreference() {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void disableAutoRotationPreference() {
|
||||||
|
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, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void enableAutoRotation() {
|
||||||
|
Settings.System.putIntForUser(mContentResolver,
|
||||||
|
Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void disableAutoRotation() {
|
||||||
|
Settings.System.putIntForUser(mContentResolver,
|
||||||
|
Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 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.Context;
|
||||||
|
|
||||||
|
import com.android.internal.view.RotationPolicy;
|
||||||
|
|
||||||
|
import org.robolectric.annotation.Implementation;
|
||||||
|
import org.robolectric.annotation.Implements;
|
||||||
|
|
||||||
|
@Implements(RotationPolicy.class)
|
||||||
|
public class ShadowRotationPolicy {
|
||||||
|
private static boolean rotationLockEnabled = false;
|
||||||
|
private static boolean rotationSupported = true;
|
||||||
|
@Implementation
|
||||||
|
public static void setRotationLock(Context context, final boolean enabled) {
|
||||||
|
rotationLockEnabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
public static boolean isRotationLocked(Context context) {
|
||||||
|
return rotationLockEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
public static boolean isRotationSupported(Context context) {
|
||||||
|
return rotationSupported;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setRotationSupported(boolean supported) {
|
||||||
|
rotationSupported = supported;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user