blob: 0a7d1c1d34609f2d73c20b067f1cf0c9efb2df5a [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;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080053
Filip Gruszczynskia590c992015-11-25 16:45:26 -080054import android.annotation.NonNull;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020055import android.app.Activity;
Jorim Jaggi26c8c422016-05-09 19:57:25 -070056import android.content.res.Configuration;
Jorim Jaggi0429f352015-12-22 16:29:16 +010057import android.graphics.Rect;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070058import android.os.Binder;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080059import android.os.Debug;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070060import android.os.IBinder;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070061import android.os.SystemClock;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080062import android.util.Slog;
63import android.view.IApplicationToken;
Robert Carr6914f082017-03-20 19:04:30 -070064import android.view.SurfaceControl;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080065import android.view.WindowManager;
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +020066import android.view.WindowManager.LayoutParams;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080067import android.view.WindowManagerPolicy.StartingSurface;
68
69import com.android.internal.util.ToBooleanFunction;
70import com.android.server.input.InputApplicationHandle;
71import com.android.server.wm.WindowManagerService.H;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080072
73import java.io.PrintWriter;
Jorim Jaggi0429f352015-12-22 16:29:16 +010074import java.util.ArrayDeque;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080075import java.util.ArrayList;
76
Bryce Lee39791592017-04-26 09:29:12 -070077import static android.os.Build.VERSION_CODES.O;
78
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080079class AppTokenList extends ArrayList<AppWindowToken> {
80}
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080081
82/**
83 * Version of WindowToken that is specifically for a particular application (or
84 * really activity) that is displaying windows.
85 */
Wale Ogunwale3f4433d2016-08-18 20:42:42 -070086class AppWindowToken extends WindowToken implements WindowManagerService.AppFreezeListener {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080087 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowToken" : TAG_WM;
88
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080089 // Non-null only for application tokens.
90 final IApplicationToken appToken;
91
Filip Gruszczynskia590c992015-11-25 16:45:26 -080092 @NonNull final AppWindowAnimator mAppAnimator;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070093
Wale Ogunwale72919d22016-12-08 18:58:50 -080094 final boolean mVoiceInteraction;
Dianne Hackborne30e02f2014-05-27 18:24:45 -070095
Wale Ogunwale51362492016-09-08 17:49:17 -070096 /** @see WindowContainer#fillsParent() */
97 private boolean mFillsParent;
Craig Mautner4c5eb222013-11-18 12:59:05 -080098 boolean layoutConfigChanges;
Wale Ogunwale72919d22016-12-08 18:58:50 -080099 boolean mShowForAllUsers;
100 int mTargetSdk;
Craig Mautnera2c77052012-03-26 12:14:43 -0700101
Bryce Lee6d410262017-02-28 15:30:17 -0800102 // Flag set while reparenting to prevent actions normally triggered by an individual parent
103 // change.
104 private boolean mReparenting;
105
Wale Ogunwalee287e192017-04-21 09:30:12 -0700106 // True if we are current in the process of removing this app token from the display
107 private boolean mRemovingFromDisplay = false;
108
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800109 // The input dispatching timeout for this application token in nanoseconds.
Wale Ogunwale72919d22016-12-08 18:58:50 -0800110 long mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800111
112 // These are used for determining when all windows associated with
113 // an activity have been drawn, so they can be made visible together
114 // at the same time.
Craig Mautner764983d2012-03-22 11:37:36 -0700115 // initialize so that it doesn't match mTransactionSequence which is an int.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700116 private long mLastTransactionSequence = Long.MIN_VALUE;
117 private int mNumInterestingWindows;
118 private int mNumDrawnWindows;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800119 boolean inPendingTransaction;
120 boolean allDrawn;
Craig Mautner7636dfb2012-11-16 15:24:11 -0800121 // Set to true when this app creates a surface while in the middle of an animation. In that
122 // case do not clear allDrawn until the animation completes.
123 boolean deferClearAllDrawn;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800124
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700125 /**
126 * These are to track the app's real drawing status if there were no saved surfaces.
127 * @see #updateDrawnWindowStates
128 */
Chong Zhang8e4bda92016-05-04 15:08:18 -0700129 boolean allDrawnExcludingSaved;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700130 private int mNumInterestingWindowsExcludingSaved;
131 private int mNumDrawnWindowsExcludingSaved;
Chong Zhang8e4bda92016-05-04 15:08:18 -0700132
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800133 // Is this window's surface needed? This is almost like hidden, except
134 // it will sometimes be true a little earlier: when the token has
135 // been shown, but is still waiting for its app transition to execute
136 // before making its windows shown.
137 boolean hiddenRequested;
138
139 // Have we told the window clients to hide themselves?
Wale Ogunwale89973222017-04-23 18:39:45 -0700140 private boolean mClientHidden;
141
142 // If true we will defer setting mClientHidden to true and reporting to the client that it is
143 // hidden.
144 boolean mDeferHidingClient;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800145
146 // Last visibility state we reported to the app token.
147 boolean reportedVisible;
148
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700149 // Last drawn state we reported to the app token.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700150 private boolean reportedDrawn;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700151
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800152 // Set to true when the token has been removed from the window mgr.
153 boolean removed;
154
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800155 // Information about an application starting window if displayed.
156 StartingData startingData;
157 WindowState startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800158 StartingSurface startingSurface;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800159 boolean startingDisplayed;
160 boolean startingMoved;
Wale Ogunwale6c459212017-05-17 08:56:03 -0700161 // True if the hidden state of this token was forced to false due to a transferred starting
162 // window.
163 private boolean mHiddenSetFromTransferredStartingWindow;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800164 boolean firstWindowDrawn;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700165 private final WindowState.UpdateReportedVisibilityResults mReportedVisibilityResults =
166 new WindowState.UpdateReportedVisibilityResults();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800167
168 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700169 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800170
Wale Ogunwale571771c2016-08-26 13:18:50 -0700171 // TODO: Have a WindowContainer state for tracking exiting/deferred removal.
Craig Mautner799bc1d2015-01-14 10:33:48 -0800172 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800173
Craig Mautnerbb742462014-07-07 15:28:55 -0700174 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700175 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700176
Wale Ogunwale72919d22016-12-08 18:58:50 -0800177 private boolean mAlwaysFocusable;
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800178
Robert Carre12aece2016-02-02 22:43:27 -0800179 boolean mAppStopped;
Robert Carrfd10cd12016-06-29 16:41:50 -0700180 int mRotationAnimationHint;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700181 private int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800182
Jorim Jaggife762342016-10-13 14:33:27 +0200183 private boolean mLastContainsShowWhenLockedWindow;
184 private boolean mLastContainsDismissKeyguardWindow;
185
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700186 // The bounds of this activity. Mainly used for aspect-ratio compatibility.
187 // TODO(b/36505427): Every level on WindowContainer now has bounds information, which directly
188 // affects the configuration. We should probably move this into that class.
189 private final Rect mBounds = new Rect();
190
Jorim Jaggi0429f352015-12-22 16:29:16 +0100191 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700192 ArrayDeque<Configuration> mFrozenMergedConfig = new ArrayDeque<>();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100193
Wale Ogunwale6c459212017-05-17 08:56:03 -0700194 private boolean mDisablePreviewScreenshots;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100195
Robert Carred3e83b2017-04-21 13:26:55 -0700196 Task mLastParent;
197
chaviwd3bf08d2017-08-01 17:24:59 -0700198 /**
199 * See {@link #canTurnScreenOn()}
200 */
201 private boolean mCanTurnScreenOn = true;
202
Wale Ogunwale72919d22016-12-08 18:58:50 -0800203 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
204 DisplayContent dc, long inputDispatchingTimeoutNanos, boolean fullscreen,
205 boolean showForAllUsers, int targetSdk, int orientation, int rotationAnimationHint,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800206 int configChanges, boolean launchTaskBehind, boolean alwaysFocusable,
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700207 AppWindowContainerController controller, Configuration overrideConfig, Rect bounds) {
208 this(service, token, voiceInteraction, dc, fullscreen, overrideConfig, bounds);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800209 setController(controller);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800210 mInputDispatchingTimeoutNanos = inputDispatchingTimeoutNanos;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800211 mShowForAllUsers = showForAllUsers;
212 mTargetSdk = targetSdk;
213 mOrientation = orientation;
214 layoutConfigChanges = (configChanges & (CONFIG_SCREEN_SIZE | CONFIG_ORIENTATION)) != 0;
215 mLaunchTaskBehind = launchTaskBehind;
216 mAlwaysFocusable = alwaysFocusable;
217 mRotationAnimationHint = rotationAnimationHint;
218
219 // Application tokens start out hidden.
220 hidden = true;
221 hiddenRequested = true;
222 }
223
224 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700225 DisplayContent dc, boolean fillsParent, Configuration overrideConfig, Rect bounds) {
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800226 super(service, token != null ? token.asBinder() : null, TYPE_APPLICATION, true, dc,
227 false /* ownerCanManageAppTokens */);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700228 appToken = token;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800229 mVoiceInteraction = voiceInteraction;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800230 mFillsParent = fillsParent;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800231 mInputApplicationHandle = new InputApplicationHandle(this);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700232 mAppAnimator = new AppWindowAnimator(this, service);
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -0700233 if (overrideConfig != null) {
234 onOverrideConfigurationChanged(overrideConfig);
235 }
236 if (bounds != null) {
237 mBounds.set(bounds);
238 }
239 }
240
241 void onOverrideConfigurationChanged(Configuration overrideConfiguration, Rect bounds) {
242 onOverrideConfigurationChanged(overrideConfiguration);
243 if (mBounds.equals(bounds)) {
244 return;
245 }
246 // TODO(b/36505427): If bounds is in WC, then we can automatically call onResize() when set.
247 mBounds.set(bounds);
248 onResize();
249 }
250
251 void getBounds(Rect outBounds) {
252 outBounds.set(mBounds);
253 }
254
255 boolean hasBounds() {
256 return !mBounds.isEmpty();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800257 }
258
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800259 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
260 firstWindowDrawn = true;
261
262 // We now have a good window to show, remove dead placeholders
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700263 removeDeadWindows();
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800264
Jorim Jaggi02886a82016-12-06 09:10:06 -0800265 if (startingWindow != null) {
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800266 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
267 + win.mToken + ": first real window is shown, no animation");
268 // If this initial window is animating, stop it -- we will do an animation to reveal
269 // it from behind the starting window, so there is no need for it to also be doing its
270 // own stuff.
271 winAnimator.clearAnimation();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800272 if (getController() != null) {
273 getController().removeStartingWindow();
274 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800275 }
276 updateReportedVisibilityLocked();
277 }
278
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800279 void updateReportedVisibilityLocked() {
280 if (appToken == null) {
281 return;
282 }
283
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700284 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700285 final int count = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800286
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700287 mReportedVisibilityResults.reset();
288
289 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700290 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700291 win.updateReportedVisibility(mReportedVisibilityResults);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800292 }
293
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700294 int numInteresting = mReportedVisibilityResults.numInteresting;
295 int numVisible = mReportedVisibilityResults.numVisible;
296 int numDrawn = mReportedVisibilityResults.numDrawn;
297 boolean nowGone = mReportedVisibilityResults.nowGone;
298
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700299 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Jorim Jaggi6dfd9ba2017-08-03 15:00:01 +0200300 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting && !hidden;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700301 if (!nowGone) {
302 // If the app is not yet gone, then it can only become visible/drawn.
303 if (!nowDrawn) {
304 nowDrawn = reportedDrawn;
305 }
306 if (!nowVisible) {
307 nowVisible = reportedVisible;
308 }
309 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800310 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800311 + numInteresting + " visible=" + numVisible);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800312 final AppWindowContainerController controller = getController();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700313 if (nowDrawn != reportedDrawn) {
314 if (nowDrawn) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800315 if (controller != null) {
316 controller.reportWindowsDrawn();
317 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700318 }
319 reportedDrawn = nowDrawn;
320 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800321 if (nowVisible != reportedVisible) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700322 if (DEBUG_VISIBILITY) Slog.v(TAG,
323 "Visibility changed in " + this + ": vis=" + nowVisible);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800324 reportedVisible = nowVisible;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800325 if (controller != null) {
326 if (nowVisible) {
327 controller.reportWindowsVisible();
328 } else {
329 controller.reportWindowsGone();
330 }
331 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800332 }
333 }
334
Wale Ogunwale89973222017-04-23 18:39:45 -0700335 boolean isClientHidden() {
336 return mClientHidden;
337 }
338
339 void setClientHidden(boolean hideClient) {
340 if (mClientHidden == hideClient || (hideClient && mDeferHidingClient)) {
341 return;
342 }
343 mClientHidden = hideClient;
344 sendAppVisibilityToClients();
345 }
346
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700347 boolean setVisibility(WindowManager.LayoutParams lp,
348 boolean visible, int transit, boolean performLayout, boolean isVoiceInteraction) {
349
350 boolean delayed = false;
351 inPendingTransaction = false;
Wale Ogunwale9e4721f2017-05-23 19:37:30 -0700352 // Reset the state of mHiddenSetFromTransferredStartingWindow since visibility is actually
353 // been set by the app now.
354 mHiddenSetFromTransferredStartingWindow = false;
Wale Ogunwale89973222017-04-23 18:39:45 -0700355 setClientHidden(!visible);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700356
357 // Allow for state changes and animation to be applied if:
358 // * token is transitioning visibility state
359 // * or the token was marked as hidden and is exiting before we had a chance to play the
360 // transition animation
361 // * or this is an opening app and windows are being replaced.
362 boolean visibilityChanged = false;
363 if (hidden == visible || (hidden && mIsExiting) || (visible && waitingForReplacement())) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700364 final AccessibilityController accessibilityController = mService.mAccessibilityController;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700365 boolean changed = false;
366 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
367 "Changing app " + this + " hidden=" + hidden + " performLayout=" + performLayout);
368
369 boolean runningAppAnimation = false;
370
Jorim Jaggi4d1835d2017-08-31 17:28:27 +0200371 if (mAppAnimator.animation == AppWindowAnimator.sDummyAnimation) {
372 mAppAnimator.setNullAnimation();
373 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700374 if (transit != AppTransition.TRANSIT_UNSET) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700375 if (mService.applyAnimationLocked(this, lp, transit, visible, isVoiceInteraction)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700376 delayed = runningAppAnimation = true;
377 }
378 final WindowState window = findMainWindow();
379 //TODO (multidisplay): Magnification is supported only for the default display.
380 if (window != null && accessibilityController != null
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700381 && getDisplayContent().getDisplayId() == DEFAULT_DISPLAY) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700382 accessibilityController.onAppWindowTransitionLocked(window, transit);
383 }
384 changed = true;
385 }
386
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700387 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700388 for (int i = 0; i < windowsCount; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700389 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700390 changed |= win.onAppVisibilityChanged(visible, runningAppAnimation);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700391 }
392
393 hidden = hiddenRequested = !visible;
394 visibilityChanged = true;
395 if (!visible) {
396 stopFreezingScreen(true, true);
397 } else {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700398 // If we are being set visible, and the starting window is not yet displayed,
399 // then make sure it doesn't get displayed.
400 if (startingWindow != null && !startingWindow.isDrawnLw()) {
401 startingWindow.mPolicyVisibility = false;
402 startingWindow.mPolicyVisibilityAfterAnim = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700403 }
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700404
405 // We are becoming visible, so better freeze the screen with the windows that are
406 // getting visible so we also wait for them.
407 forAllWindows(mService::makeWindowFreezingScreenIfNeededLocked, true);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700408 }
409
410 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "setVisibility: " + this
411 + ": hidden=" + hidden + " hiddenRequested=" + hiddenRequested);
412
413 if (changed) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700414 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700415 if (performLayout) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700416 mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700417 false /*updateInputWindows*/);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700418 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700419 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700420 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700421 }
422 }
423
424 if (mAppAnimator.animation != null) {
425 delayed = true;
426 }
427
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700428 for (int i = mChildren.size() - 1; i >= 0 && !delayed; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700429 if ((mChildren.get(i)).isWindowAnimationSet()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700430 delayed = true;
431 }
432 }
433
434 if (visibilityChanged) {
435 if (visible && !delayed) {
436 // The token was made immediately visible, there will be no entrance animation.
437 // We need to inform the client the enter animation was finished.
438 mEnteringAnimation = true;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700439 mService.mActivityManagerAppTransitionNotifier.onAppTransitionFinishedLocked(token);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700440 }
Robert Carr61b81112017-07-17 18:08:15 -0700441
Robert Carre7cc44d2017-03-20 19:04:30 -0700442 // If we are hidden but there is no delay needed we immediately
443 // apply the Surface transaction so that the ActivityManager
Robert Carr61b81112017-07-17 18:08:15 -0700444 // can have some guarantee on the Surface state following
445 // setting the visibility. This captures cases like dismissing
446 // the docked or pinned stack where there is no app transition.
447 //
448 // In the case of a "Null" animation, there will be
449 // no animation but there will still be a transition set.
450 // We still need to delay hiding the surface such that it
451 // can be synchronized with showing the next surface in the transition.
452 if (hidden && !delayed && !mService.mAppTransition.isTransitionSet()) {
Robert Carr6914f082017-03-20 19:04:30 -0700453 SurfaceControl.openTransaction();
454 for (int i = mChildren.size() - 1; i >= 0; i--) {
455 mChildren.get(i).mWinAnimator.hide("immediately hidden");
456 }
457 SurfaceControl.closeTransaction();
458 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700459
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700460 if (!mService.mClosingApps.contains(this) && !mService.mOpeningApps.contains(this)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700461 // The token is not closing nor opening, so even if there is an animation set, that
462 // doesn't mean that it goes through the normal app transition cycle so we have
463 // to inform the docked controller about visibility change.
Andrii Kulian1e32e022016-09-16 15:29:34 -0700464 // TODO(multi-display): notify docked divider on all displays where visibility was
465 // affected.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700466 mService.getDefaultDisplayContentLocked().getDockedDividerController()
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700467 .notifyAppVisibilityChanged();
Jorim Jaggi8b702ed2017-01-20 16:59:03 +0100468 mService.mTaskSnapshotController.notifyAppVisibilityChanged(this, visible);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700469 }
470 }
471
472 return delayed;
473 }
474
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +0200475 /**
476 * @return The to top most child window for which {@link LayoutParams#isFullscreen()} returns
477 * true.
478 */
479 WindowState getTopFullscreenWindow() {
480 for (int i = mChildren.size() - 1; i >= 0; i--) {
481 final WindowState win = mChildren.get(i);
482 if (win != null && win.mAttrs.isFullscreen()) {
483 return win;
484 }
485 }
486 return null;
487 }
488
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800489 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700490 WindowState candidate = null;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700491 int j = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800492 while (j > 0) {
493 j--;
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700494 final WindowState win = mChildren.get(j);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700495 final int type = win.mAttrs.type;
496 // No need to loop through child window as base application and starting types can't be
497 // child windows.
498 if (type == TYPE_BASE_APPLICATION || type == TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700499 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900500 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700501 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800502 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700503 candidate = win;
504 } else {
505 return win;
506 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800507 }
508 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700509 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800510 }
511
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800512 boolean windowsAreFocusable() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800513 return StackId.canReceiveKeys(getTask().mStack.mStackId) || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800514 }
515
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800516 AppWindowContainerController getController() {
517 final WindowContainerController controller = super.getController();
518 return controller != null ? (AppWindowContainerController) controller : null;
519 }
520
Wale Ogunwale571771c2016-08-26 13:18:50 -0700521 @Override
Wale Ogunwale44f21802016-09-02 12:49:48 -0700522 boolean isVisible() {
Wale Ogunwalee471be62016-10-03 07:53:55 -0700523 // If the app token isn't hidden then it is considered visible and there is no need to check
524 // its children windows to see if they are visible.
525 return !hidden;
Wale Ogunwale44f21802016-09-02 12:49:48 -0700526 }
527
528 @Override
Wale Ogunwalee287e192017-04-21 09:30:12 -0700529 void removeImmediately() {
530 onRemovedFromDisplay();
531 super.removeImmediately();
532 }
533
534 @Override
Wale Ogunwale571771c2016-08-26 13:18:50 -0700535 void removeIfPossible() {
Craig Mautnere3119b72015-01-20 15:02:36 -0800536 mIsExiting = false;
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800537 removeAllWindowsIfPossible();
Bryce Lee6d410262017-02-28 15:30:17 -0800538 removeImmediately();
Craig Mautnere3119b72015-01-20 15:02:36 -0800539 }
540
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700541 @Override
542 boolean checkCompleteDeferredRemoval() {
543 if (mIsExiting) {
544 removeIfPossible();
545 }
546 return super.checkCompleteDeferredRemoval();
547 }
548
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700549 void onRemovedFromDisplay() {
Wale Ogunwalee287e192017-04-21 09:30:12 -0700550 if (mRemovingFromDisplay) {
551 return;
552 }
553 mRemovingFromDisplay = true;
554
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700555 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app token: " + this);
556
Wale Ogunwale72919d22016-12-08 18:58:50 -0800557 boolean delayed = setVisibility(null, false, TRANSIT_UNSET, true, mVoiceInteraction);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700558
559 mService.mOpeningApps.remove(this);
Jorim Jaggi45ca0542017-05-30 16:16:01 -0700560 mService.mUnknownAppVisibilityController.appRemovedOrHidden(this);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100561 mService.mTaskSnapshotController.onAppRemoved(this);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700562 waitingToShow = false;
563 if (mService.mClosingApps.contains(this)) {
564 delayed = true;
565 } else if (mService.mAppTransition.isTransitionSet()) {
566 mService.mClosingApps.add(this);
567 delayed = true;
568 }
569
570 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app " + this + " delayed=" + delayed
571 + " animation=" + mAppAnimator.animation + " animating=" + mAppAnimator.animating);
572
573 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM, "removeAppToken: "
574 + this + " delayed=" + delayed + " Callers=" + Debug.getCallers(4));
575
Jorim Jaggi19be6052017-08-03 18:33:43 +0200576 if (startingData != null && getController() != null) {
577 getController().removeStartingWindow();
578 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800579
Winson Chung87e5d552017-04-05 11:49:38 -0700580 // If this window was animating, then we need to ensure that the app transition notifies
581 // that animations have completed in WMS.handleAnimatingStoppedAndTransitionLocked(), so
582 // add to that list now
583 if (mAppAnimator.animating) {
584 mService.mNoAnimationNotifyOnTransitionFinished.add(token);
585 }
586
Wale Ogunwalee287e192017-04-21 09:30:12 -0700587 final TaskStack stack = getStack();
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700588 if (delayed && !isEmpty()) {
589 // set the token aside because it has an active animation to be finished
590 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM,
591 "removeAppToken make exiting: " + this);
Wale Ogunwalee287e192017-04-21 09:30:12 -0700592 if (stack != null) {
593 stack.mExitingAppTokens.add(this);
594 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700595 mIsExiting = true;
596 } else {
597 // Make sure there is no animation running on this token, so any windows associated
598 // with it will be removed as soon as their animations are complete
599 mAppAnimator.clearAnimation();
600 mAppAnimator.animating = false;
Wale Ogunwale3106ae012017-05-16 08:56:37 -0700601 if (stack != null) {
602 stack.mExitingAppTokens.remove(this);
603 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700604 removeIfPossible();
605 }
606
607 removed = true;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700608 stopFreezingScreen(true, true);
Bryce Lee6d410262017-02-28 15:30:17 -0800609
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700610 if (mService.mFocusedApp == this) {
611 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "Removing focused app token:" + this);
612 mService.mFocusedApp = null;
613 mService.updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL, true /*updateInputWindows*/);
614 mService.mInputMonitor.setFocusedAppLw(null);
615 }
616
617 if (!delayed) {
618 updateReportedVisibilityLocked();
619 }
Wale Ogunwalee287e192017-04-21 09:30:12 -0700620
621 mRemovingFromDisplay = false;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700622 }
623
Chong Zhange05bcb12016-07-26 17:47:29 -0700624 void clearAnimatingFlags() {
Chong Zhangb0d26702016-08-12 16:03:29 -0700625 boolean wallpaperMightChange = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700626 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700627 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700628 wallpaperMightChange |= win.clearAnimatingFlags();
Chong Zhange05bcb12016-07-26 17:47:29 -0700629 }
Chong Zhangb0d26702016-08-12 16:03:29 -0700630 if (wallpaperMightChange) {
631 requestUpdateWallpaperIfNeeded();
632 }
Chong Zhange05bcb12016-07-26 17:47:29 -0700633 }
634
Robert Carre12aece2016-02-02 22:43:27 -0800635 void destroySurfaces() {
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700636 destroySurfaces(false /*cleanupOnResume*/);
637 }
638
639 /**
640 * Destroy surfaces which have been marked as eligible by the animator, taking care to ensure
641 * the client has finished with them.
642 *
643 * @param cleanupOnResume whether this is done when app is resumed without fully stopped. If
644 * set to true, destroy only surfaces of removed windows, and clear relevant flags of the
645 * others so that they are ready to be reused. If set to false (common case), destroy all
646 * surfaces that's eligible, if the app is already stopped.
647 */
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700648 private void destroySurfaces(boolean cleanupOnResume) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700649 boolean destroyedSomething = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700650 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700651 final WindowState win = mChildren.get(i);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700652 destroyedSomething |= win.destroySurface(cleanupOnResume, mAppStopped);
Robert Carre12aece2016-02-02 22:43:27 -0800653 }
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700654 if (destroyedSomething) {
655 final DisplayContent dc = getDisplayContent();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700656 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Robert Carre12aece2016-02-02 22:43:27 -0800657 }
658 }
659
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800660 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700661 * Notify that the app is now resumed, and it was not stopped before, perform a clean
662 * up of the surfaces
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800663 */
Jorim Jaggibae01b12017-04-11 16:29:10 -0700664 void notifyAppResumed(boolean wasStopped) {
Chong Zhangad24f962016-08-25 12:12:33 -0700665 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppResumed: wasStopped=" + wasStopped
Jorim Jaggibae01b12017-04-11 16:29:10 -0700666 + " " + this);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700667 mAppStopped = false;
chaviwd3bf08d2017-08-01 17:24:59 -0700668 // Allow the window to turn the screen on once the app is resumed again.
669 setCanTurnScreenOn(true);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700670 if (!wasStopped) {
671 destroySurfaces(true /*cleanupOnResume*/);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800672 }
Robert Carre12aece2016-02-02 22:43:27 -0800673 }
674
Chong Zhangbef461f2015-10-27 11:38:24 -0700675 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700676 * Notify that the app has stopped, and it is okay to destroy any surfaces which were
677 * keeping alive in case they were still being used.
678 */
679 void notifyAppStopped() {
680 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: " + this);
681 mAppStopped = true;
682 destroySurfaces();
683 // Remove any starting window that was added for this app if they are still around.
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800684 if (getController() != null) {
685 getController().removeStartingWindow();
686 }
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700687 }
688
689 /**
Chong Zhangbef461f2015-10-27 11:38:24 -0700690 * Checks whether we should save surfaces for this app.
691 *
692 * @return true if the surfaces should be saved, false otherwise.
693 */
694 boolean shouldSaveSurface() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800695 // We want to save surface if the app's windows are "allDrawn".
696 // (If we started entering animation early with saved surfaces, allDrawn
697 // should have been restored to true. So we'll save again in that case
698 // even if app didn't actually finish drawing.)
699 return allDrawn;
Robert Carr13f7be9e2015-12-02 18:39:45 -0800700 }
701
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700702 private boolean canRestoreSurfaces() {
703 for (int i = mChildren.size() -1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700704 final WindowState w = mChildren.get(i);
Chong Zhang92147042016-05-09 12:47:11 -0700705 if (w.canRestoreSurface()) {
Robert Carr13f7be9e2015-12-02 18:39:45 -0800706 return true;
707 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700708 }
709 return false;
710 }
711
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700712 private void clearWasVisibleBeforeClientHidden() {
713 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700714 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700715 w.clearWasVisibleBeforeClientHidden();
Chong Zhang92147042016-05-09 12:47:11 -0700716 }
717 }
718
Chong Zhang8e4bda92016-05-04 15:08:18 -0700719 /**
720 * Whether the app has some window that is invisible in layout, but
721 * animating with saved surface.
722 */
723 boolean isAnimatingInvisibleWithSavedSurface() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700724 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700725 final WindowState w = mChildren.get(i);
Chong Zhang8e4bda92016-05-04 15:08:18 -0700726 if (w.isAnimatingInvisibleWithSavedSurface()) {
727 return true;
728 }
729 }
730 return false;
731 }
732
733 /**
734 * Hide all window surfaces that's still invisible in layout but animating
735 * with a saved surface, and mark them destroying.
736 */
737 void stopUsingSavedSurfaceLocked() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700738 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700739 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700740 w.stopUsingSavedSurface();
Chong Zhang8e4bda92016-05-04 15:08:18 -0700741 }
742 destroySurfaces();
743 }
744
Chong Zhangf58631a2016-05-24 16:02:10 -0700745 void markSavedSurfaceExiting() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700746 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700747 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700748 w.markSavedSurfaceExiting();
Chong Zhangf58631a2016-05-24 16:02:10 -0700749 }
750 }
751
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700752 void restoreSavedSurfaceForInterestingWindows() {
Chong Zhang92147042016-05-09 12:47:11 -0700753 if (!canRestoreSurfaces()) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700754 clearWasVisibleBeforeClientHidden();
Chong Zhangbef461f2015-10-27 11:38:24 -0700755 return;
756 }
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700757
758 // Check if all interesting windows are drawn and we can mark allDrawn=true.
759 int interestingNotDrawn = -1;
760
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700761 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700762 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700763 interestingNotDrawn = w.restoreSavedSurfaceForInterestingWindow();
Chong Zhangbef461f2015-10-27 11:38:24 -0700764 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800765
Chong Zhang92147042016-05-09 12:47:11 -0700766 if (!allDrawn) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700767 allDrawn = (interestingNotDrawn == 0);
Chong Zhang92147042016-05-09 12:47:11 -0700768 if (allDrawn) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700769 mService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
Chong Zhang92147042016-05-09 12:47:11 -0700770 }
771 }
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700772 clearWasVisibleBeforeClientHidden();
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800773
774 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700775 "restoreSavedSurfaceForInterestingWindows: " + this + " allDrawn=" + allDrawn
776 + " interestingNotDrawn=" + interestingNotDrawn);
Chong Zhangbef461f2015-10-27 11:38:24 -0700777 }
778
779 void destroySavedSurfaces() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700780 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700781 final WindowState win = mChildren.get(i);
Robert Carr13f7be9e2015-12-02 18:39:45 -0800782 win.destroySavedSurface();
Chong Zhangbef461f2015-10-27 11:38:24 -0700783 }
Chong Zhang92147042016-05-09 12:47:11 -0700784 }
785
786 void clearAllDrawn() {
787 allDrawn = false;
788 deferClearAllDrawn = false;
Chong Zhang8e4bda92016-05-04 15:08:18 -0700789 allDrawnExcludingSaved = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700790 }
791
Bryce Lee6d410262017-02-28 15:30:17 -0800792 Task getTask() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800793 return (Task) getParent();
Bryce Lee6d410262017-02-28 15:30:17 -0800794 }
795
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800796 TaskStack getStack() {
797 final Task task = getTask();
798 if (task != null) {
799 return task.mStack;
800 } else {
801 return null;
802 }
803 }
804
Bryce Lee6d410262017-02-28 15:30:17 -0800805 @Override
806 void onParentSet() {
807 super.onParentSet();
808
Robert Carred3e83b2017-04-21 13:26:55 -0700809 final Task task = getTask();
810
Bryce Lee6d410262017-02-28 15:30:17 -0800811 // When the associated task is {@code null}, the {@link AppWindowToken} can no longer
812 // access visual elements like the {@link DisplayContent}. We must remove any associations
813 // such as animations.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800814 if (!mReparenting) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800815 if (task == null) {
816 // It is possible we have been marked as a closing app earlier. We must remove ourselves
817 // from this list so we do not participate in any future animations.
818 mService.mClosingApps.remove(this);
Robert Carred3e83b2017-04-21 13:26:55 -0700819 } else if (mLastParent != null && mLastParent.mStack != null) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800820 task.mStack.mExitingAppTokens.remove(this);
821 }
Bryce Lee6d410262017-02-28 15:30:17 -0800822 }
Robert Carred3e83b2017-04-21 13:26:55 -0700823 mLastParent = task;
Bryce Lee6d410262017-02-28 15:30:17 -0800824 }
825
Wale Ogunwalefa854eb2016-09-20 13:43:52 -0700826 void postWindowRemoveStartingWindowCleanup(WindowState win) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700827 // TODO: Something smells about the code below...Is there a better way?
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700828 if (startingWindow == win) {
829 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Notify removed startingWindow " + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800830 if (getController() != null) {
831 getController().removeStartingWindow();
832 }
Wale Ogunwale6c459212017-05-17 08:56:03 -0700833 } else if (mChildren.size() == 0) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700834 // If this is the last window and we had requested a starting transition window,
835 // well there is no point now.
Jorim Jaggi02886a82016-12-06 09:10:06 -0800836 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Nulling last startingData");
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700837 startingData = null;
Wale Ogunwale6c459212017-05-17 08:56:03 -0700838 if (mHiddenSetFromTransferredStartingWindow) {
839 // We set the hidden state to false for the token from a transferred starting window.
840 // We now reset it back to true since the starting window was the last window in the
841 // token.
842 hidden = true;
843 }
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100844 } else if (mChildren.size() == 1 && startingSurface != null && !isRelaunching()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700845 // If this is the last window except for a starting transition window,
846 // we need to get rid of the starting transition.
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200847 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Last window, removing starting window "
848 + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800849 if (getController() != null) {
850 getController().removeStartingWindow();
851 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700852 }
853 }
854
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700855 void removeDeadWindows() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700856 for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700857 WindowState win = mChildren.get(winNdx);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800858 if (win.mAppDied) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700859 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.w(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700860 "removeDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800861 // Set mDestroying, we don't want any animation or delayed removal here.
862 win.mDestroying = true;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700863 // Also removes child windows.
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700864 win.removeIfPossible();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800865 }
866 }
867 }
868
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700869 boolean hasWindowsAlive() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700870 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700871 // No need to loop through child windows as the answer should be the same as that of the
872 // parent window.
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700873 if (!(mChildren.get(i)).mAppDied) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700874 return true;
875 }
876 }
877 return false;
878 }
879
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700880 void setWillReplaceWindows(boolean animate) {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700881 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
882 "Marking app token " + this + " with replacing windows.");
Robert Carra1eb4392015-12-10 12:43:51 -0800883
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700884 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700885 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700886 w.setWillReplaceWindow(animate);
Robert Carra1eb4392015-12-10 12:43:51 -0800887 }
888 if (animate) {
889 // Set-up dummy animation so we can start treating windows associated with this
890 // token like they are in transition before the new app window is ready for us to
891 // run the real transition animation.
892 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700893 "setWillReplaceWindow() Setting dummy animation on: " + this);
Robert Carra1eb4392015-12-10 12:43:51 -0800894 mAppAnimator.setDummyAnimation();
895 }
896 }
897
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700898 void setWillReplaceChildWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700899 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + this
Robert Carr23fa16b2016-01-13 13:19:58 -0800900 + " with replacing child windows.");
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700901 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700902 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700903 w.setWillReplaceChildWindows();
Robert Carr23fa16b2016-01-13 13:19:58 -0800904 }
905 }
906
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700907 void clearWillReplaceWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700908 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
909 "Resetting app token " + this + " of replacing window marks.");
Chong Zhangf596cd52016-01-05 13:42:44 -0800910
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700911 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700912 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700913 w.clearWillReplaceWindow();
Chong Zhangf596cd52016-01-05 13:42:44 -0800914 }
915 }
916
Chong Zhang4d7369a2016-04-25 16:09:14 -0700917 void requestUpdateWallpaperIfNeeded() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700918 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700919 final WindowState w = mChildren.get(i);
Chong Zhang4d7369a2016-04-25 16:09:14 -0700920 w.requestUpdateWallpaperIfNeeded();
921 }
922 }
923
Chong Zhangd78ddb42016-03-02 17:01:14 -0800924 boolean isRelaunching() {
925 return mPendingRelaunchCount > 0;
926 }
927
Robert Carr68375192017-06-13 12:41:53 -0700928 boolean shouldFreezeBounds() {
929 final Task task = getTask();
930
931 // For freeform windows, we can't freeze the bounds at the moment because this would make
932 // the resizing unresponsive.
933 if (task == null || task.inFreeformWorkspace()) {
934 return false;
935 }
936
937 // We freeze the bounds while drag resizing to deal with the time between
938 // the divider/drag handle being released, and the handling it's new
939 // configuration. If we are relaunched outside of the drag resizing state,
940 // we need to be careful not to do this.
941 return getTask().isDragResizing();
942 }
943
Chong Zhangd78ddb42016-03-02 17:01:14 -0800944 void startRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700945 if (shouldFreezeBounds()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -0800946 freezeBounds();
947 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800948
949 // In the process of tearing down before relaunching, the app will
950 // try and clean up it's child surfaces. We need to prevent this from
951 // happening, so we sever the children, transfering their ownership
952 // from the client it-self to the parent surface (owned by us).
953 for (int i = mChildren.size() - 1; i >= 0; i--) {
954 final WindowState w = mChildren.get(i);
955 w.mWinAnimator.detachChildren();
956 }
957
Chong Zhangd78ddb42016-03-02 17:01:14 -0800958 mPendingRelaunchCount++;
959 }
960
961 void finishRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700962 unfreezeBounds();
963
Chong Zhangd78ddb42016-03-02 17:01:14 -0800964 if (mPendingRelaunchCount > 0) {
965 mPendingRelaunchCount--;
Bryce Lee081554b2017-05-25 07:52:12 -0700966 } else {
967 // Update keyguard flags upon finishing relaunch.
968 checkKeyguardFlagsChanged();
Chong Zhangd78ddb42016-03-02 17:01:14 -0800969 }
970 }
971
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700972 void clearRelaunching() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700973 if (mPendingRelaunchCount == 0) {
974 return;
975 }
Robert Carr68375192017-06-13 12:41:53 -0700976 unfreezeBounds();
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700977 mPendingRelaunchCount = 0;
978 }
979
Wale Ogunwale07bcab72016-10-14 15:30:09 -0700980 /**
981 * Returns true if the new child window we are adding to this token is considered greater than
982 * the existing child window in this token in terms of z-order.
983 */
984 @Override
985 protected boolean isFirstChildWindowGreaterThanSecond(WindowState newWindow,
986 WindowState existingWindow) {
987 final int type1 = newWindow.mAttrs.type;
988 final int type2 = existingWindow.mAttrs.type;
989
990 // Base application windows should be z-ordered BELOW all other windows in the app token.
991 if (type1 == TYPE_BASE_APPLICATION && type2 != TYPE_BASE_APPLICATION) {
992 return false;
993 } else if (type1 != TYPE_BASE_APPLICATION && type2 == TYPE_BASE_APPLICATION) {
994 return true;
995 }
996
997 // Starting windows should be z-ordered ABOVE all other windows in the app token.
998 if (type1 == TYPE_APPLICATION_STARTING && type2 != TYPE_APPLICATION_STARTING) {
999 return true;
1000 } else if (type1 != TYPE_APPLICATION_STARTING && type2 == TYPE_APPLICATION_STARTING) {
1001 return false;
1002 }
1003
1004 // Otherwise the new window is greater than the existing window.
1005 return true;
1006 }
1007
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001008 @Override
Robert Carra1eb4392015-12-10 12:43:51 -08001009 void addWindow(WindowState w) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001010 super.addWindow(w);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001011
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001012 boolean gotReplacementWindow = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001013 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001014 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001015 gotReplacementWindow |= candidate.setReplacementWindowIfNeeded(w);
1016 }
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001017
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001018 // if we got a replacement window, reset the timeout to give drawing more time
1019 if (gotReplacementWindow) {
1020 mService.scheduleWindowReplacementTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -08001021 }
Jorim Jaggife762342016-10-13 14:33:27 +02001022 checkKeyguardFlagsChanged();
1023 }
1024
1025 @Override
1026 void removeChild(WindowState child) {
1027 super.removeChild(child);
1028 checkKeyguardFlagsChanged();
Robert Carra1eb4392015-12-10 12:43:51 -08001029 }
1030
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001031 private boolean waitingForReplacement() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001032 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001033 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001034 if (candidate.waitingForReplacement()) {
Robert Carra1eb4392015-12-10 12:43:51 -08001035 return true;
1036 }
1037 }
1038 return false;
1039 }
1040
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001041 void onWindowReplacementTimeout() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001042 for (int i = mChildren.size() - 1; i >= 0; --i) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001043 (mChildren.get(i)).onWindowReplacementTimeout();
Robert Carra1eb4392015-12-10 12:43:51 -08001044 }
1045 }
1046
Winson Chung30480042017-01-26 10:55:34 -08001047 void reparent(Task task, int position) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001048 final Task currentTask = getTask();
1049 if (task == currentTask) {
Winson Chung30480042017-01-26 10:55:34 -08001050 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001051 "window token=" + this + " already child of task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -08001052 }
Bryce Lee6d410262017-02-28 15:30:17 -08001053
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001054 if (currentTask.mStack != task.mStack) {
Bryce Lee6d410262017-02-28 15:30:17 -08001055 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001056 "window token=" + this + " current task=" + currentTask
Bryce Lee6d410262017-02-28 15:30:17 -08001057 + " belongs to a different stack than " + task);
1058 }
1059
Winson Chung30480042017-01-26 10:55:34 -08001060 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "reParentWindowToken: removing window token=" + this
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001061 + " from task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -08001062 final DisplayContent prevDisplayContent = getDisplayContent();
1063
Bryce Lee6d410262017-02-28 15:30:17 -08001064 mReparenting = true;
1065
Winson Chung30480042017-01-26 10:55:34 -08001066 getParent().removeChild(this);
1067 task.addChild(this, position);
1068
Bryce Lee6d410262017-02-28 15:30:17 -08001069 mReparenting = false;
1070
Winson Chung30480042017-01-26 10:55:34 -08001071 // Relayout display(s).
1072 final DisplayContent displayContent = task.getDisplayContent();
1073 displayContent.setLayoutNeeded();
1074 if (prevDisplayContent != displayContent) {
1075 onDisplayChanged(displayContent);
1076 prevDisplayContent.setLayoutNeeded();
1077 }
1078 }
1079
Jorim Jaggi0429f352015-12-22 16:29:16 +01001080 /**
1081 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
1082 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
1083 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
1084 * with a queue.
1085 */
Chong Zhangd78ddb42016-03-02 17:01:14 -08001086 private void freezeBounds() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001087 final Task task = getTask();
1088 mFrozenBounds.offer(new Rect(task.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001089
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001090 if (task.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001091 // We didn't call prepareFreezingBounds on the task, so use the current value.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001092 mFrozenMergedConfig.offer(new Configuration(task.getConfiguration()));
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001093 } else {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001094 mFrozenMergedConfig.offer(new Configuration(task.mPreparedFrozenMergedConfig));
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001095 }
Andrii Kulianb10330d2016-09-16 13:51:46 -07001096 // Calling unset() to make it equal to Configuration.EMPTY.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001097 task.mPreparedFrozenMergedConfig.unset();
Jorim Jaggi0429f352015-12-22 16:29:16 +01001098 }
1099
1100 /**
1101 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
1102 */
Chong Zhangd78ddb42016-03-02 17:01:14 -08001103 private void unfreezeBounds() {
Robert Carr68375192017-06-13 12:41:53 -07001104 if (mFrozenBounds.isEmpty()) {
1105 return;
Wale Ogunwale37dbafc2016-06-27 10:15:20 -07001106 }
Robert Carr68375192017-06-13 12:41:53 -07001107 mFrozenBounds.remove();
Wale Ogunwale37dbafc2016-06-27 10:15:20 -07001108 if (!mFrozenMergedConfig.isEmpty()) {
1109 mFrozenMergedConfig.remove();
1110 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001111 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001112 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001113 win.onUnfreezeBounds();
Jorim Jaggi4846ee32016-01-07 17:39:12 +01001114 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001115 mService.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +01001116 }
1117
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001118 void setAppLayoutChanges(int changes, String reason) {
1119 if (!mChildren.isEmpty()) {
1120 final DisplayContent dc = getDisplayContent();
1121 dc.pendingLayoutChanges |= changes;
1122 if (DEBUG_LAYOUT_REPEATS) {
1123 mService.mWindowPlacerLocked.debugLayoutRepeats(reason, dc.pendingLayoutChanges);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001124 }
1125 }
1126 }
1127
1128 void removeReplacedWindowIfNeeded(WindowState replacement) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001129 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001130 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001131 if (win.removeReplacedWindowIfNeeded(replacement)) {
1132 return;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001133 }
1134 }
1135 }
1136
1137 void startFreezingScreen() {
1138 if (DEBUG_ORIENTATION) logWithStack(TAG, "Set freezing of " + appToken + ": hidden="
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001139 + hidden + " freezing=" + mAppAnimator.freezingScreen + " hiddenRequested="
1140 + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001141 if (!hiddenRequested) {
1142 if (!mAppAnimator.freezingScreen) {
1143 mAppAnimator.freezingScreen = true;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001144 mService.registerAppFreezeListener(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001145 mAppAnimator.lastFreezeDuration = 0;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001146 mService.mAppsFreezingScreen++;
1147 if (mService.mAppsFreezingScreen == 1) {
Bryce Lee8f853582017-06-05 17:25:59 -07001148 mService.startFreezingDisplayLocked(false, 0, 0, getDisplayContent());
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001149 mService.mH.removeMessages(H.APP_FREEZE_TIMEOUT);
1150 mService.mH.sendEmptyMessageDelayed(H.APP_FREEZE_TIMEOUT, 2000);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001151 }
1152 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001153 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001154 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001155 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001156 w.onStartFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001157 }
1158 }
1159 }
1160
1161 void stopFreezingScreen(boolean unfreezeSurfaceNow, boolean force) {
1162 if (!mAppAnimator.freezingScreen) {
1163 return;
1164 }
1165 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Clear freezing of " + this + " force=" + force);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001166 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001167 boolean unfrozeWindows = false;
1168 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001169 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001170 unfrozeWindows |= w.onStopFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001171 }
1172 if (force || unfrozeWindows) {
1173 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "No longer freezing: " + this);
1174 mAppAnimator.freezingScreen = false;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001175 mService.unregisterAppFreezeListener(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001176 mAppAnimator.lastFreezeDuration =
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001177 (int)(SystemClock.elapsedRealtime() - mService.mDisplayFreezeTime);
1178 mService.mAppsFreezingScreen--;
1179 mService.mLastFinishedFreezeSource = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001180 }
1181 if (unfreezeSurfaceNow) {
1182 if (unfrozeWindows) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001183 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001184 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001185 mService.stopFreezingDisplayLocked();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001186 }
1187 }
1188
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001189 @Override
1190 public void onAppFreezeTimeout() {
1191 Slog.w(TAG_WM, "Force clearing freeze: " + this);
1192 stopFreezingScreen(true, true);
1193 }
1194
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001195 boolean transferStartingWindow(IBinder transferFrom) {
Wale Ogunwale02319a62016-09-26 15:21:22 -07001196 final AppWindowToken fromToken = getDisplayContent().getAppWindowToken(transferFrom);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001197 if (fromToken == null) {
1198 return false;
1199 }
1200
1201 final WindowState tStartingWindow = fromToken.startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001202 if (tStartingWindow != null && fromToken.startingSurface != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001203 // In this case, the starting icon has already been displayed, so start
1204 // letting windows get shown immediately without any more transitions.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001205 mService.mSkipAppTransitionAnimation = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001206
1207 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Moving existing starting " + tStartingWindow
1208 + " from " + fromToken + " to " + this);
1209
1210 final long origId = Binder.clearCallingIdentity();
1211
1212 // Transfer the starting window over to the new token.
1213 startingData = fromToken.startingData;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001214 startingSurface = fromToken.startingSurface;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001215 startingDisplayed = fromToken.startingDisplayed;
1216 fromToken.startingDisplayed = false;
1217 startingWindow = tStartingWindow;
1218 reportedVisible = fromToken.reportedVisible;
1219 fromToken.startingData = null;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001220 fromToken.startingSurface = null;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001221 fromToken.startingWindow = null;
1222 fromToken.startingMoved = true;
1223 tStartingWindow.mToken = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001224 tStartingWindow.mAppToken = this;
1225
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001226 if (DEBUG_ADD_REMOVE || DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001227 "Removing starting " + tStartingWindow + " from " + fromToken);
Wale Ogunwalefa854eb2016-09-20 13:43:52 -07001228 fromToken.removeChild(tStartingWindow);
1229 fromToken.postWindowRemoveStartingWindowCleanup(tStartingWindow);
Wale Ogunwale6c459212017-05-17 08:56:03 -07001230 fromToken.mHiddenSetFromTransferredStartingWindow = false;
Wale Ogunwale92fc3722016-08-05 12:19:08 -07001231 addWindow(tStartingWindow);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001232
1233 // Propagate other interesting state between the tokens. If the old token is displayed,
1234 // we should immediately force the new one to be displayed. If it is animating, we need
1235 // to move that animation to the new one.
1236 if (fromToken.allDrawn) {
1237 allDrawn = true;
1238 deferClearAllDrawn = fromToken.deferClearAllDrawn;
1239 }
1240 if (fromToken.firstWindowDrawn) {
1241 firstWindowDrawn = true;
1242 }
1243 if (!fromToken.hidden) {
1244 hidden = false;
1245 hiddenRequested = false;
Wale Ogunwale6c459212017-05-17 08:56:03 -07001246 mHiddenSetFromTransferredStartingWindow = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001247 }
Wale Ogunwale89973222017-04-23 18:39:45 -07001248 setClientHidden(fromToken.mClientHidden);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001249 fromToken.mAppAnimator.transferCurrentAnimation(
1250 mAppAnimator, tStartingWindow.mWinAnimator);
1251
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001252 mService.updateFocusedWindowLocked(
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001253 UPDATE_FOCUS_WILL_PLACE_SURFACES, true /*updateInputWindows*/);
Andrii Kulian5406e7a2016-10-21 11:55:23 -07001254 getDisplayContent().setLayoutNeeded();
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001255 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001256 Binder.restoreCallingIdentity(origId);
1257 return true;
1258 } else if (fromToken.startingData != null) {
1259 // The previous app was getting ready to show a
1260 // starting window, but hasn't yet done so. Steal it!
1261 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1262 "Moving pending starting from " + fromToken + " to " + this);
1263 startingData = fromToken.startingData;
1264 fromToken.startingData = null;
1265 fromToken.startingMoved = true;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001266 if (getController() != null) {
1267 getController().scheduleAddStartingWindow();
1268 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001269 return true;
1270 }
1271
1272 final AppWindowAnimator tAppAnimator = fromToken.mAppAnimator;
1273 final AppWindowAnimator wAppAnimator = mAppAnimator;
1274 if (tAppAnimator.thumbnail != null) {
1275 // The old token is animating with a thumbnail, transfer that to the new token.
1276 if (wAppAnimator.thumbnail != null) {
1277 wAppAnimator.thumbnail.destroy();
1278 }
1279 wAppAnimator.thumbnail = tAppAnimator.thumbnail;
1280 wAppAnimator.thumbnailLayer = tAppAnimator.thumbnailLayer;
1281 wAppAnimator.thumbnailAnimation = tAppAnimator.thumbnailAnimation;
1282 tAppAnimator.thumbnail = null;
1283 }
1284 return false;
1285 }
1286
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001287 boolean isLastWindow(WindowState win) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001288 return mChildren.size() == 1 && mChildren.get(0) == win;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001289 }
1290
1291 void setAllAppWinAnimators() {
1292 final ArrayList<WindowStateAnimator> allAppWinAnimators = mAppAnimator.mAllAppWinAnimators;
1293 allAppWinAnimators.clear();
1294
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001295 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001296 for (int j = 0; j < windowsCount; j++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001297 (mChildren.get(j)).addWinAnimatorToList(allAppWinAnimators);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001298 }
1299 }
1300
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001301 @Override
1302 void onAppTransitionDone() {
1303 sendingToBottom = false;
1304 }
1305
Wale Ogunwale51362492016-09-08 17:49:17 -07001306 /**
1307 * We override because this class doesn't want its children affecting its reported orientation
1308 * in anyway.
1309 */
1310 @Override
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001311 int getOrientation(int candidate) {
Bryce Lee0e7b7c92017-08-10 14:59:00 -07001312 // We do not allow non-fullscreen apps to influence orientation starting in O-MR1. While we
1313 // do throw an exception in {@link Activity#onCreate} and
Bryce Lee39791592017-04-26 09:29:12 -07001314 // {@link Activity#setRequestedOrientation}, we also ignore the orientation here so that
1315 // other calculations aren't affected.
Bryce Lee0e7b7c92017-08-10 14:59:00 -07001316 if (!fillsParent() && mTargetSdk >= O_MR1) {
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001317 // Can't specify orientation if app doesn't fill parent.
1318 return SCREEN_ORIENTATION_UNSET;
1319 }
1320
1321 if (candidate == SCREEN_ORIENTATION_BEHIND) {
1322 // Allow app to specify orientation regardless of its visibility state if the current
1323 // candidate want us to use orientation behind. I.e. the visible app on-top of this one
1324 // wants us to use the orientation of the app behind it.
1325 return mOrientation;
1326 }
1327
Bryce Lee61fbcbc2017-03-10 14:14:03 -08001328 // The {@link AppWindowToken} should only specify an orientation when it is not closing or
1329 // going to the bottom. Allowing closing {@link AppWindowToken} to participate can lead to
1330 // an Activity in another task being started in the wrong orientation during the transition.
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001331 if (!(sendingToBottom || mService.mClosingApps.contains(this))
Bryce Lee00d586d2017-07-28 20:48:43 -07001332 && (isVisible() || mService.mOpeningApps.contains(this) || isOnTop())) {
Bryce Leea163b762017-01-24 11:05:01 -08001333 return mOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001334 }
Bryce Leea163b762017-01-24 11:05:01 -08001335
1336 return SCREEN_ORIENTATION_UNSET;
Wale Ogunwale51362492016-09-08 17:49:17 -07001337 }
1338
Wale Ogunwaleb198b742016-12-01 08:44:09 -08001339 /** Returns the app's preferred orientation regardless of its currently visibility state. */
1340 int getOrientationIgnoreVisibility() {
1341 return mOrientation;
1342 }
1343
Craig Mautnerdbb79912012-03-01 18:59:14 -08001344 @Override
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001345 void checkAppWindowsReadyToShow() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001346 if (allDrawn == mAppAnimator.allDrawn) {
1347 return;
1348 }
1349
1350 mAppAnimator.allDrawn = allDrawn;
1351 if (!allDrawn) {
1352 return;
1353 }
1354
1355 // The token has now changed state to having all windows shown... what to do, what to do?
1356 if (mAppAnimator.freezingScreen) {
1357 mAppAnimator.showAllWindowsLocked();
1358 stopFreezingScreen(false, true);
1359 if (DEBUG_ORIENTATION) Slog.i(TAG,
1360 "Setting mOrientationChangeComplete=true because wtoken " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001361 + " numInteresting=" + mNumInterestingWindows + " numDrawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001362 // This will set mOrientationChangeComplete and cause a pass through layout.
1363 setAppLayoutChanges(FINISH_LAYOUT_REDO_WALLPAPER,
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001364 "checkAppWindowsReadyToShow: freezingScreen");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001365 } else {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001366 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM, "checkAppWindowsReadyToShow");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001367
1368 // We can now show all of the drawn windows!
1369 if (!mService.mOpeningApps.contains(this)) {
1370 mService.mAnimator.orAnimating(mAppAnimator.showAllWindowsLocked());
1371 }
1372 }
1373 }
1374
Matthew Ng5d23afa2017-06-21 16:16:24 -07001375 /**
Bryce Leed390deb2017-06-22 13:14:28 -07001376 * Returns whether the drawn window states of this {@link AppWindowToken} has considered every
1377 * child {@link WindowState}. A child is considered if it has been passed into
1378 * {@link #updateDrawnWindowStates(WindowState)} after being added. This is used to determine
1379 * whether states, such as {@code allDrawn}, can be set, which relies on state variables such as
1380 * {@code mNumInterestingWindows}, which depend on all {@link WindowState}s being considered.
1381 *
1382 * @return {@code true} If all children have been considered, {@code false}.
1383 */
1384 private boolean allDrawnStatesConsidered() {
Bryce Lee6311c4b2017-07-06 14:09:29 -07001385 for (int i = mChildren.size() - 1; i >= 0; --i) {
1386 final WindowState child = mChildren.get(i);
1387 if (child.mightAffectAllDrawn(false /*visibleOnly*/ )
1388 && !child.getDrawnStateEvaluated()) {
Bryce Leed390deb2017-06-22 13:14:28 -07001389 return false;
1390 }
1391 }
1392 return true;
1393 }
1394
1395 /**
Matthew Ng5d23afa2017-06-21 16:16:24 -07001396 * Determines if the token has finished drawing. This should only be called from
1397 * {@link DisplayContent#applySurfaceChangesTransaction}
1398 */
Matthew Ng498c71d2017-04-18 13:55:45 -07001399 void updateAllDrawn() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001400 if (!allDrawn) {
Matthew Ng498c71d2017-04-18 13:55:45 -07001401 // Number of drawn windows can be less when a window is being relaunched, wait for
Bryce Leed390deb2017-06-22 13:14:28 -07001402 // all windows to be launched and drawn for this token be considered all drawn.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001403 final int numInteresting = mNumInterestingWindows;
Bryce Leed390deb2017-06-22 13:14:28 -07001404
1405 // We must make sure that all present children have been considered (determined by
1406 // {@link #allDrawnStatesConsidered}) before evaluating whether everything has been
1407 // drawn.
1408 if (numInteresting > 0 && allDrawnStatesConsidered()
1409 && mNumDrawnWindows >= numInteresting && !isRelaunching()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001410 if (DEBUG_VISIBILITY) Slog.v(TAG, "allDrawn: " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001411 + " interesting=" + numInteresting + " drawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001412 allDrawn = true;
1413 // Force an additional layout pass where
1414 // WindowStateAnimator#commitFinishDrawingLocked() will call performShowLocked().
Matthew Ng498c71d2017-04-18 13:55:45 -07001415 if (mDisplayContent != null) {
1416 mDisplayContent.setLayoutNeeded();
1417 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001418 mService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
Robert Carrecc06b32017-04-18 14:25:10 -07001419
Winson Chunge7ba6862017-05-24 12:13:33 -07001420 // Notify the pinned stack upon all windows drawn. If there was an animation in
1421 // progress then this signal will resume that animation.
1422 final TaskStack pinnedStack = mDisplayContent.getStackById(PINNED_STACK_ID);
1423 if (pinnedStack != null) {
1424 pinnedStack.onAllWindowsDrawn();
Robert Carrecc06b32017-04-18 14:25:10 -07001425 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001426 }
1427 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001428
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001429 if (!allDrawnExcludingSaved) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001430 int numInteresting = mNumInterestingWindowsExcludingSaved;
1431 if (numInteresting > 0 && mNumDrawnWindowsExcludingSaved >= numInteresting) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001432 if (DEBUG_VISIBILITY) Slog.v(TAG, "allDrawnExcludingSaved: " + this
1433 + " interesting=" + numInteresting
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001434 + " drawn=" + mNumDrawnWindowsExcludingSaved);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001435 allDrawnExcludingSaved = true;
Matthew Ng498c71d2017-04-18 13:55:45 -07001436 if (mDisplayContent != null) {
1437 mDisplayContent.setLayoutNeeded();
1438 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001439 if (isAnimatingInvisibleWithSavedSurface()
1440 && !mService.mFinishedEarlyAnim.contains(this)) {
1441 mService.mFinishedEarlyAnim.add(this);
1442 }
1443 }
1444 }
1445 }
1446
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001447 /**
1448 * Updated this app token tracking states for interesting and drawn windows based on the window.
1449 *
1450 * @return Returns true if the input window is considered interesting and drawn while all the
1451 * windows in this app token where not considered drawn as of the last pass.
1452 */
1453 boolean updateDrawnWindowStates(WindowState w) {
Bryce Leed390deb2017-06-22 13:14:28 -07001454 w.setDrawnStateEvaluated(true /*evaluated*/);
1455
Jorim Jaggie4b0f282017-05-17 15:10:29 +02001456 if (DEBUG_STARTING_WINDOW_VERBOSE && w == startingWindow) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001457 Slog.d(TAG, "updateWindows: starting " + w + " isOnScreen=" + w.isOnScreen()
1458 + " allDrawn=" + allDrawn + " freezingScreen=" + mAppAnimator.freezingScreen);
1459 }
1460
1461 if (allDrawn && allDrawnExcludingSaved && !mAppAnimator.freezingScreen) {
1462 return false;
1463 }
1464
1465 if (mLastTransactionSequence != mService.mTransactionSequence) {
1466 mLastTransactionSequence = mService.mTransactionSequence;
1467 mNumInterestingWindows = mNumDrawnWindows = 0;
1468 mNumInterestingWindowsExcludingSaved = 0;
1469 mNumDrawnWindowsExcludingSaved = 0;
1470 startingDisplayed = false;
1471 }
1472
1473 final WindowStateAnimator winAnimator = w.mWinAnimator;
1474
1475 boolean isInterestingAndDrawn = false;
1476
1477 if (!allDrawn && w.mightAffectAllDrawn(false /* visibleOnly */)) {
1478 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
1479 Slog.v(TAG, "Eval win " + w + ": isDrawn=" + w.isDrawnLw()
1480 + ", isAnimationSet=" + winAnimator.isAnimationSet());
1481 if (!w.isDrawnLw()) {
1482 Slog.v(TAG, "Not displayed: s=" + winAnimator.mSurfaceController
1483 + " pv=" + w.mPolicyVisibility
1484 + " mDrawState=" + winAnimator.drawStateToString()
1485 + " ph=" + w.isParentWindowHidden() + " th=" + hiddenRequested
1486 + " a=" + winAnimator.mAnimating);
1487 }
1488 }
1489
1490 if (w != startingWindow) {
1491 if (w.isInteresting()) {
1492 mNumInterestingWindows++;
1493 if (w.isDrawnLw()) {
1494 mNumDrawnWindows++;
1495
1496 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG, "tokenMayBeDrawn: "
1497 + this + " w=" + w + " numInteresting=" + mNumInterestingWindows
1498 + " freezingScreen=" + mAppAnimator.freezingScreen
1499 + " mAppFreezing=" + w.mAppFreezing);
1500
1501 isInterestingAndDrawn = true;
1502 }
1503 }
1504 } else if (w.isDrawnLw()) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001505 if (getController() != null) {
1506 getController().reportStartingWindowDrawn();
1507 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001508 startingDisplayed = true;
1509 }
1510 }
1511
1512 if (!allDrawnExcludingSaved && w.mightAffectAllDrawn(true /* visibleOnly */)) {
1513 if (w != startingWindow && w.isInteresting()) {
1514 mNumInterestingWindowsExcludingSaved++;
1515 if (w.isDrawnLw() && !w.isAnimatingWithSavedSurface()) {
1516 mNumDrawnWindowsExcludingSaved++;
1517
1518 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG,
1519 "tokenMayBeDrawnExcludingSaved: " + this + " w=" + w
1520 + " numInteresting=" + mNumInterestingWindowsExcludingSaved
1521 + " freezingScreen=" + mAppAnimator.freezingScreen
1522 + " mAppFreezing=" + w.mAppFreezing);
1523
1524 isInterestingAndDrawn = true;
1525 }
1526 }
1527 }
1528
1529 return isInterestingAndDrawn;
1530 }
1531
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001532 @Override
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001533 void stepAppWindowsAnimation(long currentTime) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001534 mAppAnimator.wasAnimating = mAppAnimator.animating;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001535 if (mAppAnimator.stepAnimationLocked(currentTime)) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001536 mAppAnimator.animating = true;
1537 mService.mAnimator.setAnimating(true);
1538 mService.mAnimator.mAppWindowAnimating = true;
1539 } else if (mAppAnimator.wasAnimating) {
1540 // stopped animating, do one more pass through the layout
Wale Ogunwale1e129a42016-11-21 13:03:47 -08001541 setAppLayoutChanges(FINISH_LAYOUT_REDO_WALLPAPER,
1542 DEBUG_LAYOUT_REPEATS ? "appToken " + this + " done" : null);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001543 if (DEBUG_ANIM) Slog.v(TAG, "updateWindowsApps...: done animating " + this);
1544 }
1545 }
1546
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001547 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001548 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001549 // For legacy reasons we process the TaskStack.mExitingAppTokens first in DisplayContent
1550 // before the non-exiting app tokens. So, we skip the exiting app tokens here.
1551 // TODO: Investigate if we need to continue to do this or if we can just process them
1552 // in-order.
1553 if (mIsExiting && !waitingForReplacement()) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001554 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001555 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001556 return forAllWindowsUnchecked(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001557 }
1558
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001559 boolean forAllWindowsUnchecked(ToBooleanFunction<WindowState> callback,
1560 boolean traverseTopToBottom) {
1561 return super.forAllWindows(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001562 }
1563
1564 @Override
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001565 AppWindowToken asAppWindowToken() {
1566 // I am an app window token!
1567 return this;
1568 }
1569
1570 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001571 boolean fillsParent() {
1572 return mFillsParent;
1573 }
1574
1575 void setFillsParent(boolean fillsParent) {
1576 mFillsParent = fillsParent;
1577 }
1578
Jorim Jaggife762342016-10-13 14:33:27 +02001579 boolean containsDismissKeyguardWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001580 // Window state is transient during relaunch. We are not guaranteed to be frozen during the
1581 // entirety of the relaunch.
1582 if (isRelaunching()) {
1583 return mLastContainsDismissKeyguardWindow;
1584 }
1585
Jorim Jaggife762342016-10-13 14:33:27 +02001586 for (int i = mChildren.size() - 1; i >= 0; i--) {
1587 if ((mChildren.get(i).mAttrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
1588 return true;
1589 }
1590 }
1591 return false;
1592 }
1593
1594 boolean containsShowWhenLockedWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001595 // When we are relaunching, it is possible for us to be unfrozen before our previous
1596 // windows have been added back. Using the cached value ensures that our previous
1597 // showWhenLocked preference is honored until relaunching is complete.
1598 if (isRelaunching()) {
1599 return mLastContainsShowWhenLockedWindow;
1600 }
1601
Jorim Jaggife762342016-10-13 14:33:27 +02001602 for (int i = mChildren.size() - 1; i >= 0; i--) {
1603 if ((mChildren.get(i).mAttrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
1604 return true;
1605 }
1606 }
Bryce Lee081554b2017-05-25 07:52:12 -07001607
Jorim Jaggife762342016-10-13 14:33:27 +02001608 return false;
1609 }
1610
1611 void checkKeyguardFlagsChanged() {
1612 final boolean containsDismissKeyguard = containsDismissKeyguardWindow();
1613 final boolean containsShowWhenLocked = containsShowWhenLockedWindow();
1614 if (containsDismissKeyguard != mLastContainsDismissKeyguardWindow
1615 || containsShowWhenLocked != mLastContainsShowWhenLockedWindow) {
1616 mService.notifyKeyguardFlagsChanged(null /* callback */);
1617 }
1618 mLastContainsDismissKeyguardWindow = containsDismissKeyguard;
1619 mLastContainsShowWhenLockedWindow = containsShowWhenLocked;
1620 }
1621
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001622 WindowState getImeTargetBelowWindow(WindowState w) {
1623 final int index = mChildren.indexOf(w);
1624 if (index > 0) {
1625 final WindowState target = mChildren.get(index - 1);
1626 if (target.canBeImeTarget()) {
1627 return target;
1628 }
1629 }
1630 return null;
1631 }
1632
Winson Chungbe9be7f2017-06-28 10:55:22 -07001633 int getLowestAnimLayer() {
1634 for (int i = 0; i < mChildren.size(); i++) {
1635 final WindowState w = mChildren.get(i);
1636 if (w.mRemoved) {
1637 continue;
1638 }
1639 return w.mWinAnimator.mAnimLayer;
1640 }
1641 return Integer.MAX_VALUE;
1642 }
1643
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001644 WindowState getHighestAnimLayerWindow(WindowState currentTarget) {
1645 WindowState candidate = null;
1646 for (int i = mChildren.indexOf(currentTarget); i >= 0; i--) {
1647 final WindowState w = mChildren.get(i);
1648 if (w.mRemoved) {
1649 continue;
1650 }
1651 if (candidate == null || w.mWinAnimator.mAnimLayer >
1652 candidate.mWinAnimator.mAnimLayer) {
1653 candidate = w;
1654 }
1655 }
1656 return candidate;
1657 }
1658
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001659 /**
1660 * See {@link Activity#setDisablePreviewScreenshots}.
1661 */
1662 void setDisablePreviewScreenshots(boolean disable) {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001663 mDisablePreviewScreenshots = disable;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001664 }
1665
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001666 /**
chaviwd3bf08d2017-08-01 17:24:59 -07001667 * Sets whether the current launch can turn the screen on. See {@link #canTurnScreenOn()}
1668 */
1669 void setCanTurnScreenOn(boolean canTurnScreenOn) {
1670 mCanTurnScreenOn = canTurnScreenOn;
1671 }
1672
1673 /**
1674 * Indicates whether the current launch can turn the screen on. This is to prevent multiple
1675 * relayouts from turning the screen back on. The screen should only turn on at most
1676 * once per activity resume.
1677 *
1678 * @return true if the screen can be turned on.
1679 */
1680 boolean canTurnScreenOn() {
1681 return mCanTurnScreenOn;
1682 }
1683
1684 /**
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001685 * Retrieves whether we'd like to generate a snapshot that's based solely on the theme. This is
1686 * the case when preview screenshots are disabled {@link #setDisablePreviewScreenshots} or when
1687 * we can't take a snapshot for other reasons, for example, if we have a secure window.
1688 *
1689 * @return True if we need to generate an app theme snapshot, false if we'd like to take a real
1690 * screenshot.
1691 */
1692 boolean shouldUseAppThemeSnapshot() {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001693 return mDisablePreviewScreenshots || forAllWindows(w -> (w.mAttrs.flags & FLAG_SECURE) != 0,
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001694 true /* topToBottom */);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001695 }
1696
Wale Ogunwale51362492016-09-08 17:49:17 -07001697 @Override
Robert Carrdee1b3f2017-02-27 11:33:33 -08001698 int getAnimLayerAdjustment() {
1699 return mAppAnimator.animLayerAdjustment;
1700 }
1701
1702 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001703 void dump(PrintWriter pw, String prefix) {
1704 super.dump(pw, prefix);
1705 if (appToken != null) {
Wale Ogunwale72919d22016-12-08 18:58:50 -08001706 pw.println(prefix + "app=true mVoiceInteraction=" + mVoiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001707 }
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001708 pw.print(prefix); pw.print("task="); pw.println(getTask());
Wale Ogunwale51362492016-09-08 17:49:17 -07001709 pw.print(prefix); pw.print(" mFillsParent="); pw.print(mFillsParent);
1710 pw.print(" mOrientation="); pw.println(mOrientation);
Wale Ogunwale89973222017-04-23 18:39:45 -07001711 pw.println(prefix + "hiddenRequested=" + hiddenRequested + " mClientHidden=" + mClientHidden
1712 + ((mDeferHidingClient) ? " mDeferHidingClient=" + mDeferHidingClient : "")
1713 + " reportedDrawn=" + reportedDrawn + " reportedVisible=" + reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -07001714 if (paused) {
1715 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001716 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -08001717 if (mAppStopped) {
1718 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
1719 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001720 if (mNumInterestingWindows != 0 || mNumDrawnWindows != 0
Craig Mautner6fbda632012-07-03 09:26:39 -07001721 || allDrawn || mAppAnimator.allDrawn) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001722 pw.print(prefix); pw.print("mNumInterestingWindows=");
1723 pw.print(mNumInterestingWindows);
1724 pw.print(" mNumDrawnWindows="); pw.print(mNumDrawnWindows);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001725 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -07001726 pw.print(" allDrawn="); pw.print(allDrawn);
1727 pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
1728 pw.println(")");
1729 }
1730 if (inPendingTransaction) {
1731 pw.print(prefix); pw.print("inPendingTransaction=");
1732 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001733 }
Craig Mautner799bc1d2015-01-14 10:33:48 -08001734 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001735 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
1736 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -08001737 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -08001738 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001739 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001740 if (startingWindow != null || startingSurface != null
Wale Ogunwale6c459212017-05-17 08:56:03 -07001741 || startingDisplayed || startingMoved || mHiddenSetFromTransferredStartingWindow) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001742 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001743 pw.print(" startingSurface="); pw.print(startingSurface);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001744 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale6c459212017-05-17 08:56:03 -07001745 pw.print(" startingMoved="); pw.print(startingMoved);
1746 pw.println(" mHiddenSetFromTransferredStartingWindow="
1747 + mHiddenSetFromTransferredStartingWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001748 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001749 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -08001750 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001751 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -08001752 }
1753 if (mPendingRelaunchCount != 0) {
1754 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +01001755 }
Wale Ogunwale9c64cb62017-04-12 13:39:59 -07001756 if (getController() != null) {
1757 pw.print(prefix); pw.print("controller="); pw.println(getController());
1758 }
Wale Ogunwalee287e192017-04-21 09:30:12 -07001759 if (mRemovingFromDisplay) {
1760 pw.println(prefix + "mRemovingFromDisplay=" + mRemovingFromDisplay);
1761 }
Jorim Jaggi4d1835d2017-08-31 17:28:27 +02001762 if (mAppAnimator.isAnimating()) {
1763 mAppAnimator.dump(pw, prefix + " ");
1764 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001765 }
1766
1767 @Override
1768 public String toString() {
1769 if (stringName == null) {
1770 StringBuilder sb = new StringBuilder();
1771 sb.append("AppWindowToken{");
1772 sb.append(Integer.toHexString(System.identityHashCode(this)));
1773 sb.append(" token="); sb.append(token); sb.append('}');
1774 stringName = sb.toString();
1775 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001776 return stringName + ((mIsExiting) ? " mIsExiting=" : "");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001777 }
Jeff Browne9bdb312012-04-05 15:30:10 -07001778}