Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 19 | import static android.graphics.Color.WHITE; |
| 20 | import static android.graphics.Color.alpha; |
Adrian Roos | 4c86459 | 2019-04-10 14:47:57 +0200 | [diff] [blame] | 21 | import static android.view.View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; |
| 22 | import static android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 23 | import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; |
| 24 | import static android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED; |
| 25 | import static android.view.WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES; |
| 26 | import static android.view.WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 27 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
| 28 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 29 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; |
| 30 | import static android.view.WindowManager.LayoutParams.FLAG_SCALED; |
| 31 | import static android.view.WindowManager.LayoutParams.FLAG_SECURE; |
| 32 | import static android.view.WindowManager.LayoutParams.FLAG_SLIPPERY; |
| 33 | import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH; |
Jorim Jaggi | a6aabac | 2019-03-11 14:23:16 -0700 | [diff] [blame] | 34 | import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; |
| 35 | import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 36 | import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; |
Jorim Jaggi | a6aabac | 2019-03-11 14:23:16 -0700 | [diff] [blame] | 37 | import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_DRAW_BAR_BACKGROUNDS; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 38 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING; |
Vishnu Nair | e86bd98 | 2018-11-28 13:23:17 -0800 | [diff] [blame] | 39 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 40 | import static com.android.internal.policy.DecorView.NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES; |
| 41 | import static com.android.internal.policy.DecorView.STATUS_BAR_COLOR_VIEW_ATTRIBUTES; |
| 42 | import static com.android.internal.policy.DecorView.getColorViewLeftInset; |
| 43 | import static com.android.internal.policy.DecorView.getColorViewTopInset; |
| 44 | import static com.android.internal.policy.DecorView.getNavigationBarRect; |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 45 | import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_STARTING_WINDOW; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 46 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 47 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 48 | |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 49 | import android.annotation.Nullable; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 50 | import android.app.ActivityManager.TaskDescription; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 51 | import android.app.ActivityManager.TaskSnapshot; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 52 | import android.app.ActivityThread; |
| 53 | import android.content.Context; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 54 | import android.graphics.Canvas; |
Winson Chung | 51f42d2 | 2018-02-01 14:59:38 -0800 | [diff] [blame] | 55 | import android.graphics.Color; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 56 | import android.graphics.GraphicBuffer; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 57 | import android.graphics.Paint; |
Valerie Hau | 3036055 | 2020-01-14 16:12:01 -0800 | [diff] [blame^] | 58 | import android.graphics.Point; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 59 | import android.graphics.Rect; |
| 60 | import android.os.Handler; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 61 | import android.os.Looper; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 62 | import android.os.Message; |
| 63 | import android.os.RemoteException; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 64 | import android.os.SystemClock; |
Andrii Kulian | 4460796 | 2017-03-16 11:06:24 -0700 | [diff] [blame] | 65 | import android.util.MergedConfiguration; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 66 | import android.util.Slog; |
Adrian Roos | 5c6b622 | 2017-11-07 17:36:10 +0100 | [diff] [blame] | 67 | import android.view.DisplayCutout; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 68 | import android.view.IWindowSession; |
Chavi Weingarten | 6ef9cc6 | 2019-02-07 16:28:45 +0000 | [diff] [blame] | 69 | import android.view.InsetsState; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 70 | import android.view.Surface; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 71 | import android.view.SurfaceControl; |
| 72 | import android.view.SurfaceSession; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 73 | import android.view.View; |
| 74 | import android.view.ViewGroup.LayoutParams; |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 75 | import android.view.ViewRootImpl; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 76 | import android.view.WindowManager; |
| 77 | import android.view.WindowManagerGlobal; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 78 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 79 | import com.android.internal.R; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 80 | import com.android.internal.annotations.VisibleForTesting; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 81 | import com.android.internal.policy.DecorView; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 82 | import com.android.internal.view.BaseIWindow; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 83 | import com.android.server.policy.WindowManagerPolicy.StartingSurface; |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 84 | import com.android.server.protolog.common.ProtoLog; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * This class represents a starting window that shows a snapshot. |
| 88 | * <p> |
| 89 | * DO NOT HOLD THE WINDOW MANAGER LOCK WHEN CALLING METHODS OF THIS CLASS! |
| 90 | */ |
| 91 | class TaskSnapshotSurface implements StartingSurface { |
| 92 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 93 | private static final long SIZE_MISMATCH_MINIMUM_TIME_MS = 450; |
| 94 | |
| 95 | /** |
| 96 | * When creating the starting window, we use the exact same layout flags such that we end up |
| 97 | * with a window with the exact same dimensions etc. However, these flags are not used in layout |
| 98 | * and might cause other side effects so we exclude them. |
| 99 | */ |
| 100 | private static final int FLAG_INHERIT_EXCLUDES = FLAG_NOT_FOCUSABLE |
| 101 | | FLAG_NOT_TOUCHABLE |
| 102 | | FLAG_NOT_TOUCH_MODAL |
| 103 | | FLAG_ALT_FOCUSABLE_IM |
| 104 | | FLAG_NOT_FOCUSABLE |
| 105 | | FLAG_HARDWARE_ACCELERATED |
| 106 | | FLAG_IGNORE_CHEEK_PRESSES |
| 107 | | FLAG_LOCAL_FOCUS_MODE |
| 108 | | FLAG_SLIPPERY |
| 109 | | FLAG_WATCH_OUTSIDE_TOUCH |
| 110 | | FLAG_SPLIT_TOUCH |
| 111 | | FLAG_SCALED |
| 112 | | FLAG_SECURE; |
| 113 | |
Jorim Jaggi | a6aabac | 2019-03-11 14:23:16 -0700 | [diff] [blame] | 114 | private static final int PRIVATE_FLAG_INHERITS = PRIVATE_FLAG_FORCE_DRAW_BAR_BACKGROUNDS; |
Jorim Jaggi | d261632 | 2017-06-07 12:38:19 -0700 | [diff] [blame] | 115 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 116 | private static final String TAG = TAG_WITH_CLASS_NAME ? "SnapshotStartingWindow" : TAG_WM; |
| 117 | private static final int MSG_REPORT_DRAW = 0; |
| 118 | private static final String TITLE_FORMAT = "SnapshotStartingWindow for taskId=%s"; |
Valerie Hau | 3036055 | 2020-01-14 16:12:01 -0800 | [diff] [blame^] | 119 | private static final Point sSurfaceSize = new Point(); //tmp var for unused relayout param |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 120 | private final Window mWindow; |
| 121 | private final Surface mSurface; |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 122 | private SurfaceControl mSurfaceControl; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 123 | private SurfaceControl mChildSurfaceControl; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 124 | private final IWindowSession mSession; |
| 125 | private final WindowManagerService mService; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 126 | private final Rect mTaskBounds; |
| 127 | private final Rect mStableInsets = new Rect(); |
| 128 | private final Rect mContentInsets = new Rect(); |
| 129 | private final Rect mFrame = new Rect(); |
Jorim Jaggi | 4448e1e | 2017-05-16 22:26:02 +0200 | [diff] [blame] | 130 | private TaskSnapshot mSnapshot; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 131 | private final CharSequence mTitle; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 132 | private boolean mHasDrawn; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 133 | private long mShownTime; |
| 134 | private final Handler mHandler; |
| 135 | private boolean mSizeMismatch; |
| 136 | private final Paint mBackgroundPaint = new Paint(); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 137 | private final int mStatusBarColor; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 138 | @VisibleForTesting final SystemBarBackgroundPainter mSystemBarBackgroundPainter; |
Jorim Jaggi | 38d44ec | 2017-06-14 16:04:59 -0700 | [diff] [blame] | 139 | private final int mOrientationOnCreation; |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 140 | private final SurfaceControl.Transaction mTransaction; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 141 | |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 142 | static TaskSnapshotSurface create(WindowManagerService service, ActivityRecord activity, |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 143 | TaskSnapshot snapshot) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 144 | |
| 145 | final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); |
| 146 | final Window window = new Window(); |
| 147 | final IWindowSession session = WindowManagerGlobal.getWindowSession(); |
| 148 | window.setSession(session); |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 149 | final SurfaceControl surfaceControl = new SurfaceControl(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 150 | final Rect tmpRect = new Rect(); |
Adrian Roos | 5c6b622 | 2017-11-07 17:36:10 +0100 | [diff] [blame] | 151 | final DisplayCutout.ParcelableWrapper tmpCutout = new DisplayCutout.ParcelableWrapper(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 152 | final Rect tmpFrame = new Rect(); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 153 | final Rect taskBounds; |
| 154 | final Rect tmpContentInsets = new Rect(); |
| 155 | final Rect tmpStableInsets = new Rect(); |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 156 | final InsetsState mTmpInsetsState = new InsetsState(); |
Andrii Kulian | 4460796 | 2017-03-16 11:06:24 -0700 | [diff] [blame] | 157 | final MergedConfiguration tmpMergedConfiguration = new MergedConfiguration(); |
Adrian Roos | 4c86459 | 2019-04-10 14:47:57 +0200 | [diff] [blame] | 158 | final TaskDescription taskDescription = new TaskDescription(); |
| 159 | taskDescription.setBackgroundColor(WHITE); |
Riddle Hsu | 84bc3d7 | 2019-12-19 19:24:07 +0800 | [diff] [blame] | 160 | final WindowState topFullscreenOpaqueWindow; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 161 | final int sysUiVis; |
| 162 | final int windowFlags; |
| 163 | final int windowPrivateFlags; |
Jorim Jaggi | 38d44ec | 2017-06-14 16:04:59 -0700 | [diff] [blame] | 164 | final int currentOrientation; |
Wale Ogunwale | db485de | 2018-10-29 09:47:07 -0700 | [diff] [blame] | 165 | synchronized (service.mGlobalLock) { |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 166 | final WindowState mainWindow = activity.findMainWindow(); |
| 167 | final Task task = activity.getTask(); |
Jorim Jaggi | e6c6ecb | 2017-07-20 18:09:20 +0200 | [diff] [blame] | 168 | if (task == null) { |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 169 | Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find task for activity=" |
| 170 | + activity); |
Jorim Jaggi | e6c6ecb | 2017-07-20 18:09:20 +0200 | [diff] [blame] | 171 | return null; |
| 172 | } |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 173 | final ActivityRecord topFullscreenActivity = |
| 174 | activity.getTask().getTopFullscreenActivity(); |
| 175 | if (topFullscreenActivity == null) { |
Jorim Jaggi | e6c6ecb | 2017-07-20 18:09:20 +0200 | [diff] [blame] | 176 | Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find top fullscreen for task=" |
| 177 | + task); |
| 178 | return null; |
| 179 | } |
Riddle Hsu | 84bc3d7 | 2019-12-19 19:24:07 +0800 | [diff] [blame] | 180 | topFullscreenOpaqueWindow = topFullscreenActivity.getTopFullscreenOpaqueWindow(); |
| 181 | if (mainWindow == null || topFullscreenOpaqueWindow == null) { |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 182 | Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find main window for activity=" |
| 183 | + activity); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 184 | return null; |
| 185 | } |
Riddle Hsu | 84bc3d7 | 2019-12-19 19:24:07 +0800 | [diff] [blame] | 186 | sysUiVis = topFullscreenOpaqueWindow.getSystemUiVisibility(); |
| 187 | windowFlags = topFullscreenOpaqueWindow.getAttrs().flags; |
| 188 | windowPrivateFlags = topFullscreenOpaqueWindow.getAttrs().privateFlags; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 189 | |
Jorim Jaggi | 90b3c47 | 2018-04-06 16:02:07 +0200 | [diff] [blame] | 190 | layoutParams.packageName = mainWindow.getAttrs().packageName; |
| 191 | layoutParams.windowAnimations = mainWindow.getAttrs().windowAnimations; |
Winson Chung | be9be7f | 2017-06-28 10:55:22 -0700 | [diff] [blame] | 192 | layoutParams.dimAmount = mainWindow.getAttrs().dimAmount; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 193 | layoutParams.type = TYPE_APPLICATION_STARTING; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 194 | layoutParams.format = snapshot.getSnapshot().getFormat(); |
| 195 | layoutParams.flags = (windowFlags & ~FLAG_INHERIT_EXCLUDES) |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 196 | | FLAG_NOT_FOCUSABLE |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 197 | | FLAG_NOT_TOUCHABLE; |
Wale Ogunwale | 01ad434 | 2017-06-30 07:07:01 -0700 | [diff] [blame] | 198 | layoutParams.privateFlags = windowPrivateFlags & PRIVATE_FLAG_INHERITS; |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 199 | layoutParams.token = activity.token; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 200 | layoutParams.width = LayoutParams.MATCH_PARENT; |
| 201 | layoutParams.height = LayoutParams.MATCH_PARENT; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 202 | layoutParams.systemUiVisibility = sysUiVis; |
Jorim Jaggi | e6c6ecb | 2017-07-20 18:09:20 +0200 | [diff] [blame] | 203 | layoutParams.setTitle(String.format(TITLE_FORMAT, task.mTaskId)); |
Bryce Lee | 6d41026 | 2017-02-28 15:30:17 -0800 | [diff] [blame] | 204 | |
Adrian Roos | 4c86459 | 2019-04-10 14:47:57 +0200 | [diff] [blame] | 205 | final TaskDescription td = task.getTaskDescription(); |
| 206 | if (td != null) { |
Mehdi Alizadeh | c5e2990 | 2019-11-14 17:18:44 -0800 | [diff] [blame] | 207 | taskDescription.copyFromPreserveHiddenFields(td); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 208 | } |
Jorim Jaggi | e6c6ecb | 2017-07-20 18:09:20 +0200 | [diff] [blame] | 209 | taskBounds = new Rect(); |
| 210 | task.getBounds(taskBounds); |
Riddle Hsu | 84bc3d7 | 2019-12-19 19:24:07 +0800 | [diff] [blame] | 211 | currentOrientation = topFullscreenOpaqueWindow.getConfiguration().orientation; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 212 | } |
| 213 | try { |
| 214 | final int res = session.addToDisplay(window, window.mSeq, layoutParams, |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 215 | View.GONE, activity.getDisplayContent().getDisplayId(), tmpFrame, tmpRect, tmpRect, |
Jorim Jaggi | f081f06 | 2019-10-24 16:24:54 +0200 | [diff] [blame] | 216 | tmpCutout, null, mTmpInsetsState); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 217 | if (res < 0) { |
| 218 | Slog.w(TAG, "Failed to add snapshot starting window res=" + res); |
| 219 | return null; |
| 220 | } |
| 221 | } catch (RemoteException e) { |
| 222 | // Local call. |
| 223 | } |
| 224 | final TaskSnapshotSurface snapshotSurface = new TaskSnapshotSurface(service, window, |
Adrian Roos | 4c86459 | 2019-04-10 14:47:57 +0200 | [diff] [blame] | 225 | surfaceControl, snapshot, layoutParams.getTitle(), taskDescription, sysUiVis, |
| 226 | windowFlags, windowPrivateFlags, taskBounds, |
Jorim Jaggi | 0dd0cf9 | 2019-12-27 15:17:44 +0100 | [diff] [blame] | 227 | currentOrientation, topFullscreenOpaqueWindow.getRequestedInsetsState()); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 228 | window.setOuter(snapshotSurface); |
| 229 | try { |
chaviw | be43ac8 | 2018-04-04 15:14:49 -0700 | [diff] [blame] | 230 | session.relayout(window, window.mSeq, layoutParams, -1, -1, View.VISIBLE, 0, -1, |
Jorim Jaggi | f081f06 | 2019-10-24 16:24:54 +0200 | [diff] [blame] | 231 | tmpFrame, tmpContentInsets, tmpRect, tmpStableInsets, tmpRect, |
Valerie Hau | 3036055 | 2020-01-14 16:12:01 -0800 | [diff] [blame^] | 232 | tmpCutout, tmpMergedConfiguration, surfaceControl, mTmpInsetsState, |
| 233 | sSurfaceSize); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 234 | } catch (RemoteException e) { |
| 235 | // Local call. |
| 236 | } |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 237 | snapshotSurface.setFrames(tmpFrame, tmpContentInsets, tmpStableInsets); |
| 238 | snapshotSurface.drawSnapshot(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 239 | return snapshotSurface; |
| 240 | } |
| 241 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 242 | @VisibleForTesting |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 243 | TaskSnapshotSurface(WindowManagerService service, Window window, SurfaceControl surfaceControl, |
Adrian Roos | 4c86459 | 2019-04-10 14:47:57 +0200 | [diff] [blame] | 244 | TaskSnapshot snapshot, CharSequence title, TaskDescription taskDescription, |
| 245 | int sysUiVis, int windowFlags, int windowPrivateFlags, Rect taskBounds, |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 246 | int currentOrientation, InsetsState insetsState) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 247 | mService = service; |
Vishnu Nair | 3319739 | 2019-08-30 10:29:37 -0700 | [diff] [blame] | 248 | mSurface = service.mSurfaceFactory.get(); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 249 | mHandler = new Handler(mService.mH.getLooper()); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 250 | mSession = WindowManagerGlobal.getWindowSession(); |
| 251 | mWindow = window; |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 252 | mSurfaceControl = surfaceControl; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 253 | mSnapshot = snapshot; |
| 254 | mTitle = title; |
Adrian Roos | 4c86459 | 2019-04-10 14:47:57 +0200 | [diff] [blame] | 255 | int backgroundColor = taskDescription.getBackgroundColor(); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 256 | mBackgroundPaint.setColor(backgroundColor != 0 ? backgroundColor : WHITE); |
| 257 | mTaskBounds = taskBounds; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 258 | mSystemBarBackgroundPainter = new SystemBarBackgroundPainter(windowFlags, |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 259 | windowPrivateFlags, sysUiVis, taskDescription, 1f, insetsState); |
Adrian Roos | 4c86459 | 2019-04-10 14:47:57 +0200 | [diff] [blame] | 260 | mStatusBarColor = taskDescription.getStatusBarColor(); |
Jorim Jaggi | 38d44ec | 2017-06-14 16:04:59 -0700 | [diff] [blame] | 261 | mOrientationOnCreation = currentOrientation; |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 262 | mTransaction = mService.mTransactionFactory.get(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void remove() { |
Wale Ogunwale | db485de | 2018-10-29 09:47:07 -0700 | [diff] [blame] | 267 | synchronized (mService.mGlobalLock) { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 268 | final long now = SystemClock.uptimeMillis(); |
| 269 | if (mSizeMismatch && now - mShownTime < SIZE_MISMATCH_MINIMUM_TIME_MS) { |
| 270 | mHandler.postAtTime(this::remove, mShownTime + SIZE_MISMATCH_MINIMUM_TIME_MS); |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 271 | ProtoLog.v(WM_DEBUG_STARTING_WINDOW, |
| 272 | "Defer removing snapshot surface in %dms", (now - mShownTime)); |
| 273 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 277 | try { |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 278 | ProtoLog.v(WM_DEBUG_STARTING_WINDOW, "Removing snapshot surface"); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 279 | mSession.remove(mWindow); |
| 280 | } catch (RemoteException e) { |
| 281 | // Local call. |
| 282 | } |
| 283 | } |
| 284 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 285 | @VisibleForTesting |
| 286 | void setFrames(Rect frame, Rect contentInsets, Rect stableInsets) { |
| 287 | mFrame.set(frame); |
| 288 | mContentInsets.set(contentInsets); |
| 289 | mStableInsets.set(stableInsets); |
| 290 | mSizeMismatch = (mFrame.width() != mSnapshot.getSnapshot().getWidth() |
| 291 | || mFrame.height() != mSnapshot.getSnapshot().getHeight()); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 292 | mSystemBarBackgroundPainter.setInsets(contentInsets, stableInsets); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | private void drawSnapshot() { |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 296 | mSurface.copyFrom(mSurfaceControl); |
| 297 | |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 298 | ProtoLog.v(WM_DEBUG_STARTING_WINDOW, "Drawing snapshot surface sizeMismatch=%b", |
| 299 | mSizeMismatch); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 300 | if (mSizeMismatch) { |
| 301 | // The dimensions of the buffer and the window don't match, so attaching the buffer |
| 302 | // will fail. Better create a child window with the exact dimensions and fill the parent |
| 303 | // window with the background color! |
Peiyong Lin | ccc06b6 | 2019-06-25 17:31:09 -0700 | [diff] [blame] | 304 | drawSizeMismatchSnapshot(); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 305 | } else { |
Peiyong Lin | ccc06b6 | 2019-06-25 17:31:09 -0700 | [diff] [blame] | 306 | drawSizeMatchSnapshot(); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 307 | } |
Wale Ogunwale | db485de | 2018-10-29 09:47:07 -0700 | [diff] [blame] | 308 | synchronized (mService.mGlobalLock) { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 309 | mShownTime = SystemClock.uptimeMillis(); |
Jorim Jaggi | 39367cf | 2017-03-13 16:41:13 +0100 | [diff] [blame] | 310 | mHasDrawn = true; |
Jorim Jaggi | 39367cf | 2017-03-13 16:41:13 +0100 | [diff] [blame] | 311 | } |
Jorim Jaggi | dc9385a | 2017-05-13 02:00:31 +0200 | [diff] [blame] | 312 | reportDrawn(); |
Jorim Jaggi | 4448e1e | 2017-05-16 22:26:02 +0200 | [diff] [blame] | 313 | |
| 314 | // In case window manager leaks us, make sure we don't retain the snapshot. |
| 315 | mSnapshot = null; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 316 | } |
| 317 | |
Peiyong Lin | ccc06b6 | 2019-06-25 17:31:09 -0700 | [diff] [blame] | 318 | private void drawSizeMatchSnapshot() { |
| 319 | mSurface.attachAndQueueBufferWithColorSpace(mSnapshot.getSnapshot(), |
| 320 | mSnapshot.getColorSpace()); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 321 | mSurface.release(); |
| 322 | } |
| 323 | |
Peiyong Lin | ccc06b6 | 2019-06-25 17:31:09 -0700 | [diff] [blame] | 324 | private void drawSizeMismatchSnapshot() { |
Shibin George | 9696bd3 | 2018-02-26 20:29:44 +0530 | [diff] [blame] | 325 | if (!mSurface.isValid()) { |
| 326 | throw new IllegalStateException("mSurface does not hold a valid surface."); |
| 327 | } |
Peiyong Lin | ccc06b6 | 2019-06-25 17:31:09 -0700 | [diff] [blame] | 328 | final GraphicBuffer buffer = mSnapshot.getSnapshot(); |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 329 | final SurfaceSession session = new SurfaceSession(); |
Winson Chung | 357deda | 2019-06-23 13:35:41 -0700 | [diff] [blame] | 330 | // We consider nearly matched dimensions as there can be rounding errors and the user won't |
| 331 | // notice very minute differences from scaling one dimension more than the other |
| 332 | final boolean aspectRatioMismatch = Math.abs( |
| 333 | ((float) buffer.getWidth() / buffer.getHeight()) |
| 334 | - ((float) mFrame.width() / mFrame.height())) > 0.01f; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 335 | |
| 336 | // Keep a reference to it such that it doesn't get destroyed when finalized. |
Vishnu Nair | 3319739 | 2019-08-30 10:29:37 -0700 | [diff] [blame] | 337 | mChildSurfaceControl = mService.mSurfaceControlFactory.apply(session) |
Robert Carr | e625fcf | 2017-09-01 12:36:28 -0700 | [diff] [blame] | 338 | .setName(mTitle + " - task-snapshot-surface") |
Vishnu Nair | e86bd98 | 2018-11-28 13:23:17 -0800 | [diff] [blame] | 339 | .setBufferSize(buffer.getWidth(), buffer.getHeight()) |
Robert Carr | e625fcf | 2017-09-01 12:36:28 -0700 | [diff] [blame] | 340 | .setFormat(buffer.getFormat()) |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 341 | .setParent(mSurfaceControl) |
Robert Carr | e625fcf | 2017-09-01 12:36:28 -0700 | [diff] [blame] | 342 | .build(); |
Vishnu Nair | 3319739 | 2019-08-30 10:29:37 -0700 | [diff] [blame] | 343 | Surface surface = mService.mSurfaceFactory.get(); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 344 | surface.copyFrom(mChildSurfaceControl); |
| 345 | |
Winson Chung | 357deda | 2019-06-23 13:35:41 -0700 | [diff] [blame] | 346 | final Rect frame; |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 347 | // We can just show the surface here as it will still be hidden as the parent is |
| 348 | // still hidden. |
| 349 | mTransaction.show(mChildSurfaceControl); |
| 350 | if (aspectRatioMismatch) { |
| 351 | // Clip off ugly navigation bar. |
| 352 | final Rect crop = calculateSnapshotCrop(); |
| 353 | frame = calculateSnapshotFrame(crop); |
| 354 | mTransaction.setWindowCrop(mChildSurfaceControl, crop); |
| 355 | mTransaction.setPosition(mChildSurfaceControl, frame.left, frame.top); |
| 356 | } else { |
| 357 | frame = null; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 358 | } |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 359 | |
| 360 | // Scale the mismatch dimensions to fill the task bounds |
| 361 | final float scale = 1 / mSnapshot.getScale(); |
| 362 | mTransaction.setMatrix(mChildSurfaceControl, scale, 0, 0, scale); |
| 363 | mTransaction.apply(); |
Peiyong Lin | ccc06b6 | 2019-06-25 17:31:09 -0700 | [diff] [blame] | 364 | surface.attachAndQueueBufferWithColorSpace(buffer, mSnapshot.getColorSpace()); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 365 | surface.release(); |
| 366 | |
Winson Chung | 357deda | 2019-06-23 13:35:41 -0700 | [diff] [blame] | 367 | if (aspectRatioMismatch) { |
| 368 | final Canvas c = mSurface.lockCanvas(null); |
| 369 | drawBackgroundAndBars(c, frame); |
| 370 | mSurface.unlockCanvasAndPost(c); |
| 371 | mSurface.release(); |
| 372 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 375 | /** |
| 376 | * Calculates the snapshot crop in snapshot coordinate space. |
| 377 | * |
| 378 | * @return crop rect in snapshot coordinate space. |
| 379 | */ |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 380 | @VisibleForTesting |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 381 | Rect calculateSnapshotCrop() { |
| 382 | final Rect rect = new Rect(); |
| 383 | rect.set(0, 0, mSnapshot.getSnapshot().getWidth(), mSnapshot.getSnapshot().getHeight()); |
| 384 | final Rect insets = mSnapshot.getContentInsets(); |
| 385 | |
| 386 | // Let's remove all system decorations except the status bar, but only if the task is at the |
| 387 | // very top of the screen. |
Adrian Roos | 98a146d | 2017-11-29 16:39:44 +0100 | [diff] [blame] | 388 | final boolean isTop = mTaskBounds.top == 0 && mFrame.top == 0; |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 389 | rect.inset((int) (insets.left * mSnapshot.getScale()), |
Adrian Roos | 98a146d | 2017-11-29 16:39:44 +0100 | [diff] [blame] | 390 | isTop ? 0 : (int) (insets.top * mSnapshot.getScale()), |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 391 | (int) (insets.right * mSnapshot.getScale()), |
| 392 | (int) (insets.bottom * mSnapshot.getScale())); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 393 | return rect; |
| 394 | } |
| 395 | |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 396 | /** |
| 397 | * Calculates the snapshot frame in window coordinate space from crop. |
| 398 | * |
| 399 | * @param crop rect that is in snapshot coordinate space. |
| 400 | */ |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 401 | @VisibleForTesting |
| 402 | Rect calculateSnapshotFrame(Rect crop) { |
| 403 | final Rect frame = new Rect(crop); |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 404 | final float scale = mSnapshot.getScale(); |
| 405 | |
| 406 | // Rescale the frame from snapshot to window coordinate space |
| 407 | frame.scale(1 / scale); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 408 | |
| 409 | // By default, offset it to to top/left corner |
Matthew Ng | cb7ac67 | 2017-07-21 17:27:42 -0700 | [diff] [blame] | 410 | frame.offsetTo((int) (-crop.left / scale), (int) (-crop.top / scale)); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 411 | |
| 412 | // However, we also need to make space for the navigation bar on the left side. |
| 413 | final int colorViewLeftInset = getColorViewLeftInset(mStableInsets.left, |
| 414 | mContentInsets.left); |
| 415 | frame.offset(colorViewLeftInset, 0); |
| 416 | return frame; |
| 417 | } |
| 418 | |
| 419 | @VisibleForTesting |
| 420 | void drawBackgroundAndBars(Canvas c, Rect frame) { |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 421 | final int statusBarHeight = mSystemBarBackgroundPainter.getStatusBarColorViewHeight(); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 422 | final boolean fillHorizontally = c.getWidth() > frame.right; |
| 423 | final boolean fillVertically = c.getHeight() > frame.bottom; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 424 | if (fillHorizontally) { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 425 | c.drawRect(frame.right, alpha(mStatusBarColor) == 0xFF ? statusBarHeight : 0, |
| 426 | c.getWidth(), fillVertically |
| 427 | ? frame.bottom |
| 428 | : c.getHeight(), |
| 429 | mBackgroundPaint); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 430 | } |
| 431 | if (fillVertically) { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 432 | c.drawRect(0, frame.bottom, c.getWidth(), c.getHeight(), mBackgroundPaint); |
| 433 | } |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 434 | mSystemBarBackgroundPainter.drawDecors(c, frame); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 437 | private void reportDrawn() { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 438 | try { |
Vishnu Nair | f7645aa | 2019-06-18 11:14:01 -0700 | [diff] [blame] | 439 | mSession.finishDrawing(mWindow, null /* postDrawTransaction */); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 440 | } catch (RemoteException e) { |
| 441 | // Local call. |
| 442 | } |
| 443 | } |
| 444 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 445 | private static Handler sHandler = new Handler(Looper.getMainLooper()) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 446 | |
| 447 | @Override |
| 448 | public void handleMessage(Message msg) { |
| 449 | switch (msg.what) { |
| 450 | case MSG_REPORT_DRAW: |
| 451 | final boolean hasDrawn; |
| 452 | final TaskSnapshotSurface surface = (TaskSnapshotSurface) msg.obj; |
Wale Ogunwale | db485de | 2018-10-29 09:47:07 -0700 | [diff] [blame] | 453 | synchronized (surface.mService.mGlobalLock) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 454 | hasDrawn = surface.mHasDrawn; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 455 | } |
| 456 | if (hasDrawn) { |
| 457 | surface.reportDrawn(); |
| 458 | } |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | }; |
| 463 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 464 | @VisibleForTesting |
| 465 | static class Window extends BaseIWindow { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 466 | |
| 467 | private TaskSnapshotSurface mOuter; |
Jorim Jaggi | 38d44ec | 2017-06-14 16:04:59 -0700 | [diff] [blame] | 468 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 469 | public void setOuter(TaskSnapshotSurface outer) { |
| 470 | mOuter = outer; |
| 471 | } |
| 472 | |
| 473 | @Override |
Jorim Jaggi | f081f06 | 2019-10-24 16:24:54 +0200 | [diff] [blame] | 474 | public void resized(Rect frame, Rect contentInsets, Rect visibleInsets, |
| 475 | Rect stableInsets, boolean reportDraw, |
Andrii Kulian | 4460796 | 2017-03-16 11:06:24 -0700 | [diff] [blame] | 476 | MergedConfiguration mergedConfiguration, Rect backDropFrame, boolean forceLayout, |
Brad Stenning | e057369 | 2019-03-11 13:52:46 -0700 | [diff] [blame] | 477 | boolean alwaysConsumeSystemBars, int displayId, |
Adrian Roos | 5c6b622 | 2017-11-07 17:36:10 +0100 | [diff] [blame] | 478 | DisplayCutout.ParcelableWrapper displayCutout) { |
Jorim Jaggi | 38d44ec | 2017-06-14 16:04:59 -0700 | [diff] [blame] | 479 | if (mergedConfiguration != null && mOuter != null |
| 480 | && mOuter.mOrientationOnCreation |
| 481 | != mergedConfiguration.getMergedConfiguration().orientation) { |
| 482 | |
| 483 | // The orientation of the screen is changing. We better remove the snapshot ASAP as |
| 484 | // we are going to wait on the new window in any case to unfreeze the screen, and |
| 485 | // the starting window is not needed anymore. |
| 486 | sHandler.post(mOuter::remove); |
| 487 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 488 | if (reportDraw) { |
| 489 | sHandler.obtainMessage(MSG_REPORT_DRAW, mOuter).sendToTarget(); |
| 490 | } |
| 491 | } |
| 492 | } |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 493 | |
| 494 | /** |
| 495 | * Helper class to draw the background of the system bars in regions the task snapshot isn't |
| 496 | * filling the window. |
| 497 | */ |
| 498 | static class SystemBarBackgroundPainter { |
| 499 | |
| 500 | private final Rect mContentInsets = new Rect(); |
| 501 | private final Rect mStableInsets = new Rect(); |
| 502 | private final Paint mStatusBarPaint = new Paint(); |
| 503 | private final Paint mNavigationBarPaint = new Paint(); |
| 504 | private final int mStatusBarColor; |
| 505 | private final int mNavigationBarColor; |
| 506 | private final int mWindowFlags; |
| 507 | private final int mWindowPrivateFlags; |
| 508 | private final int mSysUiVis; |
Winson Chung | ffde2ea | 2019-06-17 17:19:13 -0700 | [diff] [blame] | 509 | private final float mScale; |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 510 | private final InsetsState mInsetsState; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 511 | |
Winson Chung | ffde2ea | 2019-06-17 17:19:13 -0700 | [diff] [blame] | 512 | SystemBarBackgroundPainter(int windowFlags, int windowPrivateFlags, int sysUiVis, |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 513 | TaskDescription taskDescription, float scale, InsetsState insetsState) { |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 514 | mWindowFlags = windowFlags; |
| 515 | mWindowPrivateFlags = windowPrivateFlags; |
| 516 | mSysUiVis = sysUiVis; |
Winson Chung | ffde2ea | 2019-06-17 17:19:13 -0700 | [diff] [blame] | 517 | mScale = scale; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 518 | final Context context = ActivityThread.currentActivityThread().getSystemUiContext(); |
Jorim Jaggi | a6aabac | 2019-03-11 14:23:16 -0700 | [diff] [blame] | 519 | final int semiTransparent = context.getColor( |
| 520 | R.color.system_bar_background_semi_transparent); |
| 521 | mStatusBarColor = DecorView.calculateBarColor(windowFlags, FLAG_TRANSLUCENT_STATUS, |
Adrian Roos | 4c86459 | 2019-04-10 14:47:57 +0200 | [diff] [blame] | 522 | semiTransparent, taskDescription.getStatusBarColor(), sysUiVis, |
| 523 | SYSTEM_UI_FLAG_LIGHT_STATUS_BAR, |
| 524 | taskDescription.getEnsureStatusBarContrastWhenTransparent()); |
Jorim Jaggi | a6aabac | 2019-03-11 14:23:16 -0700 | [diff] [blame] | 525 | mNavigationBarColor = DecorView.calculateBarColor(windowFlags, |
Adrian Roos | 4c86459 | 2019-04-10 14:47:57 +0200 | [diff] [blame] | 526 | FLAG_TRANSLUCENT_NAVIGATION, semiTransparent, |
| 527 | taskDescription.getNavigationBarColor(), sysUiVis, |
| 528 | SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR, |
| 529 | taskDescription.getEnsureNavigationBarContrastWhenTransparent() |
| 530 | && context.getResources().getBoolean(R.bool.config_navBarNeedsScrim)); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 531 | mStatusBarPaint.setColor(mStatusBarColor); |
Adrian Roos | 4c86459 | 2019-04-10 14:47:57 +0200 | [diff] [blame] | 532 | mNavigationBarPaint.setColor(mNavigationBarColor); |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 533 | mInsetsState = insetsState; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void setInsets(Rect contentInsets, Rect stableInsets) { |
| 537 | mContentInsets.set(contentInsets); |
| 538 | mStableInsets.set(stableInsets); |
| 539 | } |
| 540 | |
| 541 | int getStatusBarColorViewHeight() { |
Jorim Jaggi | a6aabac | 2019-03-11 14:23:16 -0700 | [diff] [blame] | 542 | final boolean forceBarBackground = |
| 543 | (mWindowPrivateFlags & PRIVATE_FLAG_FORCE_DRAW_BAR_BACKGROUNDS) != 0; |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 544 | if (ViewRootImpl.sNewInsetsMode != ViewRootImpl.NEW_INSETS_MODE_FULL |
| 545 | ? STATUS_BAR_COLOR_VIEW_ATTRIBUTES.isVisible( |
| 546 | mSysUiVis, mStatusBarColor, mWindowFlags, forceBarBackground) |
| 547 | : STATUS_BAR_COLOR_VIEW_ATTRIBUTES.isVisible( |
| 548 | mInsetsState, mStatusBarColor, mWindowFlags, forceBarBackground)) { |
Winson Chung | ffde2ea | 2019-06-17 17:19:13 -0700 | [diff] [blame] | 549 | return (int) (getColorViewTopInset(mStableInsets.top, mContentInsets.top) * mScale); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 550 | } else { |
| 551 | return 0; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | private boolean isNavigationBarColorViewVisible() { |
Jorim Jaggi | a6aabac | 2019-03-11 14:23:16 -0700 | [diff] [blame] | 556 | final boolean forceBarBackground = |
| 557 | (mWindowPrivateFlags & PRIVATE_FLAG_FORCE_DRAW_BAR_BACKGROUNDS) != 0; |
Tiger Huang | 4a7835f | 2019-11-06 00:07:56 +0800 | [diff] [blame] | 558 | return ViewRootImpl.sNewInsetsMode != ViewRootImpl.NEW_INSETS_MODE_FULL |
| 559 | ? NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES.isVisible( |
| 560 | mSysUiVis, mNavigationBarColor, mWindowFlags, forceBarBackground) |
| 561 | : NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES.isVisible( |
| 562 | mInsetsState, mNavigationBarColor, mWindowFlags, forceBarBackground); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | void drawDecors(Canvas c, @Nullable Rect alreadyDrawnFrame) { |
| 566 | drawStatusBarBackground(c, alreadyDrawnFrame, getStatusBarColorViewHeight()); |
| 567 | drawNavigationBarBackground(c); |
| 568 | } |
| 569 | |
| 570 | @VisibleForTesting |
| 571 | void drawStatusBarBackground(Canvas c, @Nullable Rect alreadyDrawnFrame, |
| 572 | int statusBarHeight) { |
Winson Chung | 51f42d2 | 2018-02-01 14:59:38 -0800 | [diff] [blame] | 573 | if (statusBarHeight > 0 && Color.alpha(mStatusBarColor) != 0 |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 574 | && (alreadyDrawnFrame == null || c.getWidth() > alreadyDrawnFrame.right)) { |
Winson Chung | ffde2ea | 2019-06-17 17:19:13 -0700 | [diff] [blame] | 575 | final int rightInset = (int) (DecorView.getColorViewRightInset(mStableInsets.right, |
| 576 | mContentInsets.right) * mScale); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 577 | final int left = alreadyDrawnFrame != null ? alreadyDrawnFrame.right : 0; |
| 578 | c.drawRect(left, 0, c.getWidth() - rightInset, statusBarHeight, mStatusBarPaint); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | @VisibleForTesting |
| 583 | void drawNavigationBarBackground(Canvas c) { |
| 584 | final Rect navigationBarRect = new Rect(); |
| 585 | getNavigationBarRect(c.getWidth(), c.getHeight(), mStableInsets, mContentInsets, |
Winson Chung | ffde2ea | 2019-06-17 17:19:13 -0700 | [diff] [blame] | 586 | navigationBarRect, mScale); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 587 | final boolean visible = isNavigationBarColorViewVisible(); |
Winson Chung | 51f42d2 | 2018-02-01 14:59:38 -0800 | [diff] [blame] | 588 | if (visible && Color.alpha(mNavigationBarColor) != 0 && !navigationBarRect.isEmpty()) { |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 589 | c.drawRect(navigationBarRect, mNavigationBarPaint); |
| 590 | } |
| 591 | } |
| 592 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 593 | } |