blob: 47b5f3de446ae01560fdb48c5c459765cac87e41 [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;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070020import static android.view.Display.DEFAULT_DISPLAY;
Wale Ogunwaled1c37912016-08-16 03:19:39 -070021import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080022import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070023import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080024import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
25import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Robert Carra1eb4392015-12-10 12:43:51 -080026import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070027import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
28import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwale9017ec02016-02-25 08:55:25 -080029import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080030import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
31import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_MOVEMENT;
32import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
33import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Chong Zhang92147042016-05-09 12:47:11 -070034import static com.android.server.wm.WindowManagerService.H.NOTIFY_ACTIVITY_DRAWN;
Jorim Jaggi6626f542016-08-22 13:08:44 -070035import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_NONE;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070036import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070037import static com.android.server.wm.WindowManagerService.logWithStack;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080038
Jeff Brown4532e612012-04-05 14:27:12 -070039import com.android.server.input.InputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080040import com.android.server.wm.WindowManagerService.H;
41
Filip Gruszczynskia590c992015-11-25 16:45:26 -080042import android.annotation.NonNull;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080043import android.content.pm.ActivityInfo;
Jorim Jaggi26c8c422016-05-09 19:57:25 -070044import android.content.res.Configuration;
Jorim Jaggi0429f352015-12-22 16:29:16 +010045import android.graphics.Rect;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070046import android.os.Binder;
47import android.os.IBinder;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080048import android.os.Message;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -070049import android.os.SystemClock;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080050import android.util.Slog;
51import android.view.IApplicationToken;
52import android.view.View;
53import android.view.WindowManager;
Jorim Jaggi6626f542016-08-22 13:08:44 -070054import android.view.animation.Animation;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080055
56import java.io.PrintWriter;
Jorim Jaggi0429f352015-12-22 16:29:16 +010057import java.util.ArrayDeque;
Craig Mautnerb1fd65c02013-02-05 13:34:57 -080058import java.util.ArrayList;
59
60class AppTokenList extends ArrayList<AppWindowToken> {
61}
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080062
63/**
64 * Version of WindowToken that is specifically for a particular application (or
65 * really activity) that is displaying windows.
66 */
Craig Mautnere32c3072012-03-12 15:25:35 -070067class AppWindowToken extends WindowToken {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080068 private static final String TAG = TAG_WITH_CLASS_NAME ? "AppWindowToken" : TAG_WM;
69
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080070 // Non-null only for application tokens.
71 final IApplicationToken appToken;
72
Filip Gruszczynskia590c992015-11-25 16:45:26 -080073 @NonNull final AppWindowAnimator mAppAnimator;
Craig Mautnerd09cc4b2012-04-04 10:23:31 -070074
Dianne Hackborne30e02f2014-05-27 18:24:45 -070075 final boolean voiceInteraction;
76
Craig Mautner83162a92015-01-26 14:43:30 -080077 Task mTask;
Wale Ogunwale571771c2016-08-26 13:18:50 -070078 // TODO: Have a fillParent variable in WindowContainer to this?
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080079 boolean appFullscreen;
80 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Craig Mautner4c5eb222013-11-18 12:59:05 -080081 boolean layoutConfigChanges;
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -070082 boolean showForAllUsers;
Yorke Lee0e852472016-06-15 10:03:18 -070083 int targetSdk;
Craig Mautnera2c77052012-03-26 12:14:43 -070084
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080085 // The input dispatching timeout for this application token in nanoseconds.
86 long inputDispatchingTimeoutNanos;
87
88 // These are used for determining when all windows associated with
89 // an activity have been drawn, so they can be made visible together
90 // at the same time.
Craig Mautner764983d2012-03-22 11:37:36 -070091 // initialize so that it doesn't match mTransactionSequence which is an int.
92 long lastTransactionSequence = Long.MIN_VALUE;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080093 int numInterestingWindows;
94 int numDrawnWindows;
95 boolean inPendingTransaction;
96 boolean allDrawn;
Craig Mautner7636dfb2012-11-16 15:24:11 -080097 // Set to true when this app creates a surface while in the middle of an animation. In that
98 // case do not clear allDrawn until the animation completes.
99 boolean deferClearAllDrawn;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800100
Chong Zhang8e4bda92016-05-04 15:08:18 -0700101 // These are to track the app's real drawing status if there were no saved surfaces.
102 boolean allDrawnExcludingSaved;
103 int numInterestingWindowsExcludingSaved;
Wale Ogunwale571771c2016-08-26 13:18:50 -0700104 int numDrawnWindowsExcludingSaved;
Chong Zhang8e4bda92016-05-04 15:08:18 -0700105
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800106 // Is this window's surface needed? This is almost like hidden, except
107 // it will sometimes be true a little earlier: when the token has
108 // been shown, but is still waiting for its app transition to execute
109 // before making its windows shown.
110 boolean hiddenRequested;
111
112 // Have we told the window clients to hide themselves?
113 boolean clientHidden;
114
115 // Last visibility state we reported to the app token.
116 boolean reportedVisible;
117
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700118 // Last drawn state we reported to the app token.
119 boolean reportedDrawn;
120
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800121 // Set to true when the token has been removed from the window mgr.
122 boolean removed;
123
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800124 // Information about an application starting window if displayed.
125 StartingData startingData;
126 WindowState startingWindow;
127 View startingView;
128 boolean startingDisplayed;
129 boolean startingMoved;
130 boolean firstWindowDrawn;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700131 private final WindowState.UpdateReportedVisibilityResults mReportedVisibilityResults =
132 new WindowState.UpdateReportedVisibilityResults();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800133
134 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700135 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800136
Wale Ogunwale571771c2016-08-26 13:18:50 -0700137 // TODO: Have a WindowContainer state for tracking exiting/deferred removal.
Craig Mautner799bc1d2015-01-14 10:33:48 -0800138 boolean mIsExiting;
Craig Mautner9ef471f2014-02-07 13:11:47 -0800139
Craig Mautnerbb742462014-07-07 15:28:55 -0700140 boolean mLaunchTaskBehind;
Craig Mautner8746a472014-07-24 15:12:54 -0700141 boolean mEnteringAnimation;
Craig Mautnerbb742462014-07-07 15:28:55 -0700142
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800143 boolean mAlwaysFocusable;
144
Robert Carre12aece2016-02-02 22:43:27 -0800145 boolean mAppStopped;
Robert Carrfd10cd12016-06-29 16:41:50 -0700146 int mRotationAnimationHint;
Chong Zhangd78ddb42016-03-02 17:01:14 -0800147 int mPendingRelaunchCount;
Robert Carre12aece2016-02-02 22:43:27 -0800148
Robert Carr91b228092016-06-28 17:32:37 -0700149 private ArrayList<WindowSurfaceController.SurfaceControlWithBackground> mSurfaceViewBackgrounds =
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700150 new ArrayList<>();
Robert Carr91b228092016-06-28 17:32:37 -0700151
Jorim Jaggi0429f352015-12-22 16:29:16 +0100152 ArrayDeque<Rect> mFrozenBounds = new ArrayDeque<>();
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700153 ArrayDeque<Configuration> mFrozenMergedConfig = new ArrayDeque<>();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100154
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700155 AppWindowToken(WindowManagerService service, IApplicationToken token, boolean _voiceInteraction) {
156 super(service, token != null ? token.asBinder() : null, TYPE_APPLICATION, true);
157 appToken = token;
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700158 voiceInteraction = _voiceInteraction;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800159 mInputApplicationHandle = new InputApplicationHandle(this);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700160 mAppAnimator = new AppWindowAnimator(this, service);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800161 }
162
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800163 void onFirstWindowDrawn(WindowState win, WindowStateAnimator winAnimator) {
164 firstWindowDrawn = true;
165
166 // We now have a good window to show, remove dead placeholders
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700167 removeDeadWindows();
Wale Ogunwale9017ec02016-02-25 08:55:25 -0800168
169 if (startingData != null) {
170 if (DEBUG_STARTING_WINDOW || DEBUG_ANIM) Slog.v(TAG, "Finish starting "
171 + win.mToken + ": first real window is shown, no animation");
172 // If this initial window is animating, stop it -- we will do an animation to reveal
173 // it from behind the starting window, so there is no need for it to also be doing its
174 // own stuff.
175 winAnimator.clearAnimation();
176 winAnimator.mService.mFinishedStarting.add(this);
177 winAnimator.mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
178 }
179 updateReportedVisibilityLocked();
180 }
181
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800182 void updateReportedVisibilityLocked() {
183 if (appToken == null) {
184 return;
185 }
186
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700187 if (DEBUG_VISIBILITY) Slog.v(TAG, "Update reported visibility: " + this);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700188 final int count = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800189
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700190 mReportedVisibilityResults.reset();
191
192 for (int i = 0; i < count; i++) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700193 final WindowState win = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700194 win.updateReportedVisibility(mReportedVisibilityResults);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800195 }
196
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700197 int numInteresting = mReportedVisibilityResults.numInteresting;
198 int numVisible = mReportedVisibilityResults.numVisible;
199 int numDrawn = mReportedVisibilityResults.numDrawn;
200 boolean nowGone = mReportedVisibilityResults.nowGone;
201
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700202 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800203 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700204 if (!nowGone) {
205 // If the app is not yet gone, then it can only become visible/drawn.
206 if (!nowDrawn) {
207 nowDrawn = reportedDrawn;
208 }
209 if (!nowVisible) {
210 nowVisible = reportedVisible;
211 }
212 }
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800213 if (DEBUG_VISIBILITY) Slog.v(TAG, "VIS " + this + ": interesting="
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800214 + numInteresting + " visible=" + numVisible);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700215 if (nowDrawn != reportedDrawn) {
216 if (nowDrawn) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700217 mService.mH.obtainMessage(H.REPORT_APPLICATION_TOKEN_DRAWN, this).sendToTarget();
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700218 }
219 reportedDrawn = nowDrawn;
220 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800221 if (nowVisible != reportedVisible) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700222 if (DEBUG_VISIBILITY) Slog.v(TAG,
223 "Visibility changed in " + this + ": vis=" + nowVisible);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800224 reportedVisible = nowVisible;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700225 mService.mH.obtainMessage(H.REPORT_APPLICATION_TOKEN_WINDOWS,
226 nowVisible ? 1 : 0, nowGone ? 1 : 0, this).sendToTarget();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800227 }
228 }
229
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700230 boolean setVisibility(WindowManager.LayoutParams lp,
231 boolean visible, int transit, boolean performLayout, boolean isVoiceInteraction) {
232
233 boolean delayed = false;
234 inPendingTransaction = false;
235
236 if (clientHidden == visible) {
237 clientHidden = !visible;
238 sendAppVisibilityToClients();
239 }
240
241 // Allow for state changes and animation to be applied if:
242 // * token is transitioning visibility state
243 // * or the token was marked as hidden and is exiting before we had a chance to play the
244 // transition animation
245 // * or this is an opening app and windows are being replaced.
246 boolean visibilityChanged = false;
247 if (hidden == visible || (hidden && mIsExiting) || (visible && waitingForReplacement())) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700248 final AccessibilityController accessibilityController = mService.mAccessibilityController;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700249 boolean changed = false;
250 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
251 "Changing app " + this + " hidden=" + hidden + " performLayout=" + performLayout);
252
253 boolean runningAppAnimation = false;
254
255 if (transit != AppTransition.TRANSIT_UNSET) {
256 if (mAppAnimator.animation == AppWindowAnimator.sDummyAnimation) {
257 mAppAnimator.setNullAnimation();
258 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700259 if (mService.applyAnimationLocked(this, lp, transit, visible, isVoiceInteraction)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700260 delayed = runningAppAnimation = true;
261 }
262 final WindowState window = findMainWindow();
263 //TODO (multidisplay): Magnification is supported only for the default display.
264 if (window != null && accessibilityController != null
265 && window.getDisplayId() == DEFAULT_DISPLAY) {
266 accessibilityController.onAppWindowTransitionLocked(window, transit);
267 }
268 changed = true;
269 }
270
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700271 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700272 for (int i = 0; i < windowsCount; i++) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700273 final WindowState win = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700274 changed |= win.onAppVisibilityChanged(visible, runningAppAnimation);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700275 }
276
277 hidden = hiddenRequested = !visible;
278 visibilityChanged = true;
279 if (!visible) {
280 stopFreezingScreen(true, true);
281 } else {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700282 // If we are being set visible, and the starting window is not yet displayed,
283 // then make sure it doesn't get displayed.
284 if (startingWindow != null && !startingWindow.isDrawnLw()) {
285 startingWindow.mPolicyVisibility = false;
286 startingWindow.mPolicyVisibilityAfterAnim = false;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700287 }
288 }
289
290 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "setVisibility: " + this
291 + ": hidden=" + hidden + " hiddenRequested=" + hiddenRequested);
292
293 if (changed) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700294 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700295 if (performLayout) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700296 mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700297 false /*updateInputWindows*/);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700298 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700299 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700300 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700301 }
302 }
303
304 if (mAppAnimator.animation != null) {
305 delayed = true;
306 }
307
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700308 for (int i = mChildren.size() - 1; i >= 0 && !delayed; i--) {
309 if (((WindowState) mChildren.get(i)).isWindowAnimationSet()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700310 delayed = true;
311 }
312 }
313
314 if (visibilityChanged) {
315 if (visible && !delayed) {
316 // The token was made immediately visible, there will be no entrance animation.
317 // We need to inform the client the enter animation was finished.
318 mEnteringAnimation = true;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700319 mService.mActivityManagerAppTransitionNotifier.onAppTransitionFinishedLocked(token);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700320 }
321
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700322 if (!mService.mClosingApps.contains(this) && !mService.mOpeningApps.contains(this)) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700323 // The token is not closing nor opening, so even if there is an animation set, that
324 // doesn't mean that it goes through the normal app transition cycle so we have
325 // to inform the docked controller about visibility change.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700326 mService.getDefaultDisplayContentLocked().getDockedDividerController()
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700327 .notifyAppVisibilityChanged();
328 }
329 }
330
331 return delayed;
332 }
333
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800334 WindowState findMainWindow() {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700335 WindowState candidate = null;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700336 int j = mChildren.size();
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800337 while (j > 0) {
338 j--;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700339 final WindowState win = (WindowState) mChildren.get(j);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700340 final int type = win.mAttrs.type;
341 // No need to loop through child window as base application and starting types can't be
342 // child windows.
343 if (type == TYPE_BASE_APPLICATION || type == TYPE_APPLICATION_STARTING) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700344 // In cases where there are multiple windows, we prefer the non-exiting window. This
Sungsoo Lim0d3d1f82015-12-02 14:47:59 +0900345 // happens for example when replacing windows during an activity relaunch. When
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700346 // constructing the animation, we want the new window, not the exiting one.
Wale Ogunwalec48a3542016-02-19 15:18:45 -0800347 if (win.mAnimatingExit) {
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700348 candidate = win;
349 } else {
350 return win;
351 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800352 }
353 }
Filip Gruszczynski55a309f2015-09-04 17:15:01 -0700354 return candidate;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800355 }
356
Wale Ogunwale6cae7652015-12-26 07:36:26 -0800357 boolean windowsAreFocusable() {
358 return StackId.canReceiveKeys(mTask.mStack.mStackId) || mAlwaysFocusable;
Wale Ogunwaled045c822015-12-02 09:14:28 -0800359 }
360
Wale Ogunwale571771c2016-08-26 13:18:50 -0700361 @Override
362 void removeIfPossible() {
Craig Mautnere3119b72015-01-20 15:02:36 -0800363 mIsExiting = false;
364 removeAllWindows();
Wale Ogunwale571771c2016-08-26 13:18:50 -0700365 if (mTask != null) {
366 mTask.detachChild(this);
Craig Mautnere3119b72015-01-20 15:02:36 -0800367 }
368 }
369
Chong Zhange05bcb12016-07-26 17:47:29 -0700370 void clearAnimatingFlags() {
Chong Zhangb0d26702016-08-12 16:03:29 -0700371 boolean wallpaperMightChange = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700372 for (int i = mChildren.size() - 1; i >= 0; i--) {
373 final WindowState win = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700374 wallpaperMightChange |= win.clearAnimatingFlags();
Chong Zhange05bcb12016-07-26 17:47:29 -0700375 }
Chong Zhangb0d26702016-08-12 16:03:29 -0700376 if (wallpaperMightChange) {
377 requestUpdateWallpaperIfNeeded();
378 }
Chong Zhange05bcb12016-07-26 17:47:29 -0700379 }
380
Robert Carre12aece2016-02-02 22:43:27 -0800381 void destroySurfaces() {
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700382 destroySurfaces(false /*cleanupOnResume*/);
383 }
384
385 /**
386 * Destroy surfaces which have been marked as eligible by the animator, taking care to ensure
387 * the client has finished with them.
388 *
389 * @param cleanupOnResume whether this is done when app is resumed without fully stopped. If
390 * set to true, destroy only surfaces of removed windows, and clear relevant flags of the
391 * others so that they are ready to be reused. If set to false (common case), destroy all
392 * surfaces that's eligible, if the app is already stopped.
393 */
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700394 private void destroySurfaces(boolean cleanupOnResume) {
Robert Carre12aece2016-02-02 22:43:27 -0800395 final DisplayContentList displayList = new DisplayContentList();
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700396 for (int i = mChildren.size() - 1; i >= 0; i--) {
397 final WindowState win = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700398 final boolean destroyed = win.destroySurface(cleanupOnResume, mAppStopped);
Chong Zhangeb665572016-05-09 18:28:27 -0700399
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700400 if (destroyed) {
401 final DisplayContent displayContent = win.getDisplayContent();
402 if (displayContent != null && !displayList.contains(displayContent)) {
403 displayList.add(displayContent);
404 }
Robert Carre12aece2016-02-02 22:43:27 -0800405 }
Robert Carre12aece2016-02-02 22:43:27 -0800406 }
407 for (int i = 0; i < displayList.size(); i++) {
408 final DisplayContent displayContent = displayList.get(i);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700409 mService.mLayersController.assignLayersLocked(displayContent.getWindowList());
Robert Carre12aece2016-02-02 22:43:27 -0800410 displayContent.layoutNeeded = true;
411 }
412 }
413
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800414 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700415 * Notify that the app is now resumed, and it was not stopped before, perform a clean
416 * up of the surfaces
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800417 */
Chong Zhangad24f962016-08-25 12:12:33 -0700418 void notifyAppResumed(boolean wasStopped, boolean allowSavedSurface) {
419 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppResumed: wasStopped=" + wasStopped
420 + " allowSavedSurface=" + allowSavedSurface + " " + this);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700421 mAppStopped = false;
422 if (!wasStopped) {
423 destroySurfaces(true /*cleanupOnResume*/);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800424 }
Chong Zhangad24f962016-08-25 12:12:33 -0700425 if (!allowSavedSurface) {
426 destroySavedSurfaces();
427 }
Robert Carre12aece2016-02-02 22:43:27 -0800428 }
429
Chong Zhangbef461f2015-10-27 11:38:24 -0700430 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700431 * Notify that the app has stopped, and it is okay to destroy any surfaces which were
432 * keeping alive in case they were still being used.
433 */
434 void notifyAppStopped() {
435 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: " + this);
436 mAppStopped = true;
437 destroySurfaces();
438 // Remove any starting window that was added for this app if they are still around.
439 mTask.mService.scheduleRemoveStartingWindowLocked(this);
440 }
441
442 /**
Chong Zhangbef461f2015-10-27 11:38:24 -0700443 * Checks whether we should save surfaces for this app.
444 *
445 * @return true if the surfaces should be saved, false otherwise.
446 */
447 boolean shouldSaveSurface() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800448 // We want to save surface if the app's windows are "allDrawn".
449 // (If we started entering animation early with saved surfaces, allDrawn
450 // should have been restored to true. So we'll save again in that case
451 // even if app didn't actually finish drawing.)
452 return allDrawn;
Robert Carr13f7be9e2015-12-02 18:39:45 -0800453 }
454
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700455 private boolean canRestoreSurfaces() {
456 for (int i = mChildren.size() -1; i >= 0; i--) {
457 final WindowState w = (WindowState) mChildren.get(i);
Chong Zhang92147042016-05-09 12:47:11 -0700458 if (w.canRestoreSurface()) {
Robert Carr13f7be9e2015-12-02 18:39:45 -0800459 return true;
460 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700461 }
462 return false;
463 }
464
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700465 private void clearWasVisibleBeforeClientHidden() {
466 for (int i = mChildren.size() - 1; i >= 0; i--) {
467 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700468 w.clearWasVisibleBeforeClientHidden();
Chong Zhang92147042016-05-09 12:47:11 -0700469 }
470 }
471
Chong Zhang8e4bda92016-05-04 15:08:18 -0700472 /**
473 * Whether the app has some window that is invisible in layout, but
474 * animating with saved surface.
475 */
476 boolean isAnimatingInvisibleWithSavedSurface() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700477 for (int i = mChildren.size() - 1; i >= 0; i--) {
478 final WindowState w = (WindowState) mChildren.get(i);
Chong Zhang8e4bda92016-05-04 15:08:18 -0700479 if (w.isAnimatingInvisibleWithSavedSurface()) {
480 return true;
481 }
482 }
483 return false;
484 }
485
486 /**
487 * Hide all window surfaces that's still invisible in layout but animating
488 * with a saved surface, and mark them destroying.
489 */
490 void stopUsingSavedSurfaceLocked() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700491 for (int i = mChildren.size() - 1; i >= 0; i--) {
492 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700493 w.stopUsingSavedSurface();
Chong Zhang8e4bda92016-05-04 15:08:18 -0700494 }
495 destroySurfaces();
496 }
497
Chong Zhangf58631a2016-05-24 16:02:10 -0700498 void markSavedSurfaceExiting() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700499 for (int i = mChildren.size() - 1; i >= 0; i--) {
500 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700501 w.markSavedSurfaceExiting();
Chong Zhangf58631a2016-05-24 16:02:10 -0700502 }
503 }
504
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700505 void restoreSavedSurfaceForInterestingWindows() {
Chong Zhang92147042016-05-09 12:47:11 -0700506 if (!canRestoreSurfaces()) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700507 clearWasVisibleBeforeClientHidden();
Chong Zhangbef461f2015-10-27 11:38:24 -0700508 return;
509 }
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700510
511 // Check if all interesting windows are drawn and we can mark allDrawn=true.
512 int interestingNotDrawn = -1;
513
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700514 for (int i = mChildren.size() - 1; i >= 0; i--) {
515 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700516 interestingNotDrawn = w.restoreSavedSurfaceForInterestingWindow();
Chong Zhangbef461f2015-10-27 11:38:24 -0700517 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800518
Chong Zhang92147042016-05-09 12:47:11 -0700519 if (!allDrawn) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700520 allDrawn = (interestingNotDrawn == 0);
Chong Zhang92147042016-05-09 12:47:11 -0700521 if (allDrawn) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700522 mService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
Chong Zhang92147042016-05-09 12:47:11 -0700523 }
524 }
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700525 clearWasVisibleBeforeClientHidden();
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800526
527 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700528 "restoreSavedSurfaceForInterestingWindows: " + this + " allDrawn=" + allDrawn
529 + " interestingNotDrawn=" + interestingNotDrawn);
Chong Zhangbef461f2015-10-27 11:38:24 -0700530 }
531
532 void destroySavedSurfaces() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700533 for (int i = mChildren.size() - 1; i >= 0; i--) {
534 final WindowState win = (WindowState) mChildren.get(i);
Robert Carr13f7be9e2015-12-02 18:39:45 -0800535 win.destroySavedSurface();
Chong Zhangbef461f2015-10-27 11:38:24 -0700536 }
Chong Zhang92147042016-05-09 12:47:11 -0700537 }
538
539 void clearAllDrawn() {
540 allDrawn = false;
541 deferClearAllDrawn = false;
Chong Zhang8e4bda92016-05-04 15:08:18 -0700542 allDrawnExcludingSaved = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700543 }
544
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800545 @Override
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700546 void removeWindow(WindowState win) {
547 super.removeWindow(win);
548
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700549 // TODO: Something smells about the code below...Is there a better way?
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700550 if (startingWindow == win) {
551 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Notify removed startingWindow " + win);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700552 mService.scheduleRemoveStartingWindowLocked(this);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700553 } else if (mChildren.size() == 0 && startingData != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700554 // If this is the last window and we had requested a starting transition window,
555 // well there is no point now.
556 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Nulling last startingWindow");
557 startingData = null;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700558 } else if (mChildren.size() == 1 && startingView != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700559 // If this is the last window except for a starting transition window,
560 // we need to get rid of the starting transition.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700561 mService.scheduleRemoveStartingWindowLocked(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700562 }
563 }
564
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700565 void removeDeadWindows() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700566 for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
567 WindowState win = (WindowState) mChildren.get(winNdx);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800568 if (win.mAppDied) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700569 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.w(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700570 "removeDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800571 // Set mDestroying, we don't want any animation or delayed removal here.
572 win.mDestroying = true;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700573 // Also removes child windows.
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700574 win.removeIfPossible();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800575 }
576 }
577 }
578
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700579 boolean hasWindowsAlive() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700580 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700581 // No need to loop through child windows as the answer should be the same as that of the
582 // parent window.
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700583 if (!((WindowState) mChildren.get(i)).mAppDied) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700584 return true;
585 }
586 }
587 return false;
588 }
589
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700590 void setWillReplaceWindows(boolean animate) {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700591 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
592 "Marking app token " + this + " with replacing windows.");
Robert Carra1eb4392015-12-10 12:43:51 -0800593
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700594 for (int i = mChildren.size() - 1; i >= 0; i--) {
595 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700596 w.setWillReplaceWindow(animate);
Robert Carra1eb4392015-12-10 12:43:51 -0800597 }
598 if (animate) {
599 // Set-up dummy animation so we can start treating windows associated with this
600 // token like they are in transition before the new app window is ready for us to
601 // run the real transition animation.
602 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700603 "setWillReplaceWindow() Setting dummy animation on: " + this);
Robert Carra1eb4392015-12-10 12:43:51 -0800604 mAppAnimator.setDummyAnimation();
605 }
606 }
607
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700608 void setWillReplaceChildWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700609 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + this
Robert Carr23fa16b2016-01-13 13:19:58 -0800610 + " with replacing child windows.");
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700611 for (int i = mChildren.size() - 1; i >= 0; i--) {
612 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700613 w.setWillReplaceChildWindows();
Robert Carr23fa16b2016-01-13 13:19:58 -0800614 }
615 }
616
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700617 void clearWillReplaceWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700618 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
619 "Resetting app token " + this + " of replacing window marks.");
Chong Zhangf596cd52016-01-05 13:42:44 -0800620
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700621 for (int i = mChildren.size() - 1; i >= 0; i--) {
622 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700623 w.clearWillReplaceWindow();
Chong Zhangf596cd52016-01-05 13:42:44 -0800624 }
625 }
626
Chong Zhang4d7369a2016-04-25 16:09:14 -0700627 void requestUpdateWallpaperIfNeeded() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700628 for (int i = mChildren.size() - 1; i >= 0; i--) {
629 final WindowState w = (WindowState) mChildren.get(i);
Chong Zhang4d7369a2016-04-25 16:09:14 -0700630 w.requestUpdateWallpaperIfNeeded();
631 }
632 }
633
Chong Zhangd78ddb42016-03-02 17:01:14 -0800634 boolean isRelaunching() {
635 return mPendingRelaunchCount > 0;
636 }
637
638 void startRelaunching() {
639 if (canFreezeBounds()) {
640 freezeBounds();
641 }
642 mPendingRelaunchCount++;
643 }
644
645 void finishRelaunching() {
646 if (canFreezeBounds()) {
647 unfreezeBounds();
648 }
649 if (mPendingRelaunchCount > 0) {
650 mPendingRelaunchCount--;
651 }
652 }
653
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700654 void clearRelaunching() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700655 if (mPendingRelaunchCount == 0) {
656 return;
657 }
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700658 if (canFreezeBounds()) {
659 unfreezeBounds();
660 }
661 mPendingRelaunchCount = 0;
662 }
663
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700664 @Override
Robert Carra1eb4392015-12-10 12:43:51 -0800665 void addWindow(WindowState w) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700666 super.addWindow(w);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700667
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700668 boolean gotReplacementWindow = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700669 for (int i = mChildren.size() - 1; i >= 0; i--) {
670 final WindowState candidate = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700671 gotReplacementWindow |= candidate.setReplacementWindowIfNeeded(w);
672 }
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700673
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700674 // if we got a replacement window, reset the timeout to give drawing more time
675 if (gotReplacementWindow) {
676 mService.scheduleWindowReplacementTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -0800677 }
Robert Carra1eb4392015-12-10 12:43:51 -0800678 }
679
680 boolean waitingForReplacement() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700681 for (int i = mChildren.size() - 1; i >= 0; i--) {
682 final WindowState candidate = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700683 if (candidate.waitingForReplacement()) {
Robert Carra1eb4392015-12-10 12:43:51 -0800684 return true;
685 }
686 }
687 return false;
688 }
689
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700690 void onWindowReplacementTimeout() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700691 for (int i = mChildren.size() - 1; i >= 0; --i) {
692 ((WindowState) mChildren.get(i)).onWindowReplacementTimeout();
Robert Carra1eb4392015-12-10 12:43:51 -0800693 }
694 }
695
Chong Zhangd78ddb42016-03-02 17:01:14 -0800696 private boolean canFreezeBounds() {
697 // For freeform windows, we can't freeze the bounds at the moment because this would make
698 // the resizing unresponsive.
699 return mTask != null && !mTask.inFreeformWorkspace();
700 }
701
Jorim Jaggi0429f352015-12-22 16:29:16 +0100702 /**
703 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
704 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
705 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
706 * with a queue.
707 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800708 private void freezeBounds() {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100709 mFrozenBounds.offer(new Rect(mTask.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700710
711 if (mTask.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
712 // We didn't call prepareFreezingBounds on the task, so use the current value.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700713 final Configuration config = new Configuration(mService.mCurConfiguration);
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700714 config.updateFrom(mTask.mOverrideConfig);
715 mFrozenMergedConfig.offer(config);
716 } else {
717 mFrozenMergedConfig.offer(new Configuration(mTask.mPreparedFrozenMergedConfig));
718 }
719 mTask.mPreparedFrozenMergedConfig.setToDefaults();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100720 }
721
722 /**
723 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
724 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800725 private void unfreezeBounds() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700726 if (!mFrozenBounds.isEmpty()) {
727 mFrozenBounds.remove();
728 }
729 if (!mFrozenMergedConfig.isEmpty()) {
730 mFrozenMergedConfig.remove();
731 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700732 for (int i = mChildren.size() - 1; i >= 0; i--) {
733 final WindowState win = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700734 win.onUnfreezeBounds();
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100735 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700736 mService.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100737 }
738
Robert Carr91b228092016-06-28 17:32:37 -0700739 void addSurfaceViewBackground(WindowSurfaceController.SurfaceControlWithBackground background) {
740 mSurfaceViewBackgrounds.add(background);
741 }
742
743 void removeSurfaceViewBackground(WindowSurfaceController.SurfaceControlWithBackground background) {
744 mSurfaceViewBackgrounds.remove(background);
745 updateSurfaceViewBackgroundVisibilities();
746 }
747
748 // We use DimLayers behind SurfaceViews to prevent holes while resizing and creating.
749 // However, we need to ensure one SurfaceView doesn't cover another when they are both placed
750 // below the main app window (as traditionally a SurfaceView which is never drawn
751 // to is totally translucent). So we look at all our SurfaceView backgrounds and only enable
752 // the background for the SurfaceView with lowest Z order
753 void updateSurfaceViewBackgroundVisibilities() {
754 WindowSurfaceController.SurfaceControlWithBackground bottom = null;
755 int bottomLayer = Integer.MAX_VALUE;
756 for (int i = 0; i < mSurfaceViewBackgrounds.size(); i++) {
757 WindowSurfaceController.SurfaceControlWithBackground sc = mSurfaceViewBackgrounds.get(i);
758 if (sc.mVisible && sc.mLayer < bottomLayer) {
759 bottomLayer = sc.mLayer;
760 bottom = sc;
761 }
762 }
763 for (int i = 0; i < mSurfaceViewBackgrounds.size(); i++) {
764 WindowSurfaceController.SurfaceControlWithBackground sc = mSurfaceViewBackgrounds.get(i);
765 sc.updateBackgroundVisibility(sc != bottom);
766 }
767 }
768
Jorim Jaggi6626f542016-08-22 13:08:44 -0700769 /**
770 * See {@link WindowManagerService#overridePlayingAppAnimationsLw}
771 */
772 void overridePlayingAppAnimations(Animation a) {
773 if (mAppAnimator.isAnimating()) {
774 final WindowState win = findMainWindow();
775 final int width = win.mContainingFrame.width();
776 final int height = win.mContainingFrame.height();
777 mAppAnimator.setAnimation(a, width, height, false, STACK_CLIP_NONE);
778 }
779 }
780
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700781 void resetJustMovedInStack() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700782 for (int i = mChildren.size() - 1; i >= 0; i--) {
783 ((WindowState) mChildren.get(i)).resetJustMovedInStack();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700784 }
785 }
786
787 void notifyMovedInStack() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700788 for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
789 final WindowState win = (WindowState) mChildren.get(winNdx);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700790 win.notifyMovedInStack();
791 }
792 }
793
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700794 void setAppLayoutChanges(int changes, String reason, int displayId) {
795 final WindowAnimator windowAnimator = mAppAnimator.mAnimator;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700796 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700797 // Child windows will be on the same display as their parents.
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700798 if (displayId == ((WindowState) mChildren.get(i)).getDisplayId()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700799 windowAnimator.setPendingLayoutChanges(displayId, changes);
800 if (DEBUG_LAYOUT_REPEATS) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700801 mService.mWindowPlacerLocked.debugLayoutRepeats(
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700802 reason, windowAnimator.getPendingLayoutChanges(displayId));
803 }
804 break;
805 }
806 }
807 }
808
809 void removeReplacedWindowIfNeeded(WindowState replacement) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700810 for (int i = mChildren.size() - 1; i >= 0; i--) {
811 final WindowState win = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700812 if (win.removeReplacedWindowIfNeeded(replacement)) {
813 return;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700814 }
815 }
816 }
817
818 void startFreezingScreen() {
819 if (DEBUG_ORIENTATION) logWithStack(TAG, "Set freezing of " + appToken + ": hidden="
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700820 + hidden + " freezing=" + mAppAnimator.freezingScreen + " hiddenRequested="
821 + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700822 if (!hiddenRequested) {
823 if (!mAppAnimator.freezingScreen) {
824 mAppAnimator.freezingScreen = true;
825 mAppAnimator.lastFreezeDuration = 0;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700826 mService.mAppsFreezingScreen++;
827 if (mService.mAppsFreezingScreen == 1) {
828 mService.startFreezingDisplayLocked(false, 0, 0);
829 mService.mH.removeMessages(H.APP_FREEZE_TIMEOUT);
830 mService.mH.sendEmptyMessageDelayed(H.APP_FREEZE_TIMEOUT, 2000);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700831 }
832 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700833 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700834 for (int i = 0; i < count; i++) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700835 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700836 w.onStartFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700837 }
838 }
839 }
840
841 void stopFreezingScreen(boolean unfreezeSurfaceNow, boolean force) {
842 if (!mAppAnimator.freezingScreen) {
843 return;
844 }
845 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Clear freezing of " + this + " force=" + force);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700846 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700847 boolean unfrozeWindows = false;
848 for (int i = 0; i < count; i++) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700849 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700850 unfrozeWindows |= w.onStopFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700851 }
852 if (force || unfrozeWindows) {
853 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "No longer freezing: " + this);
854 mAppAnimator.freezingScreen = false;
855 mAppAnimator.lastFreezeDuration =
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700856 (int)(SystemClock.elapsedRealtime() - mService.mDisplayFreezeTime);
857 mService.mAppsFreezingScreen--;
858 mService.mLastFinishedFreezeSource = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700859 }
860 if (unfreezeSurfaceNow) {
861 if (unfrozeWindows) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700862 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700863 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700864 mService.stopFreezingDisplayLocked();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700865 }
866 }
867
868 boolean transferStartingWindow(IBinder transferFrom) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700869 final AppWindowToken fromToken = mService.findAppWindowToken(transferFrom);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700870 if (fromToken == null) {
871 return false;
872 }
873
874 final WindowState tStartingWindow = fromToken.startingWindow;
875 if (tStartingWindow != null && fromToken.startingView != null) {
876 // In this case, the starting icon has already been displayed, so start
877 // letting windows get shown immediately without any more transitions.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700878 mService.mSkipAppTransitionAnimation = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700879
880 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Moving existing starting " + tStartingWindow
881 + " from " + fromToken + " to " + this);
882
883 final long origId = Binder.clearCallingIdentity();
884
885 // Transfer the starting window over to the new token.
886 startingData = fromToken.startingData;
887 startingView = fromToken.startingView;
888 startingDisplayed = fromToken.startingDisplayed;
889 fromToken.startingDisplayed = false;
890 startingWindow = tStartingWindow;
891 reportedVisible = fromToken.reportedVisible;
892 fromToken.startingData = null;
893 fromToken.startingView = null;
894 fromToken.startingWindow = null;
895 fromToken.startingMoved = true;
896 tStartingWindow.mToken = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700897 tStartingWindow.mAppToken = this;
898
899 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE || DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
900 "Removing starting window: " + tStartingWindow);
901 tStartingWindow.getWindowList().remove(tStartingWindow);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700902 mService.mWindowsChanged = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700903 if (DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
904 "Removing starting " + tStartingWindow + " from " + fromToken);
905 fromToken.removeWindow(tStartingWindow);
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700906 addWindow(tStartingWindow);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700907
908 // Propagate other interesting state between the tokens. If the old token is displayed,
909 // we should immediately force the new one to be displayed. If it is animating, we need
910 // to move that animation to the new one.
911 if (fromToken.allDrawn) {
912 allDrawn = true;
913 deferClearAllDrawn = fromToken.deferClearAllDrawn;
914 }
915 if (fromToken.firstWindowDrawn) {
916 firstWindowDrawn = true;
917 }
918 if (!fromToken.hidden) {
919 hidden = false;
920 hiddenRequested = false;
921 }
922 if (clientHidden != fromToken.clientHidden) {
923 clientHidden = fromToken.clientHidden;
924 sendAppVisibilityToClients();
925 }
926 fromToken.mAppAnimator.transferCurrentAnimation(
927 mAppAnimator, tStartingWindow.mWinAnimator);
928
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700929 mService.updateFocusedWindowLocked(
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700930 UPDATE_FOCUS_WILL_PLACE_SURFACES, true /*updateInputWindows*/);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700931 mService.getDefaultDisplayContentLocked().layoutNeeded = true;
932 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700933 Binder.restoreCallingIdentity(origId);
934 return true;
935 } else if (fromToken.startingData != null) {
936 // The previous app was getting ready to show a
937 // starting window, but hasn't yet done so. Steal it!
938 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
939 "Moving pending starting from " + fromToken + " to " + this);
940 startingData = fromToken.startingData;
941 fromToken.startingData = null;
942 fromToken.startingMoved = true;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700943 final Message m = mService.mH.obtainMessage(H.ADD_STARTING, this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700944 // Note: we really want to do sendMessageAtFrontOfQueue() because we want to process the
945 // message ASAP, before any other queued messages.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700946 mService.mH.sendMessageAtFrontOfQueue(m);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700947 return true;
948 }
949
950 final AppWindowAnimator tAppAnimator = fromToken.mAppAnimator;
951 final AppWindowAnimator wAppAnimator = mAppAnimator;
952 if (tAppAnimator.thumbnail != null) {
953 // The old token is animating with a thumbnail, transfer that to the new token.
954 if (wAppAnimator.thumbnail != null) {
955 wAppAnimator.thumbnail.destroy();
956 }
957 wAppAnimator.thumbnail = tAppAnimator.thumbnail;
958 wAppAnimator.thumbnailLayer = tAppAnimator.thumbnailLayer;
959 wAppAnimator.thumbnailAnimation = tAppAnimator.thumbnailAnimation;
960 tAppAnimator.thumbnail = null;
961 }
962 return false;
963 }
964
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700965 boolean isLastWindow(WindowState win) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700966 return mChildren.size() == 1 && mChildren.get(0) == win;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700967 }
968
969 void setAllAppWinAnimators() {
970 final ArrayList<WindowStateAnimator> allAppWinAnimators = mAppAnimator.mAllAppWinAnimators;
971 allAppWinAnimators.clear();
972
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700973 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700974 for (int j = 0; j < windowsCount; j++) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700975 ((WindowState) mChildren.get(j)).addWinAnimatorToList(allAppWinAnimators);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700976 }
977 }
978
Craig Mautnerdbb79912012-03-01 18:59:14 -0800979 @Override
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700980 AppWindowToken asAppWindowToken() {
981 // I am an app window token!
982 return this;
983 }
984
985 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800986 void dump(PrintWriter pw, String prefix) {
987 super.dump(pw, prefix);
988 if (appToken != null) {
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700989 pw.print(prefix); pw.print("app=true voiceInteraction="); pw.println(voiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800990 }
Craig Mautner83162a92015-01-26 14:43:30 -0800991 pw.print(prefix); pw.print("task="); pw.println(mTask);
992 pw.print(prefix); pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800993 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
994 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
995 pw.print(" clientHidden="); pw.print(clientHidden);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700996 pw.print(" reportedDrawn="); pw.print(reportedDrawn);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800997 pw.print(" reportedVisible="); pw.println(reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -0700998 if (paused) {
999 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001000 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -08001001 if (mAppStopped) {
1002 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
1003 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001004 if (numInterestingWindows != 0 || numDrawnWindows != 0
Craig Mautner6fbda632012-07-03 09:26:39 -07001005 || allDrawn || mAppAnimator.allDrawn) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001006 pw.print(prefix); pw.print("numInterestingWindows=");
1007 pw.print(numInterestingWindows);
1008 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
1009 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -07001010 pw.print(" allDrawn="); pw.print(allDrawn);
1011 pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
1012 pw.println(")");
1013 }
1014 if (inPendingTransaction) {
1015 pw.print(prefix); pw.print("inPendingTransaction=");
1016 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001017 }
Craig Mautner799bc1d2015-01-14 10:33:48 -08001018 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001019 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
1020 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -08001021 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -08001022 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001023 }
1024 if (startingWindow != null || startingView != null
1025 || startingDisplayed || startingMoved) {
1026 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
1027 pw.print(" startingView="); pw.print(startingView);
1028 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale9017ec02016-02-25 08:55:25 -08001029 pw.print(" startingMoved="); pw.println(startingMoved);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001030 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001031 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -08001032 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001033 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -08001034 }
1035 if (mPendingRelaunchCount != 0) {
1036 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +01001037 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001038 }
1039
1040 @Override
1041 public String toString() {
1042 if (stringName == null) {
1043 StringBuilder sb = new StringBuilder();
1044 sb.append("AppWindowToken{");
1045 sb.append(Integer.toHexString(System.identityHashCode(this)));
1046 sb.append(" token="); sb.append(token); sb.append('}');
1047 stringName = sb.toString();
1048 }
1049 return stringName;
1050 }
Jeff Browne9bdb312012-04-05 15:30:10 -07001051}