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:
@@ -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 {
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user