Debian Codegraph @

libs/WindowManager/Jetpack/src/androidx/window/sidecar/SampleSidecarImpl.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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.sidecar;
18
19 import static android.view.Display.DEFAULT_DISPLAY;
20
21 import static androidx.window.util.ExtensionHelper.rotateRectToDisplayRotation;
22 import static androidx.window.util.ExtensionHelper.transformToWindowSpaceRect;
23
24 import android.app.Activity;
25 import android.app.ActivityThread;
26 import android.app.Application;
27 import android.content.Context;
28 import android.graphics.Rect;
29 import android.os.Bundle;
30 import android.os.IBinder;
31
32 import androidx.annotation.NonNull;
33 import androidx.window.common.CommonFoldingFeature;
34 import androidx.window.common.DeviceStateManagerFoldingFeatureProducer;
35 import androidx.window.common.EmptyLifecycleCallbacksAdapter;
36 import androidx.window.common.RawFoldingFeatureProducer;
37 import androidx.window.util.BaseDataProducer;
38
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.List;
42
43 /**
44 * Reference implementation of androidx.window.sidecar OEM interface for use with
45 * WindowManager Jetpack.
46 */
47 class SampleSidecarImpl extends StubSidecar {
48 private List<CommonFoldingFeature> mStoredFeatures = new ArrayList<>();
49
50 SampleSidecarImpl(Context context) {
51 ((Application) context.getApplicationContext())
52 .registerActivityLifecycleCallbacks(new NotifyOnConfigurationChanged());
53 BaseDataProducer<String> settingsFeatureProducer = new RawFoldingFeatureProducer(context);
54 BaseDataProducer<List<CommonFoldingFeature>> foldingFeatureProducer =
55 new DeviceStateManagerFoldingFeatureProducer(context,
56 settingsFeatureProducer);
57
58 foldingFeatureProducer.addDataChangedCallback(this::onDisplayFeaturesChanged);
59 }
60
61 private void setStoredFeatures(List<CommonFoldingFeature> storedFeatures) {
62 mStoredFeatures = storedFeatures;
63 }
64
65 private void onDisplayFeaturesChanged(List<CommonFoldingFeature> storedFeatures) {
66 setStoredFeatures(storedFeatures);
67 updateDeviceState(getDeviceState());
68 for (IBinder windowToken : getWindowsListeningForLayoutChanges()) {
69 SidecarWindowLayoutInfo newLayout = getWindowLayoutInfo(windowToken);
70 updateWindowLayout(windowToken, newLayout);
71 }
72 }
73
74 @NonNull
75 @Override
76 public SidecarDeviceState getDeviceState() {
77 SidecarDeviceState deviceState = new SidecarDeviceState();
78 deviceState.posture = deviceStateFromFeature();
79 return deviceState;
80 }
81
82 private int deviceStateFromFeature() {
83 for (int i = 0; i < mStoredFeatures.size(); i++) {
84 CommonFoldingFeature feature = mStoredFeatures.get(i);
85 final int state = feature.getState();
86 switch (state) {
87 case CommonFoldingFeature.COMMON_STATE_FLAT:
88 return SidecarDeviceState.POSTURE_OPENED;
89 case CommonFoldingFeature.COMMON_STATE_HALF_OPENED:
90 return SidecarDeviceState.POSTURE_HALF_OPENED;
91 case CommonFoldingFeature.COMMON_STATE_UNKNOWN:
92 return SidecarDeviceState.POSTURE_UNKNOWN;
93 }
94 }
95 return SidecarDeviceState.POSTURE_UNKNOWN;
96 }
97
98 @NonNull
99 @Override
100 public SidecarWindowLayoutInfo getWindowLayoutInfo(@NonNull IBinder windowToken) {
101 Activity activity = ActivityThread.currentActivityThread().getActivity(windowToken);
102 SidecarWindowLayoutInfo windowLayoutInfo = new SidecarWindowLayoutInfo();
103 if (activity == null) {
104 return windowLayoutInfo;
105 }
106 windowLayoutInfo.displayFeatures = getDisplayFeatures(activity);
107 return windowLayoutInfo;
108 }
109
110 private List<SidecarDisplayFeature> getDisplayFeatures(@NonNull Activity activity) {
111 int displayId = activity.getDisplay().getDisplayId();
112 if (displayId != DEFAULT_DISPLAY) {
113 return Collections.emptyList();
114 }
115
116 if (activity.isInMultiWindowMode()) {
117 // It is recommended not to report any display features in multi-window mode, since it
118 // won't be possible to synchronize the display feature positions with window movement.
119 return Collections.emptyList();
120 }
121
122 List<SidecarDisplayFeature> features = new ArrayList<>();
123 for (CommonFoldingFeature baseFeature : mStoredFeatures) {
124 SidecarDisplayFeature feature = new SidecarDisplayFeature();
125 Rect featureRect = baseFeature.getRect();
126 rotateRectToDisplayRotation(displayId, featureRect);
127 transformToWindowSpaceRect(activity, featureRect);
128 feature.setRect(featureRect);
129 feature.setType(baseFeature.getType());
130 features.add(feature);
131 }
132 return Collections.unmodifiableList(features);
133 }
134
135 @Override
136 protected void onListenersChanged() {
137 if (hasListeners()) {
138 onDisplayFeaturesChanged(mStoredFeatures);
139 }
140 }
141
142 private final class NotifyOnConfigurationChanged extends EmptyLifecycleCallbacksAdapter {
143 @Override
144 public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
145 super.onActivityCreated(activity, savedInstanceState);
146 onDisplayFeaturesChangedForActivity(activity);
147 }
148
149 @Override
150 public void onActivityConfigurationChanged(Activity activity) {
151 super.onActivityConfigurationChanged(activity);
152 onDisplayFeaturesChangedForActivity(activity);
153 }
154
155 private void onDisplayFeaturesChangedForActivity(@NonNull Activity activity) {
156 IBinder token = activity.getWindow().getAttributes().token;
157 if (token == null || mWindowLayoutChangeListenerTokens.contains(token)) {
158 onDisplayFeaturesChanged(mStoredFeatures);
159 }
160 }
161 }
162 }