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

@@ -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;
}
}