1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.accessibility;
18 
19 import android.content.Context;
20 
21 import androidx.preference.Preference;
22 import androidx.preference.PreferenceScreen;
23 
24 import com.android.internal.view.RotationPolicy;
25 import com.android.internal.view.RotationPolicy.RotationPolicyListener;
26 import com.android.settings.R;
27 import com.android.settings.core.TogglePreferenceController;
28 import com.android.settings.display.DeviceStateAutoRotationHelper;
29 import com.android.settingslib.core.lifecycle.LifecycleObserver;
30 import com.android.settingslib.core.lifecycle.events.OnStart;
31 import com.android.settingslib.core.lifecycle.events.OnStop;
32 
33 public class LockScreenRotationPreferenceController extends TogglePreferenceController implements
34         LifecycleObserver, OnStart, OnStop {
35 
36     private Preference mPreference;
37     private RotationPolicyListener mRotationPolicyListener;
38 
LockScreenRotationPreferenceController(Context context, String preferenceKey)39     public LockScreenRotationPreferenceController(Context context, String preferenceKey) {
40         super(context, preferenceKey);
41     }
42 
43     /**
44      * Returns true if rotation lock is enabled.
45      */
46     @Override
isChecked()47     public boolean isChecked() {
48         return !RotationPolicy.isRotationLocked(mContext);
49     }
50 
51     /**
52      * Enables or disables screen rotation lock from Accessibility settings. If rotation is locked
53      * for accessibility, the toggle in Display settings is hidden to avoid confusion.
54      */
55     @Override
setChecked(boolean isChecked)56     public boolean setChecked(boolean isChecked) {
57         RotationPolicy.setRotationLock(mContext, !isChecked,
58                 /* caller= */ "LockScreenRotationPreferenceController#setChecked");
59         return true;
60     }
61 
62     @Override
getAvailabilityStatus()63     public int getAvailabilityStatus() {
64         return RotationPolicy.isRotationSupported(mContext)
65                 && !DeviceStateAutoRotationHelper.isDeviceStateRotationEnabledForA11y(mContext)
66                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
67     }
68 
69     @Override
getSliceHighlightMenuRes()70     public int getSliceHighlightMenuRes() {
71         return R.string.menu_key_accessibility;
72     }
73 
74     @Override
onStop()75     public void onStop() {
76         if (mRotationPolicyListener != null) {
77             RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
78         }
79     }
80 
81     @Override
onStart()82     public void onStart() {
83         if (mRotationPolicyListener == null) {
84             mRotationPolicyListener = new RotationPolicyListener() {
85                 @Override
86                 public void onChange() {
87                     if (mPreference != null) {
88                         updateState(mPreference);
89                     }
90                 }
91             };
92         }
93         RotationPolicy.registerRotationPolicyListener(mContext, mRotationPolicyListener);
94     }
95 
96     @Override
displayPreference(PreferenceScreen screen)97     public void displayPreference(PreferenceScreen screen) {
98         mPreference = screen.findPreference(getPreferenceKey());
99         super.displayPreference(screen);
100     }
101 }
102