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; 28 import com.android.settingslib.core.lifecycle.events.OnStart; 29 import com.android.settingslib.core.lifecycle.events.OnStop; 30 31 public class LockScreenRotationPreferenceController extends TogglePreferenceController implements 32 LifecycleObserver, OnStart, OnStop { 33 34 private Preference mPreference; 35 private RotationPolicyListener mRotationPolicyListener; 36 LockScreenRotationPreferenceController(Context context, String preferenceKey)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 isChecked()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 setChecked(boolean isChecked)54 public boolean setChecked(boolean isChecked) { 55 RotationPolicy.setRotationLockForAccessibility(mContext, !isChecked); 56 return true; 57 } 58 59 @Override getAvailabilityStatus()60 public int getAvailabilityStatus() { 61 return RotationPolicy.isRotationSupported(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 62 } 63 64 @Override onStop()65 public void onStop() { 66 if (mRotationPolicyListener != null) { 67 RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener); 68 } 69 } 70 71 @Override onStart()72 public void onStart() { 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 displayPreference(PreferenceScreen screen)87 public void displayPreference(PreferenceScreen screen) { 88 mPreference = screen.findPreference(getPreferenceKey()); 89 super.displayPreference(screen); 90 } 91 } 92