Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 670410c0 authored by Fan Zhang's avatar Fan Zhang
Browse files

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
parent e1c68df2
Loading
Loading
Loading
Loading
+0 −11
Original line number Diff line number Diff line
@@ -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] -->
    <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] -->
    <string name="encryption_interstitial_header">Secure start-up</string>
+2 −3
Original line number Diff line number Diff line
@@ -57,10 +57,9 @@
        android:entries="@array/screen_timeout_entries"
        android:entryValues="@array/screen_timeout_values" />

    <DropDownPreference
    <SwitchPreference
        android:key="auto_rotate"
        android:summary="%s"
        android:title="@string/display_auto_rotate_title" />
        android:title="@string/accelerometer_title" />

    <Preference
        android:key="font_size"
+1 −1
Original line number Diff line number Diff line
@@ -91,7 +91,7 @@ public class DisplaySettings extends DashboardFragment {
            Context context, Lifecycle lifecycle) {
        final List<PreferenceController> controllers = new ArrayList<>();
        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 DozePreferenceController(context));
        controllers.add(new FontSizePreferenceController(context));
+43 −33
Original line number Diff line number Diff line
@@ -14,26 +14,33 @@
package com.android.settings.display;

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.TwoStatePreference;

import com.android.internal.logging.nano.MetricsProto;
import com.android.internal.view.RotationPolicy;
import com.android.settings.R;
import com.android.settings.core.PreferenceController;
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;

public class AutoRotatePreferenceController extends PreferenceController implements
        Preference.OnPreferenceChangeListener {
        Preference.OnPreferenceChangeListener, LifecycleObserver, OnResume, OnPause {

    private static final String KEY_AUTO_ROTATE = "auto_rotate";
    private final MetricsFeatureProvider mMetricsFeatureProvider;
    private TwoStatePreference mPreference;
    private RotationPolicy.RotationPolicyListener mRotationPolicyListener;

    public AutoRotatePreferenceController(Context context) {
    public AutoRotatePreferenceController(Context context, Lifecycle lifecycle) {
        super(context);
        mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
        if (lifecycle != null) {
            lifecycle.addObserver(this);
        }
    }

    @Override
@@ -43,28 +50,8 @@ public class AutoRotatePreferenceController extends PreferenceController impleme

    @Override
    public void updateState(Preference preference) {
        final DropDownPreference rotatePreference = (DropDownPreference) preference;
        final int rotateLockedResourceId;
        // 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);
        mPreference = (TwoStatePreference) preference;
        updatePreference();
    }

    @Override
@@ -72,17 +59,40 @@ public class AutoRotatePreferenceController extends PreferenceController impleme
        return RotationPolicy.isRotationLockToggleVisible(mContext);
    }

    private boolean allowAllRotations() {
        return mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_allowAllRotations);
    }

    @Override
    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,
                locked);
        RotationPolicy.setRotationLock(mContext, locked);
        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));
    }
}
+111 −0
Original line number Diff line number Diff line
/*
 * 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();
    }
}
Loading