1 /* 2 * Copyright (C) 2022 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 static androidx.lifecycle.Lifecycle.Event.ON_START; 20 import static androidx.lifecycle.Lifecycle.Event.ON_STOP; 21 22 import android.app.settings.SettingsEnums; 23 import android.content.Context; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.lifecycle.Lifecycle; 27 import androidx.lifecycle.LifecycleObserver; 28 import androidx.lifecycle.OnLifecycleEvent; 29 import androidx.preference.PreferenceScreen; 30 import androidx.preference.SwitchPreference; 31 32 import com.android.settings.R; 33 import com.android.settings.core.TogglePreferenceController; 34 import com.android.settings.overlay.FeatureFactory; 35 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 36 import com.android.settingslib.devicestate.DeviceStateRotationLockSettingsManager; 37 import com.android.settingslib.search.SearchIndexableRaw; 38 39 import java.util.List; 40 41 /** Controller for device state based auto rotation preferences. */ 42 public class DeviceStateAutoRotateSettingController extends TogglePreferenceController implements 43 LifecycleObserver { 44 45 private SwitchPreference mPreference; 46 47 private final DeviceStateRotationLockSettingsManager mAutoRotateSettingsManager; 48 private final int mOrder; 49 private final DeviceStateRotationLockSettingsManager.DeviceStateRotationLockSettingsListener 50 mDeviceStateRotationLockSettingsListener = () -> updateState(mPreference); 51 private final int mDeviceState; 52 private final String mDeviceStateDescription; 53 private final MetricsFeatureProvider mMetricsFeatureProvider; 54 55 @VisibleForTesting DeviceStateAutoRotateSettingController(Context context, int deviceState, String deviceStateDescription, int order, MetricsFeatureProvider metricsFeatureProvider)56 DeviceStateAutoRotateSettingController(Context context, int deviceState, 57 String deviceStateDescription, int order, 58 MetricsFeatureProvider metricsFeatureProvider) { 59 super(context, getPreferenceKeyForDeviceState(deviceState)); 60 mMetricsFeatureProvider = metricsFeatureProvider; 61 mDeviceState = deviceState; 62 mDeviceStateDescription = deviceStateDescription; 63 mAutoRotateSettingsManager = DeviceStateRotationLockSettingsManager.getInstance(context); 64 mOrder = order; 65 } 66 DeviceStateAutoRotateSettingController(Context context, int deviceState, String deviceStateDescription, int order)67 public DeviceStateAutoRotateSettingController(Context context, int deviceState, 68 String deviceStateDescription, int order) { 69 this(context, deviceState, deviceStateDescription, order, 70 FeatureFactory.getFactory(context).getMetricsFeatureProvider()); 71 } 72 init(Lifecycle lifecycle)73 void init(Lifecycle lifecycle) { 74 lifecycle.addObserver(this); 75 } 76 77 @OnLifecycleEvent(ON_START) onStart()78 void onStart() { 79 mAutoRotateSettingsManager.registerListener(mDeviceStateRotationLockSettingsListener); 80 } 81 82 @OnLifecycleEvent(ON_STOP) onStop()83 void onStop() { 84 mAutoRotateSettingsManager.unregisterListener(mDeviceStateRotationLockSettingsListener); 85 } 86 87 @Override displayPreference(PreferenceScreen screen)88 public void displayPreference(PreferenceScreen screen) { 89 mPreference = new SwitchPreference(mContext); 90 mPreference.setTitle(mDeviceStateDescription); 91 mPreference.setKey(getPreferenceKey()); 92 mPreference.setOrder(mOrder); 93 screen.addPreference(mPreference); 94 super.displayPreference(screen); 95 } 96 97 @Override getAvailabilityStatus()98 public int getAvailabilityStatus() { 99 return DeviceStateAutoRotationHelper.isDeviceStateRotationEnabled(mContext) 100 ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 101 } 102 103 @Override getPreferenceKey()104 public String getPreferenceKey() { 105 return getPreferenceKeyForDeviceState(mDeviceState); 106 } 107 getPreferenceKeyForDeviceState(int deviceState)108 private static String getPreferenceKeyForDeviceState(int deviceState) { 109 return "auto_rotate_device_state_" + deviceState; 110 } 111 112 @Override isChecked()113 public boolean isChecked() { 114 return !mAutoRotateSettingsManager.isRotationLocked(mDeviceState); 115 } 116 117 @Override setChecked(boolean isChecked)118 public boolean setChecked(boolean isChecked) { 119 boolean isRotationLocked = !isChecked; 120 logSettingChanged(isChecked); 121 mAutoRotateSettingsManager.updateSetting(mDeviceState, isRotationLocked); 122 return true; 123 } 124 logSettingChanged(boolean isChecked)125 private void logSettingChanged(boolean isChecked) { 126 boolean isRotationLocked = !isChecked; 127 mMetricsFeatureProvider.action(mContext, SettingsEnums.ACTION_ROTATION_LOCK, 128 isRotationLocked); 129 130 int actionCategory = isChecked 131 ? SettingsEnums.ACTION_ENABLE_AUTO_ROTATION_DEVICE_STATE 132 : SettingsEnums.ACTION_DISABLE_AUTO_ROTATION_DEVICE_STATE; 133 mMetricsFeatureProvider.action(mContext, actionCategory, /* value= */ mDeviceState); 134 } 135 136 @Override updateRawDataToIndex(List<SearchIndexableRaw> rawData)137 public void updateRawDataToIndex(List<SearchIndexableRaw> rawData) { 138 SearchIndexableRaw indexable = new SearchIndexableRaw(mContext); 139 indexable.key = getPreferenceKey(); 140 indexable.title = mDeviceStateDescription; 141 // Maybe pass screen title as param? 142 indexable.screenTitle = mContext.getString(R.string.accelerometer_title); 143 rawData.add(indexable); 144 } 145 146 @Override getSliceHighlightMenuRes()147 public int getSliceHighlightMenuRes() { 148 return R.string.menu_key_display; 149 } 150 151 @Override isSliceable()152 public boolean isSliceable() { 153 return true; // Maybe set to false if in accessibility settings screen 154 } 155 156 @Override isPublicSlice()157 public boolean isPublicSlice() { 158 return true; 159 } 160 } 161