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.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.app.settings.SettingsEnums;
import android.content.BroadcastReceiver;
@@ -32,7 +35,9 @@ import android.provider.Settings;
import android.service.rotationresolver.RotationResolverService;
import android.text.TextUtils;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@@ -41,14 +46,12 @@ import com.android.internal.view.RotationPolicy;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.overlay.FeatureFactory;
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
*/
public class SmartAutoRotateController extends TogglePreferenceController implements
Preference.OnPreferenceChangeListener, LifecycleObserver, OnStart, OnStop {
Preference.OnPreferenceChangeListener, LifecycleObserver {
private final MetricsFeatureProvider mMetricsFeatureProvider;
private final SensorPrivacyManager mPrivacyManager;
@@ -60,6 +63,7 @@ public class SmartAutoRotateController extends TogglePreferenceController implem
}
};
private Preference mPreference;
private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
public SmartAutoRotateController(Context context, String preferenceKey) {
super(context, preferenceKey);
@@ -70,6 +74,10 @@ public class SmartAutoRotateController extends TogglePreferenceController implem
mPowerManager = context.getSystemService(PowerManager.class);
}
public void init(Lifecycle lifecycle) {
lifecycle.addObserver(this);
}
@Override
public int getAvailabilityStatus() {
if (!isRotationResolverServiceAvailable(mContext)) {
@@ -101,21 +109,35 @@ public class SmartAutoRotateController extends TogglePreferenceController implem
return mPowerManager.isPowerSaveMode();
}
@Override
@OnLifecycleEvent(ON_START)
public void onStart() {
mContext.registerReceiver(mReceiver,
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() {
mContext.unregisterReceiver(mReceiver);
if (mRotationPolicyListener != null) {
RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
mRotationPolicyListener = null;
}
}
@Override
public boolean isChecked() {
return hasSufficientPermission(mContext) && !isCameraLocked() && !isPowerSaveMode()
&& Settings.Secure.getInt(mContext.getContentResolver(),
return !RotationPolicy.isRotationLocked(mContext) && hasSufficientPermission(mContext)
&& !isCameraLocked() && !isPowerSaveMode() && Settings.Secure.getInt(
mContext.getContentResolver(),
CAMERA_AUTOROTATE, 0) == 1;
}

View File

@@ -15,13 +15,11 @@
*/
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.hardware.SensorPrivacyManager;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
@@ -49,16 +47,19 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
private static final String TAG = "SmartAutoRotatePreferenceFragment";
private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
private SensorPrivacyManager mPrivacyManager;
private AutoRotateSwitchBarController mSwitchBarController;
private PowerManager mPowerManager;
private static final String FACE_SWITCH_PREFERENCE_ID = "face_based_rotate";
@Override
protected int getPreferenceScreenResId() {
return R.xml.auto_rotate_settings;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(SmartAutoRotateController.class).init(getLifecycle());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@@ -70,8 +71,6 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
switchBar.show();
mSwitchBarController = new AutoRotateSwitchBarController(activity, switchBar,
getSettingsLifecycle());
mPrivacyManager = SensorPrivacyManager.getInstance(activity);
mPowerManager = getSystemService(PowerManager.class);
final Preference footerPreference = findPreference(FooterPreference.KEY_FOOTER);
if (footerPreference != null) {
footerPreference.setTitle(Html.fromHtml(getString(R.string.smart_rotate_text_headline),
@@ -89,14 +88,6 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
@Override
public void 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);
}
}
};
}