blob: a9716d5042d91f059ee5af48cb0ea9b4ed03718b [file] [log] [blame]
Mill Chendce601d2019-06-10 21:26:22 +08001/*
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
17package com.android.settings.accessibility;
18
19import android.content.Context;
20
21import androidx.preference.Preference;
22import androidx.preference.PreferenceScreen;
23
24import com.android.internal.view.RotationPolicy;
25import com.android.internal.view.RotationPolicy.RotationPolicyListener;
26import com.android.settings.core.TogglePreferenceController;
27import com.android.settingslib.core.lifecycle.LifecycleObserver;
Mill Chen2e3d08e2019-06-21 21:03:52 +080028import com.android.settingslib.core.lifecycle.events.OnStart;
29import com.android.settingslib.core.lifecycle.events.OnStop;
Mill Chendce601d2019-06-10 21:26:22 +080030
31public class LockScreenRotationPreferenceController extends TogglePreferenceController implements
Mill Chen2e3d08e2019-06-21 21:03:52 +080032 LifecycleObserver, OnStart, OnStop {
Mill Chendce601d2019-06-10 21:26:22 +080033
34 private Preference mPreference;
35 private RotationPolicyListener mRotationPolicyListener;
36
37 public LockScreenRotationPreferenceController(Context context, String preferenceKey) {
38 super(context, preferenceKey);
39 }
40
41 /**
42 * Returns true if rotation lock is enabled.
43 */
44 @Override
45 public boolean isChecked() {
46 return !RotationPolicy.isRotationLocked(mContext);
47 }
48
49 /**
50 * Enables or disables screen rotation lock from Accessibility settings. If rotation is locked
51 * for accessibility, the toggle in Display settings is hidden to avoid confusion.
52 */
53 @Override
54 public boolean setChecked(boolean isChecked) {
55 RotationPolicy.setRotationLockForAccessibility(mContext, !isChecked);
56 return true;
57 }
58
59 @Override
60 public int getAvailabilityStatus() {
61 return RotationPolicy.isRotationSupported(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
62 }
63
64 @Override
Mill Chen2e3d08e2019-06-21 21:03:52 +080065 public void onStop() {
Mill Chendce601d2019-06-10 21:26:22 +080066 if (mRotationPolicyListener != null) {
67 RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener);
68 }
69 }
70
71 @Override
Mill Chen2e3d08e2019-06-21 21:03:52 +080072 public void onStart() {
Mill Chendce601d2019-06-10 21:26:22 +080073 if (mRotationPolicyListener == null) {
74 mRotationPolicyListener = new RotationPolicyListener() {
75 @Override
76 public void onChange() {
77 if (mPreference != null) {
78 updateState(mPreference);
79 }
80 }
81 };
82 }
83 RotationPolicy.registerRotationPolicyListener(mContext, mRotationPolicyListener);
84 }
85
86 @Override
87 public void displayPreference(PreferenceScreen screen) {
88 mPreference = screen.findPreference(getPreferenceKey());
89 super.displayPreference(screen);
90 }
91}