blob: 24a0d1aff9969f50997eb44f54313ee0c0651084 [file] [log] [blame]
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001/*
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 android.app.AppOpsManager;
20import android.content.res.Configuration;
21import android.graphics.Rect;
Ruchi Kandoi0d434042016-10-03 09:12:02 -070022import android.hardware.power.V1_0.PowerHint;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070023import android.os.Binder;
24import android.os.Debug;
Wale Ogunwale02319a62016-09-26 15:21:22 -070025import android.os.IBinder;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070026import android.os.ParcelFileDescriptor;
27import android.os.PowerManager;
28import android.os.RemoteException;
29import android.os.SystemClock;
30import android.os.UserHandle;
31import android.provider.Settings;
32import android.util.EventLog;
33import android.util.Slog;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070034import android.util.SparseIntArray;
35import android.view.Display;
36import android.view.DisplayInfo;
37import android.view.InputChannel;
38import android.view.WindowManager;
39import com.android.internal.util.ArrayUtils;
40import com.android.server.EventLogTags;
41import com.android.server.input.InputWindowHandle;
42
43import java.io.FileDescriptor;
44import java.io.PrintWriter;
45import java.util.ArrayList;
Wale Ogunwale02319a62016-09-26 15:21:22 -070046import java.util.HashMap;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -070047import java.util.LinkedList;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070048
49import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
Winson41275482016-10-10 15:17:45 -070050import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
Wale Ogunwale02319a62016-09-26 15:21:22 -070051import static android.view.Display.DEFAULT_DISPLAY;
Winson41275482016-10-10 15:17:45 -070052import static android.view.WindowManager.INPUT_CONSUMER_NAVIGATION;
53import static android.view.WindowManager.INPUT_CONSUMER_PIP;
54import static android.view.WindowManager.INPUT_CONSUMER_WALLPAPER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070055import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
56import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070057import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070058import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070059import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE;
60import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
61import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
62import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
63import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
64import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
65import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
66import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
67import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
68import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale02319a62016-09-26 15:21:22 -070069import static com.android.server.wm.AppTransition.TRANSIT_UNSET;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale02319a62016-09-26 15:21:22 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale02319a62016-09-26 15:21:22 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_KEEP_SCREEN_ON;
75import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
76import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
77import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
78import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_POWER;
79import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
81import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
82import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
83import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_TRACE;
84import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
85import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
86import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
87import static com.android.server.wm.WindowManagerDebugConfig.TAG_KEEP_SCREEN_ON;
88import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
89import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070090import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
91import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
92import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Wale Ogunwale02319a62016-09-26 15:21:22 -070093import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_NORMAL;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070094import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
95import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
96import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_NONE;
97import static com.android.server.wm.WindowManagerService.H.WINDOW_FREEZE_TIMEOUT;
98import static com.android.server.wm.WindowManagerService.logSurface;
99import static com.android.server.wm.WindowSurfacePlacer.SET_FORCE_HIDING_CHANGED;
100import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
101import static com.android.server.wm.WindowSurfacePlacer.SET_TURN_ON_SCREEN;
102import static com.android.server.wm.WindowSurfacePlacer.SET_UPDATE_ROTATION;
103import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_ACTION_PENDING;
104import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
105
106/** Root {@link WindowContainer} for the device. */
107// TODO: Several methods in here are accessing children of this container's children through various
108// references (WindowList I am looking at you :/). See if we can delegate instead.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700109class RootWindowContainer extends WindowContainer<DisplayContent> {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700110 private static final String TAG = TAG_WITH_CLASS_NAME ? "RootWindowContainer" : TAG_WM;
111
112 WindowManagerService mService;
113
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700114 private boolean mWallpaperForceHidingChanged = false;
115 private Object mLastWindowFreezeSource = null;
116 private Session mHoldScreen = null;
117 private float mScreenBrightness = -1;
118 private float mButtonBrightness = -1;
119 private long mUserActivityTimeout = -1;
120 private boolean mUpdateRotation = false;
121 private boolean mObscured = false;
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700122 private boolean mSyswin = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700123 // Set to true when the display contains content to show the user.
124 // When false, the display manager may choose to mirror or blank the display.
125 private boolean mDisplayHasContent = false;
126 private float mPreferredRefreshRate = 0;
127 private int mPreferredModeId = 0;
128 // Following variables are for debugging screen wakelock only.
129 // Last window that requires screen wakelock
130 WindowState mHoldScreenWindow = null;
131 // Last window that obscures all windows below
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700132 WindowState mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700133 // Only set while traversing the default display based on its content.
134 // Affects the behavior of mirroring on secondary displays.
135 private boolean mObscureApplicationContentOnSecondaryDisplays = false;
136
137 private boolean mSustainedPerformanceModeEnabled = false;
138 private boolean mSustainedPerformanceModeCurrent = false;
139
140 boolean mWallpaperMayChange = false;
Robert Carr11c26c22016-09-23 12:40:27 -0700141 // During an orientation change, we track whether all windows have rendered
142 // at the new orientation, and this will be false from changing orientation until that occurs.
143 // For seamless rotation cases this always stays true, as the windows complete their orientation
144 // changes 1 by 1 without disturbing global state.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700145 boolean mOrientationChangeComplete = true;
146 boolean mWallpaperActionPending = false;
147
148 private final ArrayList<Integer> mChangedStackList = new ArrayList();
149
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700150 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
151
Wale Ogunwale02319a62016-09-26 15:21:22 -0700152 private final ArrayList<WindowToken> mTmpTokensList = new ArrayList();
153
154 // Collection of binder tokens mapped to their window type we are allowed to create window
155 // tokens for but that are not current attached to any display. We need to track this here
156 // because a binder token can be added through {@link WindowManagerService#addWindowToken},
157 // but we don't know what display windows for the token will be added to until
158 // {@link WindowManagerService#addWindow} is called.
159 private final HashMap<IBinder, Integer> mUnattachedBinderTokens = new HashMap();
160
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700161 // State for the RemoteSurfaceTrace system used in testing. If this is enabled SurfaceControl
162 // instances will be replaced with an instance that writes a binary representation of all
163 // commands to mSurfaceTraceFd.
164 boolean mSurfaceTraceEnabled;
165 ParcelFileDescriptor mSurfaceTraceFd;
166 RemoteEventTrace mRemoteEventTrace;
167
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700168 private final WindowLayersController mLayersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700169 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700170
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700171 RootWindowContainer(WindowManagerService service) {
172 mService = service;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700173 mLayersController = new WindowLayersController(mService);
Wale Ogunwale0303c572016-10-20 10:16:29 -0700174 mWallpaperController = new WallpaperController(mService);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700175 }
176
177 WindowState computeFocusedWindow() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700178 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700179 for (int i = 0; i < count; i++) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700180 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700181 final WindowState win = dc.findFocusedWindow();
182 if (win != null) {
183 return win;
184 }
185 }
186 return null;
187 }
188
189 /**
190 * Retrieve the DisplayContent for the specified displayId. Will create a new DisplayContent if
191 * there is a Display for the displayId.
192 *
193 * @param displayId The display the caller is interested in.
194 * @return The DisplayContent associated with displayId or null if there is no Display for it.
195 */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700196 DisplayContent getDisplayContentOrCreate(int displayId) {
197 DisplayContent dc = getDisplayContent(displayId);
198
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700199 if (dc == null) {
200 final Display display = mService.mDisplayManager.getDisplay(displayId);
201 if (display != null) {
202 dc = createDisplayContent(display);
203 }
204 }
205 return dc;
206 }
207
Wale Ogunwale02319a62016-09-26 15:21:22 -0700208 DisplayContent getDisplayContent(int displayId) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700209 for (int i = mChildren.size() - 1; i >= 0; --i) {
210 final DisplayContent current = mChildren.get(i);
211 if (current.getDisplayId() == displayId) {
212 return current;
213 }
214 }
215 return null;
216 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700217
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700218 private DisplayContent createDisplayContent(final Display display) {
Wale Ogunwale0303c572016-10-20 10:16:29 -0700219 final DisplayContent dc = new DisplayContent(display, mService, mLayersController,
220 mWallpaperController);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700221 final int displayId = display.getDisplayId();
222
223 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Adding display=" + display);
224 addChild(dc, null);
225
226 final DisplayInfo displayInfo = dc.getDisplayInfo();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700227 final Rect rect = new Rect();
228 mService.mDisplaySettings.getOverscanLocked(displayInfo.name, displayInfo.uniqueId, rect);
229 displayInfo.overscanLeft = rect.left;
230 displayInfo.overscanTop = rect.top;
231 displayInfo.overscanRight = rect.right;
232 displayInfo.overscanBottom = rect.bottom;
233 if (mService.mDisplayManagerInternal != null) {
234 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(
235 displayId, displayInfo);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700236 mService.configureDisplayPolicyLocked(dc);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700237
238 // TODO(multi-display): Create an input channel for each display with touch capability.
239 if (displayId == Display.DEFAULT_DISPLAY) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700240 dc.mTapDetector = new TaskTapPointerEventListener(
241 mService, dc);
242 mService.registerPointerEventListener(dc.mTapDetector);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700243 mService.registerPointerEventListener(mService.mMousePositionTracker);
244 }
245 }
246
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700247 return dc;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700248 }
249
250 /** Adds the input stack id to the input display id and returns the bounds of the added stack.*/
251 Rect addStackToDisplay(int stackId, int displayId, boolean onTop) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700252 final DisplayContent dc = getDisplayContent(displayId);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700253 if (dc == null) {
254 Slog.w(TAG_WM, "addStackToDisplay: Trying to add stackId=" + stackId
255 + " to unknown displayId=" + displayId + " callers=" + Debug.getCallers(6));
256 return null;
257 }
258
259 boolean attachedToDisplay = false;
260 TaskStack stack = mService.mStackIdToStack.get(stackId);
261 if (stack == null) {
262 if (DEBUG_STACK) Slog.d(TAG_WM, "attachStack: stackId=" + stackId);
263
264 stack = dc.getStackById(stackId);
265 if (stack != null) {
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700266 // It's already attached to the display...clear mDeferRemoval and move stack to
267 // appropriate z-order on display as needed.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700268 stack.mDeferRemoval = false;
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700269 dc.moveStack(stack, onTop);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700270 attachedToDisplay = true;
271 } else {
272 stack = new TaskStack(mService, stackId);
273 }
274
275 mService.mStackIdToStack.put(stackId, stack);
276 if (stackId == DOCKED_STACK_ID) {
277 mService.getDefaultDisplayContentLocked().mDividerControllerLocked
278 .notifyDockedStackExistsChanged(true);
279 }
280 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700281
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700282 if (!attachedToDisplay) {
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700283 dc.attachStack(stack, onTop);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700284 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700285
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700286 if (stack.getRawFullscreen()) {
287 return null;
288 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -0700289 final Rect bounds = new Rect();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700290 stack.getRawBounds(bounds);
291 return bounds;
292 }
293
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700294 boolean isLayoutNeeded() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700295 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700296 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700297 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700298 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700299 return true;
300 }
301 }
302 return false;
303 }
304
305 void getWindows(WindowList output) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700306 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700307 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700308 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700309 output.addAll(dc.getWindowList());
310 }
311 }
312
313 void getWindows(WindowList output, boolean visibleOnly, boolean appsOnly) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700314 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700315 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700316 final WindowList windowList = mChildren.get(displayNdx).getWindowList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700317 for (int winNdx = windowList.size() - 1; winNdx >= 0; --winNdx) {
318 final WindowState w = windowList.get(winNdx);
319 if ((!visibleOnly || w.mWinAnimator.getShown())
320 && (!appsOnly || w.mAppToken != null)) {
321 output.add(w);
322 }
323 }
324 }
325 }
326
327 void getWindowsByName(WindowList output, String name) {
328 int objectId = 0;
329 // See if this is an object ID.
330 try {
331 objectId = Integer.parseInt(name, 16);
332 name = null;
333 } catch (RuntimeException e) {
334 }
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700335 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700336 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700337 final WindowList windowList = mChildren.get(displayNdx).getWindowList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700338 for (int winNdx = windowList.size() - 1; winNdx >= 0; --winNdx) {
339 final WindowState w = windowList.get(winNdx);
340 if (name != null) {
341 if (w.mAttrs.getTitle().toString().contains(name)) {
342 output.add(w);
343 }
344 } else if (System.identityHashCode(w) == objectId) {
345 output.add(w);
346 }
347 }
348 }
349 }
350
351 WindowState findWindow(int hashCode) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700352 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700353 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700354 final WindowList windows = mChildren.get(displayNdx).getWindowList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700355 final int numWindows = windows.size();
356 for (int winNdx = 0; winNdx < numWindows; ++winNdx) {
357 final WindowState w = windows.get(winNdx);
358 if (System.identityHashCode(w) == hashCode) {
359 return w;
360 }
361 }
362 }
363
364 return null;
365 }
366
Wale Ogunwale02319a62016-09-26 15:21:22 -0700367 /** Return the window token associated with the input binder token on the input display */
368 WindowToken getWindowToken(IBinder binder, DisplayContent dc) {
369 final WindowToken token = dc.getWindowToken(binder);
370 if (token != null) {
371 return token;
372 }
373
374 // There is no window token mapped to the binder on the display. Create and map a window
375 // token if it is currently allowed.
376 if (!mUnattachedBinderTokens.containsKey(binder)) {
377 return null;
378 }
379
380 final int type = mUnattachedBinderTokens.get(binder);
381 return new WindowToken(mService, binder, type, true, dc);
382 }
383
384 /** Returns all window tokens mapped to the input binder. */
385 ArrayList<WindowToken> getWindowTokens(IBinder binder) {
386 mTmpTokensList.clear();
387 for (int i = mChildren.size() - 1; i >= 0; --i) {
388 final DisplayContent dc = mChildren.get(i);
389 final WindowToken token = dc.getWindowToken(binder);
390 if (token != null) {
391 mTmpTokensList.add(token);
392 }
393 }
394 return mTmpTokensList;
395 }
396
397 /**
398 * Returns the app window token for the input binder if it exist in the system.
399 * NOTE: Only one AppWindowToken is allowed to exist in the system for a binder token, since
400 * AppWindowToken represents an activity which can only exist on one display.
401 */
402 AppWindowToken getAppWindowToken(IBinder binder) {
403 for (int i = mChildren.size() - 1; i >= 0; --i) {
404 final DisplayContent dc = mChildren.get(i);
405 final AppWindowToken atoken = dc.getAppWindowToken(binder);
406 if (atoken != null) {
407 return atoken;
408 }
409 }
410 return null;
411 }
412
413 /** Returns the display object the input window token is currently mapped on. */
414 DisplayContent getWindowTokenDisplay(WindowToken token) {
415 if (token == null) {
416 return null;
417 }
418
419 for (int i = mChildren.size() - 1; i >= 0; --i) {
420 final DisplayContent dc = mChildren.get(i);
421 final WindowToken current = dc.getWindowToken(token.token);
422 if (current == token) {
423 return dc;
424 }
425 }
426
427 return null;
428 }
429
430 void addWindowToken(IBinder binder, int type) {
431 if (mUnattachedBinderTokens.containsKey(binder)) {
432 Slog.w(TAG_WM, "addWindowToken: Attempted to add existing binder token: " + binder);
433 return;
434 }
435
436 final ArrayList<WindowToken> tokens = getWindowTokens(binder);
437
438 if (!tokens.isEmpty()) {
439 Slog.w(TAG_WM, "addWindowToken: Attempted to add binder token: " + binder
440 + " for already created window tokens: " + tokens);
441 return;
442 }
443
444 mUnattachedBinderTokens.put(binder, type);
445
446 // TODO(multi-display): By default we add this to the default display, but maybe we
447 // should provide an API for a token to be added to any display?
Wale Ogunwale0303c572016-10-20 10:16:29 -0700448 final DisplayContent dc = getDisplayContent(DEFAULT_DISPLAY);
449 final WindowToken token = new WindowToken(mService, binder, type, true, dc);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700450 if (type == TYPE_WALLPAPER) {
Wale Ogunwale0303c572016-10-20 10:16:29 -0700451 dc.mWallpaperController.addWallpaperToken(token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700452 }
453 }
454
455 ArrayList<WindowToken> removeWindowToken(IBinder binder) {
456 mUnattachedBinderTokens.remove(binder);
457
458 mTmpTokensList.clear();
459 for (int i = mChildren.size() - 1; i >= 0; --i) {
460 final DisplayContent dc = mChildren.get(i);
461 final WindowToken token = dc.removeWindowToken(binder);
462 if (token != null) {
463 mTmpTokensList.add(token);
464 }
465 }
466 return mTmpTokensList;
467 }
468
469 /**
470 * Removed the mapping to the input binder for the system if it no longer as a window token
471 * associated with it on any display.
472 */
473 void removeWindowTokenIfPossible(IBinder binder) {
474 for (int i = mChildren.size() - 1; i >= 0; --i) {
475 final DisplayContent dc = mChildren.get(i);
476 final WindowToken token = dc.getWindowToken(binder);
477 if (token != null) {
478 return;
479 }
480 }
481
482 mUnattachedBinderTokens.remove(binder);
483 }
484
485 void removeAppToken(IBinder binder) {
486 final ArrayList<WindowToken> removedTokens = removeWindowToken(binder);
487 if (removedTokens == null || removedTokens.isEmpty()) {
488 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
489 return;
490 }
491
492 for (int i = removedTokens.size() - 1; i >= 0; --i) {
493 WindowToken wtoken = removedTokens.get(i);
494 AppWindowToken appToken = wtoken.asAppWindowToken();
495
496 if (appToken == null) {
497 Slog.w(TAG_WM,
498 "Attempted to remove non-App token: " + binder + " wtoken=" + wtoken);
499 continue;
500 }
501
502 AppWindowToken startingToken = null;
503
504 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app token: " + appToken);
505
506 boolean delayed = appToken.setVisibility(null, false, TRANSIT_UNSET, true,
507 appToken.voiceInteraction);
508
509 mService.mOpeningApps.remove(appToken);
510 appToken.waitingToShow = false;
511 if (mService.mClosingApps.contains(appToken)) {
512 delayed = true;
513 } else if (mService.mAppTransition.isTransitionSet()) {
514 mService.mClosingApps.add(appToken);
515 delayed = true;
516 }
517
518 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG_WM, "Removing app " + appToken
519 + " delayed=" + delayed
520 + " animation=" + appToken.mAppAnimator.animation
521 + " animating=" + appToken.mAppAnimator.animating);
522
523 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM, "removeAppToken: "
524 + appToken + " delayed=" + delayed + " Callers=" + Debug.getCallers(4));
525
526 final TaskStack stack = appToken.mTask.mStack;
527 if (delayed && !appToken.isEmpty()) {
528 // set the token aside because it has an active animation to be finished
529 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG_WM,
530 "removeAppToken make exiting: " + appToken);
531 stack.mExitingAppTokens.add(appToken);
532 appToken.mIsExiting = true;
533 } else {
534 // Make sure there is no animation running on this token, so any windows associated
535 // with it will be removed as soon as their animations are complete
536 appToken.mAppAnimator.clearAnimation();
537 appToken.mAppAnimator.animating = false;
538 appToken.removeIfPossible();
539 }
540
541 appToken.removed = true;
542 if (appToken.startingData != null) {
543 startingToken = appToken;
544 }
545 appToken.stopFreezingScreen(true, true);
546 if (mService.mFocusedApp == appToken) {
547 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "Removing focused app token:" + appToken);
548 mService.mFocusedApp = null;
549 mService.updateFocusedWindowLocked(
550 UPDATE_FOCUS_NORMAL, true /*updateInputWindows*/);
551 mService.mInputMonitor.setFocusedAppLw(null);
552 }
553
554 if (!delayed) {
555 appToken.updateReportedVisibilityLocked();
556 }
557
558 // Will only remove if startingToken non null.
559 mService.scheduleRemoveStartingWindowLocked(startingToken);
560 }
561 }
562
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700563 // TODO: Users would have their own window containers under the display container?
564 void switchUser() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700565 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700566 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700567 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700568 dc.switchUser();
569 }
570 }
571
Andrii Kulian441e4492016-09-29 15:25:00 -0700572 /** Set new config and return array of ids of stacks that were changed during update. */
573 int[] setGlobalConfigurationIfNeeded(Configuration newConfiguration) {
574 final boolean configChanged = getConfiguration().diff(newConfiguration) != 0;
575 if (!configChanged) {
576 return null;
577 }
578 onConfigurationChanged(newConfiguration);
579 return updateStackBoundsAfterConfigChange();
580 }
581
582 @Override
583 void onConfigurationChanged(Configuration newParentConfig) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700584 prepareFreezingTaskBounds();
Andrii Kulian441e4492016-09-29 15:25:00 -0700585 super.onConfigurationChanged(newParentConfig);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700586
587 mService.mPolicy.onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700588 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700589
Andrii Kulian441e4492016-09-29 15:25:00 -0700590 /**
591 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
592 * bounds were updated.
593 */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700594 private int[] updateStackBoundsAfterConfigChange() {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700595 mChangedStackList.clear();
596
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700597 final int numDisplays = mChildren.size();
598 for (int i = 0; i < numDisplays; ++i) {
599 final DisplayContent dc = mChildren.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700600 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700601 }
602
603 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
604 }
605
606 private void prepareFreezingTaskBounds() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700607 for (int i = mChildren.size() - 1; i >= 0; i--) {
608 mChildren.get(i).prepareFreezingTaskBounds();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700609 }
610 }
611
612 void setSecureSurfaceState(int userId, boolean disabled) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700613 for (int i = mChildren.size() - 1; i >= 0; --i) {
614 final WindowList windows = mChildren.get(i).getWindowList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700615 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
616 final WindowState win = windows.get(winNdx);
617 if (win.mHasSurface && userId == UserHandle.getUserId(win.mOwnerUid)) {
618 win.mWinAnimator.setSecureLocked(disabled);
619 }
620 }
621 }
622 }
623
624 void updateAppOpsState() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700625 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700626 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700627 final WindowList windows = mChildren.get(i).getWindowList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700628 final int numWindows = windows.size();
629 for (int winNdx = 0; winNdx < numWindows; ++winNdx) {
630 final WindowState win = windows.get(winNdx);
631 if (win.mAppOp == AppOpsManager.OP_NONE) {
632 continue;
633 }
634 final int mode = mService.mAppOps.checkOpNoThrow(win.mAppOp, win.getOwningUid(),
635 win.getOwningPackage());
636 win.setAppOpVisibilityLw(mode == AppOpsManager.MODE_ALLOWED ||
637 mode == AppOpsManager.MODE_DEFAULT);
638 }
639 }
640 }
641
642 boolean canShowStrictModeViolation(int pid) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700643 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700644 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700645 final WindowList windows = mChildren.get(i).getWindowList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700646 final int numWindows = windows.size();
647 for (int winNdx = 0; winNdx < numWindows; ++winNdx) {
648 final WindowState ws = windows.get(winNdx);
649 if (ws.mSession.mPid == pid && ws.isVisibleLw()) {
650 return true;
651 }
652 }
653 }
654 return false;
655 }
656
657 void closeSystemDialogs(String reason) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700658 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700659 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700660 final WindowList windows = mChildren.get(i).getWindowList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700661 final int numWindows = windows.size();
662 for (int j = 0; j < numWindows; ++j) {
663 final WindowState w = windows.get(j);
664 if (w.mHasSurface) {
665 try {
666 w.mClient.closeSystemDialogs(reason);
667 } catch (RemoteException e) {
668 }
669 }
670 }
671 }
672 }
673
674 void removeReplacedWindows() {
675 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION removeReplacedWindows");
676 mService.openSurfaceTransaction();
677 try {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700678 for (int i = mChildren.size() - 1; i >= 0; i--) {
679 DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700680 final WindowList windows = dc.getWindowList();
681 for (int j = windows.size() - 1; j >= 0; j--) {
682 final WindowState win = windows.get(j);
683 final AppWindowToken aToken = win.mAppToken;
684 if (aToken != null) {
685 aToken.removeReplacedWindowIfNeeded(win);
686 }
687 }
688 }
689 } finally {
690 mService.closeSurfaceTransaction();
691 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION removeReplacedWindows");
692 }
693 }
694
695 boolean hasPendingLayoutChanges(WindowAnimator animator) {
696 boolean hasChanges = false;
697
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700698 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700699 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700700 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700701 final int pendingChanges = animator.getPendingLayoutChanges(dc.getDisplayId());
702 if ((pendingChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
703 animator.mBulkUpdateParams |= SET_WALLPAPER_ACTION_PENDING;
704 }
705 if (pendingChanges != 0) {
706 hasChanges = true;
707 }
708 }
709
710 return hasChanges;
711 }
712
713 void updateInputWindows(InputMonitor inputMonitor, WindowState inputFocus, boolean inDrag) {
Winson41275482016-10-10 15:17:45 -0700714 final InputConsumerImpl navInputConsumer =
715 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_NAVIGATION);
716 final InputConsumerImpl pipInputConsumer =
717 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_PIP);
718 final InputConsumerImpl wallpaperInputConsumer =
719 mService.mInputMonitor.getInputConsumer(INPUT_CONSUMER_WALLPAPER);
720 boolean addInputConsumerHandle = navInputConsumer != null;
721 boolean addPipInputConsumerHandle = pipInputConsumer != null;
722 boolean addWallpaperInputConsumerHandle = wallpaperInputConsumer != null;
723 final Rect pipTouchableBounds = addPipInputConsumerHandle ? new Rect() : null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700724 boolean disableWallpaperTouchEvents = false;
725
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700726 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700727 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700728 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700729 final WindowList windows = dc.getWindowList();
730 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
731 final WindowState child = windows.get(winNdx);
732 final InputChannel inputChannel = child.mInputChannel;
733 final InputWindowHandle inputWindowHandle = child.mInputWindowHandle;
734 if (inputChannel == null || inputWindowHandle == null || child.mRemoved
735 || child.isAdjustedForMinimizedDock()) {
736 // Skip this window because it cannot possibly receive input.
737 continue;
738 }
Winson41275482016-10-10 15:17:45 -0700739
740 if (addPipInputConsumerHandle
741 && child.getStackId() == PINNED_STACK_ID
742 && inputWindowHandle.layer <= pipInputConsumer.mWindowHandle.layer) {
743 // Update the bounds of the Pip input consumer to match the Pinned stack
744 child.getStack().getBounds(pipTouchableBounds);
745 pipInputConsumer.mWindowHandle.touchableRegion.set(pipTouchableBounds);
746 inputMonitor.addInputWindowHandle(pipInputConsumer.mWindowHandle);
747 addPipInputConsumerHandle = false;
748 }
749
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700750 if (addInputConsumerHandle
Winson41275482016-10-10 15:17:45 -0700751 && inputWindowHandle.layer <= navInputConsumer.mWindowHandle.layer) {
752 inputMonitor.addInputWindowHandle(navInputConsumer.mWindowHandle);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700753 addInputConsumerHandle = false;
754 }
755
756 if (addWallpaperInputConsumerHandle) {
757 if (child.mAttrs.type == WindowManager.LayoutParams.TYPE_WALLPAPER &&
758 child.isVisibleLw()) {
759 // Add the wallpaper input consumer above the first visible wallpaper.
Winson41275482016-10-10 15:17:45 -0700760 inputMonitor.addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700761 addWallpaperInputConsumerHandle = false;
762 }
763 }
764
765 final int flags = child.mAttrs.flags;
766 final int privateFlags = child.mAttrs.privateFlags;
767 final int type = child.mAttrs.type;
768
769 final boolean hasFocus = child == inputFocus;
770 final boolean isVisible = child.isVisibleLw();
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700771 if ((privateFlags & PRIVATE_FLAG_DISABLE_WALLPAPER_TOUCH_EVENTS) != 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700772 disableWallpaperTouchEvents = true;
773 }
Wale Ogunwale0303c572016-10-20 10:16:29 -0700774 final boolean hasWallpaper = dc.mWallpaperController.isWallpaperTarget(child)
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700775 && (privateFlags & PRIVATE_FLAG_KEYGUARD) == 0
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700776 && !disableWallpaperTouchEvents;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700777
778 // If there's a drag in progress and 'child' is a potential drop target,
779 // make sure it's been told about the drag
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700780 if (inDrag && isVisible && dc.isDefaultDisplay) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700781 mService.mDragState.sendDragStartedIfNeededLw(child);
782 }
783
784 inputMonitor.addInputWindowHandle(
785 inputWindowHandle, child, flags, type, isVisible, hasFocus, hasWallpaper);
786 }
787 }
788
789 if (addWallpaperInputConsumerHandle) {
790 // No visible wallpaper found, add the wallpaper input consumer at the end.
Winson41275482016-10-10 15:17:45 -0700791 inputMonitor.addInputWindowHandle(wallpaperInputConsumer.mWindowHandle);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700792 }
793 }
794
795 boolean reclaimSomeSurfaceMemory(WindowStateAnimator winAnimator, String operation,
796 boolean secure) {
797 final WindowSurfaceController surfaceController = winAnimator.mSurfaceController;
798 boolean leakedSurface = false;
799 boolean killedApps = false;
800
801 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, winAnimator.mWin.toString(),
802 winAnimator.mSession.mPid, operation);
803
804 final long callingIdentity = Binder.clearCallingIdentity();
805 try {
806 // There was some problem... first, do a sanity check of the window list to make sure
807 // we haven't left any dangling surfaces around.
808
809 Slog.i(TAG_WM, "Out of memory for surface! Looking for leaks...");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700810 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700811 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700812 final WindowList windows = mChildren.get(displayNdx).getWindowList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700813 final int numWindows = windows.size();
814 for (int winNdx = 0; winNdx < numWindows; ++winNdx) {
815 final WindowState ws = windows.get(winNdx);
816 final WindowStateAnimator wsa = ws.mWinAnimator;
817 if (wsa.mSurfaceController == null) {
818 continue;
819 }
820 if (!mService.mSessions.contains(wsa.mSession)) {
821 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
822 + ws + " surface=" + wsa.mSurfaceController
823 + " token=" + ws.mToken
824 + " pid=" + ws.mSession.mPid
825 + " uid=" + ws.mSession.mUid);
826 wsa.destroySurface();
827 mService.mForceRemoves.add(ws);
828 leakedSurface = true;
829 } else if (ws.mAppToken != null && ws.mAppToken.clientHidden) {
830 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
831 + ws + " surface=" + wsa.mSurfaceController
832 + " token=" + ws.mAppToken
833 + " saved=" + ws.hasSavedSurface());
834 if (SHOW_TRANSACTIONS) logSurface(ws, "LEAK DESTROY", false);
835 wsa.destroySurface();
836 leakedSurface = true;
837 }
838 }
839 }
840
841 if (!leakedSurface) {
842 Slog.w(TAG_WM, "No leaked surfaces; killing applications!");
843 SparseIntArray pidCandidates = new SparseIntArray();
844 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700845 final WindowList windows = mChildren.get(displayNdx).getWindowList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700846 final int numWindows = windows.size();
847 for (int winNdx = 0; winNdx < numWindows; ++winNdx) {
848 final WindowState ws = windows.get(winNdx);
849 if (mService.mForceRemoves.contains(ws)) {
850 continue;
851 }
852 WindowStateAnimator wsa = ws.mWinAnimator;
853 if (wsa.mSurfaceController != null) {
854 pidCandidates.append(wsa.mSession.mPid, wsa.mSession.mPid);
855 }
856 }
857 if (pidCandidates.size() > 0) {
858 int[] pids = new int[pidCandidates.size()];
859 for (int i = 0; i < pids.length; i++) {
860 pids[i] = pidCandidates.keyAt(i);
861 }
862 try {
863 if (mService.mActivityManager.killPids(pids, "Free memory", secure)) {
864 killedApps = true;
865 }
866 } catch (RemoteException e) {
867 }
868 }
869 }
870 }
871
872 if (leakedSurface || killedApps) {
873 // We managed to reclaim some memory, so get rid of the trouble surface and ask the
874 // app to request another one.
875 Slog.w(TAG_WM,
876 "Looks like we have reclaimed some memory, clearing surface for retry.");
877 if (surfaceController != null) {
878 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) logSurface(winAnimator.mWin,
879 "RECOVER DESTROY", false);
880 winAnimator.destroySurface();
881 mService.scheduleRemoveStartingWindowLocked(winAnimator.mWin.mAppToken);
882 }
883
884 try {
885 winAnimator.mWin.mClient.dispatchGetNewSurface();
886 } catch (RemoteException e) {
887 }
888 }
889 } finally {
890 Binder.restoreCallingIdentity(callingIdentity);
891 }
892
893 return leakedSurface || killedApps;
894 }
895
896 // "Something has changed! Let's make it correct now."
897 // TODO: Super crazy long method that should be broken down...
898 void performSurfacePlacement(boolean recoveringMemory) {
899 if (DEBUG_WINDOW_TRACE) Slog.v(TAG, "performSurfacePlacementInner: entry. Called by "
900 + Debug.getCallers(3));
901
902 int i;
903 boolean updateInputWindowsNeeded = false;
904
905 if (mService.mFocusMayChange) {
906 mService.mFocusMayChange = false;
907 updateInputWindowsNeeded = mService.updateFocusedWindowLocked(
908 UPDATE_FOCUS_WILL_PLACE_SURFACES, false /*updateInputWindows*/);
909 }
910
911 // Initialize state of exiting tokens.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700912 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700913 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700914 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700915 for (i = displayContent.mExitingTokens.size() - 1; i >= 0; i--) {
916 displayContent.mExitingTokens.get(i).hasVisible = false;
917 }
918 }
919
920 for (int stackNdx = mService.mStackIdToStack.size() - 1; stackNdx >= 0; --stackNdx) {
921 // Initialize state of exiting applications.
922 final AppTokenList exitingAppTokens =
923 mService.mStackIdToStack.valueAt(stackNdx).mExitingAppTokens;
924 for (int tokenNdx = exitingAppTokens.size() - 1; tokenNdx >= 0; --tokenNdx) {
925 exitingAppTokens.get(tokenNdx).hasVisible = false;
926 }
927 }
928
929 mHoldScreen = null;
930 mScreenBrightness = -1;
931 mButtonBrightness = -1;
932 mUserActivityTimeout = -1;
933 mObscureApplicationContentOnSecondaryDisplays = false;
934 mSustainedPerformanceModeCurrent = false;
935 mService.mTransactionSequence++;
936
937 // TODO(multi-display):
938 final DisplayContent defaultDisplay = mService.getDefaultDisplayContentLocked();
939 final DisplayInfo defaultInfo = defaultDisplay.getDisplayInfo();
940 final int defaultDw = defaultInfo.logicalWidth;
941 final int defaultDh = defaultInfo.logicalHeight;
942
943 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
944 ">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
945 mService.openSurfaceTransaction();
946 try {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700947 applySurfaceChangesTransaction(recoveringMemory, defaultDw, defaultDh);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700948 } catch (RuntimeException e) {
949 Slog.wtf(TAG, "Unhandled exception in Window Manager", e);
950 } finally {
951 mService.closeSurfaceTransaction();
952 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
953 "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
954 }
955
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700956 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
957
958 // If we are ready to perform an app transition, check through all of the app tokens to be
959 // shown and see if they are ready to go.
960 if (mService.mAppTransition.isReady()) {
961 defaultDisplay.pendingLayoutChanges |=
Wale Ogunwale0303c572016-10-20 10:16:29 -0700962 surfacePlacer.handleAppTransitionReadyLocked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700963 if (DEBUG_LAYOUT_REPEATS)
964 surfacePlacer.debugLayoutRepeats("after handleAppTransitionReadyLocked",
965 defaultDisplay.pendingLayoutChanges);
966 }
967
968 if (!mService.mAnimator.mAppWindowAnimating && mService.mAppTransition.isRunning()) {
969 // We have finished the animation of an app transition. To do this, we have delayed a
970 // lot of operations like showing and hiding apps, moving apps in Z-order, etc. The app
971 // token list reflects the correct Z-order, but the window list may now be out of sync
972 // with it. So here we will just rebuild the entire app window list. Fun!
973 defaultDisplay.pendingLayoutChanges |=
974 mService.handleAnimatingStoppedAndTransitionLocked();
975 if (DEBUG_LAYOUT_REPEATS)
976 surfacePlacer.debugLayoutRepeats("after handleAnimStopAndXitionLock",
977 defaultDisplay.pendingLayoutChanges);
978 }
979
980 if (mWallpaperForceHidingChanged && defaultDisplay.pendingLayoutChanges == 0
981 && !mService.mAppTransition.isReady()) {
982 // At this point, there was a window with a wallpaper that was force hiding other
983 // windows behind it, but now it is going away. This may be simple -- just animate away
984 // the wallpaper and its window -- or it may be hard -- the wallpaper now needs to be
985 // shown behind something that was hidden.
986 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
987 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
988 "after animateAwayWallpaperLocked", defaultDisplay.pendingLayoutChanges);
989 }
990 mWallpaperForceHidingChanged = false;
991
992 if (mWallpaperMayChange) {
993 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Wallpaper may change! Adjusting");
994 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
995 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("WallpaperMayChange",
996 defaultDisplay.pendingLayoutChanges);
997 }
998
999 if (mService.mFocusMayChange) {
1000 mService.mFocusMayChange = false;
1001 if (mService.updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
1002 false /*updateInputWindows*/)) {
1003 updateInputWindowsNeeded = true;
1004 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
1005 }
1006 }
1007
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001008 if (isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001009 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
1010 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("mLayoutNeeded",
1011 defaultDisplay.pendingLayoutChanges);
1012 }
1013
1014 for (i = mService.mResizingWindows.size() - 1; i >= 0; i--) {
1015 WindowState win = mService.mResizingWindows.get(i);
1016 if (win.mAppFreezing) {
1017 // Don't remove this window until rotation has completed.
1018 continue;
1019 }
1020 // Discard the saved surface if window size is changed, it can't be reused.
1021 if (win.mAppToken != null) {
1022 win.mAppToken.destroySavedSurfaces();
1023 }
1024 win.reportResized();
1025 mService.mResizingWindows.remove(i);
1026 }
1027
1028 if (DEBUG_ORIENTATION && mService.mDisplayFrozen) Slog.v(TAG,
1029 "With display frozen, orientationChangeComplete=" + mOrientationChangeComplete);
1030 if (mOrientationChangeComplete) {
1031 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
1032 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
1033 mService.mLastFinishedFreezeSource = mLastWindowFreezeSource;
1034 mService.mH.removeMessages(WINDOW_FREEZE_TIMEOUT);
1035 }
1036 mService.stopFreezingDisplayLocked();
1037 }
1038
1039 // Destroy the surface of any windows that are no longer visible.
1040 boolean wallpaperDestroyed = false;
1041 i = mService.mDestroySurface.size();
1042 if (i > 0) {
1043 do {
1044 i--;
1045 WindowState win = mService.mDestroySurface.get(i);
1046 win.mDestroying = false;
1047 if (mService.mInputMethodWindow == win) {
1048 mService.mInputMethodWindow = null;
1049 }
Wale Ogunwale0303c572016-10-20 10:16:29 -07001050 if (win.getDisplayContent().mWallpaperController.isWallpaperTarget(win)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001051 wallpaperDestroyed = true;
1052 }
1053 win.destroyOrSaveSurface();
1054 } while (i > 0);
1055 mService.mDestroySurface.clear();
1056 }
1057
1058 // Time to remove any exiting tokens?
1059 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001060 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001061 ArrayList<WindowToken> exitingTokens = displayContent.mExitingTokens;
1062 for (i = exitingTokens.size() - 1; i >= 0; i--) {
1063 WindowToken token = exitingTokens.get(i);
1064 if (!token.hasVisible) {
1065 exitingTokens.remove(i);
1066 if (token.windowType == TYPE_WALLPAPER) {
Wale Ogunwale0303c572016-10-20 10:16:29 -07001067 displayContent.mWallpaperController.removeWallpaperToken(token);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001068 }
1069 }
1070 }
1071 }
1072
1073 // Time to remove any exiting applications?
1074 for (int stackNdx = mService.mStackIdToStack.size() - 1; stackNdx >= 0; --stackNdx) {
1075 // Initialize state of exiting applications.
1076 final AppTokenList exitingAppTokens =
1077 mService.mStackIdToStack.valueAt(stackNdx).mExitingAppTokens;
1078 for (i = exitingAppTokens.size() - 1; i >= 0; i--) {
1079 final AppWindowToken token = exitingAppTokens.get(i);
1080 if (!token.hasVisible && !mService.mClosingApps.contains(token) &&
1081 (!token.mIsExiting || token.isEmpty())) {
1082 // Make sure there is no animation running on this token, so any windows
1083 // associated with it will be removed as soon as their animations are complete
1084 token.mAppAnimator.clearAnimation();
1085 token.mAppAnimator.animating = false;
1086 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
1087 "performLayout: App token exiting now removed" + token);
1088 token.removeIfPossible();
1089 }
1090 }
1091 }
1092
1093 if (wallpaperDestroyed) {
1094 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001095 defaultDisplay.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001096 }
1097
1098 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001099 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001100 if (displayContent.pendingLayoutChanges != 0) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001101 displayContent.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001102 }
1103 }
1104
1105 // Finally update all input windows now that the window changes have stabilized.
1106 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
1107
1108 mService.setHoldScreenLocked(mHoldScreen);
1109 if (!mService.mDisplayFrozen) {
1110 if (mScreenBrightness < 0 || mScreenBrightness > 1.0f) {
1111 mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(-1);
1112 } else {
1113 mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(
1114 toBrightnessOverride(mScreenBrightness));
1115 }
1116 if (mButtonBrightness < 0 || mButtonBrightness > 1.0f) {
1117 mService.mPowerManagerInternal.setButtonBrightnessOverrideFromWindowManager(-1);
1118 } else {
1119 mService.mPowerManagerInternal.setButtonBrightnessOverrideFromWindowManager(
1120 toBrightnessOverride(mButtonBrightness));
1121 }
1122 mService.mPowerManagerInternal.setUserActivityTimeoutOverrideFromWindowManager(
1123 mUserActivityTimeout);
1124 }
1125
1126 if (mSustainedPerformanceModeCurrent != mSustainedPerformanceModeEnabled) {
1127 mSustainedPerformanceModeEnabled = mSustainedPerformanceModeCurrent;
1128 mService.mPowerManagerInternal.powerHint(
Ruchi Kandoi0d434042016-10-03 09:12:02 -07001129 PowerHint.SUSTAINED_PERFORMANCE,
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001130 (mSustainedPerformanceModeEnabled ? 1 : 0));
1131 }
1132
1133 if (mService.mTurnOnScreen) {
1134 if (mService.mAllowTheaterModeWakeFromLayout
1135 || Settings.Global.getInt(mService.mContext.getContentResolver(),
1136 Settings.Global.THEATER_MODE_ON, 0) == 0) {
1137 if (DEBUG_VISIBILITY || DEBUG_POWER) {
1138 Slog.v(TAG, "Turning screen on after layout!");
1139 }
1140 mService.mPowerManager.wakeUp(SystemClock.uptimeMillis(),
1141 "android.server.wm:TURN_ON");
1142 }
1143 mService.mTurnOnScreen = false;
1144 }
1145
1146 if (mUpdateRotation) {
1147 if (DEBUG_ORIENTATION) Slog.d(TAG, "Performing post-rotate rotation");
1148 if (mService.updateRotationUncheckedLocked(false)) {
1149 mService.mH.sendEmptyMessage(SEND_NEW_CONFIGURATION);
1150 } else {
1151 mUpdateRotation = false;
1152 }
1153 }
1154
1155 if (mService.mWaitingForDrawnCallback != null ||
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001156 (mOrientationChangeComplete && !defaultDisplay.isLayoutNeeded()
1157 && !mUpdateRotation)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001158 mService.checkDrawnWindowsLocked();
1159 }
1160
1161 final int N = mService.mPendingRemove.size();
1162 if (N > 0) {
1163 if (mService.mPendingRemoveTmp.length < N) {
1164 mService.mPendingRemoveTmp = new WindowState[N+10];
1165 }
1166 mService.mPendingRemove.toArray(mService.mPendingRemoveTmp);
1167 mService.mPendingRemove.clear();
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001168 ArrayList<DisplayContent> displayList = new ArrayList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001169 for (i = 0; i < N; i++) {
1170 final WindowState w = mService.mPendingRemoveTmp[i];
1171 w.removeImmediately();
1172 final DisplayContent displayContent = w.getDisplayContent();
1173 if (displayContent != null && !displayList.contains(displayContent)) {
1174 displayList.add(displayContent);
1175 }
1176 }
1177
Wale Ogunwalec69694a2016-10-18 13:51:15 -07001178 for (int j = displayList.size() - 1; j >= 0; --j) {
1179 final DisplayContent dc = displayList.get(j);
1180 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001181 }
1182 }
1183
1184 // Remove all deferred displays stacks, tasks, and activities.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001185 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1186 mChildren.get(displayNdx).checkCompleteDeferredRemoval();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001187 }
1188
1189 if (updateInputWindowsNeeded) {
1190 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
1191 }
1192 mService.setFocusTaskRegionLocked();
1193
1194 // Check to see if we are now in a state where the screen should
1195 // be enabled, because the window obscured flags have changed.
1196 mService.enableScreenIfNeededLocked();
1197
1198 mService.scheduleAnimationLocked();
1199 mService.mWindowPlacerLocked.destroyPendingSurfaces();
1200
1201 if (DEBUG_WINDOW_TRACE) Slog.e(TAG,
1202 "performSurfacePlacementInner exit: animating=" + mService.mAnimator.isAnimating());
1203 }
1204
1205 // TODO: Super crazy long method that should be broken down...
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001206 private void applySurfaceChangesTransaction(boolean recoveringMemory, int defaultDw, int defaultDh) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001207 mHoldScreenWindow = null;
Wale Ogunwaled4a00a02016-10-10 11:29:17 -07001208 mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001209
1210 if (mService.mWatermark != null) {
1211 mService.mWatermark.positionSurface(defaultDw, defaultDh);
1212 }
1213 if (mService.mStrictModeFlash != null) {
1214 mService.mStrictModeFlash.positionSurface(defaultDw, defaultDh);
1215 }
1216 if (mService.mCircularDisplayMask != null) {
1217 mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh, mService.mRotation);
1218 }
1219 if (mService.mEmulatorDisplayOverlay != null) {
1220 mService.mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh,
1221 mService.mRotation);
1222 }
1223
1224 boolean focusDisplayed = false;
1225
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001226 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001227 for (int j = 0; j < count; ++j) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001228 final DisplayContent dc = mChildren.get(j);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001229 WindowList windows = dc.getWindowList();
1230 DisplayInfo displayInfo = dc.getDisplayInfo();
1231 final int displayId = dc.getDisplayId();
1232 final int dw = displayInfo.logicalWidth;
1233 final int dh = displayInfo.logicalHeight;
1234 final boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
1235 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
1236
1237 // Reset for each display.
1238 mDisplayHasContent = false;
1239 mPreferredRefreshRate = 0;
1240 mPreferredModeId = 0;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001241 mTmpUpdateAllDrawn.clear();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001242
1243 int repeats = 0;
1244 do {
1245 repeats++;
1246 if (repeats > 6) {
1247 Slog.w(TAG, "Animation repeat aborted after too many iterations");
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001248 dc.clearLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001249 break;
1250 }
1251
1252 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
1253 "On entry to LockedInner", dc.pendingLayoutChanges);
1254
Wale Ogunwale0303c572016-10-20 10:16:29 -07001255 if ((dc.pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
1256 dc.adjustWallpaperWindows();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001257 }
1258
1259 if (isDefaultDisplay
1260 && (dc.pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
1261 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
1262 if (mService.updateOrientationFromAppTokensLocked(true)) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001263 dc.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001264 mService.mH.sendEmptyMessage(SEND_NEW_CONFIGURATION);
1265 }
1266 }
1267
1268 if ((dc.pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001269 dc.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001270 }
1271
1272 // FIRST LOOP: Perform a layout, if needed.
1273 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
1274 surfacePlacer.performLayoutLockedInner(dc, repeats == 1,
1275 false /* updateInputWindows */);
1276 } else {
1277 Slog.w(TAG, "Layout repeat skipped after too many iterations");
1278 }
1279
1280 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think
1281 // it is animating.
1282 dc.pendingLayoutChanges = 0;
1283
1284 if (isDefaultDisplay) {
1285 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
1286 for (int i = windows.size() - 1; i >= 0; i--) {
1287 WindowState w = windows.get(i);
1288 if (w.mHasSurface) {
1289 mService.mPolicy.applyPostLayoutPolicyLw(
1290 w, w.mAttrs, w.getParentWindow());
1291 }
1292 }
1293 dc.pendingLayoutChanges |=
1294 mService.mPolicy.finishPostLayoutPolicyLw();
1295 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
1296 "after finishPostLayoutPolicyLw", dc.pendingLayoutChanges);
1297 }
1298 } while (dc.pendingLayoutChanges != 0);
1299
1300 mObscured = false;
1301 mSyswin = false;
1302 dc.resetDimming();
1303
1304 // Only used if default window
1305 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
1306
1307 for (int i = windows.size() - 1; i >= 0; i--) {
1308 WindowState w = windows.get(i);
1309 final Task task = w.getTask();
1310 final boolean obscuredChanged = w.mObscured != mObscured;
1311
1312 // Update effect.
1313 w.mObscured = mObscured;
1314 if (!mObscured) {
1315 handleNotObscuredLocked(w, displayInfo);
1316 }
1317
1318 w.applyDimLayerIfNeeded();
1319
1320 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
Wale Ogunwale0303c572016-10-20 10:16:29 -07001321 && dc.mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001322 // This is the wallpaper target and its obscured state changed... make sure the
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001323 // current wallpaper's visibility has been updated accordingly.
Wale Ogunwale0303c572016-10-20 10:16:29 -07001324 dc.mWallpaperController.updateWallpaperVisibility();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001325 }
1326
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001327 w.handleWindowMovedIfNeeded();
1328
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001329 final WindowStateAnimator winAnimator = w.mWinAnimator;
1330
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001331 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
1332 w.mContentChanged = false;
1333
1334 // Moved from updateWindowsAndWallpaperLocked().
1335 if (w.mHasSurface) {
1336 // Take care of the window being ready to display.
1337 final boolean committed = winAnimator.commitFinishDrawingLocked();
1338 if (isDefaultDisplay && committed) {
1339 if (w.mAttrs.type == TYPE_DREAM) {
1340 // HACK: When a dream is shown, it may at that point hide the lock
1341 // screen. So we need to redo the layout to let the phone window manager
1342 // make this happen.
1343 dc.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
1344 if (DEBUG_LAYOUT_REPEATS) {
1345 surfacePlacer.debugLayoutRepeats(
1346 "dream and commitFinishDrawingLocked true",
1347 dc.pendingLayoutChanges);
1348 }
1349 }
1350 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
1351 if (DEBUG_WALLPAPER_LIGHT)
1352 Slog.v(TAG, "First draw done in potential wallpaper target " + w);
1353 mWallpaperMayChange = true;
1354 dc.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
1355 if (DEBUG_LAYOUT_REPEATS) {
1356 surfacePlacer.debugLayoutRepeats(
1357 "wallpaper and commitFinishDrawingLocked true",
1358 dc.pendingLayoutChanges);
1359 }
1360 }
1361 }
1362 if (!winAnimator.isAnimationStarting() && !winAnimator.isWaitingForOpening()) {
1363 // Updates the shown frame before we set up the surface. This is needed
1364 // because the resizing could change the top-left position (in addition to
1365 // size) of the window. setSurfaceBoundariesLocked uses mShownPosition to
1366 // position the surface.
1367 //
1368 // If an animation is being started, we can't call this method because the
1369 // animation hasn't processed its initial transformation yet, but in general
1370 // we do want to update the position if the window is animating.
1371 winAnimator.computeShownFrameLocked();
1372 }
1373 winAnimator.setSurfaceBoundariesLocked(recoveringMemory);
1374 }
1375
1376 final AppWindowToken atoken = w.mAppToken;
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001377 if (atoken != null) {
1378 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
1379 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
1380 mTmpUpdateAllDrawn.add(atoken);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001381 }
1382 }
1383
1384 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
1385 && w.isDisplayedLw()) {
1386 focusDisplayed = true;
1387 }
1388
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001389 w.updateResizingWindowIfNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001390 }
1391
1392 mService.mDisplayManagerInternal.setDisplayProperties(displayId,
1393 mDisplayHasContent,
1394 mPreferredRefreshRate,
1395 mPreferredModeId,
1396 true /* inTraversal, must call performTraversalInTrans... below */);
1397
1398 dc.stopDimmingIfNeeded();
1399
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001400 while (!mTmpUpdateAllDrawn.isEmpty()) {
1401 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001402 // See if any windows have been drawn, so they (and others associated with them)
1403 // can now be shown.
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -07001404 atoken.updateAllDrawn(dc);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001405 }
1406 }
1407
1408 if (focusDisplayed) {
1409 mService.mH.sendEmptyMessage(REPORT_LOSING_FOCUS);
1410 }
1411
1412 // Give the display manager a chance to adjust properties
1413 // like display rotation if it needs to.
1414 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
1415 }
1416
1417 /**
1418 * @param w WindowState this method is applied to.
1419 * @param dispInfo info of the display that the window's obscuring state is checked against.
1420 */
1421 private void handleNotObscuredLocked(final WindowState w, final DisplayInfo dispInfo) {
1422 final WindowManager.LayoutParams attrs = w.mAttrs;
1423 final int attrFlags = attrs.flags;
1424 final boolean canBeSeen = w.isDisplayedLw();
1425 final int privateflags = attrs.privateFlags;
1426
1427 if (canBeSeen && w.isObscuringFullscreen(dispInfo)) {
1428 // This window completely covers everything behind it,
1429 // so we want to leave all of them as undimmed (for
1430 // performance reasons).
1431 if (!mObscured) {
Wale Ogunwaled4a00a02016-10-10 11:29:17 -07001432 mObscuringWindow = w;
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001433 }
1434
1435 mObscured = true;
1436 }
1437
1438 if (w.mHasSurface && canBeSeen) {
1439 if ((attrFlags & FLAG_KEEP_SCREEN_ON) != 0) {
1440 mHoldScreen = w.mSession;
1441 mHoldScreenWindow = w;
1442 } else if (DEBUG_KEEP_SCREEN_ON && w == mService.mLastWakeLockHoldingWindow) {
1443 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked: " + w + " was holding "
1444 + "screen wakelock but no longer has FLAG_KEEP_SCREEN_ON!!! called by"
1445 + Debug.getCallers(10));
1446 }
1447 if (!mSyswin && w.mAttrs.screenBrightness >= 0 && mScreenBrightness < 0) {
1448 mScreenBrightness = w.mAttrs.screenBrightness;
1449 }
1450 if (!mSyswin && w.mAttrs.buttonBrightness >= 0 && mButtonBrightness < 0) {
1451 mButtonBrightness = w.mAttrs.buttonBrightness;
1452 }
1453 if (!mSyswin && w.mAttrs.userActivityTimeout >= 0 && mUserActivityTimeout < 0) {
1454 mUserActivityTimeout = w.mAttrs.userActivityTimeout;
1455 }
1456
1457 final int type = attrs.type;
1458 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
1459 || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
1460 mSyswin = true;
1461 }
1462
1463 // This function assumes that the contents of the default display are processed first
1464 // before secondary displays.
1465 final DisplayContent displayContent = w.getDisplayContent();
1466 if (displayContent != null && displayContent.isDefaultDisplay) {
1467 // While a dream or keyguard is showing, obscure ordinary application content on
1468 // secondary displays (by forcibly enabling mirroring unless there is other content
1469 // we want to show) but still allow opaque keyguard dialogs to be shown.
1470 if (type == TYPE_DREAM || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
1471 mObscureApplicationContentOnSecondaryDisplays = true;
1472 }
1473 mDisplayHasContent = true;
1474 } else if (displayContent != null &&
1475 (!mObscureApplicationContentOnSecondaryDisplays
1476 || (mObscured && type == TYPE_KEYGUARD_DIALOG))) {
1477 // Allow full screen keyguard presentation dialogs to be seen.
1478 mDisplayHasContent = true;
1479 }
1480 if (mPreferredRefreshRate == 0 && w.mAttrs.preferredRefreshRate != 0) {
1481 mPreferredRefreshRate = w.mAttrs.preferredRefreshRate;
1482 }
1483 if (mPreferredModeId == 0 && w.mAttrs.preferredDisplayModeId != 0) {
1484 mPreferredModeId = w.mAttrs.preferredDisplayModeId;
1485 }
1486 if ((privateflags & PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE) != 0) {
1487 mSustainedPerformanceModeCurrent = true;
1488 }
1489 }
1490 }
1491
1492 boolean copyAnimToLayoutParams() {
1493 boolean doRequest = false;
1494
1495 final int bulkUpdateParams = mService.mAnimator.mBulkUpdateParams;
1496 if ((bulkUpdateParams & SET_UPDATE_ROTATION) != 0) {
1497 mUpdateRotation = true;
1498 doRequest = true;
1499 }
1500 if ((bulkUpdateParams & SET_WALLPAPER_MAY_CHANGE) != 0) {
1501 mWallpaperMayChange = true;
1502 doRequest = true;
1503 }
1504 if ((bulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0) {
1505 mWallpaperForceHidingChanged = true;
1506 doRequest = true;
1507 }
1508 if ((bulkUpdateParams & SET_ORIENTATION_CHANGE_COMPLETE) == 0) {
1509 mOrientationChangeComplete = false;
1510 } else {
1511 mOrientationChangeComplete = true;
1512 mLastWindowFreezeSource = mService.mAnimator.mLastWindowFreezeSource;
1513 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
1514 doRequest = true;
1515 }
1516 }
1517 if ((bulkUpdateParams & SET_TURN_ON_SCREEN) != 0) {
1518 mService.mTurnOnScreen = true;
1519 }
1520 if ((bulkUpdateParams & SET_WALLPAPER_ACTION_PENDING) != 0) {
1521 mWallpaperActionPending = true;
1522 }
1523
1524 return doRequest;
1525 }
1526
1527 private static int toBrightnessOverride(float value) {
1528 return (int)(value * PowerManager.BRIGHTNESS_ON);
1529 }
1530
1531 void enableSurfaceTrace(ParcelFileDescriptor pfd) {
1532 final FileDescriptor fd = pfd.getFileDescriptor();
1533 if (mSurfaceTraceEnabled) {
1534 disableSurfaceTrace();
1535 }
1536 mSurfaceTraceEnabled = true;
1537 mRemoteEventTrace = new RemoteEventTrace(mService, fd);
1538 mSurfaceTraceFd = pfd;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001539 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1540 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001541 dc.enableSurfaceTrace(fd);
1542 }
1543 }
1544
1545 void disableSurfaceTrace() {
1546 mSurfaceTraceEnabled = false;
1547 mRemoteEventTrace = null;
1548 mSurfaceTraceFd = null;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001549 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1550 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001551 dc.disableSurfaceTrace();
1552 }
1553 }
1554
1555 void dumpDisplayContents(PrintWriter pw) {
1556 pw.println("WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)");
1557 if (mService.mDisplayReady) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001558 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001559 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001560 final DisplayContent displayContent = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001561 displayContent.dump(" ", pw);
1562 }
1563 } else {
1564 pw.println(" NO DISPLAY");
1565 }
1566 }
1567
1568 void dumpLayoutNeededDisplayIds(PrintWriter pw) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001569 if (!isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001570 return;
1571 }
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001572 pw.print(" mLayoutNeeded on displays=");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001573 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001574 for (int displayNdx = 0; displayNdx < count; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001575 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001576 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001577 pw.print(displayContent.getDisplayId());
1578 }
1579 }
1580 pw.println();
1581 }
1582
1583 void dumpWindowsNoHeader(PrintWriter pw, boolean dumpAll, ArrayList<WindowState> windows) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001584 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001585 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001586 final WindowList windowList = mChildren.get(displayNdx).getWindowList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001587 for (int winNdx = windowList.size() - 1; winNdx >= 0; --winNdx) {
1588 final WindowState w = windowList.get(winNdx);
1589 if (windows == null || windows.contains(w)) {
1590 pw.println(" Window #" + winNdx + " " + w + ":");
1591 w.dump(pw, " ", dumpAll || windows != null);
1592 }
1593 }
1594 }
1595 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001596
Wale Ogunwale02319a62016-09-26 15:21:22 -07001597 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1598 pw.println(" All tokens:");
1599 for (int i = mChildren.size() - 1; i >= 0; --i) {
1600 mChildren.get(i).dumpTokens(pw, dumpAll);
1601 }
1602 }
1603
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001604 @Override
1605 String getName() {
1606 return "ROOT";
1607 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001608}