blob: 776a1753aa72a6314a2afc41bdc03325d2111879 [file] [log] [blame]
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale6cae7652015-12-26 07:36:26 -080019import static android.app.ActivityManager.StackId;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080020import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080021import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
22import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Robert Carra1eb4392015-12-10 12:43:51 -080023import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale9017ec02016-02-25 08:55:25 -080024import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080025import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
26import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
27import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
28import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Chong Zhangf596cd52016-01-05 13:42:44 -080029import static com.android.server.wm.WindowManagerService.WINDOW_REPLACEMENT_TIMEOUT_DURATION;
Chong Zhang92147042016-05-09 12:47:11 -070030import static com.android.server.wm.WindowManagerService.H.NOTIFY_ACTIVITY_DRAWN;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080031
Jeff Brown4532e612012-04-05 14:27:12 -070032import com.android.server.input.InputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080033import com.android.server.wm.WindowManagerService.H;
34
Filip Gruszczynskia590c992015-11-25 16:45:26 -080035import android.annotation.NonNull;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080036import android.content.pm.ActivityInfo;
Jorim Jaggi26c8c422016-05-09 19:57:25 -070037import android.content.res.Configuration;
Jorim Jaggi0429f352015-12-22 16:29:16 +010038import android.graphics.Rect;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080039import android.os.Message;
40import android.os.RemoteException;
41import android.util.Slog;
42import android.view.IApplicationToken;
43import android.view.View;
44import android.view.WindowManager;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080045
46import java.io.PrintWriter;
Jorim Jaggi0429f352015-12-22 16:29:16 +010047import java.util.ArrayDeque;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080048import java.util.ArrayList;
49
50class AppTokenList extends ArrayList<AppWindowToken> {
51}
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080052
53/**
54 * Version of WindowToken that is specifically for a particular application (or
55 * really activity) that is displaying windows.
56 */
Craig Mautnere32c3072012-03-12 15:25:35 -070057class AppWindowToken extends WindowToken {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080058 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowToken" : TAG_WM;
59
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080060 // Non-null only for application tokens.
61 final IApplicationToken appToken;
62
63 // All of the windows and child windows that are included in this
64 // application token. Note this list is NOT sorted!
Craig Mautner96868332012-12-04 14:29:11 -080065 final WindowList allAppWindows = new WindowList();
Filip Gruszczynskia590c992015-11-25 16:45:26 -080066 @NonNull final AppWindowAnimator mAppAnimator;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070067
Dianne Hackborne30e02f2014-05-27 18:24:45 -070068 final boolean voiceInteraction;
69
Craig Mautner83162a92015-01-26 14:43:30 -080070 Task mTask;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080071 boolean appFullscreen;
72 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Craig Mautner4c5eb222013-11-18 12:59:05 -080073 boolean layoutConfigChanges;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070074 boolean showForAllUsers;
Yorke Lee0e852472016-06-15 10:03:18 -070075 int targetSdk;
Craig Mautnera2c77052012-03-26 12:14:43 -070076
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080077 // The input dispatching timeout for this application token in nanoseconds.
78 long inputDispatchingTimeoutNanos;
79
80 // These are used for determining when all windows associated with
81 // an activity have been drawn, so they can be made visible together
82 // at the same time.
Craig Mautner764983d2012-03-22 11:37:36 -070083 // initialize so that it doesn't match mTransactionSequence which is an int.
84 long lastTransactionSequence = Long.MIN_VALUE;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080085 int numInterestingWindows;
86 int numDrawnWindows;
87 boolean inPendingTransaction;
88 boolean allDrawn;
Craig Mautner7636dfb2012-11-16 15:24:11 -080089 // Set to true when this app creates a surface while in the middle of an animation. In that
90 // case do not clear allDrawn until the animation completes.
91 boolean deferClearAllDrawn;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080092
Chong Zhang8e4bda92016-05-04 15:08:18 -070093 // These are to track the app's real drawing status if there were no saved surfaces.
94 boolean allDrawnExcludingSaved;
95 int numInterestingWindowsExcludingSaved;
96 int numDrawnWindowsExclusingSaved;
97
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080098 // Is this window's surface needed? This is almost like hidden, except
99 // it will sometimes be true a little earlier: when the token has
100 // been shown, but is still waiting for its app transition to execute
101 // before making its windows shown.
102 boolean hiddenRequested;
103
104 // Have we told the window clients to hide themselves?
105 boolean clientHidden;
106
107 // Last visibility state we reported to the app token.
108 boolean reportedVisible;
109
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700110 // Last drawn state we reported to the app token.
111 boolean reportedDrawn;
112
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800113 // Set to true when the token has been removed from the window mgr.
114 boolean removed;
115
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800116 // Information about an application starting window if displayed.
117 StartingData startingData;
118 WindowState startingWindow;
119 View startingView;
120 boolean startingDisplayed;
121 boolean startingMoved;
122 boolean firstWindowDrawn;
123
124 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700125 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800126
Craig Mautner799bc1d2015-01-14 10:33:48 -0800127 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800128
Craig Mautnerbb742462014-07-07 15:28:55 -0700129 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700130 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700131
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800132 boolean mAlwaysFocusable;
133
Robert Carre12aece2016-02-02 22:43:27 -0800134 boolean mAppStopped;
Robert Carrfd10cd12016-06-29 16:41:50 -0700135 int mRotationAnimationHint;
Chong Zhangd78ddb42016-03-02 17:01:14 -0800136 int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800137
Jorim Jaggi0429f352015-12-22 16:29:16 +0100138 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700139 ArrayDeque<Configuration> mFrozenMergedConfig = new ArrayDeque<>();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100140
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700141 AppWindowToken(WindowManagerService _service, IApplicationToken _token,
142 boolean _voiceInteraction) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800143 super(_service, _token.asBinder(),
144 WindowManager.LayoutParams.TYPE_APPLICATION, true);
145 appWindowToken = this;
146 appToken = _token;
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700147 voiceInteraction = _voiceInteraction;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800148 mInputApplicationHandle = new InputApplicationHandle(this);
Craig Mautner322e4032012-07-13 13:35:20 -0700149 mAppAnimator = new AppWindowAnimator(this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800150 }
151
152 void sendAppVisibilityToClients() {
153 final int N = allAppWindows.size();
154 for (int i=0; i<N; i++) {
155 WindowState win = allAppWindows.get(i);
156 if (win == startingWindow && clientHidden) {
157 // Don't hide the starting window.
158 continue;
159 }
160 try {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800161 if (DEBUG_VISIBILITY) Slog.v(TAG,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800162 "Setting visibility of " + win + ": " + (!clientHidden));
163 win.mClient.dispatchAppVisibility(!clientHidden);
164 } catch (RemoteException e) {
165 }
166 }
167 }
168
Chong Zhang92147042016-05-09 12:47:11 -0700169 void setVisibleBeforeClientHidden() {
170 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
171 final WindowState w = allAppWindows.get(i);
172 w.setVisibleBeforeClientHidden();
173 }
174 }
175
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800176 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
177 firstWindowDrawn = true;
178
179 // We now have a good window to show, remove dead placeholders
180 removeAllDeadWindows();
181
182 if (startingData != null) {
183 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
184 + win.mToken + ": first real window is shown, no animation");
185 // If this initial window is animating, stop it -- we will do an animation to reveal
186 // it from behind the starting window, so there is no need for it to also be doing its
187 // own stuff.
188 winAnimator.clearAnimation();
189 winAnimator.mService.mFinishedStarting.add(this);
190 winAnimator.mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
191 }
192 updateReportedVisibilityLocked();
193 }
194
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800195 void updateReportedVisibilityLocked() {
196 if (appToken == null) {
197 return;
198 }
199
200 int numInteresting = 0;
201 int numVisible = 0;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700202 int numDrawn = 0;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800203 boolean nowGone = true;
204
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800205 if (DEBUG_VISIBILITY) Slog.v(TAG,
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700206 "Update reported visibility: " + this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800207 final int N = allAppWindows.size();
208 for (int i=0; i<N; i++) {
209 WindowState win = allAppWindows.get(i);
210 if (win == startingWindow || win.mAppFreezing
211 || win.mViewVisibility != View.VISIBLE
212 || win.mAttrs.type == TYPE_APPLICATION_STARTING
213 || win.mDestroying) {
214 continue;
215 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800216 if (DEBUG_VISIBILITY) {
217 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800218 + win.isDrawnLw()
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700219 + ", isAnimationSet=" + win.mWinAnimator.isAnimationSet());
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800220 if (!win.isDrawnLw()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800221 Slog.v(TAG, "Not displayed: s=" +
Robert Carre6a83512015-11-03 16:09:21 -0800222 win.mWinAnimator.mSurfaceController
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800223 + " pv=" + win.mPolicyVisibility
Craig Mautner749a7bb2012-04-02 13:49:53 -0700224 + " mDrawState=" + win.mWinAnimator.mDrawState
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800225 + " ah=" + win.mAttachedHidden
226 + " th="
227 + (win.mAppToken != null
228 ? win.mAppToken.hiddenRequested : false)
Craig Mautnera2c77052012-03-26 12:14:43 -0700229 + " a=" + win.mWinAnimator.mAnimating);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800230 }
231 }
232 numInteresting++;
233 if (win.isDrawnLw()) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700234 numDrawn++;
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700235 if (!win.mWinAnimator.isAnimationSet()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800236 numVisible++;
237 }
238 nowGone = false;
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700239 } else if (win.mWinAnimator.isAnimationSet()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800240 nowGone = false;
241 }
242 }
243
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700244 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800245 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700246 if (!nowGone) {
247 // If the app is not yet gone, then it can only become visible/drawn.
248 if (!nowDrawn) {
249 nowDrawn = reportedDrawn;
250 }
251 if (!nowVisible) {
252 nowVisible = reportedVisible;
253 }
254 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800255 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800256 + numInteresting + " visible=" + numVisible);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700257 if (nowDrawn != reportedDrawn) {
258 if (nowDrawn) {
259 Message m = service.mH.obtainMessage(
260 H.REPORT_APPLICATION_TOKEN_DRAWN, this);
261 service.mH.sendMessage(m);
262 }
263 reportedDrawn = nowDrawn;
264 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800265 if (nowVisible != reportedVisible) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800266 if (DEBUG_VISIBILITY) Slog.v(
267 TAG, "Visibility changed in " + this
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800268 + ": vis=" + nowVisible);
269 reportedVisible = nowVisible;
270 Message m = service.mH.obtainMessage(
271 H.REPORT_APPLICATION_TOKEN_WINDOWS,
272 nowVisible ? 1 : 0,
273 nowGone ? 1 : 0,
274 this);
275 service.mH.sendMessage(m);
276 }
277 }
278
279 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700280 WindowState candidate = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800281 int j = windows.size();
282 while (j > 0) {
283 j--;
284 WindowState win = windows.get(j);
285 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
286 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700287 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900288 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700289 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800290 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700291 candidate = win;
292 } else {
293 return win;
294 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800295 }
296 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700297 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800298 }
299
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800300 boolean windowsAreFocusable() {
301 return StackId.canReceiveKeys(mTask.mStack.mStackId) || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800302 }
303
Craig Mautner72669d12012-12-18 17:23:54 -0800304 boolean isVisible() {
305 final int N = allAppWindows.size();
Craig Mautner72669d12012-12-18 17:23:54 -0800306 for (int i=0; i<N; i++) {
307 WindowState win = allAppWindows.get(i);
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700308 // If we're animating with a saved surface, we're already visible.
309 // Return true so that the alpha doesn't get cleared.
Craig Mautner72669d12012-12-18 17:23:54 -0800310 if (!win.mAppFreezing
Chong Zhang92147042016-05-09 12:47:11 -0700311 && (win.mViewVisibility == View.VISIBLE || win.isAnimatingWithSavedSurface()
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700312 || (win.mWinAnimator.isAnimationSet()
Filip Gruszczynski57f76f12015-11-04 16:10:54 -0800313 && !service.mAppTransition.isTransitionSet()))
314 && !win.mDestroying
315 && win.isDrawnLw()) {
Craig Mautner72669d12012-12-18 17:23:54 -0800316 return true;
317 }
318 }
319 return false;
320 }
321
Craig Mautnere3119b72015-01-20 15:02:36 -0800322 void removeAppFromTaskLocked() {
323 mIsExiting = false;
324 removeAllWindows();
325
Craig Mautner83162a92015-01-26 14:43:30 -0800326 // Use local variable because removeAppToken will null out mTask.
327 final Task task = mTask;
Craig Mautnere3119b72015-01-20 15:02:36 -0800328 if (task != null) {
329 if (!task.removeAppToken(this)) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800330 Slog.e(TAG, "removeAppFromTaskLocked: token=" + this
Craig Mautnere3119b72015-01-20 15:02:36 -0800331 + " not found.");
332 }
333 task.mStack.mExitingAppTokens.remove(this);
334 }
335 }
336
Robert Carre12aece2016-02-02 22:43:27 -0800337 // Here we destroy surfaces which have been marked as eligible by the animator, taking care
338 // to ensure the client has finished with them. If the client could still be using them
339 // we will skip destruction and try again when the client has stopped.
340 void destroySurfaces() {
341 final ArrayList<WindowState> allWindows = (ArrayList<WindowState>) allAppWindows.clone();
342 final DisplayContentList displayList = new DisplayContentList();
343 for (int i = allWindows.size() - 1; i >= 0; i--) {
344 final WindowState win = allWindows.get(i);
Chong Zhangeb665572016-05-09 18:28:27 -0700345
346 if (!(mAppStopped || win.mWindowRemovalAllowed)) {
Robert Carre12aece2016-02-02 22:43:27 -0800347 continue;
348 }
349
Chong Zhangeb665572016-05-09 18:28:27 -0700350 win.mWinAnimator.destroyPreservedSurfaceLocked();
351
352 if (!win.mDestroying) {
Chong Zhang5471e902016-02-12 15:34:10 -0800353 continue;
Robert Carre12aece2016-02-02 22:43:27 -0800354 }
355
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800356 if (DEBUG_ADD_REMOVE) Slog.e(TAG_WM, "win=" + win
357 + " destroySurfaces: mAppStopped=" + mAppStopped
358 + " win.mWindowRemovalAllowed=" + win.mWindowRemovalAllowed
359 + " win.mRemoveOnExit=" + win.mRemoveOnExit);
360
Robert Carre12aece2016-02-02 22:43:27 -0800361 win.destroyOrSaveSurface();
362 if (win.mRemoveOnExit) {
Robert Carre12aece2016-02-02 22:43:27 -0800363 service.removeWindowInnerLocked(win);
364 }
365 final DisplayContent displayContent = win.getDisplayContent();
366 if (displayContent != null && !displayList.contains(displayContent)) {
367 displayList.add(displayContent);
368 }
369 win.mDestroying = false;
370 }
371 for (int i = 0; i < displayList.size(); i++) {
372 final DisplayContent displayContent = displayList.get(i);
373 service.mLayersController.assignLayersLocked(displayContent.getWindowList());
374 displayContent.layoutNeeded = true;
375 }
376 }
377
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800378 /**
379 * If the application has stopped it is okay to destroy any surfaces which were keeping alive
380 * in case they were still being used.
381 */
382 void notifyAppStopped(boolean stopped) {
383 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: stopped=" + stopped + " " + this);
384 mAppStopped = stopped;
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800385
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800386 if (stopped) {
387 destroySurfaces();
388 // Remove any starting window that was added for this app if they are still around.
389 mTask.mService.scheduleRemoveStartingWindowLocked(this);
390 }
Robert Carre12aece2016-02-02 22:43:27 -0800391 }
392
Chong Zhangbef461f2015-10-27 11:38:24 -0700393 /**
394 * Checks whether we should save surfaces for this app.
395 *
396 * @return true if the surfaces should be saved, false otherwise.
397 */
398 boolean shouldSaveSurface() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800399 // We want to save surface if the app's windows are "allDrawn".
400 // (If we started entering animation early with saved surfaces, allDrawn
401 // should have been restored to true. So we'll save again in that case
402 // even if app didn't actually finish drawing.)
403 return allDrawn;
Robert Carr13f7be9e2015-12-02 18:39:45 -0800404 }
405
Chong Zhang92147042016-05-09 12:47:11 -0700406 boolean canRestoreSurfaces() {
Chong Zhangb7b4a562016-04-28 15:30:33 -0700407 for (int i = allAppWindows.size() -1; i >= 0; i--) {
Chong Zhang92147042016-05-09 12:47:11 -0700408 final WindowState w = allAppWindows.get(i);
409 if (w.canRestoreSurface()) {
Robert Carr13f7be9e2015-12-02 18:39:45 -0800410 return true;
411 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700412 }
413 return false;
414 }
415
Chong Zhang92147042016-05-09 12:47:11 -0700416 void clearVisibleBeforeClientHidden() {
417 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
418 final WindowState w = allAppWindows.get(i);
419 w.clearVisibleBeforeClientHidden();
420 }
421 }
422
Chong Zhang8e4bda92016-05-04 15:08:18 -0700423 /**
424 * Whether the app has some window that is invisible in layout, but
425 * animating with saved surface.
426 */
427 boolean isAnimatingInvisibleWithSavedSurface() {
428 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
429 final WindowState w = allAppWindows.get(i);
430 if (w.isAnimatingInvisibleWithSavedSurface()) {
431 return true;
432 }
433 }
434 return false;
435 }
436
437 /**
438 * Hide all window surfaces that's still invisible in layout but animating
439 * with a saved surface, and mark them destroying.
440 */
441 void stopUsingSavedSurfaceLocked() {
442 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
443 final WindowState w = allAppWindows.get(i);
444 if (w.isAnimatingInvisibleWithSavedSurface()) {
445 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
446 "stopUsingSavedSurfaceLocked: " + w);
447 w.clearAnimatingWithSavedSurface();
448 w.mDestroying = true;
449 w.mWinAnimator.hide("stopUsingSavedSurfaceLocked");
450 w.mWinAnimator.mWallpaperControllerLocked.hideWallpapers(w);
451 }
452 }
453 destroySurfaces();
454 }
455
Chong Zhangf58631a2016-05-24 16:02:10 -0700456 void markSavedSurfaceExiting() {
457 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
458 final WindowState w = allAppWindows.get(i);
459 if (w.isAnimatingInvisibleWithSavedSurface()) {
460 w.mAnimatingExit = true;
461 w.mWinAnimator.mAnimating = true;
462 }
463 }
464 }
465
Chong Zhangbef461f2015-10-27 11:38:24 -0700466 void restoreSavedSurfaces() {
Chong Zhang92147042016-05-09 12:47:11 -0700467 if (!canRestoreSurfaces()) {
468 clearVisibleBeforeClientHidden();
Chong Zhangbef461f2015-10-27 11:38:24 -0700469 return;
470 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800471 // Check if we have enough drawn windows to mark allDrawn= true.
472 int numInteresting = 0;
473 int numDrawn = 0;
Chong Zhangb7b4a562016-04-28 15:30:33 -0700474 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
475 WindowState w = allAppWindows.get(i);
Chong Zhang92147042016-05-09 12:47:11 -0700476 if (w != startingWindow && !w.mAppDied && w.wasVisibleBeforeClientHidden()
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800477 && (!mAppAnimator.freezingScreen || !w.mAppFreezing)) {
478 numInteresting++;
Chong Zhang92147042016-05-09 12:47:11 -0700479 if (w.hasSavedSurface()) {
480 w.restoreSavedSurface();
481 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800482 if (w.isDrawnLw()) {
483 numDrawn++;
484 }
485 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700486 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800487
Chong Zhang92147042016-05-09 12:47:11 -0700488 if (!allDrawn) {
489 allDrawn = (numInteresting > 0) && (numInteresting == numDrawn);
490 if (allDrawn) {
491 service.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
492 }
493 }
494 clearVisibleBeforeClientHidden();
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800495
496 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
Chong Zhang92147042016-05-09 12:47:11 -0700497 "restoreSavedSurfaces: " + appWindowToken + " allDrawn=" + allDrawn
498 + " numInteresting=" + numInteresting + " numDrawn=" + numDrawn);
Chong Zhangbef461f2015-10-27 11:38:24 -0700499 }
500
501 void destroySavedSurfaces() {
Chong Zhangb7b4a562016-04-28 15:30:33 -0700502 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
503 WindowState win = allAppWindows.get(i);
Robert Carr13f7be9e2015-12-02 18:39:45 -0800504 win.destroySavedSurface();
Chong Zhangbef461f2015-10-27 11:38:24 -0700505 }
Chong Zhang92147042016-05-09 12:47:11 -0700506 }
507
508 void clearAllDrawn() {
509 allDrawn = false;
510 deferClearAllDrawn = false;
Chong Zhang8e4bda92016-05-04 15:08:18 -0700511 allDrawnExcludingSaved = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700512 }
513
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800514 @Override
Craig Mautner7c9ee192014-08-14 16:08:26 -0700515 void removeAllWindows() {
Craig Mautner7b4655d2014-11-20 12:13:22 -0800516 for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
517 // removeWindowLocked at bottom of loop may remove multiple entries from
518 // allAppWindows if the window to be removed has child windows. It also may
519 // not remove any windows from allAppWindows at all if win is exiting and
520 // currently animating away. This ensures that winNdx is monotonically decreasing
521 // and never beyond allAppWindows bounds.
522 winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
523 WindowState win = allAppWindows.get(winNdx);
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800524 if (DEBUG_WINDOW_MOVEMENT) {
525 Slog.w(TAG, "removeAllWindows: removing win=" + win);
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800526 }
527
Wale Ogunwalea6ab5c42015-04-24 09:00:25 -0700528 service.removeWindowLocked(win);
Craig Mautner7c9ee192014-08-14 16:08:26 -0700529 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800530 allAppWindows.clear();
531 windows.clear();
Craig Mautner7c9ee192014-08-14 16:08:26 -0700532 }
533
Chong Zhang112eb8c2015-11-02 11:17:00 -0800534 void removeAllDeadWindows() {
535 for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700536 // removeWindowLocked at bottom of loop may remove multiple entries from
537 // allAppWindows if the window to be removed has child windows. It also may
538 // not remove any windows from allAppWindows at all if win is exiting and
539 // currently animating away. This ensures that winNdx is monotonically decreasing
540 // and never beyond allAppWindows bounds.
541 winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800542 WindowState win = allAppWindows.get(winNdx);
543 if (win.mAppDied) {
Wale Ogunwale2728bf42016-03-03 11:03:26 -0800544 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800545 Slog.w(TAG, "removeAllDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800546 }
547 // Set mDestroying, we don't want any animation or delayed removal here.
548 win.mDestroying = true;
549 service.removeWindowLocked(win);
550 }
551 }
552 }
553
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700554 boolean hasWindowsAlive() {
555 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
556 if (!allAppWindows.get(i).mAppDied) {
557 return true;
558 }
559 }
560 return false;
561 }
562
Robert Carra1eb4392015-12-10 12:43:51 -0800563 void setReplacingWindows(boolean animate) {
564 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + appWindowToken
565 + " with replacing windows.");
566
567 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
568 final WindowState w = allAppWindows.get(i);
569 w.setReplacing(animate);
570 }
571 if (animate) {
572 // Set-up dummy animation so we can start treating windows associated with this
573 // token like they are in transition before the new app window is ready for us to
574 // run the real transition animation.
575 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
576 "setReplacingWindow() Setting dummy animation on: " + this);
577 mAppAnimator.setDummyAnimation();
578 }
579 }
580
Robert Carr23fa16b2016-01-13 13:19:58 -0800581 void setReplacingChildren() {
582 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + appWindowToken
583 + " with replacing child windows.");
584 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
585 final WindowState w = allAppWindows.get(i);
Robert Carrd1a010f2016-04-07 22:36:22 -0700586 if (w.shouldBeReplacedWithChildren()) {
Robert Carr23fa16b2016-01-13 13:19:58 -0800587 w.setReplacing(false /* animate */);
588 }
589 }
590 }
591
Chong Zhangf596cd52016-01-05 13:42:44 -0800592 void resetReplacingWindows() {
593 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Resetting app token " + appWindowToken
594 + " of replacing window marks.");
595
596 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
597 final WindowState w = allAppWindows.get(i);
598 w.resetReplacing();
599 }
600 }
601
Chong Zhang4d7369a2016-04-25 16:09:14 -0700602 void requestUpdateWallpaperIfNeeded() {
603 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
604 final WindowState w = allAppWindows.get(i);
605 w.requestUpdateWallpaperIfNeeded();
606 }
607 }
608
Chong Zhangd78ddb42016-03-02 17:01:14 -0800609 boolean isRelaunching() {
610 return mPendingRelaunchCount > 0;
611 }
612
613 void startRelaunching() {
614 if (canFreezeBounds()) {
615 freezeBounds();
616 }
617 mPendingRelaunchCount++;
618 }
619
620 void finishRelaunching() {
621 if (canFreezeBounds()) {
622 unfreezeBounds();
623 }
624 if (mPendingRelaunchCount > 0) {
625 mPendingRelaunchCount--;
626 }
627 }
628
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700629 void clearRelaunching() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700630 if (mPendingRelaunchCount == 0) {
631 return;
632 }
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700633 if (canFreezeBounds()) {
634 unfreezeBounds();
635 }
636 mPendingRelaunchCount = 0;
637 }
638
Robert Carra1eb4392015-12-10 12:43:51 -0800639 void addWindow(WindowState w) {
640 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
641 WindowState candidate = allAppWindows.get(i);
642 if (candidate.mWillReplaceWindow && candidate.mReplacingWindow == null &&
Robert Carr8bc89072016-04-08 13:29:59 -0700643 candidate.getWindowTag().toString().equals(w.getWindowTag().toString())) {
Robert Carra1eb4392015-12-10 12:43:51 -0800644 candidate.mReplacingWindow = w;
Robert Carrb439a632016-04-07 22:52:10 -0700645 w.mSkipEnterAnimationForSeamlessReplacement = !candidate.mAnimateReplacingWindow;
Chong Zhangf596cd52016-01-05 13:42:44 -0800646
647 // if we got a replacement window, reset the timeout to give drawing more time
Chong Zhang32de3652016-05-12 16:00:01 -0700648 service.scheduleReplacingWindowTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -0800649 }
650 }
651 allAppWindows.add(w);
652 }
653
654 boolean waitingForReplacement() {
655 for (int i = allAppWindows.size() -1; i >= 0; i--) {
656 WindowState candidate = allAppWindows.get(i);
657 if (candidate.mWillReplaceWindow) {
658 return true;
659 }
660 }
661 return false;
662 }
663
Chong Zhangf596cd52016-01-05 13:42:44 -0800664 void clearTimedoutReplacesLocked() {
Robert Carra1eb4392015-12-10 12:43:51 -0800665 for (int i = allAppWindows.size() - 1; i >= 0;
666 // removeWindowLocked at bottom of loop may remove multiple entries from
667 // allAppWindows if the window to be removed has child windows. It also may
668 // not remove any windows from allAppWindows at all if win is exiting and
669 // currently animating away. This ensures that winNdx is monotonically decreasing
670 // and never beyond allAppWindows bounds.
671 i = Math.min(i - 1, allAppWindows.size() - 1)) {
672 WindowState candidate = allAppWindows.get(i);
673 if (candidate.mWillReplaceWindow == false) {
674 continue;
675 }
676 candidate.mWillReplaceWindow = false;
Robert Carrb439a632016-04-07 22:52:10 -0700677 if (candidate.mReplacingWindow != null) {
678 candidate.mReplacingWindow.mSkipEnterAnimationForSeamlessReplacement = false;
679 }
Chong Zhangf596cd52016-01-05 13:42:44 -0800680 // Since the window already timed out, remove it immediately now.
681 // Use removeWindowInnerLocked() instead of removeWindowLocked(), as the latter
682 // delays removal on certain conditions, which will leave the stale window in the
683 // stack and marked mWillReplaceWindow=false, so the window will never be removed.
684 service.removeWindowInnerLocked(candidate);
Robert Carra1eb4392015-12-10 12:43:51 -0800685 }
686 }
687
Chong Zhangd78ddb42016-03-02 17:01:14 -0800688 private boolean canFreezeBounds() {
689 // For freeform windows, we can't freeze the bounds at the moment because this would make
690 // the resizing unresponsive.
691 return mTask != null && !mTask.inFreeformWorkspace();
692 }
693
Jorim Jaggi0429f352015-12-22 16:29:16 +0100694 /**
695 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
696 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
697 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
698 * with a queue.
699 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800700 private void freezeBounds() {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100701 mFrozenBounds.offer(new Rect(mTask.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700702
703 if (mTask.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
704 // We didn't call prepareFreezingBounds on the task, so use the current value.
705 final Configuration config = new Configuration(service.mCurConfiguration);
706 config.updateFrom(mTask.mOverrideConfig);
707 mFrozenMergedConfig.offer(config);
708 } else {
709 mFrozenMergedConfig.offer(new Configuration(mTask.mPreparedFrozenMergedConfig));
710 }
711 mTask.mPreparedFrozenMergedConfig.setToDefaults();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100712 }
713
714 /**
715 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
716 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800717 private void unfreezeBounds() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700718 if (!mFrozenBounds.isEmpty()) {
719 mFrozenBounds.remove();
720 }
721 if (!mFrozenMergedConfig.isEmpty()) {
722 mFrozenMergedConfig.remove();
723 }
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100724 for (int i = windows.size() - 1; i >= 0; i--) {
725 final WindowState win = windows.get(i);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800726 if (!win.mHasSurface) {
727 continue;
728 }
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100729 win.mLayoutNeeded = true;
730 win.setDisplayLayoutNeeded();
731 if (!service.mResizingWindows.contains(win)) {
732 service.mResizingWindows.add(win);
733 }
734 }
735 service.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100736 }
737
Craig Mautnerdbb79912012-03-01 18:59:14 -0800738 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800739 void dump(PrintWriter pw, String prefix) {
740 super.dump(pw, prefix);
741 if (appToken != null) {
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700742 pw.print(prefix); pw.print("app=true voiceInteraction="); pw.println(voiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800743 }
744 if (allAppWindows.size() > 0) {
745 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
746 }
Craig Mautner83162a92015-01-26 14:43:30 -0800747 pw.print(prefix); pw.print("task="); pw.println(mTask);
748 pw.print(prefix); pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800749 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
750 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
751 pw.print(" clientHidden="); pw.print(clientHidden);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700752 pw.print(" reportedDrawn="); pw.print(reportedDrawn);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800753 pw.print(" reportedVisible="); pw.println(reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -0700754 if (paused) {
755 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800756 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800757 if (mAppStopped) {
758 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
759 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800760 if (numInterestingWindows != 0 || numDrawnWindows != 0
Craig Mautner6fbda632012-07-03 09:26:39 -0700761 || allDrawn || mAppAnimator.allDrawn) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800762 pw.print(prefix); pw.print("numInterestingWindows=");
763 pw.print(numInterestingWindows);
764 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
765 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -0700766 pw.print(" allDrawn="); pw.print(allDrawn);
767 pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
768 pw.println(")");
769 }
770 if (inPendingTransaction) {
771 pw.print(prefix); pw.print("inPendingTransaction=");
772 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800773 }
Craig Mautner799bc1d2015-01-14 10:33:48 -0800774 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800775 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
776 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -0800777 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -0800778 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800779 }
780 if (startingWindow != null || startingView != null
781 || startingDisplayed || startingMoved) {
782 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
783 pw.print(" startingView="); pw.print(startingView);
784 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800785 pw.print(" startingMoved="); pw.println(startingMoved);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800786 }
Jorim Jaggi0429f352015-12-22 16:29:16 +0100787 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -0800788 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700789 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -0800790 }
791 if (mPendingRelaunchCount != 0) {
792 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +0100793 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800794 }
795
796 @Override
797 public String toString() {
798 if (stringName == null) {
799 StringBuilder sb = new StringBuilder();
800 sb.append("AppWindowToken{");
801 sb.append(Integer.toHexString(System.identityHashCode(this)));
802 sb.append(" token="); sb.append(token); sb.append('}');
803 stringName = sb.toString();
804 }
805 return stringName;
806 }
Jeff Browne9bdb312012-04-05 15:30:10 -0700807}