blob: da144e6b66ac033d69d7c5d63d9345bbcb080241 [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;
Filip Gruszczynskia590c992015-11-25 16:45:26 -080055import android.annotation.NonNull;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020056import android.app.Activity;
Jorim Jaggi26c8c422016-05-09 19:57:25 -070057import android.content.res.Configuration;
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;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070064import android.os.SystemClock;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020065import android.os.Trace;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080066import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -070067import android.util.proto.ProtoOutputStream;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020068import android.view.DisplayInfo;
69import android.view.animation.Animation;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080070import android.view.IApplicationToken;
Robert Carr6914f082017-03-20 19:04:30 -070071import android.view.SurfaceControl;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080072import android.view.WindowManager;
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +020073import android.view.WindowManager.LayoutParams;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080074
75import com.android.internal.util.ToBooleanFunction;
76import com.android.server.input.InputApplicationHandle;
Adrian Roose99bc052017-11-20 17:55:31 +010077import com.android.server.policy.WindowManagerPolicy.StartingSurface;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080078import com.android.server.wm.WindowManagerService.H;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080079
80import java.io.PrintWriter;
Jorim Jaggi0429f352015-12-22 16:29:16 +010081import java.util.ArrayDeque;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080082import java.util.ArrayList;
Jorim Jaggia5e10572017-11-15 14:36:26 +010083import java.util.LinkedList;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080084
85class AppTokenList extends ArrayList<AppWindowToken> {
86}
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080087
88/**
89 * Version of WindowToken that is specifically for a particular application (or
90 * really activity) that is displaying windows.
91 */
Wale Ogunwale3f4433d2016-08-18 20:42:42 -070092class AppWindowToken extends WindowToken implements WindowManagerService.AppFreezeListener {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080093 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowToken" : TAG_WM;
94
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080095 // Non-null only for application tokens.
96 final IApplicationToken appToken;
97
Filip Gruszczynskia590c992015-11-25 16:45:26 -080098 @NonNull final AppWindowAnimator mAppAnimator;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070099
Wale Ogunwale72919d22016-12-08 18:58:50 -0800100 final boolean mVoiceInteraction;
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700101
Wale Ogunwale51362492016-09-08 17:49:17 -0700102 /** @see WindowContainer#fillsParent() */
103 private boolean mFillsParent;
Craig Mautner4c5eb222013-11-18 12:59:05 -0800104 boolean layoutConfigChanges;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800105 boolean mShowForAllUsers;
106 int mTargetSdk;
Craig Mautnera2c77052012-03-26 12:14:43 -0700107
Bryce Lee6d410262017-02-28 15:30:17 -0800108 // Flag set while reparenting to prevent actions normally triggered by an individual parent
109 // change.
110 private boolean mReparenting;
111
Wale Ogunwalee287e192017-04-21 09:30:12 -0700112 // True if we are current in the process of removing this app token from the display
113 private boolean mRemovingFromDisplay = false;
114
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800115 // The input dispatching timeout for this application token in nanoseconds.
Wale Ogunwale72919d22016-12-08 18:58:50 -0800116 long mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800117
118 // These are used for determining when all windows associated with
119 // an activity have been drawn, so they can be made visible together
120 // at the same time.
Craig Mautner764983d2012-03-22 11:37:36 -0700121 // initialize so that it doesn't match mTransactionSequence which is an int.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700122 private long mLastTransactionSequence = Long.MIN_VALUE;
123 private int mNumInterestingWindows;
124 private int mNumDrawnWindows;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800125 boolean inPendingTransaction;
126 boolean allDrawn;
Craig Mautner7636dfb2012-11-16 15:24:11 -0800127 // Set to true when this app creates a surface while in the middle of an animation. In that
128 // case do not clear allDrawn until the animation completes.
129 boolean deferClearAllDrawn;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800130
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800131 // Is this window's surface needed? This is almost like hidden, except
132 // it will sometimes be true a little earlier: when the token has
133 // been shown, but is still waiting for its app transition to execute
134 // before making its windows shown.
135 boolean hiddenRequested;
136
137 // Have we told the window clients to hide themselves?
Wale Ogunwale89973222017-04-23 18:39:45 -0700138 private boolean mClientHidden;
139
140 // If true we will defer setting mClientHidden to true and reporting to the client that it is
141 // hidden.
142 boolean mDeferHidingClient;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800143
144 // Last visibility state we reported to the app token.
145 boolean reportedVisible;
146
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700147 // Last drawn state we reported to the app token.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700148 private boolean reportedDrawn;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700149
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800150 // Set to true when the token has been removed from the window mgr.
151 boolean removed;
152
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800153 // Information about an application starting window if displayed.
154 StartingData startingData;
155 WindowState startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800156 StartingSurface startingSurface;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800157 boolean startingDisplayed;
158 boolean startingMoved;
Wale Ogunwale6c459212017-05-17 08:56:03 -0700159 // True if the hidden state of this token was forced to false due to a transferred starting
160 // window.
161 private boolean mHiddenSetFromTransferredStartingWindow;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800162 boolean firstWindowDrawn;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700163 private final WindowState.UpdateReportedVisibilityResults mReportedVisibilityResults =
164 new WindowState.UpdateReportedVisibilityResults();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800165
166 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700167 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800168
Wale Ogunwale571771c2016-08-26 13:18:50 -0700169 // TODO: Have a WindowContainer state for tracking exiting/deferred removal.
Craig Mautner799bc1d2015-01-14 10:33:48 -0800170 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800171
Craig Mautnerbb742462014-07-07 15:28:55 -0700172 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700173 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700174
Wale Ogunwale72919d22016-12-08 18:58:50 -0800175 private boolean mAlwaysFocusable;
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800176
Robert Carre12aece2016-02-02 22:43:27 -0800177 boolean mAppStopped;
Robert Carrfd10cd12016-06-29 16:41:50 -0700178 int mRotationAnimationHint;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700179 private int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800180
Jorim Jaggife762342016-10-13 14:33:27 +0200181 private boolean mLastContainsShowWhenLockedWindow;
182 private boolean mLastContainsDismissKeyguardWindow;
183
Jorim Jaggi0429f352015-12-22 16:29:16 +0100184 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700185 ArrayDeque<Configuration> mFrozenMergedConfig = new ArrayDeque<>();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100186
Wale Ogunwale6c459212017-05-17 08:56:03 -0700187 private boolean mDisablePreviewScreenshots;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100188
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700189 private Task mLastParent;
Robert Carred3e83b2017-04-21 13:26:55 -0700190
chaviwd3bf08d2017-08-01 17:24:59 -0700191 /**
192 * See {@link #canTurnScreenOn()}
193 */
194 private boolean mCanTurnScreenOn = true;
195
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200196 /**
197 * If we are running an animation, this determines the transition type. Must be one of
198 * AppTransition.TRANSIT_* constants.
199 */
200 private int mTransit;
201
202 /**
203 * If we are running an animation, this determines the flags during this animation. Must be a
204 * bitwise combination of AppTransition.TRANSIT_FLAG_* constants.
205 */
206 private int mTransitFlags;
207
208 /** Whether our surface was set to be showing in the last call to {@link #prepareSurfaces} */
209 private boolean mLastSurfaceShowing;
210
Wale Ogunwale72919d22016-12-08 18:58:50 -0800211 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
212 DisplayContent dc, long inputDispatchingTimeoutNanos, boolean fullscreen,
213 boolean showForAllUsers, int targetSdk, int orientation, int rotationAnimationHint,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800214 int configChanges, boolean launchTaskBehind, boolean alwaysFocusable,
Bryce Leef3c6a472017-11-14 14:53:06 -0800215 AppWindowContainerController controller) {
216 this(service, token, voiceInteraction, dc, fullscreen);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800217 setController(controller);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800218 mInputDispatchingTimeoutNanos = inputDispatchingTimeoutNanos;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800219 mShowForAllUsers = showForAllUsers;
220 mTargetSdk = targetSdk;
221 mOrientation = orientation;
222 layoutConfigChanges = (configChanges & (CONFIG_SCREEN_SIZE | CONFIG_ORIENTATION)) != 0;
223 mLaunchTaskBehind = launchTaskBehind;
224 mAlwaysFocusable = alwaysFocusable;
225 mRotationAnimationHint = rotationAnimationHint;
226
227 // Application tokens start out hidden.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200228 setHidden(true);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800229 hiddenRequested = true;
230 }
231
232 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
Bryce Leef3c6a472017-11-14 14:53:06 -0800233 DisplayContent dc, boolean fillsParent) {
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800234 super(service, token != null ? token.asBinder() : null, TYPE_APPLICATION, true, dc,
235 false /* ownerCanManageAppTokens */);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700236 appToken = token;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800237 mVoiceInteraction = voiceInteraction;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800238 mFillsParent = fillsParent;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800239 mInputApplicationHandle = new InputApplicationHandle(this);
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200240 mAppAnimator = new AppWindowAnimator();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800241 }
242
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800243 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
244 firstWindowDrawn = true;
245
246 // We now have a good window to show, remove dead placeholders
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700247 removeDeadWindows();
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800248
Jorim Jaggi02886a82016-12-06 09:10:06 -0800249 if (startingWindow != null) {
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800250 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
251 + win.mToken + ": first real window is shown, no animation");
252 // If this initial window is animating, stop it -- we will do an animation to reveal
253 // it from behind the starting window, so there is no need for it to also be doing its
254 // own stuff.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100255 win.cancelAnimation();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800256 if (getController() != null) {
257 getController().removeStartingWindow();
258 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800259 }
260 updateReportedVisibilityLocked();
261 }
262
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800263 void updateReportedVisibilityLocked() {
264 if (appToken == null) {
265 return;
266 }
267
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700268 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700269 final int count = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800270
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700271 mReportedVisibilityResults.reset();
272
273 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700274 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700275 win.updateReportedVisibility(mReportedVisibilityResults);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800276 }
277
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700278 int numInteresting = mReportedVisibilityResults.numInteresting;
279 int numVisible = mReportedVisibilityResults.numVisible;
280 int numDrawn = mReportedVisibilityResults.numDrawn;
281 boolean nowGone = mReportedVisibilityResults.nowGone;
282
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700283 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200284 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting && !isHidden();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700285 if (!nowGone) {
286 // If the app is not yet gone, then it can only become visible/drawn.
287 if (!nowDrawn) {
288 nowDrawn = reportedDrawn;
289 }
290 if (!nowVisible) {
291 nowVisible = reportedVisible;
292 }
293 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800294 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800295 + numInteresting + " visible=" + numVisible);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800296 final AppWindowContainerController controller = getController();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700297 if (nowDrawn != reportedDrawn) {
298 if (nowDrawn) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800299 if (controller != null) {
300 controller.reportWindowsDrawn();
301 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700302 }
303 reportedDrawn = nowDrawn;
304 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800305 if (nowVisible != reportedVisible) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700306 if (DEBUG_VISIBILITY) Slog.v(TAG,
307 "Visibility changed in " + this + ": vis=" + nowVisible);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800308 reportedVisible = nowVisible;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800309 if (controller != null) {
310 if (nowVisible) {
311 controller.reportWindowsVisible();
312 } else {
313 controller.reportWindowsGone();
314 }
315 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800316 }
317 }
318
Wale Ogunwale89973222017-04-23 18:39:45 -0700319 boolean isClientHidden() {
320 return mClientHidden;
321 }
322
323 void setClientHidden(boolean hideClient) {
324 if (mClientHidden == hideClient || (hideClient && mDeferHidingClient)) {
325 return;
326 }
327 mClientHidden = hideClient;
328 sendAppVisibilityToClients();
329 }
330
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700331 boolean setVisibility(WindowManager.LayoutParams lp,
332 boolean visible, int transit, boolean performLayout, boolean isVoiceInteraction) {
333
334 boolean delayed = false;
335 inPendingTransaction = false;
Wale Ogunwale9e4721f2017-05-23 19:37:30 -0700336 // Reset the state of mHiddenSetFromTransferredStartingWindow since visibility is actually
337 // been set by the app now.
338 mHiddenSetFromTransferredStartingWindow = false;
Wale Ogunwale89973222017-04-23 18:39:45 -0700339 setClientHidden(!visible);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700340
341 // Allow for state changes and animation to be applied if:
342 // * token is transitioning visibility state
343 // * or the token was marked as hidden and is exiting before we had a chance to play the
344 // transition animation
345 // * or this is an opening app and windows are being replaced.
346 boolean visibilityChanged = false;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200347 if (isHidden() == visible || (isHidden() && mIsExiting) || (visible && waitingForReplacement())) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700348 final AccessibilityController accessibilityController = mService.mAccessibilityController;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700349 boolean changed = false;
350 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200351 "Changing app " + this + " hidden=" + isHidden() + " performLayout=" + performLayout);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700352
353 boolean runningAppAnimation = false;
354
355 if (transit != AppTransition.TRANSIT_UNSET) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200356 if (applyAnimationLocked(lp, transit, visible, isVoiceInteraction)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700357 delayed = runningAppAnimation = true;
358 }
359 final WindowState window = findMainWindow();
360 //TODO (multidisplay): Magnification is supported only for the default display.
361 if (window != null && accessibilityController != null
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700362 && getDisplayContent().getDisplayId() == DEFAULT_DISPLAY) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700363 accessibilityController.onAppWindowTransitionLocked(window, transit);
364 }
365 changed = true;
366 }
367
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700368 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700369 for (int i = 0; i < windowsCount; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700370 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700371 changed |= win.onAppVisibilityChanged(visible, runningAppAnimation);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700372 }
373
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200374 setHidden(!visible);
375 hiddenRequested = !visible;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700376 visibilityChanged = true;
377 if (!visible) {
378 stopFreezingScreen(true, true);
379 } else {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700380 // If we are being set visible, and the starting window is not yet displayed,
381 // then make sure it doesn't get displayed.
382 if (startingWindow != null && !startingWindow.isDrawnLw()) {
383 startingWindow.mPolicyVisibility = false;
384 startingWindow.mPolicyVisibilityAfterAnim = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700385 }
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700386
387 // We are becoming visible, so better freeze the screen with the windows that are
388 // getting visible so we also wait for them.
389 forAllWindows(mService::makeWindowFreezingScreenIfNeededLocked, true);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700390 }
391
392 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "setVisibility: " + this
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200393 + ": hidden=" + isHidden() + " hiddenRequested=" + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700394
395 if (changed) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700396 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700397 if (performLayout) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700398 mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700399 false /*updateInputWindows*/);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700400 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700401 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700402 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700403 }
404 }
405
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200406 if (isReallyAnimating()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700407 delayed = true;
408 }
409
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700410 for (int i = mChildren.size() - 1; i >= 0 && !delayed; i--) {
Jorim Jaggia5e10572017-11-15 14:36:26 +0100411 if ((mChildren.get(i)).isSelfOrChildAnimating()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700412 delayed = true;
413 }
414 }
415
416 if (visibilityChanged) {
417 if (visible && !delayed) {
418 // The token was made immediately visible, there will be no entrance animation.
419 // We need to inform the client the enter animation was finished.
420 mEnteringAnimation = true;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700421 mService.mActivityManagerAppTransitionNotifier.onAppTransitionFinishedLocked(token);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700422 }
Robert Carr61b81112017-07-17 18:08:15 -0700423
Robert Carre7cc44d2017-03-20 19:04:30 -0700424 // If we are hidden but there is no delay needed we immediately
425 // apply the Surface transaction so that the ActivityManager
Robert Carr61b81112017-07-17 18:08:15 -0700426 // can have some guarantee on the Surface state following
427 // setting the visibility. This captures cases like dismissing
428 // the docked or pinned stack where there is no app transition.
429 //
430 // In the case of a "Null" animation, there will be
431 // no animation but there will still be a transition set.
432 // We still need to delay hiding the surface such that it
433 // can be synchronized with showing the next surface in the transition.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200434 if (isHidden() && !delayed && !mService.mAppTransition.isTransitionSet()) {
Robert Carr6914f082017-03-20 19:04:30 -0700435 SurfaceControl.openTransaction();
436 for (int i = mChildren.size() - 1; i >= 0; i--) {
437 mChildren.get(i).mWinAnimator.hide("immediately hidden");
438 }
439 SurfaceControl.closeTransaction();
440 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700441
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700442 if (!mService.mClosingApps.contains(this) && !mService.mOpeningApps.contains(this)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700443 // The token is not closing nor opening, so even if there is an animation set, that
444 // doesn't mean that it goes through the normal app transition cycle so we have
445 // to inform the docked controller about visibility change.
Andrii Kulian1e32e022016-09-16 15:29:34 -0700446 // TODO(multi-display): notify docked divider on all displays where visibility was
447 // affected.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700448 mService.getDefaultDisplayContentLocked().getDockedDividerController()
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700449 .notifyAppVisibilityChanged();
Jorim Jaggi8b702ed2017-01-20 16:59:03 +0100450 mService.mTaskSnapshotController.notifyAppVisibilityChanged(this, visible);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700451 }
452 }
453
454 return delayed;
455 }
456
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +0200457 /**
458 * @return The to top most child window for which {@link LayoutParams#isFullscreen()} returns
459 * true.
460 */
461 WindowState getTopFullscreenWindow() {
462 for (int i = mChildren.size() - 1; i >= 0; i--) {
463 final WindowState win = mChildren.get(i);
464 if (win != null && win.mAttrs.isFullscreen()) {
465 return win;
466 }
467 }
468 return null;
469 }
470
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800471 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700472 WindowState candidate = null;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700473 int j = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800474 while (j > 0) {
475 j--;
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700476 final WindowState win = mChildren.get(j);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700477 final int type = win.mAttrs.type;
478 // No need to loop through child window as base application and starting types can't be
479 // child windows.
480 if (type == TYPE_BASE_APPLICATION || type == TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700481 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900482 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700483 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800484 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700485 candidate = win;
486 } else {
487 return win;
488 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800489 }
490 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700491 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800492 }
493
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800494 boolean windowsAreFocusable() {
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700495 return getWindowConfiguration().canReceiveKeys() || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800496 }
497
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800498 AppWindowContainerController getController() {
499 final WindowContainerController controller = super.getController();
500 return controller != null ? (AppWindowContainerController) controller : null;
501 }
502
Wale Ogunwale571771c2016-08-26 13:18:50 -0700503 @Override
Wale Ogunwale44f21802016-09-02 12:49:48 -0700504 boolean isVisible() {
Wale Ogunwalee471be62016-10-03 07:53:55 -0700505 // If the app token isn't hidden then it is considered visible and there is no need to check
506 // its children windows to see if they are visible.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200507 return !isHidden();
Wale Ogunwale44f21802016-09-02 12:49:48 -0700508 }
509
510 @Override
Wale Ogunwalee287e192017-04-21 09:30:12 -0700511 void removeImmediately() {
512 onRemovedFromDisplay();
513 super.removeImmediately();
514 }
515
516 @Override
Wale Ogunwale571771c2016-08-26 13:18:50 -0700517 void removeIfPossible() {
Craig Mautnere3119b72015-01-20 15:02:36 -0800518 mIsExiting = false;
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800519 removeAllWindowsIfPossible();
Bryce Lee6d410262017-02-28 15:30:17 -0800520 removeImmediately();
Craig Mautnere3119b72015-01-20 15:02:36 -0800521 }
522
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700523 @Override
524 boolean checkCompleteDeferredRemoval() {
525 if (mIsExiting) {
526 removeIfPossible();
527 }
528 return super.checkCompleteDeferredRemoval();
529 }
530
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700531 void onRemovedFromDisplay() {
Wale Ogunwalee287e192017-04-21 09:30:12 -0700532 if (mRemovingFromDisplay) {
533 return;
534 }
535 mRemovingFromDisplay = true;
536
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700537 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app token: " + this);
538
Wale Ogunwale72919d22016-12-08 18:58:50 -0800539 boolean delayed = setVisibility(null, false, TRANSIT_UNSET, true, mVoiceInteraction);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700540
541 mService.mOpeningApps.remove(this);
Jorim Jaggi45ca0542017-05-30 16:16:01 -0700542 mService.mUnknownAppVisibilityController.appRemovedOrHidden(this);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100543 mService.mTaskSnapshotController.onAppRemoved(this);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700544 waitingToShow = false;
545 if (mService.mClosingApps.contains(this)) {
546 delayed = true;
547 } else if (mService.mAppTransition.isTransitionSet()) {
548 mService.mClosingApps.add(this);
549 delayed = true;
550 }
551
552 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app " + this + " delayed=" + delayed
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200553 + " animation=" + getAnimation() + " animating=" + isSelfAnimating());
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700554
555 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM, "removeAppToken: "
556 + this + " delayed=" + delayed + " Callers=" + Debug.getCallers(4));
557
Jorim Jaggi19be6052017-08-03 18:33:43 +0200558 if (startingData != null && getController() != null) {
559 getController().removeStartingWindow();
560 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800561
Winson Chung87e5d552017-04-05 11:49:38 -0700562 // If this window was animating, then we need to ensure that the app transition notifies
563 // that animations have completed in WMS.handleAnimatingStoppedAndTransitionLocked(), so
564 // add to that list now
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200565 if (isSelfAnimating()) {
Winson Chung87e5d552017-04-05 11:49:38 -0700566 mService.mNoAnimationNotifyOnTransitionFinished.add(token);
567 }
568
Wale Ogunwalee287e192017-04-21 09:30:12 -0700569 final TaskStack stack = getStack();
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700570 if (delayed && !isEmpty()) {
571 // set the token aside because it has an active animation to be finished
572 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM,
573 "removeAppToken make exiting: " + this);
Wale Ogunwalee287e192017-04-21 09:30:12 -0700574 if (stack != null) {
575 stack.mExitingAppTokens.add(this);
576 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700577 mIsExiting = true;
578 } else {
579 // Make sure there is no animation running on this token, so any windows associated
580 // with it will be removed as soon as their animations are complete
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200581 cancelAnimation();
Wale Ogunwale3106ae012017-05-16 08:56:37 -0700582 if (stack != null) {
583 stack.mExitingAppTokens.remove(this);
584 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700585 removeIfPossible();
586 }
587
588 removed = true;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700589 stopFreezingScreen(true, true);
Bryce Lee6d410262017-02-28 15:30:17 -0800590
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700591 if (mService.mFocusedApp == this) {
592 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "Removing focused app token:" + this);
593 mService.mFocusedApp = null;
594 mService.updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL, true /*updateInputWindows*/);
595 mService.mInputMonitor.setFocusedAppLw(null);
596 }
597
598 if (!delayed) {
599 updateReportedVisibilityLocked();
600 }
Wale Ogunwalee287e192017-04-21 09:30:12 -0700601
602 mRemovingFromDisplay = false;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700603 }
604
Chong Zhange05bcb12016-07-26 17:47:29 -0700605 void clearAnimatingFlags() {
Chong Zhangb0d26702016-08-12 16:03:29 -0700606 boolean wallpaperMightChange = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700607 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700608 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700609 wallpaperMightChange |= win.clearAnimatingFlags();
Chong Zhange05bcb12016-07-26 17:47:29 -0700610 }
Chong Zhangb0d26702016-08-12 16:03:29 -0700611 if (wallpaperMightChange) {
612 requestUpdateWallpaperIfNeeded();
613 }
Chong Zhange05bcb12016-07-26 17:47:29 -0700614 }
615
Robert Carre12aece2016-02-02 22:43:27 -0800616 void destroySurfaces() {
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700617 destroySurfaces(false /*cleanupOnResume*/);
618 }
619
620 /**
621 * Destroy surfaces which have been marked as eligible by the animator, taking care to ensure
622 * the client has finished with them.
623 *
624 * @param cleanupOnResume whether this is done when app is resumed without fully stopped. If
625 * set to true, destroy only surfaces of removed windows, and clear relevant flags of the
626 * others so that they are ready to be reused. If set to false (common case), destroy all
627 * surfaces that's eligible, if the app is already stopped.
628 */
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700629 private void destroySurfaces(boolean cleanupOnResume) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700630 boolean destroyedSomething = false;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100631
632 // Copying to a different list as multiple children can be removed.
633 // TODO: Not sure why this is needed.
634 final LinkedList<WindowState> children = new LinkedList<>(mChildren);
635 for (int i = children.size() - 1; i >= 0; i--) {
636 final WindowState win = children.get(i);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700637 destroyedSomething |= win.destroySurface(cleanupOnResume, mAppStopped);
Robert Carre12aece2016-02-02 22:43:27 -0800638 }
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700639 if (destroyedSomething) {
640 final DisplayContent dc = getDisplayContent();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700641 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Robert Carre12aece2016-02-02 22:43:27 -0800642 }
643 }
644
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800645 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700646 * Notify that the app is now resumed, and it was not stopped before, perform a clean
647 * up of the surfaces
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800648 */
Jorim Jaggibae01b12017-04-11 16:29:10 -0700649 void notifyAppResumed(boolean wasStopped) {
Chong Zhangad24f962016-08-25 12:12:33 -0700650 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppResumed: wasStopped=" + wasStopped
Jorim Jaggibae01b12017-04-11 16:29:10 -0700651 + " " + this);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700652 mAppStopped = false;
chaviwd3bf08d2017-08-01 17:24:59 -0700653 // Allow the window to turn the screen on once the app is resumed again.
654 setCanTurnScreenOn(true);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700655 if (!wasStopped) {
656 destroySurfaces(true /*cleanupOnResume*/);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800657 }
Robert Carre12aece2016-02-02 22:43:27 -0800658 }
659
Chong Zhangbef461f2015-10-27 11:38:24 -0700660 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700661 * Notify that the app has stopped, and it is okay to destroy any surfaces which were
662 * keeping alive in case they were still being used.
663 */
664 void notifyAppStopped() {
665 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: " + this);
666 mAppStopped = true;
667 destroySurfaces();
668 // Remove any starting window that was added for this app if they are still around.
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800669 if (getController() != null) {
670 getController().removeStartingWindow();
671 }
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700672 }
673
Chong Zhang92147042016-05-09 12:47:11 -0700674 void clearAllDrawn() {
675 allDrawn = false;
676 deferClearAllDrawn = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700677 }
678
Bryce Lee6d410262017-02-28 15:30:17 -0800679 Task getTask() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800680 return (Task) getParent();
Bryce Lee6d410262017-02-28 15:30:17 -0800681 }
682
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800683 TaskStack getStack() {
684 final Task task = getTask();
685 if (task != null) {
686 return task.mStack;
687 } else {
688 return null;
689 }
690 }
691
Bryce Lee6d410262017-02-28 15:30:17 -0800692 @Override
693 void onParentSet() {
694 super.onParentSet();
695
Robert Carred3e83b2017-04-21 13:26:55 -0700696 final Task task = getTask();
697
Bryce Lee6d410262017-02-28 15:30:17 -0800698 // When the associated task is {@code null}, the {@link AppWindowToken} can no longer
699 // access visual elements like the {@link DisplayContent}. We must remove any associations
700 // such as animations.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800701 if (!mReparenting) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800702 if (task == null) {
703 // It is possible we have been marked as a closing app earlier. We must remove ourselves
704 // from this list so we do not participate in any future animations.
705 mService.mClosingApps.remove(this);
Robert Carred3e83b2017-04-21 13:26:55 -0700706 } else if (mLastParent != null && mLastParent.mStack != null) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800707 task.mStack.mExitingAppTokens.remove(this);
708 }
Bryce Lee6d410262017-02-28 15:30:17 -0800709 }
Robert Carred3e83b2017-04-21 13:26:55 -0700710 mLastParent = task;
Bryce Lee6d410262017-02-28 15:30:17 -0800711 }
712
Wale Ogunwalefa854eb2016-09-20 13:43:52 -0700713 void postWindowRemoveStartingWindowCleanup(WindowState win) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700714 // TODO: Something smells about the code below...Is there a better way?
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700715 if (startingWindow == win) {
716 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Notify removed startingWindow " + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800717 if (getController() != null) {
718 getController().removeStartingWindow();
719 }
Wale Ogunwale6c459212017-05-17 08:56:03 -0700720 } else if (mChildren.size() == 0) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700721 // If this is the last window and we had requested a starting transition window,
722 // well there is no point now.
Jorim Jaggi02886a82016-12-06 09:10:06 -0800723 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Nulling last startingData");
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700724 startingData = null;
Wale Ogunwale6c459212017-05-17 08:56:03 -0700725 if (mHiddenSetFromTransferredStartingWindow) {
726 // We set the hidden state to false for the token from a transferred starting window.
727 // We now reset it back to true since the starting window was the last window in the
728 // token.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200729 setHidden(true);
Wale Ogunwale6c459212017-05-17 08:56:03 -0700730 }
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100731 } else if (mChildren.size() == 1 && startingSurface != null && !isRelaunching()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700732 // If this is the last window except for a starting transition window,
733 // we need to get rid of the starting transition.
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200734 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Last window, removing starting window "
735 + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800736 if (getController() != null) {
737 getController().removeStartingWindow();
738 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700739 }
740 }
741
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700742 void removeDeadWindows() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700743 for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700744 WindowState win = mChildren.get(winNdx);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800745 if (win.mAppDied) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700746 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.w(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700747 "removeDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800748 // Set mDestroying, we don't want any animation or delayed removal here.
749 win.mDestroying = true;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700750 // Also removes child windows.
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700751 win.removeIfPossible();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800752 }
753 }
754 }
755
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700756 boolean hasWindowsAlive() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700757 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700758 // No need to loop through child windows as the answer should be the same as that of the
759 // parent window.
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700760 if (!(mChildren.get(i)).mAppDied) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700761 return true;
762 }
763 }
764 return false;
765 }
766
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700767 void setWillReplaceWindows(boolean animate) {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700768 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
769 "Marking app token " + this + " with replacing windows.");
Robert Carra1eb4392015-12-10 12:43:51 -0800770
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700771 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700772 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700773 w.setWillReplaceWindow(animate);
Robert Carra1eb4392015-12-10 12:43:51 -0800774 }
Robert Carra1eb4392015-12-10 12:43:51 -0800775 }
776
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700777 void setWillReplaceChildWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700778 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + this
Robert Carr23fa16b2016-01-13 13:19:58 -0800779 + " with replacing child windows.");
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700780 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700781 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700782 w.setWillReplaceChildWindows();
Robert Carr23fa16b2016-01-13 13:19:58 -0800783 }
784 }
785
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700786 void clearWillReplaceWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700787 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
788 "Resetting app token " + this + " of replacing window marks.");
Chong Zhangf596cd52016-01-05 13:42:44 -0800789
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700790 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700791 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700792 w.clearWillReplaceWindow();
Chong Zhangf596cd52016-01-05 13:42:44 -0800793 }
794 }
795
Chong Zhang4d7369a2016-04-25 16:09:14 -0700796 void requestUpdateWallpaperIfNeeded() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700797 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700798 final WindowState w = mChildren.get(i);
Chong Zhang4d7369a2016-04-25 16:09:14 -0700799 w.requestUpdateWallpaperIfNeeded();
800 }
801 }
802
Chong Zhangd78ddb42016-03-02 17:01:14 -0800803 boolean isRelaunching() {
804 return mPendingRelaunchCount > 0;
805 }
806
Robert Carr68375192017-06-13 12:41:53 -0700807 boolean shouldFreezeBounds() {
808 final Task task = getTask();
809
810 // For freeform windows, we can't freeze the bounds at the moment because this would make
811 // the resizing unresponsive.
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700812 if (task == null || task.inFreeformWindowingMode()) {
Robert Carr68375192017-06-13 12:41:53 -0700813 return false;
814 }
815
816 // We freeze the bounds while drag resizing to deal with the time between
817 // the divider/drag handle being released, and the handling it's new
818 // configuration. If we are relaunched outside of the drag resizing state,
819 // we need to be careful not to do this.
820 return getTask().isDragResizing();
821 }
822
Chong Zhangd78ddb42016-03-02 17:01:14 -0800823 void startRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700824 if (shouldFreezeBounds()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -0800825 freezeBounds();
826 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800827
828 // In the process of tearing down before relaunching, the app will
829 // try and clean up it's child surfaces. We need to prevent this from
830 // happening, so we sever the children, transfering their ownership
831 // from the client it-self to the parent surface (owned by us).
832 for (int i = mChildren.size() - 1; i >= 0; i--) {
833 final WindowState w = mChildren.get(i);
834 w.mWinAnimator.detachChildren();
835 }
836
Chong Zhangd78ddb42016-03-02 17:01:14 -0800837 mPendingRelaunchCount++;
838 }
839
840 void finishRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700841 unfreezeBounds();
842
Chong Zhangd78ddb42016-03-02 17:01:14 -0800843 if (mPendingRelaunchCount > 0) {
844 mPendingRelaunchCount--;
Bryce Lee081554b2017-05-25 07:52:12 -0700845 } else {
846 // Update keyguard flags upon finishing relaunch.
847 checkKeyguardFlagsChanged();
Chong Zhangd78ddb42016-03-02 17:01:14 -0800848 }
849 }
850
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700851 void clearRelaunching() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700852 if (mPendingRelaunchCount == 0) {
853 return;
854 }
Robert Carr68375192017-06-13 12:41:53 -0700855 unfreezeBounds();
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700856 mPendingRelaunchCount = 0;
857 }
858
Wale Ogunwale07bcab72016-10-14 15:30:09 -0700859 /**
860 * Returns true if the new child window we are adding to this token is considered greater than
861 * the existing child window in this token in terms of z-order.
862 */
863 @Override
864 protected boolean isFirstChildWindowGreaterThanSecond(WindowState newWindow,
865 WindowState existingWindow) {
866 final int type1 = newWindow.mAttrs.type;
867 final int type2 = existingWindow.mAttrs.type;
868
869 // Base application windows should be z-ordered BELOW all other windows in the app token.
870 if (type1 == TYPE_BASE_APPLICATION && type2 != TYPE_BASE_APPLICATION) {
871 return false;
872 } else if (type1 != TYPE_BASE_APPLICATION && type2 == TYPE_BASE_APPLICATION) {
873 return true;
874 }
875
876 // Starting windows should be z-ordered ABOVE all other windows in the app token.
877 if (type1 == TYPE_APPLICATION_STARTING && type2 != TYPE_APPLICATION_STARTING) {
878 return true;
879 } else if (type1 != TYPE_APPLICATION_STARTING && type2 == TYPE_APPLICATION_STARTING) {
880 return false;
881 }
882
883 // Otherwise the new window is greater than the existing window.
884 return true;
885 }
886
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700887 @Override
Robert Carra1eb4392015-12-10 12:43:51 -0800888 void addWindow(WindowState w) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700889 super.addWindow(w);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700890
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700891 boolean gotReplacementWindow = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700892 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700893 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700894 gotReplacementWindow |= candidate.setReplacementWindowIfNeeded(w);
895 }
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700896
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700897 // if we got a replacement window, reset the timeout to give drawing more time
898 if (gotReplacementWindow) {
899 mService.scheduleWindowReplacementTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -0800900 }
Jorim Jaggife762342016-10-13 14:33:27 +0200901 checkKeyguardFlagsChanged();
902 }
903
904 @Override
905 void removeChild(WindowState child) {
906 super.removeChild(child);
907 checkKeyguardFlagsChanged();
Robert Carra1eb4392015-12-10 12:43:51 -0800908 }
909
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700910 private boolean waitingForReplacement() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700911 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700912 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700913 if (candidate.waitingForReplacement()) {
Robert Carra1eb4392015-12-10 12:43:51 -0800914 return true;
915 }
916 }
917 return false;
918 }
919
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700920 void onWindowReplacementTimeout() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700921 for (int i = mChildren.size() - 1; i >= 0; --i) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700922 (mChildren.get(i)).onWindowReplacementTimeout();
Robert Carra1eb4392015-12-10 12:43:51 -0800923 }
924 }
925
Winson Chung30480042017-01-26 10:55:34 -0800926 void reparent(Task task, int position) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800927 final Task currentTask = getTask();
928 if (task == currentTask) {
Winson Chung30480042017-01-26 10:55:34 -0800929 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800930 "window token=" + this + " already child of task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -0800931 }
Bryce Lee6d410262017-02-28 15:30:17 -0800932
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800933 if (currentTask.mStack != task.mStack) {
Bryce Lee6d410262017-02-28 15:30:17 -0800934 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800935 "window token=" + this + " current task=" + currentTask
Bryce Lee6d410262017-02-28 15:30:17 -0800936 + " belongs to a different stack than " + task);
937 }
938
Winson Chung30480042017-01-26 10:55:34 -0800939 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "reParentWindowToken: removing window token=" + this
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800940 + " from task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -0800941 final DisplayContent prevDisplayContent = getDisplayContent();
942
Bryce Lee6d410262017-02-28 15:30:17 -0800943 mReparenting = true;
944
Winson Chung30480042017-01-26 10:55:34 -0800945 getParent().removeChild(this);
946 task.addChild(this, position);
947
Bryce Lee6d410262017-02-28 15:30:17 -0800948 mReparenting = false;
949
Winson Chung30480042017-01-26 10:55:34 -0800950 // Relayout display(s).
951 final DisplayContent displayContent = task.getDisplayContent();
952 displayContent.setLayoutNeeded();
953 if (prevDisplayContent != displayContent) {
954 onDisplayChanged(displayContent);
955 prevDisplayContent.setLayoutNeeded();
956 }
957 }
958
Jorim Jaggi0429f352015-12-22 16:29:16 +0100959 /**
960 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
961 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
962 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
963 * with a queue.
964 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800965 private void freezeBounds() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800966 final Task task = getTask();
967 mFrozenBounds.offer(new Rect(task.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700968
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800969 if (task.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700970 // We didn't call prepareFreezingBounds on the task, so use the current value.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800971 mFrozenMergedConfig.offer(new Configuration(task.getConfiguration()));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700972 } else {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800973 mFrozenMergedConfig.offer(new Configuration(task.mPreparedFrozenMergedConfig));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700974 }
Andrii Kulianb10330d2016-09-16 13:51:46 -0700975 // Calling unset() to make it equal to Configuration.EMPTY.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800976 task.mPreparedFrozenMergedConfig.unset();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100977 }
978
979 /**
980 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
981 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800982 private void unfreezeBounds() {
Robert Carr68375192017-06-13 12:41:53 -0700983 if (mFrozenBounds.isEmpty()) {
984 return;
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700985 }
Robert Carr68375192017-06-13 12:41:53 -0700986 mFrozenBounds.remove();
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700987 if (!mFrozenMergedConfig.isEmpty()) {
988 mFrozenMergedConfig.remove();
989 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700990 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700991 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700992 win.onUnfreezeBounds();
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100993 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700994 mService.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100995 }
996
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700997 void setAppLayoutChanges(int changes, String reason) {
998 if (!mChildren.isEmpty()) {
999 final DisplayContent dc = getDisplayContent();
1000 dc.pendingLayoutChanges |= changes;
1001 if (DEBUG_LAYOUT_REPEATS) {
1002 mService.mWindowPlacerLocked.debugLayoutRepeats(reason, dc.pendingLayoutChanges);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001003 }
1004 }
1005 }
1006
1007 void removeReplacedWindowIfNeeded(WindowState replacement) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001008 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001009 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001010 if (win.removeReplacedWindowIfNeeded(replacement)) {
1011 return;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001012 }
1013 }
1014 }
1015
1016 void startFreezingScreen() {
1017 if (DEBUG_ORIENTATION) logWithStack(TAG, "Set freezing of " + appToken + ": hidden="
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001018 + isHidden() + " freezing=" + mAppAnimator.freezingScreen + " hiddenRequested="
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001019 + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001020 if (!hiddenRequested) {
1021 if (!mAppAnimator.freezingScreen) {
1022 mAppAnimator.freezingScreen = true;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001023 mService.registerAppFreezeListener(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001024 mAppAnimator.lastFreezeDuration = 0;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001025 mService.mAppsFreezingScreen++;
1026 if (mService.mAppsFreezingScreen == 1) {
Bryce Lee8f853582017-06-05 17:25:59 -07001027 mService.startFreezingDisplayLocked(false, 0, 0, getDisplayContent());
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001028 mService.mH.removeMessages(H.APP_FREEZE_TIMEOUT);
1029 mService.mH.sendEmptyMessageDelayed(H.APP_FREEZE_TIMEOUT, 2000);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001030 }
1031 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001032 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001033 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001034 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001035 w.onStartFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001036 }
1037 }
1038 }
1039
1040 void stopFreezingScreen(boolean unfreezeSurfaceNow, boolean force) {
1041 if (!mAppAnimator.freezingScreen) {
1042 return;
1043 }
1044 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Clear freezing of " + this + " force=" + force);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001045 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001046 boolean unfrozeWindows = false;
1047 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001048 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001049 unfrozeWindows |= w.onStopFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001050 }
1051 if (force || unfrozeWindows) {
1052 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "No longer freezing: " + this);
1053 mAppAnimator.freezingScreen = false;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001054 mService.unregisterAppFreezeListener(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001055 mAppAnimator.lastFreezeDuration =
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001056 (int)(SystemClock.elapsedRealtime() - mService.mDisplayFreezeTime);
1057 mService.mAppsFreezingScreen--;
1058 mService.mLastFinishedFreezeSource = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001059 }
1060 if (unfreezeSurfaceNow) {
1061 if (unfrozeWindows) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001062 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001063 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001064 mService.stopFreezingDisplayLocked();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001065 }
1066 }
1067
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001068 @Override
1069 public void onAppFreezeTimeout() {
1070 Slog.w(TAG_WM, "Force clearing freeze: " + this);
1071 stopFreezingScreen(true, true);
1072 }
1073
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001074 boolean transferStartingWindow(IBinder transferFrom) {
Wale Ogunwale02319a62016-09-26 15:21:22 -07001075 final AppWindowToken fromToken = getDisplayContent().getAppWindowToken(transferFrom);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001076 if (fromToken == null) {
1077 return false;
1078 }
1079
1080 final WindowState tStartingWindow = fromToken.startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001081 if (tStartingWindow != null && fromToken.startingSurface != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001082 // In this case, the starting icon has already been displayed, so start
1083 // letting windows get shown immediately without any more transitions.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001084 mService.mSkipAppTransitionAnimation = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001085
1086 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Moving existing starting " + tStartingWindow
1087 + " from " + fromToken + " to " + this);
1088
1089 final long origId = Binder.clearCallingIdentity();
Peter Visontay3556a3b2017-11-01 17:23:17 +00001090 try {
1091 // Transfer the starting window over to the new token.
1092 startingData = fromToken.startingData;
1093 startingSurface = fromToken.startingSurface;
1094 startingDisplayed = fromToken.startingDisplayed;
1095 fromToken.startingDisplayed = false;
1096 startingWindow = tStartingWindow;
1097 reportedVisible = fromToken.reportedVisible;
1098 fromToken.startingData = null;
1099 fromToken.startingSurface = null;
1100 fromToken.startingWindow = null;
1101 fromToken.startingMoved = true;
1102 tStartingWindow.mToken = this;
1103 tStartingWindow.mAppToken = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001104
Peter Visontay3556a3b2017-11-01 17:23:17 +00001105 if (DEBUG_ADD_REMOVE || DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1106 "Removing starting " + tStartingWindow + " from " + fromToken);
1107 fromToken.removeChild(tStartingWindow);
1108 fromToken.postWindowRemoveStartingWindowCleanup(tStartingWindow);
1109 fromToken.mHiddenSetFromTransferredStartingWindow = false;
1110 addWindow(tStartingWindow);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001111
Peter Visontay3556a3b2017-11-01 17:23:17 +00001112 // Propagate other interesting state between the tokens. If the old token is displayed,
1113 // we should immediately force the new one to be displayed. If it is animating, we need
1114 // to move that animation to the new one.
1115 if (fromToken.allDrawn) {
1116 allDrawn = true;
1117 deferClearAllDrawn = fromToken.deferClearAllDrawn;
1118 }
1119 if (fromToken.firstWindowDrawn) {
1120 firstWindowDrawn = true;
1121 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001122 if (!fromToken.isHidden()) {
1123 setHidden(false);
Peter Visontay3556a3b2017-11-01 17:23:17 +00001124 hiddenRequested = false;
1125 mHiddenSetFromTransferredStartingWindow = true;
1126 }
1127 setClientHidden(fromToken.mClientHidden);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001128
1129 // TODO: Transfer animation
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001130
Peter Visontay3556a3b2017-11-01 17:23:17 +00001131 mService.updateFocusedWindowLocked(
1132 UPDATE_FOCUS_WILL_PLACE_SURFACES, true /*updateInputWindows*/);
1133 getDisplayContent().setLayoutNeeded();
1134 mService.mWindowPlacerLocked.performSurfacePlacement();
1135 } finally {
1136 Binder.restoreCallingIdentity(origId);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001137 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001138 return true;
1139 } else if (fromToken.startingData != null) {
1140 // The previous app was getting ready to show a
1141 // starting window, but hasn't yet done so. Steal it!
1142 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1143 "Moving pending starting from " + fromToken + " to " + this);
1144 startingData = fromToken.startingData;
1145 fromToken.startingData = null;
1146 fromToken.startingMoved = true;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001147 if (getController() != null) {
1148 getController().scheduleAddStartingWindow();
1149 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001150 return true;
1151 }
1152
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001153 // TODO: Transfer thumbnail
1154
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001155 return false;
1156 }
1157
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001158 boolean isLastWindow(WindowState win) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001159 return mChildren.size() == 1 && mChildren.get(0) == win;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001160 }
1161
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001162 @Override
1163 void onAppTransitionDone() {
1164 sendingToBottom = false;
1165 }
1166
Wale Ogunwale51362492016-09-08 17:49:17 -07001167 /**
1168 * We override because this class doesn't want its children affecting its reported orientation
1169 * in anyway.
1170 */
1171 @Override
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001172 int getOrientation(int candidate) {
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001173 if (candidate == SCREEN_ORIENTATION_BEHIND) {
1174 // Allow app to specify orientation regardless of its visibility state if the current
1175 // candidate want us to use orientation behind. I.e. the visible app on-top of this one
1176 // wants us to use the orientation of the app behind it.
1177 return mOrientation;
1178 }
1179
Bryce Lee61fbcbc2017-03-10 14:14:03 -08001180 // The {@link AppWindowToken} should only specify an orientation when it is not closing or
1181 // going to the bottom. Allowing closing {@link AppWindowToken} to participate can lead to
1182 // an Activity in another task being started in the wrong orientation during the transition.
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001183 if (!(sendingToBottom || mService.mClosingApps.contains(this))
Bryce Lee00d586d2017-07-28 20:48:43 -07001184 && (isVisible() || mService.mOpeningApps.contains(this) || isOnTop())) {
Bryce Leea163b762017-01-24 11:05:01 -08001185 return mOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001186 }
Bryce Leea163b762017-01-24 11:05:01 -08001187
1188 return SCREEN_ORIENTATION_UNSET;
Wale Ogunwale51362492016-09-08 17:49:17 -07001189 }
1190
Wale Ogunwaleb198b742016-12-01 08:44:09 -08001191 /** Returns the app's preferred orientation regardless of its currently visibility state. */
1192 int getOrientationIgnoreVisibility() {
1193 return mOrientation;
1194 }
1195
Craig Mautnerdbb79912012-03-01 18:59:14 -08001196 @Override
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001197 void checkAppWindowsReadyToShow() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001198 if (allDrawn == mAppAnimator.allDrawn) {
1199 return;
1200 }
1201
1202 mAppAnimator.allDrawn = allDrawn;
1203 if (!allDrawn) {
1204 return;
1205 }
1206
1207 // The token has now changed state to having all windows shown... what to do, what to do?
1208 if (mAppAnimator.freezingScreen) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001209 showAllWindowsLocked();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001210 stopFreezingScreen(false, true);
1211 if (DEBUG_ORIENTATION) Slog.i(TAG,
1212 "Setting mOrientationChangeComplete=true because wtoken " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001213 + " numInteresting=" + mNumInterestingWindows + " numDrawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001214 // This will set mOrientationChangeComplete and cause a pass through layout.
1215 setAppLayoutChanges(FINISH_LAYOUT_REDO_WALLPAPER,
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001216 "checkAppWindowsReadyToShow: freezingScreen");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001217 } else {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001218 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM, "checkAppWindowsReadyToShow");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001219
1220 // We can now show all of the drawn windows!
1221 if (!mService.mOpeningApps.contains(this)) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001222 showAllWindowsLocked();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001223 }
1224 }
1225 }
1226
Matthew Ng5d23afa2017-06-21 16:16:24 -07001227 /**
Bryce Leed390deb2017-06-22 13:14:28 -07001228 * Returns whether the drawn window states of this {@link AppWindowToken} has considered every
1229 * child {@link WindowState}. A child is considered if it has been passed into
1230 * {@link #updateDrawnWindowStates(WindowState)} after being added. This is used to determine
1231 * whether states, such as {@code allDrawn}, can be set, which relies on state variables such as
1232 * {@code mNumInterestingWindows}, which depend on all {@link WindowState}s being considered.
1233 *
1234 * @return {@code true} If all children have been considered, {@code false}.
1235 */
1236 private boolean allDrawnStatesConsidered() {
Bryce Lee6311c4b2017-07-06 14:09:29 -07001237 for (int i = mChildren.size() - 1; i >= 0; --i) {
1238 final WindowState child = mChildren.get(i);
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001239 if (child.mightAffectAllDrawn() && !child.getDrawnStateEvaluated()) {
Bryce Leed390deb2017-06-22 13:14:28 -07001240 return false;
1241 }
1242 }
1243 return true;
1244 }
1245
1246 /**
Matthew Ng5d23afa2017-06-21 16:16:24 -07001247 * Determines if the token has finished drawing. This should only be called from
1248 * {@link DisplayContent#applySurfaceChangesTransaction}
1249 */
Matthew Ng498c71d2017-04-18 13:55:45 -07001250 void updateAllDrawn() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001251 if (!allDrawn) {
Matthew Ng498c71d2017-04-18 13:55:45 -07001252 // Number of drawn windows can be less when a window is being relaunched, wait for
Bryce Leed390deb2017-06-22 13:14:28 -07001253 // all windows to be launched and drawn for this token be considered all drawn.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001254 final int numInteresting = mNumInterestingWindows;
Bryce Leed390deb2017-06-22 13:14:28 -07001255
1256 // We must make sure that all present children have been considered (determined by
1257 // {@link #allDrawnStatesConsidered}) before evaluating whether everything has been
1258 // drawn.
1259 if (numInteresting > 0 && allDrawnStatesConsidered()
1260 && mNumDrawnWindows >= numInteresting && !isRelaunching()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001261 if (DEBUG_VISIBILITY) Slog.v(TAG, "allDrawn: " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001262 + " interesting=" + numInteresting + " drawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001263 allDrawn = true;
1264 // Force an additional layout pass where
1265 // WindowStateAnimator#commitFinishDrawingLocked() will call performShowLocked().
Matthew Ng498c71d2017-04-18 13:55:45 -07001266 if (mDisplayContent != null) {
1267 mDisplayContent.setLayoutNeeded();
1268 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001269 mService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
Robert Carrecc06b32017-04-18 14:25:10 -07001270
Winson Chunge7ba6862017-05-24 12:13:33 -07001271 // Notify the pinned stack upon all windows drawn. If there was an animation in
1272 // progress then this signal will resume that animation.
Wale Ogunwale61911492017-10-11 08:50:50 -07001273 final TaskStack pinnedStack = mDisplayContent.getPinnedStack();
Winson Chunge7ba6862017-05-24 12:13:33 -07001274 if (pinnedStack != null) {
1275 pinnedStack.onAllWindowsDrawn();
Robert Carrecc06b32017-04-18 14:25:10 -07001276 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001277 }
1278 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001279 }
1280
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001281 /**
1282 * Updated this app token tracking states for interesting and drawn windows based on the window.
1283 *
1284 * @return Returns true if the input window is considered interesting and drawn while all the
1285 * windows in this app token where not considered drawn as of the last pass.
1286 */
1287 boolean updateDrawnWindowStates(WindowState w) {
Bryce Leed390deb2017-06-22 13:14:28 -07001288 w.setDrawnStateEvaluated(true /*evaluated*/);
1289
Jorim Jaggie4b0f282017-05-17 15:10:29 +02001290 if (DEBUG_STARTING_WINDOW_VERBOSE && w == startingWindow) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001291 Slog.d(TAG, "updateWindows: starting " + w + " isOnScreen=" + w.isOnScreen()
1292 + " allDrawn=" + allDrawn + " freezingScreen=" + mAppAnimator.freezingScreen);
1293 }
1294
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001295 if (allDrawn && !mAppAnimator.freezingScreen) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001296 return false;
1297 }
1298
1299 if (mLastTransactionSequence != mService.mTransactionSequence) {
1300 mLastTransactionSequence = mService.mTransactionSequence;
1301 mNumInterestingWindows = mNumDrawnWindows = 0;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001302 startingDisplayed = false;
1303 }
1304
1305 final WindowStateAnimator winAnimator = w.mWinAnimator;
1306
1307 boolean isInterestingAndDrawn = false;
1308
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001309 if (!allDrawn && w.mightAffectAllDrawn()) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001310 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
1311 Slog.v(TAG, "Eval win " + w + ": isDrawn=" + w.isDrawnLw()
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001312 + ", isAnimationSet=" + isSelfAnimating());
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001313 if (!w.isDrawnLw()) {
1314 Slog.v(TAG, "Not displayed: s=" + winAnimator.mSurfaceController
1315 + " pv=" + w.mPolicyVisibility
1316 + " mDrawState=" + winAnimator.drawStateToString()
1317 + " ph=" + w.isParentWindowHidden() + " th=" + hiddenRequested
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001318 + " a=" + isSelfAnimating());
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001319 }
1320 }
1321
1322 if (w != startingWindow) {
1323 if (w.isInteresting()) {
1324 mNumInterestingWindows++;
1325 if (w.isDrawnLw()) {
1326 mNumDrawnWindows++;
1327
1328 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG, "tokenMayBeDrawn: "
1329 + this + " w=" + w + " numInteresting=" + mNumInterestingWindows
1330 + " freezingScreen=" + mAppAnimator.freezingScreen
1331 + " mAppFreezing=" + w.mAppFreezing);
1332
1333 isInterestingAndDrawn = true;
1334 }
1335 }
1336 } else if (w.isDrawnLw()) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001337 if (getController() != null) {
1338 getController().reportStartingWindowDrawn();
1339 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001340 startingDisplayed = true;
1341 }
1342 }
1343
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001344 return isInterestingAndDrawn;
1345 }
1346
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001347 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001348 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001349 // For legacy reasons we process the TaskStack.mExitingAppTokens first in DisplayContent
1350 // before the non-exiting app tokens. So, we skip the exiting app tokens here.
1351 // TODO: Investigate if we need to continue to do this or if we can just process them
1352 // in-order.
1353 if (mIsExiting && !waitingForReplacement()) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001354 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001355 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001356 return forAllWindowsUnchecked(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001357 }
1358
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001359 boolean forAllWindowsUnchecked(ToBooleanFunction<WindowState> callback,
1360 boolean traverseTopToBottom) {
1361 return super.forAllWindows(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001362 }
1363
1364 @Override
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001365 AppWindowToken asAppWindowToken() {
1366 // I am an app window token!
1367 return this;
1368 }
1369
1370 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001371 boolean fillsParent() {
1372 return mFillsParent;
1373 }
1374
1375 void setFillsParent(boolean fillsParent) {
1376 mFillsParent = fillsParent;
1377 }
1378
Jorim Jaggife762342016-10-13 14:33:27 +02001379 boolean containsDismissKeyguardWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001380 // Window state is transient during relaunch. We are not guaranteed to be frozen during the
1381 // entirety of the relaunch.
1382 if (isRelaunching()) {
1383 return mLastContainsDismissKeyguardWindow;
1384 }
1385
Jorim Jaggife762342016-10-13 14:33:27 +02001386 for (int i = mChildren.size() - 1; i >= 0; i--) {
1387 if ((mChildren.get(i).mAttrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
1388 return true;
1389 }
1390 }
1391 return false;
1392 }
1393
1394 boolean containsShowWhenLockedWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001395 // When we are relaunching, it is possible for us to be unfrozen before our previous
1396 // windows have been added back. Using the cached value ensures that our previous
1397 // showWhenLocked preference is honored until relaunching is complete.
1398 if (isRelaunching()) {
1399 return mLastContainsShowWhenLockedWindow;
1400 }
1401
Jorim Jaggife762342016-10-13 14:33:27 +02001402 for (int i = mChildren.size() - 1; i >= 0; i--) {
1403 if ((mChildren.get(i).mAttrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
1404 return true;
1405 }
1406 }
Bryce Lee081554b2017-05-25 07:52:12 -07001407
Jorim Jaggife762342016-10-13 14:33:27 +02001408 return false;
1409 }
1410
1411 void checkKeyguardFlagsChanged() {
1412 final boolean containsDismissKeyguard = containsDismissKeyguardWindow();
1413 final boolean containsShowWhenLocked = containsShowWhenLockedWindow();
1414 if (containsDismissKeyguard != mLastContainsDismissKeyguardWindow
1415 || containsShowWhenLocked != mLastContainsShowWhenLockedWindow) {
1416 mService.notifyKeyguardFlagsChanged(null /* callback */);
1417 }
1418 mLastContainsDismissKeyguardWindow = containsDismissKeyguard;
1419 mLastContainsShowWhenLockedWindow = containsShowWhenLocked;
1420 }
1421
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001422 WindowState getImeTargetBelowWindow(WindowState w) {
1423 final int index = mChildren.indexOf(w);
1424 if (index > 0) {
1425 final WindowState target = mChildren.get(index - 1);
1426 if (target.canBeImeTarget()) {
1427 return target;
1428 }
1429 }
1430 return null;
1431 }
1432
Winson Chungbe9be7f2017-06-28 10:55:22 -07001433 int getLowestAnimLayer() {
1434 for (int i = 0; i < mChildren.size(); i++) {
1435 final WindowState w = mChildren.get(i);
1436 if (w.mRemoved) {
1437 continue;
1438 }
1439 return w.mWinAnimator.mAnimLayer;
1440 }
1441 return Integer.MAX_VALUE;
1442 }
1443
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001444 WindowState getHighestAnimLayerWindow(WindowState currentTarget) {
1445 WindowState candidate = null;
1446 for (int i = mChildren.indexOf(currentTarget); i >= 0; i--) {
1447 final WindowState w = mChildren.get(i);
1448 if (w.mRemoved) {
1449 continue;
1450 }
1451 if (candidate == null || w.mWinAnimator.mAnimLayer >
1452 candidate.mWinAnimator.mAnimLayer) {
1453 candidate = w;
1454 }
1455 }
1456 return candidate;
1457 }
1458
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001459 /**
1460 * See {@link Activity#setDisablePreviewScreenshots}.
1461 */
1462 void setDisablePreviewScreenshots(boolean disable) {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001463 mDisablePreviewScreenshots = disable;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001464 }
1465
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001466 /**
chaviwd3bf08d2017-08-01 17:24:59 -07001467 * Sets whether the current launch can turn the screen on. See {@link #canTurnScreenOn()}
1468 */
1469 void setCanTurnScreenOn(boolean canTurnScreenOn) {
1470 mCanTurnScreenOn = canTurnScreenOn;
1471 }
1472
1473 /**
1474 * Indicates whether the current launch can turn the screen on. This is to prevent multiple
1475 * relayouts from turning the screen back on. The screen should only turn on at most
1476 * once per activity resume.
1477 *
1478 * @return true if the screen can be turned on.
1479 */
1480 boolean canTurnScreenOn() {
1481 return mCanTurnScreenOn;
1482 }
1483
1484 /**
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001485 * Retrieves whether we'd like to generate a snapshot that's based solely on the theme. This is
1486 * the case when preview screenshots are disabled {@link #setDisablePreviewScreenshots} or when
1487 * we can't take a snapshot for other reasons, for example, if we have a secure window.
1488 *
1489 * @return True if we need to generate an app theme snapshot, false if we'd like to take a real
1490 * screenshot.
1491 */
1492 boolean shouldUseAppThemeSnapshot() {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001493 return mDisablePreviewScreenshots || forAllWindows(w -> (w.mAttrs.flags & FLAG_SECURE) != 0,
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001494 true /* topToBottom */);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001495 }
1496
Wale Ogunwale51362492016-09-08 17:49:17 -07001497 @Override
Robert Carrdee1b3f2017-02-27 11:33:33 -08001498 int getAnimLayerAdjustment() {
1499 return mAppAnimator.animLayerAdjustment;
1500 }
1501
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001502 boolean applyAnimationLocked(WindowManager.LayoutParams lp, int transit, boolean enter,
1503 boolean isVoiceInteraction) {
1504
1505 if (mService.mDisableTransitionAnimation) {
1506 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) {
1507 Slog.v(TAG_WM, "applyAnimation: transition animation is disabled. atoken=" + this);
1508 }
1509 cancelAnimation();
1510 return false;
1511 }
1512
1513 // Only apply an animation if the display isn't frozen. If it is frozen, there is no reason
1514 // to animate and it can cause strange artifacts when we unfreeze the display if some
1515 // different animation is running.
1516 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "AWT#applyAnimationLocked");
1517 if (okToAnimate()) {
1518 final Animation a = loadAnimation(lp, transit, enter, isVoiceInteraction);
1519 if (a != null) {
1520 final AnimationAdapter adapter = new LocalAnimationAdapter(
1521 new WindowAnimationSpec(a, new Point()), mService.mSurfaceAnimationRunner);
1522 startAnimation(getPendingTransaction(), adapter, !isVisible());
1523 if (a.getZAdjustment() == Animation.ZORDER_TOP) {
1524 mAppAnimator.animLayerAdjustment = 1;
1525 getDisplayContent().assignWindowLayers(false /* setLayoutNeeded */);
1526 }
1527 mTransit = transit;
1528 mTransitFlags = mService.mAppTransition.getTransitFlags();
1529 // TODO: Skip first frame and app stack clip mode.
1530 }
1531 } else {
1532 cancelAnimation();
1533 }
1534 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
1535
1536 return isReallyAnimating();
1537 }
1538
1539 private Animation loadAnimation(WindowManager.LayoutParams lp, int transit, boolean enter,
1540 boolean isVoiceInteraction) {
1541 final DisplayContent displayContent = getTask().getDisplayContent();
1542 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
1543 final int width = displayInfo.appWidth;
1544 final int height = displayInfo.appHeight;
1545 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.v(TAG_WM,
1546 "applyAnimation: atoken=" + this);
1547
1548 // Determine the visible rect to calculate the thumbnail clip
1549 final WindowState win = findMainWindow();
1550 final Rect frame = new Rect(0, 0, width, height);
1551 final Rect displayFrame = new Rect(0, 0,
1552 displayInfo.logicalWidth, displayInfo.logicalHeight);
1553 final Rect insets = new Rect();
1554 final Rect stableInsets = new Rect();
1555 Rect surfaceInsets = null;
1556 final boolean freeform = win != null && win.inFreeformWindowingMode();
1557 if (win != null) {
1558 // Containing frame will usually cover the whole screen, including dialog windows.
1559 // For freeform workspace windows it will not cover the whole screen and it also
1560 // won't exactly match the final freeform window frame (e.g. when overlapping with
1561 // the status bar). In that case we need to use the final frame.
1562 if (freeform) {
1563 frame.set(win.mFrame);
1564 } else {
1565 frame.set(win.mContainingFrame);
1566 }
1567 surfaceInsets = win.getAttrs().surfaceInsets;
1568 insets.set(win.mContentInsets);
1569 stableInsets.set(win.mStableInsets);
1570 }
1571
1572 if (mLaunchTaskBehind) {
1573 // Differentiate the two animations. This one which is briefly on the screen
1574 // gets the !enter animation, and the other activity which remains on the
1575 // screen gets the enter animation. Both appear in the mOpeningApps set.
1576 enter = false;
1577 }
1578 if (DEBUG_APP_TRANSITIONS) Slog.d(TAG_WM, "Loading animation for app transition."
1579 + " transit=" + AppTransition.appTransitionToString(transit) + " enter=" + enter
1580 + " frame=" + frame + " insets=" + insets + " surfaceInsets=" + surfaceInsets);
1581 final Configuration displayConfig = displayContent.getConfiguration();
1582 final Animation a = mService.mAppTransition.loadAnimation(lp, transit, enter,
1583 displayConfig.uiMode, displayConfig.orientation, frame, displayFrame, insets,
1584 surfaceInsets, stableInsets, isVoiceInteraction, freeform, getTask().mTaskId);
1585 if (a != null) {
1586 if (DEBUG_ANIM) logWithStack(TAG, "Loaded animation " + a + " for " + this);
1587 final int containingWidth = frame.width();
1588 final int containingHeight = frame.height();
1589 a.initialize(containingWidth, containingHeight, width, height);
1590 a.scaleCurrentDuration(mService.getTransitionAnimationScaleLocked());
1591 }
1592 return a;
1593 }
1594
1595 /**
1596 * This must be called while inside a transaction.
1597 */
1598 void showAllWindowsLocked() {
1599 forAllWindows(windowState -> {
1600 if (DEBUG_VISIBILITY) Slog.v(TAG, "performing show on: " + windowState);
1601 windowState.performShowLocked();
1602 }, false /* traverseTopToBottom */);
Jorim Jaggia5e10572017-11-15 14:36:26 +01001603 }
1604
1605 @Override
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001606 protected void onAnimationFinished() {
1607 super.onAnimationFinished();
1608
1609 mTransit = TRANSIT_UNSET;
1610 mTransitFlags = 0;
1611 mAppAnimator.animLayerAdjustment = 0;
1612
1613 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM | FINISH_LAYOUT_REDO_WALLPAPER,
1614 "AppWindowToken");
1615
1616 if (mService.mInputMethodTarget != null && mService.mInputMethodTarget.mAppToken == this) {
1617 getDisplayContent().computeImeTarget(true /* updateImeTarget */);
1618 }
1619
1620 if (DEBUG_ANIM) Slog.v(TAG, "Animation done in " + this
1621 + ": reportedVisible=" + reportedVisible
1622 + " okToDisplay=" + okToDisplay()
1623 + " okToAnimate=" + okToAnimate()
1624 + " startingDisplayed=" + startingDisplayed);
1625
1626 // WindowState.onExitAnimationDone might modify the children list, so make a copy and then
1627 // traverse the copy.
1628 final ArrayList<WindowState> children = new ArrayList<>(mChildren);
1629 children.forEach(WindowState::onExitAnimationDone);
1630
1631 mService.mAppTransition.notifyAppTransitionFinishedLocked(token);
1632 scheduleAnimation();
1633 }
1634
1635 @Override
1636 boolean isAppAnimating() {
1637 return isSelfAnimating();
1638 }
1639
1640 @Override
1641 boolean isSelfAnimating() {
1642 // If we are about to start a transition, we also need to be considered animating.
1643 return isWaitingForTransitionStart() || isReallyAnimating();
1644 }
1645
1646 /**
1647 * @return True if and only if we are actually running an animation. Note that
1648 * {@link #isSelfAnimating} also returns true if we are waiting for an animation to
1649 * start.
1650 */
1651 private boolean isReallyAnimating() {
1652 return super.isSelfAnimating();
1653 }
1654
1655 boolean isWaitingForTransitionStart() {
1656 return mService.mAppTransition.isTransitionSet()
1657 && (mService.mOpeningApps.contains(this) || mService.mClosingApps.contains(this));
1658 }
1659
1660 public int getTransit() {
1661 return mTransit;
1662 }
1663
1664 int getTransitFlags() {
1665 return mTransitFlags;
1666 }
1667
1668 void clearThumbnail() {
1669 // TODO
1670 }
1671
1672 @Override
1673 void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1674 super.dump(pw, prefix, dumpAll);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001675 if (appToken != null) {
Wale Ogunwale72919d22016-12-08 18:58:50 -08001676 pw.println(prefix + "app=true mVoiceInteraction=" + mVoiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001677 }
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001678 pw.print(prefix); pw.print("task="); pw.println(getTask());
Wale Ogunwale51362492016-09-08 17:49:17 -07001679 pw.print(prefix); pw.print(" mFillsParent="); pw.print(mFillsParent);
1680 pw.print(" mOrientation="); pw.println(mOrientation);
Wale Ogunwale89973222017-04-23 18:39:45 -07001681 pw.println(prefix + "hiddenRequested=" + hiddenRequested + " mClientHidden=" + mClientHidden
1682 + ((mDeferHidingClient) ? " mDeferHidingClient=" + mDeferHidingClient : "")
1683 + " reportedDrawn=" + reportedDrawn + " reportedVisible=" + reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -07001684 if (paused) {
1685 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001686 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -08001687 if (mAppStopped) {
1688 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
1689 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001690 if (mNumInterestingWindows != 0 || mNumDrawnWindows != 0
Craig Mautner6fbda632012-07-03 09:26:39 -07001691 || allDrawn || mAppAnimator.allDrawn) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001692 pw.print(prefix); pw.print("mNumInterestingWindows=");
1693 pw.print(mNumInterestingWindows);
1694 pw.print(" mNumDrawnWindows="); pw.print(mNumDrawnWindows);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001695 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -07001696 pw.print(" allDrawn="); pw.print(allDrawn);
1697 pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
1698 pw.println(")");
1699 }
1700 if (inPendingTransaction) {
1701 pw.print(prefix); pw.print("inPendingTransaction=");
1702 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001703 }
Craig Mautner799bc1d2015-01-14 10:33:48 -08001704 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001705 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
1706 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -08001707 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -08001708 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001709 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001710 if (startingWindow != null || startingSurface != null
Wale Ogunwale6c459212017-05-17 08:56:03 -07001711 || startingDisplayed || startingMoved || mHiddenSetFromTransferredStartingWindow) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001712 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001713 pw.print(" startingSurface="); pw.print(startingSurface);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001714 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale6c459212017-05-17 08:56:03 -07001715 pw.print(" startingMoved="); pw.print(startingMoved);
1716 pw.println(" mHiddenSetFromTransferredStartingWindow="
1717 + mHiddenSetFromTransferredStartingWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001718 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001719 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -08001720 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001721 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -08001722 }
1723 if (mPendingRelaunchCount != 0) {
1724 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +01001725 }
Wale Ogunwale9c64cb62017-04-12 13:39:59 -07001726 if (getController() != null) {
1727 pw.print(prefix); pw.print("controller="); pw.println(getController());
1728 }
Wale Ogunwalee287e192017-04-21 09:30:12 -07001729 if (mRemovingFromDisplay) {
1730 pw.println(prefix + "mRemovingFromDisplay=" + mRemovingFromDisplay);
1731 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001732 }
1733
1734 @Override
1735 void setHidden(boolean hidden) {
1736 super.setHidden(hidden);
1737 scheduleAnimation();
1738 }
1739
1740 @Override
1741 void prepareSurfaces() {
1742 // isSelfAnimating also returns true when we are about to start a transition, so we need
1743 // to check super here.
1744 final boolean reallyAnimating = super.isSelfAnimating();
1745 final boolean show = !isHidden() || reallyAnimating;
1746 if (show && !mLastSurfaceShowing) {
1747 mPendingTransaction.show(mSurfaceControl);
1748 } else if (!show && mLastSurfaceShowing) {
1749 mPendingTransaction.hide(mSurfaceControl);
Jorim Jaggi4d1835d2017-08-31 17:28:27 +02001750 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001751 mLastSurfaceShowing = show;
1752 super.prepareSurfaces();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001753 }
1754
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001755 @CallSuper
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001756 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02001757 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07001758 final long token = proto.start(fieldId);
1759 writeNameToProto(proto, NAME);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001760 super.writeToProto(proto, WINDOW_TOKEN, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001761 proto.end(token);
1762 }
1763
1764 void writeNameToProto(ProtoOutputStream proto, long fieldId) {
1765 if (appToken == null) {
1766 return;
1767 }
1768 try {
1769 proto.write(fieldId, appToken.getName());
1770 } catch (RemoteException e) {
1771 // This shouldn't happen, but in this case fall back to outputting nothing
1772 Slog.e(TAG, e.toString());
1773 }
1774 }
1775
1776 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001777 public String toString() {
1778 if (stringName == null) {
1779 StringBuilder sb = new StringBuilder();
1780 sb.append("AppWindowToken{");
1781 sb.append(Integer.toHexString(System.identityHashCode(this)));
1782 sb.append(" token="); sb.append(token); sb.append('}');
1783 stringName = sb.toString();
1784 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001785 return stringName + ((mIsExiting) ? " mIsExiting=" : "");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001786 }
Jeff Browne9bdb312012-04-05 15:30:10 -07001787}