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
This commit is contained in:
Abel Tesfaye
2021-03-30 22:05:36 +00:00
parent a8e4d51c24
commit faaf8d5af1
4 changed files with 147 additions and 8 deletions

View File

@@ -20,6 +20,11 @@
xmlns:settings="http://schemas.android.com/apk/res-auto" xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/accelerometer_title" > 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 <com.android.settingslib.widget.BannerMessagePreference
android:key="face_rotate_permission" android:key="face_rotate_permission"
android:title="@string/adaptive_sleep_title_no_permission" android:title="@string/adaptive_sleep_title_no_permission"

View File

@@ -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.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume; 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 public class AutoRotatePreferenceController extends TogglePreferenceController implements
PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver, PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver,
OnResume, OnPause { OnResume, OnPause {

View File

@@ -25,6 +25,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
import com.android.internal.view.RotationPolicy; import com.android.internal.view.RotationPolicy;
@@ -48,6 +49,7 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
private RotationPolicy.RotationPolicyListener mRotationPolicyListener; private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
private AutoRotateSwitchBarController mSwitchBarController; private AutoRotateSwitchBarController mSwitchBarController;
@VisibleForTesting static final String AUTO_ROTATE_SWITCH_PREFERENCE_ID = "auto_rotate_switch";
@Override @Override
protected int getPreferenceScreenResId() { protected int getPreferenceScreenResId() {
@@ -65,12 +67,7 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
Bundle savedInstanceState) { Bundle savedInstanceState) {
final View view = super.onCreateView(inflater, container, savedInstanceState); final View view = super.onCreateView(inflater, container, savedInstanceState);
final SettingsActivity activity = (SettingsActivity) getActivity(); final SettingsActivity activity = (SettingsActivity) getActivity();
final SettingsMainSwitchBar switchBar = activity.getSwitchBar(); createHeader(activity);
switchBar.setTitle(
getContext().getString(R.string.auto_rotate_settings_primary_switch_title));
switchBar.show();
mSwitchBarController = new AutoRotateSwitchBarController(activity, switchBar,
getSettingsLifecycle());
final Preference footerPreference = findPreference(FooterPreference.KEY_FOOTER); final Preference footerPreference = findPreference(FooterPreference.KEY_FOOTER);
if (footerPreference != null) { if (footerPreference != null) {
footerPreference.setTitle(Html.fromHtml(getString(R.string.smart_rotate_text_headline), footerPreference.setTitle(Html.fromHtml(getString(R.string.smart_rotate_text_headline),
@@ -80,6 +77,19 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
return view; 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 @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
@@ -87,7 +97,9 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() { mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
@Override @Override
public void onChange() { public void onChange() {
mSwitchBarController.onChange(); if (mSwitchBarController != null) {
mSwitchBarController.onChange();
}
} }
}; };
} }

View File

@@ -0,0 +1,123 @@
/*
* 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);
}
}