blob: 249620131867b139ca3554a298beffe53a29d937 [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.TYPE_APPLICATION_STARTING;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020036import static com.android.internal.policy.DecorView.NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES;
37import static com.android.internal.policy.DecorView.STATUS_BAR_COLOR_VIEW_ATTRIBUTES;
38import static com.android.internal.policy.DecorView.getColorViewLeftInset;
39import static com.android.internal.policy.DecorView.getColorViewTopInset;
40import static com.android.internal.policy.DecorView.getNavigationBarRect;
Jorim Jaggie4b0f282017-05-17 15:10:29 +020041import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
Jorim Jaggi02886a82016-12-06 09:10:06 -080042import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
43import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
44
Jorim Jaggid635a4a2017-05-03 15:21:26 +020045import android.annotation.Nullable;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010046import android.app.ActivityManager.TaskDescription;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020047import android.app.ActivityManager.TaskSnapshot;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020048import android.app.ActivityThread;
49import android.content.Context;
Jorim Jaggi02886a82016-12-06 09:10:06 -080050import android.graphics.Canvas;
51import android.graphics.GraphicBuffer;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010052import android.graphics.Paint;
Jorim Jaggi02886a82016-12-06 09:10:06 -080053import android.graphics.Rect;
54import android.os.Handler;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010055import android.os.Looper;
Jorim Jaggi02886a82016-12-06 09:10:06 -080056import android.os.Message;
57import android.os.RemoteException;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020058import android.os.SystemClock;
Andrii Kulian44607962017-03-16 11:06:24 -070059import android.util.MergedConfiguration;
Jorim Jaggi02886a82016-12-06 09:10:06 -080060import android.util.Slog;
Jorim Jaggi02886a82016-12-06 09:10:06 -080061import android.view.IWindowSession;
62import android.view.Surface;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020063import android.view.SurfaceControl;
64import android.view.SurfaceSession;
Jorim Jaggi02886a82016-12-06 09:10:06 -080065import android.view.View;
66import android.view.ViewGroup.LayoutParams;
67import android.view.WindowManager;
68import android.view.WindowManagerGlobal;
69import android.view.WindowManagerPolicy.StartingSurface;
70
Jorim Jaggi30d64f32017-04-07 16:33:17 +020071import com.android.internal.R;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010072import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggi30d64f32017-04-07 16:33:17 +020073import com.android.internal.policy.DecorView;
Jorim Jaggi02886a82016-12-06 09:10:06 -080074import com.android.internal.view.BaseIWindow;
75
76/**
77 * This class represents a starting window that shows a snapshot.
78 * <p>
79 * DO NOT HOLD THE WINDOW MANAGER LOCK WHEN CALLING METHODS OF THIS CLASS!
80 */
81class TaskSnapshotSurface implements StartingSurface {
82
Jorim Jaggi30d64f32017-04-07 16:33:17 +020083 private static final long SIZE_MISMATCH_MINIMUM_TIME_MS = 450;
84
85 /**
86 * When creating the starting window, we use the exact same layout flags such that we end up
87 * with a window with the exact same dimensions etc. However, these flags are not used in layout
88 * and might cause other side effects so we exclude them.
89 */
90 private static final int FLAG_INHERIT_EXCLUDES = FLAG_NOT_FOCUSABLE
91 | FLAG_NOT_TOUCHABLE
92 | FLAG_NOT_TOUCH_MODAL
93 | FLAG_ALT_FOCUSABLE_IM
94 | FLAG_NOT_FOCUSABLE
95 | FLAG_HARDWARE_ACCELERATED
96 | FLAG_IGNORE_CHEEK_PRESSES
97 | FLAG_LOCAL_FOCUS_MODE
98 | FLAG_SLIPPERY
99 | FLAG_WATCH_OUTSIDE_TOUCH
100 | FLAG_SPLIT_TOUCH
101 | FLAG_SCALED
102 | FLAG_SECURE;
103
Jorim Jaggid2616322017-06-07 12:38:19 -0700104 private static final int PRIVATE_FLAG_INHERITS = PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND;
105
Jorim Jaggi02886a82016-12-06 09:10:06 -0800106 private static final String TAG = TAG_WITH_CLASS_NAME ? "SnapshotStartingWindow" : TAG_WM;
107 private static final int MSG_REPORT_DRAW = 0;
108 private static final String TITLE_FORMAT = "SnapshotStartingWindow for taskId=%s";
109 private final Window mWindow;
110 private final Surface mSurface;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200111 private SurfaceControl mChildSurfaceControl;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800112 private final IWindowSession mSession;
113 private final WindowManagerService mService;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200114 private final Rect mTaskBounds;
115 private final Rect mStableInsets = new Rect();
116 private final Rect mContentInsets = new Rect();
117 private final Rect mFrame = new Rect();
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200118 private TaskSnapshot mSnapshot;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200119 private final CharSequence mTitle;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800120 private boolean mHasDrawn;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200121 private long mShownTime;
122 private final Handler mHandler;
123 private boolean mSizeMismatch;
124 private final Paint mBackgroundPaint = new Paint();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200125 private final int mStatusBarColor;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200126 @VisibleForTesting final SystemBarBackgroundPainter mSystemBarBackgroundPainter;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700127 private final int mOrientationOnCreation;
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 Jaggi38d44ec2017-06-14 16:04:59 -0700149 final int currentOrientation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800150 synchronized (service.mWindowMap) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200151 final WindowState mainWindow = token.findMainWindow();
152 if (mainWindow == null) {
153 Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find main window for token="
154 + token);
155 return null;
156 }
157 sysUiVis = mainWindow.getSystemUiVisibility();
158 windowFlags = mainWindow.getAttrs().flags;
159 windowPrivateFlags = mainWindow.getAttrs().privateFlags;
160
Jorim Jaggi02886a82016-12-06 09:10:06 -0800161 layoutParams.type = TYPE_APPLICATION_STARTING;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200162 layoutParams.format = snapshot.getSnapshot().getFormat();
163 layoutParams.flags = (windowFlags & ~FLAG_INHERIT_EXCLUDES)
Jorim Jaggi02886a82016-12-06 09:10:06 -0800164 | FLAG_NOT_FOCUSABLE
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200165 | FLAG_NOT_TOUCHABLE;
Wale Ogunwale01ad4342017-06-30 07:07:01 -0700166 layoutParams.privateFlags = 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 Jaggi38d44ec2017-06-14 16:04:59 -0700186 currentOrientation = mainWindow.getConfiguration().orientation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800187 }
188 try {
189 final int res = session.addToDisplay(window, window.mSeq, layoutParams,
190 View.VISIBLE, token.getDisplayContent().getDisplayId(), tmpRect, tmpRect,
191 tmpRect, null);
192 if (res < 0) {
193 Slog.w(TAG, "Failed to add snapshot starting window res=" + res);
194 return null;
195 }
196 } catch (RemoteException e) {
197 // Local call.
198 }
199 final TaskSnapshotSurface snapshotSurface = new TaskSnapshotSurface(service, window,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200200 surface, snapshot, layoutParams.getTitle(), backgroundColor, statusBarColor,
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700201 navigationBarColor, sysUiVis, windowFlags, windowPrivateFlags, taskBounds,
202 currentOrientation);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800203 window.setOuter(snapshotSurface);
204 try {
205 session.relayout(window, window.mSeq, layoutParams, -1, -1, View.VISIBLE, 0, tmpFrame,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200206 tmpRect, tmpContentInsets, tmpRect, tmpStableInsets, tmpRect, tmpRect,
207 tmpMergedConfiguration, surface);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800208 } catch (RemoteException e) {
209 // Local call.
210 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200211 snapshotSurface.setFrames(tmpFrame, tmpContentInsets, tmpStableInsets);
212 snapshotSurface.drawSnapshot();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800213 return snapshotSurface;
214 }
215
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100216 @VisibleForTesting
217 TaskSnapshotSurface(WindowManagerService service, Window window, Surface surface,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200218 TaskSnapshot snapshot, CharSequence title, int backgroundColor, int statusBarColor,
219 int navigationBarColor, int sysUiVis, int windowFlags, int windowPrivateFlags,
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700220 Rect taskBounds, int currentOrientation) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800221 mService = service;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200222 mHandler = new Handler(mService.mH.getLooper());
Jorim Jaggi02886a82016-12-06 09:10:06 -0800223 mSession = WindowManagerGlobal.getWindowSession();
224 mWindow = window;
225 mSurface = surface;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200226 mSnapshot = snapshot;
227 mTitle = title;
228 mBackgroundPaint.setColor(backgroundColor != 0 ? backgroundColor : WHITE);
229 mTaskBounds = taskBounds;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200230 mSystemBarBackgroundPainter = new SystemBarBackgroundPainter(windowFlags,
231 windowPrivateFlags, sysUiVis, statusBarColor, navigationBarColor);
232 mStatusBarColor = statusBarColor;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700233 mOrientationOnCreation = currentOrientation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800234 }
235
236 @Override
237 public void remove() {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200238 synchronized (mService.mWindowMap) {
239 final long now = SystemClock.uptimeMillis();
240 if (mSizeMismatch && now - mShownTime < SIZE_MISMATCH_MINIMUM_TIME_MS) {
241 mHandler.postAtTime(this::remove, mShownTime + SIZE_MISMATCH_MINIMUM_TIME_MS);
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200242 if (DEBUG_STARTING_WINDOW) {
243 Slog.v(TAG, "Defer removing snapshot surface in " + (now - mShownTime) + "ms");
244 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200245 return;
246 }
247 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800248 try {
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200249 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Removing snapshot surface");
Jorim Jaggi02886a82016-12-06 09:10:06 -0800250 mSession.remove(mWindow);
251 } catch (RemoteException e) {
252 // Local call.
253 }
254 }
255
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200256 @VisibleForTesting
257 void setFrames(Rect frame, Rect contentInsets, Rect stableInsets) {
258 mFrame.set(frame);
259 mContentInsets.set(contentInsets);
260 mStableInsets.set(stableInsets);
261 mSizeMismatch = (mFrame.width() != mSnapshot.getSnapshot().getWidth()
262 || mFrame.height() != mSnapshot.getSnapshot().getHeight());
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200263 mSystemBarBackgroundPainter.setInsets(contentInsets, stableInsets);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200264 }
265
266 private void drawSnapshot() {
267 final GraphicBuffer buffer = mSnapshot.getSnapshot();
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200268 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Drawing snapshot surface sizeMismatch="
269 + mSizeMismatch);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200270 if (mSizeMismatch) {
271 // The dimensions of the buffer and the window don't match, so attaching the buffer
272 // will fail. Better create a child window with the exact dimensions and fill the parent
273 // window with the background color!
274 drawSizeMismatchSnapshot(buffer);
275 } else {
276 drawSizeMatchSnapshot(buffer);
277 }
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100278 synchronized (mService.mWindowMap) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200279 mShownTime = SystemClock.uptimeMillis();
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100280 mHasDrawn = true;
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100281 }
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200282 reportDrawn();
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200283
284 // In case window manager leaks us, make sure we don't retain the snapshot.
285 mSnapshot = null;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200286 }
287
288 private void drawSizeMatchSnapshot(GraphicBuffer buffer) {
289 mSurface.attachAndQueueBuffer(buffer);
290 mSurface.release();
291 }
292
293 private void drawSizeMismatchSnapshot(GraphicBuffer buffer) {
294 final SurfaceSession session = new SurfaceSession(mSurface);
295
296 // Keep a reference to it such that it doesn't get destroyed when finalized.
297 mChildSurfaceControl = new SurfaceControl(session,
298 mTitle + " - task-snapshot-surface",
299 buffer.getWidth(), buffer.getHeight(), buffer.getFormat(), HIDDEN);
300 Surface surface = new Surface();
301 surface.copyFrom(mChildSurfaceControl);
302
303 // Clip off ugly navigation bar.
304 final Rect crop = calculateSnapshotCrop();
305 final Rect frame = calculateSnapshotFrame(crop);
306 SurfaceControl.openTransaction();
307 try {
308 // We can just show the surface here as it will still be hidden as the parent is
309 // still hidden.
310 mChildSurfaceControl.show();
311 mChildSurfaceControl.setWindowCrop(crop);
312 mChildSurfaceControl.setPosition(frame.left, frame.top);
313 } finally {
314 SurfaceControl.closeTransaction();
315 }
316 surface.attachAndQueueBuffer(buffer);
317 surface.release();
318
319 final Canvas c = mSurface.lockCanvas(null);
320 drawBackgroundAndBars(c, frame);
321 mSurface.unlockCanvasAndPost(c);
Jorim Jaggi2f24b652017-01-18 02:17:37 +0100322 mSurface.release();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800323 }
324
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100325 @VisibleForTesting
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200326 Rect calculateSnapshotCrop() {
327 final Rect rect = new Rect();
328 rect.set(0, 0, mSnapshot.getSnapshot().getWidth(), mSnapshot.getSnapshot().getHeight());
329 final Rect insets = mSnapshot.getContentInsets();
330
331 // Let's remove all system decorations except the status bar, but only if the task is at the
332 // very top of the screen.
333 rect.inset(insets.left, mTaskBounds.top != 0 ? insets.top : 0, insets.right, insets.bottom);
334 return rect;
335 }
336
337 @VisibleForTesting
338 Rect calculateSnapshotFrame(Rect crop) {
339 final Rect frame = new Rect(crop);
340
341 // By default, offset it to to top/left corner
342 frame.offsetTo(-crop.left, -crop.top);
343
344 // However, we also need to make space for the navigation bar on the left side.
345 final int colorViewLeftInset = getColorViewLeftInset(mStableInsets.left,
346 mContentInsets.left);
347 frame.offset(colorViewLeftInset, 0);
348 return frame;
349 }
350
351 @VisibleForTesting
352 void drawBackgroundAndBars(Canvas c, Rect frame) {
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200353 final int statusBarHeight = mSystemBarBackgroundPainter.getStatusBarColorViewHeight();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200354 final boolean fillHorizontally = c.getWidth() > frame.right;
355 final boolean fillVertically = c.getHeight() > frame.bottom;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100356 if (fillHorizontally) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200357 c.drawRect(frame.right, alpha(mStatusBarColor) == 0xFF ? statusBarHeight : 0,
358 c.getWidth(), fillVertically
359 ? frame.bottom
360 : c.getHeight(),
361 mBackgroundPaint);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100362 }
363 if (fillVertically) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200364 c.drawRect(0, frame.bottom, c.getWidth(), c.getHeight(), mBackgroundPaint);
365 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200366 mSystemBarBackgroundPainter.drawDecors(c, frame);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100367 }
368
Jorim Jaggi02886a82016-12-06 09:10:06 -0800369 private void reportDrawn() {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800370 try {
371 mSession.finishDrawing(mWindow);
372 } catch (RemoteException e) {
373 // Local call.
374 }
375 }
376
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100377 private static Handler sHandler = new Handler(Looper.getMainLooper()) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800378
379 @Override
380 public void handleMessage(Message msg) {
381 switch (msg.what) {
382 case MSG_REPORT_DRAW:
383 final boolean hasDrawn;
384 final TaskSnapshotSurface surface = (TaskSnapshotSurface) msg.obj;
385 synchronized (surface.mService.mWindowMap) {
386 hasDrawn = surface.mHasDrawn;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800387 }
388 if (hasDrawn) {
389 surface.reportDrawn();
390 }
391 break;
392 }
393 }
394 };
395
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200396 @VisibleForTesting
397 static class Window extends BaseIWindow {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800398
399 private TaskSnapshotSurface mOuter;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700400
Jorim Jaggi02886a82016-12-06 09:10:06 -0800401 public void setOuter(TaskSnapshotSurface outer) {
402 mOuter = outer;
403 }
404
405 @Override
406 public void resized(Rect frame, Rect overscanInsets, Rect contentInsets, Rect visibleInsets,
Andrii Kulian44607962017-03-16 11:06:24 -0700407 Rect stableInsets, Rect outsets, boolean reportDraw,
408 MergedConfiguration mergedConfiguration, Rect backDropFrame, boolean forceLayout,
409 boolean alwaysConsumeNavBar, int displayId) {
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700410 if (mergedConfiguration != null && mOuter != null
411 && mOuter.mOrientationOnCreation
412 != mergedConfiguration.getMergedConfiguration().orientation) {
413
414 // The orientation of the screen is changing. We better remove the snapshot ASAP as
415 // we are going to wait on the new window in any case to unfreeze the screen, and
416 // the starting window is not needed anymore.
417 sHandler.post(mOuter::remove);
418 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800419 if (reportDraw) {
420 sHandler.obtainMessage(MSG_REPORT_DRAW, mOuter).sendToTarget();
421 }
422 }
423 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200424
425 /**
426 * Helper class to draw the background of the system bars in regions the task snapshot isn't
427 * filling the window.
428 */
429 static class SystemBarBackgroundPainter {
430
431 private final Rect mContentInsets = new Rect();
432 private final Rect mStableInsets = new Rect();
433 private final Paint mStatusBarPaint = new Paint();
434 private final Paint mNavigationBarPaint = new Paint();
435 private final int mStatusBarColor;
436 private final int mNavigationBarColor;
437 private final int mWindowFlags;
438 private final int mWindowPrivateFlags;
439 private final int mSysUiVis;
440
441 SystemBarBackgroundPainter( int windowFlags, int windowPrivateFlags, int sysUiVis,
442 int statusBarColor, int navigationBarColor) {
443 mWindowFlags = windowFlags;
444 mWindowPrivateFlags = windowPrivateFlags;
445 mSysUiVis = sysUiVis;
446 final Context context = ActivityThread.currentActivityThread().getSystemUiContext();
447 mStatusBarColor = DecorView.calculateStatusBarColor(windowFlags,
448 context.getColor(R.color.system_bar_background_semi_transparent),
449 statusBarColor);
450 mNavigationBarColor = navigationBarColor;
451 mStatusBarPaint.setColor(mStatusBarColor);
452 mNavigationBarPaint.setColor(navigationBarColor);
453 }
454
455 void setInsets(Rect contentInsets, Rect stableInsets) {
456 mContentInsets.set(contentInsets);
457 mStableInsets.set(stableInsets);
458 }
459
460 int getStatusBarColorViewHeight() {
461 final boolean forceStatusBarBackground =
462 (mWindowPrivateFlags & PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND) != 0;
463 if (STATUS_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
464 mSysUiVis, mStatusBarColor, mWindowFlags, forceStatusBarBackground)) {
465 return getColorViewTopInset(mStableInsets.top, mContentInsets.top);
466 } else {
467 return 0;
468 }
469 }
470
471 private boolean isNavigationBarColorViewVisible() {
472 return NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
473 mSysUiVis, mNavigationBarColor, mWindowFlags, false /* force */);
474 }
475
476 void drawDecors(Canvas c, @Nullable Rect alreadyDrawnFrame) {
477 drawStatusBarBackground(c, alreadyDrawnFrame, getStatusBarColorViewHeight());
478 drawNavigationBarBackground(c);
479 }
480
481 @VisibleForTesting
482 void drawStatusBarBackground(Canvas c, @Nullable Rect alreadyDrawnFrame,
483 int statusBarHeight) {
484 if (statusBarHeight > 0
485 && (alreadyDrawnFrame == null || c.getWidth() > alreadyDrawnFrame.right)) {
486 final int rightInset = DecorView.getColorViewRightInset(mStableInsets.right,
487 mContentInsets.right);
488 final int left = alreadyDrawnFrame != null ? alreadyDrawnFrame.right : 0;
489 c.drawRect(left, 0, c.getWidth() - rightInset, statusBarHeight, mStatusBarPaint);
490 }
491 }
492
493 @VisibleForTesting
494 void drawNavigationBarBackground(Canvas c) {
495 final Rect navigationBarRect = new Rect();
496 getNavigationBarRect(c.getWidth(), c.getHeight(), mStableInsets, mContentInsets,
497 navigationBarRect);
498 final boolean visible = isNavigationBarColorViewVisible();
499 if (visible && !navigationBarRect.isEmpty()) {
500 c.drawRect(navigationBarRect, mNavigationBarPaint);
501 }
502 }
503 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800504}