Debian Codegraph @

libs/WindowManager/Jetpack/src/androidx/window/extensions/area/WindowAreaComponentImpl.java

raw symbols SCIP
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 androidx.window.extensions.area;
18
19 import static android.hardware.devicestate.DeviceStateManager.INVALID_DEVICE_STATE;
20
21 import android.app.Activity;
22 import android.content.Context;
23 import android.hardware.devicestate.DeviceStateManager;
24 import android.hardware.devicestate.DeviceStateRequest;
25 import android.util.ArraySet;
26
27 import androidx.annotation.NonNull;
28
29 import com.android.internal.R;
30 import com.android.internal.annotations.GuardedBy;
31
32 import java.util.concurrent.Executor;
33 import java.util.function.Consumer;
34
35 /**
36 * Reference implementation of androidx.window.extensions.area OEM interface for use with
37 * WindowManager Jetpack.
38 *
39 * This component currently supports Rear Display mode with the ability to add and remove
40 * status listeners for this mode.
41 *
42 * The public methods in this class are thread-safe.
43 **/
44 public class WindowAreaComponentImpl implements WindowAreaComponent,
45 DeviceStateManager.DeviceStateCallback {
46
47 private final Object mLock = new Object();
48
49 private final DeviceStateManager mDeviceStateManager;
50 private final Executor mExecutor;
51
52 @GuardedBy("mLock")
53 private final ArraySet<Consumer<Integer>> mRearDisplayStatusListeners = new ArraySet<>();
54 private final int mRearDisplayState;
55 @WindowAreaSessionState
56 private int mRearDisplaySessionStatus = WindowAreaComponent.SESSION_STATE_INACTIVE;
57
58 @GuardedBy("mLock")
59 private int mCurrentDeviceState = INVALID_DEVICE_STATE;
60 @GuardedBy("mLock")
61 private int mCurrentDeviceBaseState = INVALID_DEVICE_STATE;
62 @GuardedBy("mLock")
63 private DeviceStateRequest mDeviceStateRequest;
64
65 public WindowAreaComponentImpl(@NonNull Context context) {
66 mDeviceStateManager = context.getSystemService(DeviceStateManager.class);
67 mExecutor = context.getMainExecutor();
68
69 // TODO(b/236022708) Move rear display state to device state config file
70 mRearDisplayState = context.getResources().getInteger(
71 R.integer.config_deviceStateRearDisplay);
72
73 mDeviceStateManager.registerCallback(mExecutor, this);
74 }
75
76 /**
77 * Adds a listener interested in receiving updates on the RearDisplayStatus
78 * of the device. Because this is being called from the OEM provided
79 * extensions, we will post the result of the listener on the executor
80 * provided by the developer at the initial call site.
81 *
82 * Depending on the initial state of the device, we will return either
83 * {@link WindowAreaComponent#STATUS_AVAILABLE} or
84 * {@link WindowAreaComponent#STATUS_UNAVAILABLE} if the feature is supported or not in that
85 * state respectively. When the rear display feature is triggered, we update the status to be
86 * {@link WindowAreaComponent#STATUS_UNAVAILABLE}. TODO(b/240727590) Prefix with AREA_
87 *
88 * TODO(b/239833099) Add a STATUS_ACTIVE option to let apps know if a feature is currently
89 * enabled.
90 *
91 * @param consumer {@link Consumer} interested in receiving updates to the status of
92 * rear display mode.
93 */
94 public void addRearDisplayStatusListener(
95 @NonNull Consumer<@WindowAreaStatus Integer> consumer) {
96 synchronized (mLock) {
97 mRearDisplayStatusListeners.add(consumer);
98
99 // If current device state is still invalid, we haven't gotten our initial value yet
100 if (mCurrentDeviceState == INVALID_DEVICE_STATE) {
101 return;
102 }
103 consumer.accept(getCurrentStatus());
104 }
105 }
106
107 /**
108 * Removes a listener no longer interested in receiving updates.
109 * @param consumer no longer interested in receiving updates to RearDisplayStatus
110 */
111 public void removeRearDisplayStatusListener(
112 @NonNull Consumer<@WindowAreaStatus Integer> consumer) {
113 synchronized (mLock) {
114 mRearDisplayStatusListeners.remove(consumer);
115 }
116 }
117
118 /**
119 * Creates and starts a rear display session and provides updates to the
120 * callback provided. Because this is being called from the OEM provided
121 * extensions, we will post the result of the listener on the executor
122 * provided by the developer at the initial call site.
123 *
124 * When we enable rear display mode, we submit a request to {@link DeviceStateManager}
125 * to override the device state to the state that corresponds to RearDisplay
126 * mode. When the {@link DeviceStateRequest} is activated, we let the
127 * consumer know that the session is active by sending
128 * {@link WindowAreaComponent#SESSION_STATE_ACTIVE}.
129 *
130 * @param activity to provide updates to the client on
131 * the status of the Session
132 * @param rearDisplaySessionCallback to provide updates to the client on
133 * the status of the Session
134 */
135 public void startRearDisplaySession(@NonNull Activity activity,
136 @NonNull Consumer<@WindowAreaSessionState Integer> rearDisplaySessionCallback) {
137 synchronized (mLock) {
138 if (mDeviceStateRequest != null) {
139 // Rear display session is already active
140 throw new IllegalStateException(
141 "Unable to start new rear display session as one is already active");
142 }
143 mDeviceStateRequest = DeviceStateRequest.newBuilder(mRearDisplayState).build();
144 mDeviceStateManager.requestState(
145 mDeviceStateRequest,
146 mExecutor,
147 new DeviceStateRequestCallbackAdapter(rearDisplaySessionCallback)
148 );
149 }
150 }
151
152 /**
153 * Ends the current rear display session and provides updates to the
154 * callback provided. Because this is being called from the OEM provided
155 * extensions, we will post the result of the listener on the executor
156 * provided by the developer.
157 */
158 public void endRearDisplaySession() {
159 synchronized (mLock) {
160 if (mDeviceStateRequest != null || isRearDisplayActive()) {
161 mDeviceStateRequest = null;
162 mDeviceStateManager.cancelStateRequest();
163 } else {
164 throw new IllegalStateException(
165 "Unable to cancel a rear display session as there is no active session");
166 }
167 }
168 }
169
170 @Override
171 public void onBaseStateChanged(int state) {
172 synchronized (mLock) {
173 mCurrentDeviceBaseState = state;
174 if (state == mCurrentDeviceState) {
175 updateStatusConsumers(getCurrentStatus());
176 }
177 }
178 }
179
180 @Override
181 public void onStateChanged(int state) {
182 synchronized (mLock) {
183 mCurrentDeviceState = state;
184 updateStatusConsumers(getCurrentStatus());
185 }
186 }
187
188 @GuardedBy("mLock")
189 private int getCurrentStatus() {
190 if (mRearDisplaySessionStatus == WindowAreaComponent.SESSION_STATE_ACTIVE
191 || isRearDisplayActive()) {
192 return WindowAreaComponent.STATUS_UNAVAILABLE;
193 }
194 return WindowAreaComponent.STATUS_AVAILABLE;
195 }
196
197 /**
198 * Helper method to determine if a rear display session is currently active by checking
199 * if the current device configuration matches that of rear display. This would be true
200 * if there is a device override currently active (base state != current state) and the current
201 * state is that which corresponds to {@code mRearDisplayState}
202 * @return {@code true} if the device is in rear display mode and {@code false} if not
203 */
204 @GuardedBy("mLock")
205 private boolean isRearDisplayActive() {
206 return (mCurrentDeviceState != mCurrentDeviceBaseState) && (mCurrentDeviceState
207 == mRearDisplayState);
208 }
209
210 @GuardedBy("mLock")
211 private void updateStatusConsumers(@WindowAreaStatus int windowAreaStatus) {
212 synchronized (mLock) {
213 for (int i = 0; i < mRearDisplayStatusListeners.size(); i++) {
214 mRearDisplayStatusListeners.valueAt(i).accept(windowAreaStatus);
215 }
216 }
217 }
218
219 /**
220 * Callback for the {@link DeviceStateRequest} to be notified of when the request has been
221 * activated or cancelled. This callback provides information to the client library
222 * on the status of the RearDisplay session through {@code mRearDisplaySessionCallback}
223 */
224 private class DeviceStateRequestCallbackAdapter implements DeviceStateRequest.Callback {
225
226 private final Consumer<Integer> mRearDisplaySessionCallback;
227
228 DeviceStateRequestCallbackAdapter(@NonNull Consumer<Integer> callback) {
229 mRearDisplaySessionCallback = callback;
230 }
231
232 @Override
233 public void onRequestActivated(@NonNull DeviceStateRequest request) {
234 synchronized (mLock) {
235 if (request.equals(mDeviceStateRequest)) {
236 mRearDisplaySessionStatus = WindowAreaComponent.SESSION_STATE_ACTIVE;
237 mRearDisplaySessionCallback.accept(mRearDisplaySessionStatus);
238 updateStatusConsumers(getCurrentStatus());
239 }
240 }
241 }
242
243 @Override
244 public void onRequestCanceled(DeviceStateRequest request) {
245 synchronized (mLock) {
246 if (request.equals(mDeviceStateRequest)) {
247 mDeviceStateRequest = null;
248 }
249 mRearDisplaySessionStatus = WindowAreaComponent.SESSION_STATE_INACTIVE;
250 mRearDisplaySessionCallback.accept(mRearDisplaySessionStatus);
251 updateStatusConsumers(getCurrentStatus());
252 }
253 }
254 }
255 }