blob: a23424175300a929896646dafc2ac39bb5802887 [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
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080091 // Is this window's surface needed? This is almost like hidden, except
92 // it will sometimes be true a little earlier: when the token has
93 // been shown, but is still waiting for its app transition to execute
94 // before making its windows shown.
95 boolean hiddenRequested;
96
97 // Have we told the window clients to hide themselves?
98 boolean clientHidden;
99
100 // Last visibility state we reported to the app token.
101 boolean reportedVisible;
102
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700103 // Last drawn state we reported to the app token.
104 boolean reportedDrawn;
105
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800106 // Set to true when the token has been removed from the window mgr.
107 boolean removed;
108
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800109 // Information about an application starting window if displayed.
110 StartingData startingData;
111 WindowState startingWindow;
112 View startingView;
113 boolean startingDisplayed;
114 boolean startingMoved;
115 boolean firstWindowDrawn;
116
117 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700118 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800119
Craig Mautner799bc1d2015-01-14 10:33:48 -0800120 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800121
Craig Mautnerbb742462014-07-07 15:28:55 -0700122 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700123 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700124
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800125 boolean mAlwaysFocusable;
126
Robert Carre12aece2016-02-02 22:43:27 -0800127 boolean mAppStopped;
Chong Zhangd78ddb42016-03-02 17:01:14 -0800128 int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800129
Jorim Jaggi0429f352015-12-22 16:29:16 +0100130 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
131
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700132 AppWindowToken(WindowManagerService _service, IApplicationToken _token,
133 boolean _voiceInteraction) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800134 super(_service, _token.asBinder(),
135 WindowManager.LayoutParams.TYPE_APPLICATION, true);
136 appWindowToken = this;
137 appToken = _token;
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700138 voiceInteraction = _voiceInteraction;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800139 mInputApplicationHandle = new InputApplicationHandle(this);
Craig Mautner322e4032012-07-13 13:35:20 -0700140 mAppAnimator = new AppWindowAnimator(this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800141 }
142
143 void sendAppVisibilityToClients() {
144 final int N = allAppWindows.size();
145 for (int i=0; i<N; i++) {
146 WindowState win = allAppWindows.get(i);
147 if (win == startingWindow && clientHidden) {
148 // Don't hide the starting window.
149 continue;
150 }
151 try {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800152 if (DEBUG_VISIBILITY) Slog.v(TAG,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800153 "Setting visibility of " + win + ": " + (!clientHidden));
154 win.mClient.dispatchAppVisibility(!clientHidden);
155 } catch (RemoteException e) {
156 }
157 }
158 }
159
Chong Zhang92147042016-05-09 12:47:11 -0700160 void setVisibleBeforeClientHidden() {
161 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
162 final WindowState w = allAppWindows.get(i);
163 w.setVisibleBeforeClientHidden();
164 }
165 }
166
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800167 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
168 firstWindowDrawn = true;
169
170 // We now have a good window to show, remove dead placeholders
171 removeAllDeadWindows();
172
173 if (startingData != null) {
174 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
175 + win.mToken + ": first real window is shown, no animation");
176 // If this initial window is animating, stop it -- we will do an animation to reveal
177 // it from behind the starting window, so there is no need for it to also be doing its
178 // own stuff.
179 winAnimator.clearAnimation();
180 winAnimator.mService.mFinishedStarting.add(this);
181 winAnimator.mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
182 }
183 updateReportedVisibilityLocked();
184 }
185
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800186 void updateReportedVisibilityLocked() {
187 if (appToken == null) {
188 return;
189 }
190
191 int numInteresting = 0;
192 int numVisible = 0;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700193 int numDrawn = 0;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800194 boolean nowGone = true;
195
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800196 if (DEBUG_VISIBILITY) Slog.v(TAG,
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700197 "Update reported visibility: " + this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800198 final int N = allAppWindows.size();
199 for (int i=0; i<N; i++) {
200 WindowState win = allAppWindows.get(i);
201 if (win == startingWindow || win.mAppFreezing
202 || win.mViewVisibility != View.VISIBLE
203 || win.mAttrs.type == TYPE_APPLICATION_STARTING
204 || win.mDestroying) {
205 continue;
206 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800207 if (DEBUG_VISIBILITY) {
208 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800209 + win.isDrawnLw()
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700210 + ", isAnimationSet=" + win.mWinAnimator.isAnimationSet());
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800211 if (!win.isDrawnLw()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800212 Slog.v(TAG, "Not displayed: s=" +
Robert Carre6a83512015-11-03 16:09:21 -0800213 win.mWinAnimator.mSurfaceController
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800214 + " pv=" + win.mPolicyVisibility
Craig Mautner749a7bb2012-04-02 13:49:53 -0700215 + " mDrawState=" + win.mWinAnimator.mDrawState
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800216 + " ah=" + win.mAttachedHidden
217 + " th="
218 + (win.mAppToken != null
219 ? win.mAppToken.hiddenRequested : false)
Craig Mautnera2c77052012-03-26 12:14:43 -0700220 + " a=" + win.mWinAnimator.mAnimating);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800221 }
222 }
223 numInteresting++;
224 if (win.isDrawnLw()) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700225 numDrawn++;
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700226 if (!win.mWinAnimator.isAnimationSet()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800227 numVisible++;
228 }
229 nowGone = false;
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700230 } else if (win.mWinAnimator.isAnimationSet()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800231 nowGone = false;
232 }
233 }
234
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700235 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800236 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700237 if (!nowGone) {
238 // If the app is not yet gone, then it can only become visible/drawn.
239 if (!nowDrawn) {
240 nowDrawn = reportedDrawn;
241 }
242 if (!nowVisible) {
243 nowVisible = reportedVisible;
244 }
245 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800246 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800247 + numInteresting + " visible=" + numVisible);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700248 if (nowDrawn != reportedDrawn) {
249 if (nowDrawn) {
250 Message m = service.mH.obtainMessage(
251 H.REPORT_APPLICATION_TOKEN_DRAWN, this);
252 service.mH.sendMessage(m);
253 }
254 reportedDrawn = nowDrawn;
255 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800256 if (nowVisible != reportedVisible) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800257 if (DEBUG_VISIBILITY) Slog.v(
258 TAG, "Visibility changed in " + this
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800259 + ": vis=" + nowVisible);
260 reportedVisible = nowVisible;
261 Message m = service.mH.obtainMessage(
262 H.REPORT_APPLICATION_TOKEN_WINDOWS,
263 nowVisible ? 1 : 0,
264 nowGone ? 1 : 0,
265 this);
266 service.mH.sendMessage(m);
267 }
268 }
269
270 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700271 WindowState candidate = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800272 int j = windows.size();
273 while (j > 0) {
274 j--;
275 WindowState win = windows.get(j);
276 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
277 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700278 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900279 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700280 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800281 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700282 candidate = win;
283 } else {
284 return win;
285 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800286 }
287 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700288 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800289 }
290
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800291 boolean windowsAreFocusable() {
292 return StackId.canReceiveKeys(mTask.mStack.mStackId) || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800293 }
294
Craig Mautner72669d12012-12-18 17:23:54 -0800295 boolean isVisible() {
296 final int N = allAppWindows.size();
Craig Mautner72669d12012-12-18 17:23:54 -0800297 for (int i=0; i<N; i++) {
298 WindowState win = allAppWindows.get(i);
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700299 // If we're animating with a saved surface, we're already visible.
300 // Return true so that the alpha doesn't get cleared.
Craig Mautner72669d12012-12-18 17:23:54 -0800301 if (!win.mAppFreezing
Chong Zhang92147042016-05-09 12:47:11 -0700302 && (win.mViewVisibility == View.VISIBLE || win.isAnimatingWithSavedSurface()
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700303 || (win.mWinAnimator.isAnimationSet()
Filip Gruszczynski57f76f12015-11-04 16:10:54 -0800304 && !service.mAppTransition.isTransitionSet()))
305 && !win.mDestroying
306 && win.isDrawnLw()) {
Craig Mautner72669d12012-12-18 17:23:54 -0800307 return true;
308 }
309 }
310 return false;
311 }
312
Craig Mautnere3119b72015-01-20 15:02:36 -0800313 void removeAppFromTaskLocked() {
314 mIsExiting = false;
315 removeAllWindows();
316
Craig Mautner83162a92015-01-26 14:43:30 -0800317 // Use local variable because removeAppToken will null out mTask.
318 final Task task = mTask;
Craig Mautnere3119b72015-01-20 15:02:36 -0800319 if (task != null) {
320 if (!task.removeAppToken(this)) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800321 Slog.e(TAG, "removeAppFromTaskLocked: token=" + this
Craig Mautnere3119b72015-01-20 15:02:36 -0800322 + " not found.");
323 }
324 task.mStack.mExitingAppTokens.remove(this);
325 }
326 }
327
Robert Carre12aece2016-02-02 22:43:27 -0800328 // Here we destroy surfaces which have been marked as eligible by the animator, taking care
329 // to ensure the client has finished with them. If the client could still be using them
330 // we will skip destruction and try again when the client has stopped.
331 void destroySurfaces() {
332 final ArrayList<WindowState> allWindows = (ArrayList<WindowState>) allAppWindows.clone();
333 final DisplayContentList displayList = new DisplayContentList();
334 for (int i = allWindows.size() - 1; i >= 0; i--) {
335 final WindowState win = allWindows.get(i);
Chong Zhangeb665572016-05-09 18:28:27 -0700336
337 if (!(mAppStopped || win.mWindowRemovalAllowed)) {
Robert Carre12aece2016-02-02 22:43:27 -0800338 continue;
339 }
340
Chong Zhangeb665572016-05-09 18:28:27 -0700341 win.mWinAnimator.destroyPreservedSurfaceLocked();
342
343 if (!win.mDestroying) {
Chong Zhang5471e902016-02-12 15:34:10 -0800344 continue;
Robert Carre12aece2016-02-02 22:43:27 -0800345 }
346
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800347 if (DEBUG_ADD_REMOVE) Slog.e(TAG_WM, "win=" + win
348 + " destroySurfaces: mAppStopped=" + mAppStopped
349 + " win.mWindowRemovalAllowed=" + win.mWindowRemovalAllowed
350 + " win.mRemoveOnExit=" + win.mRemoveOnExit);
351
Robert Carre12aece2016-02-02 22:43:27 -0800352 win.destroyOrSaveSurface();
353 if (win.mRemoveOnExit) {
Robert Carre12aece2016-02-02 22:43:27 -0800354 service.removeWindowInnerLocked(win);
355 }
356 final DisplayContent displayContent = win.getDisplayContent();
357 if (displayContent != null && !displayList.contains(displayContent)) {
358 displayList.add(displayContent);
359 }
360 win.mDestroying = false;
361 }
362 for (int i = 0; i < displayList.size(); i++) {
363 final DisplayContent displayContent = displayList.get(i);
364 service.mLayersController.assignLayersLocked(displayContent.getWindowList());
365 displayContent.layoutNeeded = true;
366 }
367 }
368
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800369 /**
370 * If the application has stopped it is okay to destroy any surfaces which were keeping alive
371 * in case they were still being used.
372 */
373 void notifyAppStopped(boolean stopped) {
374 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: stopped=" + stopped + " " + this);
375 mAppStopped = stopped;
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800376
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800377 if (stopped) {
378 destroySurfaces();
379 // Remove any starting window that was added for this app if they are still around.
380 mTask.mService.scheduleRemoveStartingWindowLocked(this);
381 }
Robert Carre12aece2016-02-02 22:43:27 -0800382 }
383
Chong Zhangbef461f2015-10-27 11:38:24 -0700384 /**
385 * Checks whether we should save surfaces for this app.
386 *
387 * @return true if the surfaces should be saved, false otherwise.
388 */
389 boolean shouldSaveSurface() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800390 // We want to save surface if the app's windows are "allDrawn".
391 // (If we started entering animation early with saved surfaces, allDrawn
392 // should have been restored to true. So we'll save again in that case
393 // even if app didn't actually finish drawing.)
394 return allDrawn;
Robert Carr13f7be9e2015-12-02 18:39:45 -0800395 }
396
Chong Zhang92147042016-05-09 12:47:11 -0700397 boolean canRestoreSurfaces() {
Chong Zhangb7b4a562016-04-28 15:30:33 -0700398 for (int i = allAppWindows.size() -1; i >= 0; i--) {
Chong Zhang92147042016-05-09 12:47:11 -0700399 final WindowState w = allAppWindows.get(i);
400 if (w.canRestoreSurface()) {
Robert Carr13f7be9e2015-12-02 18:39:45 -0800401 return true;
402 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700403 }
404 return false;
405 }
406
Chong Zhang92147042016-05-09 12:47:11 -0700407 void clearVisibleBeforeClientHidden() {
408 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
409 final WindowState w = allAppWindows.get(i);
410 w.clearVisibleBeforeClientHidden();
411 }
412 }
413
Chong Zhangbef461f2015-10-27 11:38:24 -0700414 void restoreSavedSurfaces() {
Chong Zhang92147042016-05-09 12:47:11 -0700415 if (!canRestoreSurfaces()) {
416 clearVisibleBeforeClientHidden();
Chong Zhangbef461f2015-10-27 11:38:24 -0700417 return;
418 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800419 // Check if we have enough drawn windows to mark allDrawn= true.
420 int numInteresting = 0;
421 int numDrawn = 0;
Chong Zhangb7b4a562016-04-28 15:30:33 -0700422 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
423 WindowState w = allAppWindows.get(i);
Chong Zhang92147042016-05-09 12:47:11 -0700424 if (w != startingWindow && !w.mAppDied && w.wasVisibleBeforeClientHidden()
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800425 && (!mAppAnimator.freezingScreen || !w.mAppFreezing)) {
426 numInteresting++;
Chong Zhang92147042016-05-09 12:47:11 -0700427 if (w.hasSavedSurface()) {
428 w.restoreSavedSurface();
429 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800430 if (w.isDrawnLw()) {
431 numDrawn++;
432 }
433 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700434 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800435
Chong Zhang92147042016-05-09 12:47:11 -0700436 if (!allDrawn) {
437 allDrawn = (numInteresting > 0) && (numInteresting == numDrawn);
438 if (allDrawn) {
439 service.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
440 }
441 }
442 clearVisibleBeforeClientHidden();
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800443
444 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
Chong Zhang92147042016-05-09 12:47:11 -0700445 "restoreSavedSurfaces: " + appWindowToken + " allDrawn=" + allDrawn
446 + " numInteresting=" + numInteresting + " numDrawn=" + numDrawn);
Chong Zhangbef461f2015-10-27 11:38:24 -0700447 }
448
449 void destroySavedSurfaces() {
Chong Zhangb7b4a562016-04-28 15:30:33 -0700450 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
451 WindowState win = allAppWindows.get(i);
Robert Carr13f7be9e2015-12-02 18:39:45 -0800452 win.destroySavedSurface();
Chong Zhangbef461f2015-10-27 11:38:24 -0700453 }
Chong Zhang92147042016-05-09 12:47:11 -0700454 }
455
456 void clearAllDrawn() {
457 allDrawn = false;
458 deferClearAllDrawn = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700459 }
460
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800461 @Override
Craig Mautner7c9ee192014-08-14 16:08:26 -0700462 void removeAllWindows() {
Craig Mautner7b4655d2014-11-20 12:13:22 -0800463 for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
464 // removeWindowLocked at bottom of loop may remove multiple entries from
465 // allAppWindows if the window to be removed has child windows. It also may
466 // not remove any windows from allAppWindows at all if win is exiting and
467 // currently animating away. This ensures that winNdx is monotonically decreasing
468 // and never beyond allAppWindows bounds.
469 winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
470 WindowState win = allAppWindows.get(winNdx);
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800471 if (DEBUG_WINDOW_MOVEMENT) {
472 Slog.w(TAG, "removeAllWindows: removing win=" + win);
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800473 }
474
Wale Ogunwalea6ab5c42015-04-24 09:00:25 -0700475 service.removeWindowLocked(win);
Craig Mautner7c9ee192014-08-14 16:08:26 -0700476 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800477 allAppWindows.clear();
478 windows.clear();
Craig Mautner7c9ee192014-08-14 16:08:26 -0700479 }
480
Chong Zhang112eb8c2015-11-02 11:17:00 -0800481 void removeAllDeadWindows() {
482 for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700483 // removeWindowLocked at bottom of loop may remove multiple entries from
484 // allAppWindows if the window to be removed has child windows. It also may
485 // not remove any windows from allAppWindows at all if win is exiting and
486 // currently animating away. This ensures that winNdx is monotonically decreasing
487 // and never beyond allAppWindows bounds.
488 winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800489 WindowState win = allAppWindows.get(winNdx);
490 if (win.mAppDied) {
Wale Ogunwale2728bf42016-03-03 11:03:26 -0800491 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800492 Slog.w(TAG, "removeAllDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800493 }
494 // Set mDestroying, we don't want any animation or delayed removal here.
495 win.mDestroying = true;
496 service.removeWindowLocked(win);
497 }
498 }
499 }
500
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700501 boolean hasWindowsAlive() {
502 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
503 if (!allAppWindows.get(i).mAppDied) {
504 return true;
505 }
506 }
507 return false;
508 }
509
Robert Carra1eb4392015-12-10 12:43:51 -0800510 void setReplacingWindows(boolean animate) {
511 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + appWindowToken
512 + " with replacing windows.");
513
514 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
515 final WindowState w = allAppWindows.get(i);
516 w.setReplacing(animate);
517 }
518 if (animate) {
519 // Set-up dummy animation so we can start treating windows associated with this
520 // token like they are in transition before the new app window is ready for us to
521 // run the real transition animation.
522 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
523 "setReplacingWindow() Setting dummy animation on: " + this);
524 mAppAnimator.setDummyAnimation();
525 }
526 }
527
Robert Carr23fa16b2016-01-13 13:19:58 -0800528 void setReplacingChildren() {
529 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + appWindowToken
530 + " with replacing child windows.");
531 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
532 final WindowState w = allAppWindows.get(i);
Robert Carrd1a010f2016-04-07 22:36:22 -0700533 if (w.shouldBeReplacedWithChildren()) {
Robert Carr23fa16b2016-01-13 13:19:58 -0800534 w.setReplacing(false /* animate */);
535 }
536 }
537 }
538
Chong Zhangf596cd52016-01-05 13:42:44 -0800539 void resetReplacingWindows() {
540 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Resetting app token " + appWindowToken
541 + " of replacing window marks.");
542
543 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
544 final WindowState w = allAppWindows.get(i);
545 w.resetReplacing();
546 }
547 }
548
Chong Zhang4d7369a2016-04-25 16:09:14 -0700549 void requestUpdateWallpaperIfNeeded() {
550 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
551 final WindowState w = allAppWindows.get(i);
552 w.requestUpdateWallpaperIfNeeded();
553 }
554 }
555
Chong Zhangd78ddb42016-03-02 17:01:14 -0800556 boolean isRelaunching() {
557 return mPendingRelaunchCount > 0;
558 }
559
560 void startRelaunching() {
561 if (canFreezeBounds()) {
562 freezeBounds();
563 }
564 mPendingRelaunchCount++;
565 }
566
567 void finishRelaunching() {
568 if (canFreezeBounds()) {
569 unfreezeBounds();
570 }
571 if (mPendingRelaunchCount > 0) {
572 mPendingRelaunchCount--;
573 }
574 }
575
Robert Carra1eb4392015-12-10 12:43:51 -0800576 void addWindow(WindowState w) {
577 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
578 WindowState candidate = allAppWindows.get(i);
579 if (candidate.mWillReplaceWindow && candidate.mReplacingWindow == null &&
Robert Carr8bc89072016-04-08 13:29:59 -0700580 candidate.getWindowTag().toString().equals(w.getWindowTag().toString())) {
Robert Carra1eb4392015-12-10 12:43:51 -0800581 candidate.mReplacingWindow = w;
Robert Carrb439a632016-04-07 22:52:10 -0700582 w.mSkipEnterAnimationForSeamlessReplacement = !candidate.mAnimateReplacingWindow;
Chong Zhangf596cd52016-01-05 13:42:44 -0800583
584 // if we got a replacement window, reset the timeout to give drawing more time
585 service.mH.removeMessages(H.WINDOW_REPLACEMENT_TIMEOUT);
586 service.mH.sendMessageDelayed(
587 service.mH.obtainMessage(H.WINDOW_REPLACEMENT_TIMEOUT, this),
588 WINDOW_REPLACEMENT_TIMEOUT_DURATION);
Robert Carra1eb4392015-12-10 12:43:51 -0800589 }
590 }
591 allAppWindows.add(w);
592 }
593
594 boolean waitingForReplacement() {
595 for (int i = allAppWindows.size() -1; i >= 0; i--) {
596 WindowState candidate = allAppWindows.get(i);
597 if (candidate.mWillReplaceWindow) {
598 return true;
599 }
600 }
601 return false;
602 }
603
Chong Zhangf596cd52016-01-05 13:42:44 -0800604 void clearTimedoutReplacesLocked() {
Robert Carra1eb4392015-12-10 12:43:51 -0800605 for (int i = allAppWindows.size() - 1; i >= 0;
606 // removeWindowLocked at bottom of loop may remove multiple entries from
607 // allAppWindows if the window to be removed has child windows. It also may
608 // not remove any windows from allAppWindows at all if win is exiting and
609 // currently animating away. This ensures that winNdx is monotonically decreasing
610 // and never beyond allAppWindows bounds.
611 i = Math.min(i - 1, allAppWindows.size() - 1)) {
612 WindowState candidate = allAppWindows.get(i);
613 if (candidate.mWillReplaceWindow == false) {
614 continue;
615 }
616 candidate.mWillReplaceWindow = false;
Robert Carrb439a632016-04-07 22:52:10 -0700617 if (candidate.mReplacingWindow != null) {
618 candidate.mReplacingWindow.mSkipEnterAnimationForSeamlessReplacement = false;
619 }
Chong Zhangf596cd52016-01-05 13:42:44 -0800620 // Since the window already timed out, remove it immediately now.
621 // Use removeWindowInnerLocked() instead of removeWindowLocked(), as the latter
622 // delays removal on certain conditions, which will leave the stale window in the
623 // stack and marked mWillReplaceWindow=false, so the window will never be removed.
624 service.removeWindowInnerLocked(candidate);
Robert Carra1eb4392015-12-10 12:43:51 -0800625 }
626 }
627
Chong Zhangd78ddb42016-03-02 17:01:14 -0800628 private boolean canFreezeBounds() {
629 // For freeform windows, we can't freeze the bounds at the moment because this would make
630 // the resizing unresponsive.
631 return mTask != null && !mTask.inFreeformWorkspace();
632 }
633
Jorim Jaggi0429f352015-12-22 16:29:16 +0100634 /**
635 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
636 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
637 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
638 * with a queue.
639 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800640 private void freezeBounds() {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100641 mFrozenBounds.offer(new Rect(mTask.mPreparedFrozenBounds));
642 }
643
644 /**
645 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
646 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800647 private void unfreezeBounds() {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100648 mFrozenBounds.remove();
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100649 for (int i = windows.size() - 1; i >= 0; i--) {
650 final WindowState win = windows.get(i);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800651 if (!win.mHasSurface) {
652 continue;
653 }
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100654 win.mLayoutNeeded = true;
655 win.setDisplayLayoutNeeded();
656 if (!service.mResizingWindows.contains(win)) {
657 service.mResizingWindows.add(win);
658 }
659 }
660 service.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100661 }
662
Craig Mautnerdbb79912012-03-01 18:59:14 -0800663 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800664 void dump(PrintWriter pw, String prefix) {
665 super.dump(pw, prefix);
666 if (appToken != null) {
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700667 pw.print(prefix); pw.print("app=true voiceInteraction="); pw.println(voiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800668 }
669 if (allAppWindows.size() > 0) {
670 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
671 }
Craig Mautner83162a92015-01-26 14:43:30 -0800672 pw.print(prefix); pw.print("task="); pw.println(mTask);
673 pw.print(prefix); pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800674 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
675 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
676 pw.print(" clientHidden="); pw.print(clientHidden);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700677 pw.print(" reportedDrawn="); pw.print(reportedDrawn);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800678 pw.print(" reportedVisible="); pw.println(reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -0700679 if (paused) {
680 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800681 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800682 if (mAppStopped) {
683 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
684 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800685 if (numInterestingWindows != 0 || numDrawnWindows != 0
Craig Mautner6fbda632012-07-03 09:26:39 -0700686 || allDrawn || mAppAnimator.allDrawn) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800687 pw.print(prefix); pw.print("numInterestingWindows=");
688 pw.print(numInterestingWindows);
689 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
690 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -0700691 pw.print(" allDrawn="); pw.print(allDrawn);
692 pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
693 pw.println(")");
694 }
695 if (inPendingTransaction) {
696 pw.print(prefix); pw.print("inPendingTransaction=");
697 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800698 }
Craig Mautner799bc1d2015-01-14 10:33:48 -0800699 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800700 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
701 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -0800702 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -0800703 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800704 }
705 if (startingWindow != null || startingView != null
706 || startingDisplayed || startingMoved) {
707 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
708 pw.print(" startingView="); pw.print(startingView);
709 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800710 pw.print(" startingMoved="); pw.println(startingMoved);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800711 }
Jorim Jaggi0429f352015-12-22 16:29:16 +0100712 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -0800713 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
714 }
715 if (mPendingRelaunchCount != 0) {
716 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +0100717 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800718 }
719
720 @Override
721 public String toString() {
722 if (stringName == null) {
723 StringBuilder sb = new StringBuilder();
724 sb.append("AppWindowToken{");
725 sb.append(Integer.toHexString(System.identityHashCode(this)));
726 sb.append(" token="); sb.append(token); sb.append('}');
727 stringName = sb.toString();
728 }
729 return stringName;
730 }
Jeff Browne9bdb312012-04-05 15:30:10 -0700731}