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.settingslib.core.lifecycle.LifecycleObserver;
29 import com.android.settingslib.core.lifecycle.events.OnStart;
30 import com.android.settingslib.core.lifecycle.events.OnStop;
31 
32 public class LockScreenRotationPreferenceController extends TogglePreferenceController implements
33         LifecycleObserver, OnStart, OnStop {
34 
35     private Preference mPreference;
36     private RotationPolicyListener mRotationPolicyListener;
37 
LockScreenRotationPreferenceController(Context context, String preferenceKey)38     public LockScreenRotationPreferenceController(Context context, String preferenceKey) {
39         super(context, preferenceKey);
40     }
41 
42     /**
43      * Returns true if rotation lock is enabled.
44      */
45     @Override
isChecked()46     public boolean isChecked() {
47         return !RotationPolicy.isRotationLocked(mContext);
48     }
49 
50     /**
51      * Enables or disables screen rotation lock from Accessibility settings. If rotation is locked
52      * for accessibility, the toggle in Display settings is hidden to avoid confusion.
53      */
54     @Override
setChecked(boolean isChecked)55     public boolean setChecked(boolean isChecked) {
56         RotationPolicy.setRotationLockForAccessibility(mContext, !isChecked);
57         return true;
58     }
59 
60     @Override
getAvailabilityStatus()61     public int getAvailabilityStatus() {
62         return RotationPolicy.isRotationSupported(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
63     }
64 
65     @Override
getSliceHighlightMenuRes()66     public int getSliceHighlightMenuRes() {
67         return R.string.menu_key_accessibility;
68     }
69 
70     @Override
onStop()71     public void onStop() {
72         if (mRotationPolicyListener != null) {
73             RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
74         }
75     }
76 
77     @Override
onStart()78     public void onStart() {
79         if (mRotationPolicyListener == null) {
80             mRotationPolicyListener = new RotationPolicyListener() {
81                 @Override
82                 public void onChange() {
83                     if (mPreference != null) {
84                         updateState(mPreference);
85                     }
86                 }
87             };
88         }
89         RotationPolicy.registerRotationPolicyListener(mContext, mRotationPolicyListener);
90     }
91 
92     @Override
displayPreference(PreferenceScreen screen)93     public void displayPreference(PreferenceScreen screen) {
94         mPreference = screen.findPreference(getPreferenceKey());
95         super.displayPreference(screen);
96     }
97 }
98