blob: 04a1beb202048e34f0b6f211544c9d74ee8d455b [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 Jaggi988f6682017-11-17 17:46:43 +010058import android.graphics.GraphicBuffer;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020059import android.graphics.Point;
Jorim Jaggi0429f352015-12-22 16:29:16 +010060import android.graphics.Rect;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070061import android.os.Binder;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080062import android.os.Debug;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070063import android.os.IBinder;
Steven Timotiusaf03df62017-07-18 16:56:43 -070064import android.os.RemoteException;
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
Wale Ogunwale72919d22016-12-08 18:58:50 -080098 final boolean mVoiceInteraction;
Dianne Hackborne30e02f2014-05-27 18:24:45 -070099
Wale Ogunwale51362492016-09-08 17:49:17 -0700100 /** @see WindowContainer#fillsParent() */
101 private boolean mFillsParent;
Craig Mautner4c5eb222013-11-18 12:59:05 -0800102 boolean layoutConfigChanges;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800103 boolean mShowForAllUsers;
104 int mTargetSdk;
Craig Mautnera2c77052012-03-26 12:14:43 -0700105
Bryce Lee6d410262017-02-28 15:30:17 -0800106 // Flag set while reparenting to prevent actions normally triggered by an individual parent
107 // change.
108 private boolean mReparenting;
109
Wale Ogunwalee287e192017-04-21 09:30:12 -0700110 // True if we are current in the process of removing this app token from the display
111 private boolean mRemovingFromDisplay = false;
112
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800113 // The input dispatching timeout for this application token in nanoseconds.
Wale Ogunwale72919d22016-12-08 18:58:50 -0800114 long mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800115
116 // These are used for determining when all windows associated with
117 // an activity have been drawn, so they can be made visible together
118 // at the same time.
Craig Mautner764983d2012-03-22 11:37:36 -0700119 // initialize so that it doesn't match mTransactionSequence which is an int.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700120 private long mLastTransactionSequence = Long.MIN_VALUE;
121 private int mNumInterestingWindows;
122 private int mNumDrawnWindows;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800123 boolean inPendingTransaction;
124 boolean allDrawn;
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000125 private boolean mLastAllDrawn;
126
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
Jorim Jaggi988f6682017-11-17 17:46:43 +0100211 private AppWindowThumbnail mThumbnail;
212
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000213 /** Have we been asked to have this token keep the screen frozen? */
214 private boolean mFreezingScreen;
215
216 /** Whether this token should be boosted at the top of all app window tokens. */
217 private boolean mNeedsZBoost;
218
Wale Ogunwale72919d22016-12-08 18:58:50 -0800219 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
220 DisplayContent dc, long inputDispatchingTimeoutNanos, boolean fullscreen,
221 boolean showForAllUsers, int targetSdk, int orientation, int rotationAnimationHint,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800222 int configChanges, boolean launchTaskBehind, boolean alwaysFocusable,
Bryce Leef3c6a472017-11-14 14:53:06 -0800223 AppWindowContainerController controller) {
224 this(service, token, voiceInteraction, dc, fullscreen);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800225 setController(controller);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800226 mInputDispatchingTimeoutNanos = inputDispatchingTimeoutNanos;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800227 mShowForAllUsers = showForAllUsers;
228 mTargetSdk = targetSdk;
229 mOrientation = orientation;
230 layoutConfigChanges = (configChanges & (CONFIG_SCREEN_SIZE | CONFIG_ORIENTATION)) != 0;
231 mLaunchTaskBehind = launchTaskBehind;
232 mAlwaysFocusable = alwaysFocusable;
233 mRotationAnimationHint = rotationAnimationHint;
234
235 // Application tokens start out hidden.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200236 setHidden(true);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800237 hiddenRequested = true;
238 }
239
240 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
Bryce Leef3c6a472017-11-14 14:53:06 -0800241 DisplayContent dc, boolean fillsParent) {
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800242 super(service, token != null ? token.asBinder() : null, TYPE_APPLICATION, true, dc,
243 false /* ownerCanManageAppTokens */);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700244 appToken = token;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800245 mVoiceInteraction = voiceInteraction;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800246 mFillsParent = fillsParent;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800247 mInputApplicationHandle = new InputApplicationHandle(this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800248 }
249
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800250 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
251 firstWindowDrawn = true;
252
253 // We now have a good window to show, remove dead placeholders
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700254 removeDeadWindows();
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800255
Jorim Jaggi02886a82016-12-06 09:10:06 -0800256 if (startingWindow != null) {
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800257 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
258 + win.mToken + ": first real window is shown, no animation");
259 // If this initial window is animating, stop it -- we will do an animation to reveal
260 // it from behind the starting window, so there is no need for it to also be doing its
261 // own stuff.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100262 win.cancelAnimation();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800263 if (getController() != null) {
264 getController().removeStartingWindow();
265 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800266 }
267 updateReportedVisibilityLocked();
268 }
269
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800270 void updateReportedVisibilityLocked() {
271 if (appToken == null) {
272 return;
273 }
274
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700275 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700276 final int count = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800277
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700278 mReportedVisibilityResults.reset();
279
280 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700281 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700282 win.updateReportedVisibility(mReportedVisibilityResults);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800283 }
284
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700285 int numInteresting = mReportedVisibilityResults.numInteresting;
286 int numVisible = mReportedVisibilityResults.numVisible;
287 int numDrawn = mReportedVisibilityResults.numDrawn;
288 boolean nowGone = mReportedVisibilityResults.nowGone;
289
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700290 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200291 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting && !isHidden();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700292 if (!nowGone) {
293 // If the app is not yet gone, then it can only become visible/drawn.
294 if (!nowDrawn) {
295 nowDrawn = reportedDrawn;
296 }
297 if (!nowVisible) {
298 nowVisible = reportedVisible;
299 }
300 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800301 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800302 + numInteresting + " visible=" + numVisible);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800303 final AppWindowContainerController controller = getController();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700304 if (nowDrawn != reportedDrawn) {
305 if (nowDrawn) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800306 if (controller != null) {
307 controller.reportWindowsDrawn();
308 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700309 }
310 reportedDrawn = nowDrawn;
311 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800312 if (nowVisible != reportedVisible) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700313 if (DEBUG_VISIBILITY) Slog.v(TAG,
314 "Visibility changed in " + this + ": vis=" + nowVisible);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800315 reportedVisible = nowVisible;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800316 if (controller != null) {
317 if (nowVisible) {
318 controller.reportWindowsVisible();
319 } else {
320 controller.reportWindowsGone();
321 }
322 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800323 }
324 }
325
Wale Ogunwale89973222017-04-23 18:39:45 -0700326 boolean isClientHidden() {
327 return mClientHidden;
328 }
329
330 void setClientHidden(boolean hideClient) {
331 if (mClientHidden == hideClient || (hideClient && mDeferHidingClient)) {
332 return;
333 }
334 mClientHidden = hideClient;
335 sendAppVisibilityToClients();
336 }
337
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700338 boolean setVisibility(WindowManager.LayoutParams lp,
339 boolean visible, int transit, boolean performLayout, boolean isVoiceInteraction) {
340
341 boolean delayed = false;
342 inPendingTransaction = false;
Wale Ogunwale9e4721f2017-05-23 19:37:30 -0700343 // Reset the state of mHiddenSetFromTransferredStartingWindow since visibility is actually
344 // been set by the app now.
345 mHiddenSetFromTransferredStartingWindow = false;
Wale Ogunwale89973222017-04-23 18:39:45 -0700346 setClientHidden(!visible);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700347
348 // Allow for state changes and animation to be applied if:
349 // * token is transitioning visibility state
350 // * or the token was marked as hidden and is exiting before we had a chance to play the
351 // transition animation
352 // * or this is an opening app and windows are being replaced.
353 boolean visibilityChanged = false;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200354 if (isHidden() == visible || (isHidden() && mIsExiting) || (visible && waitingForReplacement())) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700355 final AccessibilityController accessibilityController = mService.mAccessibilityController;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700356 boolean changed = false;
357 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200358 "Changing app " + this + " hidden=" + isHidden() + " performLayout=" + performLayout);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700359
360 boolean runningAppAnimation = false;
361
362 if (transit != AppTransition.TRANSIT_UNSET) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200363 if (applyAnimationLocked(lp, transit, visible, isVoiceInteraction)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700364 delayed = runningAppAnimation = true;
365 }
366 final WindowState window = findMainWindow();
367 //TODO (multidisplay): Magnification is supported only for the default display.
368 if (window != null && accessibilityController != null
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700369 && getDisplayContent().getDisplayId() == DEFAULT_DISPLAY) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700370 accessibilityController.onAppWindowTransitionLocked(window, transit);
371 }
372 changed = true;
373 }
374
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700375 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700376 for (int i = 0; i < windowsCount; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700377 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700378 changed |= win.onAppVisibilityChanged(visible, runningAppAnimation);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700379 }
380
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200381 setHidden(!visible);
382 hiddenRequested = !visible;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700383 visibilityChanged = true;
384 if (!visible) {
385 stopFreezingScreen(true, true);
386 } else {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700387 // If we are being set visible, and the starting window is not yet displayed,
388 // then make sure it doesn't get displayed.
389 if (startingWindow != null && !startingWindow.isDrawnLw()) {
390 startingWindow.mPolicyVisibility = false;
391 startingWindow.mPolicyVisibilityAfterAnim = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700392 }
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700393
394 // We are becoming visible, so better freeze the screen with the windows that are
395 // getting visible so we also wait for them.
396 forAllWindows(mService::makeWindowFreezingScreenIfNeededLocked, true);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700397 }
398
399 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "setVisibility: " + this
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200400 + ": hidden=" + isHidden() + " hiddenRequested=" + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700401
402 if (changed) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700403 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700404 if (performLayout) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700405 mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700406 false /*updateInputWindows*/);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700407 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700408 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700409 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700410 }
411 }
412
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200413 if (isReallyAnimating()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700414 delayed = true;
415 }
416
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700417 for (int i = mChildren.size() - 1; i >= 0 && !delayed; i--) {
Jorim Jaggia5e10572017-11-15 14:36:26 +0100418 if ((mChildren.get(i)).isSelfOrChildAnimating()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700419 delayed = true;
420 }
421 }
422
423 if (visibilityChanged) {
424 if (visible && !delayed) {
425 // The token was made immediately visible, there will be no entrance animation.
426 // We need to inform the client the enter animation was finished.
427 mEnteringAnimation = true;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700428 mService.mActivityManagerAppTransitionNotifier.onAppTransitionFinishedLocked(token);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700429 }
Robert Carr61b81112017-07-17 18:08:15 -0700430
Robert Carre7cc44d2017-03-20 19:04:30 -0700431 // If we are hidden but there is no delay needed we immediately
432 // apply the Surface transaction so that the ActivityManager
Robert Carr61b81112017-07-17 18:08:15 -0700433 // can have some guarantee on the Surface state following
434 // setting the visibility. This captures cases like dismissing
435 // the docked or pinned stack where there is no app transition.
436 //
437 // In the case of a "Null" animation, there will be
438 // no animation but there will still be a transition set.
439 // We still need to delay hiding the surface such that it
440 // can be synchronized with showing the next surface in the transition.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200441 if (isHidden() && !delayed && !mService.mAppTransition.isTransitionSet()) {
Robert Carr6914f082017-03-20 19:04:30 -0700442 SurfaceControl.openTransaction();
443 for (int i = mChildren.size() - 1; i >= 0; i--) {
444 mChildren.get(i).mWinAnimator.hide("immediately hidden");
445 }
446 SurfaceControl.closeTransaction();
447 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700448
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700449 if (!mService.mClosingApps.contains(this) && !mService.mOpeningApps.contains(this)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700450 // The token is not closing nor opening, so even if there is an animation set, that
451 // doesn't mean that it goes through the normal app transition cycle so we have
452 // to inform the docked controller about visibility change.
Andrii Kulian1e32e022016-09-16 15:29:34 -0700453 // TODO(multi-display): notify docked divider on all displays where visibility was
454 // affected.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700455 mService.getDefaultDisplayContentLocked().getDockedDividerController()
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700456 .notifyAppVisibilityChanged();
Jorim Jaggi8b702ed2017-01-20 16:59:03 +0100457 mService.mTaskSnapshotController.notifyAppVisibilityChanged(this, visible);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700458 }
459 }
460
461 return delayed;
462 }
463
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +0200464 /**
465 * @return The to top most child window for which {@link LayoutParams#isFullscreen()} returns
466 * true.
467 */
468 WindowState getTopFullscreenWindow() {
469 for (int i = mChildren.size() - 1; i >= 0; i--) {
470 final WindowState win = mChildren.get(i);
471 if (win != null && win.mAttrs.isFullscreen()) {
472 return win;
473 }
474 }
475 return null;
476 }
477
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800478 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700479 WindowState candidate = null;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700480 int j = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800481 while (j > 0) {
482 j--;
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700483 final WindowState win = mChildren.get(j);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700484 final int type = win.mAttrs.type;
485 // No need to loop through child window as base application and starting types can't be
486 // child windows.
487 if (type == TYPE_BASE_APPLICATION || type == TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700488 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900489 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700490 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800491 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700492 candidate = win;
493 } else {
494 return win;
495 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800496 }
497 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700498 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800499 }
500
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800501 boolean windowsAreFocusable() {
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700502 return getWindowConfiguration().canReceiveKeys() || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800503 }
504
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800505 AppWindowContainerController getController() {
506 final WindowContainerController controller = super.getController();
507 return controller != null ? (AppWindowContainerController) controller : null;
508 }
509
Wale Ogunwale571771c2016-08-26 13:18:50 -0700510 @Override
Wale Ogunwale44f21802016-09-02 12:49:48 -0700511 boolean isVisible() {
Wale Ogunwalee471be62016-10-03 07:53:55 -0700512 // If the app token isn't hidden then it is considered visible and there is no need to check
513 // its children windows to see if they are visible.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200514 return !isHidden();
Wale Ogunwale44f21802016-09-02 12:49:48 -0700515 }
516
517 @Override
Wale Ogunwalee287e192017-04-21 09:30:12 -0700518 void removeImmediately() {
519 onRemovedFromDisplay();
520 super.removeImmediately();
521 }
522
523 @Override
Wale Ogunwale571771c2016-08-26 13:18:50 -0700524 void removeIfPossible() {
Craig Mautnere3119b72015-01-20 15:02:36 -0800525 mIsExiting = false;
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800526 removeAllWindowsIfPossible();
Bryce Lee6d410262017-02-28 15:30:17 -0800527 removeImmediately();
Craig Mautnere3119b72015-01-20 15:02:36 -0800528 }
529
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700530 @Override
531 boolean checkCompleteDeferredRemoval() {
532 if (mIsExiting) {
533 removeIfPossible();
534 }
535 return super.checkCompleteDeferredRemoval();
536 }
537
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700538 void onRemovedFromDisplay() {
Wale Ogunwalee287e192017-04-21 09:30:12 -0700539 if (mRemovingFromDisplay) {
540 return;
541 }
542 mRemovingFromDisplay = true;
543
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700544 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app token: " + this);
545
Wale Ogunwale72919d22016-12-08 18:58:50 -0800546 boolean delayed = setVisibility(null, false, TRANSIT_UNSET, true, mVoiceInteraction);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700547
548 mService.mOpeningApps.remove(this);
Jorim Jaggi45ca0542017-05-30 16:16:01 -0700549 mService.mUnknownAppVisibilityController.appRemovedOrHidden(this);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100550 mService.mTaskSnapshotController.onAppRemoved(this);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700551 waitingToShow = false;
552 if (mService.mClosingApps.contains(this)) {
553 delayed = true;
554 } else if (mService.mAppTransition.isTransitionSet()) {
555 mService.mClosingApps.add(this);
556 delayed = true;
557 }
558
559 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app " + this + " delayed=" + delayed
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200560 + " animation=" + getAnimation() + " animating=" + isSelfAnimating());
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700561
562 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM, "removeAppToken: "
563 + this + " delayed=" + delayed + " Callers=" + Debug.getCallers(4));
564
Jorim Jaggi19be6052017-08-03 18:33:43 +0200565 if (startingData != null && getController() != null) {
566 getController().removeStartingWindow();
567 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800568
Winson Chung87e5d552017-04-05 11:49:38 -0700569 // If this window was animating, then we need to ensure that the app transition notifies
570 // that animations have completed in WMS.handleAnimatingStoppedAndTransitionLocked(), so
571 // add to that list now
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200572 if (isSelfAnimating()) {
Winson Chung87e5d552017-04-05 11:49:38 -0700573 mService.mNoAnimationNotifyOnTransitionFinished.add(token);
574 }
575
Wale Ogunwalee287e192017-04-21 09:30:12 -0700576 final TaskStack stack = getStack();
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700577 if (delayed && !isEmpty()) {
578 // set the token aside because it has an active animation to be finished
579 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM,
580 "removeAppToken make exiting: " + this);
Wale Ogunwalee287e192017-04-21 09:30:12 -0700581 if (stack != null) {
582 stack.mExitingAppTokens.add(this);
583 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700584 mIsExiting = true;
585 } else {
586 // Make sure there is no animation running on this token, so any windows associated
587 // with it will be removed as soon as their animations are complete
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200588 cancelAnimation();
Wale Ogunwale3106ae012017-05-16 08:56:37 -0700589 if (stack != null) {
590 stack.mExitingAppTokens.remove(this);
591 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700592 removeIfPossible();
593 }
594
595 removed = true;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700596 stopFreezingScreen(true, true);
Bryce Lee6d410262017-02-28 15:30:17 -0800597
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700598 if (mService.mFocusedApp == this) {
599 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "Removing focused app token:" + this);
600 mService.mFocusedApp = null;
601 mService.updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL, true /*updateInputWindows*/);
602 mService.mInputMonitor.setFocusedAppLw(null);
603 }
604
605 if (!delayed) {
606 updateReportedVisibilityLocked();
607 }
Wale Ogunwalee287e192017-04-21 09:30:12 -0700608
609 mRemovingFromDisplay = false;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700610 }
611
Chong Zhange05bcb12016-07-26 17:47:29 -0700612 void clearAnimatingFlags() {
Chong Zhangb0d26702016-08-12 16:03:29 -0700613 boolean wallpaperMightChange = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700614 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700615 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700616 wallpaperMightChange |= win.clearAnimatingFlags();
Chong Zhange05bcb12016-07-26 17:47:29 -0700617 }
Chong Zhangb0d26702016-08-12 16:03:29 -0700618 if (wallpaperMightChange) {
619 requestUpdateWallpaperIfNeeded();
620 }
Chong Zhange05bcb12016-07-26 17:47:29 -0700621 }
622
Robert Carre12aece2016-02-02 22:43:27 -0800623 void destroySurfaces() {
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700624 destroySurfaces(false /*cleanupOnResume*/);
625 }
626
627 /**
628 * Destroy surfaces which have been marked as eligible by the animator, taking care to ensure
629 * the client has finished with them.
630 *
631 * @param cleanupOnResume whether this is done when app is resumed without fully stopped. If
632 * set to true, destroy only surfaces of removed windows, and clear relevant flags of the
633 * others so that they are ready to be reused. If set to false (common case), destroy all
634 * surfaces that's eligible, if the app is already stopped.
635 */
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700636 private void destroySurfaces(boolean cleanupOnResume) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700637 boolean destroyedSomething = false;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100638
639 // Copying to a different list as multiple children can be removed.
640 // TODO: Not sure why this is needed.
641 final LinkedList<WindowState> children = new LinkedList<>(mChildren);
642 for (int i = children.size() - 1; i >= 0; i--) {
643 final WindowState win = children.get(i);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700644 destroyedSomething |= win.destroySurface(cleanupOnResume, mAppStopped);
Robert Carre12aece2016-02-02 22:43:27 -0800645 }
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700646 if (destroyedSomething) {
647 final DisplayContent dc = getDisplayContent();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700648 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Robert Carre12aece2016-02-02 22:43:27 -0800649 }
650 }
651
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800652 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700653 * Notify that the app is now resumed, and it was not stopped before, perform a clean
654 * up of the surfaces
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800655 */
Jorim Jaggibae01b12017-04-11 16:29:10 -0700656 void notifyAppResumed(boolean wasStopped) {
Chong Zhangad24f962016-08-25 12:12:33 -0700657 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppResumed: wasStopped=" + wasStopped
Jorim Jaggibae01b12017-04-11 16:29:10 -0700658 + " " + this);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700659 mAppStopped = false;
chaviwd3bf08d2017-08-01 17:24:59 -0700660 // Allow the window to turn the screen on once the app is resumed again.
661 setCanTurnScreenOn(true);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700662 if (!wasStopped) {
663 destroySurfaces(true /*cleanupOnResume*/);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800664 }
Robert Carre12aece2016-02-02 22:43:27 -0800665 }
666
Chong Zhangbef461f2015-10-27 11:38:24 -0700667 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700668 * Notify that the app has stopped, and it is okay to destroy any surfaces which were
669 * keeping alive in case they were still being used.
670 */
671 void notifyAppStopped() {
672 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: " + this);
673 mAppStopped = true;
674 destroySurfaces();
675 // Remove any starting window that was added for this app if they are still around.
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800676 if (getController() != null) {
677 getController().removeStartingWindow();
678 }
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700679 }
680
Chong Zhang92147042016-05-09 12:47:11 -0700681 void clearAllDrawn() {
682 allDrawn = false;
683 deferClearAllDrawn = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700684 }
685
Bryce Lee6d410262017-02-28 15:30:17 -0800686 Task getTask() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800687 return (Task) getParent();
Bryce Lee6d410262017-02-28 15:30:17 -0800688 }
689
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800690 TaskStack getStack() {
691 final Task task = getTask();
692 if (task != null) {
693 return task.mStack;
694 } else {
695 return null;
696 }
697 }
698
Bryce Lee6d410262017-02-28 15:30:17 -0800699 @Override
700 void onParentSet() {
701 super.onParentSet();
702
Robert Carred3e83b2017-04-21 13:26:55 -0700703 final Task task = getTask();
704
Bryce Lee6d410262017-02-28 15:30:17 -0800705 // When the associated task is {@code null}, the {@link AppWindowToken} can no longer
706 // access visual elements like the {@link DisplayContent}. We must remove any associations
707 // such as animations.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800708 if (!mReparenting) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800709 if (task == null) {
710 // It is possible we have been marked as a closing app earlier. We must remove ourselves
711 // from this list so we do not participate in any future animations.
712 mService.mClosingApps.remove(this);
Robert Carred3e83b2017-04-21 13:26:55 -0700713 } else if (mLastParent != null && mLastParent.mStack != null) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800714 task.mStack.mExitingAppTokens.remove(this);
715 }
Bryce Lee6d410262017-02-28 15:30:17 -0800716 }
Robert Carred3e83b2017-04-21 13:26:55 -0700717 mLastParent = task;
Bryce Lee6d410262017-02-28 15:30:17 -0800718 }
719
Wale Ogunwalefa854eb2016-09-20 13:43:52 -0700720 void postWindowRemoveStartingWindowCleanup(WindowState win) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700721 // TODO: Something smells about the code below...Is there a better way?
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700722 if (startingWindow == win) {
723 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Notify removed startingWindow " + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800724 if (getController() != null) {
725 getController().removeStartingWindow();
726 }
Wale Ogunwale6c459212017-05-17 08:56:03 -0700727 } else if (mChildren.size() == 0) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700728 // If this is the last window and we had requested a starting transition window,
729 // well there is no point now.
Jorim Jaggi02886a82016-12-06 09:10:06 -0800730 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Nulling last startingData");
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700731 startingData = null;
Wale Ogunwale6c459212017-05-17 08:56:03 -0700732 if (mHiddenSetFromTransferredStartingWindow) {
733 // We set the hidden state to false for the token from a transferred starting window.
734 // We now reset it back to true since the starting window was the last window in the
735 // token.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200736 setHidden(true);
Wale Ogunwale6c459212017-05-17 08:56:03 -0700737 }
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100738 } else if (mChildren.size() == 1 && startingSurface != null && !isRelaunching()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700739 // If this is the last window except for a starting transition window,
740 // we need to get rid of the starting transition.
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200741 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Last window, removing starting window "
742 + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800743 if (getController() != null) {
744 getController().removeStartingWindow();
745 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700746 }
747 }
748
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700749 void removeDeadWindows() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700750 for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700751 WindowState win = mChildren.get(winNdx);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800752 if (win.mAppDied) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700753 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.w(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700754 "removeDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800755 // Set mDestroying, we don't want any animation or delayed removal here.
756 win.mDestroying = true;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700757 // Also removes child windows.
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700758 win.removeIfPossible();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800759 }
760 }
761 }
762
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700763 boolean hasWindowsAlive() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700764 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700765 // No need to loop through child windows as the answer should be the same as that of the
766 // parent window.
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700767 if (!(mChildren.get(i)).mAppDied) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700768 return true;
769 }
770 }
771 return false;
772 }
773
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700774 void setWillReplaceWindows(boolean animate) {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700775 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
776 "Marking app token " + this + " with replacing windows.");
Robert Carra1eb4392015-12-10 12:43:51 -0800777
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700778 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700779 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700780 w.setWillReplaceWindow(animate);
Robert Carra1eb4392015-12-10 12:43:51 -0800781 }
Robert Carra1eb4392015-12-10 12:43:51 -0800782 }
783
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700784 void setWillReplaceChildWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700785 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + this
Robert Carr23fa16b2016-01-13 13:19:58 -0800786 + " with replacing child windows.");
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700787 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700788 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700789 w.setWillReplaceChildWindows();
Robert Carr23fa16b2016-01-13 13:19:58 -0800790 }
791 }
792
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700793 void clearWillReplaceWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700794 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
795 "Resetting app token " + this + " of replacing window marks.");
Chong Zhangf596cd52016-01-05 13:42:44 -0800796
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);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700799 w.clearWillReplaceWindow();
Chong Zhangf596cd52016-01-05 13:42:44 -0800800 }
801 }
802
Chong Zhang4d7369a2016-04-25 16:09:14 -0700803 void requestUpdateWallpaperIfNeeded() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700804 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700805 final WindowState w = mChildren.get(i);
Chong Zhang4d7369a2016-04-25 16:09:14 -0700806 w.requestUpdateWallpaperIfNeeded();
807 }
808 }
809
Chong Zhangd78ddb42016-03-02 17:01:14 -0800810 boolean isRelaunching() {
811 return mPendingRelaunchCount > 0;
812 }
813
Robert Carr68375192017-06-13 12:41:53 -0700814 boolean shouldFreezeBounds() {
815 final Task task = getTask();
816
817 // For freeform windows, we can't freeze the bounds at the moment because this would make
818 // the resizing unresponsive.
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700819 if (task == null || task.inFreeformWindowingMode()) {
Robert Carr68375192017-06-13 12:41:53 -0700820 return false;
821 }
822
823 // We freeze the bounds while drag resizing to deal with the time between
824 // the divider/drag handle being released, and the handling it's new
825 // configuration. If we are relaunched outside of the drag resizing state,
826 // we need to be careful not to do this.
827 return getTask().isDragResizing();
828 }
829
Chong Zhangd78ddb42016-03-02 17:01:14 -0800830 void startRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700831 if (shouldFreezeBounds()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -0800832 freezeBounds();
833 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800834
835 // In the process of tearing down before relaunching, the app will
836 // try and clean up it's child surfaces. We need to prevent this from
837 // happening, so we sever the children, transfering their ownership
838 // from the client it-self to the parent surface (owned by us).
839 for (int i = mChildren.size() - 1; i >= 0; i--) {
840 final WindowState w = mChildren.get(i);
841 w.mWinAnimator.detachChildren();
842 }
843
Chong Zhangd78ddb42016-03-02 17:01:14 -0800844 mPendingRelaunchCount++;
845 }
846
847 void finishRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700848 unfreezeBounds();
849
Chong Zhangd78ddb42016-03-02 17:01:14 -0800850 if (mPendingRelaunchCount > 0) {
851 mPendingRelaunchCount--;
Bryce Lee081554b2017-05-25 07:52:12 -0700852 } else {
853 // Update keyguard flags upon finishing relaunch.
854 checkKeyguardFlagsChanged();
Chong Zhangd78ddb42016-03-02 17:01:14 -0800855 }
856 }
857
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700858 void clearRelaunching() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700859 if (mPendingRelaunchCount == 0) {
860 return;
861 }
Robert Carr68375192017-06-13 12:41:53 -0700862 unfreezeBounds();
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700863 mPendingRelaunchCount = 0;
864 }
865
Wale Ogunwale07bcab72016-10-14 15:30:09 -0700866 /**
867 * Returns true if the new child window we are adding to this token is considered greater than
868 * the existing child window in this token in terms of z-order.
869 */
870 @Override
871 protected boolean isFirstChildWindowGreaterThanSecond(WindowState newWindow,
872 WindowState existingWindow) {
873 final int type1 = newWindow.mAttrs.type;
874 final int type2 = existingWindow.mAttrs.type;
875
876 // Base application windows should be z-ordered BELOW all other windows in the app token.
877 if (type1 == TYPE_BASE_APPLICATION && type2 != TYPE_BASE_APPLICATION) {
878 return false;
879 } else if (type1 != TYPE_BASE_APPLICATION && type2 == TYPE_BASE_APPLICATION) {
880 return true;
881 }
882
883 // Starting windows should be z-ordered ABOVE all other windows in the app token.
884 if (type1 == TYPE_APPLICATION_STARTING && type2 != TYPE_APPLICATION_STARTING) {
885 return true;
886 } else if (type1 != TYPE_APPLICATION_STARTING && type2 == TYPE_APPLICATION_STARTING) {
887 return false;
888 }
889
890 // Otherwise the new window is greater than the existing window.
891 return true;
892 }
893
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700894 @Override
Robert Carra1eb4392015-12-10 12:43:51 -0800895 void addWindow(WindowState w) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700896 super.addWindow(w);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700897
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700898 boolean gotReplacementWindow = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700899 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700900 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700901 gotReplacementWindow |= candidate.setReplacementWindowIfNeeded(w);
902 }
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700903
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700904 // if we got a replacement window, reset the timeout to give drawing more time
905 if (gotReplacementWindow) {
906 mService.scheduleWindowReplacementTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -0800907 }
Jorim Jaggife762342016-10-13 14:33:27 +0200908 checkKeyguardFlagsChanged();
909 }
910
911 @Override
912 void removeChild(WindowState child) {
913 super.removeChild(child);
914 checkKeyguardFlagsChanged();
Robert Carra1eb4392015-12-10 12:43:51 -0800915 }
916
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700917 private boolean waitingForReplacement() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700918 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700919 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700920 if (candidate.waitingForReplacement()) {
Robert Carra1eb4392015-12-10 12:43:51 -0800921 return true;
922 }
923 }
924 return false;
925 }
926
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700927 void onWindowReplacementTimeout() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700928 for (int i = mChildren.size() - 1; i >= 0; --i) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700929 (mChildren.get(i)).onWindowReplacementTimeout();
Robert Carra1eb4392015-12-10 12:43:51 -0800930 }
931 }
932
Winson Chung30480042017-01-26 10:55:34 -0800933 void reparent(Task task, int position) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800934 final Task currentTask = getTask();
935 if (task == currentTask) {
Winson Chung30480042017-01-26 10:55:34 -0800936 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800937 "window token=" + this + " already child of task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -0800938 }
Bryce Lee6d410262017-02-28 15:30:17 -0800939
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800940 if (currentTask.mStack != task.mStack) {
Bryce Lee6d410262017-02-28 15:30:17 -0800941 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800942 "window token=" + this + " current task=" + currentTask
Bryce Lee6d410262017-02-28 15:30:17 -0800943 + " belongs to a different stack than " + task);
944 }
945
Winson Chung30480042017-01-26 10:55:34 -0800946 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "reParentWindowToken: removing window token=" + this
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800947 + " from task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -0800948 final DisplayContent prevDisplayContent = getDisplayContent();
949
Bryce Lee6d410262017-02-28 15:30:17 -0800950 mReparenting = true;
951
Winson Chung30480042017-01-26 10:55:34 -0800952 getParent().removeChild(this);
953 task.addChild(this, position);
954
Bryce Lee6d410262017-02-28 15:30:17 -0800955 mReparenting = false;
956
Winson Chung30480042017-01-26 10:55:34 -0800957 // Relayout display(s).
958 final DisplayContent displayContent = task.getDisplayContent();
959 displayContent.setLayoutNeeded();
960 if (prevDisplayContent != displayContent) {
961 onDisplayChanged(displayContent);
962 prevDisplayContent.setLayoutNeeded();
963 }
964 }
965
Jorim Jaggi0429f352015-12-22 16:29:16 +0100966 /**
967 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
968 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
969 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
970 * with a queue.
971 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800972 private void freezeBounds() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800973 final Task task = getTask();
974 mFrozenBounds.offer(new Rect(task.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700975
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800976 if (task.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700977 // We didn't call prepareFreezingBounds on the task, so use the current value.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800978 mFrozenMergedConfig.offer(new Configuration(task.getConfiguration()));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700979 } else {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800980 mFrozenMergedConfig.offer(new Configuration(task.mPreparedFrozenMergedConfig));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700981 }
Andrii Kulianb10330d2016-09-16 13:51:46 -0700982 // Calling unset() to make it equal to Configuration.EMPTY.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800983 task.mPreparedFrozenMergedConfig.unset();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100984 }
985
986 /**
987 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
988 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800989 private void unfreezeBounds() {
Robert Carr68375192017-06-13 12:41:53 -0700990 if (mFrozenBounds.isEmpty()) {
991 return;
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700992 }
Robert Carr68375192017-06-13 12:41:53 -0700993 mFrozenBounds.remove();
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700994 if (!mFrozenMergedConfig.isEmpty()) {
995 mFrozenMergedConfig.remove();
996 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700997 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700998 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700999 win.onUnfreezeBounds();
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001000 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001001 mService.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +01001002 }
1003
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001004 void setAppLayoutChanges(int changes, String reason) {
1005 if (!mChildren.isEmpty()) {
1006 final DisplayContent dc = getDisplayContent();
1007 dc.pendingLayoutChanges |= changes;
1008 if (DEBUG_LAYOUT_REPEATS) {
1009 mService.mWindowPlacerLocked.debugLayoutRepeats(reason, dc.pendingLayoutChanges);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001010 }
1011 }
1012 }
1013
1014 void removeReplacedWindowIfNeeded(WindowState replacement) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001015 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001016 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001017 if (win.removeReplacedWindowIfNeeded(replacement)) {
1018 return;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001019 }
1020 }
1021 }
1022
1023 void startFreezingScreen() {
1024 if (DEBUG_ORIENTATION) logWithStack(TAG, "Set freezing of " + appToken + ": hidden="
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001025 + isHidden() + " freezing=" + mFreezingScreen + " hiddenRequested="
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001026 + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001027 if (!hiddenRequested) {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001028 if (!mFreezingScreen) {
1029 mFreezingScreen = true;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001030 mService.registerAppFreezeListener(this);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001031 mService.mAppsFreezingScreen++;
1032 if (mService.mAppsFreezingScreen == 1) {
Bryce Lee8f853582017-06-05 17:25:59 -07001033 mService.startFreezingDisplayLocked(false, 0, 0, getDisplayContent());
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001034 mService.mH.removeMessages(H.APP_FREEZE_TIMEOUT);
1035 mService.mH.sendEmptyMessageDelayed(H.APP_FREEZE_TIMEOUT, 2000);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001036 }
1037 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001038 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001039 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001040 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001041 w.onStartFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001042 }
1043 }
1044 }
1045
1046 void stopFreezingScreen(boolean unfreezeSurfaceNow, boolean force) {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001047 if (!mFreezingScreen) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001048 return;
1049 }
1050 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Clear freezing of " + this + " force=" + force);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001051 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001052 boolean unfrozeWindows = false;
1053 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001054 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001055 unfrozeWindows |= w.onStopFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001056 }
1057 if (force || unfrozeWindows) {
1058 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "No longer freezing: " + this);
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001059 mFreezingScreen = false;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001060 mService.unregisterAppFreezeListener(this);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001061 mService.mAppsFreezingScreen--;
1062 mService.mLastFinishedFreezeSource = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001063 }
1064 if (unfreezeSurfaceNow) {
1065 if (unfrozeWindows) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001066 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001067 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001068 mService.stopFreezingDisplayLocked();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001069 }
1070 }
1071
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001072 @Override
1073 public void onAppFreezeTimeout() {
1074 Slog.w(TAG_WM, "Force clearing freeze: " + this);
1075 stopFreezingScreen(true, true);
1076 }
1077
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001078 boolean transferStartingWindow(IBinder transferFrom) {
Wale Ogunwale02319a62016-09-26 15:21:22 -07001079 final AppWindowToken fromToken = getDisplayContent().getAppWindowToken(transferFrom);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001080 if (fromToken == null) {
1081 return false;
1082 }
1083
1084 final WindowState tStartingWindow = fromToken.startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001085 if (tStartingWindow != null && fromToken.startingSurface != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001086 // In this case, the starting icon has already been displayed, so start
1087 // letting windows get shown immediately without any more transitions.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001088 mService.mSkipAppTransitionAnimation = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001089
1090 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Moving existing starting " + tStartingWindow
1091 + " from " + fromToken + " to " + this);
1092
1093 final long origId = Binder.clearCallingIdentity();
Peter Visontay3556a3b2017-11-01 17:23:17 +00001094 try {
1095 // Transfer the starting window over to the new token.
1096 startingData = fromToken.startingData;
1097 startingSurface = fromToken.startingSurface;
1098 startingDisplayed = fromToken.startingDisplayed;
1099 fromToken.startingDisplayed = false;
1100 startingWindow = tStartingWindow;
1101 reportedVisible = fromToken.reportedVisible;
1102 fromToken.startingData = null;
1103 fromToken.startingSurface = null;
1104 fromToken.startingWindow = null;
1105 fromToken.startingMoved = true;
1106 tStartingWindow.mToken = this;
1107 tStartingWindow.mAppToken = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001108
Peter Visontay3556a3b2017-11-01 17:23:17 +00001109 if (DEBUG_ADD_REMOVE || DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1110 "Removing starting " + tStartingWindow + " from " + fromToken);
1111 fromToken.removeChild(tStartingWindow);
1112 fromToken.postWindowRemoveStartingWindowCleanup(tStartingWindow);
1113 fromToken.mHiddenSetFromTransferredStartingWindow = false;
1114 addWindow(tStartingWindow);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001115
Peter Visontay3556a3b2017-11-01 17:23:17 +00001116 // Propagate other interesting state between the tokens. If the old token is displayed,
1117 // we should immediately force the new one to be displayed. If it is animating, we need
1118 // to move that animation to the new one.
1119 if (fromToken.allDrawn) {
1120 allDrawn = true;
1121 deferClearAllDrawn = fromToken.deferClearAllDrawn;
1122 }
1123 if (fromToken.firstWindowDrawn) {
1124 firstWindowDrawn = true;
1125 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001126 if (!fromToken.isHidden()) {
1127 setHidden(false);
Peter Visontay3556a3b2017-11-01 17:23:17 +00001128 hiddenRequested = false;
1129 mHiddenSetFromTransferredStartingWindow = true;
1130 }
1131 setClientHidden(fromToken.mClientHidden);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001132
Jorim Jaggi980c9de2017-11-17 01:41:37 +01001133 transferAnimation(fromToken);
1134
1135 // When transferring an animation, we no longer need to apply an animation to the
1136 // the token we transfer the animation over. Thus, remove the animation from
1137 // pending opening apps.
1138 mService.mOpeningApps.remove(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001139
Peter Visontay3556a3b2017-11-01 17:23:17 +00001140 mService.updateFocusedWindowLocked(
1141 UPDATE_FOCUS_WILL_PLACE_SURFACES, true /*updateInputWindows*/);
1142 getDisplayContent().setLayoutNeeded();
1143 mService.mWindowPlacerLocked.performSurfacePlacement();
1144 } finally {
1145 Binder.restoreCallingIdentity(origId);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001146 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001147 return true;
1148 } else if (fromToken.startingData != null) {
1149 // The previous app was getting ready to show a
1150 // starting window, but hasn't yet done so. Steal it!
1151 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1152 "Moving pending starting from " + fromToken + " to " + this);
1153 startingData = fromToken.startingData;
1154 fromToken.startingData = null;
1155 fromToken.startingMoved = true;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001156 if (getController() != null) {
1157 getController().scheduleAddStartingWindow();
1158 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001159 return true;
1160 }
1161
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001162 // TODO: Transfer thumbnail
1163
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001164 return false;
1165 }
1166
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001167 boolean isLastWindow(WindowState win) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001168 return mChildren.size() == 1 && mChildren.get(0) == win;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001169 }
1170
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001171 @Override
1172 void onAppTransitionDone() {
1173 sendingToBottom = false;
1174 }
1175
Wale Ogunwale51362492016-09-08 17:49:17 -07001176 /**
1177 * We override because this class doesn't want its children affecting its reported orientation
1178 * in anyway.
1179 */
1180 @Override
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001181 int getOrientation(int candidate) {
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001182 if (candidate == SCREEN_ORIENTATION_BEHIND) {
1183 // Allow app to specify orientation regardless of its visibility state if the current
1184 // candidate want us to use orientation behind. I.e. the visible app on-top of this one
1185 // wants us to use the orientation of the app behind it.
1186 return mOrientation;
1187 }
1188
Bryce Lee61fbcbc2017-03-10 14:14:03 -08001189 // The {@link AppWindowToken} should only specify an orientation when it is not closing or
1190 // going to the bottom. Allowing closing {@link AppWindowToken} to participate can lead to
1191 // an Activity in another task being started in the wrong orientation during the transition.
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001192 if (!(sendingToBottom || mService.mClosingApps.contains(this))
Bryce Lee00d586d2017-07-28 20:48:43 -07001193 && (isVisible() || mService.mOpeningApps.contains(this) || isOnTop())) {
Bryce Leea163b762017-01-24 11:05:01 -08001194 return mOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001195 }
Bryce Leea163b762017-01-24 11:05:01 -08001196
1197 return SCREEN_ORIENTATION_UNSET;
Wale Ogunwale51362492016-09-08 17:49:17 -07001198 }
1199
Wale Ogunwaleb198b742016-12-01 08:44:09 -08001200 /** Returns the app's preferred orientation regardless of its currently visibility state. */
1201 int getOrientationIgnoreVisibility() {
1202 return mOrientation;
1203 }
1204
Craig Mautnerdbb79912012-03-01 18:59:14 -08001205 @Override
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001206 void checkAppWindowsReadyToShow() {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001207 if (allDrawn == mLastAllDrawn) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001208 return;
1209 }
1210
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001211 mLastAllDrawn = allDrawn;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001212 if (!allDrawn) {
1213 return;
1214 }
1215
1216 // The token has now changed state to having all windows shown... what to do, what to do?
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001217 if (mFreezingScreen) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001218 showAllWindowsLocked();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001219 stopFreezingScreen(false, true);
1220 if (DEBUG_ORIENTATION) Slog.i(TAG,
1221 "Setting mOrientationChangeComplete=true because wtoken " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001222 + " numInteresting=" + mNumInterestingWindows + " numDrawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001223 // This will set mOrientationChangeComplete and cause a pass through layout.
1224 setAppLayoutChanges(FINISH_LAYOUT_REDO_WALLPAPER,
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001225 "checkAppWindowsReadyToShow: freezingScreen");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001226 } else {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001227 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM, "checkAppWindowsReadyToShow");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001228
1229 // We can now show all of the drawn windows!
1230 if (!mService.mOpeningApps.contains(this)) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001231 showAllWindowsLocked();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001232 }
1233 }
1234 }
1235
Matthew Ng5d23afa2017-06-21 16:16:24 -07001236 /**
Bryce Leed390deb2017-06-22 13:14:28 -07001237 * Returns whether the drawn window states of this {@link AppWindowToken} has considered every
1238 * child {@link WindowState}. A child is considered if it has been passed into
1239 * {@link #updateDrawnWindowStates(WindowState)} after being added. This is used to determine
1240 * whether states, such as {@code allDrawn}, can be set, which relies on state variables such as
1241 * {@code mNumInterestingWindows}, which depend on all {@link WindowState}s being considered.
1242 *
1243 * @return {@code true} If all children have been considered, {@code false}.
1244 */
1245 private boolean allDrawnStatesConsidered() {
Bryce Lee6311c4b2017-07-06 14:09:29 -07001246 for (int i = mChildren.size() - 1; i >= 0; --i) {
1247 final WindowState child = mChildren.get(i);
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001248 if (child.mightAffectAllDrawn() && !child.getDrawnStateEvaluated()) {
Bryce Leed390deb2017-06-22 13:14:28 -07001249 return false;
1250 }
1251 }
1252 return true;
1253 }
1254
1255 /**
Matthew Ng5d23afa2017-06-21 16:16:24 -07001256 * Determines if the token has finished drawing. This should only be called from
1257 * {@link DisplayContent#applySurfaceChangesTransaction}
1258 */
Matthew Ng498c71d2017-04-18 13:55:45 -07001259 void updateAllDrawn() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001260 if (!allDrawn) {
Matthew Ng498c71d2017-04-18 13:55:45 -07001261 // Number of drawn windows can be less when a window is being relaunched, wait for
Bryce Leed390deb2017-06-22 13:14:28 -07001262 // all windows to be launched and drawn for this token be considered all drawn.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001263 final int numInteresting = mNumInterestingWindows;
Bryce Leed390deb2017-06-22 13:14:28 -07001264
1265 // We must make sure that all present children have been considered (determined by
1266 // {@link #allDrawnStatesConsidered}) before evaluating whether everything has been
1267 // drawn.
1268 if (numInteresting > 0 && allDrawnStatesConsidered()
1269 && mNumDrawnWindows >= numInteresting && !isRelaunching()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001270 if (DEBUG_VISIBILITY) Slog.v(TAG, "allDrawn: " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001271 + " interesting=" + numInteresting + " drawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001272 allDrawn = true;
1273 // Force an additional layout pass where
1274 // WindowStateAnimator#commitFinishDrawingLocked() will call performShowLocked().
Matthew Ng498c71d2017-04-18 13:55:45 -07001275 if (mDisplayContent != null) {
1276 mDisplayContent.setLayoutNeeded();
1277 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001278 mService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
Robert Carrecc06b32017-04-18 14:25:10 -07001279
Winson Chunge7ba6862017-05-24 12:13:33 -07001280 // Notify the pinned stack upon all windows drawn. If there was an animation in
1281 // progress then this signal will resume that animation.
Wale Ogunwale61911492017-10-11 08:50:50 -07001282 final TaskStack pinnedStack = mDisplayContent.getPinnedStack();
Winson Chunge7ba6862017-05-24 12:13:33 -07001283 if (pinnedStack != null) {
1284 pinnedStack.onAllWindowsDrawn();
Robert Carrecc06b32017-04-18 14:25:10 -07001285 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001286 }
1287 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001288 }
1289
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001290 /**
1291 * Updated this app token tracking states for interesting and drawn windows based on the window.
1292 *
1293 * @return Returns true if the input window is considered interesting and drawn while all the
1294 * windows in this app token where not considered drawn as of the last pass.
1295 */
1296 boolean updateDrawnWindowStates(WindowState w) {
Bryce Leed390deb2017-06-22 13:14:28 -07001297 w.setDrawnStateEvaluated(true /*evaluated*/);
1298
Jorim Jaggie4b0f282017-05-17 15:10:29 +02001299 if (DEBUG_STARTING_WINDOW_VERBOSE && w == startingWindow) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001300 Slog.d(TAG, "updateWindows: starting " + w + " isOnScreen=" + w.isOnScreen()
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001301 + " allDrawn=" + allDrawn + " freezingScreen=" + mFreezingScreen);
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001302 }
1303
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001304 if (allDrawn && !mFreezingScreen) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001305 return false;
1306 }
1307
1308 if (mLastTransactionSequence != mService.mTransactionSequence) {
1309 mLastTransactionSequence = mService.mTransactionSequence;
1310 mNumInterestingWindows = mNumDrawnWindows = 0;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001311 startingDisplayed = false;
1312 }
1313
1314 final WindowStateAnimator winAnimator = w.mWinAnimator;
1315
1316 boolean isInterestingAndDrawn = false;
1317
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001318 if (!allDrawn && w.mightAffectAllDrawn()) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001319 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
1320 Slog.v(TAG, "Eval win " + w + ": isDrawn=" + w.isDrawnLw()
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001321 + ", isAnimationSet=" + isSelfAnimating());
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001322 if (!w.isDrawnLw()) {
1323 Slog.v(TAG, "Not displayed: s=" + winAnimator.mSurfaceController
1324 + " pv=" + w.mPolicyVisibility
1325 + " mDrawState=" + winAnimator.drawStateToString()
1326 + " ph=" + w.isParentWindowHidden() + " th=" + hiddenRequested
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001327 + " a=" + isSelfAnimating());
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001328 }
1329 }
1330
1331 if (w != startingWindow) {
1332 if (w.isInteresting()) {
1333 mNumInterestingWindows++;
1334 if (w.isDrawnLw()) {
1335 mNumDrawnWindows++;
1336
1337 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG, "tokenMayBeDrawn: "
1338 + this + " w=" + w + " numInteresting=" + mNumInterestingWindows
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001339 + " freezingScreen=" + mFreezingScreen
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001340 + " mAppFreezing=" + w.mAppFreezing);
1341
1342 isInterestingAndDrawn = true;
1343 }
1344 }
1345 } else if (w.isDrawnLw()) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001346 if (getController() != null) {
1347 getController().reportStartingWindowDrawn();
1348 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001349 startingDisplayed = true;
1350 }
1351 }
1352
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001353 return isInterestingAndDrawn;
1354 }
1355
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001356 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001357 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001358 // For legacy reasons we process the TaskStack.mExitingAppTokens first in DisplayContent
1359 // before the non-exiting app tokens. So, we skip the exiting app tokens here.
1360 // TODO: Investigate if we need to continue to do this or if we can just process them
1361 // in-order.
1362 if (mIsExiting && !waitingForReplacement()) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001363 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001364 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001365 return forAllWindowsUnchecked(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001366 }
1367
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001368 boolean forAllWindowsUnchecked(ToBooleanFunction<WindowState> callback,
1369 boolean traverseTopToBottom) {
1370 return super.forAllWindows(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001371 }
1372
1373 @Override
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001374 AppWindowToken asAppWindowToken() {
1375 // I am an app window token!
1376 return this;
1377 }
1378
1379 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001380 boolean fillsParent() {
1381 return mFillsParent;
1382 }
1383
1384 void setFillsParent(boolean fillsParent) {
1385 mFillsParent = fillsParent;
1386 }
1387
Jorim Jaggife762342016-10-13 14:33:27 +02001388 boolean containsDismissKeyguardWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001389 // Window state is transient during relaunch. We are not guaranteed to be frozen during the
1390 // entirety of the relaunch.
1391 if (isRelaunching()) {
1392 return mLastContainsDismissKeyguardWindow;
1393 }
1394
Jorim Jaggife762342016-10-13 14:33:27 +02001395 for (int i = mChildren.size() - 1; i >= 0; i--) {
1396 if ((mChildren.get(i).mAttrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
1397 return true;
1398 }
1399 }
1400 return false;
1401 }
1402
1403 boolean containsShowWhenLockedWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001404 // When we are relaunching, it is possible for us to be unfrozen before our previous
1405 // windows have been added back. Using the cached value ensures that our previous
1406 // showWhenLocked preference is honored until relaunching is complete.
1407 if (isRelaunching()) {
1408 return mLastContainsShowWhenLockedWindow;
1409 }
1410
Jorim Jaggife762342016-10-13 14:33:27 +02001411 for (int i = mChildren.size() - 1; i >= 0; i--) {
1412 if ((mChildren.get(i).mAttrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
1413 return true;
1414 }
1415 }
Bryce Lee081554b2017-05-25 07:52:12 -07001416
Jorim Jaggife762342016-10-13 14:33:27 +02001417 return false;
1418 }
1419
1420 void checkKeyguardFlagsChanged() {
1421 final boolean containsDismissKeyguard = containsDismissKeyguardWindow();
1422 final boolean containsShowWhenLocked = containsShowWhenLockedWindow();
1423 if (containsDismissKeyguard != mLastContainsDismissKeyguardWindow
1424 || containsShowWhenLocked != mLastContainsShowWhenLockedWindow) {
1425 mService.notifyKeyguardFlagsChanged(null /* callback */);
1426 }
1427 mLastContainsDismissKeyguardWindow = containsDismissKeyguard;
1428 mLastContainsShowWhenLockedWindow = containsShowWhenLocked;
1429 }
1430
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001431 WindowState getImeTargetBelowWindow(WindowState w) {
1432 final int index = mChildren.indexOf(w);
1433 if (index > 0) {
1434 final WindowState target = mChildren.get(index - 1);
1435 if (target.canBeImeTarget()) {
1436 return target;
1437 }
1438 }
1439 return null;
1440 }
1441
Winson Chungbe9be7f2017-06-28 10:55:22 -07001442 int getLowestAnimLayer() {
1443 for (int i = 0; i < mChildren.size(); i++) {
1444 final WindowState w = mChildren.get(i);
1445 if (w.mRemoved) {
1446 continue;
1447 }
1448 return w.mWinAnimator.mAnimLayer;
1449 }
1450 return Integer.MAX_VALUE;
1451 }
1452
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001453 WindowState getHighestAnimLayerWindow(WindowState currentTarget) {
1454 WindowState candidate = null;
1455 for (int i = mChildren.indexOf(currentTarget); i >= 0; i--) {
1456 final WindowState w = mChildren.get(i);
1457 if (w.mRemoved) {
1458 continue;
1459 }
1460 if (candidate == null || w.mWinAnimator.mAnimLayer >
1461 candidate.mWinAnimator.mAnimLayer) {
1462 candidate = w;
1463 }
1464 }
1465 return candidate;
1466 }
1467
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001468 /**
1469 * See {@link Activity#setDisablePreviewScreenshots}.
1470 */
1471 void setDisablePreviewScreenshots(boolean disable) {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001472 mDisablePreviewScreenshots = disable;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001473 }
1474
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001475 /**
chaviwd3bf08d2017-08-01 17:24:59 -07001476 * Sets whether the current launch can turn the screen on. See {@link #canTurnScreenOn()}
1477 */
1478 void setCanTurnScreenOn(boolean canTurnScreenOn) {
1479 mCanTurnScreenOn = canTurnScreenOn;
1480 }
1481
1482 /**
1483 * Indicates whether the current launch can turn the screen on. This is to prevent multiple
1484 * relayouts from turning the screen back on. The screen should only turn on at most
1485 * once per activity resume.
1486 *
1487 * @return true if the screen can be turned on.
1488 */
1489 boolean canTurnScreenOn() {
1490 return mCanTurnScreenOn;
1491 }
1492
1493 /**
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001494 * Retrieves whether we'd like to generate a snapshot that's based solely on the theme. This is
1495 * the case when preview screenshots are disabled {@link #setDisablePreviewScreenshots} or when
1496 * we can't take a snapshot for other reasons, for example, if we have a secure window.
1497 *
1498 * @return True if we need to generate an app theme snapshot, false if we'd like to take a real
1499 * screenshot.
1500 */
1501 boolean shouldUseAppThemeSnapshot() {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001502 return mDisablePreviewScreenshots || forAllWindows(w -> (w.mAttrs.flags & FLAG_SECURE) != 0,
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001503 true /* topToBottom */);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001504 }
1505
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001506 boolean applyAnimationLocked(WindowManager.LayoutParams lp, int transit, boolean enter,
1507 boolean isVoiceInteraction) {
1508
1509 if (mService.mDisableTransitionAnimation) {
1510 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) {
1511 Slog.v(TAG_WM, "applyAnimation: transition animation is disabled. atoken=" + this);
1512 }
1513 cancelAnimation();
1514 return false;
1515 }
1516
1517 // Only apply an animation if the display isn't frozen. If it is frozen, there is no reason
1518 // to animate and it can cause strange artifacts when we unfreeze the display if some
1519 // different animation is running.
1520 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "AWT#applyAnimationLocked");
1521 if (okToAnimate()) {
1522 final Animation a = loadAnimation(lp, transit, enter, isVoiceInteraction);
1523 if (a != null) {
1524 final AnimationAdapter adapter = new LocalAnimationAdapter(
Jorim Jaggi2e3c31d2017-11-20 19:49:00 +01001525 new WindowAnimationSpec(a, new Point(),
1526 mService.mAppTransition.canSkipFirstFrame()),
1527 mService.mSurfaceAnimationRunner);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001528 startAnimation(getPendingTransaction(), adapter, !isVisible());
1529 if (a.getZAdjustment() == Animation.ZORDER_TOP) {
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001530 mNeedsZBoost = true;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001531 getDisplayContent().assignWindowLayers(false /* setLayoutNeeded */);
1532 }
1533 mTransit = transit;
1534 mTransitFlags = mService.mAppTransition.getTransitFlags();
1535 // TODO: Skip first frame and app stack clip mode.
1536 }
1537 } else {
1538 cancelAnimation();
1539 }
1540 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
1541
1542 return isReallyAnimating();
1543 }
1544
1545 private Animation loadAnimation(WindowManager.LayoutParams lp, int transit, boolean enter,
1546 boolean isVoiceInteraction) {
1547 final DisplayContent displayContent = getTask().getDisplayContent();
1548 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
1549 final int width = displayInfo.appWidth;
1550 final int height = displayInfo.appHeight;
1551 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.v(TAG_WM,
1552 "applyAnimation: atoken=" + this);
1553
1554 // Determine the visible rect to calculate the thumbnail clip
1555 final WindowState win = findMainWindow();
1556 final Rect frame = new Rect(0, 0, width, height);
1557 final Rect displayFrame = new Rect(0, 0,
1558 displayInfo.logicalWidth, displayInfo.logicalHeight);
1559 final Rect insets = new Rect();
1560 final Rect stableInsets = new Rect();
1561 Rect surfaceInsets = null;
1562 final boolean freeform = win != null && win.inFreeformWindowingMode();
1563 if (win != null) {
1564 // Containing frame will usually cover the whole screen, including dialog windows.
1565 // For freeform workspace windows it will not cover the whole screen and it also
1566 // won't exactly match the final freeform window frame (e.g. when overlapping with
1567 // the status bar). In that case we need to use the final frame.
1568 if (freeform) {
1569 frame.set(win.mFrame);
1570 } else {
1571 frame.set(win.mContainingFrame);
1572 }
1573 surfaceInsets = win.getAttrs().surfaceInsets;
1574 insets.set(win.mContentInsets);
1575 stableInsets.set(win.mStableInsets);
1576 }
1577
1578 if (mLaunchTaskBehind) {
1579 // Differentiate the two animations. This one which is briefly on the screen
1580 // gets the !enter animation, and the other activity which remains on the
1581 // screen gets the enter animation. Both appear in the mOpeningApps set.
1582 enter = false;
1583 }
1584 if (DEBUG_APP_TRANSITIONS) Slog.d(TAG_WM, "Loading animation for app transition."
1585 + " transit=" + AppTransition.appTransitionToString(transit) + " enter=" + enter
1586 + " frame=" + frame + " insets=" + insets + " surfaceInsets=" + surfaceInsets);
1587 final Configuration displayConfig = displayContent.getConfiguration();
1588 final Animation a = mService.mAppTransition.loadAnimation(lp, transit, enter,
1589 displayConfig.uiMode, displayConfig.orientation, frame, displayFrame, insets,
1590 surfaceInsets, stableInsets, isVoiceInteraction, freeform, getTask().mTaskId);
1591 if (a != null) {
1592 if (DEBUG_ANIM) logWithStack(TAG, "Loaded animation " + a + " for " + this);
1593 final int containingWidth = frame.width();
1594 final int containingHeight = frame.height();
1595 a.initialize(containingWidth, containingHeight, width, height);
1596 a.scaleCurrentDuration(mService.getTransitionAnimationScaleLocked());
1597 }
1598 return a;
1599 }
1600
1601 /**
1602 * This must be called while inside a transaction.
1603 */
1604 void showAllWindowsLocked() {
1605 forAllWindows(windowState -> {
1606 if (DEBUG_VISIBILITY) Slog.v(TAG, "performing show on: " + windowState);
1607 windowState.performShowLocked();
1608 }, false /* traverseTopToBottom */);
Jorim Jaggia5e10572017-11-15 14:36:26 +01001609 }
1610
1611 @Override
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001612 protected void onAnimationFinished() {
1613 super.onAnimationFinished();
1614
1615 mTransit = TRANSIT_UNSET;
1616 mTransitFlags = 0;
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001617 mNeedsZBoost = false;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001618
1619 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM | FINISH_LAYOUT_REDO_WALLPAPER,
1620 "AppWindowToken");
1621
Jorim Jaggi988f6682017-11-17 17:46:43 +01001622 clearThumbnail();
1623
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001624 if (mService.mInputMethodTarget != null && mService.mInputMethodTarget.mAppToken == this) {
1625 getDisplayContent().computeImeTarget(true /* updateImeTarget */);
1626 }
1627
1628 if (DEBUG_ANIM) Slog.v(TAG, "Animation done in " + this
1629 + ": reportedVisible=" + reportedVisible
1630 + " okToDisplay=" + okToDisplay()
1631 + " okToAnimate=" + okToAnimate()
1632 + " startingDisplayed=" + startingDisplayed);
1633
1634 // WindowState.onExitAnimationDone might modify the children list, so make a copy and then
1635 // traverse the copy.
1636 final ArrayList<WindowState> children = new ArrayList<>(mChildren);
1637 children.forEach(WindowState::onExitAnimationDone);
1638
1639 mService.mAppTransition.notifyAppTransitionFinishedLocked(token);
1640 scheduleAnimation();
1641 }
1642
1643 @Override
1644 boolean isAppAnimating() {
1645 return isSelfAnimating();
1646 }
1647
1648 @Override
1649 boolean isSelfAnimating() {
1650 // If we are about to start a transition, we also need to be considered animating.
1651 return isWaitingForTransitionStart() || isReallyAnimating();
1652 }
1653
1654 /**
1655 * @return True if and only if we are actually running an animation. Note that
1656 * {@link #isSelfAnimating} also returns true if we are waiting for an animation to
1657 * start.
1658 */
1659 private boolean isReallyAnimating() {
1660 return super.isSelfAnimating();
1661 }
1662
Jorim Jaggi988f6682017-11-17 17:46:43 +01001663 @Override
1664 void cancelAnimation() {
1665 super.cancelAnimation();
1666 clearThumbnail();
1667 }
1668
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001669 boolean isWaitingForTransitionStart() {
1670 return mService.mAppTransition.isTransitionSet()
1671 && (mService.mOpeningApps.contains(this) || mService.mClosingApps.contains(this));
1672 }
1673
1674 public int getTransit() {
1675 return mTransit;
1676 }
1677
1678 int getTransitFlags() {
1679 return mTransitFlags;
1680 }
1681
Jorim Jaggi988f6682017-11-17 17:46:43 +01001682 void attachThumbnailAnimation() {
1683 if (!isReallyAnimating()) {
1684 return;
1685 }
1686 final int taskId = getTask().mTaskId;
1687 final GraphicBuffer thumbnailHeader =
1688 mService.mAppTransition.getAppTransitionThumbnailHeader(taskId);
1689 if (thumbnailHeader == null) {
1690 if (DEBUG_APP_TRANSITIONS) Slog.d(TAG, "No thumbnail header bitmap for: " + taskId);
1691 return;
1692 }
1693 clearThumbnail();
1694 mThumbnail = new AppWindowThumbnail(getPendingTransaction(), this, thumbnailHeader);
1695 mThumbnail.startAnimation(getPendingTransaction(), loadThumbnailAnimation(thumbnailHeader));
1696 }
1697
1698 private Animation loadThumbnailAnimation(GraphicBuffer thumbnailHeader) {
1699 final DisplayInfo displayInfo = mDisplayContent.getDisplayInfo();
1700
1701 // If this is a multi-window scenario, we use the windows frame as
1702 // destination of the thumbnail header animation. If this is a full screen
1703 // window scenario, we use the whole display as the target.
1704 WindowState win = findMainWindow();
1705 Rect appRect = win != null ? win.getContentFrameLw() :
1706 new Rect(0, 0, displayInfo.appWidth, displayInfo.appHeight);
1707 Rect insets = win != null ? win.mContentInsets : null;
1708 final Configuration displayConfig = mDisplayContent.getConfiguration();
1709 return mService.mAppTransition.createThumbnailAspectScaleAnimationLocked(
1710 appRect, insets, thumbnailHeader, getTask().mTaskId, displayConfig.uiMode,
1711 displayConfig.orientation);
1712 }
1713
1714 private void clearThumbnail() {
1715 if (mThumbnail == null) {
1716 return;
1717 }
1718 mThumbnail.destroy();
1719 mThumbnail = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001720 }
1721
1722 @Override
1723 void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1724 super.dump(pw, prefix, dumpAll);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001725 if (appToken != null) {
Wale Ogunwale72919d22016-12-08 18:58:50 -08001726 pw.println(prefix + "app=true mVoiceInteraction=" + mVoiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001727 }
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001728 pw.print(prefix); pw.print("task="); pw.println(getTask());
Wale Ogunwale51362492016-09-08 17:49:17 -07001729 pw.print(prefix); pw.print(" mFillsParent="); pw.print(mFillsParent);
1730 pw.print(" mOrientation="); pw.println(mOrientation);
Wale Ogunwale89973222017-04-23 18:39:45 -07001731 pw.println(prefix + "hiddenRequested=" + hiddenRequested + " mClientHidden=" + mClientHidden
1732 + ((mDeferHidingClient) ? " mDeferHidingClient=" + mDeferHidingClient : "")
1733 + " reportedDrawn=" + reportedDrawn + " reportedVisible=" + reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -07001734 if (paused) {
1735 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001736 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -08001737 if (mAppStopped) {
1738 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
1739 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001740 if (mNumInterestingWindows != 0 || mNumDrawnWindows != 0
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001741 || allDrawn || mLastAllDrawn) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001742 pw.print(prefix); pw.print("mNumInterestingWindows=");
1743 pw.print(mNumInterestingWindows);
1744 pw.print(" mNumDrawnWindows="); pw.print(mNumDrawnWindows);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001745 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -07001746 pw.print(" allDrawn="); pw.print(allDrawn);
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001747 pw.print(" lastAllDrawn="); pw.print(mLastAllDrawn);
Craig Mautner6fbda632012-07-03 09:26:39 -07001748 pw.println(")");
1749 }
1750 if (inPendingTransaction) {
1751 pw.print(prefix); pw.print("inPendingTransaction=");
1752 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001753 }
Craig Mautner799bc1d2015-01-14 10:33:48 -08001754 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001755 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
1756 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -08001757 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -08001758 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001759 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001760 if (startingWindow != null || startingSurface != null
Wale Ogunwale6c459212017-05-17 08:56:03 -07001761 || startingDisplayed || startingMoved || mHiddenSetFromTransferredStartingWindow) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001762 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001763 pw.print(" startingSurface="); pw.print(startingSurface);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001764 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale6c459212017-05-17 08:56:03 -07001765 pw.print(" startingMoved="); pw.print(startingMoved);
1766 pw.println(" mHiddenSetFromTransferredStartingWindow="
1767 + mHiddenSetFromTransferredStartingWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001768 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001769 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -08001770 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001771 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -08001772 }
1773 if (mPendingRelaunchCount != 0) {
1774 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +01001775 }
Wale Ogunwale9c64cb62017-04-12 13:39:59 -07001776 if (getController() != null) {
1777 pw.print(prefix); pw.print("controller="); pw.println(getController());
1778 }
Wale Ogunwalee287e192017-04-21 09:30:12 -07001779 if (mRemovingFromDisplay) {
1780 pw.println(prefix + "mRemovingFromDisplay=" + mRemovingFromDisplay);
1781 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001782 }
1783
1784 @Override
1785 void setHidden(boolean hidden) {
1786 super.setHidden(hidden);
1787 scheduleAnimation();
1788 }
1789
1790 @Override
1791 void prepareSurfaces() {
1792 // isSelfAnimating also returns true when we are about to start a transition, so we need
1793 // to check super here.
1794 final boolean reallyAnimating = super.isSelfAnimating();
1795 final boolean show = !isHidden() || reallyAnimating;
1796 if (show && !mLastSurfaceShowing) {
1797 mPendingTransaction.show(mSurfaceControl);
1798 } else if (!show && mLastSurfaceShowing) {
1799 mPendingTransaction.hide(mSurfaceControl);
Jorim Jaggi4d1835d2017-08-31 17:28:27 +02001800 }
Jorim Jaggi988f6682017-11-17 17:46:43 +01001801 if (mThumbnail != null) {
1802 mThumbnail.setShowing(mPendingTransaction, show);
1803 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001804 mLastSurfaceShowing = show;
1805 super.prepareSurfaces();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001806 }
1807
Jorim Jaggib0fc8172017-11-23 17:04:08 +00001808 boolean isFreezingScreen() {
1809 return mFreezingScreen;
1810 }
1811
1812 @Override
1813 boolean needsZBoost() {
1814 return mNeedsZBoost || super.needsZBoost();
1815 }
1816
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001817 @CallSuper
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001818 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02001819 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07001820 final long token = proto.start(fieldId);
1821 writeNameToProto(proto, NAME);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001822 super.writeToProto(proto, WINDOW_TOKEN, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001823 proto.end(token);
1824 }
1825
1826 void writeNameToProto(ProtoOutputStream proto, long fieldId) {
1827 if (appToken == null) {
1828 return;
1829 }
1830 try {
1831 proto.write(fieldId, appToken.getName());
1832 } catch (RemoteException e) {
1833 // This shouldn't happen, but in this case fall back to outputting nothing
1834 Slog.e(TAG, e.toString());
1835 }
1836 }
1837
1838 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001839 public String toString() {
1840 if (stringName == null) {
1841 StringBuilder sb = new StringBuilder();
1842 sb.append("AppWindowToken{");
1843 sb.append(Integer.toHexString(System.identityHashCode(this)));
1844 sb.append(" token="); sb.append(token); sb.append('}');
1845 stringName = sb.toString();
1846 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001847 return stringName + ((mIsExiting) ? " mIsExiting=" : "");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001848 }
Jeff Browne9bdb312012-04-05 15:30:10 -07001849}