blob: d7e7f8118e3e42b90a94e0ff3154530c764ae816 [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
Wale Ogunwale44f21802016-09-02 12:49:48 -0700362 boolean isVisible() {
363 if (hidden) {
364 // TODO: Should this be checking hiddenRequested instead of hidden?
365 return false;
366 }
367 return super.isVisible();
368 }
369
370 @Override
Wale Ogunwale571771c2016-08-26 13:18:50 -0700371 void removeIfPossible() {
Craig Mautnere3119b72015-01-20 15:02:36 -0800372 mIsExiting = false;
373 removeAllWindows();
Wale Ogunwale571771c2016-08-26 13:18:50 -0700374 if (mTask != null) {
375 mTask.detachChild(this);
Craig Mautnere3119b72015-01-20 15:02:36 -0800376 }
377 }
378
Chong Zhange05bcb12016-07-26 17:47:29 -0700379 void clearAnimatingFlags() {
Chong Zhangb0d26702016-08-12 16:03:29 -0700380 boolean wallpaperMightChange = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700381 for (int i = mChildren.size() - 1; i >= 0; i--) {
382 final WindowState win = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700383 wallpaperMightChange |= win.clearAnimatingFlags();
Chong Zhange05bcb12016-07-26 17:47:29 -0700384 }
Chong Zhangb0d26702016-08-12 16:03:29 -0700385 if (wallpaperMightChange) {
386 requestUpdateWallpaperIfNeeded();
387 }
Chong Zhange05bcb12016-07-26 17:47:29 -0700388 }
389
Robert Carre12aece2016-02-02 22:43:27 -0800390 void destroySurfaces() {
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700391 destroySurfaces(false /*cleanupOnResume*/);
392 }
393
394 /**
395 * Destroy surfaces which have been marked as eligible by the animator, taking care to ensure
396 * the client has finished with them.
397 *
398 * @param cleanupOnResume whether this is done when app is resumed without fully stopped. If
399 * set to true, destroy only surfaces of removed windows, and clear relevant flags of the
400 * others so that they are ready to be reused. If set to false (common case), destroy all
401 * surfaces that's eligible, if the app is already stopped.
402 */
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700403 private void destroySurfaces(boolean cleanupOnResume) {
Robert Carre12aece2016-02-02 22:43:27 -0800404 final DisplayContentList displayList = new DisplayContentList();
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700405 for (int i = mChildren.size() - 1; i >= 0; i--) {
406 final WindowState win = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700407 final boolean destroyed = win.destroySurface(cleanupOnResume, mAppStopped);
Chong Zhangeb665572016-05-09 18:28:27 -0700408
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700409 if (destroyed) {
410 final DisplayContent displayContent = win.getDisplayContent();
411 if (displayContent != null && !displayList.contains(displayContent)) {
412 displayList.add(displayContent);
413 }
Robert Carre12aece2016-02-02 22:43:27 -0800414 }
Robert Carre12aece2016-02-02 22:43:27 -0800415 }
416 for (int i = 0; i < displayList.size(); i++) {
417 final DisplayContent displayContent = displayList.get(i);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700418 mService.mLayersController.assignLayersLocked(displayContent.getWindowList());
Robert Carre12aece2016-02-02 22:43:27 -0800419 displayContent.layoutNeeded = true;
420 }
421 }
422
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800423 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700424 * Notify that the app is now resumed, and it was not stopped before, perform a clean
425 * up of the surfaces
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800426 */
Chong Zhangad24f962016-08-25 12:12:33 -0700427 void notifyAppResumed(boolean wasStopped, boolean allowSavedSurface) {
428 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppResumed: wasStopped=" + wasStopped
429 + " allowSavedSurface=" + allowSavedSurface + " " + this);
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700430 mAppStopped = false;
431 if (!wasStopped) {
432 destroySurfaces(true /*cleanupOnResume*/);
Wale Ogunwale8d5a5422016-03-03 18:28:21 -0800433 }
Chong Zhangad24f962016-08-25 12:12:33 -0700434 if (!allowSavedSurface) {
435 destroySavedSurfaces();
436 }
Robert Carre12aece2016-02-02 22:43:27 -0800437 }
438
Chong Zhangbef461f2015-10-27 11:38:24 -0700439 /**
Chong Zhang45e6d2d2016-07-20 18:33:56 -0700440 * Notify that the app has stopped, and it is okay to destroy any surfaces which were
441 * keeping alive in case they were still being used.
442 */
443 void notifyAppStopped() {
444 if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: " + this);
445 mAppStopped = true;
446 destroySurfaces();
447 // Remove any starting window that was added for this app if they are still around.
448 mTask.mService.scheduleRemoveStartingWindowLocked(this);
449 }
450
451 /**
Chong Zhangbef461f2015-10-27 11:38:24 -0700452 * Checks whether we should save surfaces for this app.
453 *
454 * @return true if the surfaces should be saved, false otherwise.
455 */
456 boolean shouldSaveSurface() {
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800457 // We want to save surface if the app's windows are "allDrawn".
458 // (If we started entering animation early with saved surfaces, allDrawn
459 // should have been restored to true. So we'll save again in that case
460 // even if app didn't actually finish drawing.)
461 return allDrawn;
Robert Carr13f7be9e2015-12-02 18:39:45 -0800462 }
463
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700464 private boolean canRestoreSurfaces() {
465 for (int i = mChildren.size() -1; i >= 0; i--) {
466 final WindowState w = (WindowState) mChildren.get(i);
Chong Zhang92147042016-05-09 12:47:11 -0700467 if (w.canRestoreSurface()) {
Robert Carr13f7be9e2015-12-02 18:39:45 -0800468 return true;
469 }
Chong Zhangbef461f2015-10-27 11:38:24 -0700470 }
471 return false;
472 }
473
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700474 private void clearWasVisibleBeforeClientHidden() {
475 for (int i = mChildren.size() - 1; i >= 0; i--) {
476 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700477 w.clearWasVisibleBeforeClientHidden();
Chong Zhang92147042016-05-09 12:47:11 -0700478 }
479 }
480
Chong Zhang8e4bda92016-05-04 15:08:18 -0700481 /**
482 * Whether the app has some window that is invisible in layout, but
483 * animating with saved surface.
484 */
485 boolean isAnimatingInvisibleWithSavedSurface() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700486 for (int i = mChildren.size() - 1; i >= 0; i--) {
487 final WindowState w = (WindowState) mChildren.get(i);
Chong Zhang8e4bda92016-05-04 15:08:18 -0700488 if (w.isAnimatingInvisibleWithSavedSurface()) {
489 return true;
490 }
491 }
492 return false;
493 }
494
495 /**
496 * Hide all window surfaces that's still invisible in layout but animating
497 * with a saved surface, and mark them destroying.
498 */
499 void stopUsingSavedSurfaceLocked() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700500 for (int i = mChildren.size() - 1; i >= 0; i--) {
501 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700502 w.stopUsingSavedSurface();
Chong Zhang8e4bda92016-05-04 15:08:18 -0700503 }
504 destroySurfaces();
505 }
506
Chong Zhangf58631a2016-05-24 16:02:10 -0700507 void markSavedSurfaceExiting() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700508 for (int i = mChildren.size() - 1; i >= 0; i--) {
509 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700510 w.markSavedSurfaceExiting();
Chong Zhangf58631a2016-05-24 16:02:10 -0700511 }
512 }
513
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700514 void restoreSavedSurfaceForInterestingWindows() {
Chong Zhang92147042016-05-09 12:47:11 -0700515 if (!canRestoreSurfaces()) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700516 clearWasVisibleBeforeClientHidden();
Chong Zhangbef461f2015-10-27 11:38:24 -0700517 return;
518 }
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700519
520 // Check if all interesting windows are drawn and we can mark allDrawn=true.
521 int interestingNotDrawn = -1;
522
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700523 for (int i = mChildren.size() - 1; i >= 0; i--) {
524 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700525 interestingNotDrawn = w.restoreSavedSurfaceForInterestingWindow();
Chong Zhangbef461f2015-10-27 11:38:24 -0700526 }
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800527
Chong Zhang92147042016-05-09 12:47:11 -0700528 if (!allDrawn) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700529 allDrawn = (interestingNotDrawn == 0);
Chong Zhang92147042016-05-09 12:47:11 -0700530 if (allDrawn) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700531 mService.mH.obtainMessage(NOTIFY_ACTIVITY_DRAWN, token).sendToTarget();
Chong Zhang92147042016-05-09 12:47:11 -0700532 }
533 }
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700534 clearWasVisibleBeforeClientHidden();
Chong Zhangbfc2f8f2016-01-29 15:50:34 -0800535
536 if (DEBUG_APP_TRANSITIONS || DEBUG_ANIM) Slog.d(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700537 "restoreSavedSurfaceForInterestingWindows: " + this + " allDrawn=" + allDrawn
538 + " interestingNotDrawn=" + interestingNotDrawn);
Chong Zhangbef461f2015-10-27 11:38:24 -0700539 }
540
541 void destroySavedSurfaces() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700542 for (int i = mChildren.size() - 1; i >= 0; i--) {
543 final WindowState win = (WindowState) mChildren.get(i);
Robert Carr13f7be9e2015-12-02 18:39:45 -0800544 win.destroySavedSurface();
Chong Zhangbef461f2015-10-27 11:38:24 -0700545 }
Chong Zhang92147042016-05-09 12:47:11 -0700546 }
547
548 void clearAllDrawn() {
549 allDrawn = false;
550 deferClearAllDrawn = false;
Chong Zhang8e4bda92016-05-04 15:08:18 -0700551 allDrawnExcludingSaved = false;
Chong Zhangbef461f2015-10-27 11:38:24 -0700552 }
553
Wale Ogunwale98e70d02014-11-10 12:12:27 -0800554 @Override
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700555 void removeWindow(WindowState win) {
556 super.removeWindow(win);
557
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700558 // TODO: Something smells about the code below...Is there a better way?
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700559 if (startingWindow == win) {
560 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Notify removed startingWindow " + win);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700561 mService.scheduleRemoveStartingWindowLocked(this);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700562 } else if (mChildren.size() == 0 && startingData != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700563 // If this is the last window and we had requested a starting transition window,
564 // well there is no point now.
565 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Nulling last startingWindow");
566 startingData = null;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700567 } else if (mChildren.size() == 1 && startingView != null) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700568 // If this is the last window except for a starting transition window,
569 // we need to get rid of the starting transition.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700570 mService.scheduleRemoveStartingWindowLocked(this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700571 }
572 }
573
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700574 void removeDeadWindows() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700575 for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
576 WindowState win = (WindowState) mChildren.get(winNdx);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800577 if (win.mAppDied) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700578 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE) Slog.w(TAG,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700579 "removeDeadWindows: " + win);
Chong Zhang112eb8c2015-11-02 11:17:00 -0800580 // Set mDestroying, we don't want any animation or delayed removal here.
581 win.mDestroying = true;
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700582 // Also removes child windows.
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700583 win.removeIfPossible();
Chong Zhang112eb8c2015-11-02 11:17:00 -0800584 }
585 }
586 }
587
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700588 boolean hasWindowsAlive() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700589 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700590 // No need to loop through child windows as the answer should be the same as that of the
591 // parent window.
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700592 if (!((WindowState) mChildren.get(i)).mAppDied) {
Wale Ogunwalee42d0e12016-05-02 16:40:59 -0700593 return true;
594 }
595 }
596 return false;
597 }
598
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700599 void setWillReplaceWindows(boolean animate) {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700600 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
601 "Marking app token " + this + " with replacing windows.");
Robert Carra1eb4392015-12-10 12:43:51 -0800602
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700603 for (int i = mChildren.size() - 1; i >= 0; i--) {
604 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700605 w.setWillReplaceWindow(animate);
Robert Carra1eb4392015-12-10 12:43:51 -0800606 }
607 if (animate) {
608 // Set-up dummy animation so we can start treating windows associated with this
609 // token like they are in transition before the new app window is ready for us to
610 // run the real transition animation.
611 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM,
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700612 "setWillReplaceWindow() Setting dummy animation on: " + this);
Robert Carra1eb4392015-12-10 12:43:51 -0800613 mAppAnimator.setDummyAnimation();
614 }
615 }
616
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700617 void setWillReplaceChildWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700618 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM, "Marking app token " + this
Robert Carr23fa16b2016-01-13 13:19:58 -0800619 + " with replacing child windows.");
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700620 for (int i = mChildren.size() - 1; i >= 0; i--) {
621 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700622 w.setWillReplaceChildWindows();
Robert Carr23fa16b2016-01-13 13:19:58 -0800623 }
624 }
625
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700626 void clearWillReplaceWindows() {
Wale Ogunwale455fac52016-07-21 07:24:49 -0700627 if (DEBUG_ADD_REMOVE) Slog.d(TAG_WM,
628 "Resetting app token " + this + " of replacing window marks.");
Chong Zhangf596cd52016-01-05 13:42:44 -0800629
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700630 for (int i = mChildren.size() - 1; i >= 0; i--) {
631 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700632 w.clearWillReplaceWindow();
Chong Zhangf596cd52016-01-05 13:42:44 -0800633 }
634 }
635
Chong Zhang4d7369a2016-04-25 16:09:14 -0700636 void requestUpdateWallpaperIfNeeded() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700637 for (int i = mChildren.size() - 1; i >= 0; i--) {
638 final WindowState w = (WindowState) mChildren.get(i);
Chong Zhang4d7369a2016-04-25 16:09:14 -0700639 w.requestUpdateWallpaperIfNeeded();
640 }
641 }
642
Chong Zhangd78ddb42016-03-02 17:01:14 -0800643 boolean isRelaunching() {
644 return mPendingRelaunchCount > 0;
645 }
646
647 void startRelaunching() {
648 if (canFreezeBounds()) {
649 freezeBounds();
650 }
651 mPendingRelaunchCount++;
652 }
653
654 void finishRelaunching() {
655 if (canFreezeBounds()) {
656 unfreezeBounds();
657 }
658 if (mPendingRelaunchCount > 0) {
659 mPendingRelaunchCount--;
660 }
661 }
662
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700663 void clearRelaunching() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700664 if (mPendingRelaunchCount == 0) {
665 return;
666 }
Wale Ogunwale8fd75422016-06-24 14:20:37 -0700667 if (canFreezeBounds()) {
668 unfreezeBounds();
669 }
670 mPendingRelaunchCount = 0;
671 }
672
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700673 @Override
Robert Carra1eb4392015-12-10 12:43:51 -0800674 void addWindow(WindowState w) {
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700675 super.addWindow(w);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700676
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700677 boolean gotReplacementWindow = false;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700678 for (int i = mChildren.size() - 1; i >= 0; i--) {
679 final WindowState candidate = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700680 gotReplacementWindow |= candidate.setReplacementWindowIfNeeded(w);
681 }
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700682
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700683 // if we got a replacement window, reset the timeout to give drawing more time
684 if (gotReplacementWindow) {
685 mService.scheduleWindowReplacementTimeouts(this);
Robert Carra1eb4392015-12-10 12:43:51 -0800686 }
Robert Carra1eb4392015-12-10 12:43:51 -0800687 }
688
689 boolean waitingForReplacement() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700690 for (int i = mChildren.size() - 1; i >= 0; i--) {
691 final WindowState candidate = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700692 if (candidate.waitingForReplacement()) {
Robert Carra1eb4392015-12-10 12:43:51 -0800693 return true;
694 }
695 }
696 return false;
697 }
698
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700699 void onWindowReplacementTimeout() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700700 for (int i = mChildren.size() - 1; i >= 0; --i) {
701 ((WindowState) mChildren.get(i)).onWindowReplacementTimeout();
Robert Carra1eb4392015-12-10 12:43:51 -0800702 }
703 }
704
Chong Zhangd78ddb42016-03-02 17:01:14 -0800705 private boolean canFreezeBounds() {
706 // For freeform windows, we can't freeze the bounds at the moment because this would make
707 // the resizing unresponsive.
708 return mTask != null && !mTask.inFreeformWorkspace();
709 }
710
Jorim Jaggi0429f352015-12-22 16:29:16 +0100711 /**
712 * Freezes the task bounds. The size of this task reported the app will be fixed to the bounds
713 * freezed by {@link Task#prepareFreezingBounds} until {@link #unfreezeBounds} gets called, even
714 * if they change in the meantime. If the bounds are already frozen, the bounds will be frozen
715 * with a queue.
716 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800717 private void freezeBounds() {
Jorim Jaggi0429f352015-12-22 16:29:16 +0100718 mFrozenBounds.offer(new Rect(mTask.mPreparedFrozenBounds));
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700719
720 if (mTask.mPreparedFrozenMergedConfig.equals(Configuration.EMPTY)) {
721 // We didn't call prepareFreezingBounds on the task, so use the current value.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700722 final Configuration config = new Configuration(mService.mCurConfiguration);
Jorim Jaggi26c8c422016-05-09 19:57:25 -0700723 config.updateFrom(mTask.mOverrideConfig);
724 mFrozenMergedConfig.offer(config);
725 } else {
726 mFrozenMergedConfig.offer(new Configuration(mTask.mPreparedFrozenMergedConfig));
727 }
728 mTask.mPreparedFrozenMergedConfig.setToDefaults();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100729 }
730
731 /**
732 * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
733 */
Chong Zhangd78ddb42016-03-02 17:01:14 -0800734 private void unfreezeBounds() {
Wale Ogunwale37dbafc2016-06-27 10:15:20 -0700735 if (!mFrozenBounds.isEmpty()) {
736 mFrozenBounds.remove();
737 }
738 if (!mFrozenMergedConfig.isEmpty()) {
739 mFrozenMergedConfig.remove();
740 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700741 for (int i = mChildren.size() - 1; i >= 0; i--) {
742 final WindowState win = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700743 win.onUnfreezeBounds();
Jorim Jaggi4846ee32016-01-07 17:39:12 +0100744 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700745 mService.mWindowPlacerLocked.performSurfacePlacement();
Jorim Jaggi0429f352015-12-22 16:29:16 +0100746 }
747
Robert Carr91b228092016-06-28 17:32:37 -0700748 void addSurfaceViewBackground(WindowSurfaceController.SurfaceControlWithBackground background) {
749 mSurfaceViewBackgrounds.add(background);
750 }
751
752 void removeSurfaceViewBackground(WindowSurfaceController.SurfaceControlWithBackground background) {
753 mSurfaceViewBackgrounds.remove(background);
754 updateSurfaceViewBackgroundVisibilities();
755 }
756
757 // We use DimLayers behind SurfaceViews to prevent holes while resizing and creating.
758 // However, we need to ensure one SurfaceView doesn't cover another when they are both placed
759 // below the main app window (as traditionally a SurfaceView which is never drawn
760 // to is totally translucent). So we look at all our SurfaceView backgrounds and only enable
761 // the background for the SurfaceView with lowest Z order
762 void updateSurfaceViewBackgroundVisibilities() {
763 WindowSurfaceController.SurfaceControlWithBackground bottom = null;
764 int bottomLayer = Integer.MAX_VALUE;
765 for (int i = 0; i < mSurfaceViewBackgrounds.size(); i++) {
766 WindowSurfaceController.SurfaceControlWithBackground sc = mSurfaceViewBackgrounds.get(i);
767 if (sc.mVisible && sc.mLayer < bottomLayer) {
768 bottomLayer = sc.mLayer;
769 bottom = sc;
770 }
771 }
772 for (int i = 0; i < mSurfaceViewBackgrounds.size(); i++) {
773 WindowSurfaceController.SurfaceControlWithBackground sc = mSurfaceViewBackgrounds.get(i);
774 sc.updateBackgroundVisibility(sc != bottom);
775 }
776 }
777
Jorim Jaggi6626f542016-08-22 13:08:44 -0700778 /**
779 * See {@link WindowManagerService#overridePlayingAppAnimationsLw}
780 */
781 void overridePlayingAppAnimations(Animation a) {
782 if (mAppAnimator.isAnimating()) {
783 final WindowState win = findMainWindow();
784 final int width = win.mContainingFrame.width();
785 final int height = win.mContainingFrame.height();
786 mAppAnimator.setAnimation(a, width, height, false, STACK_CLIP_NONE);
787 }
788 }
789
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700790 void resetJustMovedInStack() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700791 for (int i = mChildren.size() - 1; i >= 0; i--) {
792 ((WindowState) mChildren.get(i)).resetJustMovedInStack();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700793 }
794 }
795
796 void notifyMovedInStack() {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700797 for (int winNdx = mChildren.size() - 1; winNdx >= 0; --winNdx) {
798 final WindowState win = (WindowState) mChildren.get(winNdx);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700799 win.notifyMovedInStack();
800 }
801 }
802
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700803 void setAppLayoutChanges(int changes, String reason, int displayId) {
804 final WindowAnimator windowAnimator = mAppAnimator.mAnimator;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700805 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700806 // Child windows will be on the same display as their parents.
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700807 if (displayId == ((WindowState) mChildren.get(i)).getDisplayId()) {
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700808 windowAnimator.setPendingLayoutChanges(displayId, changes);
809 if (DEBUG_LAYOUT_REPEATS) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700810 mService.mWindowPlacerLocked.debugLayoutRepeats(
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700811 reason, windowAnimator.getPendingLayoutChanges(displayId));
812 }
813 break;
814 }
815 }
816 }
817
818 void removeReplacedWindowIfNeeded(WindowState replacement) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700819 for (int i = mChildren.size() - 1; i >= 0; i--) {
820 final WindowState win = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700821 if (win.removeReplacedWindowIfNeeded(replacement)) {
822 return;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700823 }
824 }
825 }
826
827 void startFreezingScreen() {
828 if (DEBUG_ORIENTATION) logWithStack(TAG, "Set freezing of " + appToken + ": hidden="
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700829 + hidden + " freezing=" + mAppAnimator.freezingScreen + " hiddenRequested="
830 + hiddenRequested);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700831 if (!hiddenRequested) {
832 if (!mAppAnimator.freezingScreen) {
833 mAppAnimator.freezingScreen = true;
834 mAppAnimator.lastFreezeDuration = 0;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700835 mService.mAppsFreezingScreen++;
836 if (mService.mAppsFreezingScreen == 1) {
837 mService.startFreezingDisplayLocked(false, 0, 0);
838 mService.mH.removeMessages(H.APP_FREEZE_TIMEOUT);
839 mService.mH.sendEmptyMessageDelayed(H.APP_FREEZE_TIMEOUT, 2000);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700840 }
841 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700842 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700843 for (int i = 0; i < count; i++) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700844 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700845 w.onStartFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700846 }
847 }
848 }
849
850 void stopFreezingScreen(boolean unfreezeSurfaceNow, boolean force) {
851 if (!mAppAnimator.freezingScreen) {
852 return;
853 }
854 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Clear freezing of " + this + " force=" + force);
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700855 final int count = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700856 boolean unfrozeWindows = false;
857 for (int i = 0; i < count; i++) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700858 final WindowState w = (WindowState) mChildren.get(i);
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700859 unfrozeWindows |= w.onStopFreezingScreen();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700860 }
861 if (force || unfrozeWindows) {
862 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "No longer freezing: " + this);
863 mAppAnimator.freezingScreen = false;
864 mAppAnimator.lastFreezeDuration =
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700865 (int)(SystemClock.elapsedRealtime() - mService.mDisplayFreezeTime);
866 mService.mAppsFreezingScreen--;
867 mService.mLastFinishedFreezeSource = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700868 }
869 if (unfreezeSurfaceNow) {
870 if (unfrozeWindows) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700871 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700872 }
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700873 mService.stopFreezingDisplayLocked();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700874 }
875 }
876
877 boolean transferStartingWindow(IBinder transferFrom) {
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700878 final AppWindowToken fromToken = mService.findAppWindowToken(transferFrom);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700879 if (fromToken == null) {
880 return false;
881 }
882
883 final WindowState tStartingWindow = fromToken.startingWindow;
884 if (tStartingWindow != null && fromToken.startingView != null) {
885 // In this case, the starting icon has already been displayed, so start
886 // letting windows get shown immediately without any more transitions.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700887 mService.mSkipAppTransitionAnimation = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700888
889 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Moving existing starting " + tStartingWindow
890 + " from " + fromToken + " to " + this);
891
892 final long origId = Binder.clearCallingIdentity();
893
894 // Transfer the starting window over to the new token.
895 startingData = fromToken.startingData;
896 startingView = fromToken.startingView;
897 startingDisplayed = fromToken.startingDisplayed;
898 fromToken.startingDisplayed = false;
899 startingWindow = tStartingWindow;
900 reportedVisible = fromToken.reportedVisible;
901 fromToken.startingData = null;
902 fromToken.startingView = null;
903 fromToken.startingWindow = null;
904 fromToken.startingMoved = true;
905 tStartingWindow.mToken = this;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700906 tStartingWindow.mAppToken = this;
907
908 if (DEBUG_WINDOW_MOVEMENT || DEBUG_ADD_REMOVE || DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
909 "Removing starting window: " + tStartingWindow);
910 tStartingWindow.getWindowList().remove(tStartingWindow);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700911 mService.mWindowsChanged = true;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700912 if (DEBUG_ADD_REMOVE) Slog.v(TAG_WM,
913 "Removing starting " + tStartingWindow + " from " + fromToken);
914 fromToken.removeWindow(tStartingWindow);
Wale Ogunwale92fc3722016-08-05 12:19:08 -0700915 addWindow(tStartingWindow);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700916
917 // Propagate other interesting state between the tokens. If the old token is displayed,
918 // we should immediately force the new one to be displayed. If it is animating, we need
919 // to move that animation to the new one.
920 if (fromToken.allDrawn) {
921 allDrawn = true;
922 deferClearAllDrawn = fromToken.deferClearAllDrawn;
923 }
924 if (fromToken.firstWindowDrawn) {
925 firstWindowDrawn = true;
926 }
927 if (!fromToken.hidden) {
928 hidden = false;
929 hiddenRequested = false;
930 }
931 if (clientHidden != fromToken.clientHidden) {
932 clientHidden = fromToken.clientHidden;
933 sendAppVisibilityToClients();
934 }
935 fromToken.mAppAnimator.transferCurrentAnimation(
936 mAppAnimator, tStartingWindow.mWinAnimator);
937
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700938 mService.updateFocusedWindowLocked(
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700939 UPDATE_FOCUS_WILL_PLACE_SURFACES, true /*updateInputWindows*/);
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700940 mService.getDefaultDisplayContentLocked().layoutNeeded = true;
941 mService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700942 Binder.restoreCallingIdentity(origId);
943 return true;
944 } else if (fromToken.startingData != null) {
945 // The previous app was getting ready to show a
946 // starting window, but hasn't yet done so. Steal it!
947 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
948 "Moving pending starting from " + fromToken + " to " + this);
949 startingData = fromToken.startingData;
950 fromToken.startingData = null;
951 fromToken.startingMoved = true;
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700952 final Message m = mService.mH.obtainMessage(H.ADD_STARTING, this);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700953 // Note: we really want to do sendMessageAtFrontOfQueue() because we want to process the
954 // message ASAP, before any other queued messages.
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700955 mService.mH.sendMessageAtFrontOfQueue(m);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700956 return true;
957 }
958
959 final AppWindowAnimator tAppAnimator = fromToken.mAppAnimator;
960 final AppWindowAnimator wAppAnimator = mAppAnimator;
961 if (tAppAnimator.thumbnail != null) {
962 // The old token is animating with a thumbnail, transfer that to the new token.
963 if (wAppAnimator.thumbnail != null) {
964 wAppAnimator.thumbnail.destroy();
965 }
966 wAppAnimator.thumbnail = tAppAnimator.thumbnail;
967 wAppAnimator.thumbnailLayer = tAppAnimator.thumbnailLayer;
968 wAppAnimator.thumbnailAnimation = tAppAnimator.thumbnailAnimation;
969 tAppAnimator.thumbnail = null;
970 }
971 return false;
972 }
973
Wale Ogunwale9bc47732016-08-10 14:44:22 -0700974 boolean isLastWindow(WindowState win) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700975 return mChildren.size() == 1 && mChildren.get(0) == win;
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700976 }
977
978 void setAllAppWinAnimators() {
979 final ArrayList<WindowStateAnimator> allAppWinAnimators = mAppAnimator.mAllAppWinAnimators;
980 allAppWinAnimators.clear();
981
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700982 final int windowsCount = mChildren.size();
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700983 for (int j = 0; j < windowsCount; j++) {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700984 ((WindowState) mChildren.get(j)).addWinAnimatorToList(allAppWinAnimators);
Wale Ogunwale9f25bee2016-08-02 07:23:47 -0700985 }
986 }
987
Craig Mautnerdbb79912012-03-01 18:59:14 -0800988 @Override
Wale Ogunwale2049dbf2016-08-02 21:05:23 -0700989 AppWindowToken asAppWindowToken() {
990 // I am an app window token!
991 return this;
992 }
993
994 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800995 void dump(PrintWriter pw, String prefix) {
996 super.dump(pw, prefix);
997 if (appToken != null) {
Dianne Hackborne30e02f2014-05-27 18:24:45 -0700998 pw.print(prefix); pw.print("app=true voiceInteraction="); pw.println(voiceInteraction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800999 }
Craig Mautner83162a92015-01-26 14:43:30 -08001000 pw.print(prefix); pw.print("task="); pw.println(mTask);
1001 pw.print(prefix); pw.print(" appFullscreen="); pw.print(appFullscreen);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001002 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
1003 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
1004 pw.print(" clientHidden="); pw.print(clientHidden);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -07001005 pw.print(" reportedDrawn="); pw.print(reportedDrawn);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001006 pw.print(" reportedVisible="); pw.println(reportedVisible);
Craig Mautner59431632012-04-04 11:56:44 -07001007 if (paused) {
1008 pw.print(prefix); pw.print("paused="); pw.println(paused);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001009 }
Wale Ogunwale9017ec02016-02-25 08:55:25 -08001010 if (mAppStopped) {
1011 pw.print(prefix); pw.print("mAppStopped="); pw.println(mAppStopped);
1012 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001013 if (numInterestingWindows != 0 || numDrawnWindows != 0
Craig Mautner6fbda632012-07-03 09:26:39 -07001014 || allDrawn || mAppAnimator.allDrawn) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001015 pw.print(prefix); pw.print("numInterestingWindows=");
1016 pw.print(numInterestingWindows);
1017 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
1018 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
Craig Mautner6fbda632012-07-03 09:26:39 -07001019 pw.print(" allDrawn="); pw.print(allDrawn);
1020 pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
1021 pw.println(")");
1022 }
1023 if (inPendingTransaction) {
1024 pw.print(prefix); pw.print("inPendingTransaction=");
1025 pw.println(inPendingTransaction);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001026 }
Craig Mautner799bc1d2015-01-14 10:33:48 -08001027 if (startingData != null || removed || firstWindowDrawn || mIsExiting) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001028 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
1029 pw.print(" removed="); pw.print(removed);
Craig Mautner3d7ca312015-01-08 10:56:00 -08001030 pw.print(" firstWindowDrawn="); pw.print(firstWindowDrawn);
Craig Mautner799bc1d2015-01-14 10:33:48 -08001031 pw.print(" mIsExiting="); pw.println(mIsExiting);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001032 }
1033 if (startingWindow != null || startingView != null
1034 || startingDisplayed || startingMoved) {
1035 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
1036 pw.print(" startingView="); pw.print(startingView);
1037 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
Wale Ogunwale9017ec02016-02-25 08:55:25 -08001038 pw.print(" startingMoved="); pw.println(startingMoved);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001039 }
Jorim Jaggi0429f352015-12-22 16:29:16 +01001040 if (!mFrozenBounds.isEmpty()) {
Chong Zhangd78ddb42016-03-02 17:01:14 -08001041 pw.print(prefix); pw.print("mFrozenBounds="); pw.println(mFrozenBounds);
Jorim Jaggi26c8c422016-05-09 19:57:25 -07001042 pw.print(prefix); pw.print("mFrozenMergedConfig="); pw.println(mFrozenMergedConfig);
Chong Zhangd78ddb42016-03-02 17:01:14 -08001043 }
1044 if (mPendingRelaunchCount != 0) {
1045 pw.print(prefix); pw.print("mPendingRelaunchCount="); pw.println(mPendingRelaunchCount);
Jorim Jaggi0429f352015-12-22 16:29:16 +01001046 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001047 }
1048
1049 @Override
1050 public String toString() {
1051 if (stringName == null) {
1052 StringBuilder sb = new StringBuilder();
1053 sb.append("AppWindowToken{");
1054 sb.append(Integer.toHexString(System.identityHashCode(this)));
1055 sb.append(" token="); sb.append(token); sb.append('}');
1056 stringName = sb.toString();
1057 }
1058 return stringName;
1059 }
Jeff Browne9bdb312012-04-05 15:30:10 -07001060}