Check for available rotation resolver service and camera permission before showing setting for face based auto-rotation

Test: locally with crosshatch & make -j64 RunSettingsRoboTests
ROBOTEST_FILTER="com.android.settings.display.SmartAutoRotateControllerTest"

Bug: 172857585
Change-Id: I825b0c2471c71a3de59532b39a47c5442f234fb5
This commit is contained in:
Abel Tesfaye
2021-02-11 01:25:49 +00:00
parent c0c3bce16e
commit ed7673eadd
8 changed files with 234 additions and 46 deletions

View File

@@ -954,7 +954,7 @@
<category android:name="com.android.settings.SHORTCUT" />
</intent-filter>
<meta-data android:name="com.android.settings.FRAGMENT_CLASS"
android:value="@string/rotate_settings_class" />
android:value="com.android.settings.display.SmartAutoRotatePreferenceFragment" />
<meta-data android:name="com.android.settings.PRIMARY_PROFILE_CONTROLLED"
android:value="true" />
</activity>

View File

@@ -2713,8 +2713,6 @@
<!-- Display settings --><skip/>
<!-- Sound & display settings screen, section header for settings related to display -->
<string name="display_settings">Display</string>
<!-- Sound & display settings screen, section header for settings related to display -->
<string name="rotate_settings_class" translatable="false">com.android.settings.DisplaySettings</string>
<!-- Sound & display settings screen, accelerometer-based rotation check box label -->
<string name="accelerometer_title">Auto-rotate screen</string>
<!-- Sound & display settings screen, locked rotation check box label [CHAR LIMIT=30] -->

View File

@@ -20,6 +20,13 @@
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/accelerometer_title" >
<Preference
android:key="face_rotate_permission"
android:title="@string/adaptive_sleep_title_no_permission"
android:summary="@string/adaptive_sleep_summary_no_permission"
android:icon="@drawable/ic_info_outline_24"
settings:controller="com.android.settings.display.SmartAutoRotatePermissionController" />
<SwitchPreference
android:key="face_based_rotate"
android:title="@string/auto_rotate_switch_face_based"

View File

@@ -28,6 +28,7 @@ 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 {

View File

@@ -17,9 +17,15 @@ package com.android.settings.display;
import static android.provider.Settings.Secure.CAMERA_AUTOROTATE;
import android.Manifest;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.provider.Settings;
import android.service.rotationresolver.RotationResolverService;
import android.text.TextUtils;
import androidx.preference.Preference;
@@ -43,13 +49,17 @@ public class SmartAutoRotateController extends TogglePreferenceController implem
@Override
public int getAvailabilityStatus() {
return !RotationPolicy.isRotationLocked(mContext)
if (!isRotationResolverServiceAvailable(mContext)) {
return UNSUPPORTED_ON_DEVICE;
}
return !RotationPolicy.isRotationLocked(mContext) && hasSufficientPermission(mContext)
? AVAILABLE : DISABLED_DEPENDENT_SETTING;
}
@Override
public boolean isChecked() {
return Settings.Secure.getInt(mContext.getContentResolver(),
return hasSufficientPermission(mContext) && Settings.Secure.getInt(
mContext.getContentResolver(),
CAMERA_AUTOROTATE, 0) == 1;
}
@@ -62,4 +72,24 @@ public class SmartAutoRotateController extends TogglePreferenceController implem
isChecked ? 1 : 0);
return true;
}
static boolean isRotationResolverServiceAvailable(Context context) {
final PackageManager packageManager = context.getPackageManager();
final String resolvePackage = packageManager.getRotationResolverPackageName();
if (TextUtils.isEmpty(resolvePackage)) {
return false;
}
final Intent intent = new Intent(RotationResolverService.SERVICE_INTERFACE).setPackage(
resolvePackage);
final ResolveInfo resolveInfo = packageManager.resolveService(intent,
PackageManager.MATCH_SYSTEM_ONLY);
return resolveInfo != null && resolveInfo.serviceInfo != null;
}
static boolean hasSufficientPermission(Context context) {
final PackageManager packageManager = context.getPackageManager();
final String rotationPackage = packageManager.getRotationResolverPackageName();
return rotationPackage != null && packageManager.checkPermission(
Manifest.permission.CAMERA, rotationPackage) == PackageManager.PERMISSION_GRANTED;
}
}

View File

