blob: 4a9a3f71f90e5f5ca16a6da02e931b9c83a69856 [file] [log] [blame]
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001/*
2 * Copyright (C) 2011 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
Evan Rosky2289ba12018-11-19 18:28:18 -080019import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Winson Chunge55c0192017-08-24 14:50:48 -070020import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
Jorim Jaggic6976f02018-04-18 16:31:07 +020021import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Winson Chunge55c0192017-08-24 14:50:48 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +020023import static android.content.pm.ActivityInfo.COLOR_MODE_DEFAULT;
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070025import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020026import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Jorim Jaggife762342016-10-13 14:33:27 +020027import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020028import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -080029import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Jorim Jaggife762342016-10-13 14:33:27 +020030import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Wale Ogunwaled1c37912016-08-16 03:19:39 -070031import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080032import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070033import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -080034import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
Evan Rosky2289ba12018-11-19 18:28:18 -080035import static android.view.WindowManager.TRANSIT_TASK_CHANGE_WINDOWING_MODE;
chaviw9c81e632018-07-31 11:17:52 -070036import static android.view.WindowManager.TRANSIT_UNSET;
Jorim Jaggic6976f02018-04-18 16:31:07 +020037import static android.view.WindowManager.TRANSIT_WALLPAPER_OPEN;
chaviw9c81e632018-07-31 11:17:52 -070038
Adrian Roose99bc052017-11-20 17:55:31 +010039import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
40import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070041import static com.android.server.wm.AppWindowTokenProto.ALL_DRAWN;
42import static com.android.server.wm.AppWindowTokenProto.APP_STOPPED;
43import static com.android.server.wm.AppWindowTokenProto.CLIENT_HIDDEN;
44import static com.android.server.wm.AppWindowTokenProto.DEFER_HIDING_CLIENT;
45import static com.android.server.wm.AppWindowTokenProto.FILLS_PARENT;
46import static com.android.server.wm.AppWindowTokenProto.FROZEN_BOUNDS;
47import static com.android.server.wm.AppWindowTokenProto.HIDDEN_REQUESTED;
48import static com.android.server.wm.AppWindowTokenProto.HIDDEN_SET_FROM_TRANSFERRED_STARTING_WINDOW;
49import static com.android.server.wm.AppWindowTokenProto.IS_REALLY_ANIMATING;
50import static com.android.server.wm.AppWindowTokenProto.IS_WAITING_FOR_TRANSITION_START;
51import static com.android.server.wm.AppWindowTokenProto.LAST_ALL_DRAWN;
52import static com.android.server.wm.AppWindowTokenProto.LAST_SURFACE_SHOWING;
53import static com.android.server.wm.AppWindowTokenProto.NAME;
54import static com.android.server.wm.AppWindowTokenProto.NUM_DRAWN_WINDOWS;
55import static com.android.server.wm.AppWindowTokenProto.NUM_INTERESTING_WINDOWS;
56import static com.android.server.wm.AppWindowTokenProto.REMOVED;
57import static com.android.server.wm.AppWindowTokenProto.REPORTED_DRAWN;
58import static com.android.server.wm.AppWindowTokenProto.REPORTED_VISIBLE;
59import static com.android.server.wm.AppWindowTokenProto.STARTING_DISPLAYED;
60import static com.android.server.wm.AppWindowTokenProto.STARTING_MOVED;
61import static com.android.server.wm.AppWindowTokenProto.STARTING_WINDOW;
62import static com.android.server.wm.AppWindowTokenProto.THUMBNAIL;
63import static com.android.server.wm.AppWindowTokenProto.WINDOW_TOKEN;
chaviw9c81e632018-07-31 11:17:52 -070064import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
65import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
66import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
67import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
68import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
69import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
70import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
71import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW_VERBOSE;
72import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
73import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
74import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
75import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
76import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
77import static com.android.server.wm.WindowManagerService.H.NOTIFY_ACTIVITY_DRAWN;
78import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_NORMAL;
79import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
80import static com.android.server.wm.WindowManagerService.logWithStack;
chaviw15ad49f2019-04-24 15:05:39 -070081import static com.android.server.wm.WindowState.LEGACY_POLICY_VISIBILITY;
Vishnu Naira2977262018-07-26 13:31:26 -070082import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_AFTER_ANIM;
Evan Rosky641daea2019-04-24 14:45:24 -070083import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_BEFORE_ANIM;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080084
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070085import android.annotation.CallSuper;
chaviwdcf76ec2019-01-11 16:48:46 -080086import android.annotation.Size;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020087import android.app.Activity;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -080088import android.app.ActivityManager;
Winson Chung48b25652018-10-22 14:04:30 -070089import android.content.ComponentName;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -080090import android.content.res.CompatibilityInfo;
Jorim Jaggi26c8c422016-05-09 19:57:25 -070091import android.content.res.Configuration;
Jorim Jaggi988f6682017-11-17 17:46:43 +010092import android.graphics.GraphicBuffer;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020093import android.graphics.Point;
Jorim Jaggi0429f352015-12-22 16:29:16 +010094import android.graphics.Rect;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070095import android.os.Binder;
Tiger Huang51c5a1d2018-12-11 20:24:51 +080096import android.os.Build;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080097import android.os.Debug;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070098import android.os.IBinder;
Steven Timotiusaf03df62017-07-18 16:56:43 -070099import android.os.RemoteException;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800100import android.os.SystemClock;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200101import android.os.Trace;
Evan Rosky2289ba12018-11-19 18:28:18 -0800102import android.util.ArraySet;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800103import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700104import android.util.proto.ProtoOutputStream;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200105import android.view.DisplayInfo;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800106import android.view.IApplicationToken;
Robert Carr788f5742018-07-30 17:46:45 -0700107import android.view.InputApplicationHandle;
Evan Rosky966759f2019-01-15 10:33:58 -0800108import android.view.RemoteAnimationAdapter;
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100109import android.view.RemoteAnimationDefinition;
Robert Carr6914f082017-03-20 19:04:30 -0700110import android.view.SurfaceControl;
Tony Mak64b8d562017-12-28 17:44:02 +0000111import android.view.SurfaceControl.Transaction;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800112import android.view.WindowManager;
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +0200113import android.view.WindowManager.LayoutParams;
Tony Mak64b8d562017-12-28 17:44:02 +0000114import android.view.animation.Animation;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800115
Tony Mak64b8d562017-12-28 17:44:02 +0000116import com.android.internal.R;
Riddle Hsua118b3a2018-10-11 22:05:06 +0800117import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800118import com.android.internal.util.ToBooleanFunction;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800119import com.android.server.AttributeCache;
chaviwdcf76ec2019-01-11 16:48:46 -0800120import com.android.server.LocalServices;
Christine Franks0ada2772019-02-25 13:54:57 -0800121import com.android.server.display.color.ColorDisplayService;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800122import com.android.server.policy.WindowManagerPolicy;
Adrian Roose99bc052017-11-20 17:55:31 +0100123import com.android.server.policy.WindowManagerPolicy.StartingSurface;
Evan Rosky2289ba12018-11-19 18:28:18 -0800124import com.android.server.wm.RemoteAnimationController.RemoteAnimationRecord;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800125import com.android.server.wm.WindowManagerService.H;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800126
127import java.io.PrintWriter;
chaviwdcf76ec2019-01-11 16:48:46 -0800128import java.lang.ref.WeakReference;
Jorim Jaggi0429f352015-12-22 16:29:16 +0100129import java.util.ArrayDeque;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800130import java.util.ArrayList;
lumark588a3e82018-07-20 18:53:54 +0800131import java.util.function.Consumer;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800132
133class AppTokenList extends ArrayList<AppWindowToken> {
134}
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800135
136/**
137 * Version of WindowToken that is specifically for a particular application (or
138 * really activity) that is displaying windows.
139 */
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800140class AppWindowToken extends WindowToken implements WindowManagerService.AppFreezeListener,
141 ConfigurationContainerListener {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800142 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowToken" : TAG_WM;
143
Jorim Jaggi619c9f72017-12-19 18:04:29 +0100144 /**
145 * Value to increment the z-layer when boosting a layer during animations. BOOST in l33tsp34k.
146 */
Jorim Jaggi9cd99132019-04-24 17:41:35 +0200147 @VisibleForTesting static final int Z_BOOST_BASE = 800570000;
Jorim Jaggi619c9f72017-12-19 18:04:29 +0100148
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800149 // Non-null only for application tokens.
150 final IApplicationToken appToken;
Winson Chung48b25652018-10-22 14:04:30 -0700151 final ComponentName mActivityComponent;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800152 final boolean mVoiceInteraction;
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700153
Wale Ogunwale51362492016-09-08 17:49:17 -0700154 /** @see WindowContainer#fillsParent() */
155 private boolean mFillsParent;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800156 boolean mShowForAllUsers;
157 int mTargetSdk;
Craig Mautnera2c77052012-03-26 12:14:43 -0700158
Bryce Lee6d410262017-02-28 15:30:17 -0800159 // Flag set while reparenting to prevent actions normally triggered by an individual parent
160 // change.
161 private boolean mReparenting;
162
Wale Ogunwalee287e192017-04-21 09:30:12 -0700163 // True if we are current in the process of removing this app token from the display
164 private boolean mRemovingFromDisplay = false;
165
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800166 // The input dispatching timeout for this application token in nanoseconds.
Wale Ogunwale72919d22016-12-08 18:58:50 -0800167 long mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800168
169 // These are used for determining when all windows associated with
170 // an activity have been drawn, so they can be made visible together
171 // at the same time.
Craig Mautner764983d2012-03-22 11:37:36 -0700172 // initialize so that it doesn't match mTransactionSequence which is an int.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700173 private long mLastTransactionSequence = Long.MIN_VALUE;
174 private int mNumInterestingWindows;
175 private int mNumDrawnWindows;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800176 boolean inPendingTransaction;
177 boolean allDrawn;
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000178 private boolean mLastAllDrawn;
Tiger Huangc9d0d002019-05-29 22:55:29 +0800179 private boolean mUseTransferredAnimation;
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000180
Craig Mautner7636dfb2012-11-16 15:24:11 -0800181 // Set to true when this app creates a surface while in the middle of an animation. In that
182 // case do not clear allDrawn until the animation completes.
183 boolean deferClearAllDrawn;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800184
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800185 // Is this window's surface needed? This is almost like hidden, except
186 // it will sometimes be true a little earlier: when the token has
187 // been shown, but is still waiting for its app transition to execute
188 // before making its windows shown.
189 boolean hiddenRequested;
190
191 // Have we told the window clients to hide themselves?
Wale Ogunwale89973222017-04-23 18:39:45 -0700192 private boolean mClientHidden;
193
194 // If true we will defer setting mClientHidden to true and reporting to the client that it is
195 // hidden.
196 boolean mDeferHidingClient;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800197
198 // Last visibility state we reported to the app token.
199 boolean reportedVisible;
200
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700201 // Last drawn state we reported to the app token.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700202 private boolean reportedDrawn;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700203
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800204 // Set to true when the token has been removed from the window mgr.
205 boolean removed;
206
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800207 // Information about an application starting window if displayed.
chaviwa8f07a72019-05-01 16:25:39 -0700208 StartingData mStartingData;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800209 WindowState startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800210 StartingSurface startingSurface;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800211 boolean startingDisplayed;
212 boolean startingMoved;
Jorim Jaggi60f9c972018-02-01 19:21:07 +0100213
Wale Ogunwale6c459212017-05-17 08:56:03 -0700214 // True if the hidden state of this token was forced to false due to a transferred starting
215 // window.
216 private boolean mHiddenSetFromTransferredStartingWindow;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800217 boolean firstWindowDrawn;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700218 private final WindowState.UpdateReportedVisibilityResults mReportedVisibilityResults =
219 new WindowState.UpdateReportedVisibilityResults();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800220
221 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700222 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800223
Wale Ogunwale571771c2016-08-26 13:18:50 -0700224 // TODO: Have a WindowContainer state for tracking exiting/deferred removal.
Craig Mautner799bc1d2015-01-14 10:33:48 -0800225 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800226
Craig Mautnerbb742462014-07-07 15:28:55 -0700227 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700228 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700229
Wale Ogunwale72919d22016-12-08 18:58:50 -0800230 private boolean mAlwaysFocusable;
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800231
Robert Carre12aece2016-02-02 22:43:27 -0800232 boolean mAppStopped;
Robert Carrfd10cd12016-06-29 16:41:50 -0700233 int mRotationAnimationHint;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700234 private int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800235
Jorim Jaggife762342016-10-13 14:33:27 +0200236 private boolean mLastContainsShowWhenLockedWindow;
237 private boolean mLastContainsDismissKeyguardWindow;
238
Jorim Jaggi0429f352015-12-22 16:29:16 +0100239 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700240 ArrayDeque<Configuration> mFrozenMergedConfig = new ArrayDeque<>();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100241
Riddle Hsub398da32019-01-21 21:48:16 +0800242 /**
243 * The scale to fit at least one side of the activity to its parent. If the activity uses
244 * 1920x1080, and the actually size on the screen is 960x540, then the scale is 0.5.
245 */
246 private float mSizeCompatScale = 1f;
247 /**
248 * The bounds in global coordinates for activity in size compatibility mode.
249 * @see ActivityRecord#inSizeCompatMode
250 */
251 private Rect mSizeCompatBounds;
252
Wale Ogunwale6c459212017-05-17 08:56:03 -0700253 private boolean mDisablePreviewScreenshots;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100254
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700255 private Task mLastParent;
Robert Carred3e83b2017-04-21 13:26:55 -0700256
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800257 // TODO: Remove after unification
258 ActivityRecord mActivityRecord;
259
chaviwd3bf08d2017-08-01 17:24:59 -0700260 /**
261 * See {@link #canTurnScreenOn()}
262 */
263 private boolean mCanTurnScreenOn = true;
264
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200265 /**
266 * If we are running an animation, this determines the transition type. Must be one of
267 * AppTransition.TRANSIT_* constants.
268 */
269 private int mTransit;
270
271 /**
272 * If we are running an animation, this determines the flags during this animation. Must be a
273 * bitwise combination of AppTransition.TRANSIT_FLAG_* constants.
274 */
275 private int mTransitFlags;
276
277 /** Whether our surface was set to be showing in the last call to {@link #prepareSurfaces} */
Jorim Jaggifd1891462017-12-29 15:41:36 +0100278 private boolean mLastSurfaceShowing = true;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200279
Evan Rosky2289ba12018-11-19 18:28:18 -0800280 /**
281 * This gets used during some open/close transitions as well as during a change transition
282 * where it represents the starting-state snapshot.
283 */
Jorim Jaggi988f6682017-11-17 17:46:43 +0100284 private AppWindowThumbnail mThumbnail;
Evan Rosky2289ba12018-11-19 18:28:18 -0800285 private final Rect mTransitStartRect = new Rect();
286
287 /**
288 * This leash is used to "freeze" the app surface in place after the state change, but before
289 * the animation is ready to start.
290 */
291 private SurfaceControl mTransitChangeLeash = null;
Jorim Jaggi988f6682017-11-17 17:46:43 +0100292
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000293 /** Have we been asked to have this token keep the screen frozen? */
294 private boolean mFreezingScreen;
295
296 /** Whether this token should be boosted at the top of all app window tokens. */
Jorim Jaggi9cd99132019-04-24 17:41:35 +0200297 @VisibleForTesting boolean mNeedsZBoost;
Adrian Roos4d18a2e2017-12-19 19:08:05 +0100298 private Letterbox mLetterbox;
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000299
chaviw23ee71c2017-12-18 11:29:41 -0800300 private final Point mTmpPoint = new Point();
chaviw23012112017-12-20 15:29:04 -0800301 private final Rect mTmpRect = new Rect();
Evan Rosky2289ba12018-11-19 18:28:18 -0800302 private final Rect mTmpPrevBounds = new Rect();
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100303 private RemoteAnimationDefinition mRemoteAnimationDefinition;
Jorim Jaggi6de61012018-03-19 14:53:23 +0100304 private AnimatingAppWindowTokenRegistry mAnimatingAppWindowTokenRegistry;
chaviw23ee71c2017-12-18 11:29:41 -0800305
chaviw4ad54912018-05-30 11:05:44 -0700306 /**
307 * A flag to determine if this AWT is in the process of closing or entering PIP. This is needed
308 * to help AWT know that the app is in the process of closing but hasn't yet started closing on
309 * the WM side.
310 */
311 private boolean mWillCloseOrEnterPip;
312
Vishnu Naira2977262018-07-26 13:31:26 -0700313 /** Layer used to constrain the animation to a token's stack bounds. */
314 SurfaceControl mAnimationBoundsLayer;
315
316 /** Whether this token needs to create mAnimationBoundsLayer for cropping animations. */
317 boolean mNeedsAnimationBoundsLayer;
318
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800319 private static final int STARTING_WINDOW_TYPE_NONE = 0;
320 private static final int STARTING_WINDOW_TYPE_SNAPSHOT = 1;
321 private static final int STARTING_WINDOW_TYPE_SPLASH_SCREEN = 2;
322
chaviwdcf76ec2019-01-11 16:48:46 -0800323 private AppSaturationInfo mLastAppSaturationInfo;
324
325 private final ColorDisplayService.ColorTransformController mColorTransformController =
326 (matrix, translation) -> mWmService.mH.post(() -> {
327 synchronized (mWmService.mGlobalLock) {
328 if (mLastAppSaturationInfo == null) {
329 mLastAppSaturationInfo = new AppSaturationInfo();
330 }
331
332 mLastAppSaturationInfo.setSaturation(matrix, translation);
333 updateColorTransform();
334 }
335 });
336
Winson Chung48b25652018-10-22 14:04:30 -0700337 AppWindowToken(WindowManagerService service, IApplicationToken token,
338 ComponentName activityComponent, boolean voiceInteraction, DisplayContent dc,
339 long inputDispatchingTimeoutNanos, boolean fullscreen, boolean showForAllUsers,
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100340 int targetSdk, int orientation, int rotationAnimationHint,
Winson Chung48b25652018-10-22 14:04:30 -0700341 boolean launchTaskBehind, boolean alwaysFocusable,
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800342 ActivityRecord activityRecord) {
Winson Chung48b25652018-10-22 14:04:30 -0700343 this(service, token, activityComponent, voiceInteraction, dc, fullscreen);
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800344 // TODO: remove after unification
345 mActivityRecord = activityRecord;
346 mActivityRecord.registerConfigurationChangeListener(this);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800347 mInputDispatchingTimeoutNanos = inputDispatchingTimeoutNanos;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800348 mShowForAllUsers = showForAllUsers;
349 mTargetSdk = targetSdk;
350 mOrientation = orientation;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800351 mLaunchTaskBehind = launchTaskBehind;
352 mAlwaysFocusable = alwaysFocusable;
353 mRotationAnimationHint = rotationAnimationHint;
354
355 // Application tokens start out hidden.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200356 setHidden(true);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800357 hiddenRequested = true;
chaviwdcf76ec2019-01-11 16:48:46 -0800358
359 ColorDisplayService.ColorDisplayServiceInternal cds = LocalServices.getService(
360 ColorDisplayService.ColorDisplayServiceInternal.class);
361 cds.attachColorTransformController(activityRecord.packageName, activityRecord.mUserId,
362 new WeakReference<>(mColorTransformController));
Wale Ogunwale72919d22016-12-08 18:58:50 -0800363 }
364
Winson Chung48b25652018-10-22 14:04:30 -0700365 AppWindowToken(WindowManagerService service, IApplicationToken token,
366 ComponentName activityComponent, boolean voiceInteraction, DisplayContent dc,
367 boolean fillsParent) {
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800368 super(service, token != null ? token.asBinder() : null, TYPE_APPLICATION, true, dc,
369 false /* ownerCanManageAppTokens */);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700370 appToken = token;
Winson Chung48b25652018-10-22 14:04:30 -0700371 mActivityComponent = activityComponent;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800372 mVoiceInteraction = voiceInteraction;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800373 mFillsParent = fillsParent;
Robert Carr0bcbe642018-10-11 19:07:43 -0700374 mInputApplicationHandle = new InputApplicationHandle(appToken.asBinder());
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800375 }
376
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800377 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
378 firstWindowDrawn = true;
379
380 // We now have a good window to show, remove dead placeholders
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700381 removeDeadWindows();
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800382
Jorim Jaggi02886a82016-12-06 09:10:06 -0800383 if (startingWindow != null) {
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800384 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
385 + win.mToken + ": first real window is shown, no animation");
386 // If this initial window is animating, stop it -- we will do an animation to reveal
387 // it from behind the starting window, so there is no need for it to also be doing its
388 // own stuff.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100389 win.cancelAnimation();
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800390 }
chaviwa8f07a72019-05-01 16:25:39 -0700391 removeStartingWindow();
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800392 updateReportedVisibilityLocked();
393 }
394
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800395 void updateReportedVisibilityLocked() {
396 if (appToken == null) {
397 return;
398 }
399
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700400 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700401 final int count = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800402
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700403 mReportedVisibilityResults.reset();
404
405 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700406 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700407 win.updateReportedVisibility(mReportedVisibilityResults);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800408 }
409
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700410 int numInteresting = mReportedVisibilityResults.numInteresting;
411 int numVisible = mReportedVisibilityResults.numVisible;
412 int numDrawn = mReportedVisibilityResults.numDrawn;
413 boolean nowGone = mReportedVisibilityResults.nowGone;
414
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700415 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200416 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting && !isHidden();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700417 if (!nowGone) {
418 // If the app is not yet gone, then it can only become visible/drawn.
419 if (!nowDrawn) {
420 nowDrawn = reportedDrawn;
421 }
422 if (!nowVisible) {
423 nowVisible = reportedVisible;
424 }
425 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800426 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800427 + numInteresting + " visible=" + numVisible);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700428 if (nowDrawn != reportedDrawn) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800429 if (mActivityRecord != null) {
430 mActivityRecord.onWindowsDrawn(nowDrawn, SystemClock.uptimeMillis());
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700431 }
432 reportedDrawn = nowDrawn;
433 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800434 if (nowVisible != reportedVisible) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700435 if (DEBUG_VISIBILITY) Slog.v(TAG,
436 "Visibility changed in " + this + ": vis=" + nowVisible);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800437 reportedVisible = nowVisible;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800438 if (mActivityRecord != null) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800439 if (nowVisible) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800440 onWindowsVisible();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800441 } else {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800442 onWindowsGone();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800443 }
444 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800445 }
446 }
447
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800448 private void onWindowsGone() {
449 if (mActivityRecord == null) {
450 return;
451 }
452 if (DEBUG_VISIBILITY) {
453 Slog.v(TAG_WM, "Reporting gone in " + mActivityRecord.appToken);
454 }
455 mActivityRecord.onWindowsGone();
456 }
457
458 private void onWindowsVisible() {
459 if (mActivityRecord == null) {
460 return;
461 }
462 if (DEBUG_VISIBILITY) {
463 Slog.v(TAG_WM, "Reporting visible in " + mActivityRecord.appToken);
464 }
465 mActivityRecord.onWindowsVisible();
466 }
467
Wale Ogunwale89973222017-04-23 18:39:45 -0700468 boolean isClientHidden() {
469 return mClientHidden;
470 }
471
472 void setClientHidden(boolean hideClient) {
473 if (mClientHidden == hideClient || (hideClient && mDeferHidingClient)) {
474 return;
475 }
Jorim Jaggi067b5bf2018-02-23 17:42:39 +0100476 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "setClientHidden: " + this
477 + " clientHidden=" + hideClient + " Callers=" + Debug.getCallers(5));
Wale Ogunwale89973222017-04-23 18:39:45 -0700478 mClientHidden = hideClient;
479 sendAppVisibilityToClients();
480 }
481
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800482 void setVisibility(boolean visible, boolean deferHidingClient) {
483 final AppTransition appTransition = getDisplayContent().mAppTransition;
484
485 // Don't set visibility to false if we were already not visible. This prevents WM from
486 // adding the app to the closing app list which doesn't make sense for something that is
487 // already not visible. However, set visibility to true even if we are already visible.
488 // This makes sure the app is added to the opening apps list so that the right
489 // transition can be selected.
490 // TODO: Probably a good idea to separate the concept of opening/closing apps from the
491 // concept of setting visibility...
492 if (!visible && hiddenRequested) {
493
494 if (!deferHidingClient && mDeferHidingClient) {
495 // We previously deferred telling the client to hide itself when visibility was
496 // initially set to false. Now we would like it to hide, so go ahead and set it.
497 mDeferHidingClient = deferHidingClient;
498 setClientHidden(true);
499 }
500 return;
501 }
502
503 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) {
504 Slog.v(TAG_WM, "setAppVisibility("
505 + appToken + ", visible=" + visible + "): " + appTransition
506 + " hidden=" + isHidden() + " hiddenRequested="
507 + hiddenRequested + " Callers=" + Debug.getCallers(6));
508 }
509
510 final DisplayContent displayContent = getDisplayContent();
511 displayContent.mOpeningApps.remove(this);
512 displayContent.mClosingApps.remove(this);
Evan Roskyb1e75f72019-04-26 20:23:26 -0700513 if (isInChangeTransition()) {
514 clearChangeLeash(getPendingTransaction(), true /* cancel */);
515 }
516 displayContent.mChangingApps.remove(this);
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800517 waitingToShow = false;
518 hiddenRequested = !visible;
519 mDeferHidingClient = deferHidingClient;
520
521 if (!visible) {
522 // If the app is dead while it was visible, we kept its dead window on screen.
523 // Now that the app is going invisible, we can remove it. It will be restarted
524 // if made visible again.
525 removeDeadWindows();
526 } else {
527 if (!appTransition.isTransitionSet()
528 && appTransition.isReady()) {
529 // Add the app mOpeningApps if transition is unset but ready. This means
530 // we're doing a screen freeze, and the unfreeze will wait for all opening
531 // apps to be ready.
532 displayContent.mOpeningApps.add(this);
533 }
534 startingMoved = false;
535 // If the token is currently hidden (should be the common case), or has been
536 // stopped, then we need to set up to wait for its windows to be ready.
537 if (isHidden() || mAppStopped) {
538 clearAllDrawn();
539
540 // If the app was already visible, don't reset the waitingToShow state.
541 if (isHidden()) {
542 waitingToShow = true;
543 }
544 }
545
546 // In the case where we are making an app visible but holding off for a transition,
547 // we still need to tell the client to make its windows visible so they get drawn.
548 // Otherwise, we will wait on performing the transition until all windows have been
549 // drawn, they never will be, and we are sad.
550 setClientHidden(false);
551
552 requestUpdateWallpaperIfNeeded();
553
554 if (DEBUG_ADD_REMOVE) Slog.v(TAG_WM, "No longer Stopped: " + this);
555 mAppStopped = false;
556
557 transferStartingWindowFromHiddenAboveTokenIfNeeded();
558 }
559
560 // If we are preparing an app transition, then delay changing
561 // the visibility of this token until we execute that transition.
562 if (okToAnimate() && appTransition.isTransitionSet()) {
563 inPendingTransaction = true;
564 if (visible) {
565 displayContent.mOpeningApps.add(this);
566 mEnteringAnimation = true;
567 } else {
568 displayContent.mClosingApps.add(this);
569 mEnteringAnimation = false;
570 }
571 if (appTransition.getAppTransition()
572 == WindowManager.TRANSIT_TASK_OPEN_BEHIND) {
573 // We're launchingBehind, add the launching activity to mOpeningApps.
574 final WindowState win = getDisplayContent().findFocusedWindow();
575 if (win != null) {
576 final AppWindowToken focusedToken = win.mAppToken;
577 if (focusedToken != null) {
578 if (DEBUG_APP_TRANSITIONS) {
579 Slog.d(TAG_WM, "TRANSIT_TASK_OPEN_BEHIND, "
580 + " adding " + focusedToken + " to mOpeningApps");
581 }
582 // Force animation to be loaded.
583 focusedToken.setHidden(true);
584 displayContent.mOpeningApps.add(focusedToken);
585 }
586 }
587 }
Garfield Tanb6776602019-02-20 14:44:26 -0800588 // Changes in opening apps and closing apps may cause orientation change.
589 reportDescendantOrientationChangeIfNeeded();
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800590 return;
591 }
592
593 commitVisibility(null, visible, TRANSIT_UNSET, true, mVoiceInteraction);
594 updateReportedVisibilityLocked();
595 }
596
597 boolean commitVisibility(WindowManager.LayoutParams lp,
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700598 boolean visible, int transit, boolean performLayout, boolean isVoiceInteraction) {
599
600 boolean delayed = false;
601 inPendingTransaction = false;
Wale Ogunwale9e4721f2017-05-23 19:37:30 -0700602 // Reset the state of mHiddenSetFromTransferredStartingWindow since visibility is actually
603 // been set by the app now.
604 mHiddenSetFromTransferredStartingWindow = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700605
606 // Allow for state changes and animation to be applied if:
607 // * token is transitioning visibility state
608 // * or the token was marked as hidden and is exiting before we had a chance to play the
609 // transition animation
610 // * or this is an opening app and windows are being replaced.
611 boolean visibilityChanged = false;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200612 if (isHidden() == visible || (isHidden() && mIsExiting) || (visible && waitingForReplacement())) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800613 final AccessibilityController accessibilityController =
614 mWmService.mAccessibilityController;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700615 boolean changed = false;
616 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200617 "Changing app " + this + " hidden=" + isHidden() + " performLayout=" + performLayout);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700618
619 boolean runningAppAnimation = false;
620
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100621 if (transit != WindowManager.TRANSIT_UNSET) {
Tiger Huangc9d0d002019-05-29 22:55:29 +0800622 if (mUseTransferredAnimation) {
623 runningAppAnimation = isReallyAnimating();
624 } else if (applyAnimationLocked(lp, transit, visible, isVoiceInteraction)) {
625 runningAppAnimation = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700626 }
Tiger Huangc9d0d002019-05-29 22:55:29 +0800627 delayed = runningAppAnimation;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700628 final WindowState window = findMainWindow();
Rhed Jao02655dc2018-10-30 20:44:52 +0800629 if (window != null && accessibilityController != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700630 accessibilityController.onAppWindowTransitionLocked(window, transit);
631 }
632 changed = true;
633 }
634
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700635 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700636 for (int i = 0; i < windowsCount; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700637 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700638 changed |= win.onAppVisibilityChanged(visible, runningAppAnimation);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700639 }
640
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200641 setHidden(!visible);
642 hiddenRequested = !visible;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700643 visibilityChanged = true;
644 if (!visible) {
645 stopFreezingScreen(true, true);
646 } else {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700647 // If we are being set visible, and the starting window is not yet displayed,
648 // then make sure it doesn't get displayed.
649 if (startingWindow != null && !startingWindow.isDrawnLw()) {
chaviw15ad49f2019-04-24 15:05:39 -0700650 startingWindow.clearPolicyVisibilityFlag(LEGACY_POLICY_VISIBILITY);
651 startingWindow.mLegacyPolicyVisibilityAfterAnim = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700652 }
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700653
654 // We are becoming visible, so better freeze the screen with the windows that are
655 // getting visible so we also wait for them.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800656 forAllWindows(mWmService::makeWindowFreezingScreenIfNeededLocked, true);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700657 }
658
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800659 if (DEBUG_APP_TRANSITIONS) {
660 Slog.v(TAG_WM, "commitVisibility: " + this
661 + ": hidden=" + isHidden() + " hiddenRequested=" + hiddenRequested);
662 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700663
664 if (changed) {
Arthur Hung95b38a92018-07-20 18:56:12 +0800665 getDisplayContent().getInputMonitor().setUpdateInputWindowsNeededLw();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700666 if (performLayout) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800667 mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700668 false /*updateInputWindows*/);
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800669 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700670 }
Arthur Hung95b38a92018-07-20 18:56:12 +0800671 getDisplayContent().getInputMonitor().updateInputWindowsLw(false /*force*/);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700672 }
673 }
Tiger Huangc9d0d002019-05-29 22:55:29 +0800674 mUseTransferredAnimation = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700675
lumarkd14173e2019-03-27 19:14:33 +0800676 if (isReallyAnimating()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700677 delayed = true;
Jorim Jaggiab9fcb22018-03-15 23:46:12 +0100678 } else {
679
680 // We aren't animating anything, but exiting windows rely on the animation finished
681 // callback being called in case the AppWindowToken was pretending to be animating,
682 // which we might have done because we were in closing/opening apps list.
683 onAnimationFinished();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700684 }
685
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700686 for (int i = mChildren.size() - 1; i >= 0 && !delayed; i--) {
Jorim Jaggia5e10572017-11-15 14:36:26 +0100687 if ((mChildren.get(i)).isSelfOrChildAnimating()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700688 delayed = true;
689 }
690 }
691
692 if (visibilityChanged) {
693 if (visible && !delayed) {
694 // The token was made immediately visible, there will be no entrance animation.
695 // We need to inform the client the enter animation was finished.
696 mEnteringAnimation = true;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800697 mWmService.mActivityManagerAppTransitionNotifier.onAppTransitionFinishedLocked(
698 token);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700699 }
Robert Carr61b81112017-07-17 18:08:15 -0700700
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800701 // If we're becoming visible, immediately change client visibility as well. there seem
702 // to be some edge cases where we change our visibility but client visibility never gets
703 // updated.
Jorim Jaggi110839b2018-01-22 12:49:04 +0100704 // If we're becoming invisible, update the client visibility if we are not running an
705 // animation. Otherwise, we'll update client visibility in onAnimationFinished.
Jorim Jaggi3cad57f2018-02-20 18:32:01 +0100706 if (visible || !isReallyAnimating()) {
Jorim Jaggi110839b2018-01-22 12:49:04 +0100707 setClientHidden(!visible);
Jorim Jaggi4876b4a2018-01-11 15:43:49 +0100708 }
709
lumark588a3e82018-07-20 18:53:54 +0800710 if (!getDisplayContent().mClosingApps.contains(this)
711 && !getDisplayContent().mOpeningApps.contains(this)) {
chaviwa953fcf2018-03-01 12:00:39 -0800712 // The token is not closing nor opening, so even if there is an animation set, that
713 // doesn't mean that it goes through the normal app transition cycle so we have
714 // to inform the docked controller about visibility change.
715 // TODO(multi-display): notify docked divider on all displays where visibility was
716 // affected.
lumark588a3e82018-07-20 18:53:54 +0800717 getDisplayContent().getDockedDividerController().notifyAppVisibilityChanged();
chaviwa953fcf2018-03-01 12:00:39 -0800718
719 // Take the screenshot before possibly hiding the WSA, otherwise the screenshot
720 // will not be taken.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800721 mWmService.mTaskSnapshotController.notifyAppVisibilityChanged(this, visible);
chaviwa953fcf2018-03-01 12:00:39 -0800722 }
723
Robert Carre7cc44d2017-03-20 19:04:30 -0700724 // If we are hidden but there is no delay needed we immediately
725 // apply the Surface transaction so that the ActivityManager
Robert Carr61b81112017-07-17 18:08:15 -0700726 // can have some guarantee on the Surface state following
727 // setting the visibility. This captures cases like dismissing
728 // the docked or pinned stack where there is no app transition.
729 //
730 // In the case of a "Null" animation, there will be
731 // no animation but there will still be a transition set.
732 // We still need to delay hiding the surface such that it
733 // can be synchronized with showing the next surface in the transition.
lumark588a3e82018-07-20 18:53:54 +0800734 if (isHidden() && !delayed && !getDisplayContent().mAppTransition.isTransitionSet()) {
Robert Carr6914f082017-03-20 19:04:30 -0700735 SurfaceControl.openTransaction();
736 for (int i = mChildren.size() - 1; i >= 0; i--) {
737 mChildren.get(i).mWinAnimator.hide("immediately hidden");
738 }
739 SurfaceControl.closeTransaction();
740 }
Garfield Tanb6776602019-02-20 14:44:26 -0800741
742 // Visibility changes may cause orientation request change.
743 reportDescendantOrientationChangeIfNeeded();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700744 }
745
746 return delayed;
747 }
748
Garfield Tanb6776602019-02-20 14:44:26 -0800749 private void reportDescendantOrientationChangeIfNeeded() {
750 // Orientation request is exposed only when we're visible. Therefore visibility change
751 // will change requested orientation. Notify upward the hierarchy ladder to adjust
752 // configuration. This is important to cases where activities with incompatible
753 // orientations launch, or user goes back from an activity of bi-orientation to an
754 // activity with specified orientation.
755 if (mActivityRecord.getRequestedConfigurationOrientation() == getConfiguration().orientation
756 || getOrientationIgnoreVisibility() == SCREEN_ORIENTATION_UNSET) {
757 return;
758 }
759
760 final IBinder freezeToken =
761 mActivityRecord.mayFreezeScreenLocked(mActivityRecord.app)
762 ? mActivityRecord.appToken : null;
763 onDescendantOrientationChanged(freezeToken, mActivityRecord);
764 }
765
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +0200766 /**
767 * @return The to top most child window for which {@link LayoutParams#isFullscreen()} returns
768 * true.
769 */
770 WindowState getTopFullscreenWindow() {
771 for (int i = mChildren.size() - 1; i >= 0; i--) {
772 final WindowState win = mChildren.get(i);
773 if (win != null && win.mAttrs.isFullscreen()) {
774 return win;
775 }
776 }
777 return null;
778 }
779
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800780 WindowState findMainWindow() {
Matthew Ng53e66b22018-01-12 17:13:13 -0800781 return findMainWindow(true);
782 }
783
784 /**
785 * Finds the main window that either has type base application or application starting if
786 * requested.
787 *
788 * @param includeStartingApp Allow to search application-starting windows to also be returned.
789 * @return The main window of type base application or application starting if requested.
790 */
791 WindowState findMainWindow(boolean includeStartingApp) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700792 WindowState candidate = null;
Matthew Ng53e66b22018-01-12 17:13:13 -0800793 for (int j = mChildren.size() - 1; j >= 0; --j) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700794 final WindowState win = mChildren.get(j);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700795 final int type = win.mAttrs.type;
796 // No need to loop through child window as base application and starting types can't be
797 // child windows.
Matthew Ng53e66b22018-01-12 17:13:13 -0800798 if (type == TYPE_BASE_APPLICATION
799 || (includeStartingApp && type == TYPE_APPLICATION_STARTING)) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700800 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900801 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700802 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800803 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700804 candidate = win;
805 } else {
806 return win;
807 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800808 }
809 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700810 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800811 }
812
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800813 boolean windowsAreFocusable() {
Tiger Huang51c5a1d2018-12-11 20:24:51 +0800814 if (mTargetSdk < Build.VERSION_CODES.Q) {
815 final int pid = mActivityRecord != null
816 ? (mActivityRecord.app != null ? mActivityRecord.app.getPid() : 0) : 0;
817 final AppWindowToken topFocusedAppOfMyProcess =
818 mWmService.mRoot.mTopFocusedAppByProcess.get(pid);
819 if (topFocusedAppOfMyProcess != null && topFocusedAppOfMyProcess != this) {
820 // For the apps below Q, there can be only one app which has the focused window per
821 // process, because legacy apps may not be ready for a multi-focus system.
822 return false;
823 }
824 }
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700825 return getWindowConfiguration().canReceiveKeys() || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800826 }
827
Wale Ogunwale571771c2016-08-26 13:18:50 -0700828 @Override
Wale Ogunwale44f21802016-09-02 12:49:48 -0700829 boolean isVisible() {
Wale Ogunwalee471be62016-10-03 07:53:55 -0700830 // If the app token isn't hidden then it is considered visible and there is no need to check
831 // its children windows to see if they are visible.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200832 return !isHidden();
Wale Ogunwale44f21802016-09-02 12:49:48 -0700833 }
834
835 @Override
Wale Ogunwalee287e192017-04-21 09:30:12 -0700836 void removeImmediately() {
837 onRemovedFromDisplay();
Yunfan Chen3a77f282018-12-12 15:47:52 -0800838 if (mActivityRecord != null) {
839 mActivityRecord.unregisterConfigurationChangeListener(this);
840 }
Wale Ogunwalee287e192017-04-21 09:30:12 -0700841 super.removeImmediately();
842 }
843
844 @Override
Wale Ogunwale571771c2016-08-26 13:18:50 -0700845 void removeIfPossible() {
Craig Mautnere3119b72015-01-20 15:02:36 -0800846 mIsExiting = false;
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800847 removeAllWindowsIfPossible();
Bryce Lee6d410262017-02-28 15:30:17 -0800848 removeImmediately();
Craig Mautnere3119b72015-01-20 15:02:36 -0800849 }
850
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700851 @Override
852 boolean checkCompleteDeferredRemoval() {
853 if (mIsExiting) {
854 removeIfPossible();
855 }
856 return super.checkCompleteDeferredRemoval();
857 }
858
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700859 void onRemovedFromDisplay() {
Wale Ogunwalee287e192017-04-21 09:30:12 -0700860 if (mRemovingFromDisplay) {
861 return;
862 }
863 mRemovingFromDisplay = true;
864
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700865 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app token: " + this);
866
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800867 boolean delayed = commitVisibility(null, false, TRANSIT_UNSET, true, mVoiceInteraction);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700868
lumark588a3e82018-07-20 18:53:54 +0800869 getDisplayContent().mOpeningApps.remove(this);
Evan Rosky2289ba12018-11-19 18:28:18 -0800870 getDisplayContent().mChangingApps.remove(this);
lumark588a3e82018-07-20 18:53:54 +0800871 getDisplayContent().mUnknownAppVisibilityController.appRemovedOrHidden(this);
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800872 mWmService.mTaskSnapshotController.onAppRemoved(this);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700873 waitingToShow = false;
lumark588a3e82018-07-20 18:53:54 +0800874 if (getDisplayContent().mClosingApps.contains(this)) {
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700875 delayed = true;
lumark588a3e82018-07-20 18:53:54 +0800876 } else if (getDisplayContent().mAppTransition.isTransitionSet()) {
877 getDisplayContent().mClosingApps.add(this);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700878 delayed = true;
879 }
880
881 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app " + this + " delayed=" + delayed
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200882 + " animation=" + getAnimation() + " animating=" + isSelfAnimating());
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700883
884 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM, "removeAppToken: "
885 + this + " delayed=" + delayed + " Callers=" + Debug.getCallers(4));
886
chaviwa8f07a72019-05-01 16:25:39 -0700887 if (mStartingData != null) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -0800888 removeStartingWindow();
Jorim Jaggi19be6052017-08-03 18:33:43 +0200889 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800890
Winson Chung87e5d552017-04-05 11:49:38 -0700891 // If this window was animating, then we need to ensure that the app transition notifies
lumark588a3e82018-07-20 18:53:54 +0800892 // that animations have completed in DisplayContent.handleAnimatingStoppedAndTransition(),
893 // so add to that list now
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200894 if (isSelfAnimating()) {
lumark588a3e82018-07-20 18:53:54 +0800895 getDisplayContent().mNoAnimationNotifyOnTransitionFinished.add(token);
Winson Chung87e5d552017-04-05 11:49:38 -0700896 }
897
Wale Ogunwalee287e192017-04-21 09:30:12 -0700898 final TaskStack stack = getStack();
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700899 if (delayed && !isEmpty()) {
900 // set the token aside because it has an active animation to be finished
901 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM,
902 "removeAppToken make exiting: " + this);
Wale Ogunwalee287e192017-04-21 09:30:12 -0700903 if (stack != null) {
904 stack.mExitingAppTokens.add(this);
905 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700906 mIsExiting = true;
907 } else {
908 // Make sure there is no animation running on this token, so any windows associated
909 // with it will be removed as soon as their animations are complete
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200910 cancelAnimation();
Wale Ogunwale3106ae012017-05-16 08:56:37 -0700911 if (stack != null) {
912 stack.mExitingAppTokens.remove(this);
913 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700914 removeIfPossible();
915 }
916
917 removed = true;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700918 stopFreezingScreen(true, true);
Bryce Lee6d410262017-02-28 15:30:17 -0800919
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800920 final DisplayContent dc = getDisplayContent();
921 if (dc.mFocusedApp == this) {
922 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "Removing focused app token:" + this
923 + " displayId=" + dc.getDisplayId());
924 dc.setFocusedApp(null);
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800925 mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL, true /*updateInputWindows*/);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700926 }
Riddle Hsu192fe762019-01-15 23:41:57 +0800927 if (mLetterbox != null) {
928 mLetterbox.destroy();
929 mLetterbox = null;
930 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700931
932 if (!delayed) {
933 updateReportedVisibilityLocked();
934 }
Wale Ogunwalee287e192017-04-21 09:30:12 -0700935
936 mRemovingFromDisplay = false;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700937 }
938
Chong Zhange05bcb12016-07-26 17:47:29 -0700939 void clearAnimatingFlags() {
Chong Zhangb0d26702016-08-12 16:03:29 -0700940 boolean wallpaperMightChange = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700941 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700942 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700943 wallpaperMightChange |= win.clearAnimatingFlags();
Chong Zhange05bcb12016-07-26 17:47:29 -0700944 }
Chong Zhangb0d26702016-08-12 16:03:29 -0700945 if (wallpaperMightChange) {
946 requestUpdateWallpaperIfNeeded();
947 }
Chong Zhange05bcb12016-07-26 17:47:29 -0700948 }
949
Robert Carre12aece2016-02-02 22:43:27 -0800950 void destroySurfaces() {
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700951 destroySurfaces(false /*cleanupOnResume*/);
952 }
953
954 /**
955 * Destroy surfaces which have been marked as eligible by the animator, taking care to ensure
956 * the client has finished with them.
957 *
958 * @param cleanupOnResume whether this is done when app is resumed without fully stopped. If
959 * set to true, destroy only surfaces of removed windows, and clear relevant flags of the
960 * others so that they are ready to be reused. If set to false (common case), destroy all
961 * surfaces that's eligible, if the app is already stopped.
962 */
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700963 private void destroySurfaces(boolean cleanupOnResume) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700964 boolean destroyedSomething = false;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100965
966 // Copying to a different list as multiple children can be removed.
Jorim Jaggi59f3e922018-01-05 15:40:32 +0100967 final ArrayList<WindowState> children = new ArrayList<>(mChildren);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100968 for (int i = children.size() - 1; i >= 0; i--) {
969 final WindowState win = children.get(i);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700970 destroyedSomething |= win.destroySurface(cleanupOnResume, mAppStopped);
Robert Carre12aece2016-02-02 22:43:27 -0800971 }
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700972 if (destroyedSomething) {
973 final DisplayContent dc = getDisplayContent();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700974 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Adrian Roos23df3a32018-03-15 15:41:13 +0100975 updateLetterboxSurface(null);
Robert Carre12aece2016-02-02 22:43:27 -0800976 }
977 }
978
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800979 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700980 * Notify that the app is now resumed, and it was not stopped before, perform a clean
981 * up of the surfaces
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800982 */
Jorim Jaggibae01b12017-04-11 16:29:10 -0700983 void notifyAppResumed(boolean wasStopped) {
Chong Zhangad24f962016-08-25 12:12:33 -0700984 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppResumed: wasStopped=" + wasStopped
Jorim Jaggibae01b12017-04-11 16:29:10 -0700985 + " " + this);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700986 mAppStopped = false;
chaviwd3bf08d2017-08-01 17:24:59 -0700987 // Allow the window to turn the screen on once the app is resumed again.
988 setCanTurnScreenOn(true);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700989 if (!wasStopped) {
990 destroySurfaces(true /*cleanupOnResume*/);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800991 }
Robert Carre12aece2016-02-02 22:43:27 -0800992 }
993
Chong Zhangbef461f2015-10-27 11:38:24 -0700994 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700995 * Notify that the app has stopped, and it is okay to destroy any surfaces which were
996 * keeping alive in case they were still being used.
997 */
998 void notifyAppStopped() {
999 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: " + this);
1000 mAppStopped = true;
1001 destroySurfaces();
1002 // Remove any starting window that was added for this app if they are still around.
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001003 removeStartingWindow();
Chong Zhang45e6d2d2016-07-20 18:33:56 -07001004 }
1005
Chong Zhang92147042016-05-09 12:47:11 -07001006 void clearAllDrawn() {
1007 allDrawn = false;
1008 deferClearAllDrawn = false;
Chong Zhangbef461f2015-10-27 11:38:24 -07001009 }
1010
Bryce Lee6d410262017-02-28 15:30:17 -08001011 Task getTask() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001012 return (Task) getParent();
Bryce Lee6d410262017-02-28 15:30:17 -08001013 }
1014
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001015 TaskStack getStack() {
1016 final Task task = getTask();
1017 if (task != null) {
1018 return task.mStack;
1019 } else {
1020 return null;
1021 }
1022 }
1023
Bryce Lee6d410262017-02-28 15:30:17 -08001024 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08001025 void onParentChanged() {
1026 super.onParentChanged();
Bryce Lee6d410262017-02-28 15:30:17 -08001027
Robert Carred3e83b2017-04-21 13:26:55 -07001028 final Task task = getTask();
1029
Bryce Lee6d410262017-02-28 15:30:17 -08001030 // When the associated task is {@code null}, the {@link AppWindowToken} can no longer
1031 // access visual elements like the {@link DisplayContent}. We must remove any associations
1032 // such as animations.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001033 if (!mReparenting) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001034 if (task == null) {
1035 // It is possible we have been marked as a closing app earlier. We must remove ourselves
1036 // from this list so we do not participate in any future animations.
lumark588a3e82018-07-20 18:53:54 +08001037 getDisplayContent().mClosingApps.remove(this);
Robert Carred3e83b2017-04-21 13:26:55 -07001038 } else if (mLastParent != null && mLastParent.mStack != null) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001039 task.mStack.mExitingAppTokens.remove(this);
1040 }
Bryce Lee6d410262017-02-28 15:30:17 -08001041 }
Jorim Jaggi6de61012018-03-19 14:53:23 +01001042 final TaskStack stack = getStack();
1043
1044 // If we reparent, make sure to remove ourselves from the old animation registry.
1045 if (mAnimatingAppWindowTokenRegistry != null) {
1046 mAnimatingAppWindowTokenRegistry.notifyFinished(this);
1047 }
1048 mAnimatingAppWindowTokenRegistry = stack != null
1049 ? stack.getAnimatingAppWindowTokenRegistry()
1050 : null;
1051
Robert Carred3e83b2017-04-21 13:26:55 -07001052 mLastParent = task;
chaviwdcf76ec2019-01-11 16:48:46 -08001053
1054 updateColorTransform();
Bryce Lee6d410262017-02-28 15:30:17 -08001055 }
1056
Wale Ogunwalefa854eb2016-09-20 13:43:52 -07001057 void postWindowRemoveStartingWindowCleanup(WindowState win) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001058 // TODO: Something smells about the code below...Is there a better way?
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001059 if (startingWindow == win) {
1060 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Notify removed startingWindow " + win);
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001061 removeStartingWindow();
Wale Ogunwale6c459212017-05-17 08:56:03 -07001062 } else if (mChildren.size() == 0) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001063 // If this is the last window and we had requested a starting transition window,
1064 // well there is no point now.
Jorim Jaggi02886a82016-12-06 09:10:06 -08001065 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Nulling last startingData");
chaviwa8f07a72019-05-01 16:25:39 -07001066 mStartingData = null;
Wale Ogunwale6c459212017-05-17 08:56:03 -07001067 if (mHiddenSetFromTransferredStartingWindow) {
1068 // We set the hidden state to false for the token from a transferred starting window.
1069 // We now reset it back to true since the starting window was the last window in the
1070 // token.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001071 setHidden(true);
Wale Ogunwale6c459212017-05-17 08:56:03 -07001072 }
Jorim Jaggi829b9cd2017-01-23 16:20:53 +01001073 } else if (mChildren.size() == 1 && startingSurface != null && !isRelaunching()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001074 // If this is the last window except for a starting transition window,
1075 // we need to get rid of the starting transition.
Jorim Jaggie4b0f282017-05-17 15:10:29 +02001076 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Last window, removing starting window "
1077 + win);
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001078 removeStartingWindow();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001079 }
1080 }
1081
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001082 void removeDeadWindows() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001083 for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001084 WindowState win = mChildren.get(winNdx);
Chong Zhang112eb8c2015-11-02 11:17:00 -08001085 if (win.mAppDied) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001086 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.w(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001087 "removeDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -08001088 // Set mDestroying, we don't want any animation or delayed removal here.
1089 win.mDestroying = true;
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001090 // Also removes child windows.
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001091 win.removeIfPossible();
Chong Zhang112eb8c2015-11-02 11:17:00 -08001092 }
1093 }
1094 }
1095
Wale Ogunwalee42d0e12016-05-02 16:40:59 -07001096 boolean hasWindowsAlive() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001097 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001098 // No need to loop through child windows as the answer should be the same as that of the
1099 // parent window.
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001100 if (!(mChildren.get(i)).mAppDied) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -07001101 return true;
1102 }
1103 }
1104 return false;
1105 }
1106
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001107 void setWillReplaceWindows(boolean animate) {
Wale Ogunwale455fac52016-07-21 07:24:49 -07001108 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
1109 "Marking app token " + this + " with replacing windows.");
Robert Carra1eb4392015-12-10 12:43:51 -08001110
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001111 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001112 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001113 w.setWillReplaceWindow(animate);
Robert Carra1eb4392015-12-10 12:43:51 -08001114 }
Robert Carra1eb4392015-12-10 12:43:51 -08001115 }
1116
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001117 void setWillReplaceChildWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -07001118 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + this
Robert Carr23fa16b2016-01-13 13:19:58 -08001119 + " with replacing child windows.");
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001120 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001121 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001122 w.setWillReplaceChildWindows();
Robert Carr23fa16b2016-01-13 13:19:58 -08001123 }
1124 }
1125
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001126 void clearWillReplaceWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -07001127 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
1128 "Resetting app token " + this + " of replacing window marks.");
Chong Zhangf596cd52016-01-05 13:42:44 -08001129
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001130 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001131 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001132 w.clearWillReplaceWindow();
Chong Zhangf596cd52016-01-05 13:42:44 -08001133 }
1134 }
1135
Chong Zhang4d7369a2016-04-25 16:09:14 -07001136 void requestUpdateWallpaperIfNeeded() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001137 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001138 final WindowState w = mChildren.get(i);
Chong Zhang4d7369a2016-04-25 16:09:14 -07001139 w.requestUpdateWallpaperIfNeeded();
1140 }
1141 }
1142
Chong Zhangd78ddb42016-03-02 17:01:14 -08001143 boolean isRelaunching() {
1144 return mPendingRelaunchCount > 0;
1145 }
1146
Robert Carr68375192017-06-13 12:41:53 -07001147 boolean shouldFreezeBounds() {
1148 final Task task = getTask();
1149
1150 // For freeform windows, we can't freeze the bounds at the moment because this would make
1151 // the resizing unresponsive.
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001152 if (task == null || task.inFreeformWindowingMode()) {
Robert Carr68375192017-06-13 12:41:53 -07001153 return false;
1154 }
1155
1156 // We freeze the bounds while drag resizing to deal with the time between
1157 // the divider/drag handle being released, and the handling it's new
1158 // configuration. If we are relaunched outside of the drag resizing state,
1159 // we need to be careful not to do this.
1160 return getTask().isDragResizing();
1161 }
1162
Chong Zhangd78ddb42016-03-02 17:01:14 -08001163 void startRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -07001164 if (shouldFreezeBounds()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -08001165 freezeBounds();
1166 }
Robert Carrd5c7dd62017-03-08 10:39:30 -08001167
1168 // In the process of tearing down before relaunching, the app will
1169 // try and clean up it's child surfaces. We need to prevent this from
1170 // happening, so we sever the children, transfering their ownership
1171 // from the client it-self to the parent surface (owned by us).
Robert Carr29daa922018-04-27 11:56:48 -07001172 detachChildren();
1173
1174 mPendingRelaunchCount++;
1175 }
1176
1177 void detachChildren() {
Robert Carrfd8e93b2018-05-10 13:40:25 -07001178 SurfaceControl.openTransaction();
Robert Carrd5c7dd62017-03-08 10:39:30 -08001179 for (int i = mChildren.size() - 1; i >= 0; i--) {
1180 final WindowState w = mChildren.get(i);
1181 w.mWinAnimator.detachChildren();
1182 }
Robert Carrfd8e93b2018-05-10 13:40:25 -07001183 SurfaceControl.closeTransaction();
Chong Zhangd78ddb42016-03-02 17:01:14 -08001184 }
1185
1186 void finishRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -07001187 unfreezeBounds();
1188
Chong Zhangd78ddb42016-03-02 17:01:14 -08001189 if (mPendingRelaunchCount > 0) {
1190 mPendingRelaunchCount--;
Bryce Lee081554b2017-05-25 07:52:12 -07001191 } else {
1192 // Update keyguard flags upon finishing relaunch.
1193 checkKeyguardFlagsChanged();
Chong Zhangd78ddb42016-03-02 17:01:14 -08001194 }
1195 }
1196
Wale Ogunwale8fd75422016-06-24 14:20:37 -07001197 void clearRelaunching() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -07001198 if (mPendingRelaunchCount == 0) {
1199 return;
1200 }
Robert Carr68375192017-06-13 12:41:53 -07001201 unfreezeBounds();
Wale Ogunwale8fd75422016-06-24 14:20:37 -07001202 mPendingRelaunchCount = 0;
1203 }
1204
Wale Ogunwale07bcab72016-10-14 15:30:09 -07001205 /**
1206 * Returns true if the new child window we are adding to this token is considered greater than
1207 * the existing child window in this token in terms of z-order.
1208 */
1209 @Override
1210 protected boolean isFirstChildWindowGreaterThanSecond(WindowState newWindow,
1211 WindowState existingWindow) {
1212 final int type1 = newWindow.mAttrs.type;
1213 final int type2 = existingWindow.mAttrs.type;
1214
1215 // Base application windows should be z-ordered BELOW all other windows in the app token.
1216 if (type1 == TYPE_BASE_APPLICATION && type2 != TYPE_BASE_APPLICATION) {
1217 return false;
1218 } else if (type1 != TYPE_BASE_APPLICATION && type2 == TYPE_BASE_APPLICATION) {
1219 return true;
1220 }
1221
1222 // Starting windows should be z-ordered ABOVE all other windows in the app token.
1223 if (type1 == TYPE_APPLICATION_STARTING && type2 != TYPE_APPLICATION_STARTING) {
1224 return true;
1225 } else if (type1 != TYPE_APPLICATION_STARTING && type2 == TYPE_APPLICATION_STARTING) {
1226 return false;
1227 }
1228
1229 // Otherwise the new window is greater than the existing window.
1230 return true;
1231 }
1232
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001233 @Override
Robert Carra1eb4392015-12-10 12:43:51 -08001234 void addWindow(WindowState w) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001235 super.addWindow(w);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001236
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001237 boolean gotReplacementWindow = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001238 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001239 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001240 gotReplacementWindow |= candidate.setReplacementWindowIfNeeded(w);
1241 }
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001242
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001243 // if we got a replacement window, reset the timeout to give drawing more time
1244 if (gotReplacementWindow) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001245 mWmService.scheduleWindowReplacementTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -08001246 }
Jorim Jaggife762342016-10-13 14:33:27 +02001247 checkKeyguardFlagsChanged();
1248 }
1249
1250 @Override
1251 void removeChild(WindowState child) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001252 if (!mChildren.contains(child)) {
1253 // This can be true when testing.
1254 return;
1255 }
Jorim Jaggife762342016-10-13 14:33:27 +02001256 super.removeChild(child);
1257 checkKeyguardFlagsChanged();
Adrian Roos23df3a32018-03-15 15:41:13 +01001258 updateLetterboxSurface(child);
Robert Carra1eb4392015-12-10 12:43:51 -08001259 }
1260
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001261 private boolean waitingForReplacement() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001262 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001263 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001264 if (candidate.waitingForReplacement()) {
Robert Carra1eb4392015-12-10 12:43:51 -08001265 return true;
1266 }
1267 }
1268 return false;
1269 }
1270
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001271 void onWindowReplacementTimeout() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001272 for (int i = mChildren.size() - 1; i >= 0; --i) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001273 (mChildren.get(i)).onWindowReplacementTimeout();
Robert Carra1eb4392015-12-10 12:43:51 -08001274 }
1275 }
1276
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001277 void reparent(Task task, int position) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001278 if (DEBUG_ADD_REMOVE) {
1279 Slog.i(TAG_WM, "reparent: moving app token=" + this
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001280 + " to task=" + task.mTaskId + " at " + position);
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001281 }
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001282 if (task == null) {
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001283 throw new IllegalArgumentException("reparent: could not find task");
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001284 }
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001285 final Task currentTask = getTask();
1286 if (task == currentTask) {
Winson Chung30480042017-01-26 10:55:34 -08001287 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001288 "window token=" + this + " already child of task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -08001289 }
Bryce Lee6d410262017-02-28 15:30:17 -08001290
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001291 if (currentTask.mStack != task.mStack) {
Bryce Lee6d410262017-02-28 15:30:17 -08001292 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001293 "window token=" + this + " current task=" + currentTask
Bryce Lee6d410262017-02-28 15:30:17 -08001294 + " belongs to a different stack than " + task);
1295 }
1296
Winson Chung30480042017-01-26 10:55:34 -08001297 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "reParentWindowToken: removing window token=" + this
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001298 + " from task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -08001299 final DisplayContent prevDisplayContent = getDisplayContent();
1300
Bryce Lee6d410262017-02-28 15:30:17 -08001301 mReparenting = true;
1302
Winson Chung30480042017-01-26 10:55:34 -08001303 getParent().removeChild(this);
1304 task.addChild(this, position);
1305
Bryce Lee6d410262017-02-28 15:30:17 -08001306 mReparenting = false;
1307
Winson Chung30480042017-01-26 10:55:34 -08001308 // Relayout display(s).
1309 final DisplayContent displayContent = task.getDisplayContent();
1310 displayContent.setLayoutNeeded();
1311 if (prevDisplayContent != displayContent) {
1312 onDisplayChanged(displayContent);
1313 prevDisplayContent.setLayoutNeeded();
1314 }
Yunfan Chen0e7aff92018-12-05 16:35:32 -08001315 getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
Winson Chung30480042017-01-26 10:55:34 -08001316 }
1317
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001318 @Override
1319 void onDisplayChanged(DisplayContent dc) {
1320 DisplayContent prevDc = mDisplayContent;
1321 super.onDisplayChanged(dc);
Evan Roskyb1e75f72019-04-26 20:23:26 -07001322 if (prevDc == null || prevDc == mDisplayContent) {
Evan Rosky25b56192019-02-06 16:10:56 -08001323 return;
1324 }
1325 if (prevDc.mChangingApps.contains(this)) {
1326 // This gets called *after* the AppWindowToken has been reparented to the new display.
1327 // That reparenting resulted in this window changing modes (eg. FREEFORM -> FULLSCREEN),
1328 // so this token is now "frozen" while waiting for the animation to start on prevDc
1329 // (which will be cancelled since the window is no-longer a child). However, since this
1330 // is no longer a child of prevDc, this won't be notified of the cancelled animation,
1331 // so we need to cancel the change transition here.
1332 clearChangeLeash(getPendingTransaction(), true /* cancel */);
1333 }
1334 if (prevDc.mFocusedApp == this) {
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001335 prevDc.setFocusedApp(null);
lumarkbf844642018-11-23 17:11:36 +08001336 final TaskStack stack = dc.getTopStack();
1337 if (stack != null) {
1338 final Task task = stack.getTopChild();
1339 if (task != null && task.getTopChild() == this) {
1340 dc.setFocusedApp(this);
1341 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001342 }
1343 }
Riddle Hsu192fe762019-01-15 23:41:57 +08001344
Evan Roskyb1e75f72019-04-26 20:23:26 -07001345 if (mLetterbox != null) {
Riddle Hsu192fe762019-01-15 23:41:57 +08001346 mLetterbox.onMovedToDisplay(mDisplayContent.getDisplayId());
1347 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08001348 }
1349
Jorim Jaggi0429f352015-12-22 16:29:16 +01001350 /**
1351 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
1352 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
1353 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
1354 * with a queue.
1355 */
Chong Zhangd78ddb42016-03-02 17:01:14 -08001356 private void freezeBounds() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001357 final Task task = getTask();
1358 mFrozenBounds.offer(new Rect(task.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001359
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001360 if (task.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001361 // We didn't call prepareFreezingBounds on the task, so use the current value.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001362 mFrozenMergedConfig.offer(new Configuration(task.getConfiguration()));
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001363 } else {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001364 mFrozenMergedConfig.offer(new Configuration(task.mPreparedFrozenMergedConfig));
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001365 }
Andrii Kulianb10330d2016-09-16 13:51:46 -07001366 // Calling unset() to make it equal to Configuration.EMPTY.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001367 task.mPreparedFrozenMergedConfig.unset();
Jorim Jaggi0429f352015-12-22 16:29:16 +01001368 }
1369
1370 /**
1371 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
1372 */
Chong Zhangd78ddb42016-03-02 17:01:14 -08001373 private void unfreezeBounds() {
Robert Carr68375192017-06-13 12:41:53 -07001374 if (mFrozenBounds.isEmpty()) {
1375 return;
Wale Ogunwale37dbafc2016-06-27 10:15:20 -07001376 }
Robert Carr68375192017-06-13 12:41:53 -07001377 mFrozenBounds.remove();
Wale Ogunwale37dbafc2016-06-27 10:15:20 -07001378 if (!mFrozenMergedConfig.isEmpty()) {
1379 mFrozenMergedConfig.remove();
1380 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001381 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001382 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001383 win.onUnfreezeBounds();
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001384 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001385 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +01001386 }
1387
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001388 void setAppLayoutChanges(int changes, String reason) {
1389 if (!mChildren.isEmpty()) {
1390 final DisplayContent dc = getDisplayContent();
1391 dc.pendingLayoutChanges |= changes;
1392 if (DEBUG_LAYOUT_REPEATS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001393 mWmService.mWindowPlacerLocked.debugLayoutRepeats(reason, dc.pendingLayoutChanges);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001394 }
1395 }
1396 }
1397
1398 void removeReplacedWindowIfNeeded(WindowState replacement) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001399 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001400 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001401 if (win.removeReplacedWindowIfNeeded(replacement)) {
1402 return;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001403 }
1404 }
1405 }
1406
1407 void startFreezingScreen() {
1408 if (DEBUG_ORIENTATION) logWithStack(TAG, "Set freezing of " + appToken + ": hidden="
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001409 + isHidden() + " freezing=" + mFreezingScreen + " hiddenRequested="
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001410 + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001411 if (!hiddenRequested) {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001412 if (!mFreezingScreen) {
1413 mFreezingScreen = true;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001414 mWmService.registerAppFreezeListener(this);
1415 mWmService.mAppsFreezingScreen++;
1416 if (mWmService.mAppsFreezingScreen == 1) {
1417 mWmService.startFreezingDisplayLocked(0, 0, getDisplayContent());
1418 mWmService.mH.removeMessages(H.APP_FREEZE_TIMEOUT);
1419 mWmService.mH.sendEmptyMessageDelayed(H.APP_FREEZE_TIMEOUT, 2000);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001420 }
1421 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001422 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001423 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001424 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001425 w.onStartFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001426 }
1427 }
1428 }
1429
1430 void stopFreezingScreen(boolean unfreezeSurfaceNow, boolean force) {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001431 if (!mFreezingScreen) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001432 return;
1433 }
1434 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Clear freezing of " + this + " force=" + force);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001435 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001436 boolean unfrozeWindows = false;
1437 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001438 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001439 unfrozeWindows |= w.onStopFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001440 }
1441 if (force || unfrozeWindows) {
1442 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "No longer freezing: " + this);
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001443 mFreezingScreen = false;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001444 mWmService.unregisterAppFreezeListener(this);
1445 mWmService.mAppsFreezingScreen--;
1446 mWmService.mLastFinishedFreezeSource = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001447 }
1448 if (unfreezeSurfaceNow) {
1449 if (unfrozeWindows) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001450 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001451 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001452 mWmService.stopFreezingDisplayLocked();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001453 }
1454 }
1455
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001456 @Override
1457 public void onAppFreezeTimeout() {
1458 Slog.w(TAG_WM, "Force clearing freeze: " + this);
1459 stopFreezingScreen(true, true);
1460 }
1461
Jorim Jaggi60f9c972018-02-01 19:21:07 +01001462 /**
1463 * Tries to transfer the starting window from a token that's above ourselves in the task but
1464 * not visible anymore. This is a common scenario apps use: Trampoline activity T start main
1465 * activity M in the same task. Now, when reopening the task, T starts on top of M but then
1466 * immediately finishes after, so we have to transfer T to M.
1467 */
1468 void transferStartingWindowFromHiddenAboveTokenIfNeeded() {
1469 final Task task = getTask();
1470 for (int i = task.mChildren.size() - 1; i >= 0; i--) {
1471 final AppWindowToken fromToken = task.mChildren.get(i);
1472 if (fromToken == this) {
1473 return;
1474 }
1475 if (fromToken.hiddenRequested && transferStartingWindow(fromToken.token)) {
1476 return;
1477 }
1478 }
1479 }
1480
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001481 boolean transferStartingWindow(IBinder transferFrom) {
Wale Ogunwale02319a62016-09-26 15:21:22 -07001482 final AppWindowToken fromToken = getDisplayContent().getAppWindowToken(transferFrom);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001483 if (fromToken == null) {
1484 return false;
1485 }
1486
1487 final WindowState tStartingWindow = fromToken.startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001488 if (tStartingWindow != null && fromToken.startingSurface != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001489 // In this case, the starting icon has already been displayed, so start
1490 // letting windows get shown immediately without any more transitions.
lumark588a3e82018-07-20 18:53:54 +08001491 getDisplayContent().mSkipAppTransitionAnimation = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001492
1493 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Moving existing starting " + tStartingWindow
1494 + " from " + fromToken + " to " + this);
1495
1496 final long origId = Binder.clearCallingIdentity();
Peter Visontay3556a3b2017-11-01 17:23:17 +00001497 try {
1498 // Transfer the starting window over to the new token.
chaviwa8f07a72019-05-01 16:25:39 -07001499 mStartingData = fromToken.mStartingData;
Peter Visontay3556a3b2017-11-01 17:23:17 +00001500 startingSurface = fromToken.startingSurface;
1501 startingDisplayed = fromToken.startingDisplayed;
1502 fromToken.startingDisplayed = false;
1503 startingWindow = tStartingWindow;
1504 reportedVisible = fromToken.reportedVisible;
chaviwa8f07a72019-05-01 16:25:39 -07001505 fromToken.mStartingData = null;
Peter Visontay3556a3b2017-11-01 17:23:17 +00001506 fromToken.startingSurface = null;
1507 fromToken.startingWindow = null;
1508 fromToken.startingMoved = true;
1509 tStartingWindow.mToken = this;
1510 tStartingWindow.mAppToken = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001511
Peter Visontay3556a3b2017-11-01 17:23:17 +00001512 if (DEBUG_ADD_REMOVE || DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1513 "Removing starting " + tStartingWindow + " from " + fromToken);
1514 fromToken.removeChild(tStartingWindow);
1515 fromToken.postWindowRemoveStartingWindowCleanup(tStartingWindow);
1516 fromToken.mHiddenSetFromTransferredStartingWindow = false;
1517 addWindow(tStartingWindow);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001518
Peter Visontay3556a3b2017-11-01 17:23:17 +00001519 // Propagate other interesting state between the tokens. If the old token is displayed,
1520 // we should immediately force the new one to be displayed. If it is animating, we need
1521 // to move that animation to the new one.
1522 if (fromToken.allDrawn) {
1523 allDrawn = true;
1524 deferClearAllDrawn = fromToken.deferClearAllDrawn;
1525 }
1526 if (fromToken.firstWindowDrawn) {
1527 firstWindowDrawn = true;
1528 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001529 if (!fromToken.isHidden()) {
1530 setHidden(false);
Peter Visontay3556a3b2017-11-01 17:23:17 +00001531 hiddenRequested = false;
1532 mHiddenSetFromTransferredStartingWindow = true;
1533 }
1534 setClientHidden(fromToken.mClientHidden);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001535
Jorim Jaggi980c9de2017-11-17 01:41:37 +01001536 transferAnimation(fromToken);
1537
1538 // When transferring an animation, we no longer need to apply an animation to the
Tiger Huangc9d0d002019-05-29 22:55:29 +08001539 // the token we transfer the animation over. Thus, set this flag to indicate we've
1540 // transferred the animation.
1541 mUseTransferredAnimation = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001542
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001543 mWmService.updateFocusedWindowLocked(
Peter Visontay3556a3b2017-11-01 17:23:17 +00001544 UPDATE_FOCUS_WILL_PLACE_SURFACES, true /*updateInputWindows*/);
1545 getDisplayContent().setLayoutNeeded();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001546 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Peter Visontay3556a3b2017-11-01 17:23:17 +00001547 } finally {
1548 Binder.restoreCallingIdentity(origId);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001549 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001550 return true;
chaviwa8f07a72019-05-01 16:25:39 -07001551 } else if (fromToken.mStartingData != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001552 // The previous app was getting ready to show a
1553 // starting window, but hasn't yet done so. Steal it!
1554 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1555 "Moving pending starting from " + fromToken + " to " + this);
chaviwa8f07a72019-05-01 16:25:39 -07001556 mStartingData = fromToken.mStartingData;
1557 fromToken.mStartingData = null;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001558 fromToken.startingMoved = true;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001559 scheduleAddStartingWindow();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001560 return true;
1561 }
1562
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001563 // TODO: Transfer thumbnail
1564
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001565 return false;
1566 }
1567
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001568 boolean isLastWindow(WindowState win) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001569 return mChildren.size() == 1 && mChildren.get(0) == win;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001570 }
1571
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001572 @Override
1573 void onAppTransitionDone() {
1574 sendingToBottom = false;
1575 }
1576
Wale Ogunwale51362492016-09-08 17:49:17 -07001577 /**
1578 * We override because this class doesn't want its children affecting its reported orientation
1579 * in anyway.
1580 */
1581 @Override
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001582 int getOrientation(int candidate) {
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001583 if (candidate == SCREEN_ORIENTATION_BEHIND) {
1584 // Allow app to specify orientation regardless of its visibility state if the current
1585 // candidate want us to use orientation behind. I.e. the visible app on-top of this one
1586 // wants us to use the orientation of the app behind it.
1587 return mOrientation;
1588 }
1589
Bryce Lee61fbcbc2017-03-10 14:14:03 -08001590 // The {@link AppWindowToken} should only specify an orientation when it is not closing or
1591 // going to the bottom. Allowing closing {@link AppWindowToken} to participate can lead to
1592 // an Activity in another task being started in the wrong orientation during the transition.
lumark588a3e82018-07-20 18:53:54 +08001593 if (!(sendingToBottom || getDisplayContent().mClosingApps.contains(this))
1594 && (isVisible() || getDisplayContent().mOpeningApps.contains(this))) {
Bryce Leea163b762017-01-24 11:05:01 -08001595 return mOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001596 }
Bryce Leea163b762017-01-24 11:05:01 -08001597
1598 return SCREEN_ORIENTATION_UNSET;
Wale Ogunwale51362492016-09-08 17:49:17 -07001599 }
1600
Wale Ogunwaleb198b742016-12-01 08:44:09 -08001601 /** Returns the app's preferred orientation regardless of its currently visibility state. */
1602 int getOrientationIgnoreVisibility() {
1603 return mOrientation;
1604 }
1605
Riddle Hsub398da32019-01-21 21:48:16 +08001606 /** @return {@code true} if the compatibility bounds is taking effect. */
1607 boolean inSizeCompatMode() {
1608 return mSizeCompatBounds != null;
1609 }
1610
1611 @Override
1612 float getSizeCompatScale() {
1613 return inSizeCompatMode() ? mSizeCompatScale : super.getSizeCompatScale();
1614 }
1615
1616 /**
1617 * @return Non-empty bounds if the activity has override bounds.
1618 * @see ActivityRecord#resolveOverrideConfiguration(Configuration)
1619 */
1620 Rect getResolvedOverrideBounds() {
1621 // Get bounds from resolved override configuration because it is computed with orientation.
1622 return getResolvedOverrideConfiguration().windowConfiguration.getBounds();
1623 }
1624
Craig Mautnerdbb79912012-03-01 18:59:14 -08001625 @Override
Winson Chunge55c0192017-08-24 14:50:48 -07001626 public void onConfigurationChanged(Configuration newParentConfig) {
1627 final int prevWinMode = getWindowingMode();
Evan Rosky2289ba12018-11-19 18:28:18 -08001628 mTmpPrevBounds.set(getBounds());
Winson Chunge55c0192017-08-24 14:50:48 -07001629 super.onConfigurationChanged(newParentConfig);
Riddle Hsub398da32019-01-21 21:48:16 +08001630
1631 final Task task = getTask();
1632 final Rect overrideBounds = getResolvedOverrideBounds();
1633 if (task != null && !overrideBounds.isEmpty()
1634 // If the changes come from change-listener, the incoming parent configuration is
1635 // still the old one. Make sure their orientations are the same to reduce computing
1636 // the compatibility bounds for the intermediate state.
Riddle Hsu04164182019-03-07 18:03:27 +08001637 && (task.mTaskRecord == null || task.mTaskRecord
1638 .getConfiguration().orientation == newParentConfig.orientation)) {
Riddle Hsub398da32019-01-21 21:48:16 +08001639 final Rect taskBounds = task.getBounds();
1640 // Since we only center the activity horizontally, if only the fixed height is smaller
1641 // than its container, the override bounds don't need to take effect.
1642 if ((overrideBounds.width() != taskBounds.width()
1643 || overrideBounds.height() > taskBounds.height())) {
1644 calculateCompatBoundsTransformation(newParentConfig);
1645 updateSurfacePosition();
1646 } else if (mSizeCompatBounds != null) {
1647 mSizeCompatBounds = null;
1648 mSizeCompatScale = 1f;
1649 updateSurfacePosition();
1650 }
1651 }
1652
Winson Chunge55c0192017-08-24 14:50:48 -07001653 final int winMode = getWindowingMode();
1654
1655 if (prevWinMode == winMode) {
1656 return;
1657 }
1658
1659 if (prevWinMode != WINDOWING_MODE_UNDEFINED && winMode == WINDOWING_MODE_PINNED) {
1660 // Entering PiP from fullscreen, reset the snap fraction
1661 mDisplayContent.mPinnedStackControllerLocked.resetReentrySnapFraction(this);
Winson Chung82267ce2018-04-06 16:38:26 -07001662 } else if (prevWinMode == WINDOWING_MODE_PINNED && winMode != WINDOWING_MODE_UNDEFINED
1663 && !isHidden()) {
Winson Chunge55c0192017-08-24 14:50:48 -07001664 // Leaving PiP to fullscreen, save the snap fraction based on the pre-animation bounds
1665 // for the next re-entry into PiP (assuming the activity is not hidden or destroyed)
1666 final TaskStack pinnedStack = mDisplayContent.getPinnedStack();
1667 if (pinnedStack != null) {
Winson Chung8efa1652018-06-06 09:34:23 -07001668 final Rect stackBounds;
1669 if (pinnedStack.lastAnimatingBoundsWasToFullscreen()) {
1670 // We are animating the bounds, use the pre-animation bounds to save the snap
1671 // fraction
1672 stackBounds = pinnedStack.mPreAnimationBounds;
1673 } else {
1674 // We skip the animation if the fullscreen configuration is not compatible, so
1675 // use the current bounds to calculate the saved snap fraction instead
1676 // (see PinnedActivityStack.skipResizeAnimation())
1677 stackBounds = mTmpRect;
1678 pinnedStack.getBounds(stackBounds);
1679 }
Winson Chunge55c0192017-08-24 14:50:48 -07001680 mDisplayContent.mPinnedStackControllerLocked.saveReentrySnapFraction(this,
Winson Chung8efa1652018-06-06 09:34:23 -07001681 stackBounds);
Winson Chunge55c0192017-08-24 14:50:48 -07001682 }
Evan Rosky2289ba12018-11-19 18:28:18 -08001683 } else if (shouldStartChangeTransition(prevWinMode, winMode)) {
1684 initializeChangeTransition(mTmpPrevBounds);
Winson Chunge55c0192017-08-24 14:50:48 -07001685 }
1686 }
1687
Evan Rosky2289ba12018-11-19 18:28:18 -08001688 private boolean shouldStartChangeTransition(int prevWinMode, int newWinMode) {
Evan Rosky25b56192019-02-06 16:10:56 -08001689 if (mWmService.mDisableTransitionAnimation
1690 || !isVisible()
1691 || getDisplayContent().mAppTransition.isTransitionSet()
1692 || getSurfaceControl() == null) {
Evan Rosky2289ba12018-11-19 18:28:18 -08001693 return false;
1694 }
1695 // Only do an animation into and out-of freeform mode for now. Other mode
1696 // transition animations are currently handled by system-ui.
1697 return (prevWinMode == WINDOWING_MODE_FREEFORM) != (newWinMode == WINDOWING_MODE_FREEFORM);
1698 }
1699
1700 /**
1701 * Initializes a change transition. Because the app is visible already, there is a small period
1702 * of time where the user can see the app content/window update before the transition starts.
1703 * To prevent this, we immediately take a snapshot and place the app/snapshot into a leash which
1704 * "freezes" the location/crop until the transition starts.
1705 * <p>
1706 * Here's a walk-through of the process:
1707 * 1. Create a temporary leash ("interim-change-leash") and reparent the app to it.
1708 * 2. Set the temporary leash's position/crop to the current state.
1709 * 3. Create a snapshot and place that at the top of the leash to cover up content changes.
1710 * 4. Once the transition is ready, it will reparent the app to the animation leash.
1711 * 5. Detach the interim-change-leash.
1712 */
1713 private void initializeChangeTransition(Rect startBounds) {
1714 mDisplayContent.prepareAppTransition(TRANSIT_TASK_CHANGE_WINDOWING_MODE,
1715 false /* alwaysKeepCurrent */, 0, false /* forceOverride */);
1716 mDisplayContent.mChangingApps.add(this);
1717 mTransitStartRect.set(startBounds);
1718
1719 final SurfaceControl.Builder builder = makeAnimationLeash()
1720 .setParent(getAnimationLeashParent())
1721 .setName(getSurfaceControl() + " - interim-change-leash");
1722 mTransitChangeLeash = builder.build();
1723 Transaction t = getPendingTransaction();
1724 t.setWindowCrop(mTransitChangeLeash, startBounds.width(), startBounds.height());
1725 t.setPosition(mTransitChangeLeash, startBounds.left, startBounds.top);
1726 t.show(mTransitChangeLeash);
1727 t.reparent(getSurfaceControl(), mTransitChangeLeash);
1728 onAnimationLeashCreated(t, mTransitChangeLeash);
1729
Evan Rosky966759f2019-01-15 10:33:58 -08001730 // Skip creating snapshot if this transition is controlled by a remote animator which
1731 // doesn't need it.
1732 ArraySet<Integer> activityTypes = new ArraySet<>();
1733 activityTypes.add(getActivityType());
1734 RemoteAnimationAdapter adapter =
1735 mDisplayContent.mAppTransitionController.getRemoteAnimationOverride(
1736 this, TRANSIT_TASK_CHANGE_WINDOWING_MODE, activityTypes);
1737 if (adapter != null && !adapter.getChangeNeedsSnapshot()) {
1738 return;
1739 }
1740
Evan Rosky08e20932019-05-14 10:54:07 -07001741 Task task = getTask();
1742 if (mThumbnail == null && task != null && !hasCommittedReparentToAnimationLeash()) {
1743 SurfaceControl.ScreenshotGraphicBuffer snapshot =
1744 mWmService.mTaskSnapshotController.createTaskSnapshot(
1745 task, 1 /* scaleFraction */);
Yunfan Chendcb1fcf2019-02-07 19:08:41 +09001746 if (snapshot != null) {
Evan Rosky08e20932019-05-14 10:54:07 -07001747 mThumbnail = new AppWindowThumbnail(t, this, snapshot.getGraphicBuffer(),
Yunfan Chendcb1fcf2019-02-07 19:08:41 +09001748 true /* relative */);
1749 }
Evan Rosky2289ba12018-11-19 18:28:18 -08001750 }
1751 }
1752
1753 boolean isInChangeTransition() {
Evan Roskyec9488c2019-03-01 19:32:12 -08001754 return mTransitChangeLeash != null || AppTransition.isChangeTransit(mTransit);
Evan Rosky2289ba12018-11-19 18:28:18 -08001755 }
1756
Evan Rosky966759f2019-01-15 10:33:58 -08001757 @VisibleForTesting
1758 AppWindowThumbnail getThumbnail() {
1759 return mThumbnail;
1760 }
1761
Riddle Hsub398da32019-01-21 21:48:16 +08001762 /**
1763 * Calculates the scale and offset to horizontal center the size compatibility bounds into the
1764 * region which is available to application.
1765 */
1766 private void calculateCompatBoundsTransformation(Configuration newParentConfig) {
1767 final Rect parentAppBounds = newParentConfig.windowConfiguration.getAppBounds();
Riddle Hsu74826262019-04-17 14:57:42 +08001768 final Rect parentBounds = newParentConfig.windowConfiguration.getBounds();
1769 final Rect viewportBounds = parentAppBounds != null ? parentAppBounds : parentBounds;
Riddle Hsu04164182019-03-07 18:03:27 +08001770 final Rect appBounds = getWindowConfiguration().getAppBounds();
1771 final Rect contentBounds = appBounds != null ? appBounds : getResolvedOverrideBounds();
Riddle Hsub398da32019-01-21 21:48:16 +08001772 final float contentW = contentBounds.width();
1773 final float contentH = contentBounds.height();
1774 final float viewportW = viewportBounds.width();
1775 final float viewportH = viewportBounds.height();
1776 // Only allow to scale down.
1777 mSizeCompatScale = (contentW <= viewportW && contentH <= viewportH)
1778 ? 1 : Math.min(viewportW / contentW, viewportH / contentH);
1779 final int offsetX = (int) ((viewportW - contentW * mSizeCompatScale + 1) * 0.5f)
1780 + viewportBounds.left;
1781
1782 if (mSizeCompatBounds == null) {
1783 mSizeCompatBounds = new Rect();
1784 }
1785 mSizeCompatBounds.set(contentBounds);
1786 mSizeCompatBounds.offsetTo(0, 0);
1787 mSizeCompatBounds.scale(mSizeCompatScale);
Riddle Hsu74826262019-04-17 14:57:42 +08001788 // Ensure to align the top with the parent.
1789 mSizeCompatBounds.top = parentBounds.top;
Riddle Hsu04164182019-03-07 18:03:27 +08001790 // The decor inset is included in height.
1791 mSizeCompatBounds.bottom += viewportBounds.top;
Riddle Hsub398da32019-01-21 21:48:16 +08001792 mSizeCompatBounds.left += offsetX;
1793 mSizeCompatBounds.right += offsetX;
1794 }
1795
1796 @Override
1797 public Rect getBounds() {
1798 if (mSizeCompatBounds != null) {
1799 return mSizeCompatBounds;
1800 }
1801 return super.getBounds();
1802 }
1803
1804 @Override
1805 public boolean matchParentBounds() {
1806 if (super.matchParentBounds()) {
1807 return true;
1808 }
1809 // An activity in size compatibility mode may have override bounds which equals to its
1810 // parent bounds, so the exact bounds should also be checked.
1811 final WindowContainer parent = getParent();
1812 return parent == null || parent.getBounds().equals(getResolvedOverrideBounds());
1813 }
1814
Winson Chunge55c0192017-08-24 14:50:48 -07001815 @Override
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001816 void checkAppWindowsReadyToShow() {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001817 if (allDrawn == mLastAllDrawn) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001818 return;
1819 }
1820
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001821 mLastAllDrawn = allDrawn;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001822 if (!allDrawn) {
1823 return;
1824 }
1825
1826 // The token has now changed state to having all windows shown... what to do, what to do?
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001827 if (mFreezingScreen) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001828 showAllWindowsLocked();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001829 stopFreezingScreen(false, true);
1830 if (DEBUG_ORIENTATION) Slog.i(TAG,
1831 "Setting mOrientationChangeComplete=true because wtoken " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001832 + " numInteresting=" + mNumInterestingWindows + " numDrawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001833 // This will set mOrientationChangeComplete and cause a pass through layout.
1834 setAppLayoutChanges(FINISH_LAYOUT_REDO_WALLPAPER,
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001835 "checkAppWindowsReadyToShow: freezingScreen");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001836 } else {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001837 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM, "checkAppWindowsReadyToShow");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001838
1839 // We can now show all of the drawn windows!
lumark588a3e82018-07-20 18:53:54 +08001840 if (!getDisplayContent().mOpeningApps.contains(this) && canShowWindows()) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001841 showAllWindowsLocked();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001842 }
1843 }
1844 }
1845
Matthew Ng5d23afa2017-06-21 16:16:24 -07001846 /**
Bryce Leed390deb2017-06-22 13:14:28 -07001847 * Returns whether the drawn window states of this {@link AppWindowToken} has considered every
1848 * child {@link WindowState}. A child is considered if it has been passed into
1849 * {@link #updateDrawnWindowStates(WindowState)} after being added. This is used to determine
1850 * whether states, such as {@code allDrawn}, can be set, which relies on state variables such as
1851 * {@code mNumInterestingWindows}, which depend on all {@link WindowState}s being considered.
1852 *
1853 * @return {@code true} If all children have been considered, {@code false}.
1854 */
1855 private boolean allDrawnStatesConsidered() {
Bryce Lee6311c4b2017-07-06 14:09:29 -07001856 for (int i = mChildren.size() - 1; i >= 0; --i) {
1857 final WindowState child = mChildren.get(i);
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001858 if (child.mightAffectAllDrawn() && !child.getDrawnStateEvaluated()) {
Bryce Leed390deb2017-06-22 13:14:28 -07001859 return false;
1860 }
1861 }
1862 return true;
1863 }
1864
1865 /**
Matthew Ng5d23afa2017-06-21 16:16:24 -07001866 * Determines if the token has finished drawing. This should only be called from
1867 * {@link DisplayContent#applySurfaceChangesTransaction}
1868 */
Matthew Ng498c71d2017-04-18 13:55:45 -07001869 void updateAllDrawn() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001870 if (!allDrawn) {
Matthew Ng498c71d2017-04-18 13:55:45 -07001871 // Number of drawn windows can be less when a window is being relaunched, wait for
Bryce Leed390deb2017-06-22 13:14:28 -07001872 // all windows to be launched and drawn for this token be considered all drawn.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001873 final int numInteresting = mNumInterestingWindows;
Bryce Leed390deb2017-06-22 13:14:28 -07001874
1875 // We must make sure that all present children have been considered (determined by
1876 // {@link #allDrawnStatesConsidered}) before evaluating whether everything has been
1877 // drawn.
1878 if (numInteresting > 0 && allDrawnStatesConsidered()
1879 && mNumDrawnWindows >= numInteresting && !isRelaunching()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001880 if (DEBUG_VISIBILITY) Slog.v(TAG, "allDrawn: " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001881 + " interesting=" + numInteresting + " drawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001882 allDrawn = true;
1883 // Force an additional layout pass where
1884 // WindowStateAnimator#commitFinishDrawingLocked() will call performShowLocked().
Matthew Ng498c71d2017-04-18 13:55:45 -07001885 if (mDisplayContent != null) {
1886 mDisplayContent.setLayoutNeeded();
1887 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001888 mWmService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
Robert Carrecc06b32017-04-18 14:25:10 -07001889
Winson Chunge7ba6862017-05-24 12:13:33 -07001890 // Notify the pinned stack upon all windows drawn. If there was an animation in
1891 // progress then this signal will resume that animation.
Wale Ogunwale61911492017-10-11 08:50:50 -07001892 final TaskStack pinnedStack = mDisplayContent.getPinnedStack();
Winson Chunge7ba6862017-05-24 12:13:33 -07001893 if (pinnedStack != null) {
1894 pinnedStack.onAllWindowsDrawn();
Robert Carrecc06b32017-04-18 14:25:10 -07001895 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001896 }
1897 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001898 }
1899
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001900 boolean keyDispatchingTimedOut(String reason, int windowPid) {
1901 return mActivityRecord != null && mActivityRecord.keyDispatchingTimedOut(reason, windowPid);
1902 }
1903
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001904 /**
1905 * Updated this app token tracking states for interesting and drawn windows based on the window.
1906 *
1907 * @return Returns true if the input window is considered interesting and drawn while all the
1908 * windows in this app token where not considered drawn as of the last pass.
1909 */
1910 boolean updateDrawnWindowStates(WindowState w) {
Bryce Leed390deb2017-06-22 13:14:28 -07001911 w.setDrawnStateEvaluated(true /*evaluated*/);
1912
Jorim Jaggie4b0f282017-05-17 15:10:29 +02001913 if (DEBUG_STARTING_WINDOW_VERBOSE && w == startingWindow) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001914 Slog.d(TAG, "updateWindows: starting " + w + " isOnScreen=" + w.isOnScreen()
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001915 + " allDrawn=" + allDrawn + " freezingScreen=" + mFreezingScreen);
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001916 }
1917
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001918 if (allDrawn && !mFreezingScreen) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001919 return false;
1920 }
1921
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001922 if (mLastTransactionSequence != mWmService.mTransactionSequence) {
1923 mLastTransactionSequence = mWmService.mTransactionSequence;
Matthew Ng53e66b22018-01-12 17:13:13 -08001924 mNumDrawnWindows = 0;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001925 startingDisplayed = false;
Matthew Ng53e66b22018-01-12 17:13:13 -08001926
1927 // There is the main base application window, even if it is exiting, wait for it
1928 mNumInterestingWindows = findMainWindow(false /* includeStartingApp */) != null ? 1 : 0;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001929 }
1930
1931 final WindowStateAnimator winAnimator = w.mWinAnimator;
1932
1933 boolean isInterestingAndDrawn = false;
1934
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001935 if (!allDrawn && w.mightAffectAllDrawn()) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001936 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
1937 Slog.v(TAG, "Eval win " + w + ": isDrawn=" + w.isDrawnLw()
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001938 + ", isAnimationSet=" + isSelfAnimating());
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001939 if (!w.isDrawnLw()) {
1940 Slog.v(TAG, "Not displayed: s=" + winAnimator.mSurfaceController
chaviw15ad49f2019-04-24 15:05:39 -07001941 + " pv=" + w.isVisibleByPolicy()
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001942 + " mDrawState=" + winAnimator.drawStateToString()
1943 + " ph=" + w.isParentWindowHidden() + " th=" + hiddenRequested
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001944 + " a=" + isSelfAnimating());
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001945 }
1946 }
1947
1948 if (w != startingWindow) {
1949 if (w.isInteresting()) {
Matthew Ng53e66b22018-01-12 17:13:13 -08001950 // Add non-main window as interesting since the main app has already been added
1951 if (findMainWindow(false /* includeStartingApp */) != w) {
1952 mNumInterestingWindows++;
1953 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001954 if (w.isDrawnLw()) {
1955 mNumDrawnWindows++;
1956
1957 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG, "tokenMayBeDrawn: "
1958 + this + " w=" + w + " numInteresting=" + mNumInterestingWindows
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001959 + " freezingScreen=" + mFreezingScreen
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001960 + " mAppFreezing=" + w.mAppFreezing);
1961
1962 isInterestingAndDrawn = true;
1963 }
1964 }
1965 } else if (w.isDrawnLw()) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08001966 if (mActivityRecord != null) {
1967 mActivityRecord.onStartingWindowDrawn(SystemClock.uptimeMillis());
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001968 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001969 startingDisplayed = true;
1970 }
1971 }
1972
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001973 return isInterestingAndDrawn;
1974 }
1975
Adrian Roos23df3a32018-03-15 15:41:13 +01001976 void layoutLetterbox(WindowState winHint) {
Adrian Roos4d18a2e2017-12-19 19:08:05 +01001977 final WindowState w = findMainWindow();
Adrian Roosf93b6d22018-03-21 13:48:26 +01001978 if (w == null || winHint != null && w != winHint) {
Adrian Roos4d18a2e2017-12-19 19:08:05 +01001979 return;
1980 }
Jorim Jaggia32da382018-03-28 18:01:22 +02001981 final boolean surfaceReady = w.isDrawnLw() // Regular case
Adrian Roosf93b6d22018-03-21 13:48:26 +01001982 || w.mWinAnimator.mSurfaceDestroyDeferred // The preserved surface is still ready.
1983 || w.isDragResizeChanged(); // Waiting for relayoutWindow to call preserveSurface.
Jorim Jaggi4981f152019-03-26 18:58:45 +01001984 final boolean needsLetterbox = surfaceReady && w.isLetterboxedAppWindow() && fillsParent();
Adrian Roos4d18a2e2017-12-19 19:08:05 +01001985 if (needsLetterbox) {
1986 if (mLetterbox == null) {
1987 mLetterbox = new Letterbox(() -> makeChildSurface(null));
Riddle Hsu192fe762019-01-15 23:41:57 +08001988 mLetterbox.attachInput(w);
Adrian Roos4d18a2e2017-12-19 19:08:05 +01001989 }
Adrian Roos7af9d972018-11-30 15:26:27 +01001990 getPosition(mTmpPoint);
Evan Rosky67c15642019-04-24 13:17:28 -07001991 // Get the bounds of the "space-to-fill". In multi-window mode, the task-level
1992 // represents this. In fullscreen-mode, the stack does (since the orientation letterbox
1993 // is also applied to the task).
1994 Rect spaceToFill = (inMultiWindowMode() || getStack() == null)
1995 ? getTask().getDisplayedBounds() : getStack().getDisplayedBounds();
Evan Rosky4fb1e912019-03-06 13:54:43 -08001996 mLetterbox.layout(spaceToFill, w.getFrameLw(), mTmpPoint);
Adrian Roos4d18a2e2017-12-19 19:08:05 +01001997 } else if (mLetterbox != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +01001998 mLetterbox.hide();
1999 }
2000 }
2001
2002 void updateLetterboxSurface(WindowState winHint) {
2003 final WindowState w = findMainWindow();
2004 if (w != winHint && winHint != null && w != null) {
2005 return;
2006 }
2007 layoutLetterbox(winHint);
2008 if (mLetterbox != null && mLetterbox.needsApplySurfaceChanges()) {
Tiger Huanged6794e2019-05-07 20:07:59 +08002009 mLetterbox.applySurfaceChanges(getPendingTransaction());
Adrian Roos4d18a2e2017-12-19 19:08:05 +01002010 }
2011 }
2012
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002013 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002014 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002015 // For legacy reasons we process the TaskStack.mExitingAppTokens first in DisplayContent
2016 // before the non-exiting app tokens. So, we skip the exiting app tokens here.
2017 // TODO: Investigate if we need to continue to do this or if we can just process them
2018 // in-order.
2019 if (mIsExiting && !waitingForReplacement()) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002020 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002021 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002022 return forAllWindowsUnchecked(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002023 }
2024
lumark588a3e82018-07-20 18:53:54 +08002025 @Override
2026 void forAllAppWindows(Consumer<AppWindowToken> callback) {
2027 callback.accept(this);
2028 }
2029
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08002030 boolean forAllWindowsUnchecked(ToBooleanFunction<WindowState> callback,
2031 boolean traverseTopToBottom) {
2032 return super.forAllWindows(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07002033 }
2034
2035 @Override
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07002036 AppWindowToken asAppWindowToken() {
2037 // I am an app window token!
2038 return this;
2039 }
2040
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002041 boolean addStartingWindow(String pkg, int theme, CompatibilityInfo compatInfo,
2042 CharSequence nonLocalizedLabel, int labelRes, int icon, int logo, int windowFlags,
2043 IBinder transferFrom, boolean newTask, boolean taskSwitch, boolean processRunning,
2044 boolean allowTaskSnapshot, boolean activityCreated, boolean fromRecents) {
2045 // If the display is frozen, we won't do anything until the actual window is
2046 // displayed so there is no reason to put in the starting window.
2047 if (!okToDisplay()) {
2048 return false;
2049 }
2050
chaviwa8f07a72019-05-01 16:25:39 -07002051 if (mStartingData != null) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002052 return false;
2053 }
2054
2055 final WindowState mainWin = findMainWindow();
2056 if (mainWin != null && mainWin.mWinAnimator.getShown()) {
2057 // App already has a visible window...why would you want a starting window?
2058 return false;
2059 }
2060
2061 final ActivityManager.TaskSnapshot snapshot =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002062 mWmService.mTaskSnapshotController.getSnapshot(
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002063 getTask().mTaskId, getTask().mUserId,
2064 false /* restoreFromDisk */, false /* reducedResolution */);
2065 final int type = getStartingWindowType(newTask, taskSwitch, processRunning,
2066 allowTaskSnapshot, activityCreated, fromRecents, snapshot);
2067
2068 if (type == STARTING_WINDOW_TYPE_SNAPSHOT) {
2069 return createSnapshot(snapshot);
2070 }
2071
2072 // If this is a translucent window, then don't show a starting window -- the current
2073 // effect (a full-screen opaque starting window that fades away to the real contents
2074 // when it is ready) does not work for this.
2075 if (DEBUG_STARTING_WINDOW) {
2076 Slog.v(TAG, "Checking theme of starting window: 0x" + Integer.toHexString(theme));
2077 }
2078 if (theme != 0) {
2079 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
2080 com.android.internal.R.styleable.Window,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002081 mWmService.mCurrentUserId);
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002082 if (ent == null) {
2083 // Whoops! App doesn't exist. Um. Okay. We'll just pretend like we didn't
2084 // see that.
2085 return false;
2086 }
2087 final boolean windowIsTranslucent = ent.array.getBoolean(
2088 com.android.internal.R.styleable.Window_windowIsTranslucent, false);
2089 final boolean windowIsFloating = ent.array.getBoolean(
2090 com.android.internal.R.styleable.Window_windowIsFloating, false);
2091 final boolean windowShowWallpaper = ent.array.getBoolean(
2092 com.android.internal.R.styleable.Window_windowShowWallpaper, false);
2093 final boolean windowDisableStarting = ent.array.getBoolean(
2094 com.android.internal.R.styleable.Window_windowDisablePreview, false);
2095 if (DEBUG_STARTING_WINDOW) {
2096 Slog.v(TAG, "Translucent=" + windowIsTranslucent
2097 + " Floating=" + windowIsFloating
2098 + " ShowWallpaper=" + windowShowWallpaper);
2099 }
2100 if (windowIsTranslucent) {
2101 return false;
2102 }
2103 if (windowIsFloating || windowDisableStarting) {
2104 return false;
2105 }
2106 if (windowShowWallpaper) {
2107 if (getDisplayContent().mWallpaperController
2108 .getWallpaperTarget() == null) {
2109 // If this theme is requesting a wallpaper, and the wallpaper
2110 // is not currently visible, then this effectively serves as
2111 // an opaque window and our starting window transition animation
2112 // can still work. We just need to make sure the starting window
2113 // is also showing the wallpaper.
2114 windowFlags |= FLAG_SHOW_WALLPAPER;
2115 } else {
2116 return false;
2117 }
2118 }
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002119 }
Yunfan Chen8a8c0ed2018-12-18 16:46:32 -08002120
2121 if (transferStartingWindow(transferFrom)) {
2122 return true;
2123 }
2124
2125 // There is no existing starting window, and we don't want to create a splash screen, so
2126 // that's it!
2127 if (type != STARTING_WINDOW_TYPE_SPLASH_SCREEN) {
2128 return false;
2129 }
2130
2131 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Creating SplashScreenStartingData");
chaviwa8f07a72019-05-01 16:25:39 -07002132 mStartingData = new SplashScreenStartingData(mWmService, pkg,
Yunfan Chen8a8c0ed2018-12-18 16:46:32 -08002133 theme, compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
2134 getMergedOverrideConfiguration());
2135 scheduleAddStartingWindow();
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002136 return true;
2137 }
2138
2139
2140 private boolean createSnapshot(ActivityManager.TaskSnapshot snapshot) {
2141 if (snapshot == null) {
2142 return false;
2143 }
2144
2145 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Creating SnapshotStartingData");
chaviwa8f07a72019-05-01 16:25:39 -07002146 mStartingData = new SnapshotStartingData(mWmService, snapshot);
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002147 scheduleAddStartingWindow();
2148 return true;
2149 }
2150
2151 void scheduleAddStartingWindow() {
2152 // Note: we really want to do sendMessageAtFrontOfQueue() because we
2153 // want to process the message ASAP, before any other queued
2154 // messages.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002155 if (!mWmService.mAnimationHandler.hasCallbacks(mAddStartingWindow)) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002156 if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Enqueueing ADD_STARTING");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002157 mWmService.mAnimationHandler.postAtFrontOfQueue(mAddStartingWindow);
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002158 }
2159 }
2160
2161 private final Runnable mAddStartingWindow = new Runnable() {
2162
2163 @Override
2164 public void run() {
chaviwa8f07a72019-05-01 16:25:39 -07002165 // Can be accessed without holding the global lock
2166 final StartingData startingData;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002167 synchronized (mWmService.mGlobalLock) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002168 // There can only be one adding request, silly caller!
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002169 mWmService.mAnimationHandler.removeCallbacks(this);
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002170
chaviwa8f07a72019-05-01 16:25:39 -07002171 if (mStartingData == null) {
2172 // Animation has been canceled... do nothing.
2173 if (DEBUG_STARTING_WINDOW) {
2174 Slog.v(TAG, "startingData was nulled out before handling"
2175 + " mAddStartingWindow: " + AppWindowToken.this);
2176 }
2177 return;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002178 }
chaviwa8f07a72019-05-01 16:25:39 -07002179 startingData = mStartingData;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002180 }
2181
2182 if (DEBUG_STARTING_WINDOW) {
2183 Slog.v(TAG, "Add starting " + this + ": startingData=" + startingData);
2184 }
2185
2186 WindowManagerPolicy.StartingSurface surface = null;
2187 try {
2188 surface = startingData.createStartingSurface(AppWindowToken.this);
2189 } catch (Exception e) {
2190 Slog.w(TAG, "Exception when adding starting window", e);
2191 }
2192 if (surface != null) {
2193 boolean abort = false;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002194 synchronized (mWmService.mGlobalLock) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002195 // If the window was successfully added, then
2196 // we need to remove it.
chaviwa8f07a72019-05-01 16:25:39 -07002197 if (removed || mStartingData == null) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002198 if (DEBUG_STARTING_WINDOW) {
2199 Slog.v(TAG, "Aborted starting " + AppWindowToken.this
chaviwa8f07a72019-05-01 16:25:39 -07002200 + ": removed=" + removed + " startingData=" + mStartingData);
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002201 }
2202 startingWindow = null;
chaviwa8f07a72019-05-01 16:25:39 -07002203 mStartingData = null;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002204 abort = true;
2205 } else {
2206 startingSurface = surface;
2207 }
2208 if (DEBUG_STARTING_WINDOW && !abort) {
chaviwa8f07a72019-05-01 16:25:39 -07002209 Slog.v(TAG,
2210 "Added starting " + AppWindowToken.this + ": startingWindow="
2211 + startingWindow + " startingView=" + startingSurface);
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002212 }
2213 }
2214 if (abort) {
2215 surface.remove();
2216 }
2217 } else if (DEBUG_STARTING_WINDOW) {
2218 Slog.v(TAG, "Surface returned was null: " + AppWindowToken.this);
2219 }
2220 }
2221 };
2222
2223 private int getStartingWindowType(boolean newTask, boolean taskSwitch, boolean processRunning,
2224 boolean allowTaskSnapshot, boolean activityCreated, boolean fromRecents,
2225 ActivityManager.TaskSnapshot snapshot) {
2226 if (getDisplayContent().mAppTransition.getAppTransition()
2227 == TRANSIT_DOCK_TASK_FROM_RECENTS) {
2228 // TODO(b/34099271): Remove this statement to add back the starting window and figure
2229 // out why it causes flickering, the starting window appears over the thumbnail while
2230 // the docked from recents transition occurs
2231 return STARTING_WINDOW_TYPE_NONE;
2232 } else if (newTask || !processRunning || (taskSwitch && !activityCreated)) {
2233 return STARTING_WINDOW_TYPE_SPLASH_SCREEN;
2234 } else if (taskSwitch && allowTaskSnapshot) {
Kevin716a9db2018-12-18 16:53:39 -08002235 if (mWmService.mLowRamTaskSnapshotsAndRecents) {
Kevin58b47f12018-12-17 14:09:01 -08002236 // For low RAM devices, we use the splash screen starting window instead of the
2237 // task snapshot starting window.
2238 return STARTING_WINDOW_TYPE_SPLASH_SCREEN;
2239 }
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002240 return snapshot == null ? STARTING_WINDOW_TYPE_NONE
2241 : snapshotOrientationSameAsTask(snapshot) || fromRecents
2242 ? STARTING_WINDOW_TYPE_SNAPSHOT : STARTING_WINDOW_TYPE_SPLASH_SCREEN;
2243 } else {
2244 return STARTING_WINDOW_TYPE_NONE;
2245 }
2246 }
2247
2248
2249 private boolean snapshotOrientationSameAsTask(ActivityManager.TaskSnapshot snapshot) {
2250 if (snapshot == null) {
2251 return false;
2252 }
2253 return getTask().getConfiguration().orientation == snapshot.getOrientation();
2254 }
2255
2256 void removeStartingWindow() {
2257 if (startingWindow == null) {
chaviwa8f07a72019-05-01 16:25:39 -07002258 if (mStartingData != null) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002259 // Starting window has not been added yet, but it is scheduled to be added.
2260 // Go ahead and cancel the request.
2261 if (DEBUG_STARTING_WINDOW) {
2262 Slog.v(TAG_WM, "Clearing startingData for token=" + this);
2263 }
chaviwa8f07a72019-05-01 16:25:39 -07002264 mStartingData = null;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002265 }
2266 return;
2267 }
2268
2269 final WindowManagerPolicy.StartingSurface surface;
chaviwa8f07a72019-05-01 16:25:39 -07002270 if (mStartingData != null) {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002271 surface = startingSurface;
chaviwa8f07a72019-05-01 16:25:39 -07002272 mStartingData = null;
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002273 startingSurface = null;
2274 startingWindow = null;
2275 startingDisplayed = false;
2276 if (surface == null) {
2277 if (DEBUG_STARTING_WINDOW) {
2278 Slog.v(TAG_WM, "startingWindow was set but startingSurface==null, couldn't "
2279 + "remove");
2280 }
2281 return;
2282 }
2283 } else {
2284 if (DEBUG_STARTING_WINDOW) {
2285 Slog.v(TAG_WM, "Tried to remove starting window but startingWindow was null:"
2286 + this);
2287 }
2288 return;
2289 }
2290
2291 if (DEBUG_STARTING_WINDOW) {
2292 Slog.v(TAG_WM, "Schedule remove starting " + this
2293 + " startingWindow=" + startingWindow
2294 + " startingView=" + startingSurface
2295 + " Callers=" + Debug.getCallers(5));
2296 }
2297
2298 // Use the same thread to remove the window as we used to add it, as otherwise we end up
2299 // with things in the view hierarchy being called from different threads.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002300 mWmService.mAnimationHandler.post(() -> {
Yunfan Chen1ee84ea2018-11-13 16:03:37 -08002301 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Removing startingView=" + surface);
2302 try {
2303 surface.remove();
2304 } catch (Exception e) {
2305 Slog.w(TAG_WM, "Exception when removing starting window", e);
2306 }
2307 });
2308 }
2309
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07002310 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07002311 boolean fillsParent() {
2312 return mFillsParent;
2313 }
2314
2315 void setFillsParent(boolean fillsParent) {
2316 mFillsParent = fillsParent;
2317 }
2318
Jorim Jaggife762342016-10-13 14:33:27 +02002319 boolean containsDismissKeyguardWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07002320 // Window state is transient during relaunch. We are not guaranteed to be frozen during the
2321 // entirety of the relaunch.
2322 if (isRelaunching()) {
2323 return mLastContainsDismissKeyguardWindow;
2324 }
2325
Jorim Jaggife762342016-10-13 14:33:27 +02002326 for (int i = mChildren.size() - 1; i >= 0; i--) {
2327 if ((mChildren.get(i).mAttrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
2328 return true;
2329 }
2330 }
2331 return false;
2332 }
2333
2334 boolean containsShowWhenLockedWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07002335 // When we are relaunching, it is possible for us to be unfrozen before our previous
2336 // windows have been added back. Using the cached value ensures that our previous
2337 // showWhenLocked preference is honored until relaunching is complete.
2338 if (isRelaunching()) {
2339 return mLastContainsShowWhenLockedWindow;
2340 }
2341
Jorim Jaggife762342016-10-13 14:33:27 +02002342 for (int i = mChildren.size() - 1; i >= 0; i--) {
2343 if ((mChildren.get(i).mAttrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
2344 return true;
2345 }
2346 }
Bryce Lee081554b2017-05-25 07:52:12 -07002347
Jorim Jaggife762342016-10-13 14:33:27 +02002348 return false;
2349 }
2350
2351 void checkKeyguardFlagsChanged() {
2352 final boolean containsDismissKeyguard = containsDismissKeyguardWindow();
2353 final boolean containsShowWhenLocked = containsShowWhenLockedWindow();
2354 if (containsDismissKeyguard != mLastContainsDismissKeyguardWindow
2355 || containsShowWhenLocked != mLastContainsShowWhenLockedWindow) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002356 mWmService.notifyKeyguardFlagsChanged(null /* callback */,
lumark588a3e82018-07-20 18:53:54 +08002357 getDisplayContent().getDisplayId());
Jorim Jaggife762342016-10-13 14:33:27 +02002358 }
2359 mLastContainsDismissKeyguardWindow = containsDismissKeyguard;
2360 mLastContainsShowWhenLockedWindow = containsShowWhenLocked;
2361 }
2362
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002363 WindowState getImeTargetBelowWindow(WindowState w) {
2364 final int index = mChildren.indexOf(w);
2365 if (index > 0) {
2366 final WindowState target = mChildren.get(index - 1);
2367 if (target.canBeImeTarget()) {
2368 return target;
2369 }
2370 }
2371 return null;
2372 }
2373
2374 WindowState getHighestAnimLayerWindow(WindowState currentTarget) {
2375 WindowState candidate = null;
2376 for (int i = mChildren.indexOf(currentTarget); i >= 0; i--) {
2377 final WindowState w = mChildren.get(i);
2378 if (w.mRemoved) {
2379 continue;
2380 }
Jorim Jaggi35d328a2018-08-14 17:00:20 +02002381 if (candidate == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002382 candidate = w;
2383 }
2384 }
2385 return candidate;
2386 }
2387
Jorim Jaggid635a4a2017-05-03 15:21:26 +02002388 /**
2389 * See {@link Activity#setDisablePreviewScreenshots}.
2390 */
2391 void setDisablePreviewScreenshots(boolean disable) {
Wale Ogunwale6c459212017-05-17 08:56:03 -07002392 mDisablePreviewScreenshots = disable;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01002393 }
2394
Jorim Jaggid635a4a2017-05-03 15:21:26 +02002395 /**
chaviwd3bf08d2017-08-01 17:24:59 -07002396 * Sets whether the current launch can turn the screen on. See {@link #canTurnScreenOn()}
2397 */
2398 void setCanTurnScreenOn(boolean canTurnScreenOn) {
2399 mCanTurnScreenOn = canTurnScreenOn;
2400 }
2401
2402 /**
2403 * Indicates whether the current launch can turn the screen on. This is to prevent multiple
2404 * relayouts from turning the screen back on. The screen should only turn on at most
2405 * once per activity resume.
2406 *
2407 * @return true if the screen can be turned on.
2408 */
2409 boolean canTurnScreenOn() {
2410 return mCanTurnScreenOn;
2411 }
2412
2413 /**
Jorim Jaggid635a4a2017-05-03 15:21:26 +02002414 * Retrieves whether we'd like to generate a snapshot that's based solely on the theme. This is
2415 * the case when preview screenshots are disabled {@link #setDisablePreviewScreenshots} or when
2416 * we can't take a snapshot for other reasons, for example, if we have a secure window.
2417 *
2418 * @return True if we need to generate an app theme snapshot, false if we'd like to take a real
2419 * screenshot.
2420 */
2421 boolean shouldUseAppThemeSnapshot() {
Wale Ogunwale6c459212017-05-17 08:56:03 -07002422 return mDisablePreviewScreenshots || forAllWindows(w -> (w.mAttrs.flags & FLAG_SECURE) != 0,
Jorim Jaggid635a4a2017-05-03 15:21:26 +02002423 true /* topToBottom */);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01002424 }
2425
Jorim Jaggibe418292018-03-26 16:14:12 +02002426 SurfaceControl getAppAnimationLayer() {
Jorim Jaggi391790622018-04-18 15:30:44 +02002427 return getAppAnimationLayer(isActivityTypeHome() ? ANIMATION_LAYER_HOME
2428 : needsZBoost() ? ANIMATION_LAYER_BOOSTED
2429 : ANIMATION_LAYER_STANDARD);
Jorim Jaggibe418292018-03-26 16:14:12 +02002430 }
2431
chaviw23ee71c2017-12-18 11:29:41 -08002432 @Override
Jorim Jaggi596a1992017-12-29 14:48:02 +01002433 public SurfaceControl getAnimationLeashParent() {
Robert Carrb9506032018-02-13 13:54:00 -08002434 // All normal app transitions take place in an animation layer which is below the pinned
2435 // stack but may be above the parent stacks of the given animating apps.
2436 // For transitions in the pinned stack (menu activity) we just let them occur as a child
2437 // of the pinned stack.
2438 if (!inPinnedWindowingMode()) {
2439 return getAppAnimationLayer();
2440 } else {
2441 return getStack().getSurfaceControl();
2442 }
chaviw23ee71c2017-12-18 11:29:41 -08002443 }
2444
lumarkb5a78b32019-04-25 20:31:30 +08002445
2446 @VisibleForTesting
2447 boolean shouldAnimate(int transit) {
Jorim Jaggic6976f02018-04-18 16:31:07 +02002448 final boolean isSplitScreenPrimary =
2449 getWindowingMode() == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
2450 final boolean allowSplitScreenPrimaryAnimation = transit != TRANSIT_WALLPAPER_OPEN;
2451
lumarkb5a78b32019-04-25 20:31:30 +08002452 // Don't animate while the task runs recents animation but only if we are in the mode
2453 // where we cancel with deferred screenshot, which means that the controller has
2454 // transformed the task.
lumark54284462019-03-05 20:44:27 +08002455 final RecentsAnimationController controller = mWmService.getRecentsAnimationController();
lumarkb5a78b32019-04-25 20:31:30 +08002456 if (controller != null && controller.isAnimatingTask(getTask())
2457 && controller.shouldCancelWithDeferredScreenshot()) {
lumark54284462019-03-05 20:44:27 +08002458 return false;
2459 }
2460
Jorim Jaggic6976f02018-04-18 16:31:07 +02002461 // We animate always if it's not split screen primary, and only some special cases in split
2462 // screen primary because it causes issues with stack clipping when we run an un-minimize
2463 // animation at the same time.
2464 return !isSplitScreenPrimary || allowSplitScreenPrimaryAnimation;
2465 }
2466
Vishnu Naira2977262018-07-26 13:31:26 -07002467 /**
2468 * Creates a layer to apply crop to an animation.
2469 */
2470 private SurfaceControl createAnimationBoundsLayer(Transaction t) {
2471 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.i(TAG, "Creating animation bounds layer");
2472 final SurfaceControl.Builder builder = makeAnimationLeash()
2473 .setParent(getAnimationLeashParent())
Vishnu Naire86bd982018-11-28 13:23:17 -08002474 .setName(getSurfaceControl() + " - animation-bounds");
Vishnu Naira2977262018-07-26 13:31:26 -07002475 final SurfaceControl boundsLayer = builder.build();
2476 t.show(boundsLayer);
2477 return boundsLayer;
2478 }
2479
Evan Roskyed6767f2018-10-26 17:21:06 -07002480 @Override
2481 Rect getDisplayedBounds() {
2482 final Task task = getTask();
2483 if (task != null) {
2484 final Rect overrideDisplayedBounds = task.getOverrideDisplayedBounds();
2485 if (!overrideDisplayedBounds.isEmpty()) {
2486 return overrideDisplayedBounds;
2487 }
2488 }
2489 return getBounds();
2490 }
2491
Evan Rosky641daea2019-04-24 14:45:24 -07002492 @VisibleForTesting
2493 Rect getAnimationBounds(int appStackClipMode) {
2494 if (appStackClipMode == STACK_CLIP_BEFORE_ANIM && getStack() != null) {
2495 // Using the stack bounds here effectively applies the clipping before animation.
2496 return getStack().getBounds();
2497 }
2498 // Use task-bounds if available so that activity-level letterbox (maxAspectRatio) is
2499 // included in the animation.
2500 return getTask() != null ? getTask().getBounds() : getBounds();
2501 }
2502
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002503 boolean applyAnimationLocked(WindowManager.LayoutParams lp, int transit, boolean enter,
2504 boolean isVoiceInteraction) {
2505
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002506 if (mWmService.mDisableTransitionAnimation || !shouldAnimate(transit)) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002507 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) {
Jorim Jaggic6976f02018-04-18 16:31:07 +02002508 Slog.v(TAG_WM, "applyAnimation: transition animation is disabled or skipped."
2509 + " atoken=" + this);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002510 }
2511 cancelAnimation();
2512 return false;
2513 }
2514
2515 // Only apply an animation if the display isn't frozen. If it is frozen, there is no reason
2516 // to animate and it can cause strange artifacts when we unfreeze the display if some
2517 // different animation is running.
2518 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "AWT#applyAnimationLocked");
2519 if (okToAnimate()) {
Jorim Jaggi33a701a2017-12-01 14:58:18 +01002520 final AnimationAdapter adapter;
Evan Rosky2289ba12018-11-19 18:28:18 -08002521 AnimationAdapter thumbnailAdapter = null;
Evan Rosky4fb1e912019-03-06 13:54:43 -08002522
Evan Rosky641daea2019-04-24 14:45:24 -07002523 final int appStackClipMode =
2524 getDisplayContent().mAppTransition.getAppStackClipMode();
2525
2526 // Separate position and size for use in animators.
2527 mTmpRect.set(getAnimationBounds(appStackClipMode));
Evan Rosky4fb1e912019-03-06 13:54:43 -08002528 mTmpPoint.set(mTmpRect.left, mTmpRect.top);
2529 mTmpRect.offsetTo(0, 0);
Jorim Jaggic4d29f22018-03-22 16:30:56 +01002530
Evan Roskyec9488c2019-03-01 19:32:12 -08002531 final boolean isChanging = AppTransition.isChangeTransit(transit) && enter
2532 && getDisplayContent().mChangingApps.contains(this);
Evan Rosky2289ba12018-11-19 18:28:18 -08002533
Jorim Jaggic4d29f22018-03-22 16:30:56 +01002534 // Delaying animation start isn't compatible with remote animations at all.
lumark588a3e82018-07-20 18:53:54 +08002535 if (getDisplayContent().mAppTransition.getRemoteAnimationController() != null
Jorim Jaggic4d29f22018-03-22 16:30:56 +01002536 && !mSurfaceAnimator.isAnimationStartDelayed()) {
Evan Rosky2289ba12018-11-19 18:28:18 -08002537 RemoteAnimationRecord adapters =
2538 getDisplayContent().mAppTransition.getRemoteAnimationController()
2539 .createRemoteAnimationRecord(this, mTmpPoint, mTmpRect,
2540 (isChanging ? mTransitStartRect : null));
2541 adapter = adapters.mAdapter;
2542 thumbnailAdapter = adapters.mThumbnailAdapter;
2543 } else if (isChanging) {
Evan Roskycf76bed2019-01-15 10:33:58 -08002544 final float durationScale = mWmService.getTransitionAnimationScaleLocked();
Evan Rosky2289ba12018-11-19 18:28:18 -08002545 mTmpRect.offsetTo(mTmpPoint.x, mTmpPoint.y);
2546 adapter = new LocalAnimationAdapter(
2547 new WindowChangeAnimationSpec(mTransitStartRect, mTmpRect,
Evan Roskycf76bed2019-01-15 10:33:58 -08002548 getDisplayContent().getDisplayInfo(), durationScale,
Evan Rosky2289ba12018-11-19 18:28:18 -08002549 true /* isAppAnimation */, false /* isThumbnail */),
2550 mWmService.mSurfaceAnimationRunner);
Evan Rosky966759f2019-01-15 10:33:58 -08002551 if (mThumbnail != null) {
2552 thumbnailAdapter = new LocalAnimationAdapter(
2553 new WindowChangeAnimationSpec(mTransitStartRect, mTmpRect,
Evan Roskycf76bed2019-01-15 10:33:58 -08002554 getDisplayContent().getDisplayInfo(), durationScale,
Evan Rosky966759f2019-01-15 10:33:58 -08002555 true /* isAppAnimation */, true /* isThumbnail */),
2556 mWmService.mSurfaceAnimationRunner);
2557 }
Evan Rosky2289ba12018-11-19 18:28:18 -08002558 mTransit = transit;
2559 mTransitFlags = getDisplayContent().mAppTransition.getTransitFlags();
Jorim Jaggi33a701a2017-12-01 14:58:18 +01002560 } else {
Vishnu Naira2977262018-07-26 13:31:26 -07002561 mNeedsAnimationBoundsLayer = (appStackClipMode == STACK_CLIP_AFTER_ANIM);
2562
Jorim Jaggi33a701a2017-12-01 14:58:18 +01002563 final Animation a = loadAnimation(lp, transit, enter, isVoiceInteraction);
2564 if (a != null) {
Lucas Dupin95acf432019-04-29 17:55:38 -07002565 // Only apply corner radius to animation if we're not in multi window mode.
2566 // We don't want rounded corners when in pip or split screen.
2567 final float windowCornerRadius = !inMultiWindowMode()
2568 ? getDisplayContent().getWindowCornerRadius()
2569 : 0;
Jorim Jaggi33a701a2017-12-01 14:58:18 +01002570 adapter = new LocalAnimationAdapter(
2571 new WindowAnimationSpec(a, mTmpPoint, mTmpRect,
lumark588a3e82018-07-20 18:53:54 +08002572 getDisplayContent().mAppTransition.canSkipFirstFrame(),
Vishnu Naira2977262018-07-26 13:31:26 -07002573 appStackClipMode,
Lucas Dupin3e1dc202018-12-02 15:14:20 -08002574 true /* isAppAnimation */,
Lucas Dupin95acf432019-04-29 17:55:38 -07002575 windowCornerRadius),
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002576 mWmService.mSurfaceAnimationRunner);
Jorim Jaggi33a701a2017-12-01 14:58:18 +01002577 if (a.getZAdjustment() == Animation.ZORDER_TOP) {
2578 mNeedsZBoost = true;
2579 }
2580 mTransit = transit;
lumark588a3e82018-07-20 18:53:54 +08002581 mTransitFlags = getDisplayContent().mAppTransition.getTransitFlags();
Jorim Jaggi33a701a2017-12-01 14:58:18 +01002582 } else {
2583 adapter = null;
chaviw23ee71c2017-12-18 11:29:41 -08002584 }
Jorim Jaggi33a701a2017-12-01 14:58:18 +01002585 }
2586 if (adapter != null) {
Jorim Jaggi619c9f72017-12-19 18:04:29 +01002587 startAnimation(getPendingTransaction(), adapter, !isVisible());
Jorim Jaggi82c17862018-02-21 17:50:18 +01002588 if (adapter.getShowWallpaper()) {
2589 mDisplayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
2590 }
Evan Rosky2289ba12018-11-19 18:28:18 -08002591 if (thumbnailAdapter != null) {
2592 mThumbnail.startAnimation(
2593 getPendingTransaction(), thumbnailAdapter, !isVisible());
2594 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002595 }
2596 } else {
2597 cancelAnimation();
2598 }
2599 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
2600
2601 return isReallyAnimating();
2602 }
2603
2604 private Animation loadAnimation(WindowManager.LayoutParams lp, int transit, boolean enter,
2605 boolean isVoiceInteraction) {
2606 final DisplayContent displayContent = getTask().getDisplayContent();
2607 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
2608 final int width = displayInfo.appWidth;
2609 final int height = displayInfo.appHeight;
2610 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.v(TAG_WM,
2611 "applyAnimation: atoken=" + this);
2612
2613 // Determine the visible rect to calculate the thumbnail clip
2614 final WindowState win = findMainWindow();
2615 final Rect frame = new Rect(0, 0, width, height);
2616 final Rect displayFrame = new Rect(0, 0,
2617 displayInfo.logicalWidth, displayInfo.logicalHeight);
2618 final Rect insets = new Rect();
2619 final Rect stableInsets = new Rect();
2620 Rect surfaceInsets = null;
2621 final boolean freeform = win != null && win.inFreeformWindowingMode();
2622 if (win != null) {
2623 // Containing frame will usually cover the whole screen, including dialog windows.
2624 // For freeform workspace windows it will not cover the whole screen and it also
2625 // won't exactly match the final freeform window frame (e.g. when overlapping with
2626 // the status bar). In that case we need to use the final frame.
2627 if (freeform) {
chaviw492139a2018-07-16 16:07:35 -07002628 frame.set(win.getFrameLw());
Adrian Roos4d18a2e2017-12-19 19:08:05 +01002629 } else if (win.isLetterboxedAppWindow()) {
2630 frame.set(getTask().getBounds());
Winson Chungc1674272018-02-21 10:15:17 -08002631 } else if (win.isDockedResizing()) {
2632 // If we are animating while docked resizing, then use the stack bounds as the
2633 // animation target (which will be different than the task bounds)
2634 frame.set(getTask().getParent().getBounds());
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002635 } else {
chaviw553b0212018-07-12 13:37:01 -07002636 frame.set(win.getContainingFrame());
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002637 }
2638 surfaceInsets = win.getAttrs().surfaceInsets;
Adrian Roos20e07892018-02-23 19:12:01 +01002639 // XXX(b/72757033): These are insets relative to the window frame, but we're really
2640 // interested in the insets relative to the frame we chose in the if-blocks above.
chaviw9c81e632018-07-31 11:17:52 -07002641 win.getContentInsets(insets);
2642 win.getStableInsets(stableInsets);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002643 }
2644
2645 if (mLaunchTaskBehind) {
2646 // Differentiate the two animations. This one which is briefly on the screen
2647 // gets the !enter animation, and the other activity which remains on the
2648 // screen gets the enter animation. Both appear in the mOpeningApps set.
2649 enter = false;
2650 }
2651 if (DEBUG_APP_TRANSITIONS) Slog.d(TAG_WM, "Loading animation for app transition."
2652 + " transit=" + AppTransition.appTransitionToString(transit) + " enter=" + enter
2653 + " frame=" + frame + " insets=" + insets + " surfaceInsets=" + surfaceInsets);
2654 final Configuration displayConfig = displayContent.getConfiguration();
lumark588a3e82018-07-20 18:53:54 +08002655 final Animation a = getDisplayContent().mAppTransition.loadAnimation(lp, transit, enter,
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002656 displayConfig.uiMode, displayConfig.orientation, frame, displayFrame, insets,
2657 surfaceInsets, stableInsets, isVoiceInteraction, freeform, getTask().mTaskId);
2658 if (a != null) {
2659 if (DEBUG_ANIM) logWithStack(TAG, "Loaded animation " + a + " for " + this);
2660 final int containingWidth = frame.width();
2661 final int containingHeight = frame.height();
2662 a.initialize(containingWidth, containingHeight, width, height);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002663 a.scaleCurrentDuration(mWmService.getTransitionAnimationScaleLocked());
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002664 }
2665 return a;
2666 }
2667
Jorim Jaggi619c9f72017-12-19 18:04:29 +01002668 @Override
Jorim Jaggi6de61012018-03-19 14:53:23 +01002669 public boolean shouldDeferAnimationFinish(Runnable endDeferFinishCallback) {
2670 return mAnimatingAppWindowTokenRegistry != null
2671 && mAnimatingAppWindowTokenRegistry.notifyAboutToFinish(
2672 this, endDeferFinishCallback);
2673 }
2674
2675 @Override
lumarkf6f34942019-04-29 16:56:50 +08002676 public void onAnimationLeashLost(Transaction t) {
2677 super.onAnimationLeashLost(t);
Vishnu Naira2977262018-07-26 13:31:26 -07002678 if (mAnimationBoundsLayer != null) {
Robert Carr71200f22019-02-05 09:44:53 -08002679 t.remove(mAnimationBoundsLayer);
Vishnu Naira2977262018-07-26 13:31:26 -07002680 mAnimationBoundsLayer = null;
2681 }
2682
Jorim Jaggi6de61012018-03-19 14:53:23 +01002683 if (mAnimatingAppWindowTokenRegistry != null) {
2684 mAnimatingAppWindowTokenRegistry.notifyFinished(this);
2685 }
2686 }
2687
2688 @Override
Jorim Jaggi619c9f72017-12-19 18:04:29 +01002689 protected void setLayer(Transaction t, int layer) {
2690 if (!mSurfaceAnimator.hasLeash()) {
2691 t.setLayer(mSurfaceControl, layer);
2692 }
2693 }
2694
2695 @Override
2696 protected void setRelativeLayer(Transaction t, SurfaceControl relativeTo, int layer) {
2697 if (!mSurfaceAnimator.hasLeash()) {
2698 t.setRelativeLayer(mSurfaceControl, relativeTo, layer);
2699 }
2700 }
2701
2702 @Override
2703 protected void reparentSurfaceControl(Transaction t, SurfaceControl newParent) {
2704 if (!mSurfaceAnimator.hasLeash()) {
Robert Carr10584fa2019-01-14 15:55:19 -08002705 t.reparent(mSurfaceControl, newParent);
Jorim Jaggi619c9f72017-12-19 18:04:29 +01002706 }
2707 }
2708
2709 @Override
2710 public void onAnimationLeashCreated(Transaction t, SurfaceControl leash) {
Jorim Jaggi619c9f72017-12-19 18:04:29 +01002711 // The leash is parented to the animation layer. We need to preserve the z-order by using
2712 // the prefix order index, but we boost if necessary.
Robert Carrb9506032018-02-13 13:54:00 -08002713 int layer = 0;
2714 if (!inPinnedWindowingMode()) {
2715 layer = getPrefixOrderIndex();
2716 } else {
2717 // Pinned stacks have animations take place within themselves rather than an animation
2718 // layer so we need to preserve the order relative to the stack (e.g. the order of our
2719 // task/parent).
2720 layer = getParent().getPrefixOrderIndex();
2721 }
2722
Jorim Jaggi619c9f72017-12-19 18:04:29 +01002723 if (mNeedsZBoost) {
2724 layer += Z_BOOST_BASE;
2725 }
Jorim Jaggi9cd99132019-04-24 17:41:35 +02002726 if (!mNeedsAnimationBoundsLayer) {
2727 leash.setLayer(layer);
2728 }
Robert Carr2f8aa392018-01-31 14:46:51 -08002729
2730 final DisplayContent dc = getDisplayContent();
Jorim Jaggibe418292018-03-26 16:14:12 +02002731 dc.assignStackOrdering();
Evan Rosky2289ba12018-11-19 18:28:18 -08002732
2733 if (leash == mTransitChangeLeash) {
2734 // This is a temporary state so skip any animation notifications
2735 return;
2736 } else if (mTransitChangeLeash != null) {
2737 // unparent mTransitChangeLeash for clean-up
Evan Rosky25b56192019-02-06 16:10:56 -08002738 clearChangeLeash(t, false /* cancel */);
Evan Rosky2289ba12018-11-19 18:28:18 -08002739 }
2740
Jorim Jaggi6de61012018-03-19 14:53:23 +01002741 if (mAnimatingAppWindowTokenRegistry != null) {
2742 mAnimatingAppWindowTokenRegistry.notifyStarting(this);
2743 }
Vishnu Naira2977262018-07-26 13:31:26 -07002744
2745 // If the animation needs to be cropped then an animation bounds layer is created as a child
2746 // of the pinned stack or animation layer. The leash is then reparented to this new layer.
2747 if (mNeedsAnimationBoundsLayer) {
Evan Rosky7735d352019-03-18 17:27:27 -07002748 mTmpRect.setEmpty();
2749 final Task task = getTask();
2750 if (getDisplayContent().mAppTransitionController.isTransitWithinTask(
2751 getTransit(), task)) {
2752 task.getBounds(mTmpRect);
2753 } else {
2754 final TaskStack stack = getStack();
2755 if (stack == null) {
2756 return;
2757 }
2758 // Set clip rect to stack bounds.
2759 stack.getBounds(mTmpRect);
Vishnu Naira2977262018-07-26 13:31:26 -07002760 }
2761 mAnimationBoundsLayer = createAnimationBoundsLayer(t);
2762
Vishnu Naira2977262018-07-26 13:31:26 -07002763 // Crop to stack bounds.
2764 t.setWindowCrop(mAnimationBoundsLayer, mTmpRect);
Jorim Jaggi9cd99132019-04-24 17:41:35 +02002765 t.setLayer(mAnimationBoundsLayer, layer);
Vishnu Naira2977262018-07-26 13:31:26 -07002766
2767 // Reparent leash to animation bounds layer.
Robert Carr10584fa2019-01-14 15:55:19 -08002768 t.reparent(leash, mAnimationBoundsLayer);
Vishnu Naira2977262018-07-26 13:31:26 -07002769 }
Jorim Jaggi619c9f72017-12-19 18:04:29 +01002770 }
2771
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002772 /**
2773 * This must be called while inside a transaction.
2774 */
2775 void showAllWindowsLocked() {
2776 forAllWindows(windowState -> {
2777 if (DEBUG_VISIBILITY) Slog.v(TAG, "performing show on: " + windowState);
2778 windowState.performShowLocked();
2779 }, false /* traverseTopToBottom */);
Jorim Jaggia5e10572017-11-15 14:36:26 +01002780 }
2781
2782 @Override
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002783 protected void onAnimationFinished() {
2784 super.onAnimationFinished();
2785
Ian8b2822e2019-05-14 11:59:02 -07002786 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "AWT#onAnimationFinished");
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002787 mTransit = TRANSIT_UNSET;
2788 mTransitFlags = 0;
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002789 mNeedsZBoost = false;
Vishnu Naira2977262018-07-26 13:31:26 -07002790 mNeedsAnimationBoundsLayer = false;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002791
2792 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM | FINISH_LAYOUT_REDO_WALLPAPER,
2793 "AppWindowToken");
2794
Jorim Jaggi988f6682017-11-17 17:46:43 +01002795 clearThumbnail();
Jorim Jaggi752cd822018-03-29 16:29:18 +02002796 setClientHidden(isHidden() && hiddenRequested);
Jorim Jaggi988f6682017-11-17 17:46:43 +01002797
lumarkff0ab692018-11-05 20:32:30 +08002798 getDisplayContent().computeImeTargetIfNeeded(this);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002799
2800 if (DEBUG_ANIM) Slog.v(TAG, "Animation done in " + this
2801 + ": reportedVisible=" + reportedVisible
2802 + " okToDisplay=" + okToDisplay()
2803 + " okToAnimate=" + okToAnimate()
2804 + " startingDisplayed=" + startingDisplayed);
2805
Evan Rosky2289ba12018-11-19 18:28:18 -08002806 // clean up thumbnail window
2807 if (mThumbnail != null) {
2808 mThumbnail.destroy();
2809 mThumbnail = null;
2810 }
2811
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002812 // WindowState.onExitAnimationDone might modify the children list, so make a copy and then
2813 // traverse the copy.
2814 final ArrayList<WindowState> children = new ArrayList<>(mChildren);
2815 children.forEach(WindowState::onExitAnimationDone);
2816
lumark588a3e82018-07-20 18:53:54 +08002817 getDisplayContent().mAppTransition.notifyAppTransitionFinishedLocked(token);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002818 scheduleAnimation();
Jorim Jaggi9b5e3312019-03-01 18:08:00 +01002819
2820 mActivityRecord.onAnimationFinished();
Ian8b2822e2019-05-14 11:59:02 -07002821 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002822 }
2823
2824 @Override
2825 boolean isAppAnimating() {
2826 return isSelfAnimating();
2827 }
2828
2829 @Override
2830 boolean isSelfAnimating() {
2831 // If we are about to start a transition, we also need to be considered animating.
2832 return isWaitingForTransitionStart() || isReallyAnimating();
2833 }
2834
2835 /**
2836 * @return True if and only if we are actually running an animation. Note that
2837 * {@link #isSelfAnimating} also returns true if we are waiting for an animation to
2838 * start.
2839 */
2840 private boolean isReallyAnimating() {
2841 return super.isSelfAnimating();
2842 }
2843
Evan Rosky25b56192019-02-06 16:10:56 -08002844 /**
2845 * @param cancel {@code true} if clearing the leash due to cancelling instead of transferring
2846 * to another leash.
2847 */
2848 private void clearChangeLeash(Transaction t, boolean cancel) {
2849 if (mTransitChangeLeash == null) {
2850 return;
2851 }
2852 if (cancel) {
2853 clearThumbnail();
2854 SurfaceControl sc = getSurfaceControl();
2855 SurfaceControl parentSc = getParentSurfaceControl();
2856 // Don't reparent if surface is getting destroyed
2857 if (parentSc != null && sc != null) {
2858 t.reparent(sc, getParentSurfaceControl());
2859 }
2860 }
2861 t.hide(mTransitChangeLeash);
Evan Rosky08e20932019-05-14 10:54:07 -07002862 t.remove(mTransitChangeLeash);
Evan Rosky25b56192019-02-06 16:10:56 -08002863 mTransitChangeLeash = null;
2864 if (cancel) {
lumarkf6f34942019-04-29 16:56:50 +08002865 onAnimationLeashLost(t);
Evan Rosky25b56192019-02-06 16:10:56 -08002866 }
2867 }
2868
Jorim Jaggi988f6682017-11-17 17:46:43 +01002869 @Override
2870 void cancelAnimation() {
Evan Rosky2289ba12018-11-19 18:28:18 -08002871 cancelAnimationOnly();
Jorim Jaggi988f6682017-11-17 17:46:43 +01002872 clearThumbnail();
Evan Rosky25b56192019-02-06 16:10:56 -08002873 clearChangeLeash(getPendingTransaction(), true /* cancel */);
Evan Rosky2289ba12018-11-19 18:28:18 -08002874 }
2875
2876 /**
2877 * This only cancels the animation. It doesn't do other teardown like cleaning-up thumbnail
2878 * or interim leashes.
2879 * <p>
2880 * Used when canceling in preparation for starting a new animation.
2881 */
2882 void cancelAnimationOnly() {
2883 super.cancelAnimation();
Jorim Jaggi988f6682017-11-17 17:46:43 +01002884 }
2885
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002886 boolean isWaitingForTransitionStart() {
lumark588a3e82018-07-20 18:53:54 +08002887 return getDisplayContent().mAppTransition.isTransitionSet()
2888 && (getDisplayContent().mOpeningApps.contains(this)
Evan Rosky2289ba12018-11-19 18:28:18 -08002889 || getDisplayContent().mClosingApps.contains(this)
2890 || getDisplayContent().mChangingApps.contains(this));
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002891 }
2892
2893 public int getTransit() {
2894 return mTransit;
2895 }
2896
2897 int getTransitFlags() {
2898 return mTransitFlags;
2899 }
2900
Jorim Jaggi988f6682017-11-17 17:46:43 +01002901 void attachThumbnailAnimation() {
2902 if (!isReallyAnimating()) {
2903 return;
2904 }
2905 final int taskId = getTask().mTaskId;
2906 final GraphicBuffer thumbnailHeader =
lumark588a3e82018-07-20 18:53:54 +08002907 getDisplayContent().mAppTransition.getAppTransitionThumbnailHeader(taskId);
Jorim Jaggi988f6682017-11-17 17:46:43 +01002908 if (thumbnailHeader == null) {
2909 if (DEBUG_APP_TRANSITIONS) Slog.d(TAG, "No thumbnail header bitmap for: " + taskId);
2910 return;
2911 }
2912 clearThumbnail();
2913 mThumbnail = new AppWindowThumbnail(getPendingTransaction(), this, thumbnailHeader);
2914 mThumbnail.startAnimation(getPendingTransaction(), loadThumbnailAnimation(thumbnailHeader));
2915 }
2916
Tony Mak64b8d562017-12-28 17:44:02 +00002917 /**
2918 * Attaches a surface with a thumbnail for the
2919 * {@link android.app.ActivityOptions#ANIM_OPEN_CROSS_PROFILE_APPS} animation.
2920 */
2921 void attachCrossProfileAppsThumbnailAnimation() {
2922 if (!isReallyAnimating()) {
2923 return;
2924 }
2925 clearThumbnail();
2926
2927 final WindowState win = findMainWindow();
2928 if (win == null) {
2929 return;
2930 }
chaviw492139a2018-07-16 16:07:35 -07002931 final Rect frame = win.getFrameLw();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002932 final int thumbnailDrawableRes = getTask().mUserId == mWmService.mCurrentUserId
Tony Mak64b8d562017-12-28 17:44:02 +00002933 ? R.drawable.ic_account_circle
Tony Makda4af232018-04-27 11:01:10 +01002934 : R.drawable.ic_corp_badge;
Tony Mak64b8d562017-12-28 17:44:02 +00002935 final GraphicBuffer thumbnail =
lumark588a3e82018-07-20 18:53:54 +08002936 getDisplayContent().mAppTransition
Tony Mak64b8d562017-12-28 17:44:02 +00002937 .createCrossProfileAppsThumbnail(thumbnailDrawableRes, frame);
2938 if (thumbnail == null) {
2939 return;
2940 }
2941 mThumbnail = new AppWindowThumbnail(getPendingTransaction(), this, thumbnail);
2942 final Animation animation =
lumark588a3e82018-07-20 18:53:54 +08002943 getDisplayContent().mAppTransition.createCrossProfileAppsThumbnailAnimationLocked(
chaviw492139a2018-07-16 16:07:35 -07002944 win.getFrameLw());
Tony Mak64b8d562017-12-28 17:44:02 +00002945 mThumbnail.startAnimation(getPendingTransaction(), animation, new Point(frame.left,
2946 frame.top));
2947 }
2948
Jorim Jaggi988f6682017-11-17 17:46:43 +01002949 private Animation loadThumbnailAnimation(GraphicBuffer thumbnailHeader) {
2950 final DisplayInfo displayInfo = mDisplayContent.getDisplayInfo();
2951
2952 // If this is a multi-window scenario, we use the windows frame as
2953 // destination of the thumbnail header animation. If this is a full screen
2954 // window scenario, we use the whole display as the target.
2955 WindowState win = findMainWindow();
2956 Rect appRect = win != null ? win.getContentFrameLw() :
2957 new Rect(0, 0, displayInfo.appWidth, displayInfo.appHeight);
chaviw9c81e632018-07-31 11:17:52 -07002958 final Rect insets = win != null ? win.getContentInsets() : null;
Jorim Jaggi988f6682017-11-17 17:46:43 +01002959 final Configuration displayConfig = mDisplayContent.getConfiguration();
lumark588a3e82018-07-20 18:53:54 +08002960 return getDisplayContent().mAppTransition.createThumbnailAspectScaleAnimationLocked(
Jorim Jaggi988f6682017-11-17 17:46:43 +01002961 appRect, insets, thumbnailHeader, getTask().mTaskId, displayConfig.uiMode,
2962 displayConfig.orientation);
2963 }
2964
2965 private void clearThumbnail() {
2966 if (mThumbnail == null) {
2967 return;
2968 }
2969 mThumbnail.destroy();
2970 mThumbnail = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002971 }
2972
Jorim Jaggif84e2f62018-01-16 14:17:59 +01002973 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
2974 mRemoteAnimationDefinition = definition;
2975 }
2976
2977 RemoteAnimationDefinition getRemoteAnimationDefinition() {
2978 return mRemoteAnimationDefinition;
2979 }
2980
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002981 @Override
2982 void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2983 super.dump(pw, prefix, dumpAll);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002984 if (appToken != null) {
Wale Ogunwale72919d22016-12-08 18:58:50 -08002985 pw.println(prefix + "app=true mVoiceInteraction=" + mVoiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002986 }
Winson Chung48b25652018-10-22 14:04:30 -07002987 pw.println(prefix + "component=" + mActivityComponent.flattenToShortString());
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08002988 pw.print(prefix); pw.print("task="); pw.println(getTask());
Wale Ogunwale51362492016-09-08 17:49:17 -07002989 pw.print(prefix); pw.print(" mFillsParent="); pw.print(mFillsParent);
2990 pw.print(" mOrientation="); pw.println(mOrientation);
Wale Ogunwale89973222017-04-23 18:39:45 -07002991 pw.println(prefix + "hiddenRequested=" + hiddenRequested + " mClientHidden=" + mClientHidden
2992 + ((mDeferHidingClient) ? " mDeferHidingClient=" + mDeferHidingClient : "")
2993 + " reportedDrawn=" + reportedDrawn + " reportedVisible=" + reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -07002994 if (paused) {
2995 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002996 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -08002997 if (mAppStopped) {
2998 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
2999 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07003000 if (mNumInterestingWindows != 0 || mNumDrawnWindows != 0
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003001 || allDrawn || mLastAllDrawn) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07003002 pw.print(prefix); pw.print("mNumInterestingWindows=");
3003 pw.print(mNumInterestingWindows);
3004 pw.print(" mNumDrawnWindows="); pw.print(mNumDrawnWindows);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08003005 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -07003006 pw.print(" allDrawn="); pw.print(allDrawn);
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003007 pw.print(" lastAllDrawn="); pw.print(mLastAllDrawn);
Craig Mautner6fbda632012-07-03 09:26:39 -07003008 pw.println(")");
3009 }
3010 if (inPendingTransaction) {
3011 pw.print(prefix); pw.print("inPendingTransaction=");
3012 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08003013 }
chaviwa8f07a72019-05-01 16:25:39 -07003014 if (mStartingData != null || removed || firstWindowDrawn || mIsExiting) {
3015 pw.print(prefix); pw.print("startingData="); pw.print(mStartingData);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08003016 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -08003017 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -08003018 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08003019 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08003020 if (startingWindow != null || startingSurface != null
Wale Ogunwale6c459212017-05-17 08:56:03 -07003021 || startingDisplayed || startingMoved || mHiddenSetFromTransferredStartingWindow) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08003022 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08003023 pw.print(" startingSurface="); pw.print(startingSurface);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08003024 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale6c459212017-05-17 08:56:03 -07003025 pw.print(" startingMoved="); pw.print(startingMoved);
3026 pw.println(" mHiddenSetFromTransferredStartingWindow="
3027 + mHiddenSetFromTransferredStartingWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08003028 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01003029 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -08003030 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -07003031 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -08003032 }
3033 if (mPendingRelaunchCount != 0) {
3034 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +01003035 }
Riddle Hsub398da32019-01-21 21:48:16 +08003036 if (mSizeCompatScale != 1f || mSizeCompatBounds != null) {
3037 pw.println(prefix + "mSizeCompatScale=" + mSizeCompatScale + " mSizeCompatBounds="
3038 + mSizeCompatBounds);
3039 }
Wale Ogunwalee287e192017-04-21 09:30:12 -07003040 if (mRemovingFromDisplay) {
3041 pw.println(prefix + "mRemovingFromDisplay=" + mRemovingFromDisplay);
3042 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003043 }
3044
3045 @Override
3046 void setHidden(boolean hidden) {
3047 super.setHidden(hidden);
Winson Chunge55c0192017-08-24 14:50:48 -07003048
3049 if (hidden) {
3050 // Once the app window is hidden, reset the last saved PiP snap fraction
3051 mDisplayContent.mPinnedStackControllerLocked.resetReentrySnapFraction(this);
3052 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003053 scheduleAnimation();
3054 }
3055
3056 @Override
3057 void prepareSurfaces() {
3058 // isSelfAnimating also returns true when we are about to start a transition, so we need
3059 // to check super here.
3060 final boolean reallyAnimating = super.isSelfAnimating();
3061 final boolean show = !isHidden() || reallyAnimating;
Robert Carrd8e4fb72019-01-31 11:14:30 -08003062
3063 if (mSurfaceControl != null) {
3064 if (show && !mLastSurfaceShowing) {
Tiger Huanged6794e2019-05-07 20:07:59 +08003065 getPendingTransaction().show(mSurfaceControl);
Robert Carrd8e4fb72019-01-31 11:14:30 -08003066 } else if (!show && mLastSurfaceShowing) {
Tiger Huanged6794e2019-05-07 20:07:59 +08003067 getPendingTransaction().hide(mSurfaceControl);
Robert Carrd8e4fb72019-01-31 11:14:30 -08003068 }
Jorim Jaggi4d1835d2017-08-31 17:28:27 +02003069 }
Jorim Jaggi988f6682017-11-17 17:46:43 +01003070 if (mThumbnail != null) {
Tiger Huanged6794e2019-05-07 20:07:59 +08003071 mThumbnail.setShowing(getPendingTransaction(), show);
Jorim Jaggi988f6682017-11-17 17:46:43 +01003072 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003073 mLastSurfaceShowing = show;
3074 super.prepareSurfaces();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08003075 }
3076
Jorim Jaggi77d0f36c2018-03-16 17:49:49 +01003077 /**
3078 * @return Whether our {@link #getSurfaceControl} is currently showing.
3079 */
3080 boolean isSurfaceShowing() {
3081 return mLastSurfaceShowing;
3082 }
3083
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003084 boolean isFreezingScreen() {
3085 return mFreezingScreen;
3086 }
3087
3088 @Override
3089 boolean needsZBoost() {
3090 return mNeedsZBoost || super.needsZBoost();
3091 }
3092
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07003093 @CallSuper
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08003094 @Override
Nataniel Borges023ecb52019-01-16 14:15:43 -08003095 public void writeToProto(ProtoOutputStream proto, long fieldId,
3096 @WindowTraceLogLevel int logLevel) {
3097 // Critical log level logs only visible elements to mitigate performance overheard
3098 if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible()) {
3099 return;
3100 }
3101
Steven Timotiusaf03df62017-07-18 16:56:43 -07003102 final long token = proto.start(fieldId);
3103 writeNameToProto(proto, NAME);
Nataniel Borges023ecb52019-01-16 14:15:43 -08003104 super.writeToProto(proto, WINDOW_TOKEN, logLevel);
Vishnu Nair04ab4392018-01-10 11:00:06 -08003105 proto.write(LAST_SURFACE_SHOWING, mLastSurfaceShowing);
3106 proto.write(IS_WAITING_FOR_TRANSITION_START, isWaitingForTransitionStart());
3107 proto.write(IS_REALLY_ANIMATING, isReallyAnimating());
3108 if (mThumbnail != null){
3109 mThumbnail.writeToProto(proto, THUMBNAIL);
3110 }
3111 proto.write(FILLS_PARENT, mFillsParent);
3112 proto.write(APP_STOPPED, mAppStopped);
3113 proto.write(HIDDEN_REQUESTED, hiddenRequested);
3114 proto.write(CLIENT_HIDDEN, mClientHidden);
3115 proto.write(DEFER_HIDING_CLIENT, mDeferHidingClient);
3116 proto.write(REPORTED_DRAWN, reportedDrawn);
3117 proto.write(REPORTED_VISIBLE, reportedVisible);
3118 proto.write(NUM_INTERESTING_WINDOWS, mNumInterestingWindows);
3119 proto.write(NUM_DRAWN_WINDOWS, mNumDrawnWindows);
3120 proto.write(ALL_DRAWN, allDrawn);
3121 proto.write(LAST_ALL_DRAWN, mLastAllDrawn);
3122 proto.write(REMOVED, removed);
Nataniel Borges023ecb52019-01-16 14:15:43 -08003123 if (startingWindow != null) {
Vishnu Nair04ab4392018-01-10 11:00:06 -08003124 startingWindow.writeIdentifierToProto(proto, STARTING_WINDOW);
3125 }
3126 proto.write(STARTING_DISPLAYED, startingDisplayed);
3127 proto.write(STARTING_MOVED, startingMoved);
3128 proto.write(HIDDEN_SET_FROM_TRANSFERRED_STARTING_WINDOW,
3129 mHiddenSetFromTransferredStartingWindow);
3130 for (Rect bounds : mFrozenBounds) {
3131 bounds.writeToProto(proto, FROZEN_BOUNDS);
3132 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07003133 proto.end(token);
3134 }
3135
3136 void writeNameToProto(ProtoOutputStream proto, long fieldId) {
3137 if (appToken == null) {
3138 return;
3139 }
3140 try {
3141 proto.write(fieldId, appToken.getName());
3142 } catch (RemoteException e) {
3143 // This shouldn't happen, but in this case fall back to outputting nothing
3144 Slog.e(TAG, e.toString());
3145 }
3146 }
3147
3148 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08003149 public String toString() {
3150 if (stringName == null) {
3151 StringBuilder sb = new StringBuilder();
3152 sb.append("AppWindowToken{");
3153 sb.append(Integer.toHexString(System.identityHashCode(this)));
3154 sb.append(" token="); sb.append(token); sb.append('}');
3155 stringName = sb.toString();
3156 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07003157 return stringName + ((mIsExiting) ? " mIsExiting=" : "");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08003158 }
Adrian Roos20e07892018-02-23 19:12:01 +01003159
3160 Rect getLetterboxInsets() {
3161 if (mLetterbox != null) {
3162 return mLetterbox.getInsets();
3163 } else {
3164 return new Rect();
3165 }
3166 }
Adrian Roos23df3a32018-03-15 15:41:13 +01003167
Riddle Hsu69de5fb2019-02-23 01:19:50 +08003168 /** Gets the inner bounds of letterbox. The bounds will be empty if there is no letterbox. */
3169 void getLetterboxInnerBounds(Rect outBounds) {
3170 if (mLetterbox != null) {
3171 outBounds.set(mLetterbox.getInnerFrame());
3172 } else {
3173 outBounds.setEmpty();
3174 }
3175 }
3176
Adrian Roos23df3a32018-03-15 15:41:13 +01003177 /**
Riddle Hsu69de5fb2019-02-23 01:19:50 +08003178 * @return {@code true} if there is a letterbox and any part of that letterbox overlaps with
Adrian Roos23df3a32018-03-15 15:41:13 +01003179 * the given {@code rect}.
3180 */
3181 boolean isLetterboxOverlappingWith(Rect rect) {
3182 return mLetterbox != null && mLetterbox.isOverlappingWith(rect);
3183 }
chaviw4ad54912018-05-30 11:05:44 -07003184
3185 /**
3186 * Sets if this AWT is in the process of closing or entering PIP.
3187 * {@link #mWillCloseOrEnterPip}}
3188 */
3189 void setWillCloseOrEnterPip(boolean willCloseOrEnterPip) {
3190 mWillCloseOrEnterPip = willCloseOrEnterPip;
3191 }
3192
3193 /**
3194 * Returns whether this AWT is considered closing. Conditions are either
3195 * 1. Is this app animating and was requested to be hidden
3196 * 2. App is delayed closing since it might enter PIP.
3197 */
3198 boolean isClosingOrEnteringPip() {
3199 return (isAnimating() && hiddenRequested) || mWillCloseOrEnterPip;
3200 }
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +02003201
3202 /**
3203 * @return Whether we are allowed to show non-starting windows at the moment. We disallow
3204 * showing windows during transitions in case we have windows that have wide-color-gamut
3205 * color mode set to avoid jank in the middle of the transition.
3206 */
3207 boolean canShowWindows() {
3208 return allDrawn && !(isReallyAnimating() && hasNonDefaultColorWindow());
3209 }
3210
3211 /**
3212 * @return true if we have a window that has a non-default color mode set; false otherwise.
3213 */
3214 private boolean hasNonDefaultColorWindow() {
3215 return forAllWindows(ws -> ws.mAttrs.getColorMode() != COLOR_MODE_DEFAULT,
3216 true /* topToBottom */);
3217 }
lumark588a3e82018-07-20 18:53:54 +08003218
3219 void removeFromPendingTransition() {
3220 if (isWaitingForTransitionStart() && mDisplayContent != null) {
3221 mDisplayContent.mOpeningApps.remove(this);
Evan Rosky25b56192019-02-06 16:10:56 -08003222 if (mDisplayContent.mChangingApps.remove(this)) {
3223 clearChangeLeash(getPendingTransaction(), true /* cancel */);
3224 }
lumark588a3e82018-07-20 18:53:54 +08003225 mDisplayContent.mClosingApps.remove(this);
3226 }
3227 }
chaviwdcf76ec2019-01-11 16:48:46 -08003228
3229 private void updateColorTransform() {
3230 if (mSurfaceControl != null && mLastAppSaturationInfo != null) {
Tiger Huanged6794e2019-05-07 20:07:59 +08003231 getPendingTransaction().setColorTransform(mSurfaceControl,
3232 mLastAppSaturationInfo.mMatrix, mLastAppSaturationInfo.mTranslation);
chaviwdcf76ec2019-01-11 16:48:46 -08003233 mWmService.scheduleAnimationLocked();
3234 }
3235 }
3236
3237 private static class AppSaturationInfo {
3238 float[] mMatrix = new float[9];
3239 float[] mTranslation = new float[3];
3240
3241 void setSaturation(@Size(9) float[] matrix, @Size(3) float[] translation) {
3242 System.arraycopy(matrix, 0, mMatrix, 0, mMatrix.length);
3243 System.arraycopy(translation, 0, mTranslation, 0, mTranslation.length);
3244 }
3245 }
Jeff Browne9bdb312012-04-05 15:30:10 -07003246}