blob: 908379701fd2ab258a955390d9fb33a6f3f5f527 [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
Winson Chunge55c0192017-08-24 14:50:48 -070019import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
20import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
Wale Ogunwale72919d22016-12-08 18:58:50 -080021import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
22import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020025import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070026import static android.view.Display.DEFAULT_DISPLAY;
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;
Jorim Jaggife762342016-10-13 14:33:27 +020029import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Wale Ogunwaled1c37912016-08-16 03:19:39 -070030import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080031import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070032import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Tony Mak64b8d562017-12-28 17:44:02 +000033
Adrian Roose99bc052017-11-20 17:55:31 +010034import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
35import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010036import static android.view.WindowManager.TRANSIT_UNSET;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080037import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080038import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
39import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070040import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070041import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
42import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwale9017ec02016-02-25 08:55:25 -080043import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
Jorim Jaggie4b0f282017-05-17 15:10:29 +020044import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW_VERBOSE;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070045import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080046import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
47import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
48import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
49import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Chong Zhang92147042016-05-09 12:47:11 -070050import static com.android.server.wm.WindowManagerService.H.NOTIFY_ACTIVITY_DRAWN;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070051import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_NORMAL;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070052import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070053import static com.android.server.wm.WindowManagerService.logWithStack;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070054import static com.android.server.wm.AppWindowTokenProto.ALL_DRAWN;
55import static com.android.server.wm.AppWindowTokenProto.APP_STOPPED;
56import static com.android.server.wm.AppWindowTokenProto.CLIENT_HIDDEN;
57import static com.android.server.wm.AppWindowTokenProto.DEFER_HIDING_CLIENT;
58import static com.android.server.wm.AppWindowTokenProto.FILLS_PARENT;
59import static com.android.server.wm.AppWindowTokenProto.FROZEN_BOUNDS;
60import static com.android.server.wm.AppWindowTokenProto.HIDDEN_REQUESTED;
61import static com.android.server.wm.AppWindowTokenProto.HIDDEN_SET_FROM_TRANSFERRED_STARTING_WINDOW;
62import static com.android.server.wm.AppWindowTokenProto.IS_REALLY_ANIMATING;
63import static com.android.server.wm.AppWindowTokenProto.IS_WAITING_FOR_TRANSITION_START;
64import static com.android.server.wm.AppWindowTokenProto.LAST_ALL_DRAWN;
65import static com.android.server.wm.AppWindowTokenProto.LAST_SURFACE_SHOWING;
66import static com.android.server.wm.AppWindowTokenProto.NAME;
67import static com.android.server.wm.AppWindowTokenProto.NUM_DRAWN_WINDOWS;
68import static com.android.server.wm.AppWindowTokenProto.NUM_INTERESTING_WINDOWS;
69import static com.android.server.wm.AppWindowTokenProto.REMOVED;
70import static com.android.server.wm.AppWindowTokenProto.REPORTED_DRAWN;
71import static com.android.server.wm.AppWindowTokenProto.REPORTED_VISIBLE;
72import static com.android.server.wm.AppWindowTokenProto.STARTING_DISPLAYED;
73import static com.android.server.wm.AppWindowTokenProto.STARTING_MOVED;
74import static com.android.server.wm.AppWindowTokenProto.STARTING_WINDOW;
75import static com.android.server.wm.AppWindowTokenProto.THUMBNAIL;
76import static com.android.server.wm.AppWindowTokenProto.WINDOW_TOKEN;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080077
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070078import android.annotation.CallSuper;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020079import android.app.Activity;
Jorim Jaggi26c8c422016-05-09 19:57:25 -070080import android.content.res.Configuration;
Jorim Jaggi988f6682017-11-17 17:46:43 +010081import android.graphics.GraphicBuffer;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020082import android.graphics.Point;
Jorim Jaggi0429f352015-12-22 16:29:16 +010083import android.graphics.Rect;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070084import android.os.Binder;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080085import android.os.Debug;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070086import android.os.IBinder;
Steven Timotiusaf03df62017-07-18 16:56:43 -070087import android.os.RemoteException;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020088import android.os.Trace;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080089import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -070090import android.util.proto.ProtoOutputStream;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020091import android.view.DisplayInfo;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080092import android.view.IApplicationToken;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010093import android.view.RemoteAnimationDefinition;
Robert Carr6914f082017-03-20 19:04:30 -070094import android.view.SurfaceControl;
Tony Mak64b8d562017-12-28 17:44:02 +000095import android.view.SurfaceControl.Transaction;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080096import android.view.WindowManager;
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +020097import android.view.WindowManager.LayoutParams;
Tony Mak64b8d562017-12-28 17:44:02 +000098import android.view.animation.Animation;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080099
Tony Mak64b8d562017-12-28 17:44:02 +0000100import com.android.internal.R;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800101import com.android.internal.util.ToBooleanFunction;
102import com.android.server.input.InputApplicationHandle;
Adrian Roose99bc052017-11-20 17:55:31 +0100103import com.android.server.policy.WindowManagerPolicy.StartingSurface;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800104import com.android.server.wm.WindowManagerService.H;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800105
106import java.io.PrintWriter;
Jorim Jaggi0429f352015-12-22 16:29:16 +0100107import java.util.ArrayDeque;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800108import java.util.ArrayList;
109
110class AppTokenList extends ArrayList<AppWindowToken> {
111}
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800112
113/**
114 * Version of WindowToken that is specifically for a particular application (or
115 * really activity) that is displaying windows.
116 */
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700117class AppWindowToken extends WindowToken implements WindowManagerService.AppFreezeListener {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800118 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowToken" : TAG_WM;
119
Jorim Jaggi619c9f72017-12-19 18:04:29 +0100120 /**
121 * Value to increment the z-layer when boosting a layer during animations. BOOST in l33tsp34k.
122 */
123 private static final int Z_BOOST_BASE = 800570000;
124
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800125 // Non-null only for application tokens.
126 final IApplicationToken appToken;
127
Wale Ogunwale72919d22016-12-08 18:58:50 -0800128 final boolean mVoiceInteraction;
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700129
Wale Ogunwale51362492016-09-08 17:49:17 -0700130 /** @see WindowContainer#fillsParent() */
131 private boolean mFillsParent;
Craig Mautner4c5eb222013-11-18 12:59:05 -0800132 boolean layoutConfigChanges;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800133 boolean mShowForAllUsers;
134 int mTargetSdk;
Craig Mautnera2c77052012-03-26 12:14:43 -0700135
Bryce Lee6d410262017-02-28 15:30:17 -0800136 // Flag set while reparenting to prevent actions normally triggered by an individual parent
137 // change.
138 private boolean mReparenting;
139
Wale Ogunwalee287e192017-04-21 09:30:12 -0700140 // True if we are current in the process of removing this app token from the display
141 private boolean mRemovingFromDisplay = false;
142
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800143 // The input dispatching timeout for this application token in nanoseconds.
Wale Ogunwale72919d22016-12-08 18:58:50 -0800144 long mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800145
146 // These are used for determining when all windows associated with
147 // an activity have been drawn, so they can be made visible together
148 // at the same time.
Craig Mautner764983d2012-03-22 11:37:36 -0700149 // initialize so that it doesn't match mTransactionSequence which is an int.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700150 private long mLastTransactionSequence = Long.MIN_VALUE;
151 private int mNumInterestingWindows;
152 private int mNumDrawnWindows;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800153 boolean inPendingTransaction;
154 boolean allDrawn;
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000155 private boolean mLastAllDrawn;
156
Craig Mautner7636dfb2012-11-16 15:24:11 -0800157 // Set to true when this app creates a surface while in the middle of an animation. In that
158 // case do not clear allDrawn until the animation completes.
159 boolean deferClearAllDrawn;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800160
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800161 // Is this window's surface needed? This is almost like hidden, except
162 // it will sometimes be true a little earlier: when the token has
163 // been shown, but is still waiting for its app transition to execute
164 // before making its windows shown.
165 boolean hiddenRequested;
166
167 // Have we told the window clients to hide themselves?
Wale Ogunwale89973222017-04-23 18:39:45 -0700168 private boolean mClientHidden;
169
170 // If true we will defer setting mClientHidden to true and reporting to the client that it is
171 // hidden.
172 boolean mDeferHidingClient;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800173
174 // Last visibility state we reported to the app token.
175 boolean reportedVisible;
176
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700177 // Last drawn state we reported to the app token.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700178 private boolean reportedDrawn;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700179
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800180 // Set to true when the token has been removed from the window mgr.
181 boolean removed;
182
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800183 // Information about an application starting window if displayed.
184 StartingData startingData;
185 WindowState startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800186 StartingSurface startingSurface;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800187 boolean startingDisplayed;
188 boolean startingMoved;
Jorim Jaggi60f9c972018-02-01 19:21:07 +0100189
Wale Ogunwale6c459212017-05-17 08:56:03 -0700190 // True if the hidden state of this token was forced to false due to a transferred starting
191 // window.
192 private boolean mHiddenSetFromTransferredStartingWindow;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800193 boolean firstWindowDrawn;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700194 private final WindowState.UpdateReportedVisibilityResults mReportedVisibilityResults =
195 new WindowState.UpdateReportedVisibilityResults();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800196
197 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700198 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800199
Wale Ogunwale571771c2016-08-26 13:18:50 -0700200 // TODO: Have a WindowContainer state for tracking exiting/deferred removal.
Craig Mautner799bc1d2015-01-14 10:33:48 -0800201 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800202
Craig Mautnerbb742462014-07-07 15:28:55 -0700203 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700204 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700205
Wale Ogunwale72919d22016-12-08 18:58:50 -0800206 private boolean mAlwaysFocusable;
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800207
Robert Carre12aece2016-02-02 22:43:27 -0800208 boolean mAppStopped;
Robert Carrfd10cd12016-06-29 16:41:50 -0700209 int mRotationAnimationHint;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700210 private int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800211
Jorim Jaggife762342016-10-13 14:33:27 +0200212 private boolean mLastContainsShowWhenLockedWindow;
213 private boolean mLastContainsDismissKeyguardWindow;
214
Jorim Jaggi0429f352015-12-22 16:29:16 +0100215 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700216 ArrayDeque<Configuration> mFrozenMergedConfig = new ArrayDeque<>();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100217
Wale Ogunwale6c459212017-05-17 08:56:03 -0700218 private boolean mDisablePreviewScreenshots;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100219
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700220 private Task mLastParent;
Robert Carred3e83b2017-04-21 13:26:55 -0700221
chaviwd3bf08d2017-08-01 17:24:59 -0700222 /**
223 * See {@link #canTurnScreenOn()}
224 */
225 private boolean mCanTurnScreenOn = true;
226
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200227 /**
228 * If we are running an animation, this determines the transition type. Must be one of
229 * AppTransition.TRANSIT_* constants.
230 */
231 private int mTransit;
232
233 /**
234 * If we are running an animation, this determines the flags during this animation. Must be a
235 * bitwise combination of AppTransition.TRANSIT_FLAG_* constants.
236 */
237 private int mTransitFlags;
238
239 /** Whether our surface was set to be showing in the last call to {@link #prepareSurfaces} */
Jorim Jaggifd1891462017-12-29 15:41:36 +0100240 private boolean mLastSurfaceShowing = true;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200241
Jorim Jaggi988f6682017-11-17 17:46:43 +0100242 private AppWindowThumbnail mThumbnail;
243
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000244 /** Have we been asked to have this token keep the screen frozen? */
245 private boolean mFreezingScreen;
246
247 /** Whether this token should be boosted at the top of all app window tokens. */
248 private boolean mNeedsZBoost;
Adrian Roos4d18a2e2017-12-19 19:08:05 +0100249 private Letterbox mLetterbox;
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000250
chaviw23ee71c2017-12-18 11:29:41 -0800251 private final Point mTmpPoint = new Point();
chaviw23012112017-12-20 15:29:04 -0800252 private final Rect mTmpRect = new Rect();
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100253 private RemoteAnimationDefinition mRemoteAnimationDefinition;
Jorim Jaggi6de61012018-03-19 14:53:23 +0100254 private AnimatingAppWindowTokenRegistry mAnimatingAppWindowTokenRegistry;
chaviw23ee71c2017-12-18 11:29:41 -0800255
Wale Ogunwale72919d22016-12-08 18:58:50 -0800256 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
257 DisplayContent dc, long inputDispatchingTimeoutNanos, boolean fullscreen,
258 boolean showForAllUsers, int targetSdk, int orientation, int rotationAnimationHint,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800259 int configChanges, boolean launchTaskBehind, boolean alwaysFocusable,
Bryce Leef3c6a472017-11-14 14:53:06 -0800260 AppWindowContainerController controller) {
261 this(service, token, voiceInteraction, dc, fullscreen);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800262 setController(controller);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800263 mInputDispatchingTimeoutNanos = inputDispatchingTimeoutNanos;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800264 mShowForAllUsers = showForAllUsers;
265 mTargetSdk = targetSdk;
266 mOrientation = orientation;
267 layoutConfigChanges = (configChanges & (CONFIG_SCREEN_SIZE | CONFIG_ORIENTATION)) != 0;
268 mLaunchTaskBehind = launchTaskBehind;
269 mAlwaysFocusable = alwaysFocusable;
270 mRotationAnimationHint = rotationAnimationHint;
271
272 // Application tokens start out hidden.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200273 setHidden(true);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800274 hiddenRequested = true;
275 }
276
277 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
Bryce Leef3c6a472017-11-14 14:53:06 -0800278 DisplayContent dc, boolean fillsParent) {
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800279 super(service, token != null ? token.asBinder() : null, TYPE_APPLICATION, true, dc,
280 false /* ownerCanManageAppTokens */);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700281 appToken = token;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800282 mVoiceInteraction = voiceInteraction;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800283 mFillsParent = fillsParent;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800284 mInputApplicationHandle = new InputApplicationHandle(this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800285 }
286
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800287 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
288 firstWindowDrawn = true;
289
290 // We now have a good window to show, remove dead placeholders
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700291 removeDeadWindows();
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800292
Jorim Jaggi02886a82016-12-06 09:10:06 -0800293 if (startingWindow != null) {
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800294 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
295 + win.mToken + ": first real window is shown, no animation");
296 // If this initial window is animating, stop it -- we will do an animation to reveal
297 // it from behind the starting window, so there is no need for it to also be doing its
298 // own stuff.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100299 win.cancelAnimation();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800300 if (getController() != null) {
301 getController().removeStartingWindow();
302 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800303 }
304 updateReportedVisibilityLocked();
305 }
306
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800307 void updateReportedVisibilityLocked() {
308 if (appToken == null) {
309 return;
310 }
311
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700312 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700313 final int count = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800314
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700315 mReportedVisibilityResults.reset();
316
317 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700318 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700319 win.updateReportedVisibility(mReportedVisibilityResults);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800320 }
321
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700322 int numInteresting = mReportedVisibilityResults.numInteresting;
323 int numVisible = mReportedVisibilityResults.numVisible;
324 int numDrawn = mReportedVisibilityResults.numDrawn;
325 boolean nowGone = mReportedVisibilityResults.nowGone;
326
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700327 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200328 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting && !isHidden();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700329 if (!nowGone) {
330 // If the app is not yet gone, then it can only become visible/drawn.
331 if (!nowDrawn) {
332 nowDrawn = reportedDrawn;
333 }
334 if (!nowVisible) {
335 nowVisible = reportedVisible;
336 }
337 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800338 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800339 + numInteresting + " visible=" + numVisible);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800340 final AppWindowContainerController controller = getController();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700341 if (nowDrawn != reportedDrawn) {
342 if (nowDrawn) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800343 if (controller != null) {
344 controller.reportWindowsDrawn();
345 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700346 }
347 reportedDrawn = nowDrawn;
348 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800349 if (nowVisible != reportedVisible) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700350 if (DEBUG_VISIBILITY) Slog.v(TAG,
351 "Visibility changed in " + this + ": vis=" + nowVisible);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800352 reportedVisible = nowVisible;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800353 if (controller != null) {
354 if (nowVisible) {
355 controller.reportWindowsVisible();
356 } else {
357 controller.reportWindowsGone();
358 }
359 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800360 }
361 }
362
Wale Ogunwale89973222017-04-23 18:39:45 -0700363 boolean isClientHidden() {
364 return mClientHidden;
365 }
366
367 void setClientHidden(boolean hideClient) {
368 if (mClientHidden == hideClient || (hideClient && mDeferHidingClient)) {
369 return;
370 }
Jorim Jaggi067b5bf2018-02-23 17:42:39 +0100371 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "setClientHidden: " + this
372 + " clientHidden=" + hideClient + " Callers=" + Debug.getCallers(5));
Wale Ogunwale89973222017-04-23 18:39:45 -0700373 mClientHidden = hideClient;
374 sendAppVisibilityToClients();
375 }
376
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700377 boolean setVisibility(WindowManager.LayoutParams lp,
378 boolean visible, int transit, boolean performLayout, boolean isVoiceInteraction) {
379
380 boolean delayed = false;
381 inPendingTransaction = false;
Wale Ogunwale9e4721f2017-05-23 19:37:30 -0700382 // Reset the state of mHiddenSetFromTransferredStartingWindow since visibility is actually
383 // been set by the app now.
384 mHiddenSetFromTransferredStartingWindow = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700385
386 // Allow for state changes and animation to be applied if:
387 // * token is transitioning visibility state
388 // * or the token was marked as hidden and is exiting before we had a chance to play the
389 // transition animation
390 // * or this is an opening app and windows are being replaced.
391 boolean visibilityChanged = false;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200392 if (isHidden() == visible || (isHidden() && mIsExiting) || (visible && waitingForReplacement())) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700393 final AccessibilityController accessibilityController = mService.mAccessibilityController;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700394 boolean changed = false;
395 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200396 "Changing app " + this + " hidden=" + isHidden() + " performLayout=" + performLayout);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700397
398 boolean runningAppAnimation = false;
399
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100400 if (transit != WindowManager.TRANSIT_UNSET) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200401 if (applyAnimationLocked(lp, transit, visible, isVoiceInteraction)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700402 delayed = runningAppAnimation = true;
403 }
404 final WindowState window = findMainWindow();
405 //TODO (multidisplay): Magnification is supported only for the default display.
406 if (window != null && accessibilityController != null
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700407 && getDisplayContent().getDisplayId() == DEFAULT_DISPLAY) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700408 accessibilityController.onAppWindowTransitionLocked(window, transit);
409 }
410 changed = true;
411 }
412
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700413 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700414 for (int i = 0; i < windowsCount; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700415 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700416 changed |= win.onAppVisibilityChanged(visible, runningAppAnimation);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700417 }
418
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200419 setHidden(!visible);
420 hiddenRequested = !visible;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700421 visibilityChanged = true;
422 if (!visible) {
423 stopFreezingScreen(true, true);
424 } else {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700425 // If we are being set visible, and the starting window is not yet displayed,
426 // then make sure it doesn't get displayed.
427 if (startingWindow != null && !startingWindow.isDrawnLw()) {
428 startingWindow.mPolicyVisibility = false;
429 startingWindow.mPolicyVisibilityAfterAnim = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700430 }
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700431
432 // We are becoming visible, so better freeze the screen with the windows that are
433 // getting visible so we also wait for them.
434 forAllWindows(mService::makeWindowFreezingScreenIfNeededLocked, true);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700435 }
436
437 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "setVisibility: " + this
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200438 + ": hidden=" + isHidden() + " hiddenRequested=" + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700439
440 if (changed) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700441 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700442 if (performLayout) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700443 mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700444 false /*updateInputWindows*/);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700445 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700446 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700447 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700448 }
449 }
450
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200451 if (isReallyAnimating()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700452 delayed = true;
Jorim Jaggiab9fcb22018-03-15 23:46:12 +0100453 } else {
454
455 // We aren't animating anything, but exiting windows rely on the animation finished
456 // callback being called in case the AppWindowToken was pretending to be animating,
457 // which we might have done because we were in closing/opening apps list.
458 onAnimationFinished();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700459 }
460
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700461 for (int i = mChildren.size() - 1; i >= 0 && !delayed; i--) {
Jorim Jaggia5e10572017-11-15 14:36:26 +0100462 if ((mChildren.get(i)).isSelfOrChildAnimating()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700463 delayed = true;
464 }
465 }
466
467 if (visibilityChanged) {
468 if (visible && !delayed) {
469 // The token was made immediately visible, there will be no entrance animation.
470 // We need to inform the client the enter animation was finished.
471 mEnteringAnimation = true;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700472 mService.mActivityManagerAppTransitionNotifier.onAppTransitionFinishedLocked(token);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700473 }
Robert Carr61b81112017-07-17 18:08:15 -0700474
Jorim Jaggi110839b2018-01-22 12:49:04 +0100475 // If we're becoming visible, immediately change client visibility as well although it
476 // usually gets changed in AppWindowContainerController.setVisibility already. However,
477 // there seem to be some edge cases where we change our visibility but client visibility
478 // never gets updated.
479 // If we're becoming invisible, update the client visibility if we are not running an
480 // animation. Otherwise, we'll update client visibility in onAnimationFinished.
Jorim Jaggi3cad57f2018-02-20 18:32:01 +0100481 if (visible || !isReallyAnimating()) {
Jorim Jaggi110839b2018-01-22 12:49:04 +0100482 setClientHidden(!visible);
Jorim Jaggi4876b4a2018-01-11 15:43:49 +0100483 }
484
chaviwa953fcf2018-03-01 12:00:39 -0800485 if (!mService.mClosingApps.contains(this) && !mService.mOpeningApps.contains(this)) {
486 // The token is not closing nor opening, so even if there is an animation set, that
487 // doesn't mean that it goes through the normal app transition cycle so we have
488 // to inform the docked controller about visibility change.
489 // TODO(multi-display): notify docked divider on all displays where visibility was
490 // affected.
491 mService.getDefaultDisplayContentLocked().getDockedDividerController()
492 .notifyAppVisibilityChanged();
493
494 // Take the screenshot before possibly hiding the WSA, otherwise the screenshot
495 // will not be taken.
496 mService.mTaskSnapshotController.notifyAppVisibilityChanged(this, visible);
497 }
498
Robert Carre7cc44d2017-03-20 19:04:30 -0700499 // If we are hidden but there is no delay needed we immediately
500 // apply the Surface transaction so that the ActivityManager
Robert Carr61b81112017-07-17 18:08:15 -0700501 // can have some guarantee on the Surface state following
502 // setting the visibility. This captures cases like dismissing
503 // the docked or pinned stack where there is no app transition.
504 //
505 // In the case of a "Null" animation, there will be
506 // no animation but there will still be a transition set.
507 // We still need to delay hiding the surface such that it
508 // can be synchronized with showing the next surface in the transition.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200509 if (isHidden() && !delayed && !mService.mAppTransition.isTransitionSet()) {
Robert Carr6914f082017-03-20 19:04:30 -0700510 SurfaceControl.openTransaction();
511 for (int i = mChildren.size() - 1; i >= 0; i--) {
512 mChildren.get(i).mWinAnimator.hide("immediately hidden");
513 }
514 SurfaceControl.closeTransaction();
515 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700516 }
517
518 return delayed;
519 }
520
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +0200521 /**
522 * @return The to top most child window for which {@link LayoutParams#isFullscreen()} returns
523 * true.
524 */
525 WindowState getTopFullscreenWindow() {
526 for (int i = mChildren.size() - 1; i >= 0; i--) {
527 final WindowState win = mChildren.get(i);
528 if (win != null && win.mAttrs.isFullscreen()) {
529 return win;
530 }
531 }
532 return null;
533 }
534
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800535 WindowState findMainWindow() {
Matthew Ng53e66b22018-01-12 17:13:13 -0800536 return findMainWindow(true);
537 }
538
539 /**
540 * Finds the main window that either has type base application or application starting if
541 * requested.
542 *
543 * @param includeStartingApp Allow to search application-starting windows to also be returned.
544 * @return The main window of type base application or application starting if requested.
545 */
546 WindowState findMainWindow(boolean includeStartingApp) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700547 WindowState candidate = null;
Matthew Ng53e66b22018-01-12 17:13:13 -0800548 for (int j = mChildren.size() - 1; j >= 0; --j) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700549 final WindowState win = mChildren.get(j);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700550 final int type = win.mAttrs.type;
551 // No need to loop through child window as base application and starting types can't be
552 // child windows.
Matthew Ng53e66b22018-01-12 17:13:13 -0800553 if (type == TYPE_BASE_APPLICATION
554 || (includeStartingApp && type == TYPE_APPLICATION_STARTING)) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700555 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900556 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700557 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800558 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700559 candidate = win;
560 } else {
561 return win;
562 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800563 }
564 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700565 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800566 }
567
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800568 boolean windowsAreFocusable() {
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700569 return getWindowConfiguration().canReceiveKeys() || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800570 }
571
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800572 AppWindowContainerController getController() {
573 final WindowContainerController controller = super.getController();
574 return controller != null ? (AppWindowContainerController) controller : null;
575 }
576
Wale Ogunwale571771c2016-08-26 13:18:50 -0700577 @Override
Wale Ogunwale44f21802016-09-02 12:49:48 -0700578 boolean isVisible() {
Wale Ogunwalee471be62016-10-03 07:53:55 -0700579 // If the app token isn't hidden then it is considered visible and there is no need to check
580 // its children windows to see if they are visible.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200581 return !isHidden();
Wale Ogunwale44f21802016-09-02 12:49:48 -0700582 }
583
584 @Override
Wale Ogunwalee287e192017-04-21 09:30:12 -0700585 void removeImmediately() {
586 onRemovedFromDisplay();
587 super.removeImmediately();
588 }
589
590 @Override
Wale Ogunwale571771c2016-08-26 13:18:50 -0700591 void removeIfPossible() {
Craig Mautnere3119b72015-01-20 15:02:36 -0800592 mIsExiting = false;
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800593 removeAllWindowsIfPossible();
Bryce Lee6d410262017-02-28 15:30:17 -0800594 removeImmediately();
Craig Mautnere3119b72015-01-20 15:02:36 -0800595 }
596
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700597 @Override
598 boolean checkCompleteDeferredRemoval() {
599 if (mIsExiting) {
600 removeIfPossible();
601 }
602 return super.checkCompleteDeferredRemoval();
603 }
604
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700605 void onRemovedFromDisplay() {
Wale Ogunwalee287e192017-04-21 09:30:12 -0700606 if (mRemovingFromDisplay) {
607 return;
608 }
609 mRemovingFromDisplay = true;
610
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700611 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app token: " + this);
612
Wale Ogunwale72919d22016-12-08 18:58:50 -0800613 boolean delayed = setVisibility(null, false, TRANSIT_UNSET, true, mVoiceInteraction);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700614
615 mService.mOpeningApps.remove(this);
Jorim Jaggi45ca0542017-05-30 16:16:01 -0700616 mService.mUnknownAppVisibilityController.appRemovedOrHidden(this);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100617 mService.mTaskSnapshotController.onAppRemoved(this);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700618 waitingToShow = false;
619 if (mService.mClosingApps.contains(this)) {
620 delayed = true;
621 } else if (mService.mAppTransition.isTransitionSet()) {
622 mService.mClosingApps.add(this);
623 delayed = true;
624 }
625
626 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app " + this + " delayed=" + delayed
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200627 + " animation=" + getAnimation() + " animating=" + isSelfAnimating());
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700628
629 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM, "removeAppToken: "
630 + this + " delayed=" + delayed + " Callers=" + Debug.getCallers(4));
631
Jorim Jaggi19be6052017-08-03 18:33:43 +0200632 if (startingData != null && getController() != null) {
633 getController().removeStartingWindow();
634 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800635
Winson Chung87e5d552017-04-05 11:49:38 -0700636 // If this window was animating, then we need to ensure that the app transition notifies
637 // that animations have completed in WMS.handleAnimatingStoppedAndTransitionLocked(), so
638 // add to that list now
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200639 if (isSelfAnimating()) {
Winson Chung87e5d552017-04-05 11:49:38 -0700640 mService.mNoAnimationNotifyOnTransitionFinished.add(token);
641 }
642
Wale Ogunwalee287e192017-04-21 09:30:12 -0700643 final TaskStack stack = getStack();
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700644 if (delayed && !isEmpty()) {
645 // set the token aside because it has an active animation to be finished
646 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM,
647 "removeAppToken make exiting: " + this);
Wale Ogunwalee287e192017-04-21 09:30:12 -0700648 if (stack != null) {
649 stack.mExitingAppTokens.add(this);
650 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700651 mIsExiting = true;
652 } else {
653 // Make sure there is no animation running on this token, so any windows associated
654 // with it will be removed as soon as their animations are complete
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200655 cancelAnimation();
Wale Ogunwale3106ae012017-05-16 08:56:37 -0700656 if (stack != null) {
657 stack.mExitingAppTokens.remove(this);
658 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700659 removeIfPossible();
660 }
661
662 removed = true;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700663 stopFreezingScreen(true, true);
Bryce Lee6d410262017-02-28 15:30:17 -0800664
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700665 if (mService.mFocusedApp == this) {
666 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "Removing focused app token:" + this);
667 mService.mFocusedApp = null;
668 mService.updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL, true /*updateInputWindows*/);
669 mService.mInputMonitor.setFocusedAppLw(null);
670 }
671
672 if (!delayed) {
673 updateReportedVisibilityLocked();
674 }
Wale Ogunwalee287e192017-04-21 09:30:12 -0700675
676 mRemovingFromDisplay = false;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700677 }
678
Chong Zhange05bcb12016-07-26 17:47:29 -0700679 void clearAnimatingFlags() {
Chong Zhangb0d26702016-08-12 16:03:29 -0700680 boolean wallpaperMightChange = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700681 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700682 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700683 wallpaperMightChange |= win.clearAnimatingFlags();
Chong Zhange05bcb12016-07-26 17:47:29 -0700684 }
Chong Zhangb0d26702016-08-12 16:03:29 -0700685 if (wallpaperMightChange) {
686 requestUpdateWallpaperIfNeeded();
687 }
Chong Zhange05bcb12016-07-26 17:47:29 -0700688 }
689
Robert Carre12aece2016-02-02 22:43:27 -0800690 void destroySurfaces() {
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700691 destroySurfaces(false /*cleanupOnResume*/);
692 }
693
694 /**
695 * Destroy surfaces which have been marked as eligible by the animator, taking care to ensure
696 * the client has finished with them.
697 *
698 * @param cleanupOnResume whether this is done when app is resumed without fully stopped. If
699 * set to true, destroy only surfaces of removed windows, and clear relevant flags of the
700 * others so that they are ready to be reused. If set to false (common case), destroy all
701 * surfaces that's eligible, if the app is already stopped.
702 */
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700703 private void destroySurfaces(boolean cleanupOnResume) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700704 boolean destroyedSomething = false;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100705
706 // Copying to a different list as multiple children can be removed.
Jorim Jaggi59f3e922018-01-05 15:40:32 +0100707 final ArrayList<WindowState> children = new ArrayList<>(mChildren);
Jorim Jaggia5e10572017-11-15 14:36:26 +0100708 for (int i = children.size() - 1; i >= 0; i--) {
709 final WindowState win = children.get(i);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700710 destroyedSomething |= win.destroySurface(cleanupOnResume, mAppStopped);
Robert Carre12aece2016-02-02 22:43:27 -0800711 }
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700712 if (destroyedSomething) {
713 final DisplayContent dc = getDisplayContent();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700714 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Adrian Roos23df3a32018-03-15 15:41:13 +0100715 updateLetterboxSurface(null);
Robert Carre12aece2016-02-02 22:43:27 -0800716 }
717 }
718
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800719 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700720 * Notify that the app is now resumed, and it was not stopped before, perform a clean
721 * up of the surfaces
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800722 */
Jorim Jaggibae01b12017-04-11 16:29:10 -0700723 void notifyAppResumed(boolean wasStopped) {
Chong Zhangad24f962016-08-25 12:12:33 -0700724 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppResumed: wasStopped=" + wasStopped
Jorim Jaggibae01b12017-04-11 16:29:10 -0700725 + " " + this);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700726 mAppStopped = false;
chaviwd3bf08d2017-08-01 17:24:59 -0700727 // Allow the window to turn the screen on once the app is resumed again.
728 setCanTurnScreenOn(true);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700729 if (!wasStopped) {
730 destroySurfaces(true /*cleanupOnResume*/);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800731 }
Robert Carre12aece2016-02-02 22:43:27 -0800732 }
733
Chong Zhangbef461f2015-10-27 11:38:24 -0700734 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700735 * Notify that the app has stopped, and it is okay to destroy any surfaces which were
736 * keeping alive in case they were still being used.
737 */
738 void notifyAppStopped() {
739 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: " + this);
740 mAppStopped = true;
741 destroySurfaces();
742 // Remove any starting window that was added for this app if they are still around.
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800743 if (getController() != null) {
744 getController().removeStartingWindow();
745 }
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700746 }
747
Chong Zhang92147042016-05-09 12:47:11 -0700748 void clearAllDrawn() {
749 allDrawn = false;
750 deferClearAllDrawn = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700751 }
752
Bryce Lee6d410262017-02-28 15:30:17 -0800753 Task getTask() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800754 return (Task) getParent();
Bryce Lee6d410262017-02-28 15:30:17 -0800755 }
756
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800757 TaskStack getStack() {
758 final Task task = getTask();
759 if (task != null) {
760 return task.mStack;
761 } else {
762 return null;
763 }
764 }
765
Bryce Lee6d410262017-02-28 15:30:17 -0800766 @Override
767 void onParentSet() {
768 super.onParentSet();
769
Robert Carred3e83b2017-04-21 13:26:55 -0700770 final Task task = getTask();
771
Bryce Lee6d410262017-02-28 15:30:17 -0800772 // When the associated task is {@code null}, the {@link AppWindowToken} can no longer
773 // access visual elements like the {@link DisplayContent}. We must remove any associations
774 // such as animations.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800775 if (!mReparenting) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800776 if (task == null) {
777 // It is possible we have been marked as a closing app earlier. We must remove ourselves
778 // from this list so we do not participate in any future animations.
779 mService.mClosingApps.remove(this);
Robert Carred3e83b2017-04-21 13:26:55 -0700780 } else if (mLastParent != null && mLastParent.mStack != null) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800781 task.mStack.mExitingAppTokens.remove(this);
782 }
Bryce Lee6d410262017-02-28 15:30:17 -0800783 }
Jorim Jaggi6de61012018-03-19 14:53:23 +0100784 final TaskStack stack = getStack();
785
786 // If we reparent, make sure to remove ourselves from the old animation registry.
787 if (mAnimatingAppWindowTokenRegistry != null) {
788 mAnimatingAppWindowTokenRegistry.notifyFinished(this);
789 }
790 mAnimatingAppWindowTokenRegistry = stack != null
791 ? stack.getAnimatingAppWindowTokenRegistry()
792 : null;
793
Robert Carred3e83b2017-04-21 13:26:55 -0700794 mLastParent = task;
Bryce Lee6d410262017-02-28 15:30:17 -0800795 }
796
Wale Ogunwalefa854eb2016-09-20 13:43:52 -0700797 void postWindowRemoveStartingWindowCleanup(WindowState win) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700798 // TODO: Something smells about the code below...Is there a better way?
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700799 if (startingWindow == win) {
800 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Notify removed startingWindow " + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800801 if (getController() != null) {
802 getController().removeStartingWindow();
803 }
Wale Ogunwale6c459212017-05-17 08:56:03 -0700804 } else if (mChildren.size() == 0) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700805 // If this is the last window and we had requested a starting transition window,
806 // well there is no point now.
Jorim Jaggi02886a82016-12-06 09:10:06 -0800807 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Nulling last startingData");
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700808 startingData = null;
Wale Ogunwale6c459212017-05-17 08:56:03 -0700809 if (mHiddenSetFromTransferredStartingWindow) {
810 // We set the hidden state to false for the token from a transferred starting window.
811 // We now reset it back to true since the starting window was the last window in the
812 // token.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200813 setHidden(true);
Wale Ogunwale6c459212017-05-17 08:56:03 -0700814 }
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100815 } else if (mChildren.size() == 1 && startingSurface != null && !isRelaunching()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700816 // If this is the last window except for a starting transition window,
817 // we need to get rid of the starting transition.
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200818 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Last window, removing starting window "
819 + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800820 if (getController() != null) {
821 getController().removeStartingWindow();
822 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700823 }
824 }
825
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700826 void removeDeadWindows() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700827 for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700828 WindowState win = mChildren.get(winNdx);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800829 if (win.mAppDied) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700830 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.w(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700831 "removeDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800832 // Set mDestroying, we don't want any animation or delayed removal here.
833 win.mDestroying = true;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700834 // Also removes child windows.
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700835 win.removeIfPossible();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800836 }
837 }
838 }
839
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700840 boolean hasWindowsAlive() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700841 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700842 // No need to loop through child windows as the answer should be the same as that of the
843 // parent window.
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700844 if (!(mChildren.get(i)).mAppDied) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700845 return true;
846 }
847 }
848 return false;
849 }
850
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700851 void setWillReplaceWindows(boolean animate) {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700852 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
853 "Marking app token " + this + " with replacing windows.");
Robert Carra1eb4392015-12-10 12:43:51 -0800854
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700855 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700856 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700857 w.setWillReplaceWindow(animate);
Robert Carra1eb4392015-12-10 12:43:51 -0800858 }
Robert Carra1eb4392015-12-10 12:43:51 -0800859 }
860
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700861 void setWillReplaceChildWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700862 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + this
Robert Carr23fa16b2016-01-13 13:19:58 -0800863 + " with replacing child windows.");
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700864 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700865 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700866 w.setWillReplaceChildWindows();
Robert Carr23fa16b2016-01-13 13:19:58 -0800867 }
868 }
869
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700870 void clearWillReplaceWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700871 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
872 "Resetting app token " + this + " of replacing window marks.");
Chong Zhangf596cd52016-01-05 13:42:44 -0800873
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700874 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700875 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700876 w.clearWillReplaceWindow();
Chong Zhangf596cd52016-01-05 13:42:44 -0800877 }
878 }
879
Chong Zhang4d7369a2016-04-25 16:09:14 -0700880 void requestUpdateWallpaperIfNeeded() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700881 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700882 final WindowState w = mChildren.get(i);
Chong Zhang4d7369a2016-04-25 16:09:14 -0700883 w.requestUpdateWallpaperIfNeeded();
884 }
885 }
886
Chong Zhangd78ddb42016-03-02 17:01:14 -0800887 boolean isRelaunching() {
888 return mPendingRelaunchCount > 0;
889 }
890
Robert Carr68375192017-06-13 12:41:53 -0700891 boolean shouldFreezeBounds() {
892 final Task task = getTask();
893
894 // For freeform windows, we can't freeze the bounds at the moment because this would make
895 // the resizing unresponsive.
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700896 if (task == null || task.inFreeformWindowingMode()) {
Robert Carr68375192017-06-13 12:41:53 -0700897 return false;
898 }
899
900 // We freeze the bounds while drag resizing to deal with the time between
901 // the divider/drag handle being released, and the handling it's new
902 // configuration. If we are relaunched outside of the drag resizing state,
903 // we need to be careful not to do this.
904 return getTask().isDragResizing();
905 }
906
Chong Zhangd78ddb42016-03-02 17:01:14 -0800907 void startRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700908 if (shouldFreezeBounds()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -0800909 freezeBounds();
910 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800911
912 // In the process of tearing down before relaunching, the app will
913 // try and clean up it's child surfaces. We need to prevent this from
914 // happening, so we sever the children, transfering their ownership
915 // from the client it-self to the parent surface (owned by us).
Robert Carr29daa922018-04-27 11:56:48 -0700916 detachChildren();
917
918 mPendingRelaunchCount++;
919 }
920
921 void detachChildren() {
Robert Carrfd8e93b2018-05-10 13:40:25 -0700922 SurfaceControl.openTransaction();
Robert Carrd5c7dd62017-03-08 10:39:30 -0800923 for (int i = mChildren.size() - 1; i >= 0; i--) {
924 final WindowState w = mChildren.get(i);
925 w.mWinAnimator.detachChildren();
926 }
Robert Carrfd8e93b2018-05-10 13:40:25 -0700927 SurfaceControl.closeTransaction();
Chong Zhangd78ddb42016-03-02 17:01:14 -0800928 }
929
930 void finishRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700931 unfreezeBounds();
932
Chong Zhangd78ddb42016-03-02 17:01:14 -0800933 if (mPendingRelaunchCount > 0) {
934 mPendingRelaunchCount--;
Bryce Lee081554b2017-05-25 07:52:12 -0700935 } else {
936 // Update keyguard flags upon finishing relaunch.
937 checkKeyguardFlagsChanged();
Chong Zhangd78ddb42016-03-02 17:01:14 -0800938 }
939 }
940
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700941 void clearRelaunching() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700942 if (mPendingRelaunchCount == 0) {
943 return;
944 }
Robert Carr68375192017-06-13 12:41:53 -0700945 unfreezeBounds();
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700946 mPendingRelaunchCount = 0;
947 }
948
Wale Ogunwale07bcab72016-10-14 15:30:09 -0700949 /**
950 * Returns true if the new child window we are adding to this token is considered greater than
951 * the existing child window in this token in terms of z-order.
952 */
953 @Override
954 protected boolean isFirstChildWindowGreaterThanSecond(WindowState newWindow,
955 WindowState existingWindow) {
956 final int type1 = newWindow.mAttrs.type;
957 final int type2 = existingWindow.mAttrs.type;
958
959 // Base application windows should be z-ordered BELOW all other windows in the app token.
960 if (type1 == TYPE_BASE_APPLICATION && type2 != TYPE_BASE_APPLICATION) {
961 return false;
962 } else if (type1 != TYPE_BASE_APPLICATION && type2 == TYPE_BASE_APPLICATION) {
963 return true;
964 }
965
966 // Starting windows should be z-ordered ABOVE all other windows in the app token.
967 if (type1 == TYPE_APPLICATION_STARTING && type2 != TYPE_APPLICATION_STARTING) {
968 return true;
969 } else if (type1 != TYPE_APPLICATION_STARTING && type2 == TYPE_APPLICATION_STARTING) {
970 return false;
971 }
972
973 // Otherwise the new window is greater than the existing window.
974 return true;
975 }
976
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700977 @Override
Robert Carra1eb4392015-12-10 12:43:51 -0800978 void addWindow(WindowState w) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700979 super.addWindow(w);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700980
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700981 boolean gotReplacementWindow = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700982 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700983 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700984 gotReplacementWindow |= candidate.setReplacementWindowIfNeeded(w);
985 }
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700986
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700987 // if we got a replacement window, reset the timeout to give drawing more time
988 if (gotReplacementWindow) {
989 mService.scheduleWindowReplacementTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -0800990 }
Jorim Jaggife762342016-10-13 14:33:27 +0200991 checkKeyguardFlagsChanged();
992 }
993
994 @Override
995 void removeChild(WindowState child) {
996 super.removeChild(child);
997 checkKeyguardFlagsChanged();
Adrian Roos23df3a32018-03-15 15:41:13 +0100998 updateLetterboxSurface(child);
Robert Carra1eb4392015-12-10 12:43:51 -0800999 }
1000
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001001 private boolean waitingForReplacement() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001002 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001003 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001004 if (candidate.waitingForReplacement()) {
Robert Carra1eb4392015-12-10 12:43:51 -08001005 return true;
1006 }
1007 }
1008 return false;
1009 }
1010
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001011 void onWindowReplacementTimeout() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001012 for (int i = mChildren.size() - 1; i >= 0; --i) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001013 (mChildren.get(i)).onWindowReplacementTimeout();
Robert Carra1eb4392015-12-10 12:43:51 -08001014 }
1015 }
1016
Winson Chung30480042017-01-26 10:55:34 -08001017 void reparent(Task task, int position) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001018 final Task currentTask = getTask();
1019 if (task == currentTask) {
Winson Chung30480042017-01-26 10:55:34 -08001020 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001021 "window token=" + this + " already child of task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -08001022 }
Bryce Lee6d410262017-02-28 15:30:17 -08001023
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001024 if (currentTask.mStack != task.mStack) {
Bryce Lee6d410262017-02-28 15:30:17 -08001025 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001026 "window token=" + this + " current task=" + currentTask
Bryce Lee6d410262017-02-28 15:30:17 -08001027 + " belongs to a different stack than " + task);
1028 }
1029
Winson Chung30480042017-01-26 10:55:34 -08001030 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "reParentWindowToken: removing window token=" + this
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001031 + " from task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -08001032 final DisplayContent prevDisplayContent = getDisplayContent();
1033
Bryce Lee6d410262017-02-28 15:30:17 -08001034 mReparenting = true;
1035
Winson Chung30480042017-01-26 10:55:34 -08001036 getParent().removeChild(this);
1037 task.addChild(this, position);
1038
Bryce Lee6d410262017-02-28 15:30:17 -08001039 mReparenting = false;
1040
Winson Chung30480042017-01-26 10:55:34 -08001041 // Relayout display(s).
1042 final DisplayContent displayContent = task.getDisplayContent();
1043 displayContent.setLayoutNeeded();
1044 if (prevDisplayContent != displayContent) {
1045 onDisplayChanged(displayContent);
1046 prevDisplayContent.setLayoutNeeded();
1047 }
1048 }
1049
Jorim Jaggi0429f352015-12-22 16:29:16 +01001050 /**
1051 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
1052 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
1053 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
1054 * with a queue.
1055 */
Chong Zhangd78ddb42016-03-02 17:01:14 -08001056 private void freezeBounds() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001057 final Task task = getTask();
1058 mFrozenBounds.offer(new Rect(task.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001059
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001060 if (task.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001061 // We didn't call prepareFreezingBounds on the task, so use the current value.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001062 mFrozenMergedConfig.offer(new Configuration(task.getConfiguration()));
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001063 } else {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001064 mFrozenMergedConfig.offer(new Configuration(task.mPreparedFrozenMergedConfig));
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001065 }
Andrii Kulianb10330d2016-09-16 13:51:46 -07001066 // Calling unset() to make it equal to Configuration.EMPTY.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001067 task.mPreparedFrozenMergedConfig.unset();
Jorim Jaggi0429f352015-12-22 16:29:16 +01001068 }
1069
1070 /**
1071 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
1072 */
Chong Zhangd78ddb42016-03-02 17:01:14 -08001073 private void unfreezeBounds() {
Robert Carr68375192017-06-13 12:41:53 -07001074 if (mFrozenBounds.isEmpty()) {
1075 return;
Wale Ogunwale37dbafc2016-06-27 10:15:20 -07001076 }
Robert Carr68375192017-06-13 12:41:53 -07001077 mFrozenBounds.remove();
Wale Ogunwale37dbafc2016-06-27 10:15:20 -07001078 if (!mFrozenMergedConfig.isEmpty()) {
1079 mFrozenMergedConfig.remove();
1080 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001081 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001082 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001083 win.onUnfreezeBounds();
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001084 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001085 mService.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +01001086 }
1087
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001088 void setAppLayoutChanges(int changes, String reason) {
1089 if (!mChildren.isEmpty()) {
1090 final DisplayContent dc = getDisplayContent();
1091 dc.pendingLayoutChanges |= changes;
1092 if (DEBUG_LAYOUT_REPEATS) {
1093 mService.mWindowPlacerLocked.debugLayoutRepeats(reason, dc.pendingLayoutChanges);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001094 }
1095 }
1096 }
1097
1098 void removeReplacedWindowIfNeeded(WindowState replacement) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001099 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001100 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001101 if (win.removeReplacedWindowIfNeeded(replacement)) {
1102 return;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001103 }
1104 }
1105 }
1106
1107 void startFreezingScreen() {
1108 if (DEBUG_ORIENTATION) logWithStack(TAG, "Set freezing of " + appToken + ": hidden="
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001109 + isHidden() + " freezing=" + mFreezingScreen + " hiddenRequested="
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001110 + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001111 if (!hiddenRequested) {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001112 if (!mFreezingScreen) {
1113 mFreezingScreen = true;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001114 mService.registerAppFreezeListener(this);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001115 mService.mAppsFreezingScreen++;
1116 if (mService.mAppsFreezingScreen == 1) {
Robert Carrae606b42018-02-15 15:36:23 -08001117 mService.startFreezingDisplayLocked(0, 0, getDisplayContent());
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001118 mService.mH.removeMessages(H.APP_FREEZE_TIMEOUT);
1119 mService.mH.sendEmptyMessageDelayed(H.APP_FREEZE_TIMEOUT, 2000);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001120 }
1121 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001122 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001123 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001124 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001125 w.onStartFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001126 }
1127 }
1128 }
1129
1130 void stopFreezingScreen(boolean unfreezeSurfaceNow, boolean force) {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001131 if (!mFreezingScreen) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001132 return;
1133 }
1134 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Clear freezing of " + this + " force=" + force);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001135 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001136 boolean unfrozeWindows = false;
1137 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001138 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001139 unfrozeWindows |= w.onStopFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001140 }
1141 if (force || unfrozeWindows) {
1142 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "No longer freezing: " + this);
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001143 mFreezingScreen = false;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001144 mService.unregisterAppFreezeListener(this);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001145 mService.mAppsFreezingScreen--;
1146 mService.mLastFinishedFreezeSource = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001147 }
1148 if (unfreezeSurfaceNow) {
1149 if (unfrozeWindows) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001150 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001151 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001152 mService.stopFreezingDisplayLocked();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001153 }
1154 }
1155
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001156 @Override
1157 public void onAppFreezeTimeout() {
1158 Slog.w(TAG_WM, "Force clearing freeze: " + this);
1159 stopFreezingScreen(true, true);
1160 }
1161
Jorim Jaggi60f9c972018-02-01 19:21:07 +01001162 /**
1163 * Tries to transfer the starting window from a token that's above ourselves in the task but
1164 * not visible anymore. This is a common scenario apps use: Trampoline activity T start main
1165 * activity M in the same task. Now, when reopening the task, T starts on top of M but then
1166 * immediately finishes after, so we have to transfer T to M.
1167 */
1168 void transferStartingWindowFromHiddenAboveTokenIfNeeded() {
1169 final Task task = getTask();
1170 for (int i = task.mChildren.size() - 1; i >= 0; i--) {
1171 final AppWindowToken fromToken = task.mChildren.get(i);
1172 if (fromToken == this) {
1173 return;
1174 }
1175 if (fromToken.hiddenRequested && transferStartingWindow(fromToken.token)) {
1176 return;
1177 }
1178 }
1179 }
1180
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001181 boolean transferStartingWindow(IBinder transferFrom) {
Wale Ogunwale02319a62016-09-26 15:21:22 -07001182 final AppWindowToken fromToken = getDisplayContent().getAppWindowToken(transferFrom);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001183 if (fromToken == null) {
1184 return false;
1185 }
1186
1187 final WindowState tStartingWindow = fromToken.startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001188 if (tStartingWindow != null && fromToken.startingSurface != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001189 // In this case, the starting icon has already been displayed, so start
1190 // letting windows get shown immediately without any more transitions.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001191 mService.mSkipAppTransitionAnimation = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001192
1193 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Moving existing starting " + tStartingWindow
1194 + " from " + fromToken + " to " + this);
1195
1196 final long origId = Binder.clearCallingIdentity();
Peter Visontay3556a3b2017-11-01 17:23:17 +00001197 try {
1198 // Transfer the starting window over to the new token.
1199 startingData = fromToken.startingData;
1200 startingSurface = fromToken.startingSurface;
1201 startingDisplayed = fromToken.startingDisplayed;
1202 fromToken.startingDisplayed = false;
1203 startingWindow = tStartingWindow;
1204 reportedVisible = fromToken.reportedVisible;
1205 fromToken.startingData = null;
1206 fromToken.startingSurface = null;
1207 fromToken.startingWindow = null;
1208 fromToken.startingMoved = true;
1209 tStartingWindow.mToken = this;
1210 tStartingWindow.mAppToken = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001211
Peter Visontay3556a3b2017-11-01 17:23:17 +00001212 if (DEBUG_ADD_REMOVE || DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1213 "Removing starting " + tStartingWindow + " from " + fromToken);
1214 fromToken.removeChild(tStartingWindow);
1215 fromToken.postWindowRemoveStartingWindowCleanup(tStartingWindow);
1216 fromToken.mHiddenSetFromTransferredStartingWindow = false;
1217 addWindow(tStartingWindow);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001218
Peter Visontay3556a3b2017-11-01 17:23:17 +00001219 // Propagate other interesting state between the tokens. If the old token is displayed,
1220 // we should immediately force the new one to be displayed. If it is animating, we need
1221 // to move that animation to the new one.
1222 if (fromToken.allDrawn) {
1223 allDrawn = true;
1224 deferClearAllDrawn = fromToken.deferClearAllDrawn;
1225 }
1226 if (fromToken.firstWindowDrawn) {
1227 firstWindowDrawn = true;
1228 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001229 if (!fromToken.isHidden()) {
1230 setHidden(false);
Peter Visontay3556a3b2017-11-01 17:23:17 +00001231 hiddenRequested = false;
1232 mHiddenSetFromTransferredStartingWindow = true;
1233 }
1234 setClientHidden(fromToken.mClientHidden);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001235
Jorim Jaggi980c9de2017-11-17 01:41:37 +01001236 transferAnimation(fromToken);
1237
1238 // When transferring an animation, we no longer need to apply an animation to the
1239 // the token we transfer the animation over. Thus, remove the animation from
1240 // pending opening apps.
1241 mService.mOpeningApps.remove(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001242
Peter Visontay3556a3b2017-11-01 17:23:17 +00001243 mService.updateFocusedWindowLocked(
1244 UPDATE_FOCUS_WILL_PLACE_SURFACES, true /*updateInputWindows*/);
1245 getDisplayContent().setLayoutNeeded();
1246 mService.mWindowPlacerLocked.performSurfacePlacement();
1247 } finally {
1248 Binder.restoreCallingIdentity(origId);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001249 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001250 return true;
1251 } else if (fromToken.startingData != null) {
1252 // The previous app was getting ready to show a
1253 // starting window, but hasn't yet done so. Steal it!
1254 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1255 "Moving pending starting from " + fromToken + " to " + this);
1256 startingData = fromToken.startingData;
1257 fromToken.startingData = null;
1258 fromToken.startingMoved = true;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001259 if (getController() != null) {
1260 getController().scheduleAddStartingWindow();
1261 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001262 return true;
1263 }
1264
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001265 // TODO: Transfer thumbnail
1266
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001267 return false;
1268 }
1269
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001270 boolean isLastWindow(WindowState win) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001271 return mChildren.size() == 1 && mChildren.get(0) == win;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001272 }
1273
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001274 @Override
1275 void onAppTransitionDone() {
1276 sendingToBottom = false;
1277 }
1278
Wale Ogunwale51362492016-09-08 17:49:17 -07001279 /**
1280 * We override because this class doesn't want its children affecting its reported orientation
1281 * in anyway.
1282 */
1283 @Override
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001284 int getOrientation(int candidate) {
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001285 if (candidate == SCREEN_ORIENTATION_BEHIND) {
1286 // Allow app to specify orientation regardless of its visibility state if the current
1287 // candidate want us to use orientation behind. I.e. the visible app on-top of this one
1288 // wants us to use the orientation of the app behind it.
1289 return mOrientation;
1290 }
1291
Bryce Lee61fbcbc2017-03-10 14:14:03 -08001292 // The {@link AppWindowToken} should only specify an orientation when it is not closing or
1293 // going to the bottom. Allowing closing {@link AppWindowToken} to participate can lead to
1294 // an Activity in another task being started in the wrong orientation during the transition.
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001295 if (!(sendingToBottom || mService.mClosingApps.contains(this))
Bryce Lee00d586d2017-07-28 20:48:43 -07001296 && (isVisible() || mService.mOpeningApps.contains(this) || isOnTop())) {
Bryce Leea163b762017-01-24 11:05:01 -08001297 return mOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001298 }
Bryce Leea163b762017-01-24 11:05:01 -08001299
1300 return SCREEN_ORIENTATION_UNSET;
Wale Ogunwale51362492016-09-08 17:49:17 -07001301 }
1302
Wale Ogunwaleb198b742016-12-01 08:44:09 -08001303 /** Returns the app's preferred orientation regardless of its currently visibility state. */
1304 int getOrientationIgnoreVisibility() {
1305 return mOrientation;
1306 }
1307
Craig Mautnerdbb79912012-03-01 18:59:14 -08001308 @Override
Winson Chunge55c0192017-08-24 14:50:48 -07001309 public void onConfigurationChanged(Configuration newParentConfig) {
1310 final int prevWinMode = getWindowingMode();
1311 super.onConfigurationChanged(newParentConfig);
1312 final int winMode = getWindowingMode();
1313
1314 if (prevWinMode == winMode) {
1315 return;
1316 }
1317
1318 if (prevWinMode != WINDOWING_MODE_UNDEFINED && winMode == WINDOWING_MODE_PINNED) {
1319 // Entering PiP from fullscreen, reset the snap fraction
1320 mDisplayContent.mPinnedStackControllerLocked.resetReentrySnapFraction(this);
Winson Chung82267ce2018-04-06 16:38:26 -07001321 } else if (prevWinMode == WINDOWING_MODE_PINNED && winMode != WINDOWING_MODE_UNDEFINED
1322 && !isHidden()) {
Winson Chunge55c0192017-08-24 14:50:48 -07001323 // Leaving PiP to fullscreen, save the snap fraction based on the pre-animation bounds
1324 // for the next re-entry into PiP (assuming the activity is not hidden or destroyed)
1325 final TaskStack pinnedStack = mDisplayContent.getPinnedStack();
1326 if (pinnedStack != null) {
1327 mDisplayContent.mPinnedStackControllerLocked.saveReentrySnapFraction(this,
1328 pinnedStack.mPreAnimationBounds);
1329 }
1330 }
1331 }
1332
1333 @Override
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001334 void checkAppWindowsReadyToShow() {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001335 if (allDrawn == mLastAllDrawn) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001336 return;
1337 }
1338
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001339 mLastAllDrawn = allDrawn;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001340 if (!allDrawn) {
1341 return;
1342 }
1343
1344 // The token has now changed state to having all windows shown... what to do, what to do?
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001345 if (mFreezingScreen) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001346 showAllWindowsLocked();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001347 stopFreezingScreen(false, true);
1348 if (DEBUG_ORIENTATION) Slog.i(TAG,
1349 "Setting mOrientationChangeComplete=true because wtoken " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001350 + " numInteresting=" + mNumInterestingWindows + " numDrawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001351 // This will set mOrientationChangeComplete and cause a pass through layout.
1352 setAppLayoutChanges(FINISH_LAYOUT_REDO_WALLPAPER,
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001353 "checkAppWindowsReadyToShow: freezingScreen");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001354 } else {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001355 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM, "checkAppWindowsReadyToShow");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001356
1357 // We can now show all of the drawn windows!
1358 if (!mService.mOpeningApps.contains(this)) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001359 showAllWindowsLocked();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001360 }
1361 }
1362 }
1363
Matthew Ng5d23afa2017-06-21 16:16:24 -07001364 /**
Bryce Leed390deb2017-06-22 13:14:28 -07001365 * Returns whether the drawn window states of this {@link AppWindowToken} has considered every
1366 * child {@link WindowState}. A child is considered if it has been passed into
1367 * {@link #updateDrawnWindowStates(WindowState)} after being added. This is used to determine
1368 * whether states, such as {@code allDrawn}, can be set, which relies on state variables such as
1369 * {@code mNumInterestingWindows}, which depend on all {@link WindowState}s being considered.
1370 *
1371 * @return {@code true} If all children have been considered, {@code false}.
1372 */
1373 private boolean allDrawnStatesConsidered() {
Bryce Lee6311c4b2017-07-06 14:09:29 -07001374 for (int i = mChildren.size() - 1; i >= 0; --i) {
1375 final WindowState child = mChildren.get(i);
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001376 if (child.mightAffectAllDrawn() && !child.getDrawnStateEvaluated()) {
Bryce Leed390deb2017-06-22 13:14:28 -07001377 return false;
1378 }
1379 }
1380 return true;
1381 }
1382
1383 /**
Matthew Ng5d23afa2017-06-21 16:16:24 -07001384 * Determines if the token has finished drawing. This should only be called from
1385 * {@link DisplayContent#applySurfaceChangesTransaction}
1386 */
Matthew Ng498c71d2017-04-18 13:55:45 -07001387 void updateAllDrawn() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001388 if (!allDrawn) {
Matthew Ng498c71d2017-04-18 13:55:45 -07001389 // Number of drawn windows can be less when a window is being relaunched, wait for
Bryce Leed390deb2017-06-22 13:14:28 -07001390 // all windows to be launched and drawn for this token be considered all drawn.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001391 final int numInteresting = mNumInterestingWindows;
Bryce Leed390deb2017-06-22 13:14:28 -07001392
1393 // We must make sure that all present children have been considered (determined by
1394 // {@link #allDrawnStatesConsidered}) before evaluating whether everything has been
1395 // drawn.
1396 if (numInteresting > 0 && allDrawnStatesConsidered()
1397 && mNumDrawnWindows >= numInteresting && !isRelaunching()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001398 if (DEBUG_VISIBILITY) Slog.v(TAG, "allDrawn: " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001399 + " interesting=" + numInteresting + " drawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001400 allDrawn = true;
1401 // Force an additional layout pass where
1402 // WindowStateAnimator#commitFinishDrawingLocked() will call performShowLocked().
Matthew Ng498c71d2017-04-18 13:55:45 -07001403 if (mDisplayContent != null) {
1404 mDisplayContent.setLayoutNeeded();
1405 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001406 mService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
Robert Carrecc06b32017-04-18 14:25:10 -07001407
Winson Chunge7ba6862017-05-24 12:13:33 -07001408 // Notify the pinned stack upon all windows drawn. If there was an animation in
1409 // progress then this signal will resume that animation.
Wale Ogunwale61911492017-10-11 08:50:50 -07001410 final TaskStack pinnedStack = mDisplayContent.getPinnedStack();
Winson Chunge7ba6862017-05-24 12:13:33 -07001411 if (pinnedStack != null) {
1412 pinnedStack.onAllWindowsDrawn();
Robert Carrecc06b32017-04-18 14:25:10 -07001413 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001414 }
1415 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001416 }
1417
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001418 /**
1419 * Updated this app token tracking states for interesting and drawn windows based on the window.
1420 *
1421 * @return Returns true if the input window is considered interesting and drawn while all the
1422 * windows in this app token where not considered drawn as of the last pass.
1423 */
1424 boolean updateDrawnWindowStates(WindowState w) {
Bryce Leed390deb2017-06-22 13:14:28 -07001425 w.setDrawnStateEvaluated(true /*evaluated*/);
1426
Jorim Jaggie4b0f282017-05-17 15:10:29 +02001427 if (DEBUG_STARTING_WINDOW_VERBOSE && w == startingWindow) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001428 Slog.d(TAG, "updateWindows: starting " + w + " isOnScreen=" + w.isOnScreen()
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001429 + " allDrawn=" + allDrawn + " freezingScreen=" + mFreezingScreen);
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001430 }
1431
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001432 if (allDrawn && !mFreezingScreen) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001433 return false;
1434 }
1435
1436 if (mLastTransactionSequence != mService.mTransactionSequence) {
1437 mLastTransactionSequence = mService.mTransactionSequence;
Matthew Ng53e66b22018-01-12 17:13:13 -08001438 mNumDrawnWindows = 0;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001439 startingDisplayed = false;
Matthew Ng53e66b22018-01-12 17:13:13 -08001440
1441 // There is the main base application window, even if it is exiting, wait for it
1442 mNumInterestingWindows = findMainWindow(false /* includeStartingApp */) != null ? 1 : 0;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001443 }
1444
1445 final WindowStateAnimator winAnimator = w.mWinAnimator;
1446
1447 boolean isInterestingAndDrawn = false;
1448
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001449 if (!allDrawn && w.mightAffectAllDrawn()) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001450 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
1451 Slog.v(TAG, "Eval win " + w + ": isDrawn=" + w.isDrawnLw()
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001452 + ", isAnimationSet=" + isSelfAnimating());
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001453 if (!w.isDrawnLw()) {
1454 Slog.v(TAG, "Not displayed: s=" + winAnimator.mSurfaceController
1455 + " pv=" + w.mPolicyVisibility
1456 + " mDrawState=" + winAnimator.drawStateToString()
1457 + " ph=" + w.isParentWindowHidden() + " th=" + hiddenRequested
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001458 + " a=" + isSelfAnimating());
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001459 }
1460 }
1461
1462 if (w != startingWindow) {
1463 if (w.isInteresting()) {
Matthew Ng53e66b22018-01-12 17:13:13 -08001464 // Add non-main window as interesting since the main app has already been added
1465 if (findMainWindow(false /* includeStartingApp */) != w) {
1466 mNumInterestingWindows++;
1467 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001468 if (w.isDrawnLw()) {
1469 mNumDrawnWindows++;
1470
1471 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG, "tokenMayBeDrawn: "
1472 + this + " w=" + w + " numInteresting=" + mNumInterestingWindows
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001473 + " freezingScreen=" + mFreezingScreen
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001474 + " mAppFreezing=" + w.mAppFreezing);
1475
1476 isInterestingAndDrawn = true;
1477 }
1478 }
1479 } else if (w.isDrawnLw()) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001480 if (getController() != null) {
1481 getController().reportStartingWindowDrawn();
1482 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001483 startingDisplayed = true;
1484 }
1485 }
1486
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001487 return isInterestingAndDrawn;
1488 }
1489
Adrian Roos23df3a32018-03-15 15:41:13 +01001490 void layoutLetterbox(WindowState winHint) {
Adrian Roos4d18a2e2017-12-19 19:08:05 +01001491 final WindowState w = findMainWindow();
Adrian Roosf93b6d22018-03-21 13:48:26 +01001492 if (w == null || winHint != null && w != winHint) {
Adrian Roos4d18a2e2017-12-19 19:08:05 +01001493 return;
1494 }
Jorim Jaggia32da382018-03-28 18:01:22 +02001495 final boolean surfaceReady = w.isDrawnLw() // Regular case
Adrian Roosf93b6d22018-03-21 13:48:26 +01001496 || w.mWinAnimator.mSurfaceDestroyDeferred // The preserved surface is still ready.
1497 || w.isDragResizeChanged(); // Waiting for relayoutWindow to call preserveSurface.
1498 final boolean needsLetterbox = w.isLetterboxedAppWindow() && fillsParent() && surfaceReady;
Adrian Roos4d18a2e2017-12-19 19:08:05 +01001499 if (needsLetterbox) {
1500 if (mLetterbox == null) {
1501 mLetterbox = new Letterbox(() -> makeChildSurface(null));
1502 }
Adrian Roos23df3a32018-03-15 15:41:13 +01001503 mLetterbox.layout(getParent().getBounds(), w.mFrame);
Adrian Roos4d18a2e2017-12-19 19:08:05 +01001504 } else if (mLetterbox != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +01001505 mLetterbox.hide();
1506 }
1507 }
1508
1509 void updateLetterboxSurface(WindowState winHint) {
1510 final WindowState w = findMainWindow();
1511 if (w != winHint && winHint != null && w != null) {
1512 return;
1513 }
1514 layoutLetterbox(winHint);
1515 if (mLetterbox != null && mLetterbox.needsApplySurfaceChanges()) {
1516 mLetterbox.applySurfaceChanges(mPendingTransaction);
Adrian Roos4d18a2e2017-12-19 19:08:05 +01001517 }
1518 }
1519
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001520 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001521 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001522 // For legacy reasons we process the TaskStack.mExitingAppTokens first in DisplayContent
1523 // before the non-exiting app tokens. So, we skip the exiting app tokens here.
1524 // TODO: Investigate if we need to continue to do this or if we can just process them
1525 // in-order.
1526 if (mIsExiting && !waitingForReplacement()) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001527 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001528 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001529 return forAllWindowsUnchecked(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001530 }
1531
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001532 boolean forAllWindowsUnchecked(ToBooleanFunction<WindowState> callback,
1533 boolean traverseTopToBottom) {
1534 return super.forAllWindows(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001535 }
1536
1537 @Override
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001538 AppWindowToken asAppWindowToken() {
1539 // I am an app window token!
1540 return this;
1541 }
1542
1543 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001544 boolean fillsParent() {
1545 return mFillsParent;
1546 }
1547
1548 void setFillsParent(boolean fillsParent) {
1549 mFillsParent = fillsParent;
1550 }
1551
Jorim Jaggife762342016-10-13 14:33:27 +02001552 boolean containsDismissKeyguardWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001553 // Window state is transient during relaunch. We are not guaranteed to be frozen during the
1554 // entirety of the relaunch.
1555 if (isRelaunching()) {
1556 return mLastContainsDismissKeyguardWindow;
1557 }
1558
Jorim Jaggife762342016-10-13 14:33:27 +02001559 for (int i = mChildren.size() - 1; i >= 0; i--) {
1560 if ((mChildren.get(i).mAttrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
1561 return true;
1562 }
1563 }
1564 return false;
1565 }
1566
1567 boolean containsShowWhenLockedWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001568 // When we are relaunching, it is possible for us to be unfrozen before our previous
1569 // windows have been added back. Using the cached value ensures that our previous
1570 // showWhenLocked preference is honored until relaunching is complete.
1571 if (isRelaunching()) {
1572 return mLastContainsShowWhenLockedWindow;
1573 }
1574
Jorim Jaggife762342016-10-13 14:33:27 +02001575 for (int i = mChildren.size() - 1; i >= 0; i--) {
1576 if ((mChildren.get(i).mAttrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
1577 return true;
1578 }
1579 }
Bryce Lee081554b2017-05-25 07:52:12 -07001580
Jorim Jaggife762342016-10-13 14:33:27 +02001581 return false;
1582 }
1583
1584 void checkKeyguardFlagsChanged() {
1585 final boolean containsDismissKeyguard = containsDismissKeyguardWindow();
1586 final boolean containsShowWhenLocked = containsShowWhenLockedWindow();
1587 if (containsDismissKeyguard != mLastContainsDismissKeyguardWindow
1588 || containsShowWhenLocked != mLastContainsShowWhenLockedWindow) {
1589 mService.notifyKeyguardFlagsChanged(null /* callback */);
1590 }
1591 mLastContainsDismissKeyguardWindow = containsDismissKeyguard;
1592 mLastContainsShowWhenLockedWindow = containsShowWhenLocked;
1593 }
1594
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001595 WindowState getImeTargetBelowWindow(WindowState w) {
1596 final int index = mChildren.indexOf(w);
1597 if (index > 0) {
1598 final WindowState target = mChildren.get(index - 1);
1599 if (target.canBeImeTarget()) {
1600 return target;
1601 }
1602 }
1603 return null;
1604 }
1605
Winson Chungbe9be7f2017-06-28 10:55:22 -07001606 int getLowestAnimLayer() {
1607 for (int i = 0; i < mChildren.size(); i++) {
1608 final WindowState w = mChildren.get(i);
1609 if (w.mRemoved) {
1610 continue;
1611 }
1612 return w.mWinAnimator.mAnimLayer;
1613 }
1614 return Integer.MAX_VALUE;
1615 }
1616
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001617 WindowState getHighestAnimLayerWindow(WindowState currentTarget) {
1618 WindowState candidate = null;
1619 for (int i = mChildren.indexOf(currentTarget); i >= 0; i--) {
1620 final WindowState w = mChildren.get(i);
1621 if (w.mRemoved) {
1622 continue;
1623 }
1624 if (candidate == null || w.mWinAnimator.mAnimLayer >
1625 candidate.mWinAnimator.mAnimLayer) {
1626 candidate = w;
1627 }
1628 }
1629 return candidate;
1630 }
1631
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001632 /**
1633 * See {@link Activity#setDisablePreviewScreenshots}.
1634 */
1635 void setDisablePreviewScreenshots(boolean disable) {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001636 mDisablePreviewScreenshots = disable;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001637 }
1638
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001639 /**
chaviwd3bf08d2017-08-01 17:24:59 -07001640 * Sets whether the current launch can turn the screen on. See {@link #canTurnScreenOn()}
1641 */
1642 void setCanTurnScreenOn(boolean canTurnScreenOn) {
1643 mCanTurnScreenOn = canTurnScreenOn;
1644 }
1645
1646 /**
1647 * Indicates whether the current launch can turn the screen on. This is to prevent multiple
1648 * relayouts from turning the screen back on. The screen should only turn on at most
1649 * once per activity resume.
1650 *
1651 * @return true if the screen can be turned on.
1652 */
1653 boolean canTurnScreenOn() {
1654 return mCanTurnScreenOn;
1655 }
1656
1657 /**
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001658 * Retrieves whether we'd like to generate a snapshot that's based solely on the theme. This is
1659 * the case when preview screenshots are disabled {@link #setDisablePreviewScreenshots} or when
1660 * we can't take a snapshot for other reasons, for example, if we have a secure window.
1661 *
1662 * @return True if we need to generate an app theme snapshot, false if we'd like to take a real
1663 * screenshot.
1664 */
1665 boolean shouldUseAppThemeSnapshot() {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001666 return mDisablePreviewScreenshots || forAllWindows(w -> (w.mAttrs.flags & FLAG_SECURE) != 0,
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001667 true /* topToBottom */);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001668 }
1669
Jorim Jaggibe418292018-03-26 16:14:12 +02001670 SurfaceControl getAppAnimationLayer() {
Jorim Jaggi391790622018-04-18 15:30:44 +02001671 return getAppAnimationLayer(isActivityTypeHome() ? ANIMATION_LAYER_HOME
1672 : needsZBoost() ? ANIMATION_LAYER_BOOSTED
1673 : ANIMATION_LAYER_STANDARD);
Jorim Jaggibe418292018-03-26 16:14:12 +02001674 }
1675
chaviw23ee71c2017-12-18 11:29:41 -08001676 @Override
Jorim Jaggi596a1992017-12-29 14:48:02 +01001677 public SurfaceControl getAnimationLeashParent() {
Robert Carrb9506032018-02-13 13:54:00 -08001678 // All normal app transitions take place in an animation layer which is below the pinned
1679 // stack but may be above the parent stacks of the given animating apps.
1680 // For transitions in the pinned stack (menu activity) we just let them occur as a child
1681 // of the pinned stack.
1682 if (!inPinnedWindowingMode()) {
1683 return getAppAnimationLayer();
1684 } else {
1685 return getStack().getSurfaceControl();
1686 }
chaviw23ee71c2017-12-18 11:29:41 -08001687 }
1688
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001689 boolean applyAnimationLocked(WindowManager.LayoutParams lp, int transit, boolean enter,
1690 boolean isVoiceInteraction) {
1691
1692 if (mService.mDisableTransitionAnimation) {
1693 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) {
1694 Slog.v(TAG_WM, "applyAnimation: transition animation is disabled. atoken=" + this);
1695 }
1696 cancelAnimation();
1697 return false;
1698 }
1699
1700 // Only apply an animation if the display isn't frozen. If it is frozen, there is no reason
1701 // to animate and it can cause strange artifacts when we unfreeze the display if some
1702 // different animation is running.
1703 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "AWT#applyAnimationLocked");
1704 if (okToAnimate()) {
Jorim Jaggi33a701a2017-12-01 14:58:18 +01001705 final AnimationAdapter adapter;
1706 final TaskStack stack = getStack();
1707 mTmpPoint.set(0, 0);
1708 mTmpRect.setEmpty();
1709 if (stack != null) {
1710 stack.getRelativePosition(mTmpPoint);
1711 stack.getBounds(mTmpRect);
1712 mTmpRect.offsetTo(0, 0);
1713 }
Jorim Jaggic4d29f22018-03-22 16:30:56 +01001714
1715 // Delaying animation start isn't compatible with remote animations at all.
1716 if (mService.mAppTransition.getRemoteAnimationController() != null
1717 && !mSurfaceAnimator.isAnimationStartDelayed()) {
Jorim Jaggi33a701a2017-12-01 14:58:18 +01001718 adapter = mService.mAppTransition.getRemoteAnimationController()
1719 .createAnimationAdapter(this, mTmpPoint, mTmpRect);
1720 } else {
1721 final Animation a = loadAnimation(lp, transit, enter, isVoiceInteraction);
1722 if (a != null) {
1723 adapter = new LocalAnimationAdapter(
1724 new WindowAnimationSpec(a, mTmpPoint, mTmpRect,
1725 mService.mAppTransition.canSkipFirstFrame(),
Jorim Jaggiaa763cd2018-03-22 23:20:36 +01001726 mService.mAppTransition.getAppStackClipMode(),
1727 true /* isAppAnimation */),
Jorim Jaggi33a701a2017-12-01 14:58:18 +01001728 mService.mSurfaceAnimationRunner);
1729 if (a.getZAdjustment() == Animation.ZORDER_TOP) {
1730 mNeedsZBoost = true;
1731 }
1732 mTransit = transit;
1733 mTransitFlags = mService.mAppTransition.getTransitFlags();
1734 } else {
1735 adapter = null;
chaviw23ee71c2017-12-18 11:29:41 -08001736 }
Jorim Jaggi33a701a2017-12-01 14:58:18 +01001737 }
1738 if (adapter != null) {
Jorim Jaggi619c9f72017-12-19 18:04:29 +01001739 startAnimation(getPendingTransaction(), adapter, !isVisible());
Jorim Jaggi82c17862018-02-21 17:50:18 +01001740 if (adapter.getShowWallpaper()) {
1741 mDisplayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
1742 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001743 }
1744 } else {
1745 cancelAnimation();
1746 }
1747 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
1748
1749 return isReallyAnimating();
1750 }
1751
1752 private Animation loadAnimation(WindowManager.LayoutParams lp, int transit, boolean enter,
1753 boolean isVoiceInteraction) {
1754 final DisplayContent displayContent = getTask().getDisplayContent();
1755 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
1756 final int width = displayInfo.appWidth;
1757 final int height = displayInfo.appHeight;
1758 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.v(TAG_WM,
1759 "applyAnimation: atoken=" + this);
1760
1761 // Determine the visible rect to calculate the thumbnail clip
1762 final WindowState win = findMainWindow();
1763 final Rect frame = new Rect(0, 0, width, height);
1764 final Rect displayFrame = new Rect(0, 0,
1765 displayInfo.logicalWidth, displayInfo.logicalHeight);
1766 final Rect insets = new Rect();
1767 final Rect stableInsets = new Rect();
1768 Rect surfaceInsets = null;
1769 final boolean freeform = win != null && win.inFreeformWindowingMode();
1770 if (win != null) {
1771 // Containing frame will usually cover the whole screen, including dialog windows.
1772 // For freeform workspace windows it will not cover the whole screen and it also
1773 // won't exactly match the final freeform window frame (e.g. when overlapping with
1774 // the status bar). In that case we need to use the final frame.
1775 if (freeform) {
1776 frame.set(win.mFrame);
Adrian Roos4d18a2e2017-12-19 19:08:05 +01001777 } else if (win.isLetterboxedAppWindow()) {
1778 frame.set(getTask().getBounds());
Winson Chungc1674272018-02-21 10:15:17 -08001779 } else if (win.isDockedResizing()) {
1780 // If we are animating while docked resizing, then use the stack bounds as the
1781 // animation target (which will be different than the task bounds)
1782 frame.set(getTask().getParent().getBounds());
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001783 } else {
1784 frame.set(win.mContainingFrame);
1785 }
1786 surfaceInsets = win.getAttrs().surfaceInsets;
Adrian Roos20e07892018-02-23 19:12:01 +01001787 // XXX(b/72757033): These are insets relative to the window frame, but we're really
1788 // interested in the insets relative to the frame we chose in the if-blocks above.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001789 insets.set(win.mContentInsets);
1790 stableInsets.set(win.mStableInsets);
1791 }
1792
1793 if (mLaunchTaskBehind) {
1794 // Differentiate the two animations. This one which is briefly on the screen
1795 // gets the !enter animation, and the other activity which remains on the
1796 // screen gets the enter animation. Both appear in the mOpeningApps set.
1797 enter = false;
1798 }
1799 if (DEBUG_APP_TRANSITIONS) Slog.d(TAG_WM, "Loading animation for app transition."
1800 + " transit=" + AppTransition.appTransitionToString(transit) + " enter=" + enter
1801 + " frame=" + frame + " insets=" + insets + " surfaceInsets=" + surfaceInsets);
1802 final Configuration displayConfig = displayContent.getConfiguration();
1803 final Animation a = mService.mAppTransition.loadAnimation(lp, transit, enter,
1804 displayConfig.uiMode, displayConfig.orientation, frame, displayFrame, insets,
1805 surfaceInsets, stableInsets, isVoiceInteraction, freeform, getTask().mTaskId);
1806 if (a != null) {
1807 if (DEBUG_ANIM) logWithStack(TAG, "Loaded animation " + a + " for " + this);
1808 final int containingWidth = frame.width();
1809 final int containingHeight = frame.height();
1810 a.initialize(containingWidth, containingHeight, width, height);
1811 a.scaleCurrentDuration(mService.getTransitionAnimationScaleLocked());
1812 }
1813 return a;
1814 }
1815
Jorim Jaggi619c9f72017-12-19 18:04:29 +01001816 @Override
Jorim Jaggi6de61012018-03-19 14:53:23 +01001817 public boolean shouldDeferAnimationFinish(Runnable endDeferFinishCallback) {
1818 return mAnimatingAppWindowTokenRegistry != null
1819 && mAnimatingAppWindowTokenRegistry.notifyAboutToFinish(
1820 this, endDeferFinishCallback);
1821 }
1822
1823 @Override
1824 public void onAnimationLeashDestroyed(Transaction t) {
1825 super.onAnimationLeashDestroyed(t);
1826 if (mAnimatingAppWindowTokenRegistry != null) {
1827 mAnimatingAppWindowTokenRegistry.notifyFinished(this);
1828 }
1829 }
1830
1831 @Override
Jorim Jaggi619c9f72017-12-19 18:04:29 +01001832 protected void setLayer(Transaction t, int layer) {
1833 if (!mSurfaceAnimator.hasLeash()) {
1834 t.setLayer(mSurfaceControl, layer);
1835 }
1836 }
1837
1838 @Override
1839 protected void setRelativeLayer(Transaction t, SurfaceControl relativeTo, int layer) {
1840 if (!mSurfaceAnimator.hasLeash()) {
1841 t.setRelativeLayer(mSurfaceControl, relativeTo, layer);
1842 }
1843 }
1844
1845 @Override
1846 protected void reparentSurfaceControl(Transaction t, SurfaceControl newParent) {
1847 if (!mSurfaceAnimator.hasLeash()) {
1848 t.reparent(mSurfaceControl, newParent.getHandle());
1849 }
1850 }
1851
1852 @Override
1853 public void onAnimationLeashCreated(Transaction t, SurfaceControl leash) {
Jorim Jaggi619c9f72017-12-19 18:04:29 +01001854 // The leash is parented to the animation layer. We need to preserve the z-order by using
1855 // the prefix order index, but we boost if necessary.
Robert Carrb9506032018-02-13 13:54:00 -08001856 int layer = 0;
1857 if (!inPinnedWindowingMode()) {
1858 layer = getPrefixOrderIndex();
1859 } else {
1860 // Pinned stacks have animations take place within themselves rather than an animation
1861 // layer so we need to preserve the order relative to the stack (e.g. the order of our
1862 // task/parent).
1863 layer = getParent().getPrefixOrderIndex();
1864 }
1865
Jorim Jaggi619c9f72017-12-19 18:04:29 +01001866 if (mNeedsZBoost) {
1867 layer += Z_BOOST_BASE;
1868 }
1869 leash.setLayer(layer);
Robert Carr2f8aa392018-01-31 14:46:51 -08001870
1871 final DisplayContent dc = getDisplayContent();
Jorim Jaggibe418292018-03-26 16:14:12 +02001872 dc.assignStackOrdering();
Jorim Jaggi6de61012018-03-19 14:53:23 +01001873 if (mAnimatingAppWindowTokenRegistry != null) {
1874 mAnimatingAppWindowTokenRegistry.notifyStarting(this);
1875 }
Jorim Jaggi619c9f72017-12-19 18:04:29 +01001876 }
1877
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001878 /**
1879 * This must be called while inside a transaction.
1880 */
1881 void showAllWindowsLocked() {
1882 forAllWindows(windowState -> {
1883 if (DEBUG_VISIBILITY) Slog.v(TAG, "performing show on: " + windowState);
1884 windowState.performShowLocked();
1885 }, false /* traverseTopToBottom */);
Jorim Jaggia5e10572017-11-15 14:36:26 +01001886 }
1887
1888 @Override
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001889 protected void onAnimationFinished() {
1890 super.onAnimationFinished();
1891
1892 mTransit = TRANSIT_UNSET;
1893 mTransitFlags = 0;
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001894 mNeedsZBoost = false;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001895
1896 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM | FINISH_LAYOUT_REDO_WALLPAPER,
1897 "AppWindowToken");
1898
Jorim Jaggi988f6682017-11-17 17:46:43 +01001899 clearThumbnail();
Jorim Jaggi752cd822018-03-29 16:29:18 +02001900 setClientHidden(isHidden() && hiddenRequested);
Jorim Jaggi988f6682017-11-17 17:46:43 +01001901
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001902 if (mService.mInputMethodTarget != null && mService.mInputMethodTarget.mAppToken == this) {
1903 getDisplayContent().computeImeTarget(true /* updateImeTarget */);
1904 }
1905
1906 if (DEBUG_ANIM) Slog.v(TAG, "Animation done in " + this
1907 + ": reportedVisible=" + reportedVisible
1908 + " okToDisplay=" + okToDisplay()
1909 + " okToAnimate=" + okToAnimate()
1910 + " startingDisplayed=" + startingDisplayed);
1911
1912 // WindowState.onExitAnimationDone might modify the children list, so make a copy and then
1913 // traverse the copy.
1914 final ArrayList<WindowState> children = new ArrayList<>(mChildren);
1915 children.forEach(WindowState::onExitAnimationDone);
1916
1917 mService.mAppTransition.notifyAppTransitionFinishedLocked(token);
1918 scheduleAnimation();
1919 }
1920
1921 @Override
1922 boolean isAppAnimating() {
1923 return isSelfAnimating();
1924 }
1925
1926 @Override
1927 boolean isSelfAnimating() {
1928 // If we are about to start a transition, we also need to be considered animating.
1929 return isWaitingForTransitionStart() || isReallyAnimating();
1930 }
1931
1932 /**
1933 * @return True if and only if we are actually running an animation. Note that
1934 * {@link #isSelfAnimating} also returns true if we are waiting for an animation to
1935 * start.
1936 */
1937 private boolean isReallyAnimating() {
1938 return super.isSelfAnimating();
1939 }
1940
Jorim Jaggi988f6682017-11-17 17:46:43 +01001941 @Override
1942 void cancelAnimation() {
1943 super.cancelAnimation();
1944 clearThumbnail();
1945 }
1946
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001947 boolean isWaitingForTransitionStart() {
1948 return mService.mAppTransition.isTransitionSet()
1949 && (mService.mOpeningApps.contains(this) || mService.mClosingApps.contains(this));
1950 }
1951
1952 public int getTransit() {
1953 return mTransit;
1954 }
1955
1956 int getTransitFlags() {
1957 return mTransitFlags;
1958 }
1959
Jorim Jaggi988f6682017-11-17 17:46:43 +01001960 void attachThumbnailAnimation() {
1961 if (!isReallyAnimating()) {
1962 return;
1963 }
1964 final int taskId = getTask().mTaskId;
1965 final GraphicBuffer thumbnailHeader =
1966 mService.mAppTransition.getAppTransitionThumbnailHeader(taskId);
1967 if (thumbnailHeader == null) {
1968 if (DEBUG_APP_TRANSITIONS) Slog.d(TAG, "No thumbnail header bitmap for: " + taskId);
1969 return;
1970 }
1971 clearThumbnail();
1972 mThumbnail = new AppWindowThumbnail(getPendingTransaction(), this, thumbnailHeader);
1973 mThumbnail.startAnimation(getPendingTransaction(), loadThumbnailAnimation(thumbnailHeader));
1974 }
1975
Tony Mak64b8d562017-12-28 17:44:02 +00001976 /**
1977 * Attaches a surface with a thumbnail for the
1978 * {@link android.app.ActivityOptions#ANIM_OPEN_CROSS_PROFILE_APPS} animation.
1979 */
1980 void attachCrossProfileAppsThumbnailAnimation() {
1981 if (!isReallyAnimating()) {
1982 return;
1983 }
1984 clearThumbnail();
1985
1986 final WindowState win = findMainWindow();
1987 if (win == null) {
1988 return;
1989 }
1990 final Rect frame = win.mFrame;
1991 final int thumbnailDrawableRes = getTask().mUserId == mService.mCurrentUserId
1992 ? R.drawable.ic_account_circle
Tony Makda4af232018-04-27 11:01:10 +01001993 : R.drawable.ic_corp_badge;
Tony Mak64b8d562017-12-28 17:44:02 +00001994 final GraphicBuffer thumbnail =
1995 mService.mAppTransition
1996 .createCrossProfileAppsThumbnail(thumbnailDrawableRes, frame);
1997 if (thumbnail == null) {
1998 return;
1999 }
2000 mThumbnail = new AppWindowThumbnail(getPendingTransaction(), this, thumbnail);
2001 final Animation animation =
2002 mService.mAppTransition.createCrossProfileAppsThumbnailAnimationLocked(win.mFrame);
2003 mThumbnail.startAnimation(getPendingTransaction(), animation, new Point(frame.left,
2004 frame.top));
2005 }
2006
Jorim Jaggi988f6682017-11-17 17:46:43 +01002007 private Animation loadThumbnailAnimation(GraphicBuffer thumbnailHeader) {
2008 final DisplayInfo displayInfo = mDisplayContent.getDisplayInfo();
2009
2010 // If this is a multi-window scenario, we use the windows frame as
2011 // destination of the thumbnail header animation. If this is a full screen
2012 // window scenario, we use the whole display as the target.
2013 WindowState win = findMainWindow();
2014 Rect appRect = win != null ? win.getContentFrameLw() :
2015 new Rect(0, 0, displayInfo.appWidth, displayInfo.appHeight);
2016 Rect insets = win != null ? win.mContentInsets : null;
2017 final Configuration displayConfig = mDisplayContent.getConfiguration();
2018 return mService.mAppTransition.createThumbnailAspectScaleAnimationLocked(
2019 appRect, insets, thumbnailHeader, getTask().mTaskId, displayConfig.uiMode,
2020 displayConfig.orientation);
2021 }
2022
2023 private void clearThumbnail() {
2024 if (mThumbnail == null) {
2025 return;
2026 }
2027 mThumbnail.destroy();
2028 mThumbnail = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002029 }
2030
Jorim Jaggif84e2f62018-01-16 14:17:59 +01002031 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
2032 mRemoteAnimationDefinition = definition;
2033 }
2034
2035 RemoteAnimationDefinition getRemoteAnimationDefinition() {
2036 return mRemoteAnimationDefinition;
2037 }
2038
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002039 @Override
2040 void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2041 super.dump(pw, prefix, dumpAll);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002042 if (appToken != null) {
Wale Ogunwale72919d22016-12-08 18:58:50 -08002043 pw.println(prefix + "app=true mVoiceInteraction=" + mVoiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002044 }
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08002045 pw.print(prefix); pw.print("task="); pw.println(getTask());
Wale Ogunwale51362492016-09-08 17:49:17 -07002046 pw.print(prefix); pw.print(" mFillsParent="); pw.print(mFillsParent);
2047 pw.print(" mOrientation="); pw.println(mOrientation);
Wale Ogunwale89973222017-04-23 18:39:45 -07002048 pw.println(prefix + "hiddenRequested=" + hiddenRequested + " mClientHidden=" + mClientHidden
2049 + ((mDeferHidingClient) ? " mDeferHidingClient=" + mDeferHidingClient : "")
2050 + " reportedDrawn=" + reportedDrawn + " reportedVisible=" + reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -07002051 if (paused) {
2052 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002053 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -08002054 if (mAppStopped) {
2055 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
2056 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07002057 if (mNumInterestingWindows != 0 || mNumDrawnWindows != 0
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002058 || allDrawn || mLastAllDrawn) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07002059 pw.print(prefix); pw.print("mNumInterestingWindows=");
2060 pw.print(mNumInterestingWindows);
2061 pw.print(" mNumDrawnWindows="); pw.print(mNumDrawnWindows);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002062 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -07002063 pw.print(" allDrawn="); pw.print(allDrawn);
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002064 pw.print(" lastAllDrawn="); pw.print(mLastAllDrawn);
Craig Mautner6fbda632012-07-03 09:26:39 -07002065 pw.println(")");
2066 }
2067 if (inPendingTransaction) {
2068 pw.print(prefix); pw.print("inPendingTransaction=");
2069 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002070 }
Craig Mautner799bc1d2015-01-14 10:33:48 -08002071 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002072 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
2073 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -08002074 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -08002075 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002076 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08002077 if (startingWindow != null || startingSurface != null
Wale Ogunwale6c459212017-05-17 08:56:03 -07002078 || startingDisplayed || startingMoved || mHiddenSetFromTransferredStartingWindow) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002079 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08002080 pw.print(" startingSurface="); pw.print(startingSurface);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002081 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale6c459212017-05-17 08:56:03 -07002082 pw.print(" startingMoved="); pw.print(startingMoved);
2083 pw.println(" mHiddenSetFromTransferredStartingWindow="
2084 + mHiddenSetFromTransferredStartingWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002085 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01002086 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -08002087 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -07002088 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -08002089 }
2090 if (mPendingRelaunchCount != 0) {
2091 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +01002092 }
Wale Ogunwale9c64cb62017-04-12 13:39:59 -07002093 if (getController() != null) {
2094 pw.print(prefix); pw.print("controller="); pw.println(getController());
2095 }
Wale Ogunwalee287e192017-04-21 09:30:12 -07002096 if (mRemovingFromDisplay) {
2097 pw.println(prefix + "mRemovingFromDisplay=" + mRemovingFromDisplay);
2098 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002099 }
2100
2101 @Override
2102 void setHidden(boolean hidden) {
2103 super.setHidden(hidden);
Winson Chunge55c0192017-08-24 14:50:48 -07002104
2105 if (hidden) {
2106 // Once the app window is hidden, reset the last saved PiP snap fraction
2107 mDisplayContent.mPinnedStackControllerLocked.resetReentrySnapFraction(this);
2108 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002109 scheduleAnimation();
2110 }
2111
2112 @Override
2113 void prepareSurfaces() {
2114 // isSelfAnimating also returns true when we are about to start a transition, so we need
2115 // to check super here.
2116 final boolean reallyAnimating = super.isSelfAnimating();
2117 final boolean show = !isHidden() || reallyAnimating;
2118 if (show && !mLastSurfaceShowing) {
2119 mPendingTransaction.show(mSurfaceControl);
2120 } else if (!show && mLastSurfaceShowing) {
2121 mPendingTransaction.hide(mSurfaceControl);
Jorim Jaggi4d1835d2017-08-31 17:28:27 +02002122 }
Jorim Jaggi988f6682017-11-17 17:46:43 +01002123 if (mThumbnail != null) {
2124 mThumbnail.setShowing(mPendingTransaction, show);
2125 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002126 mLastSurfaceShowing = show;
2127 super.prepareSurfaces();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002128 }
2129
Jorim Jaggi77d0f36c2018-03-16 17:49:49 +01002130 /**
2131 * @return Whether our {@link #getSurfaceControl} is currently showing.
2132 */
2133 boolean isSurfaceShowing() {
2134 return mLastSurfaceShowing;
2135 }
2136
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002137 boolean isFreezingScreen() {
2138 return mFreezingScreen;
2139 }
2140
2141 @Override
2142 boolean needsZBoost() {
2143 return mNeedsZBoost || super.needsZBoost();
2144 }
2145
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002146 @CallSuper
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002147 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002148 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002149 final long token = proto.start(fieldId);
2150 writeNameToProto(proto, NAME);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002151 super.writeToProto(proto, WINDOW_TOKEN, trim);
Vishnu Nair04ab4392018-01-10 11:00:06 -08002152 proto.write(LAST_SURFACE_SHOWING, mLastSurfaceShowing);
2153 proto.write(IS_WAITING_FOR_TRANSITION_START, isWaitingForTransitionStart());
2154 proto.write(IS_REALLY_ANIMATING, isReallyAnimating());
2155 if (mThumbnail != null){
2156 mThumbnail.writeToProto(proto, THUMBNAIL);
2157 }
2158 proto.write(FILLS_PARENT, mFillsParent);
2159 proto.write(APP_STOPPED, mAppStopped);
2160 proto.write(HIDDEN_REQUESTED, hiddenRequested);
2161 proto.write(CLIENT_HIDDEN, mClientHidden);
2162 proto.write(DEFER_HIDING_CLIENT, mDeferHidingClient);
2163 proto.write(REPORTED_DRAWN, reportedDrawn);
2164 proto.write(REPORTED_VISIBLE, reportedVisible);
2165 proto.write(NUM_INTERESTING_WINDOWS, mNumInterestingWindows);
2166 proto.write(NUM_DRAWN_WINDOWS, mNumDrawnWindows);
2167 proto.write(ALL_DRAWN, allDrawn);
2168 proto.write(LAST_ALL_DRAWN, mLastAllDrawn);
2169 proto.write(REMOVED, removed);
2170 if (startingWindow != null){
2171 startingWindow.writeIdentifierToProto(proto, STARTING_WINDOW);
2172 }
2173 proto.write(STARTING_DISPLAYED, startingDisplayed);
2174 proto.write(STARTING_MOVED, startingMoved);
2175 proto.write(HIDDEN_SET_FROM_TRANSFERRED_STARTING_WINDOW,
2176 mHiddenSetFromTransferredStartingWindow);
2177 for (Rect bounds : mFrozenBounds) {
2178 bounds.writeToProto(proto, FROZEN_BOUNDS);
2179 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002180 proto.end(token);
2181 }
2182
2183 void writeNameToProto(ProtoOutputStream proto, long fieldId) {
2184 if (appToken == null) {
2185 return;
2186 }
2187 try {
2188 proto.write(fieldId, appToken.getName());
2189 } catch (RemoteException e) {
2190 // This shouldn't happen, but in this case fall back to outputting nothing
2191 Slog.e(TAG, e.toString());
2192 }
2193 }
2194
2195 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002196 public String toString() {
2197 if (stringName == null) {
2198 StringBuilder sb = new StringBuilder();
2199 sb.append("AppWindowToken{");
2200 sb.append(Integer.toHexString(System.identityHashCode(this)));
2201 sb.append(" token="); sb.append(token); sb.append('}');
2202 stringName = sb.toString();
2203 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002204 return stringName + ((mIsExiting) ? " mIsExiting=" : "");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08002205 }
Adrian Roos20e07892018-02-23 19:12:01 +01002206
2207 Rect getLetterboxInsets() {
2208 if (mLetterbox != null) {
2209 return mLetterbox.getInsets();
2210 } else {
2211 return new Rect();
2212 }
2213 }
Adrian Roos23df3a32018-03-15 15:41:13 +01002214
2215 /**
2216 * @eturn true if there is a letterbox and any part of that letterbox overlaps with
2217 * the given {@code rect}.
2218 */
2219 boolean isLetterboxOverlappingWith(Rect rect) {
2220 return mLetterbox != null && mLetterbox.isOverlappingWith(rect);
2221 }
Jeff Browne9bdb312012-04-05 15:30:10 -07002222}