blob: de4b426b521d8a03882b64c6be3433ac75c20657 [file] [log] [blame]
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale72919d22016-12-08 18:58:50 -080019import static android.content.pm.ActivityInfo.CONFIG_ORIENTATION;
20import static android.content.pm.ActivityInfo.CONFIG_SCREEN_SIZE;
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -070021import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070022import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020023import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070024import static android.view.Display.DEFAULT_DISPLAY;
Jorim Jaggife762342016-10-13 14:33:27 +020025import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020026import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Jorim Jaggife762342016-10-13 14:33:27 +020027import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Wale Ogunwaled1c37912016-08-16 03:19:39 -070028import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080029import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070030import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Adrian Roose99bc052017-11-20 17:55:31 +010031import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
32import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070033import static com.android.server.wm.AppTransition.TRANSIT_UNSET;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080034import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080035import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
36import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070037import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070038import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
39import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwale9017ec02016-02-25 08:55:25 -080040import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
Jorim Jaggie4b0f282017-05-17 15:10:29 +020041import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW_VERBOSE;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070042import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080043import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
44import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
45import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
46import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Chong Zhang92147042016-05-09 12:47:11 -070047import static com.android.server.wm.WindowManagerService.H.NOTIFY_ACTIVITY_DRAWN;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -070048import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_NORMAL;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070049import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070050import static com.android.server.wm.WindowManagerService.logWithStack;
Steven Timotiusaf03df62017-07-18 16:56:43 -070051import static com.android.server.wm.proto.AppWindowTokenProto.NAME;
52import static com.android.server.wm.proto.AppWindowTokenProto.WINDOW_TOKEN;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080053
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070054import android.annotation.CallSuper;
Filip Gruszczynskia590c992015-11-25 16:45:26 -080055import android.annotation.NonNull;
Jorim Jaggid635a4a2017-05-03 15:21:26 +020056import android.app.Activity;
Jorim Jaggi26c8c422016-05-09 19:57:25 -070057import android.content.res.Configuration;
Jorim Jaggi988f6682017-11-17 17:46:43 +010058import android.graphics.GraphicBuffer;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020059import android.graphics.Point;
Jorim Jaggi0429f352015-12-22 16:29:16 +010060import android.graphics.Rect;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070061import android.os.Binder;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080062import android.os.Debug;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070063import android.os.IBinder;
Steven Timotiusaf03df62017-07-18 16:56:43 -070064import android.os.RemoteException;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070065import android.os.SystemClock;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020066import android.os.Trace;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080067import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -070068import android.util.proto.ProtoOutputStream;
Jorim Jaggif5f9e122017-10-24 18:21:09 +020069import android.view.DisplayInfo;
70import android.view.animation.Animation;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080071import android.view.IApplicationToken;
Robert Carr6914f082017-03-20 19:04:30 -070072import android.view.SurfaceControl;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080073import android.view.WindowManager;
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +020074import android.view.WindowManager.LayoutParams;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080075
76import com.android.internal.util.ToBooleanFunction;
77import com.android.server.input.InputApplicationHandle;
Adrian Roose99bc052017-11-20 17:55:31 +010078import com.android.server.policy.WindowManagerPolicy.StartingSurface;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080079import com.android.server.wm.WindowManagerService.H;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080080
81import java.io.PrintWriter;
Jorim Jaggi0429f352015-12-22 16:29:16 +010082import java.util.ArrayDeque;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080083import java.util.ArrayList;
Jorim Jaggia5e10572017-11-15 14:36:26 +010084import java.util.LinkedList;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080085
86class AppTokenList extends ArrayList<AppWindowToken> {
87}
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080088
89/**
90 * Version of WindowToken that is specifically for a particular application (or
91 * really activity) that is displaying windows.
92 */
Wale Ogunwale3f4433d2016-08-18 20:42:42 -070093class AppWindowToken extends WindowToken implements WindowManagerService.AppFreezeListener {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080094 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowToken" : TAG_WM;
95
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080096 // Non-null only for application tokens.
97 final IApplicationToken appToken;
98
Filip Gruszczynskia590c992015-11-25 16:45:26 -080099 @NonNull final AppWindowAnimator mAppAnimator;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -0700100
Wale Ogunwale72919d22016-12-08 18:58:50 -0800101 final boolean mVoiceInteraction;
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700102
Wale Ogunwale51362492016-09-08 17:49:17 -0700103 /** @see WindowContainer#fillsParent() */
104 private boolean mFillsParent;
Craig Mautner4c5eb222013-11-18 12:59:05 -0800105 boolean layoutConfigChanges;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800106 boolean mShowForAllUsers;
107 int mTargetSdk;
Craig Mautnera2c77052012-03-26 12:14:43 -0700108
Bryce Lee6d410262017-02-28 15:30:17 -0800109 // Flag set while reparenting to prevent actions normally triggered by an individual parent
110 // change.
111 private boolean mReparenting;
112
Wale Ogunwalee287e192017-04-21 09:30:12 -0700113 // True if we are current in the process of removing this app token from the display
114 private boolean mRemovingFromDisplay = false;
115
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800116 // The input dispatching timeout for this application token in nanoseconds.
Wale Ogunwale72919d22016-12-08 18:58:50 -0800117 long mInputDispatchingTimeoutNanos;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800118
119 // These are used for determining when all windows associated with
120 // an activity have been drawn, so they can be made visible together
121 // at the same time.
Craig Mautner764983d2012-03-22 11:37:36 -0700122 // initialize so that it doesn't match mTransactionSequence which is an int.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700123 private long mLastTransactionSequence = Long.MIN_VALUE;
124 private int mNumInterestingWindows;
125 private int mNumDrawnWindows;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800126 boolean inPendingTransaction;
127 boolean allDrawn;
Craig Mautner7636dfb2012-11-16 15:24:11 -0800128 // Set to true when this app creates a surface while in the middle of an animation. In that
129 // case do not clear allDrawn until the animation completes.
130 boolean deferClearAllDrawn;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800131
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800132 // Is this window's surface needed? This is almost like hidden, except
133 // it will sometimes be true a little earlier: when the token has
134 // been shown, but is still waiting for its app transition to execute
135 // before making its windows shown.
136 boolean hiddenRequested;
137
138 // Have we told the window clients to hide themselves?
Wale Ogunwale89973222017-04-23 18:39:45 -0700139 private boolean mClientHidden;
140
141 // If true we will defer setting mClientHidden to true and reporting to the client that it is
142 // hidden.
143 boolean mDeferHidingClient;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800144
145 // Last visibility state we reported to the app token.
146 boolean reportedVisible;
147
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700148 // Last drawn state we reported to the app token.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700149 private boolean reportedDrawn;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700150
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800151 // Set to true when the token has been removed from the window mgr.
152 boolean removed;
153
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800154 // Information about an application starting window if displayed.
155 StartingData startingData;
156 WindowState startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800157 StartingSurface startingSurface;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800158 boolean startingDisplayed;
159 boolean startingMoved;
Wale Ogunwale6c459212017-05-17 08:56:03 -0700160 // True if the hidden state of this token was forced to false due to a transferred starting
161 // window.
162 private boolean mHiddenSetFromTransferredStartingWindow;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800163 boolean firstWindowDrawn;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700164 private final WindowState.UpdateReportedVisibilityResults mReportedVisibilityResults =
165 new WindowState.UpdateReportedVisibilityResults();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800166
167 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700168 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800169
Wale Ogunwale571771c2016-08-26 13:18:50 -0700170 // TODO: Have a WindowContainer state for tracking exiting/deferred removal.
Craig Mautner799bc1d2015-01-14 10:33:48 -0800171 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800172
Craig Mautnerbb742462014-07-07 15:28:55 -0700173 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700174 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700175
Wale Ogunwale72919d22016-12-08 18:58:50 -0800176 private boolean mAlwaysFocusable;
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800177
Robert Carre12aece2016-02-02 22:43:27 -0800178 boolean mAppStopped;
Robert Carrfd10cd12016-06-29 16:41:50 -0700179 int mRotationAnimationHint;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700180 private int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800181
Jorim Jaggife762342016-10-13 14:33:27 +0200182 private boolean mLastContainsShowWhenLockedWindow;
183 private boolean mLastContainsDismissKeyguardWindow;
184
Jorim Jaggi0429f352015-12-22 16:29:16 +0100185 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700186 ArrayDeque<Configuration> mFrozenMergedConfig = new ArrayDeque<>();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100187
Wale Ogunwale6c459212017-05-17 08:56:03 -0700188 private boolean mDisablePreviewScreenshots;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100189
Wale Ogunwale034a8ec2017-09-02 17:14:40 -0700190 private Task mLastParent;
Robert Carred3e83b2017-04-21 13:26:55 -0700191
chaviwd3bf08d2017-08-01 17:24:59 -0700192 /**
193 * See {@link #canTurnScreenOn()}
194 */
195 private boolean mCanTurnScreenOn = true;
196
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200197 /**
198 * If we are running an animation, this determines the transition type. Must be one of
199 * AppTransition.TRANSIT_* constants.
200 */
201 private int mTransit;
202
203 /**
204 * If we are running an animation, this determines the flags during this animation. Must be a
205 * bitwise combination of AppTransition.TRANSIT_FLAG_* constants.
206 */
207 private int mTransitFlags;
208
209 /** Whether our surface was set to be showing in the last call to {@link #prepareSurfaces} */
210 private boolean mLastSurfaceShowing;
211
Jorim Jaggi988f6682017-11-17 17:46:43 +0100212 private AppWindowThumbnail mThumbnail;
213
Wale Ogunwale72919d22016-12-08 18:58:50 -0800214 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
215 DisplayContent dc, long inputDispatchingTimeoutNanos, boolean fullscreen,
216 boolean showForAllUsers, int targetSdk, int orientation, int rotationAnimationHint,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800217 int configChanges, boolean launchTaskBehind, boolean alwaysFocusable,
Bryce Leef3c6a472017-11-14 14:53:06 -0800218 AppWindowContainerController controller) {
219 this(service, token, voiceInteraction, dc, fullscreen);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800220 setController(controller);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800221 mInputDispatchingTimeoutNanos = inputDispatchingTimeoutNanos;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800222 mShowForAllUsers = showForAllUsers;
223 mTargetSdk = targetSdk;
224 mOrientation = orientation;
225 layoutConfigChanges = (configChanges & (CONFIG_SCREEN_SIZE | CONFIG_ORIENTATION)) != 0;
226 mLaunchTaskBehind = launchTaskBehind;
227 mAlwaysFocusable = alwaysFocusable;
228 mRotationAnimationHint = rotationAnimationHint;
229
230 // Application tokens start out hidden.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200231 setHidden(true);
Wale Ogunwale72919d22016-12-08 18:58:50 -0800232 hiddenRequested = true;
233 }
234
235 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean voiceInteraction,
Bryce Leef3c6a472017-11-14 14:53:06 -0800236 DisplayContent dc, boolean fillsParent) {
Wale Ogunwale5cd907d2017-01-26 14:14:08 -0800237 super(service, token != null ? token.asBinder() : null, TYPE_APPLICATION, true, dc,
238 false /* ownerCanManageAppTokens */);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700239 appToken = token;
Wale Ogunwale72919d22016-12-08 18:58:50 -0800240 mVoiceInteraction = voiceInteraction;
Wale Ogunwale17f175c2017-02-07 16:54:10 -0800241 mFillsParent = fillsParent;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800242 mInputApplicationHandle = new InputApplicationHandle(this);
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200243 mAppAnimator = new AppWindowAnimator();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800244 }
245
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800246 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
247 firstWindowDrawn = true;
248
249 // We now have a good window to show, remove dead placeholders
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700250 removeDeadWindows();
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800251
Jorim Jaggi02886a82016-12-06 09:10:06 -0800252 if (startingWindow != null) {
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800253 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
254 + win.mToken + ": first real window is shown, no animation");
255 // If this initial window is animating, stop it -- we will do an animation to reveal
256 // it from behind the starting window, so there is no need for it to also be doing its
257 // own stuff.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100258 win.cancelAnimation();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800259 if (getController() != null) {
260 getController().removeStartingWindow();
261 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800262 }
263 updateReportedVisibilityLocked();
264 }
265
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800266 void updateReportedVisibilityLocked() {
267 if (appToken == null) {
268 return;
269 }
270
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700271 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700272 final int count = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800273
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700274 mReportedVisibilityResults.reset();
275
276 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700277 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700278 win.updateReportedVisibility(mReportedVisibilityResults);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800279 }
280
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700281 int numInteresting = mReportedVisibilityResults.numInteresting;
282 int numVisible = mReportedVisibilityResults.numVisible;
283 int numDrawn = mReportedVisibilityResults.numDrawn;
284 boolean nowGone = mReportedVisibilityResults.nowGone;
285
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700286 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200287 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting && !isHidden();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700288 if (!nowGone) {
289 // If the app is not yet gone, then it can only become visible/drawn.
290 if (!nowDrawn) {
291 nowDrawn = reportedDrawn;
292 }
293 if (!nowVisible) {
294 nowVisible = reportedVisible;
295 }
296 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800297 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800298 + numInteresting + " visible=" + numVisible);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800299 final AppWindowContainerController controller = getController();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700300 if (nowDrawn != reportedDrawn) {
301 if (nowDrawn) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800302 if (controller != null) {
303 controller.reportWindowsDrawn();
304 }
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700305 }
306 reportedDrawn = nowDrawn;
307 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800308 if (nowVisible != reportedVisible) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700309 if (DEBUG_VISIBILITY) Slog.v(TAG,
310 "Visibility changed in " + this + ": vis=" + nowVisible);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800311 reportedVisible = nowVisible;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800312 if (controller != null) {
313 if (nowVisible) {
314 controller.reportWindowsVisible();
315 } else {
316 controller.reportWindowsGone();
317 }
318 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800319 }
320 }
321
Wale Ogunwale89973222017-04-23 18:39:45 -0700322 boolean isClientHidden() {
323 return mClientHidden;
324 }
325
326 void setClientHidden(boolean hideClient) {
327 if (mClientHidden == hideClient || (hideClient && mDeferHidingClient)) {
328 return;
329 }
330 mClientHidden = hideClient;
331 sendAppVisibilityToClients();
332 }
333
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700334 boolean setVisibility(WindowManager.LayoutParams lp,
335 boolean visible, int transit, boolean performLayout, boolean isVoiceInteraction) {
336
337 boolean delayed = false;
338 inPendingTransaction = false;
Wale Ogunwale9e4721f2017-05-23 19:37:30 -0700339 // Reset the state of mHiddenSetFromTransferredStartingWindow since visibility is actually
340 // been set by the app now.
341 mHiddenSetFromTransferredStartingWindow = false;
Wale Ogunwale89973222017-04-23 18:39:45 -0700342 setClientHidden(!visible);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700343
344 // Allow for state changes and animation to be applied if:
345 // * token is transitioning visibility state
346 // * or the token was marked as hidden and is exiting before we had a chance to play the
347 // transition animation
348 // * or this is an opening app and windows are being replaced.
349 boolean visibilityChanged = false;
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200350 if (isHidden() == visible || (isHidden() && mIsExiting) || (visible && waitingForReplacement())) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700351 final AccessibilityController accessibilityController = mService.mAccessibilityController;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700352 boolean changed = false;
353 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200354 "Changing app " + this + " hidden=" + isHidden() + " performLayout=" + performLayout);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700355
356 boolean runningAppAnimation = false;
357
358 if (transit != AppTransition.TRANSIT_UNSET) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200359 if (applyAnimationLocked(lp, transit, visible, isVoiceInteraction)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700360 delayed = runningAppAnimation = true;
361 }
362 final WindowState window = findMainWindow();
363 //TODO (multidisplay): Magnification is supported only for the default display.
364 if (window != null && accessibilityController != null
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700365 && getDisplayContent().getDisplayId() == DEFAULT_DISPLAY) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700366 accessibilityController.onAppWindowTransitionLocked(window, transit);
367 }
368 changed = true;
369 }
370
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700371 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700372 for (int i = 0; i < windowsCount; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700373 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700374 changed |= win.onAppVisibilityChanged(visible, runningAppAnimation);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700375 }
376
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200377 setHidden(!visible);
378 hiddenRequested = !visible;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700379 visibilityChanged = true;
380 if (!visible) {
381 stopFreezingScreen(true, true);
382 } else {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700383 // If we are being set visible, and the starting window is not yet displayed,
384 // then make sure it doesn't get displayed.
385 if (startingWindow != null && !startingWindow.isDrawnLw()) {
386 startingWindow.mPolicyVisibility = false;
387 startingWindow.mPolicyVisibilityAfterAnim = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700388 }
Jorim Jaggi38d44ec2017-06-14 16:04:59 -0700389
390 // We are becoming visible, so better freeze the screen with the windows that are
391 // getting visible so we also wait for them.
392 forAllWindows(mService::makeWindowFreezingScreenIfNeededLocked, true);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700393 }
394
395 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "setVisibility: " + this
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200396 + ": hidden=" + isHidden() + " hiddenRequested=" + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700397
398 if (changed) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700399 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700400 if (performLayout) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700401 mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700402 false /*updateInputWindows*/);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700403 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700404 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700405 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700406 }
407 }
408
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200409 if (isReallyAnimating()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700410 delayed = true;
411 }
412
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700413 for (int i = mChildren.size() - 1; i >= 0 && !delayed; i--) {
Jorim Jaggia5e10572017-11-15 14:36:26 +0100414 if ((mChildren.get(i)).isSelfOrChildAnimating()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700415 delayed = true;
416 }
417 }
418
419 if (visibilityChanged) {
420 if (visible && !delayed) {
421 // The token was made immediately visible, there will be no entrance animation.
422 // We need to inform the client the enter animation was finished.
423 mEnteringAnimation = true;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700424 mService.mActivityManagerAppTransitionNotifier.onAppTransitionFinishedLocked(token);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700425 }
Robert Carr61b81112017-07-17 18:08:15 -0700426
Robert Carre7cc44d2017-03-20 19:04:30 -0700427 // If we are hidden but there is no delay needed we immediately
428 // apply the Surface transaction so that the ActivityManager
Robert Carr61b81112017-07-17 18:08:15 -0700429 // can have some guarantee on the Surface state following
430 // setting the visibility. This captures cases like dismissing
431 // the docked or pinned stack where there is no app transition.
432 //
433 // In the case of a "Null" animation, there will be
434 // no animation but there will still be a transition set.
435 // We still need to delay hiding the surface such that it
436 // can be synchronized with showing the next surface in the transition.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200437 if (isHidden() && !delayed && !mService.mAppTransition.isTransitionSet()) {
Robert Carr6914f082017-03-20 19:04:30 -0700438 SurfaceControl.openTransaction();
439 for (int i = mChildren.size() - 1; i >= 0; i--) {
440 mChildren.get(i).mWinAnimator.hide("immediately hidden");
441 }
442 SurfaceControl.closeTransaction();
443 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700444
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700445 if (!mService.mClosingApps.contains(this) && !mService.mOpeningApps.contains(this)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700446 // The token is not closing nor opening, so even if there is an animation set, that
447 // doesn't mean that it goes through the normal app transition cycle so we have
448 // to inform the docked controller about visibility change.
Andrii Kulian1e32e022016-09-16 15:29:34 -0700449 // TODO(multi-display): notify docked divider on all displays where visibility was
450 // affected.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700451 mService.getDefaultDisplayContentLocked().getDockedDividerController()
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700452 .notifyAppVisibilityChanged();
Jorim Jaggi8b702ed2017-01-20 16:59:03 +0100453 mService.mTaskSnapshotController.notifyAppVisibilityChanged(this, visible);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700454 }
455 }
456
457 return delayed;
458 }
459
Jorim Jaggi87fdbcb2017-08-17 13:41:11 +0200460 /**
461 * @return The to top most child window for which {@link LayoutParams#isFullscreen()} returns
462 * true.
463 */
464 WindowState getTopFullscreenWindow() {
465 for (int i = mChildren.size() - 1; i >= 0; i--) {
466 final WindowState win = mChildren.get(i);
467 if (win != null && win.mAttrs.isFullscreen()) {
468 return win;
469 }
470 }
471 return null;
472 }
473
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800474 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700475 WindowState candidate = null;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700476 int j = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800477 while (j > 0) {
478 j--;
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700479 final WindowState win = mChildren.get(j);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700480 final int type = win.mAttrs.type;
481 // No need to loop through child window as base application and starting types can't be
482 // child windows.
483 if (type == TYPE_BASE_APPLICATION || type == TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700484 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900485 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700486 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800487 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700488 candidate = win;
489 } else {
490 return win;
491 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800492 }
493 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700494 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800495 }
496
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800497 boolean windowsAreFocusable() {
Wale Ogunwale3382ab12017-07-27 08:55:03 -0700498 return getWindowConfiguration().canReceiveKeys() || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800499 }
500
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800501 AppWindowContainerController getController() {
502 final WindowContainerController controller = super.getController();
503 return controller != null ? (AppWindowContainerController) controller : null;
504 }
505
Wale Ogunwale571771c2016-08-26 13:18:50 -0700506 @Override
Wale Ogunwale44f21802016-09-02 12:49:48 -0700507 boolean isVisible() {
Wale Ogunwalee471be62016-10-03 07:53:55 -0700508 // If the app token isn't hidden then it is considered visible and there is no need to check
509 // its children windows to see if they are visible.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200510 return !isHidden();
Wale Ogunwale44f21802016-09-02 12:49:48 -0700511 }
512
513 @Override
Wale Ogunwalee287e192017-04-21 09:30:12 -0700514 void removeImmediately() {
515 onRemovedFromDisplay();
516 super.removeImmediately();
517 }
518
519 @Override
Wale Ogunwale571771c2016-08-26 13:18:50 -0700520 void removeIfPossible() {
Craig Mautnere3119b72015-01-20 15:02:36 -0800521 mIsExiting = false;
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800522 removeAllWindowsIfPossible();
Bryce Lee6d410262017-02-28 15:30:17 -0800523 removeImmediately();
Craig Mautnere3119b72015-01-20 15:02:36 -0800524 }
525
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700526 @Override
527 boolean checkCompleteDeferredRemoval() {
528 if (mIsExiting) {
529 removeIfPossible();
530 }
531 return super.checkCompleteDeferredRemoval();
532 }
533
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700534 void onRemovedFromDisplay() {
Wale Ogunwalee287e192017-04-21 09:30:12 -0700535 if (mRemovingFromDisplay) {
536 return;
537 }
538 mRemovingFromDisplay = true;
539
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700540 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app token: " + this);
541
Wale Ogunwale72919d22016-12-08 18:58:50 -0800542 boolean delayed = setVisibility(null, false, TRANSIT_UNSET, true, mVoiceInteraction);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700543
544 mService.mOpeningApps.remove(this);
Jorim Jaggi45ca0542017-05-30 16:16:01 -0700545 mService.mUnknownAppVisibilityController.appRemovedOrHidden(this);
Jorim Jaggi10abe2f2017-01-03 16:44:46 +0100546 mService.mTaskSnapshotController.onAppRemoved(this);
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700547 waitingToShow = false;
548 if (mService.mClosingApps.contains(this)) {
549 delayed = true;
550 } else if (mService.mAppTransition.isTransitionSet()) {
551 mService.mClosingApps.add(this);
552 delayed = true;
553 }
554
555 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app " + this + " delayed=" + delayed
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200556 + " animation=" + getAnimation() + " animating=" + isSelfAnimating());
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700557
558 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM, "removeAppToken: "
559 + this + " delayed=" + delayed + " Callers=" + Debug.getCallers(4));
560
Jorim Jaggi19be6052017-08-03 18:33:43 +0200561 if (startingData != null && getController() != null) {
562 getController().removeStartingWindow();
563 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800564
Winson Chung87e5d552017-04-05 11:49:38 -0700565 // If this window was animating, then we need to ensure that the app transition notifies
566 // that animations have completed in WMS.handleAnimatingStoppedAndTransitionLocked(), so
567 // add to that list now
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200568 if (isSelfAnimating()) {
Winson Chung87e5d552017-04-05 11:49:38 -0700569 mService.mNoAnimationNotifyOnTransitionFinished.add(token);
570 }
571
Wale Ogunwalee287e192017-04-21 09:30:12 -0700572 final TaskStack stack = getStack();
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700573 if (delayed && !isEmpty()) {
574 // set the token aside because it has an active animation to be finished
575 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM,
576 "removeAppToken make exiting: " + this);
Wale Ogunwalee287e192017-04-21 09:30:12 -0700577 if (stack != null) {
578 stack.mExitingAppTokens.add(this);
579 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700580 mIsExiting = true;
581 } else {
582 // Make sure there is no animation running on this token, so any windows associated
583 // with it will be removed as soon as their animations are complete
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200584 cancelAnimation();
Wale Ogunwale3106ae012017-05-16 08:56:37 -0700585 if (stack != null) {
586 stack.mExitingAppTokens.remove(this);
587 }
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700588 removeIfPossible();
589 }
590
591 removed = true;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700592 stopFreezingScreen(true, true);
Bryce Lee6d410262017-02-28 15:30:17 -0800593
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700594 if (mService.mFocusedApp == this) {
595 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "Removing focused app token:" + this);
596 mService.mFocusedApp = null;
597 mService.updateFocusedWindowLocked(UPDATE_FOCUS_NORMAL, true /*updateInputWindows*/);
598 mService.mInputMonitor.setFocusedAppLw(null);
599 }
600
601 if (!delayed) {
602 updateReportedVisibilityLocked();
603 }
Wale Ogunwalee287e192017-04-21 09:30:12 -0700604
605 mRemovingFromDisplay = false;
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700606 }
607
Chong Zhange05bcb12016-07-26 17:47:29 -0700608 void clearAnimatingFlags() {
Chong Zhangb0d26702016-08-12 16:03:29 -0700609 boolean wallpaperMightChange = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700610 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700611 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700612 wallpaperMightChange |= win.clearAnimatingFlags();
Chong Zhange05bcb12016-07-26 17:47:29 -0700613 }
Chong Zhangb0d26702016-08-12 16:03:29 -0700614 if (wallpaperMightChange) {
615 requestUpdateWallpaperIfNeeded();
616 }
Chong Zhange05bcb12016-07-26 17:47:29 -0700617 }
618
Robert Carre12aece2016-02-02 22:43:27 -0800619 void destroySurfaces() {
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700620 destroySurfaces(false /*cleanupOnResume*/);
621 }
622
623 /**
624 * Destroy surfaces which have been marked as eligible by the animator, taking care to ensure
625 * the client has finished with them.
626 *
627 * @param cleanupOnResume whether this is done when app is resumed without fully stopped. If
628 * set to true, destroy only surfaces of removed windows, and clear relevant flags of the
629 * others so that they are ready to be reused. If set to false (common case), destroy all
630 * surfaces that's eligible, if the app is already stopped.
631 */
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700632 private void destroySurfaces(boolean cleanupOnResume) {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700633 boolean destroyedSomething = false;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100634
635 // Copying to a different list as multiple children can be removed.
636 // TODO: Not sure why this is needed.
637 final LinkedList<WindowState> children = new LinkedList<>(mChildren);
638 for (int i = children.size() - 1; i >= 0; i--) {
639 final WindowState win = children.get(i);
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700640 destroyedSomething |= win.destroySurface(cleanupOnResume, mAppStopped);
Robert Carre12aece2016-02-02 22:43:27 -0800641 }
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700642 if (destroyedSomething) {
643 final DisplayContent dc = getDisplayContent();
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700644 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Robert Carre12aece2016-02-02 22:43:27 -0800645 }
646 }
647
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800648 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700649 * Notify that the app is now resumed, and it was not stopped before, perform a clean
650 * up of the surfaces
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800651 */
Jorim Jaggibae01b12017-04-11 16:29:10 -0700652 void notifyAppResumed(boolean wasStopped) {
Chong Zhangad24f962016-08-25 12:12:33 -0700653 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppResumed: wasStopped=" + wasStopped
Jorim Jaggibae01b12017-04-11 16:29:10 -0700654 + " " + this);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700655 mAppStopped = false;
chaviwd3bf08d2017-08-01 17:24:59 -0700656 // Allow the window to turn the screen on once the app is resumed again.
657 setCanTurnScreenOn(true);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700658 if (!wasStopped) {
659 destroySurfaces(true /*cleanupOnResume*/);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800660 }
Robert Carre12aece2016-02-02 22:43:27 -0800661 }
662
Chong Zhangbef461f2015-10-27 11:38:24 -0700663 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700664 * Notify that the app has stopped, and it is okay to destroy any surfaces which were
665 * keeping alive in case they were still being used.
666 */
667 void notifyAppStopped() {
668 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: " + this);
669 mAppStopped = true;
670 destroySurfaces();
671 // Remove any starting window that was added for this app if they are still around.
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800672 if (getController() != null) {
673 getController().removeStartingWindow();
674 }
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700675 }
676
Chong Zhang92147042016-05-09 12:47:11 -0700677 void clearAllDrawn() {
678 allDrawn = false;
679 deferClearAllDrawn = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700680 }
681
Bryce Lee6d410262017-02-28 15:30:17 -0800682 Task getTask() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800683 return (Task) getParent();
Bryce Lee6d410262017-02-28 15:30:17 -0800684 }
685
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800686 TaskStack getStack() {
687 final Task task = getTask();
688 if (task != null) {
689 return task.mStack;
690 } else {
691 return null;
692 }
693 }
694
Bryce Lee6d410262017-02-28 15:30:17 -0800695 @Override
696 void onParentSet() {
697 super.onParentSet();
698
Robert Carred3e83b2017-04-21 13:26:55 -0700699 final Task task = getTask();
700
Bryce Lee6d410262017-02-28 15:30:17 -0800701 // When the associated task is {@code null}, the {@link AppWindowToken} can no longer
702 // access visual elements like the {@link DisplayContent}. We must remove any associations
703 // such as animations.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800704 if (!mReparenting) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800705 if (task == null) {
706 // It is possible we have been marked as a closing app earlier. We must remove ourselves
707 // from this list so we do not participate in any future animations.
708 mService.mClosingApps.remove(this);
Robert Carred3e83b2017-04-21 13:26:55 -0700709 } else if (mLastParent != null && mLastParent.mStack != null) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800710 task.mStack.mExitingAppTokens.remove(this);
711 }
Bryce Lee6d410262017-02-28 15:30:17 -0800712 }
Robert Carred3e83b2017-04-21 13:26:55 -0700713 mLastParent = task;
Bryce Lee6d410262017-02-28 15:30:17 -0800714 }
715
Wale Ogunwalefa854eb2016-09-20 13:43:52 -0700716 void postWindowRemoveStartingWindowCleanup(WindowState win) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700717 // TODO: Something smells about the code below...Is there a better way?
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700718 if (startingWindow == win) {
719 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Notify removed startingWindow " + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800720 if (getController() != null) {
721 getController().removeStartingWindow();
722 }
Wale Ogunwale6c459212017-05-17 08:56:03 -0700723 } else if (mChildren.size() == 0) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700724 // If this is the last window and we had requested a starting transition window,
725 // well there is no point now.
Jorim Jaggi02886a82016-12-06 09:10:06 -0800726 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Nulling last startingData");
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700727 startingData = null;
Wale Ogunwale6c459212017-05-17 08:56:03 -0700728 if (mHiddenSetFromTransferredStartingWindow) {
729 // We set the hidden state to false for the token from a transferred starting window.
730 // We now reset it back to true since the starting window was the last window in the
731 // token.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200732 setHidden(true);
Wale Ogunwale6c459212017-05-17 08:56:03 -0700733 }
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100734 } else if (mChildren.size() == 1 && startingSurface != null && !isRelaunching()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700735 // If this is the last window except for a starting transition window,
736 // we need to get rid of the starting transition.
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200737 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Last window, removing starting window "
738 + win);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800739 if (getController() != null) {
740 getController().removeStartingWindow();
741 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700742 }
743 }
744
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700745 void removeDeadWindows() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700746 for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700747 WindowState win = mChildren.get(winNdx);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800748 if (win.mAppDied) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700749 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.w(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700750 "removeDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800751 // Set mDestroying, we don't want any animation or delayed removal here.
752 win.mDestroying = true;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700753 // Also removes child windows.
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700754 win.removeIfPossible();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800755 }
756 }
757 }
758
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700759 boolean hasWindowsAlive() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700760 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700761 // No need to loop through child windows as the answer should be the same as that of the
762 // parent window.
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700763 if (!(mChildren.get(i)).mAppDied) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700764 return true;
765 }
766 }
767 return false;
768 }
769
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700770 void setWillReplaceWindows(boolean animate) {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700771 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
772 "Marking app token " + this + " with replacing windows.");
Robert Carra1eb4392015-12-10 12:43:51 -0800773
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700774 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700775 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700776 w.setWillReplaceWindow(animate);
Robert Carra1eb4392015-12-10 12:43:51 -0800777 }
Robert Carra1eb4392015-12-10 12:43:51 -0800778 }
779
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700780 void setWillReplaceChildWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700781 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + this
Robert Carr23fa16b2016-01-13 13:19:58 -0800782 + " with replacing child windows.");
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700783 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700784 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700785 w.setWillReplaceChildWindows();
Robert Carr23fa16b2016-01-13 13:19:58 -0800786 }
787 }
788
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700789 void clearWillReplaceWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700790 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
791 "Resetting app token " + this + " of replacing window marks.");
Chong Zhangf596cd52016-01-05 13:42:44 -0800792
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700793 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700794 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700795 w.clearWillReplaceWindow();
Chong Zhangf596cd52016-01-05 13:42:44 -0800796 }
797 }
798
Chong Zhang4d7369a2016-04-25 16:09:14 -0700799 void requestUpdateWallpaperIfNeeded() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700800 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700801 final WindowState w = mChildren.get(i);
Chong Zhang4d7369a2016-04-25 16:09:14 -0700802 w.requestUpdateWallpaperIfNeeded();
803 }
804 }
805
Chong Zhangd78ddb42016-03-02 17:01:14 -0800806 boolean isRelaunching() {
807 return mPendingRelaunchCount > 0;
808 }
809
Robert Carr68375192017-06-13 12:41:53 -0700810 boolean shouldFreezeBounds() {
811 final Task task = getTask();
812
813 // For freeform windows, we can't freeze the bounds at the moment because this would make
814 // the resizing unresponsive.
Wale Ogunwale44f036f2017-09-29 05:09:09 -0700815 if (task == null || task.inFreeformWindowingMode()) {
Robert Carr68375192017-06-13 12:41:53 -0700816 return false;
817 }
818
819 // We freeze the bounds while drag resizing to deal with the time between
820 // the divider/drag handle being released, and the handling it's new
821 // configuration. If we are relaunched outside of the drag resizing state,
822 // we need to be careful not to do this.
823 return getTask().isDragResizing();
824 }
825
Chong Zhangd78ddb42016-03-02 17:01:14 -0800826 void startRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700827 if (shouldFreezeBounds()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -0800828 freezeBounds();
829 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800830
831 // In the process of tearing down before relaunching, the app will
832 // try and clean up it's child surfaces. We need to prevent this from
833 // happening, so we sever the children, transfering their ownership
834 // from the client it-self to the parent surface (owned by us).
835 for (int i = mChildren.size() - 1; i >= 0; i--) {
836 final WindowState w = mChildren.get(i);
837 w.mWinAnimator.detachChildren();
838 }
839
Chong Zhangd78ddb42016-03-02 17:01:14 -0800840 mPendingRelaunchCount++;
841 }
842
843 void finishRelaunching() {
Robert Carr68375192017-06-13 12:41:53 -0700844 unfreezeBounds();
845
Chong Zhangd78ddb42016-03-02 17:01:14 -0800846 if (mPendingRelaunchCount > 0) {
847 mPendingRelaunchCount--;
Bryce Lee081554b2017-05-25 07:52:12 -0700848 } else {
849 // Update keyguard flags upon finishing relaunch.
850 checkKeyguardFlagsChanged();
Chong Zhangd78ddb42016-03-02 17:01:14 -0800851 }
852 }
853
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700854 void clearRelaunching() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700855 if (mPendingRelaunchCount == 0) {
856 return;
857 }
Robert Carr68375192017-06-13 12:41:53 -0700858 unfreezeBounds();
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700859 mPendingRelaunchCount = 0;
860 }
861
Wale Ogunwale07bcab72016-10-14 15:30:09 -0700862 /**
863 * Returns true if the new child window we are adding to this token is considered greater than
864 * the existing child window in this token in terms of z-order.
865 */
866 @Override
867 protected boolean isFirstChildWindowGreaterThanSecond(WindowState newWindow,
868 WindowState existingWindow) {
869 final int type1 = newWindow.mAttrs.type;
870 final int type2 = existingWindow.mAttrs.type;
871
872 // Base application windows should be z-ordered BELOW all other windows in the app token.
873 if (type1 == TYPE_BASE_APPLICATION && type2 != TYPE_BASE_APPLICATION) {
874 return false;
875 } else if (type1 != TYPE_BASE_APPLICATION && type2 == TYPE_BASE_APPLICATION) {
876 return true;
877 }
878
879 // Starting windows should be z-ordered ABOVE all other windows in the app token.
880 if (type1 == TYPE_APPLICATION_STARTING && type2 != TYPE_APPLICATION_STARTING) {
881 return true;
882 } else if (type1 != TYPE_APPLICATION_STARTING && type2 == TYPE_APPLICATION_STARTING) {
883 return false;
884 }
885
886 // Otherwise the new window is greater than the existing window.
887 return true;
888 }
889
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700890 @Override
Robert Carra1eb4392015-12-10 12:43:51 -0800891 void addWindow(WindowState w) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700892 super.addWindow(w);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700893
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700894 boolean gotReplacementWindow = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700895 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700896 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700897 gotReplacementWindow |= candidate.setReplacementWindowIfNeeded(w);
898 }
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700899
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700900 // if we got a replacement window, reset the timeout to give drawing more time
901 if (gotReplacementWindow) {
902 mService.scheduleWindowReplacementTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -0800903 }
Jorim Jaggife762342016-10-13 14:33:27 +0200904 checkKeyguardFlagsChanged();
905 }
906
907 @Override
908 void removeChild(WindowState child) {
909 super.removeChild(child);
910 checkKeyguardFlagsChanged();
Robert Carra1eb4392015-12-10 12:43:51 -0800911 }
912
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700913 private boolean waitingForReplacement() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700914 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700915 final WindowState candidate = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700916 if (candidate.waitingForReplacement()) {
Robert Carra1eb4392015-12-10 12:43:51 -0800917 return true;
918 }
919 }
920 return false;
921 }
922
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700923 void onWindowReplacementTimeout() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700924 for (int i = mChildren.size() - 1; i >= 0; --i) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700925 (mChildren.get(i)).onWindowReplacementTimeout();
Robert Carra1eb4392015-12-10 12:43:51 -0800926 }
927 }
928
Winson Chung30480042017-01-26 10:55:34 -0800929 void reparent(Task task, int position) {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800930 final Task currentTask = getTask();
931 if (task == currentTask) {
Winson Chung30480042017-01-26 10:55:34 -0800932 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800933 "window token=" + this + " already child of task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -0800934 }
Bryce Lee6d410262017-02-28 15:30:17 -0800935
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800936 if (currentTask.mStack != task.mStack) {
Bryce Lee6d410262017-02-28 15:30:17 -0800937 throw new IllegalArgumentException(
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800938 "window token=" + this + " current task=" + currentTask
Bryce Lee6d410262017-02-28 15:30:17 -0800939 + " belongs to a different stack than " + task);
940 }
941
Winson Chung30480042017-01-26 10:55:34 -0800942 if (DEBUG_ADD_REMOVE) Slog.i(TAG, "reParentWindowToken: removing window token=" + this
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800943 + " from task=" + currentTask);
Winson Chung30480042017-01-26 10:55:34 -0800944 final DisplayContent prevDisplayContent = getDisplayContent();
945
Bryce Lee6d410262017-02-28 15:30:17 -0800946 mReparenting = true;
947
Winson Chung30480042017-01-26 10:55:34 -0800948 getParent().removeChild(this);
949 task.addChild(this, position);
950
Bryce Lee6d410262017-02-28 15:30:17 -0800951 mReparenting = false;
952
Winson Chung30480042017-01-26 10:55:34 -0800953 // Relayout display(s).
954 final DisplayContent displayContent = task.getDisplayContent();
955 displayContent.setLayoutNeeded();
956 if (prevDisplayContent != displayContent) {
957 onDisplayChanged(displayContent);
958 prevDisplayContent.setLayoutNeeded();
959 }
960 }
961
Jorim Jaggi0429f352015-12-22 16:29:16 +0100962 /**
963 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
964 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
965 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
966 * with a queue.
967 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800968 private void freezeBounds() {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800969 final Task task = getTask();
970 mFrozenBounds.offer(new Rect(task.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700971
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800972 if (task.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700973 // We didn't call prepareFreezingBounds on the task, so use the current value.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800974 mFrozenMergedConfig.offer(new Configuration(task.getConfiguration()));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700975 } else {
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800976 mFrozenMergedConfig.offer(new Configuration(task.mPreparedFrozenMergedConfig));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700977 }
Andrii Kulianb10330d2016-09-16 13:51:46 -0700978 // Calling unset() to make it equal to Configuration.EMPTY.
Bryce Lee7fbeb8a2017-03-02 08:42:30 -0800979 task.mPreparedFrozenMergedConfig.unset();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100980 }
981
982 /**
983 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
984 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800985 private void unfreezeBounds() {
Robert Carr68375192017-06-13 12:41:53 -0700986 if (mFrozenBounds.isEmpty()) {
987 return;
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700988 }
Robert Carr68375192017-06-13 12:41:53 -0700989 mFrozenBounds.remove();
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700990 if (!mFrozenMergedConfig.isEmpty()) {
991 mFrozenMergedConfig.remove();
992 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700993 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -0700994 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700995 win.onUnfreezeBounds();
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100996 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700997 mService.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100998 }
999
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001000 void setAppLayoutChanges(int changes, String reason) {
1001 if (!mChildren.isEmpty()) {
1002 final DisplayContent dc = getDisplayContent();
1003 dc.pendingLayoutChanges |= changes;
1004 if (DEBUG_LAYOUT_REPEATS) {
1005 mService.mWindowPlacerLocked.debugLayoutRepeats(reason, dc.pendingLayoutChanges);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001006 }
1007 }
1008 }
1009
1010 void removeReplacedWindowIfNeeded(WindowState replacement) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001011 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001012 final WindowState win = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001013 if (win.removeReplacedWindowIfNeeded(replacement)) {
1014 return;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001015 }
1016 }
1017 }
1018
1019 void startFreezingScreen() {
1020 if (DEBUG_ORIENTATION) logWithStack(TAG, "Set freezing of " + appToken + ": hidden="
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001021 + isHidden() + " freezing=" + mAppAnimator.freezingScreen + " hiddenRequested="
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001022 + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001023 if (!hiddenRequested) {
1024 if (!mAppAnimator.freezingScreen) {
1025 mAppAnimator.freezingScreen = true;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001026 mService.registerAppFreezeListener(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001027 mAppAnimator.lastFreezeDuration = 0;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001028 mService.mAppsFreezingScreen++;
1029 if (mService.mAppsFreezingScreen == 1) {
Bryce Lee8f853582017-06-05 17:25:59 -07001030 mService.startFreezingDisplayLocked(false, 0, 0, getDisplayContent());
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001031 mService.mH.removeMessages(H.APP_FREEZE_TIMEOUT);
1032 mService.mH.sendEmptyMessageDelayed(H.APP_FREEZE_TIMEOUT, 2000);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001033 }
1034 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001035 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001036 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001037 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001038 w.onStartFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001039 }
1040 }
1041 }
1042
1043 void stopFreezingScreen(boolean unfreezeSurfaceNow, boolean force) {
1044 if (!mAppAnimator.freezingScreen) {
1045 return;
1046 }
1047 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Clear freezing of " + this + " force=" + force);
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001048 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001049 boolean unfrozeWindows = false;
1050 for (int i = 0; i < count; i++) {
Wale Ogunwaled90546a2016-09-09 23:28:03 -07001051 final WindowState w = mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001052 unfrozeWindows |= w.onStopFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001053 }
1054 if (force || unfrozeWindows) {
1055 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "No longer freezing: " + this);
1056 mAppAnimator.freezingScreen = false;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001057 mService.unregisterAppFreezeListener(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001058 mAppAnimator.lastFreezeDuration =
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001059 (int)(SystemClock.elapsedRealtime() - mService.mDisplayFreezeTime);
1060 mService.mAppsFreezingScreen--;
1061 mService.mLastFinishedFreezeSource = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001062 }
1063 if (unfreezeSurfaceNow) {
1064 if (unfrozeWindows) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001065 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001066 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001067 mService.stopFreezingDisplayLocked();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001068 }
1069 }
1070
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001071 @Override
1072 public void onAppFreezeTimeout() {
1073 Slog.w(TAG_WM, "Force clearing freeze: " + this);
1074 stopFreezingScreen(true, true);
1075 }
1076
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001077 boolean transferStartingWindow(IBinder transferFrom) {
Wale Ogunwale02319a62016-09-26 15:21:22 -07001078 final AppWindowToken fromToken = getDisplayContent().getAppWindowToken(transferFrom);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001079 if (fromToken == null) {
1080 return false;
1081 }
1082
1083 final WindowState tStartingWindow = fromToken.startingWindow;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001084 if (tStartingWindow != null && fromToken.startingSurface != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001085 // In this case, the starting icon has already been displayed, so start
1086 // letting windows get shown immediately without any more transitions.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001087 mService.mSkipAppTransitionAnimation = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001088
1089 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Moving existing starting " + tStartingWindow
1090 + " from " + fromToken + " to " + this);
1091
1092 final long origId = Binder.clearCallingIdentity();
Peter Visontay3556a3b2017-11-01 17:23:17 +00001093 try {
1094 // Transfer the starting window over to the new token.
1095 startingData = fromToken.startingData;
1096 startingSurface = fromToken.startingSurface;
1097 startingDisplayed = fromToken.startingDisplayed;
1098 fromToken.startingDisplayed = false;
1099 startingWindow = tStartingWindow;
1100 reportedVisible = fromToken.reportedVisible;
1101 fromToken.startingData = null;
1102 fromToken.startingSurface = null;
1103 fromToken.startingWindow = null;
1104 fromToken.startingMoved = true;
1105 tStartingWindow.mToken = this;
1106 tStartingWindow.mAppToken = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001107
Peter Visontay3556a3b2017-11-01 17:23:17 +00001108 if (DEBUG_ADD_REMOVE || DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1109 "Removing starting " + tStartingWindow + " from " + fromToken);
1110 fromToken.removeChild(tStartingWindow);
1111 fromToken.postWindowRemoveStartingWindowCleanup(tStartingWindow);
1112 fromToken.mHiddenSetFromTransferredStartingWindow = false;
1113 addWindow(tStartingWindow);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001114
Peter Visontay3556a3b2017-11-01 17:23:17 +00001115 // Propagate other interesting state between the tokens. If the old token is displayed,
1116 // we should immediately force the new one to be displayed. If it is animating, we need
1117 // to move that animation to the new one.
1118 if (fromToken.allDrawn) {
1119 allDrawn = true;
1120 deferClearAllDrawn = fromToken.deferClearAllDrawn;
1121 }
1122 if (fromToken.firstWindowDrawn) {
1123 firstWindowDrawn = true;
1124 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001125 if (!fromToken.isHidden()) {
1126 setHidden(false);
Peter Visontay3556a3b2017-11-01 17:23:17 +00001127 hiddenRequested = false;
1128 mHiddenSetFromTransferredStartingWindow = true;
1129 }
1130 setClientHidden(fromToken.mClientHidden);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001131
Jorim Jaggi980c9de2017-11-17 01:41:37 +01001132 transferAnimation(fromToken);
1133
1134 // When transferring an animation, we no longer need to apply an animation to the
1135 // the token we transfer the animation over. Thus, remove the animation from
1136 // pending opening apps.
1137 mService.mOpeningApps.remove(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001138
Peter Visontay3556a3b2017-11-01 17:23:17 +00001139 mService.updateFocusedWindowLocked(
1140 UPDATE_FOCUS_WILL_PLACE_SURFACES, true /*updateInputWindows*/);
1141 getDisplayContent().setLayoutNeeded();
1142 mService.mWindowPlacerLocked.performSurfacePlacement();
1143 } finally {
1144 Binder.restoreCallingIdentity(origId);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001145 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001146 return true;
1147 } else if (fromToken.startingData != null) {
1148 // The previous app was getting ready to show a
1149 // starting window, but hasn't yet done so. Steal it!
1150 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
1151 "Moving pending starting from " + fromToken + " to " + this);
1152 startingData = fromToken.startingData;
1153 fromToken.startingData = null;
1154 fromToken.startingMoved = true;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001155 if (getController() != null) {
1156 getController().scheduleAddStartingWindow();
1157 }
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001158 return true;
1159 }
1160
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001161 // TODO: Transfer thumbnail
1162
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001163 return false;
1164 }
1165
Wale Ogunwale9bc47732016-08-10 14:44:22 -07001166 boolean isLastWindow(WindowState win) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -07001167 return mChildren.size() == 1 && mChildren.get(0) == win;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -07001168 }
1169
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001170 @Override
1171 void onAppTransitionDone() {
1172 sendingToBottom = false;
1173 }
1174
Wale Ogunwale51362492016-09-08 17:49:17 -07001175 /**
1176 * We override because this class doesn't want its children affecting its reported orientation
1177 * in anyway.
1178 */
1179 @Override
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001180 int getOrientation(int candidate) {
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001181 if (candidate == SCREEN_ORIENTATION_BEHIND) {
1182 // Allow app to specify orientation regardless of its visibility state if the current
1183 // candidate want us to use orientation behind. I.e. the visible app on-top of this one
1184 // wants us to use the orientation of the app behind it.
1185 return mOrientation;
1186 }
1187
Bryce Lee61fbcbc2017-03-10 14:14:03 -08001188 // The {@link AppWindowToken} should only specify an orientation when it is not closing or
1189 // going to the bottom. Allowing closing {@link AppWindowToken} to participate can lead to
1190 // an Activity in another task being started in the wrong orientation during the transition.
Wale Ogunwale5e5a68d2017-03-24 17:36:38 -07001191 if (!(sendingToBottom || mService.mClosingApps.contains(this))
Bryce Lee00d586d2017-07-28 20:48:43 -07001192 && (isVisible() || mService.mOpeningApps.contains(this) || isOnTop())) {
Bryce Leea163b762017-01-24 11:05:01 -08001193 return mOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001194 }
Bryce Leea163b762017-01-24 11:05:01 -08001195
1196 return SCREEN_ORIENTATION_UNSET;
Wale Ogunwale51362492016-09-08 17:49:17 -07001197 }
1198
Wale Ogunwaleb198b742016-12-01 08:44:09 -08001199 /** Returns the app's preferred orientation regardless of its currently visibility state. */
1200 int getOrientationIgnoreVisibility() {
1201 return mOrientation;
1202 }
1203
Craig Mautnerdbb79912012-03-01 18:59:14 -08001204 @Override
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001205 void checkAppWindowsReadyToShow() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001206 if (allDrawn == mAppAnimator.allDrawn) {
1207 return;
1208 }
1209
1210 mAppAnimator.allDrawn = allDrawn;
1211 if (!allDrawn) {
1212 return;
1213 }
1214
1215 // The token has now changed state to having all windows shown... what to do, what to do?
1216 if (mAppAnimator.freezingScreen) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001217 showAllWindowsLocked();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001218 stopFreezingScreen(false, true);
1219 if (DEBUG_ORIENTATION) Slog.i(TAG,
1220 "Setting mOrientationChangeComplete=true because wtoken " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001221 + " numInteresting=" + mNumInterestingWindows + " numDrawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001222 // This will set mOrientationChangeComplete and cause a pass through layout.
1223 setAppLayoutChanges(FINISH_LAYOUT_REDO_WALLPAPER,
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001224 "checkAppWindowsReadyToShow: freezingScreen");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001225 } else {
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -07001226 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM, "checkAppWindowsReadyToShow");
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001227
1228 // We can now show all of the drawn windows!
1229 if (!mService.mOpeningApps.contains(this)) {
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001230 showAllWindowsLocked();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001231 }
1232 }
1233 }
1234
Matthew Ng5d23afa2017-06-21 16:16:24 -07001235 /**
Bryce Leed390deb2017-06-22 13:14:28 -07001236 * Returns whether the drawn window states of this {@link AppWindowToken} has considered every
1237 * child {@link WindowState}. A child is considered if it has been passed into
1238 * {@link #updateDrawnWindowStates(WindowState)} after being added. This is used to determine
1239 * whether states, such as {@code allDrawn}, can be set, which relies on state variables such as
1240 * {@code mNumInterestingWindows}, which depend on all {@link WindowState}s being considered.
1241 *
1242 * @return {@code true} If all children have been considered, {@code false}.
1243 */
1244 private boolean allDrawnStatesConsidered() {
Bryce Lee6311c4b2017-07-06 14:09:29 -07001245 for (int i = mChildren.size() - 1; i >= 0; --i) {
1246 final WindowState child = mChildren.get(i);
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001247 if (child.mightAffectAllDrawn() && !child.getDrawnStateEvaluated()) {
Bryce Leed390deb2017-06-22 13:14:28 -07001248 return false;
1249 }
1250 }
1251 return true;
1252 }
1253
1254 /**
Matthew Ng5d23afa2017-06-21 16:16:24 -07001255 * Determines if the token has finished drawing. This should only be called from
1256 * {@link DisplayContent#applySurfaceChangesTransaction}
1257 */
Matthew Ng498c71d2017-04-18 13:55:45 -07001258 void updateAllDrawn() {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001259 if (!allDrawn) {
Matthew Ng498c71d2017-04-18 13:55:45 -07001260 // Number of drawn windows can be less when a window is being relaunched, wait for
Bryce Leed390deb2017-06-22 13:14:28 -07001261 // all windows to be launched and drawn for this token be considered all drawn.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001262 final int numInteresting = mNumInterestingWindows;
Bryce Leed390deb2017-06-22 13:14:28 -07001263
1264 // We must make sure that all present children have been considered (determined by
1265 // {@link #allDrawnStatesConsidered}) before evaluating whether everything has been
1266 // drawn.
1267 if (numInteresting > 0 && allDrawnStatesConsidered()
1268 && mNumDrawnWindows >= numInteresting && !isRelaunching()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001269 if (DEBUG_VISIBILITY) Slog.v(TAG, "allDrawn: " + this
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001270 + " interesting=" + numInteresting + " drawn=" + mNumDrawnWindows);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001271 allDrawn = true;
1272 // Force an additional layout pass where
1273 // WindowStateAnimator#commitFinishDrawingLocked() will call performShowLocked().
Matthew Ng498c71d2017-04-18 13:55:45 -07001274 if (mDisplayContent != null) {
1275 mDisplayContent.setLayoutNeeded();
1276 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001277 mService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
Robert Carrecc06b32017-04-18 14:25:10 -07001278
Winson Chunge7ba6862017-05-24 12:13:33 -07001279 // Notify the pinned stack upon all windows drawn. If there was an animation in
1280 // progress then this signal will resume that animation.
Wale Ogunwale61911492017-10-11 08:50:50 -07001281 final TaskStack pinnedStack = mDisplayContent.getPinnedStack();
Winson Chunge7ba6862017-05-24 12:13:33 -07001282 if (pinnedStack != null) {
1283 pinnedStack.onAllWindowsDrawn();
Robert Carrecc06b32017-04-18 14:25:10 -07001284 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001285 }
1286 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001287 }
1288
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001289 /**
1290 * Updated this app token tracking states for interesting and drawn windows based on the window.
1291 *
1292 * @return Returns true if the input window is considered interesting and drawn while all the
1293 * windows in this app token where not considered drawn as of the last pass.
1294 */
1295 boolean updateDrawnWindowStates(WindowState w) {
Bryce Leed390deb2017-06-22 13:14:28 -07001296 w.setDrawnStateEvaluated(true /*evaluated*/);
1297
Jorim Jaggie4b0f282017-05-17 15:10:29 +02001298 if (DEBUG_STARTING_WINDOW_VERBOSE && w == startingWindow) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001299 Slog.d(TAG, "updateWindows: starting " + w + " isOnScreen=" + w.isOnScreen()
1300 + " allDrawn=" + allDrawn + " freezingScreen=" + mAppAnimator.freezingScreen);
1301 }
1302
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001303 if (allDrawn && !mAppAnimator.freezingScreen) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001304 return false;
1305 }
1306
1307 if (mLastTransactionSequence != mService.mTransactionSequence) {
1308 mLastTransactionSequence = mService.mTransactionSequence;
1309 mNumInterestingWindows = mNumDrawnWindows = 0;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001310 startingDisplayed = false;
1311 }
1312
1313 final WindowStateAnimator winAnimator = w.mWinAnimator;
1314
1315 boolean isInterestingAndDrawn = false;
1316
Jorim Jaggie7d2b852017-08-28 17:55:15 +02001317 if (!allDrawn && w.mightAffectAllDrawn()) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001318 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) {
1319 Slog.v(TAG, "Eval win " + w + ": isDrawn=" + w.isDrawnLw()
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001320 + ", isAnimationSet=" + isSelfAnimating());
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001321 if (!w.isDrawnLw()) {
1322 Slog.v(TAG, "Not displayed: s=" + winAnimator.mSurfaceController
1323 + " pv=" + w.mPolicyVisibility
1324 + " mDrawState=" + winAnimator.drawStateToString()
1325 + " ph=" + w.isParentWindowHidden() + " th=" + hiddenRequested
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001326 + " a=" + isSelfAnimating());
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001327 }
1328 }
1329
1330 if (w != startingWindow) {
1331 if (w.isInteresting()) {
1332 mNumInterestingWindows++;
1333 if (w.isDrawnLw()) {
1334 mNumDrawnWindows++;
1335
1336 if (DEBUG_VISIBILITY || DEBUG_ORIENTATION) Slog.v(TAG, "tokenMayBeDrawn: "
1337 + this + " w=" + w + " numInteresting=" + mNumInterestingWindows
1338 + " freezingScreen=" + mAppAnimator.freezingScreen
1339 + " mAppFreezing=" + w.mAppFreezing);
1340
1341 isInterestingAndDrawn = true;
1342 }
1343 }
1344 } else if (w.isDrawnLw()) {
Jorim Jaggi3878ca32017-02-02 17:13:05 -08001345 if (getController() != null) {
1346 getController().reportStartingWindowDrawn();
1347 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001348 startingDisplayed = true;
1349 }
1350 }
1351
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001352 return isInterestingAndDrawn;
1353 }
1354
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001355 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001356 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001357 // For legacy reasons we process the TaskStack.mExitingAppTokens first in DisplayContent
1358 // before the non-exiting app tokens. So, we skip the exiting app tokens here.
1359 // TODO: Investigate if we need to continue to do this or if we can just process them
1360 // in-order.
1361 if (mIsExiting && !waitingForReplacement()) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001362 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001363 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001364 return forAllWindowsUnchecked(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001365 }
1366
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08001367 boolean forAllWindowsUnchecked(ToBooleanFunction<WindowState> callback,
1368 boolean traverseTopToBottom) {
1369 return super.forAllWindows(callback, traverseTopToBottom);
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07001370 }
1371
1372 @Override
Wale Ogunwale2049dbf2016-08-02 21:05:23 -07001373 AppWindowToken asAppWindowToken() {
1374 // I am an app window token!
1375 return this;
1376 }
1377
1378 @Override
Wale Ogunwale51362492016-09-08 17:49:17 -07001379 boolean fillsParent() {
1380 return mFillsParent;
1381 }
1382
1383 void setFillsParent(boolean fillsParent) {
1384 mFillsParent = fillsParent;
1385 }
1386
Jorim Jaggife762342016-10-13 14:33:27 +02001387 boolean containsDismissKeyguardWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001388 // Window state is transient during relaunch. We are not guaranteed to be frozen during the
1389 // entirety of the relaunch.
1390 if (isRelaunching()) {
1391 return mLastContainsDismissKeyguardWindow;
1392 }
1393
Jorim Jaggife762342016-10-13 14:33:27 +02001394 for (int i = mChildren.size() - 1; i >= 0; i--) {
1395 if ((mChildren.get(i).mAttrs.flags & FLAG_DISMISS_KEYGUARD) != 0) {
1396 return true;
1397 }
1398 }
1399 return false;
1400 }
1401
1402 boolean containsShowWhenLockedWindow() {
Bryce Lee081554b2017-05-25 07:52:12 -07001403 // When we are relaunching, it is possible for us to be unfrozen before our previous
1404 // windows have been added back. Using the cached value ensures that our previous
1405 // showWhenLocked preference is honored until relaunching is complete.
1406 if (isRelaunching()) {
1407 return mLastContainsShowWhenLockedWindow;
1408 }
1409
Jorim Jaggife762342016-10-13 14:33:27 +02001410 for (int i = mChildren.size() - 1; i >= 0; i--) {
1411 if ((mChildren.get(i).mAttrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
1412 return true;
1413 }
1414 }
Bryce Lee081554b2017-05-25 07:52:12 -07001415
Jorim Jaggife762342016-10-13 14:33:27 +02001416 return false;
1417 }
1418
1419 void checkKeyguardFlagsChanged() {
1420 final boolean containsDismissKeyguard = containsDismissKeyguardWindow();
1421 final boolean containsShowWhenLocked = containsShowWhenLockedWindow();
1422 if (containsDismissKeyguard != mLastContainsDismissKeyguardWindow
1423 || containsShowWhenLocked != mLastContainsShowWhenLockedWindow) {
1424 mService.notifyKeyguardFlagsChanged(null /* callback */);
1425 }
1426 mLastContainsDismissKeyguardWindow = containsDismissKeyguard;
1427 mLastContainsShowWhenLockedWindow = containsShowWhenLocked;
1428 }
1429
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001430 WindowState getImeTargetBelowWindow(WindowState w) {
1431 final int index = mChildren.indexOf(w);
1432 if (index > 0) {
1433 final WindowState target = mChildren.get(index - 1);
1434 if (target.canBeImeTarget()) {
1435 return target;
1436 }
1437 }
1438 return null;
1439 }
1440
Winson Chungbe9be7f2017-06-28 10:55:22 -07001441 int getLowestAnimLayer() {
1442 for (int i = 0; i < mChildren.size(); i++) {
1443 final WindowState w = mChildren.get(i);
1444 if (w.mRemoved) {
1445 continue;
1446 }
1447 return w.mWinAnimator.mAnimLayer;
1448 }
1449 return Integer.MAX_VALUE;
1450 }
1451
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001452 WindowState getHighestAnimLayerWindow(WindowState currentTarget) {
1453 WindowState candidate = null;
1454 for (int i = mChildren.indexOf(currentTarget); i >= 0; i--) {
1455 final WindowState w = mChildren.get(i);
1456 if (w.mRemoved) {
1457 continue;
1458 }
1459 if (candidate == null || w.mWinAnimator.mAnimLayer >
1460 candidate.mWinAnimator.mAnimLayer) {
1461 candidate = w;
1462 }
1463 }
1464 return candidate;
1465 }
1466
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001467 /**
1468 * See {@link Activity#setDisablePreviewScreenshots}.
1469 */
1470 void setDisablePreviewScreenshots(boolean disable) {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001471 mDisablePreviewScreenshots = disable;
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001472 }
1473
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001474 /**
chaviwd3bf08d2017-08-01 17:24:59 -07001475 * Sets whether the current launch can turn the screen on. See {@link #canTurnScreenOn()}
1476 */
1477 void setCanTurnScreenOn(boolean canTurnScreenOn) {
1478 mCanTurnScreenOn = canTurnScreenOn;
1479 }
1480
1481 /**
1482 * Indicates whether the current launch can turn the screen on. This is to prevent multiple
1483 * relayouts from turning the screen back on. The screen should only turn on at most
1484 * once per activity resume.
1485 *
1486 * @return true if the screen can be turned on.
1487 */
1488 boolean canTurnScreenOn() {
1489 return mCanTurnScreenOn;
1490 }
1491
1492 /**
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001493 * Retrieves whether we'd like to generate a snapshot that's based solely on the theme. This is
1494 * the case when preview screenshots are disabled {@link #setDisablePreviewScreenshots} or when
1495 * we can't take a snapshot for other reasons, for example, if we have a secure window.
1496 *
1497 * @return True if we need to generate an app theme snapshot, false if we'd like to take a real
1498 * screenshot.
1499 */
1500 boolean shouldUseAppThemeSnapshot() {
Wale Ogunwale6c459212017-05-17 08:56:03 -07001501 return mDisablePreviewScreenshots || forAllWindows(w -> (w.mAttrs.flags & FLAG_SECURE) != 0,
Jorim Jaggid635a4a2017-05-03 15:21:26 +02001502 true /* topToBottom */);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +01001503 }
1504
Wale Ogunwale51362492016-09-08 17:49:17 -07001505 @Override
Robert Carrdee1b3f2017-02-27 11:33:33 -08001506 int getAnimLayerAdjustment() {
1507 return mAppAnimator.animLayerAdjustment;
1508 }
1509
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001510 boolean applyAnimationLocked(WindowManager.LayoutParams lp, int transit, boolean enter,
1511 boolean isVoiceInteraction) {
1512
1513 if (mService.mDisableTransitionAnimation) {
1514 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) {
1515 Slog.v(TAG_WM, "applyAnimation: transition animation is disabled. atoken=" + this);
1516 }
1517 cancelAnimation();
1518 return false;
1519 }
1520
1521 // Only apply an animation if the display isn't frozen. If it is frozen, there is no reason
1522 // to animate and it can cause strange artifacts when we unfreeze the display if some
1523 // different animation is running.
1524 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "AWT#applyAnimationLocked");
1525 if (okToAnimate()) {
1526 final Animation a = loadAnimation(lp, transit, enter, isVoiceInteraction);
1527 if (a != null) {
1528 final AnimationAdapter adapter = new LocalAnimationAdapter(
1529 new WindowAnimationSpec(a, new Point()), mService.mSurfaceAnimationRunner);
1530 startAnimation(getPendingTransaction(), adapter, !isVisible());
1531 if (a.getZAdjustment() == Animation.ZORDER_TOP) {
1532 mAppAnimator.animLayerAdjustment = 1;
1533 getDisplayContent().assignWindowLayers(false /* setLayoutNeeded */);
1534 }
1535 mTransit = transit;
1536 mTransitFlags = mService.mAppTransition.getTransitFlags();
1537 // TODO: Skip first frame and app stack clip mode.
1538 }
1539 } else {
1540 cancelAnimation();
1541 }
1542 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
1543
1544 return isReallyAnimating();
1545 }
1546
1547 private Animation loadAnimation(WindowManager.LayoutParams lp, int transit, boolean enter,
1548 boolean isVoiceInteraction) {
1549 final DisplayContent displayContent = getTask().getDisplayContent();
1550 final DisplayInfo displayInfo = displayContent.getDisplayInfo();
1551 final int width = displayInfo.appWidth;
1552 final int height = displayInfo.appHeight;
1553 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.v(TAG_WM,
1554 "applyAnimation: atoken=" + this);
1555
1556 // Determine the visible rect to calculate the thumbnail clip
1557 final WindowState win = findMainWindow();
1558 final Rect frame = new Rect(0, 0, width, height);
1559 final Rect displayFrame = new Rect(0, 0,
1560 displayInfo.logicalWidth, displayInfo.logicalHeight);
1561 final Rect insets = new Rect();
1562 final Rect stableInsets = new Rect();
1563 Rect surfaceInsets = null;
1564 final boolean freeform = win != null && win.inFreeformWindowingMode();
1565 if (win != null) {
1566 // Containing frame will usually cover the whole screen, including dialog windows.
1567 // For freeform workspace windows it will not cover the whole screen and it also
1568 // won't exactly match the final freeform window frame (e.g. when overlapping with
1569 // the status bar). In that case we need to use the final frame.
1570 if (freeform) {
1571 frame.set(win.mFrame);
1572 } else {
1573 frame.set(win.mContainingFrame);
1574 }
1575 surfaceInsets = win.getAttrs().surfaceInsets;
1576 insets.set(win.mContentInsets);
1577 stableInsets.set(win.mStableInsets);
1578 }
1579
1580 if (mLaunchTaskBehind) {
1581 // Differentiate the two animations. This one which is briefly on the screen
1582 // gets the !enter animation, and the other activity which remains on the
1583 // screen gets the enter animation. Both appear in the mOpeningApps set.
1584 enter = false;
1585 }
1586 if (DEBUG_APP_TRANSITIONS) Slog.d(TAG_WM, "Loading animation for app transition."
1587 + " transit=" + AppTransition.appTransitionToString(transit) + " enter=" + enter
1588 + " frame=" + frame + " insets=" + insets + " surfaceInsets=" + surfaceInsets);
1589 final Configuration displayConfig = displayContent.getConfiguration();
1590 final Animation a = mService.mAppTransition.loadAnimation(lp, transit, enter,
1591 displayConfig.uiMode, displayConfig.orientation, frame, displayFrame, insets,
1592 surfaceInsets, stableInsets, isVoiceInteraction, freeform, getTask().mTaskId);
1593 if (a != null) {
1594 if (DEBUG_ANIM) logWithStack(TAG, "Loaded animation " + a + " for " + this);
1595 final int containingWidth = frame.width();
1596 final int containingHeight = frame.height();
1597 a.initialize(containingWidth, containingHeight, width, height);
1598 a.scaleCurrentDuration(mService.getTransitionAnimationScaleLocked());
1599 }
1600 return a;
1601 }
1602
1603 /**
1604 * This must be called while inside a transaction.
1605 */
1606 void showAllWindowsLocked() {
1607 forAllWindows(windowState -> {
1608 if (DEBUG_VISIBILITY) Slog.v(TAG, "performing show on: " + windowState);
1609 windowState.performShowLocked();
1610 }, false /* traverseTopToBottom */);
Jorim Jaggia5e10572017-11-15 14:36:26 +01001611 }
1612
1613 @Override
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001614 protected void onAnimationFinished() {
1615 super.onAnimationFinished();
1616
1617 mTransit = TRANSIT_UNSET;
1618 mTransitFlags = 0;
1619 mAppAnimator.animLayerAdjustment = 0;
1620
1621 setAppLayoutChanges(FINISH_LAYOUT_REDO_ANIM | FINISH_LAYOUT_REDO_WALLPAPER,
1622 "AppWindowToken");
1623
Jorim Jaggi988f6682017-11-17 17:46:43 +01001624 clearThumbnail();
1625
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001626 if (mService.mInputMethodTarget != null && mService.mInputMethodTarget.mAppToken == this) {
1627 getDisplayContent().computeImeTarget(true /* updateImeTarget */);
1628 }
1629
1630 if (DEBUG_ANIM) Slog.v(TAG, "Animation done in " + this
1631 + ": reportedVisible=" + reportedVisible
1632 + " okToDisplay=" + okToDisplay()
1633 + " okToAnimate=" + okToAnimate()
1634 + " startingDisplayed=" + startingDisplayed);
1635
1636 // WindowState.onExitAnimationDone might modify the children list, so make a copy and then
1637 // traverse the copy.
1638 final ArrayList<WindowState> children = new ArrayList<>(mChildren);
1639 children.forEach(WindowState::onExitAnimationDone);
1640
1641 mService.mAppTransition.notifyAppTransitionFinishedLocked(token);
1642 scheduleAnimation();
1643 }
1644
1645 @Override
1646 boolean isAppAnimating() {
1647 return isSelfAnimating();
1648 }
1649
1650 @Override
1651 boolean isSelfAnimating() {
1652 // If we are about to start a transition, we also need to be considered animating.
1653 return isWaitingForTransitionStart() || isReallyAnimating();
1654 }
1655
1656 /**
1657 * @return True if and only if we are actually running an animation. Note that
1658 * {@link #isSelfAnimating} also returns true if we are waiting for an animation to
1659 * start.
1660 */
1661 private boolean isReallyAnimating() {
1662 return super.isSelfAnimating();
1663 }
1664
Jorim Jaggi988f6682017-11-17 17:46:43 +01001665 @Override
1666 void cancelAnimation() {
1667 super.cancelAnimation();
1668 clearThumbnail();
1669 }
1670
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001671 boolean isWaitingForTransitionStart() {
1672 return mService.mAppTransition.isTransitionSet()
1673 && (mService.mOpeningApps.contains(this) || mService.mClosingApps.contains(this));
1674 }
1675
1676 public int getTransit() {
1677 return mTransit;
1678 }
1679
1680 int getTransitFlags() {
1681 return mTransitFlags;
1682 }
1683
Jorim Jaggi988f6682017-11-17 17:46:43 +01001684 void attachThumbnailAnimation() {
1685 if (!isReallyAnimating()) {
1686 return;
1687 }
1688 final int taskId = getTask().mTaskId;
1689 final GraphicBuffer thumbnailHeader =
1690 mService.mAppTransition.getAppTransitionThumbnailHeader(taskId);
1691 if (thumbnailHeader == null) {
1692 if (DEBUG_APP_TRANSITIONS) Slog.d(TAG, "No thumbnail header bitmap for: " + taskId);
1693 return;
1694 }
1695 clearThumbnail();
1696 mThumbnail = new AppWindowThumbnail(getPendingTransaction(), this, thumbnailHeader);
1697 mThumbnail.startAnimation(getPendingTransaction(), loadThumbnailAnimation(thumbnailHeader));
1698 }
1699
1700 private Animation loadThumbnailAnimation(GraphicBuffer thumbnailHeader) {
1701 final DisplayInfo displayInfo = mDisplayContent.getDisplayInfo();
1702
1703 // If this is a multi-window scenario, we use the windows frame as
1704 // destination of the thumbnail header animation. If this is a full screen
1705 // window scenario, we use the whole display as the target.
1706 WindowState win = findMainWindow();
1707 Rect appRect = win != null ? win.getContentFrameLw() :
1708 new Rect(0, 0, displayInfo.appWidth, displayInfo.appHeight);
1709 Rect insets = win != null ? win.mContentInsets : null;
1710 final Configuration displayConfig = mDisplayContent.getConfiguration();
1711 return mService.mAppTransition.createThumbnailAspectScaleAnimationLocked(
1712 appRect, insets, thumbnailHeader, getTask().mTaskId, displayConfig.uiMode,
1713 displayConfig.orientation);
1714 }
1715
1716 private void clearThumbnail() {
1717 if (mThumbnail == null) {
1718 return;
1719 }
1720 mThumbnail.destroy();
1721 mThumbnail = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001722 }
1723
1724 @Override
1725 void dump(PrintWriter pw, String prefix, boolean dumpAll) {
1726 super.dump(pw, prefix, dumpAll);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001727 if (appToken != null) {
Wale Ogunwale72919d22016-12-08 18:58:50 -08001728 pw.println(prefix + "app=true mVoiceInteraction=" + mVoiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001729 }
Bryce Lee7fbeb8a2017-03-02 08:42:30 -08001730 pw.print(prefix); pw.print("task="); pw.println(getTask());
Wale Ogunwale51362492016-09-08 17:49:17 -07001731 pw.print(prefix); pw.print(" mFillsParent="); pw.print(mFillsParent);
1732 pw.print(" mOrientation="); pw.println(mOrientation);
Wale Ogunwale89973222017-04-23 18:39:45 -07001733 pw.println(prefix + "hiddenRequested=" + hiddenRequested + " mClientHidden=" + mClientHidden
1734 + ((mDeferHidingClient) ? " mDeferHidingClient=" + mDeferHidingClient : "")
1735 + " reportedDrawn=" + reportedDrawn + " reportedVisible=" + reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -07001736 if (paused) {
1737 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001738 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -08001739 if (mAppStopped) {
1740 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
1741 }
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001742 if (mNumInterestingWindows != 0 || mNumDrawnWindows != 0
Craig Mautner6fbda632012-07-03 09:26:39 -07001743 || allDrawn || mAppAnimator.allDrawn) {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001744 pw.print(prefix); pw.print("mNumInterestingWindows=");
1745 pw.print(mNumInterestingWindows);
1746 pw.print(" mNumDrawnWindows="); pw.print(mNumDrawnWindows);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001747 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -07001748 pw.print(" allDrawn="); pw.print(allDrawn);
1749 pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
1750 pw.println(")");
1751 }
1752 if (inPendingTransaction) {
1753 pw.print(prefix); pw.print("inPendingTransaction=");
1754 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001755 }
Craig Mautner799bc1d2015-01-14 10:33:48 -08001756 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001757 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
1758 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -08001759 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -08001760 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001761 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001762 if (startingWindow != null || startingSurface != null
Wale Ogunwale6c459212017-05-17 08:56:03 -07001763 || startingDisplayed || startingMoved || mHiddenSetFromTransferredStartingWindow) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001764 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -08001765 pw.print(" startingSurface="); pw.print(startingSurface);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001766 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale6c459212017-05-17 08:56:03 -07001767 pw.print(" startingMoved="); pw.print(startingMoved);
1768 pw.println(" mHiddenSetFromTransferredStartingWindow="
1769 + mHiddenSetFromTransferredStartingWindow);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001770 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001771 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -08001772 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001773 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -08001774 }
1775 if (mPendingRelaunchCount != 0) {
1776 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +01001777 }
Wale Ogunwale9c64cb62017-04-12 13:39:59 -07001778 if (getController() != null) {
1779 pw.print(prefix); pw.print("controller="); pw.println(getController());
1780 }
Wale Ogunwalee287e192017-04-21 09:30:12 -07001781 if (mRemovingFromDisplay) {
1782 pw.println(prefix + "mRemovingFromDisplay=" + mRemovingFromDisplay);
1783 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001784 }
1785
1786 @Override
1787 void setHidden(boolean hidden) {
1788 super.setHidden(hidden);
1789 scheduleAnimation();
1790 }
1791
1792 @Override
1793 void prepareSurfaces() {
1794 // isSelfAnimating also returns true when we are about to start a transition, so we need
1795 // to check super here.
1796 final boolean reallyAnimating = super.isSelfAnimating();
1797 final boolean show = !isHidden() || reallyAnimating;
1798 if (show && !mLastSurfaceShowing) {
1799 mPendingTransaction.show(mSurfaceControl);
1800 } else if (!show && mLastSurfaceShowing) {
1801 mPendingTransaction.hide(mSurfaceControl);
Jorim Jaggi4d1835d2017-08-31 17:28:27 +02001802 }
Jorim Jaggi988f6682017-11-17 17:46:43 +01001803 if (mThumbnail != null) {
1804 mThumbnail.setShowing(mPendingTransaction, show);
1805 }
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001806 mLastSurfaceShowing = show;
1807 super.prepareSurfaces();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001808 }
1809
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001810 @CallSuper
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001811 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02001812 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07001813 final long token = proto.start(fieldId);
1814 writeNameToProto(proto, NAME);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001815 super.writeToProto(proto, WINDOW_TOKEN, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001816 proto.end(token);
1817 }
1818
1819 void writeNameToProto(ProtoOutputStream proto, long fieldId) {
1820 if (appToken == null) {
1821 return;
1822 }
1823 try {
1824 proto.write(fieldId, appToken.getName());
1825 } catch (RemoteException e) {
1826 // This shouldn't happen, but in this case fall back to outputting nothing
1827 Slog.e(TAG, e.toString());
1828 }
1829 }
1830
1831 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001832 public String toString() {
1833 if (stringName == null) {
1834 StringBuilder sb = new StringBuilder();
1835 sb.append("AppWindowToken{");
1836 sb.append(Integer.toHexString(System.identityHashCode(this)));
1837 sb.append(" token="); sb.append(token); sb.append('}');
1838 stringName = sb.toString();
1839 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001840 return stringName + ((mIsExiting) ? " mIsExiting=" : "");
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001841 }
Jeff Browne9bdb312012-04-05 15:30:10 -07001842}