Debian Codegraph @

libs/WindowManager/Jetpack/src/androidx/window/common/DeviceStateManagerFoldingFeatureProducer.java

raw symbols SCIP
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 androidx.window.common;
18
19 import static android.hardware.devicestate.DeviceStateManager.INVALID_DEVICE_STATE;
20
21 import static androidx.window.common.CommonFoldingFeature.COMMON_STATE_UNKNOWN;
22 import static androidx.window.common.CommonFoldingFeature.parseListFromString;
23
24 import android.annotation.NonNull;
25 import android.content.Context;
26 import android.hardware.devicestate.DeviceStateManager;
27 import android.hardware.devicestate.DeviceStateManager.DeviceStateCallback;
28 import android.text.TextUtils;
29 import android.util.Log;
30 import android.util.SparseIntArray;
31
32 import androidx.window.util.AcceptOnceConsumer;
33 import androidx.window.util.BaseDataProducer;
34
35 import com.android.internal.R;
36
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Objects;
40 import java.util.Optional;
41 import java.util.Set;
42 import java.util.function.Consumer;
43
44 /**
45 * An implementation of {@link androidx.window.util.BaseDataProducer} that returns
46 * the device's posture by mapping the state returned from {@link DeviceStateManager} to
47 * values provided in the resources' config at {@link R.array#config_device_state_postures}.
48 */
49 public final class DeviceStateManagerFoldingFeatureProducer
50 extends BaseDataProducer<List<CommonFoldingFeature>> {
51 private static final String TAG =
52 DeviceStateManagerFoldingFeatureProducer.class.getSimpleName();
53 private static final boolean DEBUG = false;
54
55 private final SparseIntArray mDeviceStateToPostureMap = new SparseIntArray();
56
57 private int mCurrentDeviceState = INVALID_DEVICE_STATE;
58
59 @NonNull
60 private final BaseDataProducer<String> mRawFoldSupplier;
61
62 public DeviceStateManagerFoldingFeatureProducer(@NonNull Context context,
63 @NonNull BaseDataProducer<String> rawFoldSupplier) {
64 mRawFoldSupplier = rawFoldSupplier;
65 String[] deviceStatePosturePairs = context.getResources()
66 .getStringArray(R.array.config_device_state_postures);
67 for (String deviceStatePosturePair : deviceStatePosturePairs) {
68 String[] deviceStatePostureMapping = deviceStatePosturePair.split(":");
69 if (deviceStatePostureMapping.length != 2) {
70 if (DEBUG) {
71 Log.e(TAG, "Malformed device state posture pair: "
72 + deviceStatePosturePair);
73 }
74 continue;
75 }
76
77 int deviceState;
78 int posture;
79 try {
80 deviceState = Integer.parseInt(deviceStatePostureMapping[0]);
81 posture = Integer.parseInt(deviceStatePostureMapping[1]);
82 } catch (NumberFormatException e) {
83 if (DEBUG) {
84 Log.e(TAG, "Failed to parse device state or posture: "
85 + deviceStatePosturePair,
86 e);
87 }
88 continue;
89 }
90
91 mDeviceStateToPostureMap.put(deviceState, posture);
92 }
93
94 if (mDeviceStateToPostureMap.size() > 0) {
95 DeviceStateCallback deviceStateCallback = (state) -> {
96 mCurrentDeviceState = state;
97 mRawFoldSupplier.getData(this::notifyFoldingFeatureChange);
98 };
99 Objects.requireNonNull(context.getSystemService(DeviceStateManager.class))
100 .registerCallback(context.getMainExecutor(), deviceStateCallback);
101 }
102 }
103
104 /**
105 * Add a callback to mCallbacks if there is no device state. This callback will be run
106 * once a device state is set. Otherwise,run the callback immediately.
107 */
108 private void runCallbackWhenValidState(@NonNull Consumer<List<CommonFoldingFeature>> callback,
109 String displayFeaturesString) {
110 if (isCurrentStateValid()) {
111 callback.accept(calculateFoldingFeature(displayFeaturesString));
112 } else {
113 // This callback will be added to mCallbacks and removed once it runs once.
114 AcceptOnceConsumer<List<CommonFoldingFeature>> singleRunCallback =
115 new AcceptOnceConsumer<>(this, callback);
116 addDataChangedCallback(singleRunCallback);
117 }
118 }
119
120 /**
121 * Checks to find {@link DeviceStateManagerFoldingFeatureProducer#mCurrentDeviceState} in the
122 * {@link DeviceStateManagerFoldingFeatureProducer#mDeviceStateToPostureMap} which was
123 * initialized in the constructor of {@link DeviceStateManagerFoldingFeatureProducer}.
124 * Returns a boolean value of whether the device state is valid.
125 */
126 private boolean isCurrentStateValid() {
127 // If the device state is not found in the map, indexOfKey returns a negative number.
128 return mDeviceStateToPostureMap.indexOfKey(mCurrentDeviceState) >= 0;
129 }
130
131 @Override
132 protected void onListenersChanged(
133 @NonNull Set<Consumer<List<CommonFoldingFeature>>> callbacks) {
134 super.onListenersChanged(callbacks);
135 if (callbacks.isEmpty()) {
136 mCurrentDeviceState = INVALID_DEVICE_STATE;
137 mRawFoldSupplier.removeDataChangedCallback(this::notifyFoldingFeatureChange);
138 } else {
139 mRawFoldSupplier.addDataChangedCallback(this::notifyFoldingFeatureChange);
140 }
141 }
142
143 @NonNull
144 @Override
145 public Optional<List<CommonFoldingFeature>> getCurrentData() {
146 Optional<String> displayFeaturesString = mRawFoldSupplier.getCurrentData();
147 if (!isCurrentStateValid()) {
148 return Optional.empty();
149 } else {
150 return displayFeaturesString.map(this::calculateFoldingFeature);
151 }
152 }
153
154 /**
155 * Adds the data to the storeFeaturesConsumer when the data is ready.
156 * @param storeFeaturesConsumer a consumer to collect the data when it is first available.
157 */
158 @Override
159 public void getData(Consumer<List<CommonFoldingFeature>> storeFeaturesConsumer) {
160 mRawFoldSupplier.getData((String displayFeaturesString) -> {
161 if (TextUtils.isEmpty(displayFeaturesString)) {
162 storeFeaturesConsumer.accept(new ArrayList<>());
163 } else {
164 runCallbackWhenValidState(storeFeaturesConsumer, displayFeaturesString);
165 }
166 });
167 }
168
169 private void notifyFoldingFeatureChange(String displayFeaturesString) {
170 if (!isCurrentStateValid()) {
171 return;
172 }
173 if (TextUtils.isEmpty(displayFeaturesString)) {
174 notifyDataChanged(new ArrayList<>());
175 } else {
176 notifyDataChanged(calculateFoldingFeature(displayFeaturesString));
177 }
178 }
179
180 private List<CommonFoldingFeature> calculateFoldingFeature(String displayFeaturesString) {
181 final int globalHingeState = globalHingeState();
182 return parseListFromString(displayFeaturesString, globalHingeState);
183 }
184
185 private int globalHingeState() {
186 return mDeviceStateToPostureMap.get(mCurrentDeviceState, COMMON_STATE_UNKNOWN);
187 }
188 }