1 /* 2 * Copyright (C) 2021 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.display; 18 19 import android.app.settings.SettingsEnums; 20 import android.content.Context; 21 22 import com.android.internal.view.RotationPolicy; 23 import com.android.settings.R; 24 import com.android.settings.overlay.FeatureFactory; 25 import com.android.settings.widget.SettingsMainSwitchPreferenceController; 26 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 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 /** 32 * The main switch controller for auto-rotate. 33 */ 34 public class AutoRotateSwitchBarController extends SettingsMainSwitchPreferenceController implements 35 LifecycleObserver, OnStart, OnStop { 36 37 private final MetricsFeatureProvider mMetricsFeatureProvider; 38 private RotationPolicy.RotationPolicyListener mRotationPolicyListener; 39 AutoRotateSwitchBarController(Context context, String key)40 public AutoRotateSwitchBarController(Context context, String key) { 41 super(context, key); 42 mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider(); 43 } 44 45 @Override getAvailabilityStatus()46 public int getAvailabilityStatus() { 47 return RotationPolicy.isRotationLockToggleVisible(mContext) 48 && !DeviceStateAutoRotationHelper.isDeviceStateRotationEnabled(mContext) 49 ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 50 } 51 52 @Override onStart()53 public void onStart() { 54 if (mRotationPolicyListener == null) { 55 mRotationPolicyListener = new RotationPolicy.RotationPolicyListener() { 56 @Override 57 public void onChange() { 58 if (mSwitchPreference != null) { 59 updateState(mSwitchPreference); 60 } 61 } 62 }; 63 } 64 RotationPolicy.registerRotationPolicyListener(mContext, 65 mRotationPolicyListener); 66 } 67 68 @Override onStop()69 public void onStop() { 70 if (mRotationPolicyListener != null) { 71 RotationPolicy.unregisterRotationPolicyListener(mContext, mRotationPolicyListener); 72 } 73 } 74 75 @Override isChecked()76 public boolean isChecked() { 77 return !RotationPolicy.isRotationLocked(mContext); 78 } 79 80 @Override setChecked(boolean isChecked)81 public boolean setChecked(boolean isChecked) { 82 final boolean isLocked = !isChecked; 83 mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ROTATE_ROTATE_MASTER_TOGGLE, 84 isLocked); 85 RotationPolicy.setRotationLock(mContext, isLocked, 86 /* caller= */ "AutoRotateSwitchBarController#setChecked"); 87 return true; 88 } 89 90 @Override getSliceHighlightMenuRes()91 public int getSliceHighlightMenuRes() { 92 return R.string.menu_key_display; 93 } 94 95 } 96 97