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 com.android.settingslib.devicestate.DeviceStateAutoRotateSettingUtils.isDeviceStateRotationLockEnabled; 20 21 import android.content.Context; 22 import android.util.Log; 23 24 import androidx.lifecycle.Lifecycle; 25 26 import com.android.internal.view.RotationPolicy; 27 import com.android.settings.R; 28 import com.android.settings.core.BasePreferenceController; 29 import com.android.settingslib.core.AbstractPreferenceController; 30 import com.android.settingslib.devicestate.SettableDeviceState; 31 import com.android.settingslib.search.SearchIndexableRaw; 32 33 import com.google.common.collect.ImmutableList; 34 35 import java.util.ArrayList; 36 import java.util.List; 37 38 /** 39 * Helper class with utility methods related to device state auto-rotation that can be used in 40 * auto-rotation settings fragments and controllers. 41 */ 42 public class DeviceStateAutoRotationHelper { 43 44 private static final String TAG = "DeviceStateAutoRotHelpr"; 45 initControllers(Lifecycle lifecycle, List<DeviceStateAutoRotateSettingController> controllers)46 static void initControllers(Lifecycle lifecycle, 47 List<DeviceStateAutoRotateSettingController> controllers) { 48 for (DeviceStateAutoRotateSettingController controller : controllers) { 49 controller.init(lifecycle); 50 } 51 } 52 createPreferenceControllers( Context context)53 static ImmutableList<AbstractPreferenceController> createPreferenceControllers( 54 Context context) { 55 List<SettableDeviceState> settableDeviceStates = DeviceStateAutoRotateSettingManagerProvider 56 .getSingletonInstance(context).getSettableDeviceStates(); 57 int numDeviceStates = settableDeviceStates.size(); 58 if (numDeviceStates == 0) { 59 return ImmutableList.of(); 60 } 61 String[] deviceStateSettingDescriptions = context.getResources().getStringArray( 62 R.array.config_settableAutoRotationDeviceStatesDescriptions); 63 if (numDeviceStates != deviceStateSettingDescriptions.length) { 64 Log.wtf(TAG, 65 "Mismatch between number of device states and device states descriptions."); 66 return ImmutableList.of(); 67 } 68 69 ImmutableList.Builder<AbstractPreferenceController> controllers = 70 ImmutableList.builderWithExpectedSize(numDeviceStates); 71 for (int i = 0; i < numDeviceStates; i++) { 72 SettableDeviceState settableDeviceState = settableDeviceStates.get(i); 73 if (!settableDeviceState.isSettable()) { 74 continue; 75 } 76 // Preferences with a lower order will be showed first. Here we go below 0 to make sure 77 // we are shown before statically declared preferences in XML. 78 int order = -numDeviceStates + i; 79 controllers.add(new DeviceStateAutoRotateSettingController( 80 context, 81 settableDeviceState.getDeviceState(), 82 deviceStateSettingDescriptions[i], 83 order 84 )); 85 } 86 return controllers.build(); 87 } 88 getRawDataToIndex( Context context, boolean enabled)89 static List<SearchIndexableRaw> getRawDataToIndex( 90 Context context, boolean enabled) { 91 // Check what the "enabled" param is for 92 List<AbstractPreferenceController> controllers = createPreferenceControllers(context); 93 List<SearchIndexableRaw> rawData = new ArrayList<>(); 94 for (AbstractPreferenceController controller : controllers) { 95 ((BasePreferenceController) controller).updateRawDataToIndex(rawData); 96 } 97 return rawData; 98 } 99 100 /** Returns whether the device state based auto-rotation settings are enabled. */ isDeviceStateRotationEnabled(Context context)101 public static boolean isDeviceStateRotationEnabled(Context context) { 102 return RotationPolicy.isRotationLockToggleVisible(context) 103 && isDeviceStateRotationLockEnabled(context); 104 } 105 106 /** 107 * Returns whether the device state based auto-rotation settings are enabled for the 108 * accessibility settings page. 109 */ isDeviceStateRotationEnabledForA11y(Context context)110 public static boolean isDeviceStateRotationEnabledForA11y(Context context) { 111 return RotationPolicy.isRotationSupported(context) 112 && isDeviceStateRotationLockEnabled(context); 113 } 114 } 115