blob: abbb5f48c606436cec11a9d843bf931116a71cf2 [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 Jaggi0429f352015-12-22 16:29:16 +010037import android.graphics.Rect;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080038import android.os.Message;
39import android.os.RemoteException;
40import android.util.Slog;
41import android.view.IApplicationToken;
42import android.view.View;
43import android.view.WindowManager;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080044
45import java.io.PrintWriter;
Jorim Jaggi0429f352015-12-22 16:29:16 +010046import java.util.ArrayDeque;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080047import java.util.ArrayList;
48
49class AppTokenList extends ArrayList<AppWindowToken> {
50}
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080051
52/**
53 * Version of WindowToken that is specifically for a particular application (or
54 * really activity) that is displaying windows.
55 */
Craig Mautnere32c3072012-03-12 15:25:35 -070056class AppWindowToken extends WindowToken {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080057 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowToken" : TAG_WM;
58
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080059 // Non-null only for application tokens.
60 final IApplicationToken appToken;
61
62 // All of the windows and child windows that are included in this
63 // application token. Note this list is NOT sorted!
Craig Mautner96868332012-12-04 14:29:11 -080064 final WindowList allAppWindows = new WindowList();
Filip Gruszczynskia590c992015-11-25 16:45:26 -080065 @NonNull final AppWindowAnimator mAppAnimator;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070066
Dianne Hackborne30e02f2014-05-27 18:24:45 -070067 final boolean voiceInteraction;
68
Craig Mautner83162a92015-01-26 14:43:30 -080069 Task mTask;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080070 boolean appFullscreen;
71 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Craig Mautner4c5eb222013-11-18 12:59:05 -080072 boolean layoutConfigChanges;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070073 boolean showForAllUsers;
Craig Mautnera2c77052012-03-26 12:14:43 -070074
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080075 // The input dispatching timeout for this application token in nanoseconds.
76 long inputDispatchingTimeoutNanos;
77
78 // These are used for determining when all windows associated with
79 // an activity have been drawn, so they can be made visible together
80 // at the same time.
Craig Mautner764983d2012-03-22 11:37:36 -070081 // initialize so that it doesn't match mTransactionSequence which is an int.
82 long lastTransactionSequence = Long.MIN_VALUE;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080083 int numInterestingWindows;
84 int numDrawnWindows;
85 boolean inPendingTransaction;
86 boolean allDrawn;
Craig Mautner7636dfb2012-11-16 15:24:11 -080087 // Set to true when this app creates a surface while in the middle of an animation. In that
88 // case do not clear allDrawn until the animation completes.
89 boolean deferClearAllDrawn;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080090
Chong Zhang8e4bda92016-05-04 15:08:18 -070091 // These are to track the app's real drawing status if there were no saved surfaces.
92 boolean allDrawnExcludingSaved;
93 int numInterestingWindowsExcludingSaved;
94 int numDrawnWindowsExclusingSaved;
95
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080096 // Is this window's surface needed? This is almost like hidden, except
97 // it will sometimes be true a little earlier: when the token has
98 // been shown, but is still waiting for its app transition to execute
99 // before making its windows shown.
100 boolean hiddenRequested;
101
102 // Have we told the window clients to hide themselves?
103 boolean clientHidden;
104
105 // Last visibility state we reported to the app token.
106 boolean reportedVisible;
107
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700108 // Last drawn state we reported to the app token.
109 boolean reportedDrawn;
110
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800111 // Set to true when the token has been removed from the window mgr.
112 boolean removed;
113
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800114 // Information about an application starting window if displayed.
115 StartingData startingData;
116 WindowState startingWindow;
117 View startingView;
118 boolean startingDisplayed;
119 boolean startingMoved;
120 boolean firstWindowDrawn;
121
122 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700123 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800124
Craig Mautner799bc1d2015-01-14 10:33:48 -0800125 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800126
Craig Mautnerbb742462014-07-07 15:28:55 -0700127 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700128 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700129
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800130 boolean mAlwaysFocusable;
131
Robert Carre12aece2016-02-02 22:43:27 -0800132 boolean mAppStopped;
Chong Zhangd78ddb42016-03-02 17:01:14 -0800133 int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800134
Jorim Jaggi0429f352015-12-22 16:29:16 +0100135 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
136
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700137 AppWindowToken(WindowManagerService _service, IApplicationToken _token,
138 boolean _voiceInteraction) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800139 super(_service, _token.asBinder(),
140 WindowManager.LayoutParams.TYPE_APPLICATION, true);
141 appWindowToken = this;
142 appToken = _token;
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700143 voiceInteraction = _voiceInteraction;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800144 mInputApplicationHandle = new InputApplicationHandle(this);
Craig Mautner322e4032012-07-13 13:35:20 -0700145 mAppAnimator = new AppWindowAnimator(this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800146 }
147
148 void sendAppVisibilityToClients() {
149 final int N = allAppWindows.size();
150 for (int i=0; i<N; i++) {
151 WindowState win = allAppWindows.get(i);
152 if (win == startingWindow && clientHidden) {
153 // Don't hide the starting window.
154 continue;
155 }
156 try {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800157 if (DEBUG_VISIBILITY) Slog.v(TAG,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800158 "Setting visibility of " + win + ": " + (!clientHidden));
159 win.mClient.dispatchAppVisibility(!clientHidden);
160 } catch (RemoteException e) {
161 }
162 }
163 }
164
Chong Zhang92147042016-05-09 12:47:11 -0700165 void setVisibleBeforeClientHidden() {
166 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
167 final WindowState w = allAppWindows.get(i);
168 w.setVisibleBeforeClientHidden();
169 }
170 }
171
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800172 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
173 firstWindowDrawn = true;
174
175 // We now have a good window to show, remove dead placeholders
176 removeAllDeadWindows();
177
178 if (startingData != null) {
179 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
180 + win.mToken + ": first real window is shown, no animation");
181 // If this initial window is animating, stop it -- we will do an animation to reveal
182 // it from behind the starting window, so there is no need for it to also be doing its
183 // own stuff.
184 winAnimator.clearAnimation();
185 winAnimator.mService.mFinishedStarting.add(this);
186 winAnimator.mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
187 }
188 updateReportedVisibilityLocked();
189 }
190
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800191 void updateReportedVisibilityLocked() {
192 if (appToken == null) {
193 return;
194 }
195
196 int numInteresting = 0;
197 int numVisible = 0;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700198 int numDrawn = 0;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800199 boolean nowGone = true;
200
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800201 if (DEBUG_VISIBILITY) Slog.v(TAG,
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700202 "Update reported visibility: " + this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800203 final int N = allAppWindows.size();
204 for (int i=0; i<N; i++) {
205 WindowState win = allAppWindows.get(i);
206 if (win == startingWindow || win.mAppFreezing
207 || win.mViewVisibility != View.VISIBLE
208 || win.mAttrs.type == TYPE_APPLICATION_STARTING
209 || win.mDestroying) {
210 continue;
211 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800212 if (DEBUG_VISIBILITY) {
213 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800214 + win.isDrawnLw()
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700215 + ", isAnimationSet=" + win.mWinAnimator.isAnimationSet());
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800216 if (!win.isDrawnLw()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800217 Slog.v(TAG, "Not displayed: s=" +
Robert Carre6a83512015-11-03 16:09:21 -0800218 win.mWinAnimator.mSurfaceController
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800219 + " pv=" + win.mPolicyVisibility
Craig Mautner749a7bb2012-04-02 13:49:53 -0700220 + " mDrawState=" + win.mWinAnimator.mDrawState
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800221 + " ah=" + win.mAttachedHidden
222 + " th="
223 + (win.mAppToken != null
224 ? win.mAppToken.hiddenRequested : false)
Craig Mautnera2c77052012-03-26 12:14:43 -0700225 + " a=" + win.mWinAnimator.mAnimating);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800226 }
227 }
228 numInteresting++;
229 if (win.isDrawnLw()) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700230 numDrawn++;
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700231 if (!win.mWinAnimator.isAnimationSet()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800232 numVisible++;
233 }
234 nowGone = false;
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700235 } else if (win.mWinAnimator.isAnimationSet()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800236 nowGone = false;
237 }
238 }
239
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700240 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800241 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700242 if (!nowGone) {
243 // If the app is not yet gone, then it can only become visible/drawn.
244 if (!nowDrawn) {
245 nowDrawn = reportedDrawn;
246 }
247 if (!nowVisible) {
248 nowVisible = reportedVisible;
249 }
250 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800251 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800252 + numInteresting + " visible=" + numVisible);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700253 if (nowDrawn != reportedDrawn) {
254 if (nowDrawn) {
255 Message m = service.mH.obtainMessage(
256 H.REPORT_APPLICATION_TOKEN_DRAWN, this);
257 service.mH.sendMessage(m);
258 }
259 reportedDrawn = nowDrawn;
260 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800261 if (nowVisible != reportedVisible) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800262 if (DEBUG_VISIBILITY) Slog.v(
263 TAG, "Visibility changed in " + this
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800264 + ": vis=" + nowVisible);
265 reportedVisible = nowVisible;
266 Message m = service.mH.obtainMessage(
267 H.REPORT_APPLICATION_TOKEN_WINDOWS,
268 nowVisible ? 1 : 0,
269 nowGone ? 1 : 0,
270 this);
271 service.mH.sendMessage(m);
272 }
273 }
274
275 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700276 WindowState candidate = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800277 int j = windows.size();
278 while (j > 0) {
279 j--;
280 WindowState win = windows.get(j);
281 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
282 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700283 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900284 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700285 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800286 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700287 candidate = win;
288 } else {
289 return win;
290 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800291 }
292 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700293 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800294 }
295
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800296 boolean windowsAreFocusable() {
297 return StackId.canReceiveKeys(mTask.mStack.mStackId) || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800298 }
299
Craig Mautner72669d12012-12-18 17:23:54 -0800300 boolean isVisible() {
301 final int N = allAppWindows.size();
Craig Mautner72669d12012-12-18 17:23:54 -0800302 for (int i=0; i<N; i++) {
303 WindowState win = allAppWindows.get(i);
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700304 // If we're animating with a saved surface, we're already visible.
305 // Return true so that the alpha doesn't get cleared.
Craig Mautner72669d12012-12-18 17:23:54 -0800306 if (!win.mAppFreezing
Chong Zhang92147042016-05-09 12:47:11 -0700307 && (win.mViewVisibility == View.VISIBLE || win.isAnimatingWithSavedSurface()
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700308 || (win.mWinAnimator.isAnimationSet()
Filip Gruszczynski57f76f12015-11-04 16:10:54 -0800309 && !service.mAppTransition.isTransitionSet()))
310 && !win.mDestroying
311 && win.isDrawnLw()) {
Craig Mautner72669d12012-12-18 17:23:54 -0800312 return true;
313 }
314 }
315 return false;
316 }
317
Craig Mautnere3119b72015-01-20 15:02:36 -0800318 void removeAppFromTaskLocked() {
319 mIsExiting = false;
320 removeAllWindows();
321
Craig Mautner83162a92015-01-26 14:43:30 -0800322 // Use local variable because removeAppToken will null out mTask.
323 final Task task = mTask;
Craig Mautnere3119b72015-01-20 15:02:36 -0800324 if (task != null) {
325 if (!task.removeAppToken(this)) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800326 Slog.e(TAG, "removeAppFromTaskLocked: token=" + this
Craig Mautnere3119b72015-01-20 15:02:36 -0800327 + " not found.");
328 }
329 task.mStack.mExitingAppTokens.remove(this);
330 }
331 }
332
Robert Carre12aece2016-02-02 22:43:27 -0800333 // Here we destroy surfaces which have been marked as eligible by the animator, taking care
334 // to ensure the client has finished with them. If the client could still be using them
335 // we will skip destruction and try again when the client has stopped.
336 void destroySurfaces() {
337 final ArrayList<WindowState> allWindows = (ArrayList<WindowState>) allAppWindows.clone();
338 final DisplayContentList displayList = new DisplayContentList();
339 for (int i = allWindows.size() - 1; i >= 0; i--) {
340 final WindowState win = allWindows.get(i);
Chong Zhangeb665572016-05-09 18:28:27 -0700341
342 if (!(mAppStopped || win.mWindowRemovalAllowed)) {
Robert Carre12aece2016-02-02 22:43:27 -0800343 continue;
344 }
345
Chong Zhangeb665572016-05-09 18:28:27 -0700346 win.mWinAnimator.destroyPreservedSurfaceLocked();
347
348 if (!win.mDestroying) {
Chong Zhang5471e902016-02-12 15:34:10 -0800349 continue;
Robert Carre12aece2016-02-02 22:43:27 -0800350 }
351
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800352 if (DEBUG_ADD_REMOVE) Slog.e(TAG_WM, "win=" + win
353 + " destroySurfaces: mAppStopped=" + mAppStopped
354 + " win.mWindowRemovalAllowed=" + win.mWindowRemovalAllowed
355 + " win.mRemoveOnExit=" + win.mRemoveOnExit);
356
Robert Carre12aece2016-02-02 22:43:27 -0800357 win.destroyOrSaveSurface();
358 if (win.mRemoveOnExit) {
Robert Carre12aece2016-02-02 22:43:27 -0800359 service.removeWindowInnerLocked(win);
360 }
361 final DisplayContent displayContent = win.getDisplayContent();
362 if (displayContent != null && !displayList.contains(displayContent)) {
363 displayList.add(displayContent);
364 }
365 win.mDestroying = false;
366 }
367 for (int i = 0; i < displayList.size(); i++) {
368 final DisplayContent displayContent = displayList.get(i);
369 service.mLayersController.assignLayersLocked(displayContent.getWindowList());
370 displayContent.layoutNeeded = true;
371 }
372 }
373
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800374 /**
375 * If the application has stopped it is okay to destroy any surfaces which were keeping alive
376 * in case they were still being used.
377 */
378 void notifyAppStopped(boolean stopped) {
379 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: stopped=" + stopped + " " + this);
380 mAppStopped = stopped;
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800381
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800382 if (stopped) {
383 destroySurfaces();
384 // Remove any starting window that was added for this app if they are still around.
385 mTask.mService.scheduleRemoveStartingWindowLocked(this);
386 }
Robert Carre12aece2016-02-02 22:43:27 -0800387 }
388
Chong Zhangbef461f2015-10-27 11:38:24 -0700389 /**
390 * Checks whether we should save surfaces for this app.
391 *
392 * @return true if the surfaces should be saved, false otherwise.
393 */
394 boolean shouldSaveSurface() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800395 // We want to save surface if the app's windows are "allDrawn".
396 // (If we started entering animation early with saved surfaces, allDrawn
397 // should have been restored to true. So we'll save again in that case
398 // even if app didn't actually finish drawing.)
399 return allDrawn;
Robert Carr13f7be9e2015-12-02 18:39:45 -0800400 }
401
Chong Zhang92147042016-05-09 12:47:11 -0700402 boolean canRestoreSurfaces() {
Chong Zhangb7b4a562016-04-28 15:30:33 -0700403 for (int i = allAppWindows.size() -1; i >= 0; i--) {
Chong Zhang92147042016-05-09 12:47:11 -0700404 final WindowState w = allAppWindows.get(i);
405 if (w.canRestoreSurface()) {
Robert Carr13f7be9e2015-12-02 18:39:45 -0800406 return true;
407 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700408 }
409 return false;
410 }
411
Chong Zhang92147042016-05-09 12:47:11 -0700412 void clearVisibleBeforeClientHidden() {
413 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
414 final WindowState w = allAppWindows.get(i);
415 w.clearVisibleBeforeClientHidden();
416 }
417 }
418
Chong Zhang8e4bda92016-05-04 15:08:18 -0700419 /**
420 * Whether the app has some window that is invisible in layout, but
421 * animating with saved surface.
422 */
423 boolean isAnimatingInvisibleWithSavedSurface() {
424 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
425 final WindowState w = allAppWindows.get(i);
426 if (w.isAnimatingInvisibleWithSavedSurface()) {
427 return true;
428 }
429 }
430 return false;
431 }
432
433 /**
434 * Hide all window surfaces that's still invisible in layout but animating
435 * with a saved surface, and mark them destroying.
436 */
437 void stopUsingSavedSurfaceLocked() {
438 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
439 final WindowState w = allAppWindows.get(i);
440 if (w.isAnimatingInvisibleWithSavedSurface()) {
441 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
442 "stopUsingSavedSurfaceLocked: " + w);
443 w.clearAnimatingWithSavedSurface();
444 w.mDestroying = true;
445 w.mWinAnimator.hide("stopUsingSavedSurfaceLocked");
446 w.mWinAnimator.mWallpaperControllerLocked.hideWallpapers(w);
447 }
448 }
449 destroySurfaces();
450 }
451
Chong Zhangbef461f2015-10-27 11:38:24 -0700452 void restoreSavedSurfaces() {
Chong Zhang92147042016-05-09 12:47:11 -0700453 if (!canRestoreSurfaces()) {
454 clearVisibleBeforeClientHidden();
Chong Zhangbef461f2015-10-27 11:38:24 -0700455 return;
456 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800457 // Check if we have enough drawn windows to mark allDrawn= true.
458 int numInteresting = 0;
459 int numDrawn = 0;
Chong Zhangb7b4a562016-04-28 15:30:33 -0700460 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
461 WindowState w = allAppWindows.get(i);
Chong Zhang92147042016-05-09 12:47:11 -0700462 if (w != startingWindow && !w.mAppDied && w.wasVisibleBeforeClientHidden()
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800463 && (!mAppAnimator.freezingScreen || !w.mAppFreezing)) {
464 numInteresting++;
Chong Zhang92147042016-05-09 12:47:11 -0700465 if (w.hasSavedSurface()) {
466 w.restoreSavedSurface();
467 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800468 if (w.isDrawnLw()) {
469 numDrawn++;
470 }
471 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700472 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800473
Chong Zhang92147042016-05-09 12:47:11 -0700474 if (!allDrawn) {
475 allDrawn = (numInteresting > 0) && (numInteresting == numDrawn);
476 if (allDrawn) {
477 service.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
478 }
479 }
480 clearVisibleBeforeClientHidden();
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800481
482 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
Chong Zhang92147042016-05-09 12:47:11 -0700483 "restoreSavedSurfaces: " + appWindowToken + " allDrawn=" + allDrawn
484 + " numInteresting=" + numInteresting + " numDrawn=" + numDrawn);
Chong Zhangbef461f2015-10-27 11:38:24 -0700485 }
486
487 void destroySavedSurfaces() {
Chong Zhangb7b4a562016-04-28 15:30:33 -0700488 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
489 WindowState win = allAppWindows.get(i);
Robert Carr13f7be9e2015-12-02 18:39:45 -0800490 win.destroySavedSurface();
Chong Zhangbef461f2015-10-27 11:38:24 -0700491 }
Chong Zhang92147042016-05-09 12:47:11 -0700492 }
493
494 void clearAllDrawn() {
495 allDrawn = false;
496 deferClearAllDrawn = false;
Chong Zhang8e4bda92016-05-04 15:08:18 -0700497 allDrawnExcludingSaved = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700498 }
499
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800500 @Override
Craig Mautner7c9ee192014-08-14 16:08:26 -0700501 void removeAllWindows() {
Craig Mautner7b4655d2014-11-20 12:13:22 -0800502 for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
503 // removeWindowLocked at bottom of loop may remove multiple entries from
504 // allAppWindows if the window to be removed has child windows. It also may
505 // not remove any windows from allAppWindows at all if win is exiting and
506 // currently animating away. This ensures that winNdx is monotonically decreasing
507 // and never beyond allAppWindows bounds.
508 winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
509 WindowState win = allAppWindows.get(winNdx);
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800510 if (DEBUG_WINDOW_MOVEMENT) {
511 Slog.w(TAG, "removeAllWindows: removing win=" + win);
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800512 }
513
Wale Ogunwalea6ab5c42015-04-24 09:00:25 -0700514 service.removeWindowLocked(win);
Craig Mautner7c9ee192014-08-14 16:08:26 -0700515 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800516 allAppWindows.clear();
517 windows.clear();
Craig Mautner7c9ee192014-08-14 16:08:26 -0700518 }
519
Chong Zhang112eb8c2015-11-02 11:17:00 -0800520 void removeAllDeadWindows() {
521 for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700522 // removeWindowLocked at bottom of loop may remove multiple entries from
523 // allAppWindows if the window to be removed has child windows. It also may
524 // not remove any windows from allAppWindows at all if win is exiting and
525 // currently animating away. This ensures that winNdx is monotonically decreasing
526 // and never beyond allAppWindows bounds.
527 winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800528 WindowState win = allAppWindows.get(winNdx);
529 if (win.mAppDied) {
Wale Ogunwale2728bf42016-03-03 11:03:26 -0800530 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800531 Slog.w(TAG, "removeAllDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800532 }
533 // Set mDestroying, we don't want any animation or delayed removal here.
534 win.mDestroying = true;
535 service.removeWindowLocked(win);
536 }
537 }
538 }
539
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700540 boolean hasWindowsAlive() {
541 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
542 if (!allAppWindows.get(i).mAppDied) {
543 return true;
544 }
545 }
546 return false;
547 }
548
Robert Carra1eb4392015-12-10 12:43:51 -0800549 void setReplacingWindows(boolean animate) {
550 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + appWindowToken
551 + " with replacing windows.");
552
553 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
554 final WindowState w = allAppWindows.get(i);
555 w.setReplacing(animate);
556 }
557 if (animate) {
558 // Set-up dummy animation so we can start treating windows associated with this
559 // token like they are in transition before the new app window is ready for us to
560 // run the real transition animation.
561 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
562 "setReplacingWindow() Setting dummy animation on: " + this);
563 mAppAnimator.setDummyAnimation();
564 }
565 }
566
Robert Carr23fa16b2016-01-13 13:19:58 -0800567 void setReplacingChildren() {
568 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + appWindowToken
569 + " with replacing child windows.");
570 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
571 final WindowState w = allAppWindows.get(i);
Robert Carrd1a010f2016-04-07 22:36:22 -0700572 if (w.shouldBeReplacedWithChildren()) {
Robert Carr23fa16b2016-01-13 13:19:58 -0800573 w.setReplacing(false /* animate */);
574 }
575 }
576 }
577
Chong Zhangf596cd52016-01-05 13:42:44 -0800578 void resetReplacingWindows() {
579 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Resetting app token " + appWindowToken
580 + " of replacing window marks.");
581
582 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
583 final WindowState w = allAppWindows.get(i);
584 w.resetReplacing();
585 }
586 }
587
Chong Zhang4d7369a2016-04-25 16:09:14 -0700588 void requestUpdateWallpaperIfNeeded() {
589 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
590 final WindowState w = allAppWindows.get(i);
591 w.requestUpdateWallpaperIfNeeded();
592 }
593 }
594
Chong Zhangd78ddb42016-03-02 17:01:14 -0800595 boolean isRelaunching() {
596 return mPendingRelaunchCount > 0;
597 }
598
599 void startRelaunching() {
600 if (canFreezeBounds()) {
601 freezeBounds();
602 }
603 mPendingRelaunchCount++;
604 }
605
606 void finishRelaunching() {
607 if (canFreezeBounds()) {
608 unfreezeBounds();
609 }
610 if (mPendingRelaunchCount > 0) {
611 mPendingRelaunchCount--;
612 }
613 }
614
Robert Carra1eb4392015-12-10 12:43:51 -0800615 void addWindow(WindowState w) {
616 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
617 WindowState candidate = allAppWindows.get(i);
618 if (candidate.mWillReplaceWindow && candidate.mReplacingWindow == null &&
Robert Carr8bc89072016-04-08 13:29:59 -0700619 candidate.getWindowTag().toString().equals(w.getWindowTag().toString())) {
Robert Carra1eb4392015-12-10 12:43:51 -0800620 candidate.mReplacingWindow = w;
Robert Carrb439a632016-04-07 22:52:10 -0700621 w.mSkipEnterAnimationForSeamlessReplacement = !candidate.mAnimateReplacingWindow;
Chong Zhangf596cd52016-01-05 13:42:44 -0800622
623 // if we got a replacement window, reset the timeout to give drawing more time
Chong Zhang32de3652016-05-12 16:00:01 -0700624 service.scheduleReplacingWindowTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -0800625 }
626 }
627 allAppWindows.add(w);
628 }
629
630 boolean waitingForReplacement() {
631 for (int i = allAppWindows.size() -1; i >= 0; i--) {
632 WindowState candidate = allAppWindows.get(i);
633 if (candidate.mWillReplaceWindow) {
634 return true;
635 }
636 }
637 return false;
638 }
639
Chong Zhangf596cd52016-01-05 13:42:44 -0800640 void clearTimedoutReplacesLocked() {
Robert Carra1eb4392015-12-10 12:43:51 -0800641 for (int i = allAppWindows.size() - 1; i >= 0;
642 // removeWindowLocked at bottom of loop may remove multiple entries from
643 // allAppWindows if the window to be removed has child windows. It also may
644 // not remove any windows from allAppWindows at all if win is exiting and
645 // currently animating away. This ensures that winNdx is monotonically decreasing
646 // and never beyond allAppWindows bounds.
647 i = Math.min(i - 1, allAppWindows.size() - 1)) {
648 WindowState candidate = allAppWindows.get(i);
649 if (candidate.mWillReplaceWindow == false) {
650 continue;
651 }
652 candidate.mWillReplaceWindow = false;
Robert Carrb439a632016-04-07 22:52:10 -0700653 if (candidate.mReplacingWindow != null) {
654 candidate.mReplacingWindow.mSkipEnterAnimationForSeamlessReplacement = false;
655 }
Chong Zhangf596cd52016-01-05 13:42:44 -0800656 // Since the window already timed out, remove it immediately now.
657 // Use removeWindowInnerLocked() instead of removeWindowLocked(), as the latter
658 // delays removal on certain conditions, which will leave the stale window in the
659 // stack and marked mWillReplaceWindow=false, so the window will never be removed.
660 service.removeWindowInnerLocked(candidate);
Robert Carra1eb4392015-12-10 12:43:51 -0800661 }
662 }
663
Chong Zhangd78ddb42016-03-02 17:01:14 -0800664 private boolean canFreezeBounds() {
665 // For freeform windows, we can't freeze the bounds at the moment because this would make
666 // the resizing unresponsive.
667 return mTask != null && !mTask.inFreeformWorkspace();
668 }
669
Jorim Jaggi0429f352015-12-22 16:29:16 +0100670 /**
671 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
672 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
673 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
674 * with a queue.
675 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800676 private void freezeBounds() {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100677 mFrozenBounds.offer(new Rect(mTask.mPreparedFrozenBounds));
678 }
679
680 /**
681 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
682 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800683 private void unfreezeBounds() {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100684 mFrozenBounds.remove();
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100685 for (int i = windows.size() - 1; i >= 0; i--) {
686 final WindowState win = windows.get(i);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800687 if (!win.mHasSurface) {
688 continue;
689 }
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100690 win.mLayoutNeeded = true;
691 win.setDisplayLayoutNeeded();
692 if (!service.mResizingWindows.contains(win)) {
693 service.mResizingWindows.add(win);
694 }
695 }
696 service.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100697 }
698
Craig Mautnerdbb79912012-03-01 18:59:14 -0800699 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800700 void dump(PrintWriter pw, String prefix) {
701 super.dump(pw, prefix);
702 if (appToken != null) {
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700703 pw.print(prefix); pw.print("app=true voiceInteraction="); pw.println(voiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800704 }
705 if (allAppWindows.size() > 0) {
706 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
707 }
Craig Mautner83162a92015-01-26 14:43:30 -0800708 pw.print(prefix); pw.print("task="); pw.println(mTask);
709 pw.print(prefix); pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800710 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
711 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
712 pw.print(" clientHidden="); pw.print(clientHidden);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700713 pw.print(" reportedDrawn="); pw.print(reportedDrawn);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800714 pw.print(" reportedVisible="); pw.println(reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -0700715 if (paused) {
716 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800717 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800718 if (mAppStopped) {
719 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
720 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800721 if (numInterestingWindows != 0 || numDrawnWindows != 0
Craig Mautner6fbda632012-07-03 09:26:39 -0700722 || allDrawn || mAppAnimator.allDrawn) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800723 pw.print(prefix); pw.print("numInterestingWindows=");
724 pw.print(numInterestingWindows);
725 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
726 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -0700727 pw.print(" allDrawn="); pw.print(allDrawn);
728 pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
729 pw.println(")");
730 }
731 if (inPendingTransaction) {
732 pw.print(prefix); pw.print("inPendingTransaction=");
733 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800734 }
Craig Mautner799bc1d2015-01-14 10:33:48 -0800735 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800736 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
737 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -0800738 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -0800739 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800740 }
741 if (startingWindow != null || startingView != null
742 || startingDisplayed || startingMoved) {
743 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
744 pw.print(" startingView="); pw.print(startingView);
745 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800746 pw.print(" startingMoved="); pw.println(startingMoved);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800747 }
Jorim Jaggi0429f352015-12-22 16:29:16 +0100748 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -0800749 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
750 }
751 if (mPendingRelaunchCount != 0) {
752 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +0100753 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800754 }
755
756 @Override
757 public String toString() {
758 if (stringName == null) {
759 StringBuilder sb = new StringBuilder();
760 sb.append("AppWindowToken{");
761 sb.append(Integer.toHexString(System.identityHashCode(this)));
762 sb.append(" token="); sb.append(token); sb.append('}');
763 stringName = sb.toString();
764 }
765 return stringName;
766 }
Jeff Browne9bdb312012-04-05 15:30:10 -0700767}