Mill Chen | dce601d | 2019-06-10 21:26:22 +0800 | [diff] [blame] | 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.core.TogglePreferenceController; |
| 27 | import com.android.settingslib.core.lifecycle.LifecycleObserver; |
Mill Chen | 2e3d08e | 2019-06-21 21:03:52 +0800 | [diff] [blame] | 28 | import com.android.settingslib.core.lifecycle.events.OnStart; |
| 29 | import com.android.settingslib.core.lifecycle.events.OnStop; |
Mill Chen | dce601d | 2019-06-10 21:26:22 +0800 | [diff] [blame] | 30 | |
| 31 | public class LockScreenRotationPreferenceController extends TogglePreferenceController implements |
Mill Chen | 2e3d08e | 2019-06-21 21:03:52 +0800 | [diff] [blame] | 32 | LifecycleObserver, OnStart, OnStop { |
Mill Chen | dce601d | 2019-06-10 21:26:22 +0800 | [diff] [blame] | 33 | |
| 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 Chen | 2e3d08e | 2019-06-21 21:03:52 +0800 | [diff] [blame] | 65 | public void onStop() { |
Mill Chen | dce601d | 2019-06-10 21:26:22 +0800 | [diff] [blame] | 66 | if (mRotationPolicyListener != null) { |
| 67 | RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Override |
Mill Chen | 2e3d08e | 2019-06-21 21:03:52 +0800 | [diff] [blame] | 72 | public void onStart() { |
Mill Chen | dce601d | 2019-06-10 21:26:22 +0800 | [diff] [blame] | 73 | 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 | } |