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