blob: 551e3bf13339238a8a034ee5ec4781425c391ad7 [file] [log] [blame]
Jorim Jaggi02886a82016-12-06 09:10:06 -08001/*
2 * Copyright (C) 2016 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
17package com.android.server.wm;
18
Jorim Jaggi30d64f32017-04-07 16:33:17 +020019import static android.graphics.Color.WHITE;
20import static android.graphics.Color.alpha;
21import static android.view.SurfaceControl.HIDDEN;
22import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
23import static android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
24import static android.view.WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES;
25import static android.view.WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE;
Jorim Jaggi02886a82016-12-06 09:10:06 -080026import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
27import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020028import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
29import static android.view.WindowManager.LayoutParams.FLAG_SCALED;
30import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
31import static android.view.WindowManager.LayoutParams.FLAG_SLIPPERY;
32import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH;
33import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
34import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND;
Jorim Jaggi02886a82016-12-06 09:10:06 -080035import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_TASK_SNAPSHOT;
36import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020037import static com.android.internal.policy.DecorView.NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES;
38import static com.android.internal.policy.DecorView.STATUS_BAR_COLOR_VIEW_ATTRIBUTES;
39import static com.android.internal.policy.DecorView.getColorViewLeftInset;
40import static com.android.internal.policy.DecorView.getColorViewTopInset;
41import static com.android.internal.policy.DecorView.getNavigationBarRect;
Jorim Jaggie4b0f282017-05-17 15:10:29 +020042import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
Jorim Jaggi02886a82016-12-06 09:10:06 -080043import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
44import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
45
Jorim Jaggid635a4a2017-05-03 15:21:26 +020046import android.annotation.Nullable;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010047import android.app.ActivityManager.TaskDescription;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020048import android.app.ActivityManager.TaskSnapshot;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020049import android.app.ActivityThread;
50import android.content.Context;
Jorim Jaggi02886a82016-12-06 09:10:06 -080051import android.graphics.Canvas;
52import android.graphics.GraphicBuffer;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010053import android.graphics.Paint;
Jorim Jaggi02886a82016-12-06 09:10:06 -080054import android.graphics.Rect;
55import android.os.Handler;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010056import android.os.Looper;
Jorim Jaggi02886a82016-12-06 09:10:06 -080057import android.os.Message;
58import android.os.RemoteException;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020059import android.os.SystemClock;
Andrii Kulian44607962017-03-16 11:06:24 -070060import android.util.MergedConfiguration;
Jorim Jaggi02886a82016-12-06 09:10:06 -080061import android.util.Slog;
Jorim Jaggi02886a82016-12-06 09:10:06 -080062import android.view.IWindowSession;
63import android.view.Surface;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020064import android.view.SurfaceControl;
65import android.view.SurfaceSession;
Jorim Jaggi02886a82016-12-06 09:10:06 -080066import android.view.View;
67import android.view.ViewGroup.LayoutParams;
68import android.view.WindowManager;
69import android.view.WindowManagerGlobal;
70import android.view.WindowManagerPolicy.StartingSurface;
71
Jorim Jaggi30d64f32017-04-07 16:33:17 +020072import com.android.internal.R;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010073import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020074import com.android.internal.policy.DecorView;
Jorim Jaggi02886a82016-12-06 09:10:06 -080075import com.android.internal.view.BaseIWindow;
76
77/**
78 * This class represents a starting window that shows a snapshot.
79 * <p>
80 * DO NOT HOLD THE WINDOW MANAGER LOCK WHEN CALLING METHODS OF THIS CLASS!
81 */
82class TaskSnapshotSurface implements StartingSurface {
83
Jorim Jaggi30d64f32017-04-07 16:33:17 +020084 private static final long SIZE_MISMATCH_MINIMUM_TIME_MS = 450;
85
86 /**
87 * When creating the starting window, we use the exact same layout flags such that we end up
88 * with a window with the exact same dimensions etc. However, these flags are not used in layout
89 * and might cause other side effects so we exclude them.
90 */
91 private static final int FLAG_INHERIT_EXCLUDES = FLAG_NOT_FOCUSABLE
92 | FLAG_NOT_TOUCHABLE
93 | FLAG_NOT_TOUCH_MODAL
94 | FLAG_ALT_FOCUSABLE_IM
95 | FLAG_NOT_FOCUSABLE
96 | FLAG_HARDWARE_ACCELERATED
97 | FLAG_IGNORE_CHEEK_PRESSES
98 | FLAG_LOCAL_FOCUS_MODE
99 | FLAG_SLIPPERY
100 | FLAG_WATCH_OUTSIDE_TOUCH
101 | FLAG_SPLIT_TOUCH
102 | FLAG_SCALED
103 | FLAG_SECURE;
104
Jorim Jaggid2616322017-06-07 12:38:19 -0700105 private static final int PRIVATE_FLAG_INHERITS = PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND;
106
Jorim Jaggi02886a82016-12-06 09:10:06 -0800107 private static final String TAG = TAG_WITH_CLASS_NAME ? "SnapshotStartingWindow" : TAG_WM;
108 private static final int MSG_REPORT_DRAW = 0;
109 private static final String TITLE_FORMAT = "SnapshotStartingWindow for taskId=%s";
110 private final Window mWindow;
111 private final Surface mSurface;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200112 private SurfaceControl mChildSurfaceControl;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800113 private final IWindowSession mSession;
114 private final WindowManagerService mService;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200115 private final Rect mTaskBounds;
116 private final Rect mStableInsets = new Rect();
117 private final Rect mContentInsets = new Rect();
118 private final Rect mFrame = new Rect();
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200119 private TaskSnapshot mSnapshot;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200120 private final CharSequence mTitle;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800121 private boolean mHasDrawn;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200122 private long mShownTime;
123 private final Handler mHandler;
124 private boolean mSizeMismatch;
125 private final Paint mBackgroundPaint = new Paint();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200126 private final int mStatusBarColor;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200127 @VisibleForTesting final SystemBarBackgroundPainter mSystemBarBackgroundPainter;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800128
129 static TaskSnapshotSurface create(WindowManagerService service, AppWindowToken token,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200130 TaskSnapshot snapshot) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800131
132 final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
133 final Window window = new Window();
134 final IWindowSession session = WindowManagerGlobal.getWindowSession();
135 window.setSession(session);
136 final Surface surface = new Surface();
137 final Rect tmpRect = new Rect();
138 final Rect tmpFrame = new Rect();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200139 final Rect taskBounds;
140 final Rect tmpContentInsets = new Rect();
141 final Rect tmpStableInsets = new Rect();
Andrii Kulian44607962017-03-16 11:06:24 -0700142 final MergedConfiguration tmpMergedConfiguration = new MergedConfiguration();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200143 int backgroundColor = WHITE;
144 int statusBarColor = 0;
145 int navigationBarColor = 0;
146 final int sysUiVis;
147 final int windowFlags;
148 final int windowPrivateFlags;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800149 synchronized (service.mWindowMap) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200150 final WindowState mainWindow = token.findMainWindow();
151 if (mainWindow == null) {
152 Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find main window for token="
153 + token);
154 return null;
155 }
156 sysUiVis = mainWindow.getSystemUiVisibility();
157 windowFlags = mainWindow.getAttrs().flags;
158 windowPrivateFlags = mainWindow.getAttrs().privateFlags;
159
Jorim Jaggi02886a82016-12-06 09:10:06 -0800160 layoutParams.type = TYPE_APPLICATION_STARTING;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200161 layoutParams.format = snapshot.getSnapshot().getFormat();
162 layoutParams.flags = (windowFlags & ~FLAG_INHERIT_EXCLUDES)
Jorim Jaggi02886a82016-12-06 09:10:06 -0800163 | FLAG_NOT_FOCUSABLE
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200164 | FLAG_NOT_TOUCHABLE;
Jorim Jaggid2616322017-06-07 12:38:19 -0700165 layoutParams.privateFlags = PRIVATE_FLAG_TASK_SNAPSHOT
166 | (windowPrivateFlags & PRIVATE_FLAG_INHERITS);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800167 layoutParams.token = token.token;
168 layoutParams.width = LayoutParams.MATCH_PARENT;
169 layoutParams.height = LayoutParams.MATCH_PARENT;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200170 layoutParams.systemUiVisibility = sysUiVis;
Bryce Lee6d410262017-02-28 15:30:17 -0800171 final Task task = token.getTask();
172 if (task != null) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200173 layoutParams.setTitle(String.format(TITLE_FORMAT, task.mTaskId));
Bryce Lee6d410262017-02-28 15:30:17 -0800174
175 final TaskDescription taskDescription = task.getTaskDescription();
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100176 if (taskDescription != null) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200177 backgroundColor = taskDescription.getBackgroundColor();
178 statusBarColor = taskDescription.getStatusBarColor();
179 navigationBarColor = taskDescription.getNavigationBarColor();
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100180 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200181 taskBounds = new Rect();
182 task.getBounds(taskBounds);
183 } else {
184 taskBounds = null;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100185 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800186 }
187 try {
188 final int res = session.addToDisplay(window, window.mSeq, layoutParams,
189 View.VISIBLE, token.getDisplayContent().getDisplayId(), tmpRect, tmpRect,
190 tmpRect, null);
191 if (res < 0) {
192 Slog.w(TAG, "Failed to add snapshot starting window res=" + res);
193 return null;
194 }
195 } catch (RemoteException e) {
196 // Local call.
197 }
198 final TaskSnapshotSurface snapshotSurface = new TaskSnapshotSurface(service, window,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200199 surface, snapshot, layoutParams.getTitle(), backgroundColor, statusBarColor,
200 navigationBarColor, sysUiVis, windowFlags, windowPrivateFlags, taskBounds);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800201 window.setOuter(snapshotSurface);
202 try {
203 session.relayout(window, window.mSeq, layoutParams, -1, -1, View.VISIBLE, 0, tmpFrame,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200204 tmpRect, tmpContentInsets, tmpRect, tmpStableInsets, tmpRect, tmpRect,
205 tmpMergedConfiguration, surface);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800206 } catch (RemoteException e) {
207 // Local call.
208 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200209 snapshotSurface.setFrames(tmpFrame, tmpContentInsets, tmpStableInsets);
210 snapshotSurface.drawSnapshot();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800211 return snapshotSurface;
212 }
213
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100214 @VisibleForTesting
215 TaskSnapshotSurface(WindowManagerService service, Window window, Surface surface,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200216 TaskSnapshot snapshot, CharSequence title, int backgroundColor, int statusBarColor,
217 int navigationBarColor, int sysUiVis, int windowFlags, int windowPrivateFlags,
218 Rect taskBounds) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800219 mService = service;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200220 mHandler = new Handler(mService.mH.getLooper());
Jorim Jaggi02886a82016-12-06 09:10:06 -0800221 mSession = WindowManagerGlobal.getWindowSession();
222 mWindow = window;
223 mSurface = surface;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200224 mSnapshot = snapshot;
225 mTitle = title;
226 mBackgroundPaint.setColor(backgroundColor != 0 ? backgroundColor : WHITE);
227 mTaskBounds = taskBounds;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200228 mSystemBarBackgroundPainter = new SystemBarBackgroundPainter(windowFlags,
229 windowPrivateFlags, sysUiVis, statusBarColor, navigationBarColor);
230 mStatusBarColor = statusBarColor;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800231 }
232
233 @Override
234 public void remove() {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200235 synchronized (mService.mWindowMap) {
236 final long now = SystemClock.uptimeMillis();
237 if (mSizeMismatch && now - mShownTime < SIZE_MISMATCH_MINIMUM_TIME_MS) {
238 mHandler.postAtTime(this::remove, mShownTime + SIZE_MISMATCH_MINIMUM_TIME_MS);
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200239 if (DEBUG_STARTING_WINDOW) {
240 Slog.v(TAG, "Defer removing snapshot surface in " + (now - mShownTime) + "ms");
241 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200242 return;
243 }
244 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800245 try {
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200246 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Removing snapshot surface");
Jorim Jaggi02886a82016-12-06 09:10:06 -0800247 mSession.remove(mWindow);
248 } catch (RemoteException e) {
249 // Local call.
250 }
251 }
252
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200253 @VisibleForTesting
254 void setFrames(Rect frame, Rect contentInsets, Rect stableInsets) {
255 mFrame.set(frame);
256 mContentInsets.set(contentInsets);
257 mStableInsets.set(stableInsets);
258 mSizeMismatch = (mFrame.width() != mSnapshot.getSnapshot().getWidth()
259 || mFrame.height() != mSnapshot.getSnapshot().getHeight());
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200260 mSystemBarBackgroundPainter.setInsets(contentInsets, stableInsets);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200261 }
262
263 private void drawSnapshot() {
264 final GraphicBuffer buffer = mSnapshot.getSnapshot();
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200265 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Drawing snapshot surface sizeMismatch="
266 + mSizeMismatch);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200267 if (mSizeMismatch) {
268 // The dimensions of the buffer and the window don't match, so attaching the buffer
269 // will fail. Better create a child window with the exact dimensions and fill the parent
270 // window with the background color!
271 drawSizeMismatchSnapshot(buffer);
272 } else {
273 drawSizeMatchSnapshot(buffer);
274 }
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100275 synchronized (mService.mWindowMap) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200276 mShownTime = SystemClock.uptimeMillis();
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100277 mHasDrawn = true;
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100278 }
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200279 reportDrawn();
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200280
281 // In case window manager leaks us, make sure we don't retain the snapshot.
282 mSnapshot = null;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200283 }
284
285 private void drawSizeMatchSnapshot(GraphicBuffer buffer) {
286 mSurface.attachAndQueueBuffer(buffer);
287 mSurface.release();
288 }
289
290 private void drawSizeMismatchSnapshot(GraphicBuffer buffer) {
291 final SurfaceSession session = new SurfaceSession(mSurface);
292
293 // Keep a reference to it such that it doesn't get destroyed when finalized.
294 mChildSurfaceControl = new SurfaceControl(session,
295 mTitle + " - task-snapshot-surface",
296 buffer.getWidth(), buffer.getHeight(), buffer.getFormat(), HIDDEN);
297 Surface surface = new Surface();
298 surface.copyFrom(mChildSurfaceControl);
299
300 // Clip off ugly navigation bar.
301 final Rect crop = calculateSnapshotCrop();
302 final Rect frame = calculateSnapshotFrame(crop);
303 SurfaceControl.openTransaction();
304 try {
305 // We can just show the surface here as it will still be hidden as the parent is
306 // still hidden.
307 mChildSurfaceControl.show();
308 mChildSurfaceControl.setWindowCrop(crop);
309 mChildSurfaceControl.setPosition(frame.left, frame.top);
310 } finally {
311 SurfaceControl.closeTransaction();
312 }
313 surface.attachAndQueueBuffer(buffer);
314 surface.release();
315
316 final Canvas c = mSurface.lockCanvas(null);
317 drawBackgroundAndBars(c, frame);
318 mSurface.unlockCanvasAndPost(c);
Jorim Jaggi2f24b652017-01-18 02:17:37 +0100319 mSurface.release();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800320 }
321
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100322 @VisibleForTesting
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200323 Rect calculateSnapshotCrop() {
324 final Rect rect = new Rect();
325 rect.set(0, 0, mSnapshot.getSnapshot().getWidth(), mSnapshot.getSnapshot().getHeight());
326 final Rect insets = mSnapshot.getContentInsets();
327
328 // Let's remove all system decorations except the status bar, but only if the task is at the
329 // very top of the screen.
330 rect.inset(insets.left, mTaskBounds.top != 0 ? insets.top : 0, insets.right, insets.bottom);
331 return rect;
332 }
333
334 @VisibleForTesting
335 Rect calculateSnapshotFrame(Rect crop) {
336 final Rect frame = new Rect(crop);
337
338 // By default, offset it to to top/left corner
339 frame.offsetTo(-crop.left, -crop.top);
340
341 // However, we also need to make space for the navigation bar on the left side.
342 final int colorViewLeftInset = getColorViewLeftInset(mStableInsets.left,
343 mContentInsets.left);
344 frame.offset(colorViewLeftInset, 0);
345 return frame;
346 }
347
348 @VisibleForTesting
349 void drawBackgroundAndBars(Canvas c, Rect frame) {
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200350 final int statusBarHeight = mSystemBarBackgroundPainter.getStatusBarColorViewHeight();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200351 final boolean fillHorizontally = c.getWidth() > frame.right;
352 final boolean fillVertically = c.getHeight() > frame.bottom;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100353 if (fillHorizontally) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200354 c.drawRect(frame.right, alpha(mStatusBarColor) == 0xFF ? statusBarHeight : 0,
355 c.getWidth(), fillVertically
356 ? frame.bottom
357 : c.getHeight(),
358 mBackgroundPaint);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100359 }
360 if (fillVertically) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200361 c.drawRect(0, frame.bottom, c.getWidth(), c.getHeight(), mBackgroundPaint);
362 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200363 mSystemBarBackgroundPainter.drawDecors(c, frame);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100364 }
365
Jorim Jaggi02886a82016-12-06 09:10:06 -0800366 private void reportDrawn() {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800367 try {
368 mSession.finishDrawing(mWindow);
369 } catch (RemoteException e) {
370 // Local call.
371 }
372 }
373
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100374 private static Handler sHandler = new Handler(Looper.getMainLooper()) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800375
376 @Override
377 public void handleMessage(Message msg) {
378 switch (msg.what) {
379 case MSG_REPORT_DRAW:
380 final boolean hasDrawn;
381 final TaskSnapshotSurface surface = (TaskSnapshotSurface) msg.obj;
382 synchronized (surface.mService.mWindowMap) {
383 hasDrawn = surface.mHasDrawn;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800384 }
385 if (hasDrawn) {
386 surface.reportDrawn();
387 }
388 break;
389 }
390 }
391 };
392
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200393 @VisibleForTesting
394 static class Window extends BaseIWindow {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800395
396 private TaskSnapshotSurface mOuter;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800397 public void setOuter(TaskSnapshotSurface outer) {
398 mOuter = outer;
399 }
400
401 @Override
402 public void resized(Rect frame, Rect overscanInsets, Rect contentInsets, Rect visibleInsets,
Andrii Kulian44607962017-03-16 11:06:24 -0700403 Rect stableInsets, Rect outsets, boolean reportDraw,
404 MergedConfiguration mergedConfiguration, Rect backDropFrame, boolean forceLayout,
405 boolean alwaysConsumeNavBar, int displayId) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800406 if (reportDraw) {
407 sHandler.obtainMessage(MSG_REPORT_DRAW, mOuter).sendToTarget();
408 }
409 }
410 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200411
412 /**
413 * Helper class to draw the background of the system bars in regions the task snapshot isn't
414 * filling the window.
415 */
416 static class SystemBarBackgroundPainter {
417
418 private final Rect mContentInsets = new Rect();
419 private final Rect mStableInsets = new Rect();
420 private final Paint mStatusBarPaint = new Paint();
421 private final Paint mNavigationBarPaint = new Paint();
422 private final int mStatusBarColor;
423 private final int mNavigationBarColor;
424 private final int mWindowFlags;
425 private final int mWindowPrivateFlags;
426 private final int mSysUiVis;
427
428 SystemBarBackgroundPainter( int windowFlags, int windowPrivateFlags, int sysUiVis,
429 int statusBarColor, int navigationBarColor) {
430 mWindowFlags = windowFlags;
431 mWindowPrivateFlags = windowPrivateFlags;
432 mSysUiVis = sysUiVis;
433 final Context context = ActivityThread.currentActivityThread().getSystemUiContext();
434 mStatusBarColor = DecorView.calculateStatusBarColor(windowFlags,
435 context.getColor(R.color.system_bar_background_semi_transparent),
436 statusBarColor);
437 mNavigationBarColor = navigationBarColor;
438 mStatusBarPaint.setColor(mStatusBarColor);
439 mNavigationBarPaint.setColor(navigationBarColor);
440 }
441
442 void setInsets(Rect contentInsets, Rect stableInsets) {
443 mContentInsets.set(contentInsets);
444 mStableInsets.set(stableInsets);
445 }
446
447 int getStatusBarColorViewHeight() {
448 final boolean forceStatusBarBackground =
449 (mWindowPrivateFlags & PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND) != 0;
450 if (STATUS_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
451 mSysUiVis, mStatusBarColor, mWindowFlags, forceStatusBarBackground)) {
452 return getColorViewTopInset(mStableInsets.top, mContentInsets.top);
453 } else {
454 return 0;
455 }
456 }
457
458 private boolean isNavigationBarColorViewVisible() {
459 return NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
460 mSysUiVis, mNavigationBarColor, mWindowFlags, false /* force */);
461 }
462
463 void drawDecors(Canvas c, @Nullable Rect alreadyDrawnFrame) {
464 drawStatusBarBackground(c, alreadyDrawnFrame, getStatusBarColorViewHeight());
465 drawNavigationBarBackground(c);
466 }
467
468 @VisibleForTesting
469 void drawStatusBarBackground(Canvas c, @Nullable Rect alreadyDrawnFrame,
470 int statusBarHeight) {
471 if (statusBarHeight > 0
472 && (alreadyDrawnFrame == null || c.getWidth() > alreadyDrawnFrame.right)) {
473 final int rightInset = DecorView.getColorViewRightInset(mStableInsets.right,
474 mContentInsets.right);
475 final int left = alreadyDrawnFrame != null ? alreadyDrawnFrame.right : 0;
476 c.drawRect(left, 0, c.getWidth() - rightInset, statusBarHeight, mStatusBarPaint);
477 }
478 }
479
480 @VisibleForTesting
481 void drawNavigationBarBackground(Canvas c) {
482 final Rect navigationBarRect = new Rect();
483 getNavigationBarRect(c.getWidth(), c.getHeight(), mStableInsets, mContentInsets,
484 navigationBarRect);
485 final boolean visible = isNavigationBarColorViewVisible();
486 if (visible && !navigationBarRect.isEmpty()) {
487 c.drawRect(navigationBarRect, mNavigationBarPaint);
488 }
489 }
490 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800491}