1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 package com.android.settings.display; 15 16 import android.app.settings.SettingsEnums; 17 import android.content.Context; 18 import android.text.TextUtils; 19 20 import androidx.preference.Preference; 21 22 import com.android.internal.view.RotationPolicy; 23 import com.android.settings.core.PreferenceControllerMixin; 24 import com.android.settings.core.TogglePreferenceController; 25 import com.android.settings.overlay.FeatureFactory; 26 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 27 import com.android.settingslib.core.lifecycle.LifecycleObserver; 28 import com.android.settingslib.core.lifecycle.events.OnPause; 29 import com.android.settingslib.core.lifecycle.events.OnResume; 30 31 public class AutoRotatePreferenceController extends TogglePreferenceController implements 32 PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver, 33 OnResume, OnPause { 34 35 private final MetricsFeatureProvider mMetricsFeatureProvider; 36 private Preference mPreference; 37 private RotationPolicy.RotationPolicyListener mRotationPolicyListener; 38 AutoRotatePreferenceController(Context context, String key)39 public AutoRotatePreferenceController(Context context, String key) { 40 super(context, key); 41 mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); 42 } 43 44 @Override updateState(Preference preference)45 public void updateState(Preference preference) { 46 mPreference = preference; 47 super.updateState(preference); 48 } 49 50 @Override onResume()51 public void onResume() { 52 if (mRotationPolicyListener == null) { 53 mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() { 54 @Override 55 public void onChange() { 56 if (mPreference != null) { 57 updateState(mPreference); 58 } 59 } 60 }; 61 } 62 RotationPolicy.registerRotationPolicyListener(mContext, 63 mRotationPolicyListener); 64 } 65 66 @Override onPause()67 public void onPause() { 68 if (mRotationPolicyListener != null) { 69 RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener); 70 } 71 } 72 73 @Override getAvailabilityStatus()74 public int getAvailabilityStatus() { 75 return RotationPolicy.isRotationLockToggleVisible(mContext) 76 ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 77 } 78 79 @Override isSliceable()80 public boolean isSliceable() { 81 return TextUtils.equals(getPreferenceKey(), "auto_rotate"); 82 } 83 84 @Override isChecked()85 public boolean isChecked() { 86 return !RotationPolicy.isRotationLocked(mContext); 87 } 88 89 @Override setChecked(boolean isChecked)90 public boolean setChecked(boolean isChecked) { 91 final boolean isLocked = !isChecked; 92 mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ROTATION_LOCK, 93 isLocked); 94 RotationPolicy.setRotationLock(mContext, isLocked); 95 return true; 96 } 97 } 98