blob: 7545a10602a64d3c08cc819630a7c1bf4d3c2bd9 [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 Ogunwale6cae7652015-12-26 07:36:26 -080019import static android.app.ActivityManager.StackId;
Winson Chunge7ba6862017-05-24 12:13:33 -070020import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale72919d22016-12-08 18:58:50 -080021import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
22import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070023import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070024import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
Bryce Lee0e7b7c92017-08-10 14:59:00 -070025import static android.os.Build.VERSION_CODES.O_MR1;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070026import static android.view.Display.DEFAULT_DISPLAY;
Jorim Jaggife762342016-10-13 14:33:27 +020027import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020028import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Jorim Jaggife762342016-10-13 14:33:27 +020029import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Wale Ogunwaled1c37912016-08-16 03:19:39 -070030import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080031import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070032import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -070033import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
34import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070035import static com.android.server.wm.AppTransition.TRANSIT_UNSET;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080036import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080037import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
38import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070039import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070040import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
41import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwale9017ec02016-02-25 08:55:25 -080042import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
Jorim Jaggie4b0f282017-05-17 15:10:29 +020043import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW_VERBOSE;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070044import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080045import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
46import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
47import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
48import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Chong Zhang92147042016-05-09 12:47:11 -070049import static com.android.server.wm.WindowManagerService.H.NOTIFY_ACTIVITY_DRAWN;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070050import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_NORMAL;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070051import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070052import static com.android.server.wm.WindowManagerService.logWithStack;
Steven Timotiusaf03df62017-07-18 16:56:43 -070053import static com.android.server.wm.proto.AppWindowTokenProto.NAME;
54import static com.android.server.wm.proto.AppWindowTokenProto.WINDOW_TOKEN;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080055
Filip Gruszczynskia590c992015-11-25 16:45:26 -080056import android.annotation.NonNull;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020057import android.app.Activity;
Jorim Jaggi26c8c422016-05-09 19:57:25 -070058import android.content.res.Configuration;
Jorim Jaggi0429f352015-12-22 16:29:16 +010059import android.graphics.Rect;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070060import android.os.Binder;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080061import android.os.Debug;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070062import android.os.IBinder;
Steven Timotiusaf03df62017-07-18 16:56:43 -070063import android.os.RemoteException;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070064import android.os.SystemClock;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080065import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -070066import android.util.proto.ProtoOutputStream;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080067import android.view.IApplicationToken;
Robert Carr6914f082017-03-20 19:04:30 -070068import android.view.SurfaceControl;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080069import android.view.WindowManager;
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +020070import android.view.WindowManager.LayoutParams;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080071import android.view.WindowManagerPolicy.StartingSurface;
72
73import com.android.internal.util.ToBooleanFunction;
74import com.android.server.input.InputApplicationHandle;
75import com.android.server.wm.WindowManagerService.H;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080076
77import java.io.PrintWriter;
Jorim Jaggi0429f352015-12-22 16:29:16 +010078import java.util.ArrayDeque;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080079import java.util.ArrayList;
80
Bryce Lee39791592017-04-26 09:29:12 -070081import static android.os.Build.VERSION_CODES.O;
82
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080083class AppTokenList extends ArrayList<AppWindowToken> {
84}
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080085
86/**
87 * Version of WindowToken that is specifically for a particular application (or
88 * really activity) that is displaying windows.
89 */
Wale Ogunwale3f4433d2016-08-18 20:42:42 -070090class AppWindowToken extends WindowToken implements WindowManagerService.AppFreezeListener {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080091 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowToken" : TAG_WM;
92
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080093 // Non-null only for application tokens.
94 final IApplicationToken appToken;
95
Filip Gruszczynskia590c992015-11-25 16:45:26 -080096 @NonNull final AppWindowAnimator mAppAnimator;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070097
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;
Craig Mautner7636dfb2012-11-16 15:24:11 -0800125 // Set to true when this app creates a surface while in the middle of an animation. In that
126 // case do not clear allDrawn until the animation completes.
127 boolean deferClearAllDrawn;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800128
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700129 /**
130 * These are to track the app's real drawing status if there were no saved surfaces.
131 * @see #updateDrawnWindowStates
132 */
Chong Zhang8e4bda92016-05-04 15:08:18 -0700133 boolean allDrawnExcludingSaved;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700134 private int mNumInterestingWindowsExcludingSaved;
135 private int mNumDrawnWindowsExcludingSaved;
Chong Zhang8e4bda92016-05-04 15:08:18 -0700136
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800137 // Is this window's surface needed? This is almost like hidden, except
138 // it will sometimes be true a little earlier: when the token has
139 // been shown, but is still waiting for its app transition to execute
140 // before making its windows shown.
141 boolean hiddenRequested;
142
143 // Have we told the window clients to hide themselves?
Wale Ogunwale89973222017-04-23 18:39:45 -0700144 private boolean mClientHidden;
145
146 // If true we will defer setting mClientHidden to true and reporting to the client that it is
147 // hidden.
148 boolean mDeferHidingClient;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800149
150 // Last visibility state we reported to the app token.
151 boolean reportedVisible;
152
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700153 // Last drawn state we reported to the app token.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700154 private boolean reportedDrawn;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700155
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800156 // Set to true when the token has been removed from the window mgr.
157 boolean removed;
158
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800159 // Information about an application starting window if displayed.
160 StartingData startingData;
161 WindowState startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800162 StartingSurface startingSurface;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800163 boolean startingDisplayed;
164 boolean startingMoved;
Wale Ogunwale6c459212017-05-17 08:56:03 -0700165 // True if the hidden state of this token was forced to false due to a transferred starting
166 // window.
167 private boolean mHiddenSetFromTransferredStartingWindow;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800168 boolean firstWindowDrawn;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700169 private final WindowState.UpdateReportedVisibilityResults mReportedVisibilityResults =
170 new WindowState.UpdateReportedVisibilityResults();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800171
172 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700173 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800174
Wale Ogunwale571771c2016-08-26 13:18:50 -0700175 // TODO: Have a WindowContainer state for tracking exiting/deferred removal.
Craig Mautner799bc1d2015-01-14 10:33:48 -0800176 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800177
Craig Mautnerbb742462014-07-07 15:28:55 -0700178 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700179 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700180
Wale Ogunwale72919d22016-12-08 18:58:50 -0800181 private boolean mAlwaysFocusable;
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800182
Robert Carre12aece2016-02-02 22:43:27 -0800183 boolean mAppStopped;
Robert Carrfd10cd12016-06-29 16:41:50 -0700184 int mRotationAnimationHint;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700185 private int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800186
Jorim Jaggife762342016-10-13 14:33:27 +0200187 private boolean mLastContainsShowWhenLockedWindow;
188 private boolean mLastContainsDismissKeyguardWindow;
189
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700190 // The bounds of this activity. Mainly used for aspect-ratio compatibility.
191 // TODO(b/36505427): Every level on WindowContainer now has bounds information, which directly
192 // affects the configuration. We should probably move this into that class.
193 private final Rect mBounds = new Rect();
194
Jorim Jaggi0429f352015-12-22 16:29:16 +0100195 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700196 ArrayDeque<Configuration> mFrozenMergedConfig = new ArrayDeque<>();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100197
Wale Ogunwale6c459212017-05-17 08:56:03 -0700198 private boolean mDisablePreviewScreenshots;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100199
Robert Carred3e83b2017-04-21 13:26:55 -0700200 Task mLastParent;
201
chaviwd3bf08d2017-08-01 17:24:59 -0700202 /**
203 * See {@link #canTurnScreenOn()}
204 */
205 private boolean mCanTurnScreenOn = true;
206
Wale Ogunwale72919d22016-12-08 18:58:50 -0800207 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
208 DisplayContent dc, long inputDispatchingTimeoutNanos, boolean fullscreen,
209 boolean showForAllUsers, int targetSdk, int orientation, int rotationAnimationHint,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800210 int configChanges, boolean launchTaskBehind, boolean alwaysFocusable,
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700211 AppWindowContainerController controller, Configuration overrideConfig, Rect bounds) {
212 this(service, token, voiceInteraction, dc, fullscreen, overrideConfig, bounds);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800213 setController(controller);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800214 mInputDispatchingTimeoutNanos = inputDispatchingTimeoutNanos;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800215 mShowForAllUsers = showForAllUsers;
216 mTargetSdk = targetSdk;
217 mOrientation = orientation;
218 layoutConfigChanges = (configChanges & (CONFIG_SCREEN_SIZE | CONFIG_ORIENTATION)) != 0;
219 mLaunchTaskBehind = launchTaskBehind;
220 mAlwaysFocusable = alwaysFocusable;
221 mRotationAnimationHint = rotationAnimationHint;
222
223 // Application tokens start out hidden.
224 hidden = true;
225 hiddenRequested = true;
226 }
227
228 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700229 DisplayContent dc, boolean fillsParent, Configuration overrideConfig, Rect bounds) {
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800230 super(service, token != null ? token.asBinder() : null, TYPE_APPLICATION, true, dc,
231 false /* ownerCanManageAppTokens */);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700232 appToken = token;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800233 mVoiceInteraction = voiceInteraction;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800234 mFillsParent = fillsParent;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800235 mInputApplicationHandle = new InputApplicationHandle(this);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700236 mAppAnimator = new AppWindowAnimator(this, service);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700237 if (overrideConfig != null) {
238 onOverrideConfigurationChanged(overrideConfig);
239 }
240 if (bounds != null) {
241 mBounds.set(bounds);
242 }
243 }
244
245 void onOverrideConfigurationChanged(Configuration overrideConfiguration, Rect bounds) {
246 onOverrideConfigurationChanged(overrideConfiguration);
247 if (mBounds.equals(bounds)) {
248 return;
249 }
250 // TODO(b/36505427): If bounds is in WC, then we can automatically call onResize() when set.
251 mBounds.set(bounds);
252 onResize();
253 }
254
255 void getBounds(Rect outBounds) {
256 outBounds.set(mBounds);
257 }
258
259 boolean hasBounds() {
260 return !mBounds.isEmpty();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800261 }
262
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800263 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
264 firstWindowDrawn = true;
265
266 // We now have a good window to show, remove dead placeholders
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700267 removeDeadWindows();
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800268
Jorim Jaggi02886a82016-12-06 09:10:06 -0800269 if (startingWindow != null) {
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800270 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
271 + win.mToken + ": first real window is shown, no animation");
272 // If this initial window is animating, stop it -- we will do an animation to reveal
273 // it from behind the starting window, so there is no need for it to also be doing its
274 // own stuff.
275 winAnimator.clearAnimation();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800276 if (getController() != null) {
277 getController().removeStartingWindow();
278 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800279 }
280 updateReportedVisibilityLocked();
281 }
282
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800283 void updateReportedVisibilityLocked() {
284 if (appToken == null) {
285 return;
286 }
287
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700288 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700289 final int count = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800290
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700291 mReportedVisibilityResults.reset();
292
293 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700294 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700295 win.updateReportedVisibility(mReportedVisibilityResults);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800296 }
297
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700298 int numInteresting = mReportedVisibilityResults.numInteresting;
299 int numVisible = mReportedVisibilityResults.numVisible;
300 int numDrawn = mReportedVisibilityResults.numDrawn;
301 boolean nowGone = mReportedVisibilityResults.nowGone;
302
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700303 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Jorim Jaggi6dfd9ba2017-08-03 15:00:01 +0200304 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting && !hidden;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700305 if (!nowGone) {
306 // If the app is not yet gone, then it can only become visible/drawn.
307 if (!nowDrawn) {
308 nowDrawn = reportedDrawn;
309 }
310 if (!nowVisible) {
311 nowVisible = reportedVisible;
312 }
313 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800314 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800315 + numInteresting + " visible=" + numVisible);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800316 final AppWindowContainerController controller = getController();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700317 if (nowDrawn != reportedDrawn) {
318 if (nowDrawn) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800319 if (controller != null) {
320 controller.reportWindowsDrawn();
321 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700322 }
323 reportedDrawn = nowDrawn;
324 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800325 if (nowVisible != reportedVisible) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700326 if (DEBUG_VISIBILITY) Slog.v(TAG,
327 "Visibility changed in " + this + ": vis=" + nowVisible);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800328 reportedVisible = nowVisible;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800329 if (controller != null) {
330 if (nowVisible) {
331 controller.reportWindowsVisible();
332 } else {
333 controller.reportWindowsGone();
334 }
335 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800336 }
337 }
338
Wale Ogunwale89973222017-04-23 18:39:45 -0700339 boolean isClientHidden() {
340 return mClientHidden;
341 }
342
343 void setClientHidden(boolean hideClient) {
344 if (mClientHidden == hideClient || (hideClient && mDeferHidingClient)) {
345 return;
346 }
347 mClientHidden = hideClient;
348 sendAppVisibilityToClients();
349 }
350
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700351 boolean setVisibility(WindowManager.LayoutParams lp,
352 boolean visible, int transit, boolean performLayout, boolean isVoiceInteraction) {
353
354 boolean delayed = false;
355 inPendingTransaction = false;
Wale Ogunwale9e4721f2017-05-23 19:37:30 -0700356 // Reset the state of mHiddenSetFromTransferredStartingWindow since visibility is actually
357 // been set by the app now.
358 mHiddenSetFromTransferredStartingWindow = false;
Wale Ogunwale89973222017-04-23 18:39:45 -0700359 setClientHidden(!visible);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700360
361 // Allow for state changes and animation to be applied if:
362 // * token is transitioning visibility state
363 // * or the token was marked as hidden and is exiting before we had a chance to play the
364 // transition animation
365 // * or this is an opening app and windows are being replaced.
366 boolean visibilityChanged = false;
367 if (hidden == visible || (hidden && mIsExiting) || (visible && waitingForReplacement())) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700368 final AccessibilityController accessibilityController = mService.mAccessibilityController;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700369 boolean changed = false;
370 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
371 "Changing app " + this + " hidden=" + hidden + " performLayout=" + performLayout);
372
373 boolean runningAppAnimation = false;
374
375 if (transit != AppTransition.TRANSIT_UNSET) {
376 if (mAppAnimator.animation == AppWindowAnimator.sDummyAnimation) {
377 mAppAnimator.setNullAnimation();
378 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700379 if (mService.applyAnimationLocked(this, lp, transit, visible, isVoiceInteraction)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700380 delayed = runningAppAnimation = true;
381 }
382 final WindowState window = findMainWindow();
383 //TODO (multidisplay): Magnification is supported only for the default display.
384 if (window != null && accessibilityController != null
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700385 && getDisplayContent().getDisplayId() == DEFAULT_DISPLAY) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700386 accessibilityController.onAppWindowTransitionLocked(window, transit);
387 }
388 changed = true;
389 }
390
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700391 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700392 for (int i = 0; i < windowsCount; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700393 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700394 changed |= win.onAppVisibilityChanged(visible, runningAppAnimation);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700395 }
396
397 hidden = hiddenRequested = !visible;
398 visibilityChanged = true;
399 if (!visible) {
400 stopFreezingScreen(true, true);
401 } else {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700402 // If we are being set visible, and the starting window is not yet displayed,
403 // then make sure it doesn't get displayed.
404 if (startingWindow != null && !startingWindow.isDrawnLw()) {
405 startingWindow.mPolicyVisibility = false;
406 startingWindow.mPolicyVisibilityAfterAnim = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700407 }
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700408
409 // We are becoming visible, so better freeze the screen with the windows that are
410 // getting visible so we also wait for them.
411 forAllWindows(mService::makeWindowFreezingScreenIfNeededLocked, true);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700412 }
413
414 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "setVisibility: " + this
415 + ": hidden=" + hidden + " hiddenRequested=" + hiddenRequested);
416
417 if (changed) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700418 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700419 if (performLayout) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700420 mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700421 false /*updateInputWindows*/);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700422 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700423 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700424 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700425 }
426 }
427
428 if (mAppAnimator.animation != null) {
429 delayed = true;
430 }
431
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700432 for (int i = mChildren.size() - 1; i >= 0 && !delayed; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700433 if ((mChildren.get(i)).isWindowAnimationSet()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700434 delayed = true;
435 }
436 }
437
438 if (visibilityChanged) {
439 if (visible && !delayed) {
440 // The token was made immediately visible, there will be no entrance animation.
441 // We need to inform the client the enter animation was finished.
442 mEnteringAnimation = true;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700443 mService.mActivityManagerAppTransitionNotifier.onAppTransitionFinishedLocked(token);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700444 }
Robert Carr61b81112017-07-17 18:08:15 -0700445
Robert Carre7cc44d2017-03-20 19:04:30 -0700446 // If we are hidden but there is no delay needed we immediately
447 // apply the Surface transaction so that the ActivityManager
Robert Carr61b81112017-07-17 18:08:15 -0700448 // can have some guarantee on the Surface state following
449 // setting the visibility. This captures cases like dismissing
450 // the docked or pinned stack where there is no app transition.
451 //
452 // In the case of a "Null" animation, there will be
453 // no animation but there will still be a transition set.
454 // We still need to delay hiding the surface such that it
455 // can be synchronized with showing the next surface in the transition.
456 if (hidden && !delayed && !mService.mAppTransition.isTransitionSet()) {
Robert Carr6914f082017-03-20 19:04:30 -0700457 SurfaceControl.openTransaction();
458 for (int i = mChildren.size() - 1; i >= 0; i--) {
459 mChildren.get(i).mWinAnimator.hide("immediately hidden");
460 }
461 SurfaceControl.closeTransaction();
462 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700463
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700464 if (!mService.mClosingApps.contains(this) && !mService.mOpeningApps.contains(this)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700465 // The token is not closing nor opening, so even if there is an animation set, that
466 // doesn't mean that it goes through the normal app transition cycle so we have
467 // to inform the docked controller about visibility change.
Andrii Kulian1e32e022016-09-16 15:29:34 -0700468 // TODO(multi-display): notify docked divider on all displays where visibility was
469 // affected.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700470 mService.getDefaultDisplayContentLocked().getDockedDividerController()
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700471 .notifyAppVisibilityChanged();
Jorim Jaggi8b702ed2017-01-20 16:59:03 +0100472 mService.mTaskSnapshotController.notifyAppVisibilityChanged(this, visible);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700473 }
474 }
475
476 return delayed;
477 }
478
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +0200479 /**
480 * @return The to top most child window for which {@link LayoutParams#isFullscreen()} returns
481 * true.
482 */
483 WindowState getTopFullscreenWindow() {
484 for (int i = mChildren.size() - 1; i >= 0; i--) {
485 final WindowState win = mChildren.get(i);
486 if (win != null && win.mAttrs.isFullscreen()) {
487 return win;
488 }
489 }
490 return null;
491 }
492
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800493 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700494 WindowState candidate = null;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700495 int j = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800496 while (j > 0) {
497 j--;
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700498 final WindowState win = mChildren.get(j);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700499 final int type = win.mAttrs.type;
500 // No need to loop through child window as base application and starting types can't be
501 // child windows.
502 if (type == TYPE_BASE_APPLICATION || type == TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700503 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900504 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700505 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800506 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700507 candidate = win;
508 } else {
509 return win;
510 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800511 }
512 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700513 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800514 }
515
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800516 boolean windowsAreFocusable() {
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700517 return getWindowConfiguration().canReceiveKeys() || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800518 }
519
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800520 AppWindowContainerController getController() {
521 final WindowContainerController controller = super.getController();
522 return controller != null ? (AppWindowContainerController) controller : null;
523 }
524
Wale Ogunwale571771c2016-08-26 13:18:50 -0700525 @Override
Wale Ogunwale44f21802016-09-02 12:49:48 -0700526 boolean isVisible() {
Wale Ogunwalee471be62016-10-03 07:53:55 -0700527 // If the app token isn't hidden then it is considered visible and there is no need to check
528 // its children windows to see if they are visible.
529 return !hidden;
Wale Ogunwale44f21802016-09-02 12:49:48 -0700530 }
531
532 @Override
Wale Ogunwalee287e192017-04-21 09:30:12 -0700533 void removeImmediately() {
534 onRemovedFromDisplay();
535 super.removeImmediately();
536 }
537
538 @Override
Wale Ogunwale571771c2016-08-26 13:18:50 -0700539 void removeIfPossible() {
Craig Mautnere3119b72015-01-20 15:02:36 -0800540 mIsExiting = false;
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800541 removeAllWindowsIfPossible();
Bryce Lee6d410262017-02-28 15:30:17 -0800542 removeImmediately();
Craig Mautnere3119b72015-01-20 15:02:36 -0800543 }
544
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700545 @Override
546 boolean checkCompleteDeferredRemoval() {
547 if (mIsExiting) {
548 removeIfPossible();
549 }
550 return super.checkCompleteDeferredRemoval();
551 }
552
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700553 void onRemovedFromDisplay() {
Wale Ogunwalee287e192017-04-21 09:30:12 -0700554 if (mRemovingFromDisplay) {
555 return;
556 }
557 mRemovingFromDisplay = true;
558
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700559 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app token: " + this);
560
Wale Ogunwale72919d22016-12-08 18:58:50 -0800561 boolean delayed = setVisibility(null, false, TRANSIT_UNSET, true, mVoiceInteraction);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700562
563 mService.mOpeningApps.remove(this);
Jorim Jaggi45ca0542017-05-30 16:16:01 -0700564 mService.mUnknownAppVisibilityController.appRemovedOrHidden(this);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100565 mService.mTaskSnapshotController.onAppRemoved(this);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700566 waitingToShow = false;
567 if (mService.mClosingApps.contains(this)) {
568 delayed = true;
569 } else if (mService.mAppTransition.isTransitionSet()) {
570 mService.mClosingApps.add(this);
571 delayed = true;
572 }
573
574 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app " + this + " delayed=" + delayed
575 + " animation=" + mAppAnimator.animation + " animating=" + mAppAnimator.animating);
576
577 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM, "removeAppToken: "
578 + this + " delayed=" + delayed + " Callers=" + Debug.getCallers(4));
579
Jorim Jaggi19be6052017-08-03 18:33:43 +0200580 if (startingData != null && getController() != null) {
581 getController().removeStartingWindow();
582 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800583
Winson Chung87e5d552017-04-05 11:49:38 -0700584 // If this window was animating, then we need to ensure that the app transition notifies
585 // that animations have completed in WMS.handleAnimatingStoppedAndTransitionLocked(), so
586 // add to that list now
587 if (mAppAnimator.animating) {
588 mService.mNoAnimationNotifyOnTransitionFinished.add(token);
589 }
590
Wale Ogunwalee287e192017-04-21 09:30:12 -0700591 final TaskStack stack = getStack();
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700592 if (delayed && !isEmpty()) {
593 // set the token aside because it has an active animation to be finished
594 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM,
595 "removeAppToken make exiting: " + this);
Wale Ogunwalee287e192017-04-21 09:30:12 -0700596 if (stack != null) {
597 stack.mExitingAppTokens.add(this);
598 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700599 mIsExiting = true;
600 } else {
601 // Make sure there is no animation running on this token, so any windows associated
602 // with it will be removed as soon as their animations are complete
603 mAppAnimator.clearAnimation();
604 mAppAnimator.animating = false;
Wale Ogunwale3106ae012017-05-16 08:56:37 -0700605 if (stack != null) {
606 stack.mExitingAppTokens.remove(this);
607 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700608 removeIfPossible();
609 }
610
611 removed = true;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700612 stopFreezingScreen(true, true);
Bryce Lee6d410262017-02-28 15:30:17 -0800613
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700614 if (mService.mFocusedApp == this) {
615 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "Removing focused app token:" + this);
616 mService.mFocusedApp = null;
617 mService.updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL, true /*updateInputWindows*/);
618 mService.mInputMonitor.setFocusedAppLw(null);
619 }
620
621 if (!delayed) {
622 updateReportedVisibilityLocked();
623 }
Wale Ogunwalee287e192017-04-21 09:30:12 -0700624
625 mRemovingFromDisplay = false;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700626 }
627
Chong Zhange05bcb12016-07-26 17:47:29 -0700628 void clearAnimatingFlags() {
Chong Zhangb0d26702016-08-12 16:03:29 -0700629 boolean wallpaperMightChange = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700630 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700631 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700632 wallpaperMightChange |= win.clearAnimatingFlags();
Chong Zhange05bcb12016-07-26 17:47:29 -0700633 }
Chong Zhangb0d26702016-08-12 16:03:29 -0700634 if (wallpaperMightChange) {
635 requestUpdateWallpaperIfNeeded();
636 }
Chong Zhange05bcb12016-07-26 17:47:29 -0700637 }
638
Robert Carre12aece2016-02-02 22:43:27 -0800639 void destroySurfaces() {
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700640 destroySurfaces(false /*cleanupOnResume*/);
641 }
642
643 /**
644 * Destroy surfaces which have been marked as eligible by the animator, taking care to ensure
645 * the client has finished with them.
646 *
647 * @param cleanupOnResume whether this is done when app is resumed without fully stopped. If
648 * set to true, destroy only surfaces of removed windows, and clear relevant flags of the
649 * others so that they are ready to be reused. If set to false (common case), destroy all
650 * surfaces that's eligible, if the app is already stopped.
651 */
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700652 private void destroySurfaces(boolean cleanupOnResume) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700653 boolean destroyedSomething = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700654 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700655 final WindowState win = mChildren.get(i);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700656 destroyedSomething |= win.destroySurface(cleanupOnResume, mAppStopped);
Robert Carre12aece2016-02-02 22:43:27 -0800657 }
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700658 if (destroyedSomething) {
659 final DisplayContent dc = getDisplayContent();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700660 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Robert Carre12aece2016-02-02 22:43:27 -0800661 }
662 }
663
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800664 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700665 * Notify that the app is now resumed, and it was not stopped before, perform a clean
666 * up of the surfaces
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800667 */
Jorim Jaggibae01b12017-04-11 16:29:10 -0700668 void notifyAppResumed(boolean wasStopped) {
Chong Zhangad24f962016-08-25 12:12:33 -0700669 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppResumed: wasStopped=" + wasStopped
Jorim Jaggibae01b12017-04-11 16:29:10 -0700670 + " " + this);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700671 mAppStopped = false;
chaviwd3bf08d2017-08-01 17:24:59 -0700672 // Allow the window to turn the screen on once the app is resumed again.
673 setCanTurnScreenOn(true);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700674 if (!wasStopped) {
675 destroySurfaces(true /*cleanupOnResume*/);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800676 }
Robert Carre12aece2016-02-02 22:43:27 -0800677 }
678
Chong Zhangbef461f2015-10-27 11:38:24 -0700679 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700680 * Notify that the app has stopped, and it is okay to destroy any surfaces which were
681 * keeping alive in case they were still being used.
682 */
683 void notifyAppStopped() {
684 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: " + this);
685 mAppStopped = true;
686 destroySurfaces();
687 // Remove any starting window that was added for this app if they are still around.
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800688 if (getController() != null) {
689 getController().removeStartingWindow();
690 }
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700691 }
692
693 /**
Chong Zhangbef461f2015-10-27 11:38:24 -0700694 * Checks whether we should save surfaces for this app.
695 *
696 * @return true if the surfaces should be saved, false otherwise.
697 */
698 boolean shouldSaveSurface() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800699 // We want to save surface if the app's windows are "allDrawn".
700 // (If we started entering animation early with saved surfaces, allDrawn
701 // should have been restored to true. So we'll save again in that case
702 // even if app didn't actually finish drawing.)
703 return allDrawn;
Robert Carr13f7be9e2015-12-02 18:39:45 -0800704 }
705
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700706 private boolean canRestoreSurfaces() {
707 for (int i = mChildren.size() -1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700708 final WindowState w = mChildren.get(i);
Chong Zhang92147042016-05-09 12:47:11 -0700709 if (w.canRestoreSurface()) {
Robert Carr13f7be9e2015-12-02 18:39:45 -0800710 return true;
711 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700712 }
713 return false;
714 }
715
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700716 private void clearWasVisibleBeforeClientHidden() {
717 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700718 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700719 w.clearWasVisibleBeforeClientHidden();
Chong Zhang92147042016-05-09 12:47:11 -0700720 }
721 }
722
Chong Zhang8e4bda92016-05-04 15:08:18 -0700723 /**
724 * Whether the app has some window that is invisible in layout, but
725 * animating with saved surface.
726 */
727 boolean isAnimatingInvisibleWithSavedSurface() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700728 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700729 final WindowState w = mChildren.get(i);
Chong Zhang8e4bda92016-05-04 15:08:18 -0700730 if (w.isAnimatingInvisibleWithSavedSurface()) {
731 return true;
732 }
733 }
734 return false;
735 }
736
737 /**
738 * Hide all window surfaces that's still invisible in layout but animating
739 * with a saved surface, and mark them destroying.
740 */
741 void stopUsingSavedSurfaceLocked() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700742 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700743 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700744 w.stopUsingSavedSurface();
Chong Zhang8e4bda92016-05-04 15:08:18 -0700745 }
746 destroySurfaces();
747 }
748
Chong Zhangf58631a2016-05-24 16:02:10 -0700749 void markSavedSurfaceExiting() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700750 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700751 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700752 w.markSavedSurfaceExiting();
Chong Zhangf58631a2016-05-24 16:02:10 -0700753 }
754 }
755
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700756 void restoreSavedSurfaceForInterestingWindows() {
Chong Zhang92147042016-05-09 12:47:11 -0700757 if (!canRestoreSurfaces()) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700758 clearWasVisibleBeforeClientHidden();
Chong Zhangbef461f2015-10-27 11:38:24 -0700759 return;
760 }
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700761
762 // Check if all interesting windows are drawn and we can mark allDrawn=true.
763 int interestingNotDrawn = -1;
764
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700765 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700766 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700767 interestingNotDrawn = w.restoreSavedSurfaceForInterestingWindow();
Chong Zhangbef461f2015-10-27 11:38:24 -0700768 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800769
Chong Zhang92147042016-05-09 12:47:11 -0700770 if (!allDrawn) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700771 allDrawn = (interestingNotDrawn == 0);
Chong Zhang92147042016-05-09 12:47:11 -0700772 if (allDrawn) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700773 mService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
Chong Zhang92147042016-05-09 12:47:11 -0700774 }
775 }
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700776 clearWasVisibleBeforeClientHidden();
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800777
778 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700779 "restoreSavedSurfaceForInterestingWindows: " + this + " allDrawn=" + allDrawn
780 + " interestingNotDrawn=" + interestingNotDrawn);
Chong Zhangbef461f2015-10-27 11:38:24 -0700781 }
782
783 void destroySavedSurfaces() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700784 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700785 final WindowState win = mChildren.get(i);
Robert Carr13f7be9e2015-12-02 18:39:45 -0800786 win.destroySavedSurface();
Chong Zhangbef461f2015-10-27 11:38:24 -0700787 }
Chong Zhang92147042016-05-09 12:47:11 -0700788 }
789
790 void clearAllDrawn() {
791 allDrawn = false;
792 deferClearAllDrawn = false;
Chong Zhang8e4bda92016-05-04 15:08:18 -0700793 allDrawnExcludingSaved = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700794 }
795
Bryce Lee6d410262017-02-28 15:30:17 -0800796 Task getTask() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800797 return (Task) getParent();
Bryce Lee6d410262017-02-28 15:30:17 -0800798 }
799
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800800 TaskStack getStack() {
801 final Task task = getTask();
802 if (task != null) {
803 return task.mStack;
804 } else {
805 return null;
806 }
807 }
808
Bryce Lee6d410262017-02-28 15:30:17 -0800809 @Override
810 void onParentSet() {
811 super.onParentSet();
812
Robert Carred3e83b2017-04-21 13:26:55 -0700813 final Task task = getTask();
814
Bryce Lee6d410262017-02-28 15:30:17 -0800815 // When the associated task is {@code null}, the {@link AppWindowToken} can no longer
816 // access visual elements like the {@link DisplayContent}. We must remove any associations
817 // such as animations.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800818 if (!mReparenting) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800819 if (task == null) {
820 // It is possible we have been marked as a closing app earlier. We must remove ourselves
821 // from this list so we do not participate in any future animations.
822 mService.mClosingApps.remove(this);
Robert Carred3e83b2017-04-21 13:26:55 -0700823 } else if (mLastParent != null && mLastParent.mStack != null) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800824 task.mStack.mExitingAppTokens.remove(this);
825 }
Bryce Lee6d410262017-02-28 15:30:17 -0800826 }
Robert Carred3e83b2017-04-21 13:26:55 -0700827 mLastParent = task;
Bryce Lee6d410262017-02-28 15:30:17 -0800828 }
829
Wale Ogunwalefa854eb2016-09-20 13:43:52 -0700830 void postWindowRemoveStartingWindowCleanup(WindowState win) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700831 // TODO: Something smells about the code below...Is there a better way?
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700832 if (startingWindow == win) {
833 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Notify removed startingWindow " + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800834 if (getController() != null) {
835 getController().removeStartingWindow();
836 }
Wale Ogunwale6c459212017-05-17 08:56:03 -0700837 } else if (mChildren.size() == 0) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700838 // If this is the last window and we had requested a starting transition window,
839 // well there is no point now.
Jorim Jaggi02886a82016-12-06 09:10:06 -0800840 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Nulling last startingData");
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700841 startingData = null;
Wale Ogunwale6c459212017-05-17 08:56:03 -0700842 if (mHiddenSetFromTransferredStartingWindow) {
843 // We set the hidden state to false for the token from a transferred starting window.
844 // We now reset it back to true since the starting window was the last window in the
845 // token.
846 hidden = true;
847 }
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100848 } else if (mChildren.size() == 1 && startingSurface != null && !isRelaunching()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700849 // If this is the last window except for a starting transition window,
850 // we need to get rid of the starting transition.
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200851 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Last window, removing starting window "
852 + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800853 if (getController() != null) {
854 getController().removeStartingWindow();
855 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700856 }
857 }
858
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700859 void removeDeadWindows() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700860 for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700861 WindowState win = mChildren.get(winNdx);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800862 if (win.mAppDied) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700863 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.w(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700864 "removeDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800865 // Set mDestroying, we don't want any animation or delayed removal here.
866 win.mDestroying = true;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700867 // Also removes child windows.
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700868 win.removeIfPossible();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800869 }
870 }
871 }
872
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700873 boolean hasWindowsAlive() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700874 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700875 // No need to loop through child windows as the answer should be the same as that of the
876 // parent window.
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700877 if (!(mChildren.get(i)).mAppDied) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700878 return true;
879 }
880 }
881 return false;
882 }
883
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700884 void setWillReplaceWindows(boolean animate) {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700885 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
886 "Marking app token " + this + " with replacing windows.");
Robert Carra1eb4392015-12-10 12:43:51 -0800887
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700888 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700889 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700890 w.setWillReplaceWindow(animate);
Robert Carra1eb4392015-12-10 12:43:51 -0800891 }
892 if (animate) {
893 // Set-up dummy animation so we can start treating windows associated with this
894 // token like they are in transition before the new app window is ready for us to
895 // run the real transition animation.
896 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700897 "setWillReplaceWindow() Setting dummy animation on: " + this);
Robert Carra1eb4392015-12-10 12:43:51 -0800898 mAppAnimator.setDummyAnimation();
899 }
900 }
901
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700902 void setWillReplaceChildWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700903 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + this
Robert Carr23fa16b2016-01-13 13:19:58 -0800904 + " with replacing child windows.");
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700905 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700906 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700907 w.setWillReplaceChildWindows();
Robert Carr23fa16b2016-01-13 13:19:58 -0800908 }
909 }
910
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700911 void clearWillReplaceWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700912 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
913 "Resetting app token " + this + " of replacing window marks.");
Chong Zhangf596cd52016-01-05 13:42:44 -0800914
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700915 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700916 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700917 w.clearWillReplaceWindow();
Chong Zhangf596cd52016-01-05 13:42:44 -0800918 }
919 }
920
Chong Zhang4d7369a2016-04-25 16:09:14 -0700921 void requestUpdateWallpaperIfNeeded() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700922 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700923 final WindowState w = mChildren.get(i);
Chong Zhang4d7369a2016-04-25 16:09:14 -0700924 w.requestUpdateWallpaperIfNeeded();
925 }
926 }
927
Chong Zhangd78ddb42016-03-02 17:01:14 -0800928 boolean isRelaunching() {
929 return mPendingRelaunchCount > 0;
930 }
931
Robert Carr68375192017-06-13 12:41:53 -0700932 boolean shouldFreezeBounds() {
933 final Task task = getTask();
934
935 // For freeform windows, we can't freeze the bounds at the moment because this would make
936 // the resizing unresponsive.
937 if (task == null || task.inFreeformWorkspace()) {
938 return false;
939 }
940
941 // We freeze the bounds while drag resizing to deal with the time between
942 // the divider/drag handle being released, and the handling it's new
943 // configuration. If we are relaunched outside of the drag resizing state,
944 // we need to be careful not to do this.
945 return getTask().isDragResizing();
946 }
947
Chong Zhangd78ddb42016-03-02 17:01:14 -0800948 void startRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700949 if (shouldFreezeBounds()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -0800950 freezeBounds();
951 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800952
953 // In the process of tearing down before relaunching, the app will
954 // try and clean up it's child surfaces. We need to prevent this from
955 // happening, so we sever the children, transfering their ownership
956 // from the client it-self to the parent surface (owned by us).
957 for (int i = mChildren.size() - 1; i >= 0; i--) {
958 final WindowState w = mChildren.get(i);
959 w.mWinAnimator.detachChildren();
960 }
961
Chong Zhangd78ddb42016-03-02 17:01:14 -0800962 mPendingRelaunchCount++;
963 }
964
965 void finishRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700966 unfreezeBounds();
967
Chong Zhangd78ddb42016-03-02 17:01:14 -0800968 if (mPendingRelaunchCount > 0) {
969 mPendingRelaunchCount--;
Bryce Lee081554b2017-05-25 07:52:12 -0700970 } else {
971 // Update keyguard flags upon finishing relaunch.
972 checkKeyguardFlagsChanged();
Chong Zhangd78ddb42016-03-02 17:01:14 -0800973 }
974 }
975
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700976 void clearRelaunching() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700977 if (mPendingRelaunchCount == 0) {
978 return;
979 }
Robert Carr68375192017-06-13 12:41:53 -0700980 unfreezeBounds();
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700981 mPendingRelaunchCount = 0;
982 }
983
Wale Ogunwale07bcab72016-10-14 15:30:09 -0700984 /**
985 * Returns true if the new child window we are adding to this token is considered greater than
986 * the existing child window in this token in terms of z-order.
987 */
988 @Override
989 protected boolean isFirstChildWindowGreaterThanSecond(WindowState newWindow,
990 WindowState existingWindow) {
991 final int type1 = newWindow.mAttrs.type;
992 final int type2 = existingWindow.mAttrs.type;
993
994 // Base application windows should be z-ordered BELOW all other windows in the app token.
995 if (type1 == TYPE_BASE_APPLICATION && type2 != TYPE_BASE_APPLICATION) {
996 return false;
997 } else if (type1 != TYPE_BASE_APPLICATION && type2 == TYPE_BASE_APPLICATION) {
998 return true;
999 }
1000
1001 // Starting windows should be z-ordered ABOVE all other windows in the app token.
1002 if (type1 == TYPE_APPLICATION_STARTING && type2 != TYPE_APPLICATION_STARTING) {
1003 return true;
1004 } else if (type1 != TYPE_APPLICATION_STARTING && type2 == TYPE_APPLICATION_STARTING) {
1005 return false;
1006 }
1007
1008 // Otherwise the new window is greater than the existing window.
1009 return true;
1010 }
1011
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001012 @Override
Robert Carra1eb4392015-12-10 12:43:51 -08001013 void addWindow(WindowState w) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001014 super.addWindow(w);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001015
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001016 boolean gotReplacementWindow = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001017 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001018 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001019 gotReplacementWindow |= candidate.setReplacementWindowIfNeeded(w);
1020 }
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001021
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001022 // if we got a replacement window, reset the timeout to give drawing more time
1023 if (gotReplacementWindow) {
1024 mService.scheduleWindowReplacementTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -08001025 }
Jorim Jaggife762342016-10-13 14:33:27 +02001026 checkKeyguardFlagsChanged();
1027 }
1028
1029 @Override
1030 void removeChild(WindowState child) {
1031 super.removeChild(child);
1032 checkKeyguardFlagsChanged();
Robert Carra1eb4392015-12-10 12:43:51 -08001033 }
1034
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001035 private boolean waitingForReplacement() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001036 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001037 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001038 if (candidate.waitingForReplacement()) {
Robert Carra1eb4392015-12-10 12:43:51 -08001039 return true;
1040 }
1041 }
1042 return false;
1043 }
1044
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001045 void onWindowReplacementTimeout() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001046 for (int i = mChildren.size() - 1; i >= 0; --i) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001047 (mChildren.get(i)).onWindowReplacementTimeout();
Robert Carra1eb4392015-12-10 12:43:51 -08001048 }
1049 }
1050
Winson Chung30480042017-01-26 10:55:34 -08001051 void reparent(Task task, int position) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001052 final Task currentTask = getTask();
1053 if (task == currentTask) {
Winson Chung30480042017-01-26 10:55:34 -08001054 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001055 "window token=" + this + " already child of task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -08001056 }
Bryce Lee6d410262017-02-28 15:30:17 -08001057
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001058 if (currentTask.mStack != task.mStack) {
Bryce Lee6d410262017-02-28 15:30:17 -08001059 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001060 "window token=" + this + " current task=" + currentTask
Bryce Lee6d410262017-02-28 15:30:17 -08001061 + " belongs to a different stack than " + task);
1062 }
1063
Winson Chung30480042017-01-26 10:55:34 -08001064 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "reParentWindowToken: removing window token=" + this
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001065 + " from task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -08001066 final DisplayContent prevDisplayContent = getDisplayContent();
1067
Bryce Lee6d410262017-02-28 15:30:17 -08001068 mReparenting = true;
1069
Winson Chung30480042017-01-26 10:55:34 -08001070 getParent().removeChild(this);
1071 task.addChild(this, position);
1072
Bryce Lee6d410262017-02-28 15:30:17 -08001073 mReparenting = false;
1074
Winson Chung30480042017-01-26 10:55:34 -08001075 // Relayout display(s).
1076 final DisplayContent displayContent = task.getDisplayContent();
1077 displayContent.setLayoutNeeded();
1078 if (prevDisplayContent != displayContent) {
1079 onDisplayChanged(displayContent);
1080 prevDisplayContent.setLayoutNeeded();
1081 }
1082 }
1083
Jorim Jaggi0429f352015-12-22 16:29:16 +01001084 /**
1085 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
1086 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
1087 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
1088 * with a queue.
1089 */
Chong Zhangd78ddb42016-03-02 17:01:14 -08001090 private void freezeBounds() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001091 final Task task = getTask();
1092 mFrozenBounds.offer(new Rect(task.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001093
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001094 if (task.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001095 // We didn't call prepareFreezingBounds on the task, so use the current value.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001096 mFrozenMergedConfig.offer(new Configuration(task.getConfiguration()));
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001097 } else {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001098 mFrozenMergedConfig.offer(new Configuration(task.mPreparedFrozenMergedConfig));
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001099 }
Andrii Kulianb10330d2016-09-16 13:51:46 -07001100 // Calling unset() to make it equal to Configuration.EMPTY.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001101 task.mPreparedFrozenMergedConfig.unset();
Jorim Jaggi0429f352015-12-22 16:29:16 +01001102 }
1103
1104 /**
1105 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
1106 */
Chong Zhangd78ddb42016-03-02 17:01:14 -08001107 private void unfreezeBounds() {
Robert Carr68375192017-06-13 12:41:53 -07001108 if (mFrozenBounds.isEmpty()) {
1109 return;
Wale Ogunwale37dbafc2016-06-27 10:15:20 -07001110 }
Robert Carr68375192017-06-13 12:41:53 -07001111 mFrozenBounds.remove();
Wale Ogunwale37dbafc2016-06-27 10:15:20 -07001112 if (!mFrozenMergedConfig.isEmpty()) {
1113 mFrozenMergedConfig.remove();
1114 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001115 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001116 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001117 win.onUnfreezeBounds();
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001118 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001119 mService.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +01001120 }
1121
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001122 void setAppLayoutChanges(int changes, String reason) {
1123 if (!mChildren.isEmpty()) {
1124 final DisplayContent dc = getDisplayContent();
1125 dc.pendingLayoutChanges |= changes;
1126 if (DEBUG_LAYOUT_REPEATS) {
1127 mService.mWindowPlacerLocked.debugLayoutRepeats(reason, dc.pendingLayoutChanges);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001128 }
1129 }
1130 }
1131
1132 void removeReplacedWindowIfNeeded(WindowState replacement) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001133 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001134 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001135 if (win.removeReplacedWindowIfNeeded(replacement)) {
1136 return;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001137 }
1138 }
1139 }
1140
1141 void startFreezingScreen() {
1142 if (DEBUG_ORIENTATION) logWithStack(TAG, "Set freezing of " + appToken + ": hidden="
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001143 + hidden + " freezing=" + mAppAnimator.freezingScreen + " hiddenRequested="
1144 + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001145 if (!hiddenRequested) {
1146 if (!mAppAnimator.freezingScreen) {
1147 mAppAnimator.freezingScreen = true;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001148 mService.registerAppFreezeListener(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001149 mAppAnimator.lastFreezeDuration = 0;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001150 mService.mAppsFreezingScreen++;
1151 if (mService.mAppsFreezingScreen == 1) {
Bryce Lee8f853582017-06-05 17:25:59 -07001152 mService.startFreezingDisplayLocked(false, 0, 0, getDisplayContent());
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001153 mService.mH.removeMessages(H.APP_FREEZE_TIMEOUT);
1154 mService.mH.sendEmptyMessageDelayed(H.APP_FREEZE_TIMEOUT, 2000);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001155 }
1156 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001157 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001158 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001159 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001160 w.onStartFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001161 }
1162 }
1163 }
1164
1165 void stopFreezingScreen(boolean unfreezeSurfaceNow, boolean force) {
1166 if (!mAppAnimator.freezingScreen) {
1167 return;
1168 }
1169 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Clear freezing of " + this + " force=" + force);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001170 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001171 boolean unfrozeWindows = false;
1172 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001173 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001174 unfrozeWindows |= w.onStopFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001175 }
1176 if (force || unfrozeWindows) {
1177 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "No longer freezing: " + this);
1178 mAppAnimator.freezingScreen = false;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001179 mService.unregisterAppFreezeListener(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001180 mAppAnimator.lastFreezeDuration =
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001181 (int)(SystemClock.elapsedRealtime() - mService.mDisplayFreezeTime);
1182 mService.mAppsFreezingScreen--;
1183 mService.mLastFinishedFreezeSource = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001184 }
1185 if (unfreezeSurfaceNow) {
1186 if (unfrozeWindows) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001187 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001188 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001189 mService.stopFreezingDisplayLocked();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001190 }
1191 }
1192
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001193 @Override
1194 public void onAppFreezeTimeout() {
1195 Slog.w(TAG_WM, "Force clearing freeze: " + this);
1196 stopFreezingScreen(true, true);
1197 }
1198
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001199 boolean transferStartingWindow(IBinder transferFrom) {
Wale Ogunwale02319a62016-09-26 15:21:22 -07001200 final AppWindowToken fromToken = getDisplayContent().getAppWindowToken(transferFrom);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001201 if (fromToken == null) {
1202 return false;
1203 }
1204
1205 final WindowState tStartingWindow = fromToken.startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001206 if (tStartingWindow != null && fromToken.startingSurface != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001207 // In this case, the starting icon has already been displayed, so start
1208 // letting windows get shown immediately without any more transitions.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001209 mService.mSkipAppTransitionAnimation = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001210
1211 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Moving existing starting " + tStartingWindow
1212 + " from " + fromToken + " to " + this);
1213
1214 final long origId = Binder.clearCallingIdentity();
1215
1216 // Transfer the starting window over to the new token.
1217 startingData = fromToken.startingData;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001218 startingSurface = fromToken.startingSurface;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001219 startingDisplayed = fromToken.startingDisplayed;
1220 fromToken.startingDisplayed = false;
1221 startingWindow = tStartingWindow;
1222 reportedVisible = fromToken.reportedVisible;
1223 fromToken.startingData = null;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001224 fromToken.startingSurface = null;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001225 fromToken.startingWindow = null;
1226 fromToken.startingMoved = true;
1227 tStartingWindow.mToken = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001228 tStartingWindow.mAppToken = this;
1229
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001230 if (DEBUG_ADD_REMOVE || DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001231 "Removing starting " + tStartingWindow + " from " + fromToken);
Wale Ogunwalefa854eb2016-09-20 13:43:52 -07001232 fromToken.removeChild(tStartingWindow);
1233 fromToken.postWindowRemoveStartingWindowCleanup(tStartingWindow);
Wale Ogunwale6c459212017-05-17 08:56:03 -07001234 fromToken.mHiddenSetFromTransferredStartingWindow = false;
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001235 addWindow(tStartingWindow);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001236
1237 // Propagate other interesting state between the tokens. If the old token is displayed,
1238 // we should immediately force the new one to be displayed. If it is animating, we need
1239 // to move that animation to the new one.
1240 if (fromToken.allDrawn) {
1241 allDrawn = true;
1242 deferClearAllDrawn = fromToken.deferClearAllDrawn;
1243 }
1244 if (fromToken.firstWindowDrawn) {
1245 firstWindowDrawn = true;
1246 }
1247 if (!fromToken.hidden) {
1248 hidden = false;
1249 hiddenRequested = false;
Wale Ogunwale6c459212017-05-17 08:56:03 -07001250 mHiddenSetFromTransferredStartingWindow = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001251 }
Wale Ogunwale89973222017-04-23 18:39:45 -07001252 setClientHidden(fromToken.mClientHidden);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001253 fromToken.mAppAnimator.transferCurrentAnimation(
1254 mAppAnimator, tStartingWindow.mWinAnimator);
1255
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001256 mService.updateFocusedWindowLocked(
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001257 UPDATE_FOCUS_WILL_PLACE_SURFACES, true /*updateInputWindows*/);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001258 getDisplayContent().setLayoutNeeded();
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001259 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001260 Binder.restoreCallingIdentity(origId);
1261 return true;
1262 } else if (fromToken.startingData != null) {
1263 // The previous app was getting ready to show a
1264 // starting window, but hasn't yet done so. Steal it!
1265 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1266 "Moving pending starting from " + fromToken + " to " + this);
1267 startingData = fromToken.startingData;
1268 fromToken.startingData = null;
1269 fromToken.startingMoved = true;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001270 if (getController() != null) {
1271 getController().scheduleAddStartingWindow();
1272 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001273 return true;
1274 }
1275
1276 final AppWindowAnimator tAppAnimator = fromToken.mAppAnimator;
1277 final AppWindowAnimator wAppAnimator = mAppAnimator;
1278 if (tAppAnimator.thumbnail != null) {
1279 // The old token is animating with a thumbnail, transfer that to the new token.
1280 if (wAppAnimator.thumbnail != null) {
1281 wAppAnimator.thumbnail.destroy();
1282 }
1283 wAppAnimator.thumbnail = tAppAnimator.thumbnail;
1284 wAppAnimator.thumbnailLayer = tAppAnimator.thumbnailLayer;
1285 wAppAnimator.thumbnailAnimation = tAppAnimator.thumbnailAnimation;
1286 tAppAnimator.thumbnail = null;
1287 }
1288 return false;
1289 }
1290
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001291 boolean isLastWindow(WindowState win) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001292 return mChildren.size() == 1 && mChildren.get(0) == win;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001293 }
1294
1295 void setAllAppWinAnimators() {
1296 final ArrayList<WindowStateAnimator> allAppWinAnimators = mAppAnimator.mAllAppWinAnimators;
1297 allAppWinAnimators.clear();
1298
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001299 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001300 for (int j = 0; j < windowsCount; j++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001301 (mChildren.get(j)).addWinAnimatorToList(allAppWinAnimators);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001302 }
1303 }
1304
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001305 @Override
1306 void onAppTransitionDone() {
1307 sendingToBottom = false;
1308 }
1309
Wale Ogunwale51362492016-09-08 17:49:17 -07001310 /**
1311 * We override because this class doesn't want its children affecting its reported orientation
1312 * in anyway.
1313 */
1314 @Override
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001315 int getOrientation(int candidate) {
Bryce Lee0e7b7c92017-08-10 14:59:00 -07001316 // We do not allow non-fullscreen apps to influence orientation starting in O-MR1. While we
1317 // do throw an exception in {@link Activity#onCreate} and
Bryce Lee39791592017-04-26 09:29:12 -07001318 // {@link Activity#setRequestedOrientation}, we also ignore the orientation here so that
1319 // other calculations aren't affected.
Bryce Lee0e7b7c92017-08-10 14:59:00 -07001320 if (!fillsParent() && mTargetSdk >= O_MR1) {
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001321 // Can't specify orientation if app doesn't fill parent.
1322 return SCREEN_ORIENTATION_UNSET;
1323 }
1324
1325 if (candidate == SCREEN_ORIENTATION_BEHIND) {
1326 // Allow app to specify orientation regardless of its visibility state if the current
1327 // candidate want us to use orientation behind. I.e. the visible app on-top of this one
1328 // wants us to use the orientation of the app behind it.
1329 return mOrientation;
1330 }
1331
Bryce Lee61fbcbc2017-03-10 14:14:03 -08001332 // The {@link AppWindowToken} should only specify an orientation when it is not closing or
1333 // going to the bottom. Allowing closing {@link AppWindowToken} to participate can lead to
1334 // an Activity in another task being started in the wrong orientation during the transition.
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001335 if (!(sendingToBottom || mService.mClosingApps.contains(this))
Bryce Lee00d586d2017-07-28 20:48:43 -07001336 && (isVisible() || mService.mOpeningApps.contains(this) || isOnTop())) {
Bryce Leea163b762017-01-24 11:05:01 -08001337 return mOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001338 }
Bryce Leea163b762017-01-24 11:05:01 -08001339
1340 return SCREEN_ORIENTATION_UNSET;
Wale Ogunwale51362492016-09-08 17:49:17 -07001341 }
1342
Wale Ogunwaleb198b742016-12-01 08:44:09 -08001343 /** Returns the app's preferred orientation regardless of its currently visibility state. */
1344 int getOrientationIgnoreVisibility() {
1345 return mOrientation;
1346 }
1347
Craig Mautnerdbb79912012-03-01 18:59:14 -08001348 @Override
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001349 void checkAppWindowsReadyToShow() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001350 if (allDrawn == mAppAnimator.allDrawn) {
1351 return;
1352 }
1353
1354 mAppAnimator.allDrawn = allDrawn;
1355 if (!allDrawn) {
1356 return;
1357 }
1358
1359 // The token has now changed state to having all windows shown... what to do, what to do?
1360 if (mAppAnimator.freezingScreen) {
1361 mAppAnimator.showAllWindowsLocked();
1362 stopFreezingScreen(false, true);
1363 if (DEBUG_ORIENTATION) Slog.i(TAG,
1364 "Setting mOrientationChangeComplete=true because wtoken " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001365 + " numInteresting=" + mNumInterestingWindows + " numDrawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001366 // This will set mOrientationChangeComplete and cause a pass through layout.
1367 setAppLayoutChanges(FINISH_LAYOUT_REDO_WALLPAPER,
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001368 "checkAppWindowsReadyToShow: freezingScreen");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001369 } else {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001370 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM, "checkAppWindowsReadyToShow");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001371
1372 // We can now show all of the drawn windows!
1373 if (!mService.mOpeningApps.contains(this)) {
1374 mService.mAnimator.orAnimating(mAppAnimator.showAllWindowsLocked());
1375 }
1376 }
1377 }
1378
Matthew Ng5d23afa2017-06-21 16:16:24 -07001379 /**
Bryce Leed390deb2017-06-22 13:14:28 -07001380 * Returns whether the drawn window states of this {@link AppWindowToken} has considered every
1381 * child {@link WindowState}. A child is considered if it has been passed into
1382 * {@link #updateDrawnWindowStates(WindowState)} after being added. This is used to determine
1383 * whether states, such as {@code allDrawn}, can be set, which relies on state variables such as
1384 * {@code mNumInterestingWindows}, which depend on all {@link WindowState}s being considered.
1385 *
1386 * @return {@code true} If all children have been considered, {@code false}.
1387 */
1388 private boolean allDrawnStatesConsidered() {
Bryce Lee6311c4b2017-07-06 14:09:29 -07001389 for (int i = mChildren.size() - 1; i >= 0; --i) {
1390 final WindowState child = mChildren.get(i);
1391 if (child.mightAffectAllDrawn(false /*visibleOnly*/ )
1392 && !child.getDrawnStateEvaluated()) {
Bryce Leed390deb2017-06-22 13:14:28 -07001393 return false;
1394 }
1395 }
1396 return true;
1397 }
1398
1399 /**
Matthew Ng5d23afa2017-06-21 16:16:24 -07001400 * Determines if the token has finished drawing. This should only be called from
1401 * {@link DisplayContent#applySurfaceChangesTransaction}
1402 */
Matthew Ng498c71d2017-04-18 13:55:45 -07001403 void updateAllDrawn() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001404 if (!allDrawn) {
Matthew Ng498c71d2017-04-18 13:55:45 -07001405 // Number of drawn windows can be less when a window is being relaunched, wait for
Bryce Leed390deb2017-06-22 13:14:28 -07001406 // all windows to be launched and drawn for this token be considered all drawn.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001407 final int numInteresting = mNumInterestingWindows;
Bryce Leed390deb2017-06-22 13:14:28 -07001408
1409 // We must make sure that all present children have been considered (determined by
1410 // {@link #allDrawnStatesConsidered}) before evaluating whether everything has been
1411 // drawn.
1412 if (numInteresting > 0 && allDrawnStatesConsidered()
1413 && mNumDrawnWindows >= numInteresting && !isRelaunching()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001414 if (DEBUG_VISIBILITY) Slog.v(TAG, "allDrawn: " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001415 + " interesting=" + numInteresting + " drawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001416 allDrawn = true;
1417 // Force an additional layout pass where
1418 // WindowStateAnimator#commitFinishDrawingLocked() will call performShowLocked().
Matthew Ng498c71d2017-04-18 13:55:45 -07001419 if (mDisplayContent != null) {
1420 mDisplayContent.setLayoutNeeded();
1421 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001422 mService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
Robert Carrecc06b32017-04-18 14:25:10 -07001423
Winson Chunge7ba6862017-05-24 12:13:33 -07001424 // Notify the pinned stack upon all windows drawn. If there was an animation in
1425 // progress then this signal will resume that animation.
1426 final TaskStack pinnedStack = mDisplayContent.getStackById(PINNED_STACK_ID);
1427 if (pinnedStack != null) {
1428 pinnedStack.onAllWindowsDrawn();
Robert Carrecc06b32017-04-18 14:25:10 -07001429 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001430 }
1431 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001432
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001433 if (!allDrawnExcludingSaved) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001434 int numInteresting = mNumInterestingWindowsExcludingSaved;
1435 if (numInteresting > 0 && mNumDrawnWindowsExcludingSaved >= numInteresting) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001436 if (DEBUG_VISIBILITY) Slog.v(TAG, "allDrawnExcludingSaved: " + this
1437 + " interesting=" + numInteresting
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001438 + " drawn=" + mNumDrawnWindowsExcludingSaved);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001439 allDrawnExcludingSaved = true;
Matthew Ng498c71d2017-04-18 13:55:45 -07001440 if (mDisplayContent != null) {
1441 mDisplayContent.setLayoutNeeded();
1442 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001443 if (isAnimatingInvisibleWithSavedSurface()
1444 && !mService.mFinishedEarlyAnim.contains(this)) {
1445 mService.mFinishedEarlyAnim.add(this);
1446 }
1447 }
1448 }
1449 }
1450
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001451 /**
1452 * Updated this app token tracking states for interesting and drawn windows based on the window.
1453 *
1454 * @return Returns true if the input window is considered interesting and drawn while all the
1455 * windows in this app token where not considered drawn as of the last pass.
1456 */
1457 boolean updateDrawnWindowStates(WindowState w) {
Bryce Leed390deb2017-06-22 13:14:28 -07001458 w.setDrawnStateEvaluated(true /*evaluated*/);
1459
Jorim Jaggie4b0f282017-05-17 15:10:29 +02001460 if (DEBUG_STARTING_WINDOW_VERBOSE && w == startingWindow) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001461 Slog.d(TAG, "updateWindows: starting " + w + " isOnScreen=" + w.isOnScreen()
1462 + " allDrawn=" + allDrawn + " freezingScreen=" + mAppAnimator.freezingScreen);
1463 }
1464
1465 if (allDrawn && allDrawnExcludingSaved && !mAppAnimator.freezingScreen) {
1466 return false;
1467 }
1468
1469 if (mLastTransactionSequence != mService.mTransactionSequence) {
1470 mLastTransactionSequence = mService.mTransactionSequence;
1471 mNumInterestingWindows = mNumDrawnWindows = 0;
1472 mNumInterestingWindowsExcludingSaved = 0;
1473 mNumDrawnWindowsExcludingSaved = 0;
1474 startingDisplayed = false;
1475 }
1476
1477 final WindowStateAnimator winAnimator = w.mWinAnimator;
1478
1479 boolean isInterestingAndDrawn = false;
1480
1481 if (!allDrawn && w.mightAffectAllDrawn(false /* visibleOnly */)) {
1482 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
1483 Slog.v(TAG, "Eval win " + w + ": isDrawn=" + w.isDrawnLw()
1484 + ", isAnimationSet=" + winAnimator.isAnimationSet());
1485 if (!w.isDrawnLw()) {
1486 Slog.v(TAG, "Not displayed: s=" + winAnimator.mSurfaceController
1487 + " pv=" + w.mPolicyVisibility
1488 + " mDrawState=" + winAnimator.drawStateToString()
1489 + " ph=" + w.isParentWindowHidden() + " th=" + hiddenRequested
1490 + " a=" + winAnimator.mAnimating);
1491 }
1492 }
1493
1494 if (w != startingWindow) {
1495 if (w.isInteresting()) {
1496 mNumInterestingWindows++;
1497 if (w.isDrawnLw()) {
1498 mNumDrawnWindows++;
1499
1500 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG, "tokenMayBeDrawn: "
1501 + this + " w=" + w + " numInteresting=" + mNumInterestingWindows
1502 + " freezingScreen=" + mAppAnimator.freezingScreen
1503 + " mAppFreezing=" + w.mAppFreezing);
1504
1505 isInterestingAndDrawn = true;
1506 }
1507 }
1508 } else if (w.isDrawnLw()) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001509 if (getController() != null) {
1510 getController().reportStartingWindowDrawn();
1511 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001512 startingDisplayed = true;
1513 }
1514 }
1515
1516 if (!allDrawnExcludingSaved && w.mightAffectAllDrawn(true /* visibleOnly */)) {
1517 if (w != startingWindow && w.isInteresting()) {
1518 mNumInterestingWindowsExcludingSaved++;
1519 if (w.isDrawnLw() && !w.isAnimatingWithSavedSurface()) {
1520 mNumDrawnWindowsExcludingSaved++;
1521
1522 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG,
1523 "tokenMayBeDrawnExcludingSaved: " + this + " w=" + w
1524 + " numInteresting=" + mNumInterestingWindowsExcludingSaved
1525 + " freezingScreen=" + mAppAnimator.freezingScreen
1526 + " mAppFreezing=" + w.mAppFreezing);
1527
1528 isInterestingAndDrawn = true;
1529 }
1530 }
1531 }
1532
1533 return isInterestingAndDrawn;
1534 }
1535
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001536 @Override
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001537 void stepAppWindowsAnimation(long currentTime) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001538 mAppAnimator.wasAnimating = mAppAnimator.animating;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001539 if (mAppAnimator.stepAnimationLocked(currentTime)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001540 mAppAnimator.animating = true;
1541 mService.mAnimator.setAnimating(true);
1542 mService.mAnimator.mAppWindowAnimating = true;
1543 } else if (mAppAnimator.wasAnimating) {
1544 // stopped animating, do one more pass through the layout
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001545 setAppLayoutChanges(FINISH_LAYOUT_REDO_WALLPAPER,
1546 DEBUG_LAYOUT_REPEATS ? "appToken " + this + " done" : null);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001547 if (DEBUG_ANIM) Slog.v(TAG, "updateWindowsApps...: done animating " + this);
1548 }
1549 }
1550
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001551 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001552 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001553 // For legacy reasons we process the TaskStack.mExitingAppTokens first in DisplayContent
1554 // before the non-exiting app tokens. So, we skip the exiting app tokens here.
1555 // TODO: Investigate if we need to continue to do this or if we can just process them
1556 // in-order.
1557 if (mIsExiting && !waitingForReplacement()) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001558 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001559 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001560 return forAllWindowsUnchecked(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001561 }
1562
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001563 boolean forAllWindowsUnchecked(ToBooleanFunction<WindowState> callback,
1564 boolean traverseTopToBottom) {
1565 return super.forAllWindows(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001566 }
1567
1568 @Override
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001569 AppWindowToken asAppWindowToken() {
1570 // I am an app window token!
1571 return this;
1572 }
1573
1574 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001575 boolean fillsParent() {
1576 return mFillsParent;
1577 }
1578
1579 void setFillsParent(boolean fillsParent) {
1580 mFillsParent = fillsParent;
1581 }
1582
Jorim Jaggife762342016-10-13 14:33:27 +02001583 boolean containsDismissKeyguardWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001584 // Window state is transient during relaunch. We are not guaranteed to be frozen during the
1585 // entirety of the relaunch.
1586 if (isRelaunching()) {
1587 return mLastContainsDismissKeyguardWindow;
1588 }
1589
Jorim Jaggife762342016-10-13 14:33:27 +02001590 for (int i = mChildren.size() - 1; i >= 0; i--) {
1591 if ((mChildren.get(i).mAttrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
1592 return true;
1593 }
1594 }
1595 return false;
1596 }
1597
1598 boolean containsShowWhenLockedWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001599 // When we are relaunching, it is possible for us to be unfrozen before our previous
1600 // windows have been added back. Using the cached value ensures that our previous
1601 // showWhenLocked preference is honored until relaunching is complete.
1602 if (isRelaunching()) {
1603 return mLastContainsShowWhenLockedWindow;
1604 }
1605
Jorim Jaggife762342016-10-13 14:33:27 +02001606 for (int i = mChildren.size() - 1; i >= 0; i--) {
1607 if ((mChildren.get(i).mAttrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
1608 return true;
1609 }
1610 }
Bryce Lee081554b2017-05-25 07:52:12 -07001611
Jorim Jaggife762342016-10-13 14:33:27 +02001612 return false;
1613 }
1614
1615 void checkKeyguardFlagsChanged() {
1616 final boolean containsDismissKeyguard = containsDismissKeyguardWindow();
1617 final boolean containsShowWhenLocked = containsShowWhenLockedWindow();
1618 if (containsDismissKeyguard != mLastContainsDismissKeyguardWindow
1619 || containsShowWhenLocked != mLastContainsShowWhenLockedWindow) {
1620 mService.notifyKeyguardFlagsChanged(null /* callback */);
1621 }
1622 mLastContainsDismissKeyguardWindow = containsDismissKeyguard;
1623 mLastContainsShowWhenLockedWindow = containsShowWhenLocked;
1624 }
1625
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001626 WindowState getImeTargetBelowWindow(WindowState w) {
1627 final int index = mChildren.indexOf(w);
1628 if (index > 0) {
1629 final WindowState target = mChildren.get(index - 1);
1630 if (target.canBeImeTarget()) {
1631 return target;
1632 }
1633 }
1634 return null;
1635 }
1636
Winson Chungbe9be7f2017-06-28 10:55:22 -07001637 int getLowestAnimLayer() {
1638 for (int i = 0; i < mChildren.size(); i++) {
1639 final WindowState w = mChildren.get(i);
1640 if (w.mRemoved) {
1641 continue;
1642 }
1643 return w.mWinAnimator.mAnimLayer;
1644 }
1645 return Integer.MAX_VALUE;
1646 }
1647
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001648 WindowState getHighestAnimLayerWindow(WindowState currentTarget) {
1649 WindowState candidate = null;
1650 for (int i = mChildren.indexOf(currentTarget); i >= 0; i--) {
1651 final WindowState w = mChildren.get(i);
1652 if (w.mRemoved) {
1653 continue;
1654 }
1655 if (candidate == null || w.mWinAnimator.mAnimLayer >
1656 candidate.mWinAnimator.mAnimLayer) {
1657 candidate = w;
1658 }
1659 }
1660 return candidate;
1661 }
1662
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001663 /**
1664 * See {@link Activity#setDisablePreviewScreenshots}.
1665 */
1666 void setDisablePreviewScreenshots(boolean disable) {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001667 mDisablePreviewScreenshots = disable;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001668 }
1669
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001670 /**
chaviwd3bf08d2017-08-01 17:24:59 -07001671 * Sets whether the current launch can turn the screen on. See {@link #canTurnScreenOn()}
1672 */
1673 void setCanTurnScreenOn(boolean canTurnScreenOn) {
1674 mCanTurnScreenOn = canTurnScreenOn;
1675 }
1676
1677 /**
1678 * Indicates whether the current launch can turn the screen on. This is to prevent multiple
1679 * relayouts from turning the screen back on. The screen should only turn on at most
1680 * once per activity resume.
1681 *
1682 * @return true if the screen can be turned on.
1683 */
1684 boolean canTurnScreenOn() {
1685 return mCanTurnScreenOn;
1686 }
1687
1688 /**
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001689 * Retrieves whether we'd like to generate a snapshot that's based solely on the theme. This is
1690 * the case when preview screenshots are disabled {@link #setDisablePreviewScreenshots} or when
1691 * we can't take a snapshot for other reasons, for example, if we have a secure window.
1692 *
1693 * @return True if we need to generate an app theme snapshot, false if we'd like to take a real
1694 * screenshot.
1695 */
1696 boolean shouldUseAppThemeSnapshot() {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001697 return mDisablePreviewScreenshots || forAllWindows(w -> (w.mAttrs.flags & FLAG_SECURE) != 0,
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001698 true /* topToBottom */);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001699 }
1700
Wale Ogunwale51362492016-09-08 17:49:17 -07001701 @Override
Robert Carrdee1b3f2017-02-27 11:33:33 -08001702 int getAnimLayerAdjustment() {
1703 return mAppAnimator.animLayerAdjustment;
1704 }
1705
1706 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001707 void dump(PrintWriter pw, String prefix) {
1708 super.dump(pw, prefix);
1709 if (appToken != null) {
Wale Ogunwale72919d22016-12-08 18:58:50 -08001710 pw.println(prefix + "app=true mVoiceInteraction=" + mVoiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001711 }
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001712 pw.print(prefix); pw.print("task="); pw.println(getTask());
Wale Ogunwale51362492016-09-08 17:49:17 -07001713 pw.print(prefix); pw.print(" mFillsParent="); pw.print(mFillsParent);
1714 pw.print(" mOrientation="); pw.println(mOrientation);
Wale Ogunwale89973222017-04-23 18:39:45 -07001715 pw.println(prefix + "hiddenRequested=" + hiddenRequested + " mClientHidden=" + mClientHidden
1716 + ((mDeferHidingClient) ? " mDeferHidingClient=" + mDeferHidingClient : "")
1717 + " reportedDrawn=" + reportedDrawn + " reportedVisible=" + reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -07001718 if (paused) {
1719 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001720 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -08001721 if (mAppStopped) {
1722 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
1723 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001724 if (mNumInterestingWindows != 0 || mNumDrawnWindows != 0
Craig Mautner6fbda632012-07-03 09:26:39 -07001725 || allDrawn || mAppAnimator.allDrawn) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001726 pw.print(prefix); pw.print("mNumInterestingWindows=");
1727 pw.print(mNumInterestingWindows);
1728 pw.print(" mNumDrawnWindows="); pw.print(mNumDrawnWindows);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001729 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -07001730 pw.print(" allDrawn="); pw.print(allDrawn);
1731 pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
1732 pw.println(")");
1733 }
1734 if (inPendingTransaction) {
1735 pw.print(prefix); pw.print("inPendingTransaction=");
1736 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001737 }
Craig Mautner799bc1d2015-01-14 10:33:48 -08001738 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001739 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
1740 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -08001741 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -08001742 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001743 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001744 if (startingWindow != null || startingSurface != null
Wale Ogunwale6c459212017-05-17 08:56:03 -07001745 || startingDisplayed || startingMoved || mHiddenSetFromTransferredStartingWindow) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001746 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001747 pw.print(" startingSurface="); pw.print(startingSurface);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001748 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale6c459212017-05-17 08:56:03 -07001749 pw.print(" startingMoved="); pw.print(startingMoved);
1750 pw.println(" mHiddenSetFromTransferredStartingWindow="
1751 + mHiddenSetFromTransferredStartingWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001752 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001753 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -08001754 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001755 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -08001756 }
1757 if (mPendingRelaunchCount != 0) {
1758 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +01001759 }
Wale Ogunwale9c64cb62017-04-12 13:39:59 -07001760 if (getController() != null) {
1761 pw.print(prefix); pw.print("controller="); pw.println(getController());
1762 }
Wale Ogunwalee287e192017-04-21 09:30:12 -07001763 if (mRemovingFromDisplay) {
1764 pw.println(prefix + "mRemovingFromDisplay=" + mRemovingFromDisplay);
1765 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001766 }
1767
1768 @Override
Steven Timotiusaf03df62017-07-18 16:56:43 -07001769 void writeToProto(ProtoOutputStream proto, long fieldId) {
1770 final long token = proto.start(fieldId);
1771 writeNameToProto(proto, NAME);
1772 super.writeToProto(proto, WINDOW_TOKEN);
1773 proto.end(token);
1774 }
1775
1776 void writeNameToProto(ProtoOutputStream proto, long fieldId) {
1777 if (appToken == null) {
1778 return;
1779 }
1780 try {
1781 proto.write(fieldId, appToken.getName());
1782 } catch (RemoteException e) {
1783 // This shouldn't happen, but in this case fall back to outputting nothing
1784 Slog.e(TAG, e.toString());
1785 }
1786 }
1787
1788 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001789 public String toString() {
1790 if (stringName == null) {
1791 StringBuilder sb = new StringBuilder();
1792 sb.append("AppWindowToken{");
1793 sb.append(Integer.toHexString(System.identityHashCode(this)));
1794 sb.append(" token="); sb.append(token); sb.append('}');
1795 stringName = sb.toString();
1796 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001797 return stringName + ((mIsExiting) ? " mIsExiting=" : "");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001798 }
Jeff Browne9bdb312012-04-05 15:30:10 -07001799}