Merge "Fix issue where smart auto rotate toggle is not disabled" into sc-dev

This commit is contained in:
Abel Tesfaye
2021-05-14 15:55:01 +00:00
committed by Android (Google) Code Review
2 changed files with 37 additions and 24 deletions

View File

@@ -18,6 +18,9 @@ package com.android.settings.display;
import static android.hardware.SensorPrivacyManager.Sensors.CAMERA; import static android.hardware.SensorPrivacyManager.Sensors.CAMERA;
import static android.provider.Settings.Secure.CAMERA_AUTOROTATE; import static android.provider.Settings.Secure.CAMERA_AUTOROTATE;
import static androidx.lifecycle.Lifecycle.Event.ON_START;
import static androidx.lifecycle.Lifecycle.Event.ON_STOP;
import android.Manifest; import android.Manifest;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@@ -32,7 +35,9 @@ import android.provider.Settings;
import android.service.rotationresolver.RotationResolverService; import android.service.rotationresolver.RotationResolverService;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver; import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
@@ -41,14 +46,12 @@ import com.android.internal.view.RotationPolicy;
import com.android.settings.core.TogglePreferenceController; import com.android.settings.core.TogglePreferenceController;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
/** /**
* SmartAutoRotateController controls whether auto rotation is enabled * SmartAutoRotateController controls whether auto rotation is enabled
*/ */
public class SmartAutoRotateController extends TogglePreferenceController implements public class SmartAutoRotateController extends TogglePreferenceController implements
Preference.OnPreferenceChangeListener, LifecycleObserver, OnStart, OnStop { Preference.OnPreferenceChangeListener, LifecycleObserver {
private final MetricsFeatureProvider mMetricsFeatureProvider; private final MetricsFeatureProvider mMetricsFeatureProvider;
private final SensorPrivacyManager mPrivacyManager; private final SensorPrivacyManager mPrivacyManager;
@@ -60,6 +63,7 @@ public class SmartAutoRotateController extends TogglePreferenceController implem
} }
}; };
private Preference mPreference; private Preference mPreference;
private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
public SmartAutoRotateController(Context context, String preferenceKey) { public SmartAutoRotateController(Context context, String preferenceKey) {
super(context, preferenceKey); super(context, preferenceKey);
@@ -70,6 +74,10 @@ public class SmartAutoRotateController extends TogglePreferenceController implem
mPowerManager = context.getSystemService(PowerManager.class); mPowerManager = context.getSystemService(PowerManager.class);
} }
public void init(Lifecycle lifecycle) {
lifecycle.addObserver(this);
}
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
if (!isRotationResolverServiceAvailable(mContext)) { if (!isRotationResolverServiceAvailable(mContext)) {
@@ -101,21 +109,35 @@ public class SmartAutoRotateController extends TogglePreferenceController implem
return mPowerManager.isPowerSaveMode(); return mPowerManager.isPowerSaveMode();
} }
@Override @OnLifecycleEvent(ON_START)
public void onStart() { public void onStart() {
mContext.registerReceiver(mReceiver, mContext.registerReceiver(mReceiver,
new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)); new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
if (mRotationPolicyListener == null) {
mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
@Override
public void onChange() {
updateState(mPreference);
}
};
}
RotationPolicy.registerRotationPolicyListener(mContext, mRotationPolicyListener);
} }
@Override @OnLifecycleEvent(ON_STOP)
public void onStop() { public void onStop() {
mContext.unregisterReceiver(mReceiver); mContext.unregisterReceiver(mReceiver);
if (mRotationPolicyListener != null) {
RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
mRotationPolicyListener = null;
}
} }
@Override @Override
public boolean isChecked() { public boolean isChecked() {
return hasSufficientPermission(mContext) && !isCameraLocked() && !isPowerSaveMode() return !RotationPolicy.isRotationLocked(mContext) && hasSufficientPermission(mContext)
&& Settings.Secure.getInt(mContext.getContentResolver(), && !isCameraLocked() && !isPowerSaveMode() && Settings.Secure.getInt(
mContext.getContentResolver(),
CAMERA_AUTOROTATE, 0) == 1; CAMERA_AUTOROTATE, 0) == 1;
} }

View File

@@ -15,13 +15,11 @@
*/ */
package com.android.settings.display; package com.android.settings.display;
import static com.android.settings.display.SmartAutoRotateController.hasSufficientPermission;
import static com.android.settings.display.SmartAutoRotateController.isRotationResolverServiceAvailable; import static com.android.settings.display.SmartAutoRotateController.isRotationResolverServiceAvailable;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.hardware.SensorPrivacyManager; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerManager;
import android.text.Html; import android.text.Html;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@@ -49,16 +47,19 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
private static final String TAG = "SmartAutoRotatePreferenceFragment"; private static final String TAG = "SmartAutoRotatePreferenceFragment";
private RotationPolicy.RotationPolicyListener mRotationPolicyListener; private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
private SensorPrivacyManager mPrivacyManager;
private AutoRotateSwitchBarController mSwitchBarController; private AutoRotateSwitchBarController mSwitchBarController;
private PowerManager mPowerManager;
private static final String FACE_SWITCH_PREFERENCE_ID = "face_based_rotate";
@Override @Override
protected int getPreferenceScreenResId() { protected int getPreferenceScreenResId() {
return R.xml.auto_rotate_settings; return R.xml.auto_rotate_settings;
} }
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(SmartAutoRotateController.class).init(getLifecycle());
}
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
@@ -70,8 +71,6 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
switchBar.show(); switchBar.show();
mSwitchBarController = new AutoRotateSwitchBarController(activity, switchBar, mSwitchBarController = new AutoRotateSwitchBarController(activity, switchBar,
getSettingsLifecycle()); getSettingsLifecycle());
mPrivacyManager = SensorPrivacyManager.getInstance(activity);
mPowerManager = getSystemService(PowerManager.class);
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),
@@ -89,14 +88,6 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
@Override @Override
public void onChange() { public void onChange() {
mSwitchBarController.onChange(); mSwitchBarController.onChange();
final boolean isLocked = RotationPolicy.isRotationLocked(getContext());
final boolean isCameraLocked = mPrivacyManager.isSensorPrivacyEnabled(
SensorPrivacyManager.Sensors.CAMERA);
final boolean isBatterySaver = mPowerManager.isPowerSaveMode();
final Preference preference = findPreference(FACE_SWITCH_PREFERENCE_ID);
if (preference != null && hasSufficientPermission(getContext())) {
preference.setEnabled(!isLocked && !isCameraLocked && !isBatterySaver);
}
} }
}; };
} }