• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.setRotationLockForAccessibility(mContext, !isChecked);
58         return true;
59     }
60 
61     @Override
getAvailabilityStatus()62     public int getAvailabilityStatus() {
63         return RotationPolicy.isRotationSupported(mContext)
64                 && !DeviceStateAutoRotationHelper.isDeviceStateRotationEnabledForA11y(mContext)
65                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
66     }
67 
68     @Override
getSliceHighlightMenuRes()69     public int getSliceHighlightMenuRes() {
70         return R.string.menu_key_accessibility;
71     }
72 
73     @Override
onStop()74     public void onStop() {
75         if (mRotationPolicyListener != null) {
76             RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
77         }
78     }
79 
80     @Override
onStart()81     public void onStart() {
82         if (mRotationPolicyListener == null) {
83             mRotationPolicyListener = new RotationPolicyListener() {
84                 @Override
85                 public void onChange() {
86                     if (mPreference != null) {
87                         updateState(mPreference);
88                     }
89                 }
90             };
91         }
92         RotationPolicy.registerRotationPolicyListener(mContext, mRotationPolicyListener);
93     }
94 
95     @Override
displayPreference(PreferenceScreen screen)96     public void displayPreference(PreferenceScreen screen) {
97         mPreference = screen.findPreference(getPreferenceKey());
98         super.displayPreference(screen);
99     }
100 }
101