blob: a96d224129187bf7bec8e7f9b3ef1dd317924344 [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 Jaggi38d44ec2017-06-14 16:04:59 -0700128 private final int mOrientationOnCreation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800129
130 static TaskSnapshotSurface create(WindowManagerService service, AppWindowToken token,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200131 TaskSnapshot snapshot) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800132
133 final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
134 final Window window = new Window();
135 final IWindowSession session = WindowManagerGlobal.getWindowSession();
136 window.setSession(session);
137 final Surface surface = new Surface();
138 final Rect tmpRect = new Rect();
139 final Rect tmpFrame = new Rect();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200140 final Rect taskBounds;
141 final Rect tmpContentInsets = new Rect();
142 final Rect tmpStableInsets = new Rect();
Andrii Kulian44607962017-03-16 11:06:24 -0700143 final MergedConfiguration tmpMergedConfiguration = new MergedConfiguration();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200144 int backgroundColor = WHITE;
145 int statusBarColor = 0;
146 int navigationBarColor = 0;
147 final int sysUiVis;
148 final int windowFlags;
149 final int windowPrivateFlags;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700150 final int currentOrientation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800151 synchronized (service.mWindowMap) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200152 final WindowState mainWindow = token.findMainWindow();
153 if (mainWindow == null) {
154 Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find main window for token="
155 + token);
156 return null;
157 }
158 sysUiVis = mainWindow.getSystemUiVisibility();
159 windowFlags = mainWindow.getAttrs().flags;
160 windowPrivateFlags = mainWindow.getAttrs().privateFlags;
161
Jorim Jaggi02886a82016-12-06 09:10:06 -0800162 layoutParams.type = TYPE_APPLICATION_STARTING;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200163 layoutParams.format = snapshot.getSnapshot().getFormat();
164 layoutParams.flags = (windowFlags & ~FLAG_INHERIT_EXCLUDES)
Jorim Jaggi02886a82016-12-06 09:10:06 -0800165 | FLAG_NOT_FOCUSABLE
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200166 | FLAG_NOT_TOUCHABLE;
Jorim Jaggid2616322017-06-07 12:38:19 -0700167 layoutParams.privateFlags = PRIVATE_FLAG_TASK_SNAPSHOT
168 | (windowPrivateFlags & PRIVATE_FLAG_INHERITS);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800169 layoutParams.token = token.token;
170 layoutParams.width = LayoutParams.MATCH_PARENT;
171 layoutParams.height = LayoutParams.MATCH_PARENT;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200172 layoutParams.systemUiVisibility = sysUiVis;
Bryce Lee6d410262017-02-28 15:30:17 -0800173 final Task task = token.getTask();
174 if (task != null) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200175 layoutParams.setTitle(String.format(TITLE_FORMAT, task.mTaskId));
Bryce Lee6d410262017-02-28 15:30:17 -0800176
177 final TaskDescription taskDescription = task.getTaskDescription();
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100178 if (taskDescription != null) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200179 backgroundColor = taskDescription.getBackgroundColor();
180 statusBarColor = taskDescription.getStatusBarColor();
181 navigationBarColor = taskDescription.getNavigationBarColor();
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100182 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200183 taskBounds = new Rect();
184 task.getBounds(taskBounds);
185 } else {
186 taskBounds = null;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100187 }
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700188 currentOrientation = mainWindow.getConfiguration().orientation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800189 }
190 try {
191 final int res = session.addToDisplay(window, window.mSeq, layoutParams,
192 View.VISIBLE, token.getDisplayContent().getDisplayId(), tmpRect, tmpRect,
193 tmpRect, null);
194 if (res < 0) {
195 Slog.w(TAG, "Failed to add snapshot starting window res=" + res);
196 return null;
197 }
198 } catch (RemoteException e) {
199 // Local call.
200 }
201 final TaskSnapshotSurface snapshotSurface = new TaskSnapshotSurface(service, window,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200202 surface, snapshot, layoutParams.getTitle(), backgroundColor, statusBarColor,
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700203 navigationBarColor, sysUiVis, windowFlags, windowPrivateFlags, taskBounds,
204 currentOrientation);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800205 window.setOuter(snapshotSurface);
206 try {
207 session.relayout(window, window.mSeq, layoutParams, -1, -1, View.VISIBLE, 0, tmpFrame,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200208 tmpRect, tmpContentInsets, tmpRect, tmpStableInsets, tmpRect, tmpRect,
209 tmpMergedConfiguration, surface);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800210 } catch (RemoteException e) {
211 // Local call.
212 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200213 snapshotSurface.setFrames(tmpFrame, tmpContentInsets, tmpStableInsets);
214 snapshotSurface.drawSnapshot();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800215 return snapshotSurface;
216 }
217
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100218 @VisibleForTesting
219 TaskSnapshotSurface(WindowManagerService service, Window window, Surface surface,
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200220 TaskSnapshot snapshot, CharSequence title, int backgroundColor, int statusBarColor,
221 int navigationBarColor, int sysUiVis, int windowFlags, int windowPrivateFlags,
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700222 Rect taskBounds, int currentOrientation) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800223 mService = service;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200224 mHandler = new Handler(mService.mH.getLooper());
Jorim Jaggi02886a82016-12-06 09:10:06 -0800225 mSession = WindowManagerGlobal.getWindowSession();
226 mWindow = window;
227 mSurface = surface;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200228 mSnapshot = snapshot;
229 mTitle = title;
230 mBackgroundPaint.setColor(backgroundColor != 0 ? backgroundColor : WHITE);
231 mTaskBounds = taskBounds;
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200232 mSystemBarBackgroundPainter = new SystemBarBackgroundPainter(windowFlags,
233 windowPrivateFlags, sysUiVis, statusBarColor, navigationBarColor);
234 mStatusBarColor = statusBarColor;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700235 mOrientationOnCreation = currentOrientation;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800236 }
237
238 @Override
239 public void remove() {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200240 synchronized (mService.mWindowMap) {
241 final long now = SystemClock.uptimeMillis();
242 if (mSizeMismatch && now - mShownTime < SIZE_MISMATCH_MINIMUM_TIME_MS) {
243 mHandler.postAtTime(this::remove, mShownTime + SIZE_MISMATCH_MINIMUM_TIME_MS);
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200244 if (DEBUG_STARTING_WINDOW) {
245 Slog.v(TAG, "Defer removing snapshot surface in " + (now - mShownTime) + "ms");
246 }
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200247 return;
248 }
249 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800250 try {
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200251 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Removing snapshot surface");
Jorim Jaggi02886a82016-12-06 09:10:06 -0800252 mSession.remove(mWindow);
253 } catch (RemoteException e) {
254 // Local call.
255 }
256 }
257
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200258 @VisibleForTesting
259 void setFrames(Rect frame, Rect contentInsets, Rect stableInsets) {
260 mFrame.set(frame);
261 mContentInsets.set(contentInsets);
262 mStableInsets.set(stableInsets);
263 mSizeMismatch = (mFrame.width() != mSnapshot.getSnapshot().getWidth()
264 || mFrame.height() != mSnapshot.getSnapshot().getHeight());
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200265 mSystemBarBackgroundPainter.setInsets(contentInsets, stableInsets);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200266 }
267
268 private void drawSnapshot() {
269 final GraphicBuffer buffer = mSnapshot.getSnapshot();
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200270 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Drawing snapshot surface sizeMismatch="
271 + mSizeMismatch);
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200272 if (mSizeMismatch) {
273 // The dimensions of the buffer and the window don't match, so attaching the buffer
274 // will fail. Better create a child window with the exact dimensions and fill the parent
275 // window with the background color!
276 drawSizeMismatchSnapshot(buffer);
277 } else {
278 drawSizeMatchSnapshot(buffer);
279 }
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100280 synchronized (mService.mWindowMap) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200281 mShownTime = SystemClock.uptimeMillis();
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100282 mHasDrawn = true;
Jorim Jaggi39367cf2017-03-13 16:41:13 +0100283 }
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200284 reportDrawn();
Jorim Jaggi4448e1e2017-05-16 22:26:02 +0200285
286 // In case window manager leaks us, make sure we don't retain the snapshot.
287 mSnapshot = null;
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200288 }
289
290 private void drawSizeMatchSnapshot(GraphicBuffer buffer) {
291 mSurface.attachAndQueueBuffer(buffer);
292 mSurface.release();
293 }
294
295 private void drawSizeMismatchSnapshot(GraphicBuffer buffer) {
296 final SurfaceSession session = new SurfaceSession(mSurface);
297
298 // Keep a reference to it such that it doesn't get destroyed when finalized.
299 mChildSurfaceControl = new SurfaceControl(session,
300 mTitle + " - task-snapshot-surface",
301 buffer.getWidth(), buffer.getHeight(), buffer.getFormat(), HIDDEN);
302 Surface surface = new Surface();
303 surface.copyFrom(mChildSurfaceControl);
304
305 // Clip off ugly navigation bar.
306 final Rect crop = calculateSnapshotCrop();
307 final Rect frame = calculateSnapshotFrame(crop);
308 SurfaceControl.openTransaction();
309 try {
310 // We can just show the surface here as it will still be hidden as the parent is
311 // still hidden.
312 mChildSurfaceControl.show();
313 mChildSurfaceControl.setWindowCrop(crop);
314 mChildSurfaceControl.setPosition(frame.left, frame.top);
315 } finally {
316 SurfaceControl.closeTransaction();
317 }
318 surface.attachAndQueueBuffer(buffer);
319 surface.release();
320
321 final Canvas c = mSurface.lockCanvas(null);
322 drawBackgroundAndBars(c, frame);
323 mSurface.unlockCanvasAndPost(c);
Jorim Jaggi2f24b652017-01-18 02:17:37 +0100324 mSurface.release();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800325 }
326
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100327 @VisibleForTesting
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200328 Rect calculateSnapshotCrop() {
329 final Rect rect = new Rect();
330 rect.set(0, 0, mSnapshot.getSnapshot().getWidth(), mSnapshot.getSnapshot().getHeight());
331 final Rect insets = mSnapshot.getContentInsets();
332
333 // Let's remove all system decorations except the status bar, but only if the task is at the
334 // very top of the screen.
335 rect.inset(insets.left, mTaskBounds.top != 0 ? insets.top : 0, insets.right, insets.bottom);
336 return rect;
337 }
338
339 @VisibleForTesting
340 Rect calculateSnapshotFrame(Rect crop) {
341 final Rect frame = new Rect(crop);
342
343 // By default, offset it to to top/left corner
344 frame.offsetTo(-crop.left, -crop.top);
345
346 // However, we also need to make space for the navigation bar on the left side.
347 final int colorViewLeftInset = getColorViewLeftInset(mStableInsets.left,
348 mContentInsets.left);
349 frame.offset(colorViewLeftInset, 0);
350 return frame;
351 }
352
353 @VisibleForTesting
354 void drawBackgroundAndBars(Canvas c, Rect frame) {
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200355 final int statusBarHeight = mSystemBarBackgroundPainter.getStatusBarColorViewHeight();
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200356 final boolean fillHorizontally = c.getWidth() > frame.right;
357 final boolean fillVertically = c.getHeight() > frame.bottom;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100358 if (fillHorizontally) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200359 c.drawRect(frame.right, alpha(mStatusBarColor) == 0xFF ? statusBarHeight : 0,
360 c.getWidth(), fillVertically
361 ? frame.bottom
362 : c.getHeight(),
363 mBackgroundPaint);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100364 }
365 if (fillVertically) {
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200366 c.drawRect(0, frame.bottom, c.getWidth(), c.getHeight(), mBackgroundPaint);
367 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200368 mSystemBarBackgroundPainter.drawDecors(c, frame);
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100369 }
370
Jorim Jaggi02886a82016-12-06 09:10:06 -0800371 private void reportDrawn() {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800372 try {
373 mSession.finishDrawing(mWindow);
374 } catch (RemoteException e) {
375 // Local call.
376 }
377 }
378
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100379 private static Handler sHandler = new Handler(Looper.getMainLooper()) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800380
381 @Override
382 public void handleMessage(Message msg) {
383 switch (msg.what) {
384 case MSG_REPORT_DRAW:
385 final boolean hasDrawn;
386 final TaskSnapshotSurface surface = (TaskSnapshotSurface) msg.obj;
387 synchronized (surface.mService.mWindowMap) {
388 hasDrawn = surface.mHasDrawn;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800389 }
390 if (hasDrawn) {
391 surface.reportDrawn();
392 }
393 break;
394 }
395 }
396 };
397
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200398 @VisibleForTesting
399 static class Window extends BaseIWindow {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800400
401 private TaskSnapshotSurface mOuter;
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700402
Jorim Jaggi02886a82016-12-06 09:10:06 -0800403 public void setOuter(TaskSnapshotSurface outer) {
404 mOuter = outer;
405 }
406
407 @Override
408 public void resized(Rect frame, Rect overscanInsets, Rect contentInsets, Rect visibleInsets,
Andrii Kulian44607962017-03-16 11:06:24 -0700409 Rect stableInsets, Rect outsets, boolean reportDraw,
410 MergedConfiguration mergedConfiguration, Rect backDropFrame, boolean forceLayout,
411 boolean alwaysConsumeNavBar, int displayId) {
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700412 if (mergedConfiguration != null && mOuter != null
413 && mOuter.mOrientationOnCreation
414 != mergedConfiguration.getMergedConfiguration().orientation) {
415
416 // The orientation of the screen is changing. We better remove the snapshot ASAP as
417 // we are going to wait on the new window in any case to unfreeze the screen, and
418 // the starting window is not needed anymore.
419 sHandler.post(mOuter::remove);
420 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800421 if (reportDraw) {
422 sHandler.obtainMessage(MSG_REPORT_DRAW, mOuter).sendToTarget();
423 }
424 }
425 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200426
427 /**
428 * Helper class to draw the background of the system bars in regions the task snapshot isn't
429 * filling the window.
430 */
431 static class SystemBarBackgroundPainter {
432
433 private final Rect mContentInsets = new Rect();
434 private final Rect mStableInsets = new Rect();
435 private final Paint mStatusBarPaint = new Paint();
436 private final Paint mNavigationBarPaint = new Paint();
437 private final int mStatusBarColor;
438 private final int mNavigationBarColor;
439 private final int mWindowFlags;
440 private final int mWindowPrivateFlags;
441 private final int mSysUiVis;
442
443 SystemBarBackgroundPainter( int windowFlags, int windowPrivateFlags, int sysUiVis,
444 int statusBarColor, int navigationBarColor) {
445 mWindowFlags = windowFlags;
446 mWindowPrivateFlags = windowPrivateFlags;
447 mSysUiVis = sysUiVis;
448 final Context context = ActivityThread.currentActivityThread().getSystemUiContext();
449 mStatusBarColor = DecorView.calculateStatusBarColor(windowFlags,
450 context.getColor(R.color.system_bar_background_semi_transparent),
451 statusBarColor);
452 mNavigationBarColor = navigationBarColor;
453 mStatusBarPaint.setColor(mStatusBarColor);
454 mNavigationBarPaint.setColor(navigationBarColor);
455 }
456
457 void setInsets(Rect contentInsets, Rect stableInsets) {
458 mContentInsets.set(contentInsets);
459 mStableInsets.set(stableInsets);
460 }
461
462 int getStatusBarColorViewHeight() {
463 final boolean forceStatusBarBackground =
464 (mWindowPrivateFlags & PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND) != 0;
465 if (STATUS_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
466 mSysUiVis, mStatusBarColor, mWindowFlags, forceStatusBarBackground)) {
467 return getColorViewTopInset(mStableInsets.top, mContentInsets.top);
468 } else {
469 return 0;
470 }
471 }
472
473 private boolean isNavigationBarColorViewVisible() {
474 return NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES.isVisible(
475 mSysUiVis, mNavigationBarColor, mWindowFlags, false /* force */);
476 }
477
478 void drawDecors(Canvas c, @Nullable Rect alreadyDrawnFrame) {
479 drawStatusBarBackground(c, alreadyDrawnFrame, getStatusBarColorViewHeight());
480 drawNavigationBarBackground(c);
481 }
482
483 @VisibleForTesting
484 void drawStatusBarBackground(Canvas c, @Nullable Rect alreadyDrawnFrame,
485 int statusBarHeight) {
486 if (statusBarHeight > 0
487 && (alreadyDrawnFrame == null || c.getWidth() > alreadyDrawnFrame.right)) {
488 final int rightInset = DecorView.getColorViewRightInset(mStableInsets.right,
489 mContentInsets.right);
490 final int left = alreadyDrawnFrame != null ? alreadyDrawnFrame.right : 0;
491 c.drawRect(left, 0, c.getWidth() - rightInset, statusBarHeight, mStatusBarPaint);
492 }
493 }
494
495 @VisibleForTesting
496 void drawNavigationBarBackground(Canvas c) {
497 final Rect navigationBarRect = new Rect();
498 getNavigationBarRect(c.getWidth(), c.getHeight(), mStableInsets, mContentInsets,
499 navigationBarRect);
500 final boolean visible = isNavigationBarColorViewVisible();
501 if (visible && !navigationBarRect.isEmpty()) {
502 c.drawRect(navigationBarRect, mNavigationBarPaint);
503 }
504 }
505 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800506}