blob: 2731f42991a538dce458e9ef96c9705fc0b3d96b [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;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080030
Jeff Brown4532e612012-04-05 14:27:12 -070031import com.android.server.input.InputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080032import com.android.server.wm.WindowManagerService.H;
33
Filip Gruszczynskia590c992015-11-25 16:45:26 -080034import android.annotation.NonNull;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080035import android.content.pm.ActivityInfo;
Jorim Jaggi0429f352015-12-22 16:29:16 +010036import android.graphics.Rect;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080037import android.os.Message;
38import android.os.RemoteException;
39import android.util.Slog;
40import android.view.IApplicationToken;
41import android.view.View;
42import android.view.WindowManager;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080043
44import java.io.PrintWriter;
Jorim Jaggi0429f352015-12-22 16:29:16 +010045import java.util.ArrayDeque;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080046import java.util.ArrayList;
47
48class AppTokenList extends ArrayList<AppWindowToken> {
49}
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080050
51/**
52 * Version of WindowToken that is specifically for a particular application (or
53 * really activity) that is displaying windows.
54 */
Craig Mautnere32c3072012-03-12 15:25:35 -070055class AppWindowToken extends WindowToken {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080056 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowToken" : TAG_WM;
57
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080058 // Non-null only for application tokens.
59 final IApplicationToken appToken;
60
61 // All of the windows and child windows that are included in this
62 // application token. Note this list is NOT sorted!
Craig Mautner96868332012-12-04 14:29:11 -080063 final WindowList allAppWindows = new WindowList();
Filip Gruszczynskia590c992015-11-25 16:45:26 -080064 @NonNull final AppWindowAnimator mAppAnimator;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070065
Dianne Hackborne30e02f2014-05-27 18:24:45 -070066 final boolean voiceInteraction;
67
Chong Zhangdb20b5f2015-10-23 14:01:43 -070068 // Whether we're performing an entering animation with a saved surface.
69 boolean mAnimatingWithSavedSurface;
70
Jorim Jaggi4846ee32016-01-07 17:39:12 +010071
Craig Mautner83162a92015-01-26 14:43:30 -080072 Task mTask;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080073 boolean appFullscreen;
74 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Craig Mautner4c5eb222013-11-18 12:59:05 -080075 boolean layoutConfigChanges;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070076 boolean showForAllUsers;
Craig Mautnera2c77052012-03-26 12:14:43 -070077
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080078 // The input dispatching timeout for this application token in nanoseconds.
79 long inputDispatchingTimeoutNanos;
80
81 // These are used for determining when all windows associated with
82 // an activity have been drawn, so they can be made visible together
83 // at the same time.
Craig Mautner764983d2012-03-22 11:37:36 -070084 // initialize so that it doesn't match mTransactionSequence which is an int.
85 long lastTransactionSequence = Long.MIN_VALUE;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080086 int numInterestingWindows;
87 int numDrawnWindows;
88 boolean inPendingTransaction;
89 boolean allDrawn;
Craig Mautner7636dfb2012-11-16 15:24:11 -080090 // Set to true when this app creates a surface while in the middle of an animation. In that
91 // case do not clear allDrawn until the animation completes.
92 boolean deferClearAllDrawn;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080093
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080094 // Is this window's surface needed? This is almost like hidden, except
95 // it will sometimes be true a little earlier: when the token has
96 // been shown, but is still waiting for its app transition to execute
97 // before making its windows shown.
98 boolean hiddenRequested;
99
100 // Have we told the window clients to hide themselves?
101 boolean clientHidden;
102
103 // Last visibility state we reported to the app token.
104 boolean reportedVisible;
105
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700106 // Last drawn state we reported to the app token.
107 boolean reportedDrawn;
108
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800109 // Set to true when the token has been removed from the window mgr.
110 boolean removed;
111
Chong Zhang112eb8c2015-11-02 11:17:00 -0800112 boolean appDied;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800113 // Information about an application starting window if displayed.
114 StartingData startingData;
115 WindowState startingWindow;
116 View startingView;
117 boolean startingDisplayed;
118 boolean startingMoved;
119 boolean firstWindowDrawn;
120
121 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700122 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800123
Craig Mautner799bc1d2015-01-14 10:33:48 -0800124 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800125
Craig Mautnerbb742462014-07-07 15:28:55 -0700126 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700127 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700128
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800129 boolean mAlwaysFocusable;
130
Robert Carre12aece2016-02-02 22:43:27 -0800131 boolean mAppStopped;
132
Jorim Jaggi0429f352015-12-22 16:29:16 +0100133 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
134
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700135 AppWindowToken(WindowManagerService _service, IApplicationToken _token,
136 boolean _voiceInteraction) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800137 super(_service, _token.asBinder(),
138 WindowManager.LayoutParams.TYPE_APPLICATION, true);
139 appWindowToken = this;
140 appToken = _token;
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700141 voiceInteraction = _voiceInteraction;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800142 mInputApplicationHandle = new InputApplicationHandle(this);
Craig Mautner322e4032012-07-13 13:35:20 -0700143 mAppAnimator = new AppWindowAnimator(this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800144 }
145
146 void sendAppVisibilityToClients() {
147 final int N = allAppWindows.size();
148 for (int i=0; i<N; i++) {
149 WindowState win = allAppWindows.get(i);
150 if (win == startingWindow && clientHidden) {
151 // Don't hide the starting window.
152 continue;
153 }
154 try {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800155 if (DEBUG_VISIBILITY) Slog.v(TAG,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800156 "Setting visibility of " + win + ": " + (!clientHidden));
157 win.mClient.dispatchAppVisibility(!clientHidden);
158 } catch (RemoteException e) {
159 }
160 }
161 }
162
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800163 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
164 firstWindowDrawn = true;
165
166 // We now have a good window to show, remove dead placeholders
167 removeAllDeadWindows();
168
169 if (startingData != null) {
170 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
171 + win.mToken + ": first real window is shown, no animation");
172 // If this initial window is animating, stop it -- we will do an animation to reveal
173 // it from behind the starting window, so there is no need for it to also be doing its
174 // own stuff.
175 winAnimator.clearAnimation();
176 winAnimator.mService.mFinishedStarting.add(this);
177 winAnimator.mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
178 }
179 updateReportedVisibilityLocked();
180 }
181
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800182 void updateReportedVisibilityLocked() {
183 if (appToken == null) {
184 return;
185 }
186
187 int numInteresting = 0;
188 int numVisible = 0;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700189 int numDrawn = 0;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800190 boolean nowGone = true;
191
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800192 if (DEBUG_VISIBILITY) Slog.v(TAG,
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700193 "Update reported visibility: " + this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800194 final int N = allAppWindows.size();
195 for (int i=0; i<N; i++) {
196 WindowState win = allAppWindows.get(i);
197 if (win == startingWindow || win.mAppFreezing
198 || win.mViewVisibility != View.VISIBLE
199 || win.mAttrs.type == TYPE_APPLICATION_STARTING
200 || win.mDestroying) {
201 continue;
202 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800203 if (DEBUG_VISIBILITY) {
204 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800205 + win.isDrawnLw()
Craig Mautnera2c77052012-03-26 12:14:43 -0700206 + ", isAnimating=" + win.mWinAnimator.isAnimating());
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800207 if (!win.isDrawnLw()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800208 Slog.v(TAG, "Not displayed: s=" +
Robert Carre6a83512015-11-03 16:09:21 -0800209 win.mWinAnimator.mSurfaceController
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800210 + " pv=" + win.mPolicyVisibility
Craig Mautner749a7bb2012-04-02 13:49:53 -0700211 + " mDrawState=" + win.mWinAnimator.mDrawState
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800212 + " ah=" + win.mAttachedHidden
213 + " th="
214 + (win.mAppToken != null
215 ? win.mAppToken.hiddenRequested : false)
Craig Mautnera2c77052012-03-26 12:14:43 -0700216 + " a=" + win.mWinAnimator.mAnimating);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800217 }
218 }
219 numInteresting++;
220 if (win.isDrawnLw()) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700221 numDrawn++;
Craig Mautnera2c77052012-03-26 12:14:43 -0700222 if (!win.mWinAnimator.isAnimating()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800223 numVisible++;
224 }
225 nowGone = false;
Craig Mautnera2c77052012-03-26 12:14:43 -0700226 } else if (win.mWinAnimator.isAnimating()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800227 nowGone = false;
228 }
229 }
230
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700231 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800232 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700233 if (!nowGone) {
234 // If the app is not yet gone, then it can only become visible/drawn.
235 if (!nowDrawn) {
236 nowDrawn = reportedDrawn;
237 }
238 if (!nowVisible) {
239 nowVisible = reportedVisible;
240 }
241 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800242 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800243 + numInteresting + " visible=" + numVisible);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700244 if (nowDrawn != reportedDrawn) {
245 if (nowDrawn) {
246 Message m = service.mH.obtainMessage(
247 H.REPORT_APPLICATION_TOKEN_DRAWN, this);
248 service.mH.sendMessage(m);
249 }
250 reportedDrawn = nowDrawn;
251 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800252 if (nowVisible != reportedVisible) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800253 if (DEBUG_VISIBILITY) Slog.v(
254 TAG, "Visibility changed in " + this
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800255 + ": vis=" + nowVisible);
256 reportedVisible = nowVisible;
257 Message m = service.mH.obtainMessage(
258 H.REPORT_APPLICATION_TOKEN_WINDOWS,
259 nowVisible ? 1 : 0,
260 nowGone ? 1 : 0,
261 this);
262 service.mH.sendMessage(m);
263 }
264 }
265
266 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700267 WindowState candidate = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800268 int j = windows.size();
269 while (j > 0) {
270 j--;
271 WindowState win = windows.get(j);
272 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
273 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700274 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900275 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700276 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800277 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700278 candidate = win;
279 } else {
280 return win;
281 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800282 }
283 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700284 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800285 }
286
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800287 boolean windowsAreFocusable() {
288 return StackId.canReceiveKeys(mTask.mStack.mStackId) || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800289 }
290
Craig Mautner72669d12012-12-18 17:23:54 -0800291 boolean isVisible() {
292 final int N = allAppWindows.size();
Craig Mautner72669d12012-12-18 17:23:54 -0800293 for (int i=0; i<N; i++) {
294 WindowState win = allAppWindows.get(i);
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700295 // If we're animating with a saved surface, we're already visible.
296 // Return true so that the alpha doesn't get cleared.
Craig Mautner72669d12012-12-18 17:23:54 -0800297 if (!win.mAppFreezing
Filip Gruszczynski57f76f12015-11-04 16:10:54 -0800298 && (win.mViewVisibility == View.VISIBLE || mAnimatingWithSavedSurface
299 || (win.mWinAnimator.isAnimating()
300 && !service.mAppTransition.isTransitionSet()))
301 && !win.mDestroying
302 && win.isDrawnLw()) {
Craig Mautner72669d12012-12-18 17:23:54 -0800303 return true;
304 }
305 }
306 return false;
307 }
308
Craig Mautnere3119b72015-01-20 15:02:36 -0800309 void removeAppFromTaskLocked() {
310 mIsExiting = false;
311 removeAllWindows();
312
Craig Mautner83162a92015-01-26 14:43:30 -0800313 // Use local variable because removeAppToken will null out mTask.
314 final Task task = mTask;
Craig Mautnere3119b72015-01-20 15:02:36 -0800315 if (task != null) {
316 if (!task.removeAppToken(this)) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800317 Slog.e(TAG, "removeAppFromTaskLocked: token=" + this
Craig Mautnere3119b72015-01-20 15:02:36 -0800318 + " not found.");
319 }
320 task.mStack.mExitingAppTokens.remove(this);
321 }
322 }
323
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800324 void setWindowsExiting(boolean exiting) {
Chong Zhangeb22e8e2016-01-20 19:52:22 -0800325 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
326 WindowState win = allAppWindows.get(i);
Chong Zhang6bb82502016-02-02 14:13:48 -0800327 // If the app already requested to remove its window, we don't modify
328 // its exiting state. Otherwise the stale window won't get removed on
329 // exit and could cause focus to be given to the wrong window.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800330 if (!(win.mRemoveOnExit && win.mAnimatingExit)) {
331 win.mAnimatingExit = exiting;
Chong Zhang6bb82502016-02-02 14:13:48 -0800332 }
Chong Zhang5471e902016-02-12 15:34:10 -0800333 // If we're no longer exiting, remove the window from destroying list
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800334 if (!win.mAnimatingExit && win.mDestroying) {
Chong Zhang5471e902016-02-12 15:34:10 -0800335 win.mDestroying = false;
336 service.mDestroySurface.remove(win);
337 }
Chong Zhangeb22e8e2016-01-20 19:52:22 -0800338 }
339 }
340
Robert Carre12aece2016-02-02 22:43:27 -0800341 // Here we destroy surfaces which have been marked as eligible by the animator, taking care
342 // to ensure the client has finished with them. If the client could still be using them
343 // we will skip destruction and try again when the client has stopped.
344 void destroySurfaces() {
345 final ArrayList<WindowState> allWindows = (ArrayList<WindowState>) allAppWindows.clone();
346 final DisplayContentList displayList = new DisplayContentList();
347 for (int i = allWindows.size() - 1; i >= 0; i--) {
348 final WindowState win = allWindows.get(i);
349 if (!win.mDestroying) {
350 continue;
351 }
352
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800353 if (!(mAppStopped || win.mWindowRemovalAllowed)) {
Chong Zhang5471e902016-02-12 15:34:10 -0800354 continue;
Robert Carre12aece2016-02-02 22:43:27 -0800355 }
356
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800357 if (DEBUG_ADD_REMOVE) Slog.e(TAG_WM, "win=" + win
358 + " destroySurfaces: mAppStopped=" + mAppStopped
359 + " win.mWindowRemovalAllowed=" + win.mWindowRemovalAllowed
360 + " win.mRemoveOnExit=" + win.mRemoveOnExit);
361
Robert Carre12aece2016-02-02 22:43:27 -0800362 win.destroyOrSaveSurface();
363 if (win.mRemoveOnExit) {
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800364 win.mAnimatingExit = false;
Robert Carre12aece2016-02-02 22:43:27 -0800365 service.removeWindowInnerLocked(win);
366 }
367 final DisplayContent displayContent = win.getDisplayContent();
368 if (displayContent != null && !displayList.contains(displayContent)) {
369 displayList.add(displayContent);
370 }
371 win.mDestroying = false;
372 }
373 for (int i = 0; i < displayList.size(); i++) {
374 final DisplayContent displayContent = displayList.get(i);
375 service.mLayersController.assignLayersLocked(displayContent.getWindowList());
376 displayContent.layoutNeeded = true;
377 }
378 }
379
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800380 /**
381 * If the application has stopped it is okay to destroy any surfaces which were keeping alive
382 * in case they were still being used.
383 */
384 void notifyAppStopped(boolean stopped) {
385 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: stopped=" + stopped + " " + this);
386 mAppStopped = stopped;
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800387
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800388 if (stopped) {
389 destroySurfaces();
390 // Remove any starting window that was added for this app if they are still around.
391 mTask.mService.scheduleRemoveStartingWindowLocked(this);
392 }
Robert Carre12aece2016-02-02 22:43:27 -0800393 }
394
Chong Zhangbef461f2015-10-27 11:38:24 -0700395 /**
396 * Checks whether we should save surfaces for this app.
397 *
398 * @return true if the surfaces should be saved, false otherwise.
399 */
400 boolean shouldSaveSurface() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800401 // We want to save surface if the app's windows are "allDrawn".
402 // (If we started entering animation early with saved surfaces, allDrawn
403 // should have been restored to true. So we'll save again in that case
404 // even if app didn't actually finish drawing.)
405 return allDrawn;
Robert Carr13f7be9e2015-12-02 18:39:45 -0800406 }
407
408 boolean hasSavedSurface() {
409 for (int i = windows.size() -1; i >= 0; i--) {
410 final WindowState ws = windows.get(i);
411 if (ws.hasSavedSurface()) {
412 return true;
413 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700414 }
415 return false;
416 }
417
418 void restoreSavedSurfaces() {
Robert Carr13f7be9e2015-12-02 18:39:45 -0800419 if (!hasSavedSurface()) {
Chong Zhangbef461f2015-10-27 11:38:24 -0700420 return;
421 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700422 mAnimatingWithSavedSurface = true;
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800423
424 // Check if we have enough drawn windows to mark allDrawn= true.
425 int numInteresting = 0;
426 int numDrawn = 0;
Chong Zhangbef461f2015-10-27 11:38:24 -0700427 for (int i = windows.size() - 1; i >= 0; i--) {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800428 WindowState w = windows.get(i);
Chong Zhang4113ffa2016-02-18 12:39:13 -0800429 if (w.hasSavedSurface()) {
430 w.restoreSavedSurface();
431 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800432 if (w != startingWindow && !w.mAppDied
433 && (!mAppAnimator.freezingScreen || !w.mAppFreezing)) {
434 numInteresting++;
435 if (w.isDrawnLw()) {
436 numDrawn++;
437 }
438 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700439 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800440
Chong Zhang4113ffa2016-02-18 12:39:13 -0800441 allDrawn |= (numInteresting > 0) && (numInteresting == numDrawn);
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800442
443 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
444 "restoreSavedSurfaces: " + appWindowToken + " allDrawn=" + allDrawn);
Chong Zhangbef461f2015-10-27 11:38:24 -0700445 }
446
447 void destroySavedSurfaces() {
Robert Carr13f7be9e2015-12-02 18:39:45 -0800448 for (int i = windows.size() - 1; i >= 0; i--) {
449 WindowState win = windows.get(i);
450 win.destroySavedSurface();
Chong Zhangbef461f2015-10-27 11:38:24 -0700451 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800452 mAnimatingWithSavedSurface = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700453 }
454
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800455 @Override
Craig Mautner7c9ee192014-08-14 16:08:26 -0700456 void removeAllWindows() {
Craig Mautner7b4655d2014-11-20 12:13:22 -0800457 for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
458 // removeWindowLocked at bottom of loop may remove multiple entries from
459 // allAppWindows if the window to be removed has child windows. It also may
460 // not remove any windows from allAppWindows at all if win is exiting and
461 // currently animating away. This ensures that winNdx is monotonically decreasing
462 // and never beyond allAppWindows bounds.
463 winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
464 WindowState win = allAppWindows.get(winNdx);
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800465 if (DEBUG_WINDOW_MOVEMENT) {
466 Slog.w(TAG, "removeAllWindows: removing win=" + win);
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800467 }
468
Wale Ogunwalea6ab5c42015-04-24 09:00:25 -0700469 service.removeWindowLocked(win);
Craig Mautner7c9ee192014-08-14 16:08:26 -0700470 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800471 allAppWindows.clear();
472 windows.clear();
Craig Mautner7c9ee192014-08-14 16:08:26 -0700473 }
474
Chong Zhang112eb8c2015-11-02 11:17:00 -0800475 void removeAllDeadWindows() {
476 for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
477 // removeWindowLocked at bottom of loop may remove multiple entries from
478 // allAppWindows if the window to be removed has child windows. It also may
479 // not remove any windows from allAppWindows at all if win is exiting and
480 // currently animating away. This ensures that winNdx is monotonically decreasing
481 // and never beyond allAppWindows bounds.
482 winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
483 WindowState win = allAppWindows.get(winNdx);
484 if (win.mAppDied) {
Wale Ogunwale2728bf42016-03-03 11:03:26 -0800485 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800486 Slog.w(TAG, "removeAllDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800487 }
488 // Set mDestroying, we don't want any animation or delayed removal here.
489 win.mDestroying = true;
490 service.removeWindowLocked(win);
491 }
492 }
493 }
494
Robert Carra1eb4392015-12-10 12:43:51 -0800495 void setReplacingWindows(boolean animate) {
496 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + appWindowToken
497 + " with replacing windows.");
498
499 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
500 final WindowState w = allAppWindows.get(i);
501 w.setReplacing(animate);
502 }
503 if (animate) {
504 // Set-up dummy animation so we can start treating windows associated with this
505 // token like they are in transition before the new app window is ready for us to
506 // run the real transition animation.
507 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
508 "setReplacingWindow() Setting dummy animation on: " + this);
509 mAppAnimator.setDummyAnimation();
510 }
511 }
512
Robert Carr23fa16b2016-01-13 13:19:58 -0800513 void setReplacingChildren() {
514 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + appWindowToken
515 + " with replacing child windows.");
516 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
517 final WindowState w = allAppWindows.get(i);
518 if (w.isChildWindow()) {
519 w.setReplacing(false /* animate */);
520 }
521 }
522 }
523
Chong Zhangf596cd52016-01-05 13:42:44 -0800524 void resetReplacingWindows() {
525 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Resetting app token " + appWindowToken
526 + " of replacing window marks.");
527
528 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
529 final WindowState w = allAppWindows.get(i);
530 w.resetReplacing();
531 }
532 }
533
Robert Carra1eb4392015-12-10 12:43:51 -0800534 void addWindow(WindowState w) {
535 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
536 WindowState candidate = allAppWindows.get(i);
537 if (candidate.mWillReplaceWindow && candidate.mReplacingWindow == null &&
538 candidate.getWindowTag().equals(w.getWindowTag().toString())) {
539 candidate.mReplacingWindow = w;
Chong Zhangf596cd52016-01-05 13:42:44 -0800540
541 // if we got a replacement window, reset the timeout to give drawing more time
542 service.mH.removeMessages(H.WINDOW_REPLACEMENT_TIMEOUT);
543 service.mH.sendMessageDelayed(
544 service.mH.obtainMessage(H.WINDOW_REPLACEMENT_TIMEOUT, this),
545 WINDOW_REPLACEMENT_TIMEOUT_DURATION);
Robert Carra1eb4392015-12-10 12:43:51 -0800546 }
547 }
548 allAppWindows.add(w);
549 }
550
551 boolean waitingForReplacement() {
552 for (int i = allAppWindows.size() -1; i >= 0; i--) {
553 WindowState candidate = allAppWindows.get(i);
554 if (candidate.mWillReplaceWindow) {
555 return true;
556 }
557 }
558 return false;
559 }
560
Chong Zhangf596cd52016-01-05 13:42:44 -0800561 void clearTimedoutReplacesLocked() {
Robert Carra1eb4392015-12-10 12:43:51 -0800562 for (int i = allAppWindows.size() - 1; i >= 0;
563 // removeWindowLocked at bottom of loop may remove multiple entries from
564 // allAppWindows if the window to be removed has child windows. It also may
565 // not remove any windows from allAppWindows at all if win is exiting and
566 // currently animating away. This ensures that winNdx is monotonically decreasing
567 // and never beyond allAppWindows bounds.
568 i = Math.min(i - 1, allAppWindows.size() - 1)) {
569 WindowState candidate = allAppWindows.get(i);
570 if (candidate.mWillReplaceWindow == false) {
571 continue;
572 }
573 candidate.mWillReplaceWindow = false;
Chong Zhangf596cd52016-01-05 13:42:44 -0800574 // Since the window already timed out, remove it immediately now.
575 // Use removeWindowInnerLocked() instead of removeWindowLocked(), as the latter
576 // delays removal on certain conditions, which will leave the stale window in the
577 // stack and marked mWillReplaceWindow=false, so the window will never be removed.
578 service.removeWindowInnerLocked(candidate);
Robert Carra1eb4392015-12-10 12:43:51 -0800579 }
580 }
581
Jorim Jaggi0429f352015-12-22 16:29:16 +0100582 /**
583 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
584 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
585 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
586 * with a queue.
587 */
588 void freezeBounds() {
589 mFrozenBounds.offer(new Rect(mTask.mPreparedFrozenBounds));
590 }
591
592 /**
593 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
594 */
595 void unfreezeBounds() {
596 mFrozenBounds.remove();
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100597 for (int i = windows.size() - 1; i >= 0; i--) {
598 final WindowState win = windows.get(i);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800599 if (!win.mHasSurface) {
600 continue;
601 }
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100602 win.mLayoutNeeded = true;
603 win.setDisplayLayoutNeeded();
604 if (!service.mResizingWindows.contains(win)) {
605 service.mResizingWindows.add(win);
606 }
607 }
608 service.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100609 }
610
Craig Mautnerdbb79912012-03-01 18:59:14 -0800611 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800612 void dump(PrintWriter pw, String prefix) {
613 super.dump(pw, prefix);
614 if (appToken != null) {
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700615 pw.print(prefix); pw.print("app=true voiceInteraction="); pw.println(voiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800616 }
617 if (allAppWindows.size() > 0) {
618 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
619 }
Craig Mautner83162a92015-01-26 14:43:30 -0800620 pw.print(prefix); pw.print("task="); pw.println(mTask);
621 pw.print(prefix); pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800622 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
623 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
624 pw.print(" clientHidden="); pw.print(clientHidden);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700625 pw.print(" reportedDrawn="); pw.print(reportedDrawn);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800626 pw.print(" reportedVisible="); pw.println(reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -0700627 if (paused) {
628 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800629 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800630 if (mAppStopped) {
631 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
632 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800633 if (numInterestingWindows != 0 || numDrawnWindows != 0
Craig Mautner6fbda632012-07-03 09:26:39 -0700634 || allDrawn || mAppAnimator.allDrawn) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800635 pw.print(prefix); pw.print("numInterestingWindows=");
636 pw.print(numInterestingWindows);
637 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
638 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -0700639 pw.print(" allDrawn="); pw.print(allDrawn);
640 pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
641 pw.println(")");
642 }
643 if (inPendingTransaction) {
644 pw.print(prefix); pw.print("inPendingTransaction=");
645 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800646 }
Craig Mautner799bc1d2015-01-14 10:33:48 -0800647 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800648 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
649 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -0800650 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -0800651 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800652 }
653 if (startingWindow != null || startingView != null
654 || startingDisplayed || startingMoved) {
655 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
656 pw.print(" startingView="); pw.print(startingView);
657 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800658 pw.print(" startingMoved="); pw.println(startingMoved);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800659 }
Jorim Jaggi0429f352015-12-22 16:29:16 +0100660 if (!mFrozenBounds.isEmpty()) {
661 pw.print(prefix); pw.print("mFrozenBounds="); pw.print(mFrozenBounds);
662 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800663 }
664
665 @Override
666 public String toString() {
667 if (stringName == null) {
668 StringBuilder sb = new StringBuilder();
669 sb.append("AppWindowToken{");
670 sb.append(Integer.toHexString(System.identityHashCode(this)));
671 sb.append(" token="); sb.append(token); sb.append('}');
672 stringName = sb.toString();
673 }
674 return stringName;
675 }
Jeff Browne9bdb312012-04-05 15:30:10 -0700676}