blob: b773a4e016bdae1fcd596b737bcb04d21e9c16f8 [file] [log] [blame]
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale6cae7652015-12-26 07:36:26 -080019import static android.app.ActivityManager.StackId;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080020import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080021import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
22import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Robert Carra1eb4392015-12-10 12:43:51 -080023import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale9017ec02016-02-25 08:55:25 -080024import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080025import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
26import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
27import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
28import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Chong Zhangf596cd52016-01-05 13:42:44 -080029import static com.android.server.wm.WindowManagerService.WINDOW_REPLACEMENT_TIMEOUT_DURATION;
Chong Zhang92147042016-05-09 12:47:11 -070030import static com.android.server.wm.WindowManagerService.H.NOTIFY_ACTIVITY_DRAWN;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080031
Jeff Brown4532e612012-04-05 14:27:12 -070032import com.android.server.input.InputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080033import com.android.server.wm.WindowManagerService.H;
34
Filip Gruszczynskia590c992015-11-25 16:45:26 -080035import android.annotation.NonNull;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080036import android.content.pm.ActivityInfo;
Jorim Jaggi26c8c422016-05-09 19:57:25 -070037import android.content.res.Configuration;
Jorim Jaggi0429f352015-12-22 16:29:16 +010038import android.graphics.Rect;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080039import android.os.Message;
40import android.os.RemoteException;
41import android.util.Slog;
42import android.view.IApplicationToken;
43import android.view.View;
44import android.view.WindowManager;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080045
46import java.io.PrintWriter;
Jorim Jaggi0429f352015-12-22 16:29:16 +010047import java.util.ArrayDeque;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080048import java.util.ArrayList;
49
50class AppTokenList extends ArrayList<AppWindowToken> {
51}
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080052
53/**
54 * Version of WindowToken that is specifically for a particular application (or
55 * really activity) that is displaying windows.
56 */
Craig Mautnere32c3072012-03-12 15:25:35 -070057class AppWindowToken extends WindowToken {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080058 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowToken" : TAG_WM;
59
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080060 // Non-null only for application tokens.
61 final IApplicationToken appToken;
62
63 // All of the windows and child windows that are included in this
64 // application token. Note this list is NOT sorted!
Craig Mautner96868332012-12-04 14:29:11 -080065 final WindowList allAppWindows = new WindowList();
Filip Gruszczynskia590c992015-11-25 16:45:26 -080066 @NonNull final AppWindowAnimator mAppAnimator;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070067
Dianne Hackborne30e02f2014-05-27 18:24:45 -070068 final boolean voiceInteraction;
69
Craig Mautner83162a92015-01-26 14:43:30 -080070 Task mTask;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080071 boolean appFullscreen;
72 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Craig Mautner4c5eb222013-11-18 12:59:05 -080073 boolean layoutConfigChanges;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070074 boolean showForAllUsers;
Craig Mautnera2c77052012-03-26 12:14:43 -070075
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080076 // The input dispatching timeout for this application token in nanoseconds.
77 long inputDispatchingTimeoutNanos;
78
79 // These are used for determining when all windows associated with
80 // an activity have been drawn, so they can be made visible together
81 // at the same time.
Craig Mautner764983d2012-03-22 11:37:36 -070082 // initialize so that it doesn't match mTransactionSequence which is an int.
83 long lastTransactionSequence = Long.MIN_VALUE;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080084 int numInterestingWindows;
85 int numDrawnWindows;
86 boolean inPendingTransaction;
87 boolean allDrawn;
Craig Mautner7636dfb2012-11-16 15:24:11 -080088 // Set to true when this app creates a surface while in the middle of an animation. In that
89 // case do not clear allDrawn until the animation completes.
90 boolean deferClearAllDrawn;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080091
Chong Zhang8e4bda92016-05-04 15:08:18 -070092 // These are to track the app's real drawing status if there were no saved surfaces.
93 boolean allDrawnExcludingSaved;
94 int numInterestingWindowsExcludingSaved;
95 int numDrawnWindowsExclusingSaved;
96
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080097 // Is this window's surface needed? This is almost like hidden, except
98 // it will sometimes be true a little earlier: when the token has
99 // been shown, but is still waiting for its app transition to execute
100 // before making its windows shown.
101 boolean hiddenRequested;
102
103 // Have we told the window clients to hide themselves?
104 boolean clientHidden;
105
106 // Last visibility state we reported to the app token.
107 boolean reportedVisible;
108
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700109 // Last drawn state we reported to the app token.
110 boolean reportedDrawn;
111
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800112 // Set to true when the token has been removed from the window mgr.
113 boolean removed;
114
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800115 // Information about an application starting window if displayed.
116 StartingData startingData;
117 WindowState startingWindow;
118 View startingView;
119 boolean startingDisplayed;
120 boolean startingMoved;
121 boolean firstWindowDrawn;
122
123 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700124 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800125
Craig Mautner799bc1d2015-01-14 10:33:48 -0800126 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800127
Craig Mautnerbb742462014-07-07 15:28:55 -0700128 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700129 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700130
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800131 boolean mAlwaysFocusable;
132
Robert Carre12aece2016-02-02 22:43:27 -0800133 boolean mAppStopped;
Chong Zhangd78ddb42016-03-02 17:01:14 -0800134 int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800135
Jorim Jaggi0429f352015-12-22 16:29:16 +0100136 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700137 ArrayDeque<Configuration> mFrozenMergedConfig = new ArrayDeque<>();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100138
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700139 AppWindowToken(WindowManagerService _service, IApplicationToken _token,
140 boolean _voiceInteraction) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800141 super(_service, _token.asBinder(),
142 WindowManager.LayoutParams.TYPE_APPLICATION, true);
143 appWindowToken = this;
144 appToken = _token;
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700145 voiceInteraction = _voiceInteraction;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800146 mInputApplicationHandle = new InputApplicationHandle(this);
Craig Mautner322e4032012-07-13 13:35:20 -0700147 mAppAnimator = new AppWindowAnimator(this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800148 }
149
150 void sendAppVisibilityToClients() {
151 final int N = allAppWindows.size();
152 for (int i=0; i<N; i++) {
153 WindowState win = allAppWindows.get(i);
154 if (win == startingWindow && clientHidden) {
155 // Don't hide the starting window.
156 continue;
157 }
158 try {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800159 if (DEBUG_VISIBILITY) Slog.v(TAG,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800160 "Setting visibility of " + win + ": " + (!clientHidden));
161 win.mClient.dispatchAppVisibility(!clientHidden);
162 } catch (RemoteException e) {
163 }
164 }
165 }
166
Chong Zhang92147042016-05-09 12:47:11 -0700167 void setVisibleBeforeClientHidden() {
168 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
169 final WindowState w = allAppWindows.get(i);
170 w.setVisibleBeforeClientHidden();
171 }
172 }
173
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800174 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
175 firstWindowDrawn = true;
176
177 // We now have a good window to show, remove dead placeholders
178 removeAllDeadWindows();
179
180 if (startingData != null) {
181 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
182 + win.mToken + ": first real window is shown, no animation");
183 // If this initial window is animating, stop it -- we will do an animation to reveal
184 // it from behind the starting window, so there is no need for it to also be doing its
185 // own stuff.
186 winAnimator.clearAnimation();
187 winAnimator.mService.mFinishedStarting.add(this);
188 winAnimator.mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
189 }
190 updateReportedVisibilityLocked();
191 }
192
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800193 void updateReportedVisibilityLocked() {
194 if (appToken == null) {
195 return;
196 }
197
198 int numInteresting = 0;
199 int numVisible = 0;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700200 int numDrawn = 0;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800201 boolean nowGone = true;
202
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800203 if (DEBUG_VISIBILITY) Slog.v(TAG,
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700204 "Update reported visibility: " + this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800205 final int N = allAppWindows.size();
206 for (int i=0; i<N; i++) {
207 WindowState win = allAppWindows.get(i);
208 if (win == startingWindow || win.mAppFreezing
209 || win.mViewVisibility != View.VISIBLE
210 || win.mAttrs.type == TYPE_APPLICATION_STARTING
211 || win.mDestroying) {
212 continue;
213 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800214 if (DEBUG_VISIBILITY) {
215 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800216 + win.isDrawnLw()
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700217 + ", isAnimationSet=" + win.mWinAnimator.isAnimationSet());
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800218 if (!win.isDrawnLw()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800219 Slog.v(TAG, "Not displayed: s=" +
Robert Carre6a83512015-11-03 16:09:21 -0800220 win.mWinAnimator.mSurfaceController
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800221 + " pv=" + win.mPolicyVisibility
Craig Mautner749a7bb2012-04-02 13:49:53 -0700222 + " mDrawState=" + win.mWinAnimator.mDrawState
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800223 + " ah=" + win.mAttachedHidden
224 + " th="
225 + (win.mAppToken != null
226 ? win.mAppToken.hiddenRequested : false)
Craig Mautnera2c77052012-03-26 12:14:43 -0700227 + " a=" + win.mWinAnimator.mAnimating);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800228 }
229 }
230 numInteresting++;
231 if (win.isDrawnLw()) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700232 numDrawn++;
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700233 if (!win.mWinAnimator.isAnimationSet()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800234 numVisible++;
235 }
236 nowGone = false;
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700237 } else if (win.mWinAnimator.isAnimationSet()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800238 nowGone = false;
239 }
240 }
241
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700242 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800243 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700244 if (!nowGone) {
245 // If the app is not yet gone, then it can only become visible/drawn.
246 if (!nowDrawn) {
247 nowDrawn = reportedDrawn;
248 }
249 if (!nowVisible) {
250 nowVisible = reportedVisible;
251 }
252 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800253 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800254 + numInteresting + " visible=" + numVisible);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700255 if (nowDrawn != reportedDrawn) {
256 if (nowDrawn) {
257 Message m = service.mH.obtainMessage(
258 H.REPORT_APPLICATION_TOKEN_DRAWN, this);
259 service.mH.sendMessage(m);
260 }
261 reportedDrawn = nowDrawn;
262 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800263 if (nowVisible != reportedVisible) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800264 if (DEBUG_VISIBILITY) Slog.v(
265 TAG, "Visibility changed in " + this
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800266 + ": vis=" + nowVisible);
267 reportedVisible = nowVisible;
268 Message m = service.mH.obtainMessage(
269 H.REPORT_APPLICATION_TOKEN_WINDOWS,
270 nowVisible ? 1 : 0,
271 nowGone ? 1 : 0,
272 this);
273 service.mH.sendMessage(m);
274 }
275 }
276
277 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700278 WindowState candidate = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800279 int j = windows.size();
280 while (j > 0) {
281 j--;
282 WindowState win = windows.get(j);
283 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
284 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700285 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900286 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700287 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800288 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700289 candidate = win;
290 } else {
291 return win;
292 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800293 }
294 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700295 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800296 }
297
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800298 boolean windowsAreFocusable() {
299 return StackId.canReceiveKeys(mTask.mStack.mStackId) || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800300 }
301
Craig Mautner72669d12012-12-18 17:23:54 -0800302 boolean isVisible() {
303 final int N = allAppWindows.size();
Craig Mautner72669d12012-12-18 17:23:54 -0800304 for (int i=0; i<N; i++) {
305 WindowState win = allAppWindows.get(i);
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700306 // If we're animating with a saved surface, we're already visible.
307 // Return true so that the alpha doesn't get cleared.
Craig Mautner72669d12012-12-18 17:23:54 -0800308 if (!win.mAppFreezing
Chong Zhang92147042016-05-09 12:47:11 -0700309 && (win.mViewVisibility == View.VISIBLE || win.isAnimatingWithSavedSurface()
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700310 || (win.mWinAnimator.isAnimationSet()
Filip Gruszczynski57f76f12015-11-04 16:10:54 -0800311 && !service.mAppTransition.isTransitionSet()))
312 && !win.mDestroying
313 && win.isDrawnLw()) {
Craig Mautner72669d12012-12-18 17:23:54 -0800314 return true;
315 }
316 }
317 return false;
318 }
319
Craig Mautnere3119b72015-01-20 15:02:36 -0800320 void removeAppFromTaskLocked() {
321 mIsExiting = false;
322 removeAllWindows();
323
Craig Mautner83162a92015-01-26 14:43:30 -0800324 // Use local variable because removeAppToken will null out mTask.
325 final Task task = mTask;
Craig Mautnere3119b72015-01-20 15:02:36 -0800326 if (task != null) {
327 if (!task.removeAppToken(this)) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800328 Slog.e(TAG, "removeAppFromTaskLocked: token=" + this
Craig Mautnere3119b72015-01-20 15:02:36 -0800329 + " not found.");
330 }
331 task.mStack.mExitingAppTokens.remove(this);
332 }
333 }
334
Robert Carre12aece2016-02-02 22:43:27 -0800335 // Here we destroy surfaces which have been marked as eligible by the animator, taking care
336 // to ensure the client has finished with them. If the client could still be using them
337 // we will skip destruction and try again when the client has stopped.
338 void destroySurfaces() {
339 final ArrayList<WindowState> allWindows = (ArrayList<WindowState>) allAppWindows.clone();
340 final DisplayContentList displayList = new DisplayContentList();
341 for (int i = allWindows.size() - 1; i >= 0; i--) {
342 final WindowState win = allWindows.get(i);
Chong Zhangeb665572016-05-09 18:28:27 -0700343
344 if (!(mAppStopped || win.mWindowRemovalAllowed)) {
Robert Carre12aece2016-02-02 22:43:27 -0800345 continue;
346 }
347
Chong Zhangeb665572016-05-09 18:28:27 -0700348 win.mWinAnimator.destroyPreservedSurfaceLocked();
349
350 if (!win.mDestroying) {
Chong Zhang5471e902016-02-12 15:34:10 -0800351 continue;
Robert Carre12aece2016-02-02 22:43:27 -0800352 }
353
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800354 if (DEBUG_ADD_REMOVE) Slog.e(TAG_WM, "win=" + win
355 + " destroySurfaces: mAppStopped=" + mAppStopped
356 + " win.mWindowRemovalAllowed=" + win.mWindowRemovalAllowed
357 + " win.mRemoveOnExit=" + win.mRemoveOnExit);
358
Robert Carre12aece2016-02-02 22:43:27 -0800359 win.destroyOrSaveSurface();
360 if (win.mRemoveOnExit) {
Robert Carre12aece2016-02-02 22:43:27 -0800361 service.removeWindowInnerLocked(win);
362 }
363 final DisplayContent displayContent = win.getDisplayContent();
364 if (displayContent != null && !displayList.contains(displayContent)) {
365 displayList.add(displayContent);
366 }
367 win.mDestroying = false;
368 }
369 for (int i = 0; i < displayList.size(); i++) {
370 final DisplayContent displayContent = displayList.get(i);
371 service.mLayersController.assignLayersLocked(displayContent.getWindowList());
372 displayContent.layoutNeeded = true;
373 }
374 }
375
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800376 /**
377 * If the application has stopped it is okay to destroy any surfaces which were keeping alive
378 * in case they were still being used.
379 */
380 void notifyAppStopped(boolean stopped) {
381 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: stopped=" + stopped + " " + this);
382 mAppStopped = stopped;
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800383
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800384 if (stopped) {
385 destroySurfaces();
386 // Remove any starting window that was added for this app if they are still around.
387 mTask.mService.scheduleRemoveStartingWindowLocked(this);
388 }
Robert Carre12aece2016-02-02 22:43:27 -0800389 }
390
Chong Zhangbef461f2015-10-27 11:38:24 -0700391 /**
392 * Checks whether we should save surfaces for this app.
393 *
394 * @return true if the surfaces should be saved, false otherwise.
395 */
396 boolean shouldSaveSurface() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800397 // We want to save surface if the app's windows are "allDrawn".
398 // (If we started entering animation early with saved surfaces, allDrawn
399 // should have been restored to true. So we'll save again in that case
400 // even if app didn't actually finish drawing.)
401 return allDrawn;
Robert Carr13f7be9e2015-12-02 18:39:45 -0800402 }
403
Chong Zhang92147042016-05-09 12:47:11 -0700404 boolean canRestoreSurfaces() {
Chong Zhangb7b4a562016-04-28 15:30:33 -0700405 for (int i = allAppWindows.size() -1; i >= 0; i--) {
Chong Zhang92147042016-05-09 12:47:11 -0700406 final WindowState w = allAppWindows.get(i);
407 if (w.canRestoreSurface()) {
Robert Carr13f7be9e2015-12-02 18:39:45 -0800408 return true;
409 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700410 }
411 return false;
412 }
413
Chong Zhang92147042016-05-09 12:47:11 -0700414 void clearVisibleBeforeClientHidden() {
415 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
416 final WindowState w = allAppWindows.get(i);
417 w.clearVisibleBeforeClientHidden();
418 }
419 }
420
Chong Zhang8e4bda92016-05-04 15:08:18 -0700421 /**
422 * Whether the app has some window that is invisible in layout, but
423 * animating with saved surface.
424 */
425 boolean isAnimatingInvisibleWithSavedSurface() {
426 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
427 final WindowState w = allAppWindows.get(i);
428 if (w.isAnimatingInvisibleWithSavedSurface()) {
429 return true;
430 }
431 }
432 return false;
433 }
434
435 /**
436 * Hide all window surfaces that's still invisible in layout but animating
437 * with a saved surface, and mark them destroying.
438 */
439 void stopUsingSavedSurfaceLocked() {
440 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
441 final WindowState w = allAppWindows.get(i);
442 if (w.isAnimatingInvisibleWithSavedSurface()) {
443 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
444 "stopUsingSavedSurfaceLocked: " + w);
445 w.clearAnimatingWithSavedSurface();
446 w.mDestroying = true;
447 w.mWinAnimator.hide("stopUsingSavedSurfaceLocked");
448 w.mWinAnimator.mWallpaperControllerLocked.hideWallpapers(w);
449 }
450 }
451 destroySurfaces();
452 }
453
Chong Zhangbef461f2015-10-27 11:38:24 -0700454 void restoreSavedSurfaces() {
Chong Zhang92147042016-05-09 12:47:11 -0700455 if (!canRestoreSurfaces()) {
456 clearVisibleBeforeClientHidden();
Chong Zhangbef461f2015-10-27 11:38:24 -0700457 return;
458 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800459 // Check if we have enough drawn windows to mark allDrawn= true.
460 int numInteresting = 0;
461 int numDrawn = 0;
Chong Zhangb7b4a562016-04-28 15:30:33 -0700462 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
463 WindowState w = allAppWindows.get(i);
Chong Zhang92147042016-05-09 12:47:11 -0700464 if (w != startingWindow && !w.mAppDied && w.wasVisibleBeforeClientHidden()
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800465 && (!mAppAnimator.freezingScreen || !w.mAppFreezing)) {
466 numInteresting++;
Chong Zhang92147042016-05-09 12:47:11 -0700467 if (w.hasSavedSurface()) {
468 w.restoreSavedSurface();
469 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800470 if (w.isDrawnLw()) {
471 numDrawn++;
472 }
473 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700474 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800475
Chong Zhang92147042016-05-09 12:47:11 -0700476 if (!allDrawn) {
477 allDrawn = (numInteresting > 0) && (numInteresting == numDrawn);
478 if (allDrawn) {
479 service.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
480 }
481 }
482 clearVisibleBeforeClientHidden();
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800483
484 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
Chong Zhang92147042016-05-09 12:47:11 -0700485 "restoreSavedSurfaces: " + appWindowToken + " allDrawn=" + allDrawn
486 + " numInteresting=" + numInteresting + " numDrawn=" + numDrawn);
Chong Zhangbef461f2015-10-27 11:38:24 -0700487 }
488
489 void destroySavedSurfaces() {
Chong Zhangb7b4a562016-04-28 15:30:33 -0700490 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
491 WindowState win = allAppWindows.get(i);
Robert Carr13f7be9e2015-12-02 18:39:45 -0800492 win.destroySavedSurface();
Chong Zhangbef461f2015-10-27 11:38:24 -0700493 }
Chong Zhang92147042016-05-09 12:47:11 -0700494 }
495
496 void clearAllDrawn() {
497 allDrawn = false;
498 deferClearAllDrawn = false;
Chong Zhang8e4bda92016-05-04 15:08:18 -0700499 allDrawnExcludingSaved = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700500 }
501
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800502 @Override
Craig Mautner7c9ee192014-08-14 16:08:26 -0700503 void removeAllWindows() {
Craig Mautner7b4655d2014-11-20 12:13:22 -0800504 for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
505 // removeWindowLocked at bottom of loop may remove multiple entries from
506 // allAppWindows if the window to be removed has child windows. It also may
507 // not remove any windows from allAppWindows at all if win is exiting and
508 // currently animating away. This ensures that winNdx is monotonically decreasing
509 // and never beyond allAppWindows bounds.
510 winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
511 WindowState win = allAppWindows.get(winNdx);
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800512 if (DEBUG_WINDOW_MOVEMENT) {
513 Slog.w(TAG, "removeAllWindows: removing win=" + win);
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800514 }
515
Wale Ogunwalea6ab5c42015-04-24 09:00:25 -0700516 service.removeWindowLocked(win);
Craig Mautner7c9ee192014-08-14 16:08:26 -0700517 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800518 allAppWindows.clear();
519 windows.clear();
Craig Mautner7c9ee192014-08-14 16:08:26 -0700520 }
521
Chong Zhang112eb8c2015-11-02 11:17:00 -0800522 void removeAllDeadWindows() {
523 for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700524 // removeWindowLocked at bottom of loop may remove multiple entries from
525 // allAppWindows if the window to be removed has child windows. It also may
526 // not remove any windows from allAppWindows at all if win is exiting and
527 // currently animating away. This ensures that winNdx is monotonically decreasing
528 // and never beyond allAppWindows bounds.
529 winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800530 WindowState win = allAppWindows.get(winNdx);
531 if (win.mAppDied) {
Wale Ogunwale2728bf42016-03-03 11:03:26 -0800532 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800533 Slog.w(TAG, "removeAllDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800534 }
535 // Set mDestroying, we don't want any animation or delayed removal here.
536 win.mDestroying = true;
537 service.removeWindowLocked(win);
538 }
539 }
540 }
541
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700542 boolean hasWindowsAlive() {
543 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
544 if (!allAppWindows.get(i).mAppDied) {
545 return true;
546 }
547 }
548 return false;
549 }
550
Robert Carra1eb4392015-12-10 12:43:51 -0800551 void setReplacingWindows(boolean animate) {
552 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + appWindowToken
553 + " with replacing windows.");
554
555 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
556 final WindowState w = allAppWindows.get(i);
557 w.setReplacing(animate);
558 }
559 if (animate) {
560 // Set-up dummy animation so we can start treating windows associated with this
561 // token like they are in transition before the new app window is ready for us to
562 // run the real transition animation.
563 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
564 "setReplacingWindow() Setting dummy animation on: " + this);
565 mAppAnimator.setDummyAnimation();
566 }
567 }
568
Robert Carr23fa16b2016-01-13 13:19:58 -0800569 void setReplacingChildren() {
570 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + appWindowToken
571 + " with replacing child windows.");
572 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
573 final WindowState w = allAppWindows.get(i);
Robert Carrd1a010f2016-04-07 22:36:22 -0700574 if (w.shouldBeReplacedWithChildren()) {
Robert Carr23fa16b2016-01-13 13:19:58 -0800575 w.setReplacing(false /* animate */);
576 }
577 }
578 }
579
Chong Zhangf596cd52016-01-05 13:42:44 -0800580 void resetReplacingWindows() {
581 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Resetting app token " + appWindowToken
582 + " of replacing window marks.");
583
584 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
585 final WindowState w = allAppWindows.get(i);
586 w.resetReplacing();
587 }
588 }
589
Chong Zhang4d7369a2016-04-25 16:09:14 -0700590 void requestUpdateWallpaperIfNeeded() {
591 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
592 final WindowState w = allAppWindows.get(i);
593 w.requestUpdateWallpaperIfNeeded();
594 }
595 }
596
Chong Zhangd78ddb42016-03-02 17:01:14 -0800597 boolean isRelaunching() {
598 return mPendingRelaunchCount > 0;
599 }
600
601 void startRelaunching() {
602 if (canFreezeBounds()) {
603 freezeBounds();
604 }
605 mPendingRelaunchCount++;
606 }
607
608 void finishRelaunching() {
609 if (canFreezeBounds()) {
610 unfreezeBounds();
611 }
612 if (mPendingRelaunchCount > 0) {
613 mPendingRelaunchCount--;
614 }
615 }
616
Robert Carra1eb4392015-12-10 12:43:51 -0800617 void addWindow(WindowState w) {
618 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
619 WindowState candidate = allAppWindows.get(i);
620 if (candidate.mWillReplaceWindow && candidate.mReplacingWindow == null &&
Robert Carr8bc89072016-04-08 13:29:59 -0700621 candidate.getWindowTag().toString().equals(w.getWindowTag().toString())) {
Robert Carra1eb4392015-12-10 12:43:51 -0800622 candidate.mReplacingWindow = w;
Robert Carrb439a632016-04-07 22:52:10 -0700623 w.mSkipEnterAnimationForSeamlessReplacement = !candidate.mAnimateReplacingWindow;
Chong Zhangf596cd52016-01-05 13:42:44 -0800624
625 // if we got a replacement window, reset the timeout to give drawing more time
Chong Zhang32de3652016-05-12 16:00:01 -0700626 service.scheduleReplacingWindowTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -0800627 }
628 }
629 allAppWindows.add(w);
630 }
631
632 boolean waitingForReplacement() {
633 for (int i = allAppWindows.size() -1; i >= 0; i--) {
634 WindowState candidate = allAppWindows.get(i);
635 if (candidate.mWillReplaceWindow) {
636 return true;
637 }
638 }
639 return false;
640 }
641
Chong Zhangf596cd52016-01-05 13:42:44 -0800642 void clearTimedoutReplacesLocked() {
Robert Carra1eb4392015-12-10 12:43:51 -0800643 for (int i = allAppWindows.size() - 1; i >= 0;
644 // removeWindowLocked at bottom of loop may remove multiple entries from
645 // allAppWindows if the window to be removed has child windows. It also may
646 // not remove any windows from allAppWindows at all if win is exiting and
647 // currently animating away. This ensures that winNdx is monotonically decreasing
648 // and never beyond allAppWindows bounds.
649 i = Math.min(i - 1, allAppWindows.size() - 1)) {
650 WindowState candidate = allAppWindows.get(i);
651 if (candidate.mWillReplaceWindow == false) {
652 continue;
653 }
654 candidate.mWillReplaceWindow = false;
Robert Carrb439a632016-04-07 22:52:10 -0700655 if (candidate.mReplacingWindow != null) {
656 candidate.mReplacingWindow.mSkipEnterAnimationForSeamlessReplacement = false;
657 }
Chong Zhangf596cd52016-01-05 13:42:44 -0800658 // Since the window already timed out, remove it immediately now.
659 // Use removeWindowInnerLocked() instead of removeWindowLocked(), as the latter
660 // delays removal on certain conditions, which will leave the stale window in the
661 // stack and marked mWillReplaceWindow=false, so the window will never be removed.
662 service.removeWindowInnerLocked(candidate);
Robert Carra1eb4392015-12-10 12:43:51 -0800663 }
664 }
665
Chong Zhangd78ddb42016-03-02 17:01:14 -0800666 private boolean canFreezeBounds() {
667 // For freeform windows, we can't freeze the bounds at the moment because this would make
668 // the resizing unresponsive.
669 return mTask != null && !mTask.inFreeformWorkspace();
670 }
671
Jorim Jaggi0429f352015-12-22 16:29:16 +0100672 /**
673 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
674 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
675 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
676 * with a queue.
677 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800678 private void freezeBounds() {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100679 mFrozenBounds.offer(new Rect(mTask.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700680
681 if (mTask.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
682 // We didn't call prepareFreezingBounds on the task, so use the current value.
683 final Configuration config = new Configuration(service.mCurConfiguration);
684 config.updateFrom(mTask.mOverrideConfig);
685 mFrozenMergedConfig.offer(config);
686 } else {
687 mFrozenMergedConfig.offer(new Configuration(mTask.mPreparedFrozenMergedConfig));
688 }
689 mTask.mPreparedFrozenMergedConfig.setToDefaults();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100690 }
691
692 /**
693 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
694 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800695 private void unfreezeBounds() {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100696 mFrozenBounds.remove();
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700697 mFrozenMergedConfig.remove();
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100698 for (int i = windows.size() - 1; i >= 0; i--) {
699 final WindowState win = windows.get(i);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800700 if (!win.mHasSurface) {
701 continue;
702 }
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100703 win.mLayoutNeeded = true;
704 win.setDisplayLayoutNeeded();
705 if (!service.mResizingWindows.contains(win)) {
706 service.mResizingWindows.add(win);
707 }
708 }
709 service.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100710 }
711
Craig Mautnerdbb79912012-03-01 18:59:14 -0800712 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800713 void dump(PrintWriter pw, String prefix) {
714 super.dump(pw, prefix);
715 if (appToken != null) {
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700716 pw.print(prefix); pw.print("app=true voiceInteraction="); pw.println(voiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800717 }
718 if (allAppWindows.size() > 0) {
719 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
720 }
Craig Mautner83162a92015-01-26 14:43:30 -0800721 pw.print(prefix); pw.print("task="); pw.println(mTask);
722 pw.print(prefix); pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800723 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
724 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
725 pw.print(" clientHidden="); pw.print(clientHidden);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700726 pw.print(" reportedDrawn="); pw.print(reportedDrawn);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800727 pw.print(" reportedVisible="); pw.println(reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -0700728 if (paused) {
729 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800730 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800731 if (mAppStopped) {
732 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
733 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800734 if (numInterestingWindows != 0 || numDrawnWindows != 0
Craig Mautner6fbda632012-07-03 09:26:39 -0700735 || allDrawn || mAppAnimator.allDrawn) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800736 pw.print(prefix); pw.print("numInterestingWindows=");
737 pw.print(numInterestingWindows);
738 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
739 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -0700740 pw.print(" allDrawn="); pw.print(allDrawn);
741 pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
742 pw.println(")");
743 }
744 if (inPendingTransaction) {
745 pw.print(prefix); pw.print("inPendingTransaction=");
746 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800747 }
Craig Mautner799bc1d2015-01-14 10:33:48 -0800748 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800749 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
750 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -0800751 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -0800752 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800753 }
754 if (startingWindow != null || startingView != null
755 || startingDisplayed || startingMoved) {
756 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
757 pw.print(" startingView="); pw.print(startingView);
758 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800759 pw.print(" startingMoved="); pw.println(startingMoved);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800760 }
Jorim Jaggi0429f352015-12-22 16:29:16 +0100761 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -0800762 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700763 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -0800764 }
765 if (mPendingRelaunchCount != 0) {
766 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +0100767 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800768 }
769
770 @Override
771 public String toString() {
772 if (stringName == null) {
773 StringBuilder sb = new StringBuilder();
774 sb.append("AppWindowToken{");
775 sb.append(Integer.toHexString(System.identityHashCode(this)));
776 sb.append(" token="); sb.append(token); sb.append('}');
777 stringName = sb.toString();
778 }
779 return stringName;
780 }
Jeff Browne9bdb312012-04-05 15:30:10 -0700781}