@@ -0,0 +1,63 @@
/*
* 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.SmartAutoRotateController.hasSufficientPermission;
import static com.android.settings.display.SmartAutoRotateController.isRotationResolverServiceAvailable;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import androidx.preference.Preference;
import com.android.settings.core.BasePreferenceController;
/**
* The controller of camera based rotate permission warning preference. The preference appears when
* the camera permission is missing for the camera based rotation feature.
*/
public class SmartAutoRotatePermissionController extends BasePreferenceController {
private final Intent mIntent;
public SmartAutoRotatePermissionController(Context context, String key) {
super(context, key);
final String packageName = context.getPackageManager().getRotationResolverPackageName();
mIntent = new Intent(
android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
mIntent.setData(Uri.parse("package:" + packageName));
}
@Override
@AvailabilityStatus
public int getAvailabilityStatus() {
return isRotationResolverServiceAvailable(mContext) && !hasSufficientPermission(mContext)
? AVAILABLE_UNSEARCHABLE
: UNSUPPORTED_ON_DEVICE;
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (TextUtils.equals(getPreferenceKey(), preference.getKey())) {
mContext.startActivity(mIntent);
return true;
}
return super.handlePreferenceTreeClick(preference);
}
}

View File

@@ -15,8 +15,10 @@
*/
package com.android.settings.display;
import static com.android.settings.display.SmartAutoRotateController.hasSufficientPermission;
import static com.android.settings.display.SmartAutoRotateController.isRotationResolverServiceAvailable;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
@@ -31,16 +33,12 @@ import com.android.settings.SettingsActivity;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.widget.SettingsMainSwitchBar;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.Indexable;
import com.android.settingslib.widget.FooterPreference;
import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.List;
import com.android.settingslib.widget.FooterPreference;
/**
* Preference fragment used to auto rotation
* Preference fragment used for auto rotation
*/
@SuppressWarnings("WeakerAccess")
@SearchIndexable
@@ -51,7 +49,6 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
private AutoRotateSwitchBarController mSwitchBarController;
private static final String FACE_SWITCH_PREFERENCE_ID = "face_based_rotate";
private static final String SMART_AUTO_ROTATE_CONTROLLER_KEY = "auto_rotate";
@Override
protected int getPreferenceScreenResId() {
@@ -69,6 +66,12 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
switchBar.show();
mSwitchBarController = new AutoRotateSwitchBarController(activity, switchBar,
getSettingsLifecycle());
final Preference footerPreference = findPreference(FooterPreference.KEY_FOOTER);
if (footerPreference != null) {
footerPreference.setTitle(Html.fromHtml(getString(R.string.smart_rotate_text_headline),
Html.FROM_HTML_MODE_COMPACT));
footerPreference.setVisible(isRotationResolverServiceAvailable(activity));
}
return view;
}
@@ -82,7 +85,7 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
mSwitchBarController.onChange();
final boolean isLocked = RotationPolicy.isRotationLocked(getContext());
final Preference preference = findPreference(FACE_SWITCH_PREFERENCE_ID);
if (preference != null) {
if (preference != null && hasSufficientPermission(getContext())) {
preference.setEnabled(!isLocked);
}
}
@@ -90,17 +93,11 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
}
RotationPolicy.registerRotationPolicyListener(getPrefContext(),
mRotationPolicyListener);
findPreference(FooterPreference.KEY_FOOTER).setTitle(
Html.fromHtml(getString(R.string.smart_rotate_text_headline),
Html.FROM_HTML_MODE_COMPACT));
}
@Override
public void onPause() {
super.onPause();
if (mRotationPolicyListener != null) {
RotationPolicy.unregisterRotationPolicyListener(getPrefContext(),
mRotationPolicyListener);
@@ -112,36 +109,11 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
return SettingsEnums.DISPLAY_AUTO_ROTATE_SETTINGS;
}
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context);
}
private static List<AbstractPreferenceController> buildPreferenceControllers(
Context context) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(
new SmartAutoRotatePreferenceController(context, SMART_AUTO_ROTATE_CONTROLLER_KEY));
return controllers;
}
@Override
protected String getLogTag() {
return TAG;
}
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.auto_rotate_settings) {
@Override
public List<AbstractPreferenceController> createPreferenceControllers(
Context context) {
return buildPreferenceControllers(context);
}
@Override
protected boolean isPageSearchEnabled(Context context) {
return false;
}
};
new BaseSearchIndexProvider(R.xml.auto_rotate_settings);
}

View File

@@ -0,0 +1,117 @@
/*
* 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.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.DISABLED_DEPENDENT_SETTING;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
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.os.UserHandle;
import android.provider.Settings;
import androidx.preference.Preference;
import com.android.settings.testutils.ResolveInfoBuilder;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class SmartAutoRotateControllerTest {
private static final String PACKAGE_NAME = "package_name";
private SmartAutoRotateController mController;
@Mock
private PackageManager mPackageManager;
@Mock
private Preference mPreference;
private ContentResolver mContentResolver;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
final Context context = Mockito.spy(RuntimeEnvironment.application);
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);
mController = new SmartAutoRotateController(context, "test_key");
doReturn(mController.getPreferenceKey()).when(mPreference).getKey();
final ResolveInfo resolveInfo = new ResolveInfoBuilder(PACKAGE_NAME).build();
resolveInfo.serviceInfo = new ServiceInfo();
when(mPackageManager.resolveService(any(), anyInt())).thenReturn(resolveInfo);
enableAutoRotation();
}
@Test
public void getAvailabilityStatus_returnAvailable() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_resolveInfoIsNull_returnUnsupportedOnDevice() {
when(mPackageManager.resolveService(any(), anyInt())).thenReturn(null);
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
@Test
public void getAvailabilityStatus_noCameraPermission_returnDisableDependentSetting() {
doReturn(PackageManager.PERMISSION_DENIED).when(mPackageManager).checkPermission(
Manifest.permission.CAMERA, PACKAGE_NAME);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING);
}
@Test
public void getAvailabilityStatus_rotationLocked_returnDisableDependentSetting() {
disableAutoRotation();
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING);
}
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);
}
}