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

Commit faaf8d5a authored by Abel Tesfaye's avatar Abel Tesfaye
Browse files

Use list+ toggle on devices without face detection

Test: verified intended behavior on crosshatch and no regression on flame, make RunSettingsRoboTests -j$(nproc) ROBOTEST_FILTER=SmartAutoRotatePreferenceFragmentTest

Bug: 181299673
Change-Id: Ic9f44091e95915bc78bc70b86d7f132ba6e159ed
parent a8e4d51c
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -20,6 +20,11 @@
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:title="@string/accelerometer_title" >

    <SwitchPreference
        android:key="auto_rotate_switch"
        android:title="@string/auto_rotate_settings_primary_switch_title"
        settings:controller="com.android.settings.display.AutoRotatePreferenceController"/>

    <com.android.settingslib.widget.BannerMessagePreference
        android:key="face_rotate_permission"
        android:title="@string/adaptive_sleep_title_no_permission"
+0 −1
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;

// TODO b/180515542 this class is no longer needed on S+
public class AutoRotatePreferenceController extends TogglePreferenceController implements
        PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver,
        OnResume, OnPause {
+19 −7
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;

import com.android.internal.view.RotationPolicy;
@@ -48,6 +49,7 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {

    private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
    private AutoRotateSwitchBarController mSwitchBarController;
    @VisibleForTesting static final String AUTO_ROTATE_SWITCH_PREFERENCE_ID = "auto_rotate_switch";

    @Override
    protected int getPreferenceScreenResId() {
@@ -65,12 +67,7 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
            Bundle savedInstanceState) {
        final View view = super.onCreateView(inflater, container, savedInstanceState);
        final SettingsActivity activity = (SettingsActivity) getActivity();
        final SettingsMainSwitchBar switchBar = activity.getSwitchBar();
        switchBar.setTitle(
                getContext().getString(R.string.auto_rotate_settings_primary_switch_title));
        switchBar.show();
        mSwitchBarController = new AutoRotateSwitchBarController(activity, switchBar,
                getSettingsLifecycle());
        createHeader(activity);
        final Preference footerPreference = findPreference(FooterPreference.KEY_FOOTER);
        if (footerPreference != null) {
            footerPreference.setTitle(Html.fromHtml(getString(R.string.smart_rotate_text_headline),
@@ -80,6 +77,19 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
        return view;
    }

    @VisibleForTesting
    void createHeader(SettingsActivity activity) {
        if (isRotationResolverServiceAvailable(activity)) {
            final SettingsMainSwitchBar switchBar = activity.getSwitchBar();
            switchBar.setTitle(
                    getContext().getString(R.string.auto_rotate_settings_primary_switch_title));
            switchBar.show();
            mSwitchBarController = new AutoRotateSwitchBarController(activity, switchBar,
                    getSettingsLifecycle());
            findPreference(AUTO_ROTATE_SWITCH_PREFERENCE_ID).setVisible(false);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
@@ -87,8 +97,10 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
            mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
                @Override
                public void onChange() {
                    if (mSwitchBarController != null) {
                        mSwitchBarController.onChange();
                    }
                }
            };
        }
        RotationPolicy.registerRotationPolicyListener(getPrefContext(),
+123 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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 static com.android.settings.display.SmartAutoRotatePreferenceFragment.AUTO_ROTATE_SWITCH_PREFERENCE_ID;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.view.View;

import androidx.preference.Preference;

import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.testutils.ResolveInfoBuilder;
import com.android.settings.widget.SettingsMainSwitchBar;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

@RunWith(RobolectricTestRunner.class)
public class SmartAutoRotatePreferenceFragmentTest {

    private static final String PACKAGE_NAME = "package_name";

    private SmartAutoRotatePreferenceFragment mFragment;

    private SettingsMainSwitchBar mSwitchBar;

    @Mock
    private PackageManager mPackageManager;

    @Mock
    private View mView;

    @Mock
    private SettingsActivity mActivity;

    @Mock
    private Preference mRotateSwitchPreference;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        final Context context = spy(RuntimeEnvironment.application);
        ContentResolver mContentResolver = RuntimeEnvironment.application.getContentResolver();
        when(context.getPackageManager()).thenReturn(mPackageManager);
        when(context.getContentResolver()).thenReturn(mContentResolver);
        doReturn(PACKAGE_NAME).when(mPackageManager).getRotationResolverPackageName();
        doReturn(PackageManager.PERMISSION_GRANTED).when(mPackageManager).checkPermission(
                Manifest.permission.CAMERA, PACKAGE_NAME);

        final ResolveInfo resolveInfo = new ResolveInfoBuilder(PACKAGE_NAME).build();
        resolveInfo.serviceInfo = new ServiceInfo();
        when(mPackageManager.resolveService(any(), anyInt())).thenReturn(resolveInfo);

        mFragment = spy(new SmartAutoRotatePreferenceFragment());
        when(mActivity.getPackageManager()).thenReturn(mPackageManager);
        when(mFragment.getActivity()).thenReturn(mActivity);
        when(mFragment.getContext()).thenReturn(context);
        doReturn(mView).when(mFragment).getView();

        when(mFragment.findPreference(AUTO_ROTATE_SWITCH_PREFERENCE_ID)).thenReturn(
                mRotateSwitchPreference);

        mSwitchBar = spy(new SettingsMainSwitchBar(context));
        when(mActivity.getSwitchBar()).thenReturn(mSwitchBar);
        doReturn(mSwitchBar).when(mView).findViewById(R.id.switch_bar);
    }


    @Test
    public void createHeader_faceDetectionSupported_switchBarIsEnabled() {
        mFragment.createHeader(mActivity);

        verify(mSwitchBar, times(1)).show();
        verify(mRotateSwitchPreference, times(1)).setVisible(false);
    }

    @Test
    public void createHeader_faceDetectionUnSupported_switchBarIsDisabled() {
        doReturn(null).when(mPackageManager).getRotationResolverPackageName();

        mFragment.createHeader(mActivity);

        verify(mSwitchBar, never()).show();
        verify(mRotateSwitchPreference, never()).setVisible(false);
    }

}