blob: 444a05df5fdcaf28273c7f77cfe194feb743764e [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
Wale Ogunwale72919d22016-12-08 18:58:50 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070021import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070022import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020023import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070024import static android.view.Display.DEFAULT_DISPLAY;
Jorim Jaggife762342016-10-13 14:33:27 +020025import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020026import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Jorim Jaggife762342016-10-13 14:33:27 +020027import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Wale Ogunwaled1c37912016-08-16 03:19:39 -070028import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080029import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070030import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Adrian Roose99bc052017-11-20 17:55:31 +010031import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
32import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070033import static com.android.server.wm.AppTransition.TRANSIT_UNSET;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080034import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080035import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
36import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070037import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070038import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
39import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwale9017ec02016-02-25 08:55:25 -080040import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
Jorim Jaggie4b0f282017-05-17 15:10:29 +020041import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW_VERBOSE;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070042import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080043import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
44import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
45import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
46import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Chong Zhang92147042016-05-09 12:47:11 -070047import static com.android.server.wm.WindowManagerService.H.NOTIFY_ACTIVITY_DRAWN;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070048import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_NORMAL;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070049import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070050import static com.android.server.wm.WindowManagerService.logWithStack;
Steven Timotiusaf03df62017-07-18 16:56:43 -070051import static com.android.server.wm.proto.AppWindowTokenProto.NAME;
52import static com.android.server.wm.proto.AppWindowTokenProto.WINDOW_TOKEN;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080053
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070054import android.annotation.CallSuper;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020055import android.app.Activity;
Jorim Jaggi26c8c422016-05-09 19:57:25 -070056import android.content.res.Configuration;
Jorim Jaggi988f6682017-11-17 17:46:43 +010057import android.graphics.GraphicBuffer;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020058import android.graphics.Point;
Jorim Jaggi0429f352015-12-22 16:29:16 +010059import android.graphics.Rect;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070060import android.os.Binder;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080061import android.os.Debug;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070062import android.os.IBinder;
Steven Timotiusaf03df62017-07-18 16:56:43 -070063import android.os.RemoteException;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020064import android.os.Trace;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080065import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -070066import android.util.proto.ProtoOutputStream;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020067import android.view.DisplayInfo;
68import android.view.animation.Animation;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080069import android.view.IApplicationToken;
Robert Carr6914f082017-03-20 19:04:30 -070070import android.view.SurfaceControl;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080071import android.view.WindowManager;
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +020072import android.view.WindowManager.LayoutParams;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080073
74import com.android.internal.util.ToBooleanFunction;
75import com.android.server.input.InputApplicationHandle;
Adrian Roose99bc052017-11-20 17:55:31 +010076import com.android.server.policy.WindowManagerPolicy.StartingSurface;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080077import com.android.server.wm.WindowManagerService.H;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080078
79import java.io.PrintWriter;
Jorim Jaggi0429f352015-12-22 16:29:16 +010080import java.util.ArrayDeque;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080081import java.util.ArrayList;
Jorim Jaggia5e10572017-11-15 14:36:26 +010082import java.util.LinkedList;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080083
84class AppTokenList extends ArrayList<AppWindowToken> {
85}
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080086
87/**
88 * Version of WindowToken that is specifically for a particular application (or
89 * really activity) that is displaying windows.
90 */
Wale Ogunwale3f4433d2016-08-18 20:42:42 -070091class AppWindowToken extends WindowToken implements WindowManagerService.AppFreezeListener {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080092 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowToken" : TAG_WM;
93
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080094 // Non-null only for application tokens.
95 final IApplicationToken appToken;
96
Wale Ogunwale72919d22016-12-08 18:58:50 -080097 final boolean mVoiceInteraction;
Dianne Hackborne30e02f2014-05-27 18:24:45 -070098
Wale Ogunwale51362492016-09-08 17:49:17 -070099 /** @see WindowContainer#fillsParent() */
100 private boolean mFillsParent;
Craig Mautner4c5eb222013-11-18 12:59:05 -0800101 boolean layoutConfigChanges;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800102 boolean mShowForAllUsers;
103 int mTargetSdk;
Craig Mautnera2c77052012-03-26 12:14:43 -0700104
Bryce Lee6d410262017-02-28 15:30:17 -0800105 // Flag set while reparenting to prevent actions normally triggered by an individual parent
106 // change.
107 private boolean mReparenting;
108
Wale Ogunwalee287e192017-04-21 09:30:12 -0700109 // True if we are current in the process of removing this app token from the display
110 private boolean mRemovingFromDisplay = false;
111
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800112 // The input dispatching timeout for this application token in nanoseconds.
Wale Ogunwale72919d22016-12-08 18:58:50 -0800113 long mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800114
115 // These are used for determining when all windows associated with
116 // an activity have been drawn, so they can be made visible together
117 // at the same time.
Craig Mautner764983d2012-03-22 11:37:36 -0700118 // initialize so that it doesn't match mTransactionSequence which is an int.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700119 private long mLastTransactionSequence = Long.MIN_VALUE;
120 private int mNumInterestingWindows;
121 private int mNumDrawnWindows;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800122 boolean inPendingTransaction;
123 boolean allDrawn;
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000124 private boolean mLastAllDrawn;
125
Craig Mautner7636dfb2012-11-16 15:24:11 -0800126 // Set to true when this app creates a surface while in the middle of an animation. In that
127 // case do not clear allDrawn until the animation completes.
128 boolean deferClearAllDrawn;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800129
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800130 // Is this window's surface needed? This is almost like hidden, except
131 // it will sometimes be true a little earlier: when the token has
132 // been shown, but is still waiting for its app transition to execute
133 // before making its windows shown.
134 boolean hiddenRequested;
135
136 // Have we told the window clients to hide themselves?
Wale Ogunwale89973222017-04-23 18:39:45 -0700137 private boolean mClientHidden;
138
139 // If true we will defer setting mClientHidden to true and reporting to the client that it is
140 // hidden.
141 boolean mDeferHidingClient;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800142
143 // Last visibility state we reported to the app token.
144 boolean reportedVisible;
145
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700146 // Last drawn state we reported to the app token.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700147 private boolean reportedDrawn;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700148
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800149 // Set to true when the token has been removed from the window mgr.
150 boolean removed;
151
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800152 // Information about an application starting window if displayed.
153 StartingData startingData;
154 WindowState startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800155 StartingSurface startingSurface;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800156 boolean startingDisplayed;
157 boolean startingMoved;
Wale Ogunwale6c459212017-05-17 08:56:03 -0700158 // True if the hidden state of this token was forced to false due to a transferred starting
159 // window.
160 private boolean mHiddenSetFromTransferredStartingWindow;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800161 boolean firstWindowDrawn;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700162 private final WindowState.UpdateReportedVisibilityResults mReportedVisibilityResults =
163 new WindowState.UpdateReportedVisibilityResults();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800164
165 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700166 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800167
Wale Ogunwale571771c2016-08-26 13:18:50 -0700168 // TODO: Have a WindowContainer state for tracking exiting/deferred removal.
Craig Mautner799bc1d2015-01-14 10:33:48 -0800169 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800170
Craig Mautnerbb742462014-07-07 15:28:55 -0700171 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700172 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700173
Wale Ogunwale72919d22016-12-08 18:58:50 -0800174 private boolean mAlwaysFocusable;
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800175
Robert Carre12aece2016-02-02 22:43:27 -0800176 boolean mAppStopped;
Robert Carrfd10cd12016-06-29 16:41:50 -0700177 int mRotationAnimationHint;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700178 private int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800179
Jorim Jaggife762342016-10-13 14:33:27 +0200180 private boolean mLastContainsShowWhenLockedWindow;
181 private boolean mLastContainsDismissKeyguardWindow;
182
Jorim Jaggi0429f352015-12-22 16:29:16 +0100183 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700184 ArrayDeque<Configuration> mFrozenMergedConfig = new ArrayDeque<>();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100185
Wale Ogunwale6c459212017-05-17 08:56:03 -0700186 private boolean mDisablePreviewScreenshots;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100187
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700188 private Task mLastParent;
Robert Carred3e83b2017-04-21 13:26:55 -0700189
chaviwd3bf08d2017-08-01 17:24:59 -0700190 /**
191 * See {@link #canTurnScreenOn()}
192 */
193 private boolean mCanTurnScreenOn = true;
194
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200195 /**
196 * If we are running an animation, this determines the transition type. Must be one of
197 * AppTransition.TRANSIT_* constants.
198 */
199 private int mTransit;
200
201 /**
202 * If we are running an animation, this determines the flags during this animation. Must be a
203 * bitwise combination of AppTransition.TRANSIT_FLAG_* constants.
204 */
205 private int mTransitFlags;
206
207 /** Whether our surface was set to be showing in the last call to {@link #prepareSurfaces} */
208 private boolean mLastSurfaceShowing;
209
Jorim Jaggi988f6682017-11-17 17:46:43 +0100210 private AppWindowThumbnail mThumbnail;
211
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000212 /** Have we been asked to have this token keep the screen frozen? */
213 private boolean mFreezingScreen;
214
215 /** Whether this token should be boosted at the top of all app window tokens. */
216 private boolean mNeedsZBoost;
217
chaviw23ee71c2017-12-18 11:29:41 -0800218 private final Point mTmpPoint = new Point();
219
Wale Ogunwale72919d22016-12-08 18:58:50 -0800220 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
221 DisplayContent dc, long inputDispatchingTimeoutNanos, boolean fullscreen,
222 boolean showForAllUsers, int targetSdk, int orientation, int rotationAnimationHint,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800223 int configChanges, boolean launchTaskBehind, boolean alwaysFocusable,
Bryce Leef3c6a472017-11-14 14:53:06 -0800224 AppWindowContainerController controller) {
225 this(service, token, voiceInteraction, dc, fullscreen);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800226 setController(controller);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800227 mInputDispatchingTimeoutNanos = inputDispatchingTimeoutNanos;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800228 mShowForAllUsers = showForAllUsers;
229 mTargetSdk = targetSdk;
230 mOrientation = orientation;
231 layoutConfigChanges = (configChanges & (CONFIG_SCREEN_SIZE | CONFIG_ORIENTATION)) != 0;
232 mLaunchTaskBehind = launchTaskBehind;
233 mAlwaysFocusable = alwaysFocusable;
234 mRotationAnimationHint = rotationAnimationHint;
235
236 // Application tokens start out hidden.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200237 setHidden(true);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800238 hiddenRequested = true;
239 }
240
241 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
Bryce Leef3c6a472017-11-14 14:53:06 -0800242 DisplayContent dc, boolean fillsParent) {
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800243 super(service, token != null ? token.asBinder() : null, TYPE_APPLICATION, true, dc,
244 false /* ownerCanManageAppTokens */);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700245 appToken = token;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800246 mVoiceInteraction = voiceInteraction;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800247 mFillsParent = fillsParent;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800248 mInputApplicationHandle = new InputApplicationHandle(this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800249 }
250
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800251 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
252 firstWindowDrawn = true;
253
254 // We now have a good window to show, remove dead placeholders
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700255 removeDeadWindows();
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800256
Jorim Jaggi02886a82016-12-06 09:10:06 -0800257 if (startingWindow != null) {
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800258 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
259 + win.mToken + ": first real window is shown, no animation");
260 // If this initial window is animating, stop it -- we will do an animation to reveal
261 // it from behind the starting window, so there is no need for it to also be doing its
262 // own stuff.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100263 win.cancelAnimation();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800264 if (getController() != null) {
265 getController().removeStartingWindow();
266 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800267 }
268 updateReportedVisibilityLocked();
269 }
270
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800271 void updateReportedVisibilityLocked() {
272 if (appToken == null) {
273 return;
274 }
275
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700276 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700277 final int count = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800278
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700279 mReportedVisibilityResults.reset();
280
281 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700282 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700283 win.updateReportedVisibility(mReportedVisibilityResults);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800284 }
285
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700286 int numInteresting = mReportedVisibilityResults.numInteresting;
287 int numVisible = mReportedVisibilityResults.numVisible;
288 int numDrawn = mReportedVisibilityResults.numDrawn;
289 boolean nowGone = mReportedVisibilityResults.nowGone;
290
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700291 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200292 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting && !isHidden();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700293 if (!nowGone) {
294 // If the app is not yet gone, then it can only become visible/drawn.
295 if (!nowDrawn) {
296 nowDrawn = reportedDrawn;
297 }
298 if (!nowVisible) {
299 nowVisible = reportedVisible;
300 }
301 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800302 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800303 + numInteresting + " visible=" + numVisible);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800304 final AppWindowContainerController controller = getController();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700305 if (nowDrawn != reportedDrawn) {
306 if (nowDrawn) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800307 if (controller != null) {
308 controller.reportWindowsDrawn();
309 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700310 }
311 reportedDrawn = nowDrawn;
312 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800313 if (nowVisible != reportedVisible) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700314 if (DEBUG_VISIBILITY) Slog.v(TAG,
315 "Visibility changed in " + this + ": vis=" + nowVisible);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800316 reportedVisible = nowVisible;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800317 if (controller != null) {
318 if (nowVisible) {
319 controller.reportWindowsVisible();
320 } else {
321 controller.reportWindowsGone();
322 }
323 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800324 }
325 }
326
Wale Ogunwale89973222017-04-23 18:39:45 -0700327 boolean isClientHidden() {
328 return mClientHidden;
329 }
330
331 void setClientHidden(boolean hideClient) {
332 if (mClientHidden == hideClient || (hideClient && mDeferHidingClient)) {
333 return;
334 }
335 mClientHidden = hideClient;
336 sendAppVisibilityToClients();
337 }
338
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700339 boolean setVisibility(WindowManager.LayoutParams lp,
340 boolean visible, int transit, boolean performLayout, boolean isVoiceInteraction) {
341
342 boolean delayed = false;
343 inPendingTransaction = false;
Wale Ogunwale9e4721f2017-05-23 19:37:30 -0700344 // Reset the state of mHiddenSetFromTransferredStartingWindow since visibility is actually
345 // been set by the app now.
346 mHiddenSetFromTransferredStartingWindow = false;
Wale Ogunwale89973222017-04-23 18:39:45 -0700347 setClientHidden(!visible);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700348
349 // Allow for state changes and animation to be applied if:
350 // * token is transitioning visibility state
351 // * or the token was marked as hidden and is exiting before we had a chance to play the
352 // transition animation
353 // * or this is an opening app and windows are being replaced.
354 boolean visibilityChanged = false;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200355 if (isHidden() == visible || (isHidden() && mIsExiting) || (visible && waitingForReplacement())) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700356 final AccessibilityController accessibilityController = mService.mAccessibilityController;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700357 boolean changed = false;
358 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200359 "Changing app " + this + " hidden=" + isHidden() + " performLayout=" + performLayout);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700360
361 boolean runningAppAnimation = false;
362
363 if (transit != AppTransition.TRANSIT_UNSET) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200364 if (applyAnimationLocked(lp, transit, visible, isVoiceInteraction)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700365 delayed = runningAppAnimation = true;
366 }
367 final WindowState window = findMainWindow();
368 //TODO (multidisplay): Magnification is supported only for the default display.
369 if (window != null && accessibilityController != null
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700370 && getDisplayContent().getDisplayId() == DEFAULT_DISPLAY) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700371 accessibilityController.onAppWindowTransitionLocked(window, transit);
372 }
373 changed = true;
374 }
375
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700376 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700377 for (int i = 0; i < windowsCount; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700378 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700379 changed |= win.onAppVisibilityChanged(visible, runningAppAnimation);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700380 }
381
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200382 setHidden(!visible);
383 hiddenRequested = !visible;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700384 visibilityChanged = true;
385 if (!visible) {
386 stopFreezingScreen(true, true);
387 } else {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700388 // If we are being set visible, and the starting window is not yet displayed,
389 // then make sure it doesn't get displayed.
390 if (startingWindow != null && !startingWindow.isDrawnLw()) {
391 startingWindow.mPolicyVisibility = false;
392 startingWindow.mPolicyVisibilityAfterAnim = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700393 }
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700394
395 // We are becoming visible, so better freeze the screen with the windows that are
396 // getting visible so we also wait for them.
397 forAllWindows(mService::makeWindowFreezingScreenIfNeededLocked, true);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700398 }
399
400 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "setVisibility: " + this
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200401 + ": hidden=" + isHidden() + " hiddenRequested=" + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700402
403 if (changed) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700404 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700405 if (performLayout) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700406 mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700407 false /*updateInputWindows*/);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700408 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700409 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700410 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700411 }
412 }
413
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200414 if (isReallyAnimating()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700415 delayed = true;
416 }
417
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700418 for (int i = mChildren.size() - 1; i >= 0 && !delayed; i--) {
Jorim Jaggia5e10572017-11-15 14:36:26 +0100419 if ((mChildren.get(i)).isSelfOrChildAnimating()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700420 delayed = true;
421 }
422 }
423
424 if (visibilityChanged) {
425 if (visible && !delayed) {
426 // The token was made immediately visible, there will be no entrance animation.
427 // We need to inform the client the enter animation was finished.
428 mEnteringAnimation = true;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700429 mService.mActivityManagerAppTransitionNotifier.onAppTransitionFinishedLocked(token);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700430 }
Robert Carr61b81112017-07-17 18:08:15 -0700431
Robert Carre7cc44d2017-03-20 19:04:30 -0700432 // If we are hidden but there is no delay needed we immediately
433 // apply the Surface transaction so that the ActivityManager
Robert Carr61b81112017-07-17 18:08:15 -0700434 // can have some guarantee on the Surface state following
435 // setting the visibility. This captures cases like dismissing
436 // the docked or pinned stack where there is no app transition.
437 //
438 // In the case of a "Null" animation, there will be
439 // no animation but there will still be a transition set.
440 // We still need to delay hiding the surface such that it
441 // can be synchronized with showing the next surface in the transition.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200442 if (isHidden() && !delayed && !mService.mAppTransition.isTransitionSet()) {
Robert Carr6914f082017-03-20 19:04:30 -0700443 SurfaceControl.openTransaction();
444 for (int i = mChildren.size() - 1; i >= 0; i--) {
445 mChildren.get(i).mWinAnimator.hide("immediately hidden");
446 }
447 SurfaceControl.closeTransaction();
448 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700449
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700450 if (!mService.mClosingApps.contains(this) && !mService.mOpeningApps.contains(this)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700451 // The token is not closing nor opening, so even if there is an animation set, that
452 // doesn't mean that it goes through the normal app transition cycle so we have
453 // to inform the docked controller about visibility change.
Andrii Kulian1e32e022016-09-16 15:29:34 -0700454 // TODO(multi-display): notify docked divider on all displays where visibility was
455 // affected.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700456 mService.getDefaultDisplayContentLocked().getDockedDividerController()
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700457 .notifyAppVisibilityChanged();
Jorim Jaggi8b702ed2017-01-20 16:59:03 +0100458 mService.mTaskSnapshotController.notifyAppVisibilityChanged(this, visible);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700459 }
460 }
461
462 return delayed;
463 }
464
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +0200465 /**
466 * @return The to top most child window for which {@link LayoutParams#isFullscreen()} returns
467 * true.
468 */
469 WindowState getTopFullscreenWindow() {
470 for (int i = mChildren.size() - 1; i >= 0; i--) {
471 final WindowState win = mChildren.get(i);
472 if (win != null && win.mAttrs.isFullscreen()) {
473 return win;
474 }
475 }
476 return null;
477 }
478
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800479 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700480 WindowState candidate = null;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700481 int j = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800482 while (j > 0) {
483 j--;
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700484 final WindowState win = mChildren.get(j);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700485 final int type = win.mAttrs.type;
486 // No need to loop through child window as base application and starting types can't be
487 // child windows.
488 if (type == TYPE_BASE_APPLICATION || type == TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700489 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900490 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700491 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800492 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700493 candidate = win;
494 } else {
495 return win;
496 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800497 }
498 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700499 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800500 }
501
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800502 boolean windowsAreFocusable() {
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700503 return getWindowConfiguration().canReceiveKeys() || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800504 }
505
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800506 AppWindowContainerController getController() {
507 final WindowContainerController controller = super.getController();
508 return controller != null ? (AppWindowContainerController) controller : null;
509 }
510
Wale Ogunwale571771c2016-08-26 13:18:50 -0700511 @Override
Wale Ogunwale44f21802016-09-02 12:49:48 -0700512 boolean isVisible() {
Wale Ogunwalee471be62016-10-03 07:53:55 -0700513 // If the app token isn't hidden then it is considered visible and there is no need to check
514 // its children windows to see if they are visible.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200515 return !isHidden();
Wale Ogunwale44f21802016-09-02 12:49:48 -0700516 }
517
518 @Override
Wale Ogunwalee287e192017-04-21 09:30:12 -0700519 void removeImmediately() {
520 onRemovedFromDisplay();
521 super.removeImmediately();
522 }
523
524 @Override
Wale Ogunwale571771c2016-08-26 13:18:50 -0700525 void removeIfPossible() {
Craig Mautnere3119b72015-01-20 15:02:36 -0800526 mIsExiting = false;
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800527 removeAllWindowsIfPossible();
Bryce Lee6d410262017-02-28 15:30:17 -0800528 removeImmediately();
Craig Mautnere3119b72015-01-20 15:02:36 -0800529 }
530
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700531 @Override
532 boolean checkCompleteDeferredRemoval() {
533 if (mIsExiting) {
534 removeIfPossible();
535 }
536 return super.checkCompleteDeferredRemoval();
537 }
538
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700539 void onRemovedFromDisplay() {
Wale Ogunwalee287e192017-04-21 09:30:12 -0700540 if (mRemovingFromDisplay) {
541 return;
542 }
543 mRemovingFromDisplay = true;
544
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700545 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app token: " + this);
546
Wale Ogunwale72919d22016-12-08 18:58:50 -0800547 boolean delayed = setVisibility(null, false, TRANSIT_UNSET, true, mVoiceInteraction);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700548
549 mService.mOpeningApps.remove(this);
Jorim Jaggi45ca0542017-05-30 16:16:01 -0700550 mService.mUnknownAppVisibilityController.appRemovedOrHidden(this);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100551 mService.mTaskSnapshotController.onAppRemoved(this);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700552 waitingToShow = false;
553 if (mService.mClosingApps.contains(this)) {
554 delayed = true;
555 } else if (mService.mAppTransition.isTransitionSet()) {
556 mService.mClosingApps.add(this);
557 delayed = true;
558 }
559
560 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app " + this + " delayed=" + delayed
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200561 + " animation=" + getAnimation() + " animating=" + isSelfAnimating());
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700562
563 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM, "removeAppToken: "
564 + this + " delayed=" + delayed + " Callers=" + Debug.getCallers(4));
565
Jorim Jaggi19be6052017-08-03 18:33:43 +0200566 if (startingData != null && getController() != null) {
567 getController().removeStartingWindow();
568 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800569
Winson Chung87e5d552017-04-05 11:49:38 -0700570 // If this window was animating, then we need to ensure that the app transition notifies
571 // that animations have completed in WMS.handleAnimatingStoppedAndTransitionLocked(), so
572 // add to that list now
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200573 if (isSelfAnimating()) {
Winson Chung87e5d552017-04-05 11:49:38 -0700574 mService.mNoAnimationNotifyOnTransitionFinished.add(token);
575 }
576
Wale Ogunwalee287e192017-04-21 09:30:12 -0700577 final TaskStack stack = getStack();
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700578 if (delayed && !isEmpty()) {
579 // set the token aside because it has an active animation to be finished
580 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM,
581 "removeAppToken make exiting: " + this);
Wale Ogunwalee287e192017-04-21 09:30:12 -0700582 if (stack != null) {
583 stack.mExitingAppTokens.add(this);
584 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700585 mIsExiting = true;
586 } else {
587 // Make sure there is no animation running on this token, so any windows associated
588 // with it will be removed as soon as their animations are complete
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200589 cancelAnimation();
Wale Ogunwale3106ae012017-05-16 08:56:37 -0700590 if (stack != null) {
591 stack.mExitingAppTokens.remove(this);
592 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700593 removeIfPossible();
594 }
595
596 removed = true;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700597 stopFreezingScreen(true, true);
Bryce Lee6d410262017-02-28 15:30:17 -0800598
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700599 if (mService.mFocusedApp == this) {
600 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "Removing focused app token:" + this);
601 mService.mFocusedApp = null;
602 mService.updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL, true /*updateInputWindows*/);
603 mService.mInputMonitor.setFocusedAppLw(null);
604 }
605
606 if (!delayed) {
607 updateReportedVisibilityLocked();
608 }
Wale Ogunwalee287e192017-04-21 09:30:12 -0700609
610 mRemovingFromDisplay = false;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700611 }
612
Chong Zhange05bcb12016-07-26 17:47:29 -0700613 void clearAnimatingFlags() {
Chong Zhangb0d26702016-08-12 16:03:29 -0700614 boolean wallpaperMightChange = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700615 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700616 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700617 wallpaperMightChange |= win.clearAnimatingFlags();
Chong Zhange05bcb12016-07-26 17:47:29 -0700618 }
Chong Zhangb0d26702016-08-12 16:03:29 -0700619 if (wallpaperMightChange) {
620 requestUpdateWallpaperIfNeeded();
621 }
Chong Zhange05bcb12016-07-26 17:47:29 -0700622 }
623
Robert Carre12aece2016-02-02 22:43:27 -0800624 void destroySurfaces() {
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700625 destroySurfaces(false /*cleanupOnResume*/);
626 }
627
628 /**
629 * Destroy surfaces which have been marked as eligible by the animator, taking care to ensure
630 * the client has finished with them.
631 *
632 * @param cleanupOnResume whether this is done when app is resumed without fully stopped. If
633 * set to true, destroy only surfaces of removed windows, and clear relevant flags of the
634 * others so that they are ready to be reused. If set to false (common case), destroy all
635 * surfaces that's eligible, if the app is already stopped.
636 */
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700637 private void destroySurfaces(boolean cleanupOnResume) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700638 boolean destroyedSomething = false;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100639
640 // Copying to a different list as multiple children can be removed.
641 // TODO: Not sure why this is needed.
642 final LinkedList<WindowState> children = new LinkedList<>(mChildren);
643 for (int i = children.size() - 1; i >= 0; i--) {
644 final WindowState win = children.get(i);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700645 destroyedSomething |= win.destroySurface(cleanupOnResume, mAppStopped);
Robert Carre12aece2016-02-02 22:43:27 -0800646 }
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700647 if (destroyedSomething) {
648 final DisplayContent dc = getDisplayContent();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700649 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Robert Carre12aece2016-02-02 22:43:27 -0800650 }
651 }
652
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800653 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700654 * Notify that the app is now resumed, and it was not stopped before, perform a clean
655 * up of the surfaces
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800656 */
Jorim Jaggibae01b12017-04-11 16:29:10 -0700657 void notifyAppResumed(boolean wasStopped) {
Chong Zhangad24f962016-08-25 12:12:33 -0700658 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppResumed: wasStopped=" + wasStopped
Jorim Jaggibae01b12017-04-11 16:29:10 -0700659 + " " + this);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700660 mAppStopped = false;
chaviwd3bf08d2017-08-01 17:24:59 -0700661 // Allow the window to turn the screen on once the app is resumed again.
662 setCanTurnScreenOn(true);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700663 if (!wasStopped) {
664 destroySurfaces(true /*cleanupOnResume*/);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800665 }
Robert Carre12aece2016-02-02 22:43:27 -0800666 }
667
Chong Zhangbef461f2015-10-27 11:38:24 -0700668 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700669 * Notify that the app has stopped, and it is okay to destroy any surfaces which were
670 * keeping alive in case they were still being used.
671 */
672 void notifyAppStopped() {
673 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: " + this);
674 mAppStopped = true;
675 destroySurfaces();
676 // Remove any starting window that was added for this app if they are still around.
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800677 if (getController() != null) {
678 getController().removeStartingWindow();
679 }
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700680 }
681
Chong Zhang92147042016-05-09 12:47:11 -0700682 void clearAllDrawn() {
683 allDrawn = false;
684 deferClearAllDrawn = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700685 }
686
Bryce Lee6d410262017-02-28 15:30:17 -0800687 Task getTask() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800688 return (Task) getParent();
Bryce Lee6d410262017-02-28 15:30:17 -0800689 }
690
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800691 TaskStack getStack() {
692 final Task task = getTask();
693 if (task != null) {
694 return task.mStack;
695 } else {
696 return null;
697 }
698 }
699
Bryce Lee6d410262017-02-28 15:30:17 -0800700 @Override
701 void onParentSet() {
702 super.onParentSet();
703
Robert Carred3e83b2017-04-21 13:26:55 -0700704 final Task task = getTask();
705
Bryce Lee6d410262017-02-28 15:30:17 -0800706 // When the associated task is {@code null}, the {@link AppWindowToken} can no longer
707 // access visual elements like the {@link DisplayContent}. We must remove any associations
708 // such as animations.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800709 if (!mReparenting) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800710 if (task == null) {
711 // It is possible we have been marked as a closing app earlier. We must remove ourselves
712 // from this list so we do not participate in any future animations.
713 mService.mClosingApps.remove(this);
Robert Carred3e83b2017-04-21 13:26:55 -0700714 } else if (mLastParent != null && mLastParent.mStack != null) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800715 task.mStack.mExitingAppTokens.remove(this);
716 }
Bryce Lee6d410262017-02-28 15:30:17 -0800717 }
Robert Carred3e83b2017-04-21 13:26:55 -0700718 mLastParent = task;
Bryce Lee6d410262017-02-28 15:30:17 -0800719 }
720
Wale Ogunwalefa854eb2016-09-20 13:43:52 -0700721 void postWindowRemoveStartingWindowCleanup(WindowState win) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700722 // TODO: Something smells about the code below...Is there a better way?
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700723 if (startingWindow == win) {
724 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Notify removed startingWindow " + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800725 if (getController() != null) {
726 getController().removeStartingWindow();
727 }
Wale Ogunwale6c459212017-05-17 08:56:03 -0700728 } else if (mChildren.size() == 0) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700729 // If this is the last window and we had requested a starting transition window,
730 // well there is no point now.
Jorim Jaggi02886a82016-12-06 09:10:06 -0800731 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Nulling last startingData");
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700732 startingData = null;
Wale Ogunwale6c459212017-05-17 08:56:03 -0700733 if (mHiddenSetFromTransferredStartingWindow) {
734 // We set the hidden state to false for the token from a transferred starting window.
735 // We now reset it back to true since the starting window was the last window in the
736 // token.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200737 setHidden(true);
Wale Ogunwale6c459212017-05-17 08:56:03 -0700738 }
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100739 } else if (mChildren.size() == 1 && startingSurface != null && !isRelaunching()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700740 // If this is the last window except for a starting transition window,
741 // we need to get rid of the starting transition.
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200742 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Last window, removing starting window "
743 + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800744 if (getController() != null) {
745 getController().removeStartingWindow();
746 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700747 }
748 }
749
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700750 void removeDeadWindows() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700751 for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700752 WindowState win = mChildren.get(winNdx);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800753 if (win.mAppDied) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700754 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.w(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700755 "removeDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800756 // Set mDestroying, we don't want any animation or delayed removal here.
757 win.mDestroying = true;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700758 // Also removes child windows.
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700759 win.removeIfPossible();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800760 }
761 }
762 }
763
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700764 boolean hasWindowsAlive() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700765 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700766 // No need to loop through child windows as the answer should be the same as that of the
767 // parent window.
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700768 if (!(mChildren.get(i)).mAppDied) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700769 return true;
770 }
771 }
772 return false;
773 }
774
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700775 void setWillReplaceWindows(boolean animate) {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700776 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
777 "Marking app token " + this + " with replacing windows.");
Robert Carra1eb4392015-12-10 12:43:51 -0800778
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700779 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700780 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700781 w.setWillReplaceWindow(animate);
Robert Carra1eb4392015-12-10 12:43:51 -0800782 }
Robert Carra1eb4392015-12-10 12:43:51 -0800783 }
784
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700785 void setWillReplaceChildWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700786 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + this
Robert Carr23fa16b2016-01-13 13:19:58 -0800787 + " with replacing child windows.");
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700788 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700789 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700790 w.setWillReplaceChildWindows();
Robert Carr23fa16b2016-01-13 13:19:58 -0800791 }
792 }
793
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700794 void clearWillReplaceWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700795 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
796 "Resetting app token " + this + " of replacing window marks.");
Chong Zhangf596cd52016-01-05 13:42:44 -0800797
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700798 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700799 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700800 w.clearWillReplaceWindow();
Chong Zhangf596cd52016-01-05 13:42:44 -0800801 }
802 }
803
Chong Zhang4d7369a2016-04-25 16:09:14 -0700804 void requestUpdateWallpaperIfNeeded() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700805 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700806 final WindowState w = mChildren.get(i);
Chong Zhang4d7369a2016-04-25 16:09:14 -0700807 w.requestUpdateWallpaperIfNeeded();
808 }
809 }
810
Chong Zhangd78ddb42016-03-02 17:01:14 -0800811 boolean isRelaunching() {
812 return mPendingRelaunchCount > 0;
813 }
814
Robert Carr68375192017-06-13 12:41:53 -0700815 boolean shouldFreezeBounds() {
816 final Task task = getTask();
817
818 // For freeform windows, we can't freeze the bounds at the moment because this would make
819 // the resizing unresponsive.
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700820 if (task == null || task.inFreeformWindowingMode()) {
Robert Carr68375192017-06-13 12:41:53 -0700821 return false;
822 }
823
824 // We freeze the bounds while drag resizing to deal with the time between
825 // the divider/drag handle being released, and the handling it's new
826 // configuration. If we are relaunched outside of the drag resizing state,
827 // we need to be careful not to do this.
828 return getTask().isDragResizing();
829 }
830
Chong Zhangd78ddb42016-03-02 17:01:14 -0800831 void startRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700832 if (shouldFreezeBounds()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -0800833 freezeBounds();
834 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800835
836 // In the process of tearing down before relaunching, the app will
837 // try and clean up it's child surfaces. We need to prevent this from
838 // happening, so we sever the children, transfering their ownership
839 // from the client it-self to the parent surface (owned by us).
840 for (int i = mChildren.size() - 1; i >= 0; i--) {
841 final WindowState w = mChildren.get(i);
842 w.mWinAnimator.detachChildren();
843 }
844
Chong Zhangd78ddb42016-03-02 17:01:14 -0800845 mPendingRelaunchCount++;
846 }
847
848 void finishRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700849 unfreezeBounds();
850
Chong Zhangd78ddb42016-03-02 17:01:14 -0800851 if (mPendingRelaunchCount > 0) {
852 mPendingRelaunchCount--;
Bryce Lee081554b2017-05-25 07:52:12 -0700853 } else {
854 // Update keyguard flags upon finishing relaunch.
855 checkKeyguardFlagsChanged();
Chong Zhangd78ddb42016-03-02 17:01:14 -0800856 }
857 }
858
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700859 void clearRelaunching() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700860 if (mPendingRelaunchCount == 0) {
861 return;
862 }
Robert Carr68375192017-06-13 12:41:53 -0700863 unfreezeBounds();
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700864 mPendingRelaunchCount = 0;
865 }
866
Wale Ogunwale07bcab72016-10-14 15:30:09 -0700867 /**
868 * Returns true if the new child window we are adding to this token is considered greater than
869 * the existing child window in this token in terms of z-order.
870 */
871 @Override
872 protected boolean isFirstChildWindowGreaterThanSecond(WindowState newWindow,
873 WindowState existingWindow) {
874 final int type1 = newWindow.mAttrs.type;
875 final int type2 = existingWindow.mAttrs.type;
876
877 // Base application windows should be z-ordered BELOW all other windows in the app token.
878 if (type1 == TYPE_BASE_APPLICATION && type2 != TYPE_BASE_APPLICATION) {
879 return false;
880 } else if (type1 != TYPE_BASE_APPLICATION && type2 == TYPE_BASE_APPLICATION) {
881 return true;
882 }
883
884 // Starting windows should be z-ordered ABOVE all other windows in the app token.
885 if (type1 == TYPE_APPLICATION_STARTING && type2 != TYPE_APPLICATION_STARTING) {
886 return true;
887 } else if (type1 != TYPE_APPLICATION_STARTING && type2 == TYPE_APPLICATION_STARTING) {
888 return false;
889 }
890
891 // Otherwise the new window is greater than the existing window.
892 return true;
893 }
894
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700895 @Override
Robert Carra1eb4392015-12-10 12:43:51 -0800896 void addWindow(WindowState w) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700897 super.addWindow(w);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700898
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700899 boolean gotReplacementWindow = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700900 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700901 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700902 gotReplacementWindow |= candidate.setReplacementWindowIfNeeded(w);
903 }
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700904
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700905 // if we got a replacement window, reset the timeout to give drawing more time
906 if (gotReplacementWindow) {
907 mService.scheduleWindowReplacementTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -0800908 }
Jorim Jaggife762342016-10-13 14:33:27 +0200909 checkKeyguardFlagsChanged();
910 }
911
912 @Override
913 void removeChild(WindowState child) {
914 super.removeChild(child);
915 checkKeyguardFlagsChanged();
Robert Carra1eb4392015-12-10 12:43:51 -0800916 }
917
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700918 private boolean waitingForReplacement() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700919 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700920 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700921 if (candidate.waitingForReplacement()) {
Robert Carra1eb4392015-12-10 12:43:51 -0800922 return true;
923 }
924 }
925 return false;
926 }
927
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700928 void onWindowReplacementTimeout() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700929 for (int i = mChildren.size() - 1; i >= 0; --i) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700930 (mChildren.get(i)).onWindowReplacementTimeout();
Robert Carra1eb4392015-12-10 12:43:51 -0800931 }
932 }
933
Winson Chung30480042017-01-26 10:55:34 -0800934 void reparent(Task task, int position) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800935 final Task currentTask = getTask();
936 if (task == currentTask) {
Winson Chung30480042017-01-26 10:55:34 -0800937 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800938 "window token=" + this + " already child of task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -0800939 }
Bryce Lee6d410262017-02-28 15:30:17 -0800940
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800941 if (currentTask.mStack != task.mStack) {
Bryce Lee6d410262017-02-28 15:30:17 -0800942 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800943 "window token=" + this + " current task=" + currentTask
Bryce Lee6d410262017-02-28 15:30:17 -0800944 + " belongs to a different stack than " + task);
945 }
946
Winson Chung30480042017-01-26 10:55:34 -0800947 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "reParentWindowToken: removing window token=" + this
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800948 + " from task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -0800949 final DisplayContent prevDisplayContent = getDisplayContent();
950
Bryce Lee6d410262017-02-28 15:30:17 -0800951 mReparenting = true;
952
Winson Chung30480042017-01-26 10:55:34 -0800953 getParent().removeChild(this);
954 task.addChild(this, position);
955
Bryce Lee6d410262017-02-28 15:30:17 -0800956 mReparenting = false;
957
Winson Chung30480042017-01-26 10:55:34 -0800958 // Relayout display(s).
959 final DisplayContent displayContent = task.getDisplayContent();
960 displayContent.setLayoutNeeded();
961 if (prevDisplayContent != displayContent) {
962 onDisplayChanged(displayContent);
963 prevDisplayContent.setLayoutNeeded();
964 }
965 }
966
Jorim Jaggi0429f352015-12-22 16:29:16 +0100967 /**
968 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
969 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
970 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
971 * with a queue.
972 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800973 private void freezeBounds() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800974 final Task task = getTask();
975 mFrozenBounds.offer(new Rect(task.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700976
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800977 if (task.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700978 // We didn't call prepareFreezingBounds on the task, so use the current value.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800979 mFrozenMergedConfig.offer(new Configuration(task.getConfiguration()));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700980 } else {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800981 mFrozenMergedConfig.offer(new Configuration(task.mPreparedFrozenMergedConfig));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700982 }
Andrii Kulianb10330d2016-09-16 13:51:46 -0700983 // Calling unset() to make it equal to Configuration.EMPTY.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800984 task.mPreparedFrozenMergedConfig.unset();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100985 }
986
987 /**
988 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
989 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800990 private void unfreezeBounds() {
Robert Carr68375192017-06-13 12:41:53 -0700991 if (mFrozenBounds.isEmpty()) {
992 return;
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700993 }
Robert Carr68375192017-06-13 12:41:53 -0700994 mFrozenBounds.remove();
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700995 if (!mFrozenMergedConfig.isEmpty()) {
996 mFrozenMergedConfig.remove();
997 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700998 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700999 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001000 win.onUnfreezeBounds();
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001001 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001002 mService.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +01001003 }
1004
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001005 void setAppLayoutChanges(int changes, String reason) {
1006 if (!mChildren.isEmpty()) {
1007 final DisplayContent dc = getDisplayContent();
1008 dc.pendingLayoutChanges |= changes;
1009 if (DEBUG_LAYOUT_REPEATS) {
1010 mService.mWindowPlacerLocked.debugLayoutRepeats(reason, dc.pendingLayoutChanges);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001011 }
1012 }
1013 }
1014
1015 void removeReplacedWindowIfNeeded(WindowState replacement) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001016 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001017 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001018 if (win.removeReplacedWindowIfNeeded(replacement)) {
1019 return;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001020 }
1021 }
1022 }
1023
1024 void startFreezingScreen() {
1025 if (DEBUG_ORIENTATION) logWithStack(TAG, "Set freezing of " + appToken + ": hidden="
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001026 + isHidden() + " freezing=" + mFreezingScreen + " hiddenRequested="
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001027 + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001028 if (!hiddenRequested) {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001029 if (!mFreezingScreen) {
1030 mFreezingScreen = true;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001031 mService.registerAppFreezeListener(this);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001032 mService.mAppsFreezingScreen++;
1033 if (mService.mAppsFreezingScreen == 1) {
Bryce Lee8f853582017-06-05 17:25:59 -07001034 mService.startFreezingDisplayLocked(false, 0, 0, getDisplayContent());
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001035 mService.mH.removeMessages(H.APP_FREEZE_TIMEOUT);
1036 mService.mH.sendEmptyMessageDelayed(H.APP_FREEZE_TIMEOUT, 2000);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001037 }
1038 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001039 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001040 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001041 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001042 w.onStartFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001043 }
1044 }
1045 }
1046
1047 void stopFreezingScreen(boolean unfreezeSurfaceNow, boolean force) {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001048 if (!mFreezingScreen) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001049 return;
1050 }
1051 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Clear freezing of " + this + " force=" + force);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001052 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001053 boolean unfrozeWindows = false;
1054 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001055 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001056 unfrozeWindows |= w.onStopFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001057 }
1058 if (force || unfrozeWindows) {
1059 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "No longer freezing: " + this);
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001060 mFreezingScreen = false;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001061 mService.unregisterAppFreezeListener(this);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001062 mService.mAppsFreezingScreen--;
1063 mService.mLastFinishedFreezeSource = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001064 }
1065 if (unfreezeSurfaceNow) {
1066 if (unfrozeWindows) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001067 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001068 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001069 mService.stopFreezingDisplayLocked();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001070 }
1071 }
1072
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001073 @Override
1074 public void onAppFreezeTimeout() {
1075 Slog.w(TAG_WM, "Force clearing freeze: " + this);
1076 stopFreezingScreen(true, true);
1077 }
1078
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001079 boolean transferStartingWindow(IBinder transferFrom) {
Wale Ogunwale02319a62016-09-26 15:21:22 -07001080 final AppWindowToken fromToken = getDisplayContent().getAppWindowToken(transferFrom);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001081 if (fromToken == null) {
1082 return false;
1083 }
1084
1085 final WindowState tStartingWindow = fromToken.startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001086 if (tStartingWindow != null && fromToken.startingSurface != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001087 // In this case, the starting icon has already been displayed, so start
1088 // letting windows get shown immediately without any more transitions.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001089 mService.mSkipAppTransitionAnimation = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001090
1091 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Moving existing starting " + tStartingWindow
1092 + " from " + fromToken + " to " + this);
1093
1094 final long origId = Binder.clearCallingIdentity();
Peter Visontay3556a3b2017-11-01 17:23:17 +00001095 try {
1096 // Transfer the starting window over to the new token.
1097 startingData = fromToken.startingData;
1098 startingSurface = fromToken.startingSurface;
1099 startingDisplayed = fromToken.startingDisplayed;
1100 fromToken.startingDisplayed = false;
1101 startingWindow = tStartingWindow;
1102 reportedVisible = fromToken.reportedVisible;
1103 fromToken.startingData = null;
1104 fromToken.startingSurface = null;
1105 fromToken.startingWindow = null;
1106 fromToken.startingMoved = true;
1107 tStartingWindow.mToken = this;
1108 tStartingWindow.mAppToken = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001109
Peter Visontay3556a3b2017-11-01 17:23:17 +00001110 if (DEBUG_ADD_REMOVE || DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1111 "Removing starting " + tStartingWindow + " from " + fromToken);
1112 fromToken.removeChild(tStartingWindow);
1113 fromToken.postWindowRemoveStartingWindowCleanup(tStartingWindow);
1114 fromToken.mHiddenSetFromTransferredStartingWindow = false;
1115 addWindow(tStartingWindow);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001116
Peter Visontay3556a3b2017-11-01 17:23:17 +00001117 // Propagate other interesting state between the tokens. If the old token is displayed,
1118 // we should immediately force the new one to be displayed. If it is animating, we need
1119 // to move that animation to the new one.
1120 if (fromToken.allDrawn) {
1121 allDrawn = true;
1122 deferClearAllDrawn = fromToken.deferClearAllDrawn;
1123 }
1124 if (fromToken.firstWindowDrawn) {
1125 firstWindowDrawn = true;
1126 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001127 if (!fromToken.isHidden()) {
1128 setHidden(false);
Peter Visontay3556a3b2017-11-01 17:23:17 +00001129 hiddenRequested = false;
1130 mHiddenSetFromTransferredStartingWindow = true;
1131 }
1132 setClientHidden(fromToken.mClientHidden);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001133
Jorim Jaggi980c9de2017-11-17 01:41:37 +01001134 transferAnimation(fromToken);
1135
1136 // When transferring an animation, we no longer need to apply an animation to the
1137 // the token we transfer the animation over. Thus, remove the animation from
1138 // pending opening apps.
1139 mService.mOpeningApps.remove(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001140
Peter Visontay3556a3b2017-11-01 17:23:17 +00001141 mService.updateFocusedWindowLocked(
1142 UPDATE_FOCUS_WILL_PLACE_SURFACES, true /*updateInputWindows*/);
1143 getDisplayContent().setLayoutNeeded();
1144 mService.mWindowPlacerLocked.performSurfacePlacement();
1145 } finally {
1146 Binder.restoreCallingIdentity(origId);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001147 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001148 return true;
1149 } else if (fromToken.startingData != null) {
1150 // The previous app was getting ready to show a
1151 // starting window, but hasn't yet done so. Steal it!
1152 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1153 "Moving pending starting from " + fromToken + " to " + this);
1154 startingData = fromToken.startingData;
1155 fromToken.startingData = null;
1156 fromToken.startingMoved = true;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001157 if (getController() != null) {
1158 getController().scheduleAddStartingWindow();
1159 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001160 return true;
1161 }
1162
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001163 // TODO: Transfer thumbnail
1164
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001165 return false;
1166 }
1167
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001168 boolean isLastWindow(WindowState win) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001169 return mChildren.size() == 1 && mChildren.get(0) == win;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001170 }
1171
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001172 @Override
1173 void onAppTransitionDone() {
1174 sendingToBottom = false;
1175 }
1176
Wale Ogunwale51362492016-09-08 17:49:17 -07001177 /**
1178 * We override because this class doesn't want its children affecting its reported orientation
1179 * in anyway.
1180 */
1181 @Override
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001182 int getOrientation(int candidate) {
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001183 if (candidate == SCREEN_ORIENTATION_BEHIND) {
1184 // Allow app to specify orientation regardless of its visibility state if the current
1185 // candidate want us to use orientation behind. I.e. the visible app on-top of this one
1186 // wants us to use the orientation of the app behind it.
1187 return mOrientation;
1188 }
1189
Bryce Lee61fbcbc2017-03-10 14:14:03 -08001190 // The {@link AppWindowToken} should only specify an orientation when it is not closing or
1191 // going to the bottom. Allowing closing {@link AppWindowToken} to participate can lead to
1192 // an Activity in another task being started in the wrong orientation during the transition.
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001193 if (!(sendingToBottom || mService.mClosingApps.contains(this))
Bryce Lee00d586d2017-07-28 20:48:43 -07001194 && (isVisible() || mService.mOpeningApps.contains(this) || isOnTop())) {
Bryce Leea163b762017-01-24 11:05:01 -08001195 return mOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001196 }
Bryce Leea163b762017-01-24 11:05:01 -08001197
1198 return SCREEN_ORIENTATION_UNSET;
Wale Ogunwale51362492016-09-08 17:49:17 -07001199 }
1200
Wale Ogunwaleb198b742016-12-01 08:44:09 -08001201 /** Returns the app's preferred orientation regardless of its currently visibility state. */
1202 int getOrientationIgnoreVisibility() {
1203 return mOrientation;
1204 }
1205
Craig Mautnerdbb79912012-03-01 18:59:14 -08001206 @Override
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001207 void checkAppWindowsReadyToShow() {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001208 if (allDrawn == mLastAllDrawn) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001209 return;
1210 }
1211
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001212 mLastAllDrawn = allDrawn;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001213 if (!allDrawn) {
1214 return;
1215 }
1216
1217 // The token has now changed state to having all windows shown... what to do, what to do?
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001218 if (mFreezingScreen) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001219 showAllWindowsLocked();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001220 stopFreezingScreen(false, true);
1221 if (DEBUG_ORIENTATION) Slog.i(TAG,
1222 "Setting mOrientationChangeComplete=true because wtoken " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001223 + " numInteresting=" + mNumInterestingWindows + " numDrawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001224 // This will set mOrientationChangeComplete and cause a pass through layout.
1225 setAppLayoutChanges(FINISH_LAYOUT_REDO_WALLPAPER,
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001226 "checkAppWindowsReadyToShow: freezingScreen");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001227 } else {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001228 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM, "checkAppWindowsReadyToShow");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001229
1230 // We can now show all of the drawn windows!
1231 if (!mService.mOpeningApps.contains(this)) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001232 showAllWindowsLocked();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001233 }
1234 }
1235 }
1236
Matthew Ng5d23afa2017-06-21 16:16:24 -07001237 /**
Bryce Leed390deb2017-06-22 13:14:28 -07001238 * Returns whether the drawn window states of this {@link AppWindowToken} has considered every
1239 * child {@link WindowState}. A child is considered if it has been passed into
1240 * {@link #updateDrawnWindowStates(WindowState)} after being added. This is used to determine
1241 * whether states, such as {@code allDrawn}, can be set, which relies on state variables such as
1242 * {@code mNumInterestingWindows}, which depend on all {@link WindowState}s being considered.
1243 *
1244 * @return {@code true} If all children have been considered, {@code false}.
1245 */
1246 private boolean allDrawnStatesConsidered() {
Bryce Lee6311c4b2017-07-06 14:09:29 -07001247 for (int i = mChildren.size() - 1; i >= 0; --i) {
1248 final WindowState child = mChildren.get(i);
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001249 if (child.mightAffectAllDrawn() && !child.getDrawnStateEvaluated()) {
Bryce Leed390deb2017-06-22 13:14:28 -07001250 return false;
1251 }
1252 }
1253 return true;
1254 }
1255
1256 /**
Matthew Ng5d23afa2017-06-21 16:16:24 -07001257 * Determines if the token has finished drawing. This should only be called from
1258 * {@link DisplayContent#applySurfaceChangesTransaction}
1259 */
Matthew Ng498c71d2017-04-18 13:55:45 -07001260 void updateAllDrawn() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001261 if (!allDrawn) {
Matthew Ng498c71d2017-04-18 13:55:45 -07001262 // Number of drawn windows can be less when a window is being relaunched, wait for
Bryce Leed390deb2017-06-22 13:14:28 -07001263 // all windows to be launched and drawn for this token be considered all drawn.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001264 final int numInteresting = mNumInterestingWindows;
Bryce Leed390deb2017-06-22 13:14:28 -07001265
1266 // We must make sure that all present children have been considered (determined by
1267 // {@link #allDrawnStatesConsidered}) before evaluating whether everything has been
1268 // drawn.
1269 if (numInteresting > 0 && allDrawnStatesConsidered()
1270 && mNumDrawnWindows >= numInteresting && !isRelaunching()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001271 if (DEBUG_VISIBILITY) Slog.v(TAG, "allDrawn: " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001272 + " interesting=" + numInteresting + " drawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001273 allDrawn = true;
1274 // Force an additional layout pass where
1275 // WindowStateAnimator#commitFinishDrawingLocked() will call performShowLocked().
Matthew Ng498c71d2017-04-18 13:55:45 -07001276 if (mDisplayContent != null) {
1277 mDisplayContent.setLayoutNeeded();
1278 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001279 mService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
Robert Carrecc06b32017-04-18 14:25:10 -07001280
Winson Chunge7ba6862017-05-24 12:13:33 -07001281 // Notify the pinned stack upon all windows drawn. If there was an animation in
1282 // progress then this signal will resume that animation.
Wale Ogunwale61911492017-10-11 08:50:50 -07001283 final TaskStack pinnedStack = mDisplayContent.getPinnedStack();
Winson Chunge7ba6862017-05-24 12:13:33 -07001284 if (pinnedStack != null) {
1285 pinnedStack.onAllWindowsDrawn();
Robert Carrecc06b32017-04-18 14:25:10 -07001286 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001287 }
1288 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001289 }
1290
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001291 /**
1292 * Updated this app token tracking states for interesting and drawn windows based on the window.
1293 *
1294 * @return Returns true if the input window is considered interesting and drawn while all the
1295 * windows in this app token where not considered drawn as of the last pass.
1296 */
1297 boolean updateDrawnWindowStates(WindowState w) {
Bryce Leed390deb2017-06-22 13:14:28 -07001298 w.setDrawnStateEvaluated(true /*evaluated*/);
1299
Jorim Jaggie4b0f282017-05-17 15:10:29 +02001300 if (DEBUG_STARTING_WINDOW_VERBOSE && w == startingWindow) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001301 Slog.d(TAG, "updateWindows: starting " + w + " isOnScreen=" + w.isOnScreen()
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001302 + " allDrawn=" + allDrawn + " freezingScreen=" + mFreezingScreen);
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001303 }
1304
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001305 if (allDrawn && !mFreezingScreen) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001306 return false;
1307 }
1308
1309 if (mLastTransactionSequence != mService.mTransactionSequence) {
1310 mLastTransactionSequence = mService.mTransactionSequence;
1311 mNumInterestingWindows = mNumDrawnWindows = 0;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001312 startingDisplayed = false;
1313 }
1314
1315 final WindowStateAnimator winAnimator = w.mWinAnimator;
1316
1317 boolean isInterestingAndDrawn = false;
1318
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001319 if (!allDrawn && w.mightAffectAllDrawn()) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001320 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
1321 Slog.v(TAG, "Eval win " + w + ": isDrawn=" + w.isDrawnLw()
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001322 + ", isAnimationSet=" + isSelfAnimating());
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001323 if (!w.isDrawnLw()) {
1324 Slog.v(TAG, "Not displayed: s=" + winAnimator.mSurfaceController
1325 + " pv=" + w.mPolicyVisibility
1326 + " mDrawState=" + winAnimator.drawStateToString()
1327 + " ph=" + w.isParentWindowHidden() + " th=" + hiddenRequested
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001328 + " a=" + isSelfAnimating());
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001329 }
1330 }
1331
1332 if (w != startingWindow) {
1333 if (w.isInteresting()) {
1334 mNumInterestingWindows++;
1335 if (w.isDrawnLw()) {
1336 mNumDrawnWindows++;
1337
1338 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG, "tokenMayBeDrawn: "
1339 + this + " w=" + w + " numInteresting=" + mNumInterestingWindows
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001340 + " freezingScreen=" + mFreezingScreen
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001341 + " mAppFreezing=" + w.mAppFreezing);
1342
1343 isInterestingAndDrawn = true;
1344 }
1345 }
1346 } else if (w.isDrawnLw()) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001347 if (getController() != null) {
1348 getController().reportStartingWindowDrawn();
1349 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001350 startingDisplayed = true;
1351 }
1352 }
1353
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001354 return isInterestingAndDrawn;
1355 }
1356
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001357 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001358 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001359 // For legacy reasons we process the TaskStack.mExitingAppTokens first in DisplayContent
1360 // before the non-exiting app tokens. So, we skip the exiting app tokens here.
1361 // TODO: Investigate if we need to continue to do this or if we can just process them
1362 // in-order.
1363 if (mIsExiting && !waitingForReplacement()) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001364 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001365 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001366 return forAllWindowsUnchecked(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001367 }
1368
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001369 boolean forAllWindowsUnchecked(ToBooleanFunction<WindowState> callback,
1370 boolean traverseTopToBottom) {
1371 return super.forAllWindows(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001372 }
1373
1374 @Override
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001375 AppWindowToken asAppWindowToken() {
1376 // I am an app window token!
1377 return this;
1378 }
1379
1380 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001381 boolean fillsParent() {
1382 return mFillsParent;
1383 }
1384
1385 void setFillsParent(boolean fillsParent) {
1386 mFillsParent = fillsParent;
1387 }
1388
Jorim Jaggife762342016-10-13 14:33:27 +02001389 boolean containsDismissKeyguardWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001390 // Window state is transient during relaunch. We are not guaranteed to be frozen during the
1391 // entirety of the relaunch.
1392 if (isRelaunching()) {
1393 return mLastContainsDismissKeyguardWindow;
1394 }
1395
Jorim Jaggife762342016-10-13 14:33:27 +02001396 for (int i = mChildren.size() - 1; i >= 0; i--) {
1397 if ((mChildren.get(i).mAttrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
1398 return true;
1399 }
1400 }
1401 return false;
1402 }
1403
1404 boolean containsShowWhenLockedWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001405 // When we are relaunching, it is possible for us to be unfrozen before our previous
1406 // windows have been added back. Using the cached value ensures that our previous
1407 // showWhenLocked preference is honored until relaunching is complete.
1408 if (isRelaunching()) {
1409 return mLastContainsShowWhenLockedWindow;
1410 }
1411
Jorim Jaggife762342016-10-13 14:33:27 +02001412 for (int i = mChildren.size() - 1; i >= 0; i--) {
1413 if ((mChildren.get(i).mAttrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
1414 return true;
1415 }
1416 }
Bryce Lee081554b2017-05-25 07:52:12 -07001417
Jorim Jaggife762342016-10-13 14:33:27 +02001418 return false;
1419 }
1420
1421 void checkKeyguardFlagsChanged() {
1422 final boolean containsDismissKeyguard = containsDismissKeyguardWindow();
1423 final boolean containsShowWhenLocked = containsShowWhenLockedWindow();
1424 if (containsDismissKeyguard != mLastContainsDismissKeyguardWindow
1425 || containsShowWhenLocked != mLastContainsShowWhenLockedWindow) {
1426 mService.notifyKeyguardFlagsChanged(null /* callback */);
1427 }
1428 mLastContainsDismissKeyguardWindow = containsDismissKeyguard;
1429 mLastContainsShowWhenLockedWindow = containsShowWhenLocked;
1430 }
1431
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001432 WindowState getImeTargetBelowWindow(WindowState w) {
1433 final int index = mChildren.indexOf(w);
1434 if (index > 0) {
1435 final WindowState target = mChildren.get(index - 1);
1436 if (target.canBeImeTarget()) {
1437 return target;
1438 }
1439 }
1440 return null;
1441 }
1442
Winson Chungbe9be7f2017-06-28 10:55:22 -07001443 int getLowestAnimLayer() {
1444 for (int i = 0; i < mChildren.size(); i++) {
1445 final WindowState w = mChildren.get(i);
1446 if (w.mRemoved) {
1447 continue;
1448 }
1449 return w.mWinAnimator.mAnimLayer;
1450 }
1451 return Integer.MAX_VALUE;
1452 }
1453
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001454 WindowState getHighestAnimLayerWindow(WindowState currentTarget) {
1455 WindowState candidate = null;
1456 for (int i = mChildren.indexOf(currentTarget); i >= 0; i--) {
1457 final WindowState w = mChildren.get(i);
1458 if (w.mRemoved) {
1459 continue;
1460 }
1461 if (candidate == null || w.mWinAnimator.mAnimLayer >
1462 candidate.mWinAnimator.mAnimLayer) {
1463 candidate = w;
1464 }
1465 }
1466 return candidate;
1467 }
1468
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001469 /**
1470 * See {@link Activity#setDisablePreviewScreenshots}.
1471 */
1472 void setDisablePreviewScreenshots(boolean disable) {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001473 mDisablePreviewScreenshots = disable;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001474 }
1475
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001476 /**
chaviwd3bf08d2017-08-01 17:24:59 -07001477 * Sets whether the current launch can turn the screen on. See {@link #canTurnScreenOn()}
1478 */
1479 void setCanTurnScreenOn(boolean canTurnScreenOn) {
1480 mCanTurnScreenOn = canTurnScreenOn;
1481 }
1482
1483 /**
1484 * Indicates whether the current launch can turn the screen on. This is to prevent multiple
1485 * relayouts from turning the screen back on. The screen should only turn on at most
1486 * once per activity resume.
1487 *
1488 * @return true if the screen can be turned on.
1489 */
1490 boolean canTurnScreenOn() {
1491 return mCanTurnScreenOn;
1492 }
1493
1494 /**
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001495 * Retrieves whether we'd like to generate a snapshot that's based solely on the theme. This is
1496 * the case when preview screenshots are disabled {@link #setDisablePreviewScreenshots} or when
1497 * we can't take a snapshot for other reasons, for example, if we have a secure window.
1498 *
1499 * @return True if we need to generate an app theme snapshot, false if we'd like to take a real
1500 * screenshot.
1501 */
1502 boolean shouldUseAppThemeSnapshot() {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001503 return mDisablePreviewScreenshots || forAllWindows(w -> (w.mAttrs.flags & FLAG_SECURE) != 0,
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001504 true /* topToBottom */);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001505 }
1506
chaviw23ee71c2017-12-18 11:29:41 -08001507 @Override
1508 public SurfaceControl.Builder makeAnimationLeash() {
1509 return super.makeAnimationLeash()
1510 .setParent(getAppAnimationLayer());
1511 }
1512
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001513 boolean applyAnimationLocked(WindowManager.LayoutParams lp, int transit, boolean enter,
1514 boolean isVoiceInteraction) {
1515
1516 if (mService.mDisableTransitionAnimation) {
1517 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) {
1518 Slog.v(TAG_WM, "applyAnimation: transition animation is disabled. atoken=" + this);
1519 }
1520 cancelAnimation();
1521 return false;
1522 }
1523
1524 // Only apply an animation if the display isn't frozen. If it is frozen, there is no reason
1525 // to animate and it can cause strange artifacts when we unfreeze the display if some
1526 // different animation is running.
1527 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "AWT#applyAnimationLocked");
1528 if (okToAnimate()) {
1529 final Animation a = loadAnimation(lp, transit, enter, isVoiceInteraction);
1530 if (a != null) {
chaviw23ee71c2017-12-18 11:29:41 -08001531 final TaskStack stack = getStack();
1532 mTmpPoint.set(0, 0);
1533 if (stack != null) {
1534 stack.getRelativePosition(mTmpPoint);
1535 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001536 final AnimationAdapter adapter = new LocalAnimationAdapter(
chaviw23ee71c2017-12-18 11:29:41 -08001537 new WindowAnimationSpec(a, mTmpPoint,
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +01001538 mService.mAppTransition.canSkipFirstFrame()),
1539 mService.mSurfaceAnimationRunner);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001540 startAnimation(getPendingTransaction(), adapter, !isVisible());
1541 if (a.getZAdjustment() == Animation.ZORDER_TOP) {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001542 mNeedsZBoost = true;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001543 getDisplayContent().assignWindowLayers(false /* setLayoutNeeded */);
1544 }
1545 mTransit = transit;
1546 mTransitFlags = mService.mAppTransition.getTransitFlags();
1547 // TODO: Skip first frame and app stack clip mode.
1548 }
1549 } else {
1550 cancelAnimation();
1551 }
1552 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
1553
1554 return isReallyAnimating();
1555 }
1556
1557 private Animation loadAnimation(WindowManager.LayoutParams lp, int transit, boolean enter,
1558 boolean isVoiceInteraction) {
1559 final DisplayContent displayContent = getTask().getDisplayContent();
1560 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
1561 final int width = displayInfo.appWidth;
1562 final int height = displayInfo.appHeight;
1563 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.v(TAG_WM,
1564 "applyAnimation: atoken=" + this);
1565
1566 // Determine the visible rect to calculate the thumbnail clip
1567 final WindowState win = findMainWindow();
1568 final Rect frame = new Rect(0, 0, width, height);
1569 final Rect displayFrame = new Rect(0, 0,
1570 displayInfo.logicalWidth, displayInfo.logicalHeight);
1571 final Rect insets = new Rect();
1572 final Rect stableInsets = new Rect();
1573 Rect surfaceInsets = null;
1574 final boolean freeform = win != null && win.inFreeformWindowingMode();
1575 if (win != null) {
1576 // Containing frame will usually cover the whole screen, including dialog windows.
1577 // For freeform workspace windows it will not cover the whole screen and it also
1578 // won't exactly match the final freeform window frame (e.g. when overlapping with
1579 // the status bar). In that case we need to use the final frame.
1580 if (freeform) {
1581 frame.set(win.mFrame);
1582 } else {
1583 frame.set(win.mContainingFrame);
1584 }
1585 surfaceInsets = win.getAttrs().surfaceInsets;
1586 insets.set(win.mContentInsets);
1587 stableInsets.set(win.mStableInsets);
1588 }
1589
1590 if (mLaunchTaskBehind) {
1591 // Differentiate the two animations. This one which is briefly on the screen
1592 // gets the !enter animation, and the other activity which remains on the
1593 // screen gets the enter animation. Both appear in the mOpeningApps set.
1594 enter = false;
1595 }
1596 if (DEBUG_APP_TRANSITIONS) Slog.d(TAG_WM, "Loading animation for app transition."
1597 + " transit=" + AppTransition.appTransitionToString(transit) + " enter=" + enter
1598 + " frame=" + frame + " insets=" + insets + " surfaceInsets=" + surfaceInsets);
1599 final Configuration displayConfig = displayContent.getConfiguration();
1600 final Animation a = mService.mAppTransition.loadAnimation(lp, transit, enter,
1601 displayConfig.uiMode, displayConfig.orientation, frame, displayFrame, insets,
1602 surfaceInsets, stableInsets, isVoiceInteraction, freeform, getTask().mTaskId);
1603 if (a != null) {
1604 if (DEBUG_ANIM) logWithStack(TAG, "Loaded animation " + a + " for " + this);
1605 final int containingWidth = frame.width();
1606 final int containingHeight = frame.height();
1607 a.initialize(containingWidth, containingHeight, width, height);
1608 a.scaleCurrentDuration(mService.getTransitionAnimationScaleLocked());
1609 }
1610 return a;
1611 }
1612
1613 /**
1614 * This must be called while inside a transaction.
1615 */
1616 void showAllWindowsLocked() {
1617 forAllWindows(windowState -> {
1618 if (DEBUG_VISIBILITY) Slog.v(TAG, "performing show on: " + windowState);
1619 windowState.performShowLocked();
1620 }, false /* traverseTopToBottom */);
Jorim Jaggia5e10572017-11-15 14:36:26 +01001621 }
1622
1623 @Override
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001624 protected void onAnimationFinished() {
1625 super.onAnimationFinished();
1626
1627 mTransit = TRANSIT_UNSET;
1628 mTransitFlags = 0;
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001629 mNeedsZBoost = false;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001630
1631 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM | FINISH_LAYOUT_REDO_WALLPAPER,
1632 "AppWindowToken");
1633
Jorim Jaggi988f6682017-11-17 17:46:43 +01001634 clearThumbnail();
1635
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001636 if (mService.mInputMethodTarget != null && mService.mInputMethodTarget.mAppToken == this) {
1637 getDisplayContent().computeImeTarget(true /* updateImeTarget */);
1638 }
1639
1640 if (DEBUG_ANIM) Slog.v(TAG, "Animation done in " + this
1641 + ": reportedVisible=" + reportedVisible
1642 + " okToDisplay=" + okToDisplay()
1643 + " okToAnimate=" + okToAnimate()
1644 + " startingDisplayed=" + startingDisplayed);
1645
1646 // WindowState.onExitAnimationDone might modify the children list, so make a copy and then
1647 // traverse the copy.
1648 final ArrayList<WindowState> children = new ArrayList<>(mChildren);
1649 children.forEach(WindowState::onExitAnimationDone);
1650
1651 mService.mAppTransition.notifyAppTransitionFinishedLocked(token);
1652 scheduleAnimation();
1653 }
1654
1655 @Override
1656 boolean isAppAnimating() {
1657 return isSelfAnimating();
1658 }
1659
1660 @Override
1661 boolean isSelfAnimating() {
1662 // If we are about to start a transition, we also need to be considered animating.
1663 return isWaitingForTransitionStart() || isReallyAnimating();
1664 }
1665
1666 /**
1667 * @return True if and only if we are actually running an animation. Note that
1668 * {@link #isSelfAnimating} also returns true if we are waiting for an animation to
1669 * start.
1670 */
1671 private boolean isReallyAnimating() {
1672 return super.isSelfAnimating();
1673 }
1674
Jorim Jaggi988f6682017-11-17 17:46:43 +01001675 @Override
1676 void cancelAnimation() {
1677 super.cancelAnimation();
1678 clearThumbnail();
1679 }
1680
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001681 boolean isWaitingForTransitionStart() {
1682 return mService.mAppTransition.isTransitionSet()
1683 && (mService.mOpeningApps.contains(this) || mService.mClosingApps.contains(this));
1684 }
1685
1686 public int getTransit() {
1687 return mTransit;
1688 }
1689
1690 int getTransitFlags() {
1691 return mTransitFlags;
1692 }
1693
Jorim Jaggi988f6682017-11-17 17:46:43 +01001694 void attachThumbnailAnimation() {
1695 if (!isReallyAnimating()) {
1696 return;
1697 }
1698 final int taskId = getTask().mTaskId;
1699 final GraphicBuffer thumbnailHeader =
1700 mService.mAppTransition.getAppTransitionThumbnailHeader(taskId);
1701 if (thumbnailHeader == null) {
1702 if (DEBUG_APP_TRANSITIONS) Slog.d(TAG, "No thumbnail header bitmap for: " + taskId);
1703 return;
1704 }
1705 clearThumbnail();
1706 mThumbnail = new AppWindowThumbnail(getPendingTransaction(), this, thumbnailHeader);
1707 mThumbnail.startAnimation(getPendingTransaction(), loadThumbnailAnimation(thumbnailHeader));
1708 }
1709
1710 private Animation loadThumbnailAnimation(GraphicBuffer thumbnailHeader) {
1711 final DisplayInfo displayInfo = mDisplayContent.getDisplayInfo();
1712
1713 // If this is a multi-window scenario, we use the windows frame as
1714 // destination of the thumbnail header animation. If this is a full screen
1715 // window scenario, we use the whole display as the target.
1716 WindowState win = findMainWindow();
1717 Rect appRect = win != null ? win.getContentFrameLw() :
1718 new Rect(0, 0, displayInfo.appWidth, displayInfo.appHeight);
1719 Rect insets = win != null ? win.mContentInsets : null;
1720 final Configuration displayConfig = mDisplayContent.getConfiguration();
1721 return mService.mAppTransition.createThumbnailAspectScaleAnimationLocked(
1722 appRect, insets, thumbnailHeader, getTask().mTaskId, displayConfig.uiMode,
1723 displayConfig.orientation);
1724 }
1725
1726 private void clearThumbnail() {
1727 if (mThumbnail == null) {
1728 return;
1729 }
1730 mThumbnail.destroy();
1731 mThumbnail = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001732 }
1733
1734 @Override
1735 void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1736 super.dump(pw, prefix, dumpAll);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001737 if (appToken != null) {
Wale Ogunwale72919d22016-12-08 18:58:50 -08001738 pw.println(prefix + "app=true mVoiceInteraction=" + mVoiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001739 }
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001740 pw.print(prefix); pw.print("task="); pw.println(getTask());
Wale Ogunwale51362492016-09-08 17:49:17 -07001741 pw.print(prefix); pw.print(" mFillsParent="); pw.print(mFillsParent);
1742 pw.print(" mOrientation="); pw.println(mOrientation);
Wale Ogunwale89973222017-04-23 18:39:45 -07001743 pw.println(prefix + "hiddenRequested=" + hiddenRequested + " mClientHidden=" + mClientHidden
1744 + ((mDeferHidingClient) ? " mDeferHidingClient=" + mDeferHidingClient : "")
1745 + " reportedDrawn=" + reportedDrawn + " reportedVisible=" + reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -07001746 if (paused) {
1747 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001748 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -08001749 if (mAppStopped) {
1750 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
1751 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001752 if (mNumInterestingWindows != 0 || mNumDrawnWindows != 0
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001753 || allDrawn || mLastAllDrawn) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001754 pw.print(prefix); pw.print("mNumInterestingWindows=");
1755 pw.print(mNumInterestingWindows);
1756 pw.print(" mNumDrawnWindows="); pw.print(mNumDrawnWindows);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001757 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -07001758 pw.print(" allDrawn="); pw.print(allDrawn);
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001759 pw.print(" lastAllDrawn="); pw.print(mLastAllDrawn);
Craig Mautner6fbda632012-07-03 09:26:39 -07001760 pw.println(")");
1761 }
1762 if (inPendingTransaction) {
1763 pw.print(prefix); pw.print("inPendingTransaction=");
1764 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001765 }
Craig Mautner799bc1d2015-01-14 10:33:48 -08001766 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001767 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
1768 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -08001769 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -08001770 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001771 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001772 if (startingWindow != null || startingSurface != null
Wale Ogunwale6c459212017-05-17 08:56:03 -07001773 || startingDisplayed || startingMoved || mHiddenSetFromTransferredStartingWindow) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001774 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001775 pw.print(" startingSurface="); pw.print(startingSurface);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001776 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale6c459212017-05-17 08:56:03 -07001777 pw.print(" startingMoved="); pw.print(startingMoved);
1778 pw.println(" mHiddenSetFromTransferredStartingWindow="
1779 + mHiddenSetFromTransferredStartingWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001780 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001781 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -08001782 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001783 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -08001784 }
1785 if (mPendingRelaunchCount != 0) {
1786 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +01001787 }
Wale Ogunwale9c64cb62017-04-12 13:39:59 -07001788 if (getController() != null) {
1789 pw.print(prefix); pw.print("controller="); pw.println(getController());
1790 }
Wale Ogunwalee287e192017-04-21 09:30:12 -07001791 if (mRemovingFromDisplay) {
1792 pw.println(prefix + "mRemovingFromDisplay=" + mRemovingFromDisplay);
1793 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001794 }
1795
1796 @Override
1797 void setHidden(boolean hidden) {
1798 super.setHidden(hidden);
1799 scheduleAnimation();
1800 }
1801
1802 @Override
1803 void prepareSurfaces() {
1804 // isSelfAnimating also returns true when we are about to start a transition, so we need
1805 // to check super here.
1806 final boolean reallyAnimating = super.isSelfAnimating();
1807 final boolean show = !isHidden() || reallyAnimating;
1808 if (show && !mLastSurfaceShowing) {
1809 mPendingTransaction.show(mSurfaceControl);
1810 } else if (!show && mLastSurfaceShowing) {
1811 mPendingTransaction.hide(mSurfaceControl);
Jorim Jaggi4d1835d2017-08-31 17:28:27 +02001812 }
Jorim Jaggi988f6682017-11-17 17:46:43 +01001813 if (mThumbnail != null) {
1814 mThumbnail.setShowing(mPendingTransaction, show);
1815 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001816 mLastSurfaceShowing = show;
1817 super.prepareSurfaces();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001818 }
1819
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001820 boolean isFreezingScreen() {
1821 return mFreezingScreen;
1822 }
1823
1824 @Override
1825 boolean needsZBoost() {
1826 return mNeedsZBoost || super.needsZBoost();
1827 }
1828
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001829 @CallSuper
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001830 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02001831 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07001832 final long token = proto.start(fieldId);
1833 writeNameToProto(proto, NAME);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001834 super.writeToProto(proto, WINDOW_TOKEN, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001835 proto.end(token);
1836 }
1837
1838 void writeNameToProto(ProtoOutputStream proto, long fieldId) {
1839 if (appToken == null) {
1840 return;
1841 }
1842 try {
1843 proto.write(fieldId, appToken.getName());
1844 } catch (RemoteException e) {
1845 // This shouldn't happen, but in this case fall back to outputting nothing
1846 Slog.e(TAG, e.toString());
1847 }
1848 }
1849
1850 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001851 public String toString() {
1852 if (stringName == null) {
1853 StringBuilder sb = new StringBuilder();
1854 sb.append("AppWindowToken{");
1855 sb.append(Integer.toHexString(System.identityHashCode(this)));
1856 sb.append(" token="); sb.append(token); sb.append('}');
1857 stringName = sb.toString();
1858 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001859 return stringName + ((mIsExiting) ? " mIsExiting=" : "");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001860 }
Jeff Browne9bdb312012-04-05 15:30:10 -07001861}