blob: edcc32e5b58765b8bc7372672ed4a59b1d688f3e [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;
Chong Zhangd78ddb42016-03-02 17:01:14 -0800135 int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800136
Jorim Jaggi0429f352015-12-22 16:29:16 +0100137 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700138 ArrayDeque<Configuration> mFrozenMergedConfig = new ArrayDeque<>();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100139
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700140 AppWindowToken(WindowManagerService _service, IApplicationToken _token,
141 boolean _voiceInteraction) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800142 super(_service, _token.asBinder(),
143 WindowManager.LayoutParams.TYPE_APPLICATION, true);
144 appWindowToken = this;
145 appToken = _token;
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700146 voiceInteraction = _voiceInteraction;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800147 mInputApplicationHandle = new InputApplicationHandle(this);
Craig Mautner322e4032012-07-13 13:35:20 -0700148 mAppAnimator = new AppWindowAnimator(this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800149 }
150
151 void sendAppVisibilityToClients() {
152 final int N = allAppWindows.size();
153 for (int i=0; i<N; i++) {
154 WindowState win = allAppWindows.get(i);
155 if (win == startingWindow && clientHidden) {
156 // Don't hide the starting window.
157 continue;
158 }
159 try {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800160 if (DEBUG_VISIBILITY) Slog.v(TAG,
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800161 "Setting visibility of " + win + ": " + (!clientHidden));
162 win.mClient.dispatchAppVisibility(!clientHidden);
163 } catch (RemoteException e) {
164 }
165 }
166 }
167
Chong Zhang92147042016-05-09 12:47:11 -0700168 void setVisibleBeforeClientHidden() {
169 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
170 final WindowState w = allAppWindows.get(i);
171 w.setVisibleBeforeClientHidden();
172 }
173 }
174
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800175 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
176 firstWindowDrawn = true;
177
178 // We now have a good window to show, remove dead placeholders
179 removeAllDeadWindows();
180
181 if (startingData != null) {
182 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
183 + win.mToken + ": first real window is shown, no animation");
184 // If this initial window is animating, stop it -- we will do an animation to reveal
185 // it from behind the starting window, so there is no need for it to also be doing its
186 // own stuff.
187 winAnimator.clearAnimation();
188 winAnimator.mService.mFinishedStarting.add(this);
189 winAnimator.mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
190 }
191 updateReportedVisibilityLocked();
192 }
193
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800194 void updateReportedVisibilityLocked() {
195 if (appToken == null) {
196 return;
197 }
198
199 int numInteresting = 0;
200 int numVisible = 0;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700201 int numDrawn = 0;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800202 boolean nowGone = true;
203
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800204 if (DEBUG_VISIBILITY) Slog.v(TAG,
Craig Mautnerc8bc97e2012-04-02 12:54:54 -0700205 "Update reported visibility: " + this);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800206 final int N = allAppWindows.size();
207 for (int i=0; i<N; i++) {
208 WindowState win = allAppWindows.get(i);
209 if (win == startingWindow || win.mAppFreezing
210 || win.mViewVisibility != View.VISIBLE
211 || win.mAttrs.type == TYPE_APPLICATION_STARTING
212 || win.mDestroying) {
213 continue;
214 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800215 if (DEBUG_VISIBILITY) {
216 Slog.v(TAG, "Win " + win + ": isDrawn="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800217 + win.isDrawnLw()
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700218 + ", isAnimationSet=" + win.mWinAnimator.isAnimationSet());
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800219 if (!win.isDrawnLw()) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800220 Slog.v(TAG, "Not displayed: s=" +
Robert Carre6a83512015-11-03 16:09:21 -0800221 win.mWinAnimator.mSurfaceController
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800222 + " pv=" + win.mPolicyVisibility
Craig Mautner749a7bb2012-04-02 13:49:53 -0700223 + " mDrawState=" + win.mWinAnimator.mDrawState
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800224 + " ah=" + win.mAttachedHidden
225 + " th="
226 + (win.mAppToken != null
227 ? win.mAppToken.hiddenRequested : false)
Craig Mautnera2c77052012-03-26 12:14:43 -0700228 + " a=" + win.mWinAnimator.mAnimating);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800229 }
230 }
231 numInteresting++;
232 if (win.isDrawnLw()) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700233 numDrawn++;
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700234 if (!win.mWinAnimator.isAnimationSet()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800235 numVisible++;
236 }
237 nowGone = false;
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700238 } else if (win.mWinAnimator.isAnimationSet()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800239 nowGone = false;
240 }
241 }
242
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700243 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800244 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700245 if (!nowGone) {
246 // If the app is not yet gone, then it can only become visible/drawn.
247 if (!nowDrawn) {
248 nowDrawn = reportedDrawn;
249 }
250 if (!nowVisible) {
251 nowVisible = reportedVisible;
252 }
253 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800254 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800255 + numInteresting + " visible=" + numVisible);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700256 if (nowDrawn != reportedDrawn) {
257 if (nowDrawn) {
258 Message m = service.mH.obtainMessage(
259 H.REPORT_APPLICATION_TOKEN_DRAWN, this);
260 service.mH.sendMessage(m);
261 }
262 reportedDrawn = nowDrawn;
263 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800264 if (nowVisible != reportedVisible) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800265 if (DEBUG_VISIBILITY) Slog.v(
266 TAG, "Visibility changed in " + this
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800267 + ": vis=" + nowVisible);
268 reportedVisible = nowVisible;
269 Message m = service.mH.obtainMessage(
270 H.REPORT_APPLICATION_TOKEN_WINDOWS,
271 nowVisible ? 1 : 0,
272 nowGone ? 1 : 0,
273 this);
274 service.mH.sendMessage(m);
275 }
276 }
277
278 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700279 WindowState candidate = null;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800280 int j = windows.size();
281 while (j > 0) {
282 j--;
283 WindowState win = windows.get(j);
284 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
285 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700286 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900287 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700288 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800289 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700290 candidate = win;
291 } else {
292 return win;
293 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800294 }
295 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700296 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800297 }
298
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800299 boolean windowsAreFocusable() {
300 return StackId.canReceiveKeys(mTask.mStack.mStackId) || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800301 }
302
Craig Mautner72669d12012-12-18 17:23:54 -0800303 boolean isVisible() {
304 final int N = allAppWindows.size();
Craig Mautner72669d12012-12-18 17:23:54 -0800305 for (int i=0; i<N; i++) {
306 WindowState win = allAppWindows.get(i);
Chong Zhangdb20b5f2015-10-23 14:01:43 -0700307 // If we're animating with a saved surface, we're already visible.
308 // Return true so that the alpha doesn't get cleared.
Craig Mautner72669d12012-12-18 17:23:54 -0800309 if (!win.mAppFreezing
Chong Zhang92147042016-05-09 12:47:11 -0700310 && (win.mViewVisibility == View.VISIBLE || win.isAnimatingWithSavedSurface()
Jorim Jaggi5c80c412016-04-19 20:03:47 -0700311 || (win.mWinAnimator.isAnimationSet()
Filip Gruszczynski57f76f12015-11-04 16:10:54 -0800312 && !service.mAppTransition.isTransitionSet()))
313 && !win.mDestroying
314 && win.isDrawnLw()) {
Craig Mautner72669d12012-12-18 17:23:54 -0800315 return true;
316 }
317 }
318 return false;
319 }
320
Craig Mautnere3119b72015-01-20 15:02:36 -0800321 void removeAppFromTaskLocked() {
322 mIsExiting = false;
323 removeAllWindows();
324
Craig Mautner83162a92015-01-26 14:43:30 -0800325 // Use local variable because removeAppToken will null out mTask.
326 final Task task = mTask;
Craig Mautnere3119b72015-01-20 15:02:36 -0800327 if (task != null) {
328 if (!task.removeAppToken(this)) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800329 Slog.e(TAG, "removeAppFromTaskLocked: token=" + this
Craig Mautnere3119b72015-01-20 15:02:36 -0800330 + " not found.");
331 }
332 task.mStack.mExitingAppTokens.remove(this);
333 }
334 }
335
Robert Carre12aece2016-02-02 22:43:27 -0800336 // Here we destroy surfaces which have been marked as eligible by the animator, taking care
337 // to ensure the client has finished with them. If the client could still be using them
338 // we will skip destruction and try again when the client has stopped.
339 void destroySurfaces() {
340 final ArrayList<WindowState> allWindows = (ArrayList<WindowState>) allAppWindows.clone();
341 final DisplayContentList displayList = new DisplayContentList();
342 for (int i = allWindows.size() - 1; i >= 0; i--) {
343 final WindowState win = allWindows.get(i);
Chong Zhangeb665572016-05-09 18:28:27 -0700344
345 if (!(mAppStopped || win.mWindowRemovalAllowed)) {
Robert Carre12aece2016-02-02 22:43:27 -0800346 continue;
347 }
348
Chong Zhangeb665572016-05-09 18:28:27 -0700349 win.mWinAnimator.destroyPreservedSurfaceLocked();
350
351 if (!win.mDestroying) {
Chong Zhang5471e902016-02-12 15:34:10 -0800352 continue;
Robert Carre12aece2016-02-02 22:43:27 -0800353 }
354
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800355 if (DEBUG_ADD_REMOVE) Slog.e(TAG_WM, "win=" + win
356 + " destroySurfaces: mAppStopped=" + mAppStopped
357 + " win.mWindowRemovalAllowed=" + win.mWindowRemovalAllowed
358 + " win.mRemoveOnExit=" + win.mRemoveOnExit);
359
Robert Carre12aece2016-02-02 22:43:27 -0800360 win.destroyOrSaveSurface();
361 if (win.mRemoveOnExit) {
Robert Carre12aece2016-02-02 22:43:27 -0800362 service.removeWindowInnerLocked(win);
363 }
364 final DisplayContent displayContent = win.getDisplayContent();
365 if (displayContent != null && !displayList.contains(displayContent)) {
366 displayList.add(displayContent);
367 }
368 win.mDestroying = false;
369 }
370 for (int i = 0; i < displayList.size(); i++) {
371 final DisplayContent displayContent = displayList.get(i);
372 service.mLayersController.assignLayersLocked(displayContent.getWindowList());
373 displayContent.layoutNeeded = true;
374 }
375 }
376
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800377 /**
378 * If the application has stopped it is okay to destroy any surfaces which were keeping alive
379 * in case they were still being used.
380 */
381 void notifyAppStopped(boolean stopped) {
382 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: stopped=" + stopped + " " + this);
383 mAppStopped = stopped;
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800384
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800385 if (stopped) {
386 destroySurfaces();
387 // Remove any starting window that was added for this app if they are still around.
388 mTask.mService.scheduleRemoveStartingWindowLocked(this);
389 }
Robert Carre12aece2016-02-02 22:43:27 -0800390 }
391
Chong Zhangbef461f2015-10-27 11:38:24 -0700392 /**
393 * Checks whether we should save surfaces for this app.
394 *
395 * @return true if the surfaces should be saved, false otherwise.
396 */
397 boolean shouldSaveSurface() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800398 // We want to save surface if the app's windows are "allDrawn".
399 // (If we started entering animation early with saved surfaces, allDrawn
400 // should have been restored to true. So we'll save again in that case
401 // even if app didn't actually finish drawing.)
402 return allDrawn;
Robert Carr13f7be9e2015-12-02 18:39:45 -0800403 }
404
Chong Zhang92147042016-05-09 12:47:11 -0700405 boolean canRestoreSurfaces() {
Chong Zhangb7b4a562016-04-28 15:30:33 -0700406 for (int i = allAppWindows.size() -1; i >= 0; i--) {
Chong Zhang92147042016-05-09 12:47:11 -0700407 final WindowState w = allAppWindows.get(i);
408 if (w.canRestoreSurface()) {
Robert Carr13f7be9e2015-12-02 18:39:45 -0800409 return true;
410 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700411 }
412 return false;
413 }
414
Chong Zhang92147042016-05-09 12:47:11 -0700415 void clearVisibleBeforeClientHidden() {
416 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
417 final WindowState w = allAppWindows.get(i);
418 w.clearVisibleBeforeClientHidden();
419 }
420 }
421
Chong Zhang8e4bda92016-05-04 15:08:18 -0700422 /**
423 * Whether the app has some window that is invisible in layout, but
424 * animating with saved surface.
425 */
426 boolean isAnimatingInvisibleWithSavedSurface() {
427 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
428 final WindowState w = allAppWindows.get(i);
429 if (w.isAnimatingInvisibleWithSavedSurface()) {
430 return true;
431 }
432 }
433 return false;
434 }
435
436 /**
437 * Hide all window surfaces that's still invisible in layout but animating
438 * with a saved surface, and mark them destroying.
439 */
440 void stopUsingSavedSurfaceLocked() {
441 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
442 final WindowState w = allAppWindows.get(i);
443 if (w.isAnimatingInvisibleWithSavedSurface()) {
444 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
445 "stopUsingSavedSurfaceLocked: " + w);
446 w.clearAnimatingWithSavedSurface();
447 w.mDestroying = true;
448 w.mWinAnimator.hide("stopUsingSavedSurfaceLocked");
449 w.mWinAnimator.mWallpaperControllerLocked.hideWallpapers(w);
450 }
451 }
452 destroySurfaces();
453 }
454
Chong Zhangf58631a2016-05-24 16:02:10 -0700455 void markSavedSurfaceExiting() {
456 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
457 final WindowState w = allAppWindows.get(i);
458 if (w.isAnimatingInvisibleWithSavedSurface()) {
459 w.mAnimatingExit = true;
460 w.mWinAnimator.mAnimating = true;
461 }
462 }
463 }
464
Chong Zhangbef461f2015-10-27 11:38:24 -0700465 void restoreSavedSurfaces() {
Chong Zhang92147042016-05-09 12:47:11 -0700466 if (!canRestoreSurfaces()) {
467 clearVisibleBeforeClientHidden();
Chong Zhangbef461f2015-10-27 11:38:24 -0700468 return;
469 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800470 // Check if we have enough drawn windows to mark allDrawn= true.
471 int numInteresting = 0;
472 int numDrawn = 0;
Chong Zhangb7b4a562016-04-28 15:30:33 -0700473 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
474 WindowState w = allAppWindows.get(i);
Chong Zhang92147042016-05-09 12:47:11 -0700475 if (w != startingWindow && !w.mAppDied && w.wasVisibleBeforeClientHidden()
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800476 && (!mAppAnimator.freezingScreen || !w.mAppFreezing)) {
477 numInteresting++;
Chong Zhang92147042016-05-09 12:47:11 -0700478 if (w.hasSavedSurface()) {
479 w.restoreSavedSurface();
480 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800481 if (w.isDrawnLw()) {
482 numDrawn++;
483 }
484 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700485 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800486
Chong Zhang92147042016-05-09 12:47:11 -0700487 if (!allDrawn) {
488 allDrawn = (numInteresting > 0) && (numInteresting == numDrawn);
489 if (allDrawn) {
490 service.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
491 }
492 }
493 clearVisibleBeforeClientHidden();
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800494
495 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
Chong Zhang92147042016-05-09 12:47:11 -0700496 "restoreSavedSurfaces: " + appWindowToken + " allDrawn=" + allDrawn
497 + " numInteresting=" + numInteresting + " numDrawn=" + numDrawn);
Chong Zhangbef461f2015-10-27 11:38:24 -0700498 }
499
500 void destroySavedSurfaces() {
Chong Zhangb7b4a562016-04-28 15:30:33 -0700501 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
502 WindowState win = allAppWindows.get(i);
Robert Carr13f7be9e2015-12-02 18:39:45 -0800503 win.destroySavedSurface();
Chong Zhangbef461f2015-10-27 11:38:24 -0700504 }
Chong Zhang92147042016-05-09 12:47:11 -0700505 }
506
507 void clearAllDrawn() {
508 allDrawn = false;
509 deferClearAllDrawn = false;
Chong Zhang8e4bda92016-05-04 15:08:18 -0700510 allDrawnExcludingSaved = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700511 }
512
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800513 @Override
Craig Mautner7c9ee192014-08-14 16:08:26 -0700514 void removeAllWindows() {
Craig Mautner7b4655d2014-11-20 12:13:22 -0800515 for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
516 // removeWindowLocked at bottom of loop may remove multiple entries from
517 // allAppWindows if the window to be removed has child windows. It also may
518 // not remove any windows from allAppWindows at all if win is exiting and
519 // currently animating away. This ensures that winNdx is monotonically decreasing
520 // and never beyond allAppWindows bounds.
521 winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
522 WindowState win = allAppWindows.get(winNdx);
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800523 if (DEBUG_WINDOW_MOVEMENT) {
524 Slog.w(TAG, "removeAllWindows: removing win=" + win);
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800525 }
526
Wale Ogunwalea6ab5c42015-04-24 09:00:25 -0700527 service.removeWindowLocked(win);
Craig Mautner7c9ee192014-08-14 16:08:26 -0700528 }
Craig Mautnere3119b72015-01-20 15:02:36 -0800529 allAppWindows.clear();
530 windows.clear();
Craig Mautner7c9ee192014-08-14 16:08:26 -0700531 }
532
Chong Zhang112eb8c2015-11-02 11:17:00 -0800533 void removeAllDeadWindows() {
534 for (int winNdx = allAppWindows.size() - 1; winNdx >= 0;
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700535 // removeWindowLocked at bottom of loop may remove multiple entries from
536 // allAppWindows if the window to be removed has child windows. It also may
537 // not remove any windows from allAppWindows at all if win is exiting and
538 // currently animating away. This ensures that winNdx is monotonically decreasing
539 // and never beyond allAppWindows bounds.
540 winNdx = Math.min(winNdx - 1, allAppWindows.size() - 1)) {
Chong Zhang112eb8c2015-11-02 11:17:00 -0800541 WindowState win = allAppWindows.get(winNdx);
542 if (win.mAppDied) {
Wale Ogunwale2728bf42016-03-03 11:03:26 -0800543 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800544 Slog.w(TAG, "removeAllDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800545 }
546 // Set mDestroying, we don't want any animation or delayed removal here.
547 win.mDestroying = true;
548 service.removeWindowLocked(win);
549 }
550 }
551 }
552
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700553 boolean hasWindowsAlive() {
554 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
555 if (!allAppWindows.get(i).mAppDied) {
556 return true;
557 }
558 }
559 return false;
560 }
561
Robert Carra1eb4392015-12-10 12:43:51 -0800562 void setReplacingWindows(boolean animate) {
563 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + appWindowToken
564 + " with replacing windows.");
565
566 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
567 final WindowState w = allAppWindows.get(i);
568 w.setReplacing(animate);
569 }
570 if (animate) {
571 // Set-up dummy animation so we can start treating windows associated with this
572 // token like they are in transition before the new app window is ready for us to
573 // run the real transition animation.
574 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
575 "setReplacingWindow() Setting dummy animation on: " + this);
576 mAppAnimator.setDummyAnimation();
577 }
578 }
579
Robert Carr23fa16b2016-01-13 13:19:58 -0800580 void setReplacingChildren() {
581 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + appWindowToken
582 + " with replacing child windows.");
583 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
584 final WindowState w = allAppWindows.get(i);
Robert Carrd1a010f2016-04-07 22:36:22 -0700585 if (w.shouldBeReplacedWithChildren()) {
Robert Carr23fa16b2016-01-13 13:19:58 -0800586 w.setReplacing(false /* animate */);
587 }
588 }
589 }
590
Chong Zhangf596cd52016-01-05 13:42:44 -0800591 void resetReplacingWindows() {
592 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Resetting app token " + appWindowToken
593 + " of replacing window marks.");
594
595 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
596 final WindowState w = allAppWindows.get(i);
597 w.resetReplacing();
598 }
599 }
600
Chong Zhang4d7369a2016-04-25 16:09:14 -0700601 void requestUpdateWallpaperIfNeeded() {
602 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
603 final WindowState w = allAppWindows.get(i);
604 w.requestUpdateWallpaperIfNeeded();
605 }
606 }
607
Chong Zhangd78ddb42016-03-02 17:01:14 -0800608 boolean isRelaunching() {
609 return mPendingRelaunchCount > 0;
610 }
611
612 void startRelaunching() {
613 if (canFreezeBounds()) {
614 freezeBounds();
615 }
616 mPendingRelaunchCount++;
617 }
618
619 void finishRelaunching() {
620 if (canFreezeBounds()) {
621 unfreezeBounds();
622 }
623 if (mPendingRelaunchCount > 0) {
624 mPendingRelaunchCount--;
625 }
626 }
627
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700628 void clearRelaunching() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700629 if (mPendingRelaunchCount == 0) {
630 return;
631 }
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700632 if (canFreezeBounds()) {
633 unfreezeBounds();
634 }
635 mPendingRelaunchCount = 0;
636 }
637
Robert Carra1eb4392015-12-10 12:43:51 -0800638 void addWindow(WindowState w) {
639 for (int i = allAppWindows.size() - 1; i >= 0; i--) {
640 WindowState candidate = allAppWindows.get(i);
641 if (candidate.mWillReplaceWindow && candidate.mReplacingWindow == null &&
Robert Carr8bc89072016-04-08 13:29:59 -0700642 candidate.getWindowTag().toString().equals(w.getWindowTag().toString())) {
Robert Carra1eb4392015-12-10 12:43:51 -0800643 candidate.mReplacingWindow = w;
Robert Carrb439a632016-04-07 22:52:10 -0700644 w.mSkipEnterAnimationForSeamlessReplacement = !candidate.mAnimateReplacingWindow;
Chong Zhangf596cd52016-01-05 13:42:44 -0800645
646 // if we got a replacement window, reset the timeout to give drawing more time
Chong Zhang32de3652016-05-12 16:00:01 -0700647 service.scheduleReplacingWindowTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -0800648 }
649 }
650 allAppWindows.add(w);
651 }
652
653 boolean waitingForReplacement() {
654 for (int i = allAppWindows.size() -1; i >= 0; i--) {
655 WindowState candidate = allAppWindows.get(i);
656 if (candidate.mWillReplaceWindow) {
657 return true;
658 }
659 }
660 return false;
661 }
662
Chong Zhangf596cd52016-01-05 13:42:44 -0800663 void clearTimedoutReplacesLocked() {
Robert Carra1eb4392015-12-10 12:43:51 -0800664 for (int i = allAppWindows.size() - 1; i >= 0;
665 // removeWindowLocked at bottom of loop may remove multiple entries from
666 // allAppWindows if the window to be removed has child windows. It also may
667 // not remove any windows from allAppWindows at all if win is exiting and
668 // currently animating away. This ensures that winNdx is monotonically decreasing
669 // and never beyond allAppWindows bounds.
670 i = Math.min(i - 1, allAppWindows.size() - 1)) {
671 WindowState candidate = allAppWindows.get(i);
672 if (candidate.mWillReplaceWindow == false) {
673 continue;
674 }
675 candidate.mWillReplaceWindow = false;
Robert Carrb439a632016-04-07 22:52:10 -0700676 if (candidate.mReplacingWindow != null) {
677 candidate.mReplacingWindow.mSkipEnterAnimationForSeamlessReplacement = false;
678 }
Chong Zhangf596cd52016-01-05 13:42:44 -0800679 // Since the window already timed out, remove it immediately now.
680 // Use removeWindowInnerLocked() instead of removeWindowLocked(), as the latter
681 // delays removal on certain conditions, which will leave the stale window in the
682 // stack and marked mWillReplaceWindow=false, so the window will never be removed.
683 service.removeWindowInnerLocked(candidate);
Robert Carra1eb4392015-12-10 12:43:51 -0800684 }
685 }
686
Chong Zhangd78ddb42016-03-02 17:01:14 -0800687 private boolean canFreezeBounds() {
688 // For freeform windows, we can't freeze the bounds at the moment because this would make
689 // the resizing unresponsive.
690 return mTask != null && !mTask.inFreeformWorkspace();
691 }
692
Jorim Jaggi0429f352015-12-22 16:29:16 +0100693 /**
694 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
695 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
696 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
697 * with a queue.
698 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800699 private void freezeBounds() {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100700 mFrozenBounds.offer(new Rect(mTask.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700701
702 if (mTask.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
703 // We didn't call prepareFreezingBounds on the task, so use the current value.
704 final Configuration config = new Configuration(service.mCurConfiguration);
705 config.updateFrom(mTask.mOverrideConfig);
706 mFrozenMergedConfig.offer(config);
707 } else {
708 mFrozenMergedConfig.offer(new Configuration(mTask.mPreparedFrozenMergedConfig));
709 }
710 mTask.mPreparedFrozenMergedConfig.setToDefaults();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100711 }
712
713 /**
714 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
715 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800716 private void unfreezeBounds() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700717 if (!mFrozenBounds.isEmpty()) {
718 mFrozenBounds.remove();
719 }
720 if (!mFrozenMergedConfig.isEmpty()) {
721 mFrozenMergedConfig.remove();
722 }
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100723 for (int i = windows.size() - 1; i >= 0; i--) {
724 final WindowState win = windows.get(i);
Jorim Jaggi69abc192016-02-04 19:34:00 -0800725 if (!win.mHasSurface) {
726 continue;
727 }
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100728 win.mLayoutNeeded = true;
729 win.setDisplayLayoutNeeded();
730 if (!service.mResizingWindows.contains(win)) {
731 service.mResizingWindows.add(win);
732 }
733 }
734 service.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100735 }
736
Craig Mautnerdbb79912012-03-01 18:59:14 -0800737 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800738 void dump(PrintWriter pw, String prefix) {
739 super.dump(pw, prefix);
740 if (appToken != null) {
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700741 pw.print(prefix); pw.print("app=true voiceInteraction="); pw.println(voiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800742 }
743 if (allAppWindows.size() > 0) {
744 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
745 }
Craig Mautner83162a92015-01-26 14:43:30 -0800746 pw.print(prefix); pw.print("task="); pw.println(mTask);
747 pw.print(prefix); pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800748 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
749 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
750 pw.print(" clientHidden="); pw.print(clientHidden);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700751 pw.print(" reportedDrawn="); pw.print(reportedDrawn);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800752 pw.print(" reportedVisible="); pw.println(reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -0700753 if (paused) {
754 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800755 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800756 if (mAppStopped) {
757 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
758 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800759 if (numInterestingWindows != 0 || numDrawnWindows != 0
Craig Mautner6fbda632012-07-03 09:26:39 -0700760 || allDrawn || mAppAnimator.allDrawn) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800761 pw.print(prefix); pw.print("numInterestingWindows=");
762 pw.print(numInterestingWindows);
763 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
764 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -0700765 pw.print(" allDrawn="); pw.print(allDrawn);
766 pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
767 pw.println(")");
768 }
769 if (inPendingTransaction) {
770 pw.print(prefix); pw.print("inPendingTransaction=");
771 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800772 }
Craig Mautner799bc1d2015-01-14 10:33:48 -0800773 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800774 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
775 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -0800776 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -0800777 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800778 }
779 if (startingWindow != null || startingView != null
780 || startingDisplayed || startingMoved) {
781 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
782 pw.print(" startingView="); pw.print(startingView);
783 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800784 pw.print(" startingMoved="); pw.println(startingMoved);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800785 }
Jorim Jaggi0429f352015-12-22 16:29:16 +0100786 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -0800787 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700788 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -0800789 }
790 if (mPendingRelaunchCount != 0) {
791 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +0100792 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800793 }
794
795 @Override
796 public String toString() {
797 if (stringName == null) {
798 StringBuilder sb = new StringBuilder();
799 sb.append("AppWindowToken{");
800 sb.append(Integer.toHexString(System.identityHashCode(this)));
801 sb.append(" token="); sb.append(token); sb.append('}');
802 stringName = sb.toString();
803 }
804 return stringName;
805 }
Jeff Browne9bdb312012-04-05 15:30:10 -0700806}