blob: 40f772aaa529e688a7bd9451941ce95aaa18dffa [file] [log] [blame]
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -08001/*
2 * Copyright (C) 2016 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
19import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
20import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Matthew Ng606dd802017-06-05 14:06:32 -070021
Jorim Jaggif84e2f62018-01-16 14:17:59 +010022import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
23import static android.view.WindowManager.TRANSIT_UNSET;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080024import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
25import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
26import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080027import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW;
28import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
29import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
30import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080031
Jorim Jaggie2c77f92016-12-29 14:57:22 +010032import android.app.ActivityManager.TaskSnapshot;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080033import android.content.res.CompatibilityInfo;
34import android.content.res.Configuration;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080035import android.os.Debug;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080036import android.os.Handler;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080037import android.os.IBinder;
Sudheer Shankac766db02017-06-12 10:37:29 -070038import android.os.Looper;
39import android.os.Message;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080040import android.util.Slog;
41import android.view.IApplicationToken;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010042import android.view.RemoteAnimationDefinition;
43import android.view.WindowManager;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080044
Winson Chung30480042017-01-26 10:55:34 -080045import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -080046import com.android.server.AttributeCache;
Adrian Roose99bc052017-11-20 17:55:31 +010047import com.android.server.policy.WindowManagerPolicy.StartingSurface;
48
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080049/**
50 * Controller for the app window token container. This is created by activity manager to link
51 * activity records to the app window token container they use in window manager.
52 *
53 * Test class: {@link AppWindowContainerControllerTests}
54 */
55public class AppWindowContainerController
56 extends WindowContainerController<AppWindowToken, AppWindowContainerListener> {
57
Jorim Jaggi02886a82016-12-06 09:10:06 -080058 private static final int STARTING_WINDOW_TYPE_NONE = 0;
59 private static final int STARTING_WINDOW_TYPE_SNAPSHOT = 1;
60 private static final int STARTING_WINDOW_TYPE_SPLASH_SCREEN = 2;
61
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080062 private final IApplicationToken mToken;
Jorim Jaggi9bafc712017-01-19 17:28:30 +010063 private final Handler mHandler;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080064
Sudheer Shankac766db02017-06-12 10:37:29 -070065 private final class H extends Handler {
66 public static final int NOTIFY_WINDOWS_DRAWN = 1;
67 public static final int NOTIFY_STARTING_WINDOW_DRAWN = 2;
Jorim Jaggi3878ca32017-02-02 17:13:05 -080068
Sudheer Shankac766db02017-06-12 10:37:29 -070069 public H(Looper looper) {
70 super(looper);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080071 }
Sudheer Shankac766db02017-06-12 10:37:29 -070072
73 @Override
74 public void handleMessage(Message msg) {
75 switch (msg.what) {
76 case NOTIFY_WINDOWS_DRAWN:
77 if (mListener == null) {
78 return;
79 }
80 if (DEBUG_VISIBILITY) Slog.v(TAG_WM, "Reporting drawn in "
81 + AppWindowContainerController.this.mToken);
82 mListener.onWindowsDrawn(msg.getWhen());
83 break;
84 case NOTIFY_STARTING_WINDOW_DRAWN:
85 if (mListener == null) {
86 return;
87 }
88 if (DEBUG_VISIBILITY) Slog.v(TAG_WM, "Reporting drawn in "
89 + AppWindowContainerController.this.mToken);
90 mListener.onStartingWindowDrawn(msg.getWhen());
91 break;
92 default:
93 break;
94 }
95 }
96 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -080097
98 private final Runnable mOnWindowsVisible = () -> {
99 if (mListener == null) {
100 return;
101 }
102 if (DEBUG_VISIBILITY) Slog.v(TAG_WM, "Reporting visible in "
103 + AppWindowContainerController.this.mToken);
104 mListener.onWindowsVisible();
105 };
106
107 private final Runnable mOnWindowsGone = () -> {
108 if (mListener == null) {
109 return;
110 }
111 if (DEBUG_VISIBILITY) Slog.v(TAG_WM, "Reporting gone in "
112 + AppWindowContainerController.this.mToken);
113 mListener.onWindowsGone();
114 };
115
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800116 private final Runnable mAddStartingWindow = () -> {
117 final StartingData startingData;
Jorim Jaggie09fc6b2017-01-19 15:33:36 +0100118 final AppWindowToken container;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800119
120 synchronized (mWindowMap) {
Jorim Jaggi73f88202017-01-12 13:54:40 +0100121 if (mContainer == null) {
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200122 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "mContainer was null while trying to"
123 + " add starting window");
Jorim Jaggi73f88202017-01-12 13:54:40 +0100124 return;
125 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800126 startingData = mContainer.startingData;
Jorim Jaggie09fc6b2017-01-19 15:33:36 +0100127 container = mContainer;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800128 }
129
130 if (startingData == null) {
131 // Animation has been canceled... do nothing.
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200132 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "startingData was nulled out before handling"
133 + " mAddStartingWindow: " + mContainer);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800134 return;
135 }
136
137 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Add starting "
Jorim Jaggie09fc6b2017-01-19 15:33:36 +0100138 + this + ": startingData=" + container.startingData);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800139
Jorim Jaggi02886a82016-12-06 09:10:06 -0800140 StartingSurface surface = null;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800141 try {
Jorim Jaggie09fc6b2017-01-19 15:33:36 +0100142 surface = startingData.createStartingSurface(container);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800143 } catch (Exception e) {
144 Slog.w(TAG_WM, "Exception when adding starting window", e);
145 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800146 if (surface != null) {
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800147 boolean abort = false;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800148 synchronized(mWindowMap) {
Jorim Jaggicfeff742017-05-23 18:00:48 +0200149 // If the window was successfully added, then
150 // we need to remove it.
Jorim Jaggie09fc6b2017-01-19 15:33:36 +0100151 if (container.removed || container.startingData == null) {
Jorim Jaggicfeff742017-05-23 18:00:48 +0200152 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
153 "Aborted starting " + container
154 + ": removed=" + container.removed
155 + " startingData=" + container.startingData);
156 container.startingWindow = null;
157 container.startingData = null;
158 abort = true;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800159 } else {
Jorim Jaggie09fc6b2017-01-19 15:33:36 +0100160 container.startingSurface = surface;
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800161 }
162 if (DEBUG_STARTING_WINDOW && !abort) Slog.v(TAG_WM,
163 "Added starting " + mContainer
164 + ": startingWindow="
Jorim Jaggie09fc6b2017-01-19 15:33:36 +0100165 + container.startingWindow + " startingView="
166 + container.startingSurface);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800167 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800168 if (abort) {
Jorim Jaggie09fc6b2017-01-19 15:33:36 +0100169 surface.remove();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800170 }
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200171 } else if (DEBUG_STARTING_WINDOW) {
172 Slog.v(TAG_WM, "Surface returned was null: " + mContainer);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800173 }
174 };
175
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800176 public AppWindowContainerController(TaskWindowContainerController taskController,
177 IApplicationToken token, AppWindowContainerListener listener, int index,
178 int requestedOrientation, boolean fullscreen, boolean showForAllUsers, int configChanges,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800179 boolean voiceInteraction, boolean launchTaskBehind, boolean alwaysFocusable,
Bryce Leef3c6a472017-11-14 14:53:06 -0800180 int targetSdkVersion, int rotationAnimationHint, long inputDispatchingTimeoutNanos) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800181 this(taskController, token, listener, index, requestedOrientation, fullscreen,
182 showForAllUsers,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800183 configChanges, voiceInteraction, launchTaskBehind, alwaysFocusable,
184 targetSdkVersion, rotationAnimationHint, inputDispatchingTimeoutNanos,
Bryce Leef3c6a472017-11-14 14:53:06 -0800185 WindowManagerService.getInstance());
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800186 }
187
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800188 public AppWindowContainerController(TaskWindowContainerController taskController,
189 IApplicationToken token, AppWindowContainerListener listener, int index,
190 int requestedOrientation, boolean fullscreen, boolean showForAllUsers, int configChanges,
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800191 boolean voiceInteraction, boolean launchTaskBehind, boolean alwaysFocusable,
192 int targetSdkVersion, int rotationAnimationHint, long inputDispatchingTimeoutNanos,
Bryce Leef3c6a472017-11-14 14:53:06 -0800193 WindowManagerService service) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800194 super(listener, service);
Sudheer Shankac766db02017-06-12 10:37:29 -0700195 mHandler = new H(service.mH.getLooper());
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800196 mToken = token;
197 synchronized(mWindowMap) {
198 AppWindowToken atoken = mRoot.getAppWindowToken(mToken.asBinder());
199 if (atoken != null) {
200 // TODO: Should this throw an exception instead?
201 Slog.w(TAG_WM, "Attempted to add existing app token: " + mToken);
202 return;
203 }
204
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800205 final Task task = taskController.mContainer;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800206 if (task == null) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800207 throw new IllegalArgumentException("AppWindowContainerController: invalid "
208 + " controller=" + taskController);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800209 }
210
Winson Chung30480042017-01-26 10:55:34 -0800211 atoken = createAppWindow(mService, token, voiceInteraction, task.getDisplayContent(),
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800212 inputDispatchingTimeoutNanos, fullscreen, showForAllUsers, targetSdkVersion,
213 requestedOrientation, rotationAnimationHint, configChanges, launchTaskBehind,
Bryce Leef3c6a472017-11-14 14:53:06 -0800214 alwaysFocusable, this);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800215 if (DEBUG_TOKEN_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM, "addAppToken: " + atoken
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800216 + " controller=" + taskController + " at " + index);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800217 task.addChild(atoken, index);
218 }
219 }
220
Winson Chung30480042017-01-26 10:55:34 -0800221 @VisibleForTesting
222 AppWindowToken createAppWindow(WindowManagerService service, IApplicationToken token,
223 boolean voiceInteraction, DisplayContent dc, long inputDispatchingTimeoutNanos,
224 boolean fullscreen, boolean showForAllUsers, int targetSdk, int orientation,
225 int rotationAnimationHint, int configChanges, boolean launchTaskBehind,
Bryce Leef3c6a472017-11-14 14:53:06 -0800226 boolean alwaysFocusable, AppWindowContainerController controller) {
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100227 return new AppWindowToken(service, token, voiceInteraction, dc,
Winson Chung30480042017-01-26 10:55:34 -0800228 inputDispatchingTimeoutNanos, fullscreen, showForAllUsers, targetSdk, orientation,
229 rotationAnimationHint, configChanges, launchTaskBehind, alwaysFocusable,
Bryce Leef3c6a472017-11-14 14:53:06 -0800230 controller);
Winson Chung30480042017-01-26 10:55:34 -0800231 }
232
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800233 public void removeContainer(int displayId) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800234 synchronized(mWindowMap) {
235 final DisplayContent dc = mRoot.getDisplayContent(displayId);
236 if (dc == null) {
237 Slog.w(TAG_WM, "removeAppToken: Attempted to remove binder token: "
238 + mToken + " from non-existing displayId=" + displayId);
239 return;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800240 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800241 dc.removeAppToken(mToken.asBinder());
242 super.removeContainer();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800243 }
244 }
245
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800246 @Override
247 public void removeContainer() {
248 throw new UnsupportedOperationException("Use removeContainer(displayId) instead.");
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800249 }
250
Winson Chung30480042017-01-26 10:55:34 -0800251 public void reparent(TaskWindowContainerController taskController, int position) {
252 synchronized (mWindowMap) {
253 if (DEBUG_ADD_REMOVE) Slog.i(TAG_WM, "reparent: moving app token=" + mToken
254 + " to task=" + taskController + " at " + position);
255 if (mContainer == null) {
256 if (DEBUG_ADD_REMOVE) Slog.i(TAG_WM,
257 "reparent: could not find app token=" + mToken);
258 return;
259 }
260 final Task task = taskController.mContainer;
261 if (task == null) {
262 throw new IllegalArgumentException("reparent: could not find task="
263 + taskController);
264 }
265 mContainer.reparent(task, position);
266 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
267 }
268 }
269
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800270 public Configuration setOrientation(int requestedOrientation, int displayId,
271 Configuration displayConfig, boolean freezeScreenIfNeeded) {
272 synchronized(mWindowMap) {
273 if (mContainer == null) {
274 Slog.w(TAG_WM,
275 "Attempted to set orientation of non-existing app token: " + mToken);
276 return null;
277 }
278
279 mContainer.setOrientation(requestedOrientation);
280
281 final IBinder binder = freezeScreenIfNeeded ? mToken.asBinder() : null;
282 return mService.updateOrientationFromAppTokens(displayConfig, binder, displayId);
283
284 }
285 }
286
287 public int getOrientation() {
288 synchronized(mWindowMap) {
289 if (mContainer == null) {
290 return SCREEN_ORIENTATION_UNSPECIFIED;
291 }
292
293 return mContainer.getOrientationIgnoreVisibility();
294 }
295 }
296
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100297 public void setDisablePreviewScreenshots(boolean disable) {
298 synchronized (mWindowMap) {
299 if (mContainer == null) {
300 Slog.w(TAG_WM, "Attempted to set disable screenshots of non-existing app"
301 + " token: " + mToken);
302 return;
303 }
Jorim Jaggid635a4a2017-05-03 15:21:26 +0200304 mContainer.setDisablePreviewScreenshots(disable);
Jorim Jaggi0fe7ce962017-02-22 16:45:48 +0100305 }
306 }
307
Tim Murray68ed8442017-08-29 23:21:27 +0000308 public void setVisibility(boolean visible, boolean deferHidingClient) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800309 synchronized(mWindowMap) {
310 if (mContainer == null) {
311 Slog.w(TAG_WM, "Attempted to set visibility of non-existing app token: "
312 + mToken);
313 return;
314 }
315
316 final AppWindowToken wtoken = mContainer;
317
Wale Ogunwale0c20ee32017-05-23 10:34:48 -0700318 // Don't set visibility to false if we were already not visible. This prevents WM from
319 // adding the app to the closing app list which doesn't make sense for something that is
320 // already not visible. However, set visibility to true even if we are already visible.
321 // This makes sure the app is added to the opening apps list so that the right
322 // transition can be selected.
323 // TODO: Probably a good idea to separate the concept of opening/closing apps from the
324 // concept of setting visibility...
325 if (!visible && wtoken.hiddenRequested) {
326
327 if (!deferHidingClient && wtoken.mDeferHidingClient) {
328 // We previously deferred telling the client to hide itself when visibility was
329 // initially set to false. Now we would like it to hide, so go ahead and set it.
330 wtoken.mDeferHidingClient = deferHidingClient;
331 wtoken.setClientHidden(true);
332 }
333 return;
334 }
335
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800336 if (DEBUG_APP_TRANSITIONS || DEBUG_ORIENTATION) Slog.v(TAG_WM, "setAppVisibility("
337 + mToken + ", visible=" + visible + "): " + mService.mAppTransition
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200338 + " hidden=" + wtoken.isHidden() + " hiddenRequested="
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800339 + wtoken.hiddenRequested + " Callers=" + Debug.getCallers(6));
340
341 mService.mOpeningApps.remove(wtoken);
342 mService.mClosingApps.remove(wtoken);
343 wtoken.waitingToShow = false;
344 wtoken.hiddenRequested = !visible;
Wale Ogunwale89973222017-04-23 18:39:45 -0700345 wtoken.mDeferHidingClient = deferHidingClient;
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800346
347 if (!visible) {
348 // If the app is dead while it was visible, we kept its dead window on screen.
349 // Now that the app is going invisible, we can remove it. It will be restarted
350 // if made visible again.
351 wtoken.removeDeadWindows();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800352 } else {
353 if (!mService.mAppTransition.isTransitionSet()
354 && mService.mAppTransition.isReady()) {
355 // Add the app mOpeningApps if transition is unset but ready. This means
356 // we're doing a screen freeze, and the unfreeze will wait for all opening
357 // apps to be ready.
358 mService.mOpeningApps.add(wtoken);
359 }
360 wtoken.startingMoved = false;
361 // If the token is currently hidden (should be the common case), or has been
362 // stopped, then we need to set up to wait for its windows to be ready.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200363 if (wtoken.isHidden() || wtoken.mAppStopped) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800364 wtoken.clearAllDrawn();
365
366 // If the app was already visible, don't reset the waitingToShow state.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200367 if (wtoken.isHidden()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800368 wtoken.waitingToShow = true;
369 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800370 }
Jorim Jaggi067b5bf2018-02-23 17:42:39 +0100371
372 // In the case where we are making an app visible but holding off for a transition,
373 // we still need to tell the client to make its windows visible so they get drawn.
374 // Otherwise, we will wait on performing the transition until all windows have been
375 // drawn, they never will be, and we are sad.
376 wtoken.setClientHidden(false);
377
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800378 wtoken.requestUpdateWallpaperIfNeeded();
379
380 if (DEBUG_ADD_REMOVE) Slog.v(TAG_WM, "No longer Stopped: " + wtoken);
381 wtoken.mAppStopped = false;
382 }
383
384 // If we are preparing an app transition, then delay changing
385 // the visibility of this token until we execute that transition.
David Stevensf62360c2017-03-16 19:00:20 -0700386 if (wtoken.okToAnimate() && mService.mAppTransition.isTransitionSet()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800387 wtoken.inPendingTransaction = true;
388 if (visible) {
389 mService.mOpeningApps.add(wtoken);
390 wtoken.mEnteringAnimation = true;
391 } else {
392 mService.mClosingApps.add(wtoken);
393 wtoken.mEnteringAnimation = false;
394 }
395 if (mService.mAppTransition.getAppTransition()
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100396 == WindowManager.TRANSIT_TASK_OPEN_BEHIND) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800397 // We're launchingBehind, add the launching activity to mOpeningApps.
398 final WindowState win =
399 mService.getDefaultDisplayContentLocked().findFocusedWindow();
400 if (win != null) {
401 final AppWindowToken focusedToken = win.mAppToken;
402 if (focusedToken != null) {
403 if (DEBUG_APP_TRANSITIONS) Slog.d(TAG_WM, "TRANSIT_TASK_OPEN_BEHIND, "
404 + " adding " + focusedToken + " to mOpeningApps");
405 // Force animation to be loaded.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200406 focusedToken.setHidden(true);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800407 mService.mOpeningApps.add(focusedToken);
408 }
409 }
410 }
411 return;
412 }
413
414 wtoken.setVisibility(null, visible, TRANSIT_UNSET, true, wtoken.mVoiceInteraction);
415 wtoken.updateReportedVisibilityLocked();
416 }
417 }
418
419 /**
420 * Notifies that we launched an app that might be visible or not visible depending on what kind
421 * of Keyguard flags it's going to set on its windows.
422 */
423 public void notifyUnknownVisibilityLaunched() {
424 synchronized(mWindowMap) {
425 if (mContainer != null) {
426 mService.mUnknownAppVisibilityController.notifyLaunched(mContainer);
427 }
428 }
429 }
430
431 public boolean addStartingWindow(String pkg, int theme, CompatibilityInfo compatInfo,
432 CharSequence nonLocalizedLabel, int labelRes, int icon, int logo, int windowFlags,
Jorim Jaggibae01b12017-04-11 16:29:10 -0700433 IBinder transferFrom, boolean newTask, boolean taskSwitch, boolean processRunning,
Jorim Jaggi42befc62017-06-13 11:54:04 -0700434 boolean allowTaskSnapshot, boolean activityCreated, boolean fromRecents) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800435 synchronized(mWindowMap) {
436 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "setAppStartingWindow: token=" + mToken
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200437 + " pkg=" + pkg + " transferFrom=" + transferFrom + " newTask=" + newTask
438 + " taskSwitch=" + taskSwitch + " processRunning=" + processRunning
439 + " allowTaskSnapshot=" + allowTaskSnapshot);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800440
441 if (mContainer == null) {
442 Slog.w(TAG_WM, "Attempted to set icon of non-existing app token: " + mToken);
443 return false;
444 }
445
446 // If the display is frozen, we won't do anything until the actual window is
447 // displayed so there is no reason to put in the starting window.
David Stevensf62360c2017-03-16 19:00:20 -0700448 if (!mContainer.okToDisplay()) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800449 return false;
450 }
451
452 if (mContainer.startingData != null) {
453 return false;
454 }
455
Wale Ogunwale4ba3e832017-04-20 16:45:13 -0700456 final WindowState mainWin = mContainer.findMainWindow();
Jorim Jaggie6b1b3b2017-07-24 16:44:18 +0200457 if (mainWin != null && mainWin.mWinAnimator.getShown()) {
458 // App already has a visible window...why would you want a starting window?
Wale Ogunwale4ba3e832017-04-20 16:45:13 -0700459 return false;
460 }
461
Jorim Jaggi42befc62017-06-13 11:54:04 -0700462 final TaskSnapshot snapshot = mService.mTaskSnapshotController.getSnapshot(
463 mContainer.getTask().mTaskId, mContainer.getTask().mUserId,
464 false /* restoreFromDisk */, false /* reducedResolution */);
Jorim Jaggibae01b12017-04-11 16:29:10 -0700465 final int type = getStartingWindowType(newTask, taskSwitch, processRunning,
Jorim Jaggi42befc62017-06-13 11:54:04 -0700466 allowTaskSnapshot, activityCreated, fromRecents, snapshot);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800467
468 if (type == STARTING_WINDOW_TYPE_SNAPSHOT) {
Jorim Jaggi42befc62017-06-13 11:54:04 -0700469 return createSnapshot(snapshot);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800470 }
471
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800472 // If this is a translucent window, then don't show a starting window -- the current
473 // effect (a full-screen opaque starting window that fades away to the real contents
474 // when it is ready) does not work for this.
475 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Checking theme of starting window: 0x"
476 + Integer.toHexString(theme));
477 if (theme != 0) {
478 AttributeCache.Entry ent = AttributeCache.instance().get(pkg, theme,
479 com.android.internal.R.styleable.Window, mService.mCurrentUserId);
480 if (ent == null) {
481 // Whoops! App doesn't exist. Um. Okay. We'll just pretend like we didn't
482 // see that.
483 return false;
484 }
485 final boolean windowIsTranslucent = ent.array.getBoolean(
486 com.android.internal.R.styleable.Window_windowIsTranslucent, false);
487 final boolean windowIsFloating = ent.array.getBoolean(
488 com.android.internal.R.styleable.Window_windowIsFloating, false);
489 final boolean windowShowWallpaper = ent.array.getBoolean(
490 com.android.internal.R.styleable.Window_windowShowWallpaper, false);
491 final boolean windowDisableStarting = ent.array.getBoolean(
492 com.android.internal.R.styleable.Window_windowDisablePreview, false);
493 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Translucent=" + windowIsTranslucent
494 + " Floating=" + windowIsFloating
495 + " ShowWallpaper=" + windowShowWallpaper);
496 if (windowIsTranslucent) {
497 return false;
498 }
499 if (windowIsFloating || windowDisableStarting) {
500 return false;
501 }
502 if (windowShowWallpaper) {
503 if (mContainer.getDisplayContent().mWallpaperController.getWallpaperTarget()
504 == null) {
505 // If this theme is requesting a wallpaper, and the wallpaper
506 // is not currently visible, then this effectively serves as
507 // an opaque window and our starting window transition animation
508 // can still work. We just need to make sure the starting window
509 // is also showing the wallpaper.
510 windowFlags |= FLAG_SHOW_WALLPAPER;
511 } else {
512 return false;
513 }
514 }
515 }
516
517 if (mContainer.transferStartingWindow(transferFrom)) {
518 return true;
519 }
520
Jorim Jaggi02886a82016-12-06 09:10:06 -0800521 // There is no existing starting window, and we don't want to create a splash screen, so
522 // that's it!
523 if (type != STARTING_WINDOW_TYPE_SPLASH_SCREEN) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800524 return false;
525 }
526
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200527 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Creating SplashScreenStartingData");
Jorim Jaggie09fc6b2017-01-19 15:33:36 +0100528 mContainer.startingData = new SplashScreenStartingData(mService, pkg, theme,
Jorim Jaggi02886a82016-12-06 09:10:06 -0800529 compatInfo, nonLocalizedLabel, labelRes, icon, logo, windowFlags,
530 mContainer.getMergedOverrideConfiguration());
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800531 scheduleAddStartingWindow();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800532 }
533 return true;
534 }
535
Jorim Jaggibae01b12017-04-11 16:29:10 -0700536 private int getStartingWindowType(boolean newTask, boolean taskSwitch, boolean processRunning,
Jorim Jaggi42befc62017-06-13 11:54:04 -0700537 boolean allowTaskSnapshot, boolean activityCreated, boolean fromRecents,
538 TaskSnapshot snapshot) {
Matthew Ng606dd802017-06-05 14:06:32 -0700539 if (mService.mAppTransition.getAppTransition() == TRANSIT_DOCK_TASK_FROM_RECENTS) {
540 // TODO(b/34099271): Remove this statement to add back the starting window and figure
541 // out why it causes flickering, the starting window appears over the thumbnail while
542 // the docked from recents transition occurs
543 return STARTING_WINDOW_TYPE_NONE;
544 } else if (newTask || !processRunning || (taskSwitch && !activityCreated)) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800545 return STARTING_WINDOW_TYPE_SPLASH_SCREEN;
Jorim Jaggibae01b12017-04-11 16:29:10 -0700546 } else if (taskSwitch && allowTaskSnapshot) {
Jorim Jaggi42befc62017-06-13 11:54:04 -0700547 return snapshot == null ? STARTING_WINDOW_TYPE_NONE
Jorim Jaggi1dda7a62017-06-28 14:40:27 -0400548 : snapshotOrientationSameAsTask(snapshot) || fromRecents
Wale Ogunwaledfb7fb22017-06-23 14:52:40 -0700549 ? STARTING_WINDOW_TYPE_SNAPSHOT : STARTING_WINDOW_TYPE_SPLASH_SCREEN;
Jorim Jaggi02886a82016-12-06 09:10:06 -0800550 } else {
551 return STARTING_WINDOW_TYPE_NONE;
552 }
553 }
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800554
Jorim Jaggi02886a82016-12-06 09:10:06 -0800555 void scheduleAddStartingWindow() {
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800556 // Note: we really want to do sendMessageAtFrontOfQueue() because we
557 // want to process the message ASAP, before any other queued
558 // messages.
559 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Enqueueing ADD_STARTING");
Jorim Jaggied7993b2017-03-28 18:50:01 +0100560 mService.mAnimationHandler.postAtFrontOfQueue(mAddStartingWindow);
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800561 }
562
Jorim Jaggi42befc62017-06-13 11:54:04 -0700563 private boolean createSnapshot(TaskSnapshot snapshot) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800564 if (snapshot == null) {
565 return false;
566 }
567
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200568 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Creating SnapshotStartingData");
Jorim Jaggi30d64f32017-04-07 16:33:17 +0200569 mContainer.startingData = new SnapshotStartingData(mService, snapshot);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800570 scheduleAddStartingWindow();
571 return true;
572 }
573
Jorim Jaggi1dda7a62017-06-28 14:40:27 -0400574 private boolean snapshotOrientationSameAsTask(TaskSnapshot snapshot) {
Jorim Jaggi42befc62017-06-13 11:54:04 -0700575 if (snapshot == null) {
576 return false;
577 }
Jorim Jaggi1dda7a62017-06-28 14:40:27 -0400578 return mContainer.getTask().getConfiguration().orientation == snapshot.getOrientation();
Jorim Jaggi42befc62017-06-13 11:54:04 -0700579 }
580
Jorim Jaggi19be6052017-08-03 18:33:43 +0200581 public void removeStartingWindow() {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800582 synchronized (mWindowMap) {
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800583 if (mContainer.startingWindow == null) {
584 if (mContainer.startingData != null) {
585 // Starting window has not been added yet, but it is scheduled to be added.
586 // Go ahead and cancel the request.
587 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM,
588 "Clearing startingData for token=" + mContainer);
589 mContainer.startingData = null;
590 }
591 return;
592 }
593
Seigo Nonaka82871bd2017-07-17 20:22:44 -0700594 final StartingSurface surface;
595 if (mContainer.startingData != null) {
596 surface = mContainer.startingSurface;
597 mContainer.startingData = null;
598 mContainer.startingSurface = null;
599 mContainer.startingWindow = null;
600 mContainer.startingDisplayed = false;
Seigo Nonakaeafe7372017-08-16 12:39:49 -0700601 if (surface == null) {
602 if (DEBUG_STARTING_WINDOW) {
603 Slog.v(TAG_WM, "startingWindow was set but startingSurface==null, couldn't "
604 + "remove");
605 }
606 return;
Seigo Nonaka82871bd2017-07-17 20:22:44 -0700607 }
608 } else {
609 if (DEBUG_STARTING_WINDOW) {
610 Slog.v(TAG_WM, "Tried to remove starting window but startingWindow was null:"
611 + mContainer);
612 }
613 return;
614 }
615
Jorim Jaggie4b0f282017-05-17 15:10:29 +0200616 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Schedule remove starting " + mContainer
Seigo Nonaka82871bd2017-07-17 20:22:44 -0700617 + " startingWindow=" + mContainer.startingWindow
618 + " startingView=" + mContainer.startingSurface);
Jorim Jaggia3382192017-07-18 14:33:21 +0200619
620 // Use the same thread to remove the window as we used to add it, as otherwise we end up
621 // with things in the view hierarchy being called from different threads.
Jorim Jaggi8829cf12017-09-05 12:28:52 +0200622 mService.mAnimationHandler.post(() -> {
Seigo Nonaka82871bd2017-07-17 20:22:44 -0700623 if (DEBUG_STARTING_WINDOW) Slog.v(TAG_WM, "Removing startingView=" + surface);
624 try {
625 surface.remove();
626 } catch (Exception e) {
627 Slog.w(TAG_WM, "Exception when removing starting window", e);
628 }
629 });
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800630 }
631 }
632
633 public void pauseKeyDispatching() {
634 synchronized (mWindowMap) {
635 if (mContainer != null) {
636 mService.mInputMonitor.pauseDispatchingLw(mContainer);
637 }
638 }
639 }
640
641 public void resumeKeyDispatching() {
642 synchronized (mWindowMap) {
643 if (mContainer != null) {
644 mService.mInputMonitor.resumeDispatchingLw(mContainer);
645 }
646 }
647 }
648
Jorim Jaggibae01b12017-04-11 16:29:10 -0700649 public void notifyAppResumed(boolean wasStopped) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800650 synchronized(mWindowMap) {
651 if (mContainer == null) {
652 Slog.w(TAG_WM, "Attempted to notify resumed of non-existing app token: " + mToken);
653 return;
654 }
Jorim Jaggibae01b12017-04-11 16:29:10 -0700655 mContainer.notifyAppResumed(wasStopped);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800656 }
657 }
658
659 public void notifyAppStopped() {
660 synchronized(mWindowMap) {
661 if (mContainer == null) {
662 Slog.w(TAG_WM, "Attempted to notify stopped of non-existing app token: "
663 + mToken);
664 return;
665 }
666 mContainer.notifyAppStopped();
667 }
668 }
669
670 public void startFreezingScreen(int configChanges) {
671 synchronized(mWindowMap) {
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800672 if (mContainer == null) {
673 Slog.w(TAG_WM,
674 "Attempted to freeze screen with non-existing app token: " + mContainer);
675 return;
676 }
David Stevensf62360c2017-03-16 19:00:20 -0700677
678 if (configChanges == 0 && mContainer.okToDisplay()) {
679 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Skipping set freeze of " + mToken);
680 return;
681 }
682
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800683 mContainer.startFreezingScreen();
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800684 }
685 }
686
687 public void stopFreezingScreen(boolean force) {
688 synchronized(mWindowMap) {
689 if (mContainer == null) {
690 return;
691 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800692 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Clear freezing of " + mToken + ": hidden="
Jorim Jaggib0fc8172017-11-23 17:04:08 +0000693 + mContainer.isHidden() + " freezing=" + mContainer.isFreezingScreen());
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800694 mContainer.stopFreezingScreen(true, force);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800695 }
696 }
697
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100698 public void registerRemoteAnimations(RemoteAnimationDefinition definition) {
699 synchronized (mWindowMap) {
700 if (mContainer == null) {
701 Slog.w(TAG_WM, "Attempted to register remote animations with non-existing app"
702 + " token: " + mToken);
703 return;
704 }
705 mContainer.registerRemoteAnimations(definition);
706 }
707 }
708
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800709 void reportStartingWindowDrawn() {
Sudheer Shankac766db02017-06-12 10:37:29 -0700710 mHandler.sendMessage(mHandler.obtainMessage(H.NOTIFY_STARTING_WINDOW_DRAWN));
Jorim Jaggi3878ca32017-02-02 17:13:05 -0800711 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800712
713 void reportWindowsDrawn() {
Sudheer Shankac766db02017-06-12 10:37:29 -0700714 mHandler.sendMessage(mHandler.obtainMessage(H.NOTIFY_WINDOWS_DRAWN));
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800715 }
716
717 void reportWindowsVisible() {
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800718 mHandler.post(mOnWindowsVisible);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800719 }
720
721 void reportWindowsGone() {
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800722 mHandler.post(mOnWindowsGone);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800723 }
724
725 /** Calls directly into activity manager so window manager lock shouldn't held. */
Wale Ogunwale7402ddf2017-03-29 12:58:24 -0700726 boolean keyDispatchingTimedOut(String reason, int windowPid) {
727 return mListener != null && mListener.keyDispatchingTimedOut(reason, windowPid);
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800728 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800729
730 @Override
731 public String toString() {
Wale Ogunwale9c64cb62017-04-12 13:39:59 -0700732 return "AppWindowContainerController{"
733 + " token=" + mToken
734 + " mContainer=" + mContainer
735 + " mListener=" + mListener
736 + "}";
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800737 }
Wale Ogunwale26c0dfe2016-12-14 14:42:30 -0800738}