Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/compatui/CompatUIWindowManager.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 com.android.wm.shell.compatui;
18
19 import static android.app.TaskInfo.CAMERA_COMPAT_CONTROL_DISMISSED;
20 import static android.app.TaskInfo.CAMERA_COMPAT_CONTROL_HIDDEN;
21 import static android.app.TaskInfo.CAMERA_COMPAT_CONTROL_TREATMENT_APPLIED;
22 import static android.app.TaskInfo.CAMERA_COMPAT_CONTROL_TREATMENT_SUGGESTED;
23
24 import android.annotation.Nullable;
25 import android.app.TaskInfo;
26 import android.app.TaskInfo.CameraCompatControlState;
27 import android.content.Context;
28 import android.graphics.Rect;
29 import android.util.Log;
30 import android.view.LayoutInflater;
31 import android.view.View;
32
33 import com.android.internal.annotations.VisibleForTesting;
34 import com.android.wm.shell.R;
35 import com.android.wm.shell.ShellTaskOrganizer;
36 import com.android.wm.shell.common.DisplayLayout;
37 import com.android.wm.shell.common.SyncTransactionQueue;
38 import com.android.wm.shell.compatui.CompatUIController.CompatUICallback;
39 import com.android.wm.shell.compatui.letterboxedu.LetterboxEduWindowManager;
40
41 /**
42 * Window manager for the Size Compat restart button and Camera Compat control.
43 */
44 class CompatUIWindowManager extends CompatUIWindowManagerAbstract {
45
46 /**
47 * The Compat UI should be below the Letterbox Education.
48 */
49 private static final int Z_ORDER = LetterboxEduWindowManager.Z_ORDER - 1;
50
51 private final CompatUICallback mCallback;
52
53 // Remember the last reported states in case visibility changes due to keyguard or IME updates.
54 @VisibleForTesting
55 boolean mHasSizeCompat;
56
57 @VisibleForTesting
58 @CameraCompatControlState
59 int mCameraCompatControlState = CAMERA_COMPAT_CONTROL_HIDDEN;
60
61 @VisibleForTesting
62 CompatUIHintsState mCompatUIHintsState;
63
64 @Nullable
65 @VisibleForTesting
66 CompatUILayout mLayout;
67
68 CompatUIWindowManager(Context context, TaskInfo taskInfo,
69 SyncTransactionQueue syncQueue, CompatUICallback callback,
70 ShellTaskOrganizer.TaskListener taskListener, DisplayLayout displayLayout,
71 CompatUIHintsState compatUIHintsState) {
72 super(context, taskInfo, syncQueue, taskListener, displayLayout);
73 mCallback = callback;
74 mHasSizeCompat = taskInfo.topActivityInSizeCompat;
75 mCameraCompatControlState = taskInfo.cameraCompatControlState;
76 mCompatUIHintsState = compatUIHintsState;
77 }
78
79 @Override
80 protected int getZOrder() {
81 return Z_ORDER;
82 }
83
84 @Override
85 protected @Nullable View getLayout() {
86 return mLayout;
87 }
88
89 @Override
90 protected void removeLayout() {
91 mLayout = null;
92 }
93
94 @Override
95 protected boolean eligibleToShowLayout() {
96 return mHasSizeCompat || shouldShowCameraControl();
97 }
98
99 @Override
100 protected View createLayout() {
101 mLayout = inflateLayout();
102 mLayout.inject(this);
103
104 updateVisibilityOfViews();
105
106 if (mHasSizeCompat) {
107 mCallback.onSizeCompatRestartButtonAppeared(mTaskId);
108 }
109
110 return mLayout;
111 }
112
113 @VisibleForTesting
114 CompatUILayout inflateLayout() {
115 return (CompatUILayout) LayoutInflater.from(mContext).inflate(R.layout.compat_ui_layout,
116 null);
117 }
118
119 @Override
120 public boolean updateCompatInfo(TaskInfo taskInfo, ShellTaskOrganizer.TaskListener taskListener,
121 boolean canShow) {
122 final boolean prevHasSizeCompat = mHasSizeCompat;
123 final int prevCameraCompatControlState = mCameraCompatControlState;
124 mHasSizeCompat = taskInfo.topActivityInSizeCompat;
125 mCameraCompatControlState = taskInfo.cameraCompatControlState;
126
127 if (!super.updateCompatInfo(taskInfo, taskListener, canShow)) {
128 return false;
129 }
130
131 if (prevHasSizeCompat != mHasSizeCompat
132 || prevCameraCompatControlState != mCameraCompatControlState) {
133 updateVisibilityOfViews();
134 }
135
136 return true;
137 }
138
139 /** Called when the restart button is clicked. */
140 void onRestartButtonClicked() {
141 mCallback.onSizeCompatRestartButtonClicked(mTaskId);
142 }
143
144 /** Called when the camera treatment button is clicked. */
145 void onCameraTreatmentButtonClicked() {
146 if (!shouldShowCameraControl()) {
147 Log.w(getTag(), "Camera compat shouldn't receive clicks in the hidden state.");
148 return;
149 }
150 // When a camera control is shown, only two states are allowed: "treament applied" and
151 // "treatment suggested". Clicks on the conrol's treatment button toggle between these
152 // two states.
153 mCameraCompatControlState =
154 mCameraCompatControlState == CAMERA_COMPAT_CONTROL_TREATMENT_SUGGESTED
155 ? CAMERA_COMPAT_CONTROL_TREATMENT_APPLIED
156 : CAMERA_COMPAT_CONTROL_TREATMENT_SUGGESTED;
157 mCallback.onCameraControlStateUpdated(mTaskId, mCameraCompatControlState);
158 mLayout.updateCameraTreatmentButton(mCameraCompatControlState);
159 }
160
161 /** Called when the camera dismiss button is clicked. */
162 void onCameraDismissButtonClicked() {
163 if (!shouldShowCameraControl()) {
164 Log.w(getTag(), "Camera compat shouldn't receive clicks in the hidden state.");
165 return;
166 }
167 mCameraCompatControlState = CAMERA_COMPAT_CONTROL_DISMISSED;
168 mCallback.onCameraControlStateUpdated(mTaskId, CAMERA_COMPAT_CONTROL_DISMISSED);
169 mLayout.setCameraControlVisibility(/* show= */ false);
170 }
171
172 /** Called when the restart button is long clicked. */
173 void onRestartButtonLongClicked() {
174 if (mLayout == null) {
175 return;
176 }
177 mLayout.setSizeCompatHintVisibility(/* show= */ true);
178 }
179
180 /** Called when either dismiss or treatment camera buttons is long clicked. */
181 void onCameraButtonLongClicked() {
182 if (mLayout == null) {
183 return;
184 }
185 mLayout.setCameraCompatHintVisibility(/* show= */ true);
186 }
187
188 @Override
189 @VisibleForTesting
190 public void updateSurfacePosition() {
191 if (mLayout == null) {
192 return;
193 }
194 // Position of the button in the container coordinate.
195 final Rect taskBounds = getTaskBounds();
196 final Rect taskStableBounds = getTaskStableBounds();
197 final int positionX = getLayoutDirection() == View.LAYOUT_DIRECTION_RTL
198 ? taskStableBounds.left - taskBounds.left
199 : taskStableBounds.right - taskBounds.left - mLayout.getMeasuredWidth();
200 final int positionY = taskStableBounds.bottom - taskBounds.top
201 - mLayout.getMeasuredHeight();
202
203 updateSurfacePosition(positionX, positionY);
204 }
205
206 private void updateVisibilityOfViews() {
207 if (mLayout == null) {
208 return;
209 }
210 // Size Compat mode restart button.
211 mLayout.setRestartButtonVisibility(mHasSizeCompat);
212 // Only show by default for the first time.
213 if (mHasSizeCompat && !mCompatUIHintsState.mHasShownSizeCompatHint) {
214 mLayout.setSizeCompatHintVisibility(/* show= */ true);
215 mCompatUIHintsState.mHasShownSizeCompatHint = true;
216 }
217
218 // Camera control for stretched issues.
219 mLayout.setCameraControlVisibility(shouldShowCameraControl());
220 // Only show by default for the first time.
221 if (shouldShowCameraControl() && !mCompatUIHintsState.mHasShownCameraCompatHint) {
222 mLayout.setCameraCompatHintVisibility(/* show= */ true);
223 mCompatUIHintsState.mHasShownCameraCompatHint = true;
224 }
225 if (shouldShowCameraControl()) {
226 mLayout.updateCameraTreatmentButton(mCameraCompatControlState);
227 }
228 }
229
230 private boolean shouldShowCameraControl() {
231 return mCameraCompatControlState != CAMERA_COMPAT_CONTROL_HIDDEN
232 && mCameraCompatControlState != CAMERA_COMPAT_CONTROL_DISMISSED;
233 }
234
235 /**
236 * A class holding the state of the compat UI hints, which is shared between all compat UI
237 * window managers.
238 */
239 static class CompatUIHintsState {
240 @VisibleForTesting
241 boolean mHasShownSizeCompatHint;
242 @VisibleForTesting
243 boolean mHasShownCameraCompatHint;
244 }
245 }