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.R;
24 import com.android.settings.core.PreferenceControllerMixin;
25 import com.android.settings.core.TogglePreferenceController;
26 import com.android.settings.overlay.FeatureFactory;
27 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
28 import com.android.settingslib.core.lifecycle.LifecycleObserver;
29 import com.android.settingslib.core.lifecycle.events.OnPause;
30 import com.android.settingslib.core.lifecycle.events.OnResume;
31 
32 public class AutoRotatePreferenceController extends TogglePreferenceController implements
33         PreferenceControllerMixin, Preference.OnPreferenceChangeListener, LifecycleObserver,
34         OnResume, OnPause {
35 
36     private final MetricsFeatureProvider mMetricsFeatureProvider;
37     private Preference mPreference;
38     private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
39 
AutoRotatePreferenceController(Context context, String key)40     public AutoRotatePreferenceController(Context context, String key) {
41         super(context, key);
42         mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
43     }
44 
45     @Override
updateState(Preference preference)46     public void updateState(Preference preference) {
47         mPreference = preference;
48         super.updateState(preference);
49     }
50 
51     @Override
onResume()52     public void onResume() {
53         if (mRotationPolicyListener == null) {
54             mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() {
55                 @Override
56                 public void onChange() {
57                     if (mPreference != null) {
58                         updateState(mPreference);
59                     }
60                 }
61             };
62         }
63         RotationPolicy.registerRotationPolicyListener(mContext,
64                 mRotationPolicyListener);
65     }
66 
67     @Override
onPause()68     public void onPause() {
69         if (mRotationPolicyListener != null) {
70             RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
71         }
72     }
73 
74     @Override
getAvailabilityStatus()75     public int getAvailabilityStatus() {
76         return RotationPolicy.isRotationLockToggleVisible(mContext)
77                 && !DeviceStateAutoRotationHelper.isDeviceStateRotationEnabled(mContext)
78                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
79     }
80 
81     @Override
isSliceable()82     public boolean isSliceable() {
83         return TextUtils.equals(getPreferenceKey(), "auto_rotate");
84     }
85 
86     @Override
isPublicSlice()87     public boolean isPublicSlice() {
88         return true;
89     }
90 
91     @Override
getSliceHighlightMenuRes()92     public int getSliceHighlightMenuRes() {
93         return R.string.menu_key_display;
94     }
95 
96     @Override
isChecked()97     public boolean isChecked() {
98         return !RotationPolicy.isRotationLocked(mContext);
99     }
100 
101     @Override
setChecked(boolean isChecked)102     public boolean setChecked(boolean isChecked) {
103         final boolean isLocked = !isChecked;
104         mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ROTATION_LOCK,
105                 isLocked);
106         RotationPolicy.setRotationLock(mContext, isLocked,
107                 /* caller= */ "AutoRotatePreferenceController#setChecked");
108         return true;
109     }
110 }
111