blob: 2cc96c9ee7b6ce2c6fa2ae1ef0ff57e14333ff06 [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
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070019import android.annotation.CallSuper;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070020import 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;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020025import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -070026import android.os.IBinder;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020027import android.os.Looper;
28import android.os.Message;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070029import android.os.ParcelFileDescriptor;
30import android.os.PowerManager;
31import android.os.RemoteException;
32import android.os.SystemClock;
33import android.os.UserHandle;
34import android.provider.Settings;
David Stevensf833ba92017-03-16 19:00:20 -070035import android.util.ArraySet;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070036import android.util.EventLog;
37import android.util.Slog;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070038import android.util.SparseIntArray;
Steven Timotiusaf03df62017-07-18 16:56:43 -070039import android.util.proto.ProtoOutputStream;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070040import android.view.Display;
41import android.view.DisplayInfo;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070042import android.view.WindowManager;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020043
Wale Ogunwalee05f5012016-09-16 16:27:29 -070044import com.android.internal.util.ArrayUtils;
45import com.android.server.EventLogTags;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070046
47import java.io.FileDescriptor;
48import java.io.PrintWriter;
49import java.util.ArrayList;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080050import java.util.function.Consumer;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070051
Wale Ogunwalef7cab102016-10-25 15:25:14 -070052import static android.app.AppOpsManager.MODE_ALLOWED;
53import static android.app.AppOpsManager.MODE_DEFAULT;
54import static android.app.AppOpsManager.OP_NONE;
Wale Ogunwale02319a62016-09-26 15:21:22 -070055import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian48146fd2017-08-07 11:22:50 -070056import static android.view.Display.INVALID_DISPLAY;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070057import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
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;
Jorim Jaggi879ff722016-11-04 18:08:17 -070062
Adrian Roose99bc052017-11-20 17:55:31 +010063import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
65import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070066import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
67import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_KEEP_SCREEN_ON;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
69import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
70import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_POWER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
72import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
73import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_TRACE;
74import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
75import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
76import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
77import static com.android.server.wm.WindowManagerDebugConfig.TAG_KEEP_SCREEN_ON;
78import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
79import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070080import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
81import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070082import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
83import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
84import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_NONE;
85import static com.android.server.wm.WindowManagerService.H.WINDOW_FREEZE_TIMEOUT;
86import static com.android.server.wm.WindowManagerService.logSurface;
87import static com.android.server.wm.WindowSurfacePlacer.SET_FORCE_HIDING_CHANGED;
88import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070089import static com.android.server.wm.WindowSurfacePlacer.SET_UPDATE_ROTATION;
90import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_ACTION_PENDING;
91import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070092import static com.android.server.wm.proto.RootWindowContainerProto.DISPLAYS;
93import static com.android.server.wm.proto.RootWindowContainerProto.WINDOWS;
94import static com.android.server.wm.proto.RootWindowContainerProto.WINDOW_CONTAINER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070095
96/** Root {@link WindowContainer} for the device. */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -070097class RootWindowContainer extends WindowContainer<DisplayContent> {
Wale Ogunwalee05f5012016-09-16 16:27:29 -070098 private static final String TAG = TAG_WITH_CLASS_NAME ? "RootWindowContainer" : TAG_WM;
99
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200100 private static final int SET_SCREEN_BRIGHTNESS_OVERRIDE = 1;
101 private static final int SET_USER_ACTIVITY_TIMEOUT = 2;
102
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700103 private boolean mWallpaperForceHidingChanged = false;
104 private Object mLastWindowFreezeSource = null;
105 private Session mHoldScreen = null;
106 private float mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700107 private long mUserActivityTimeout = -1;
108 private boolean mUpdateRotation = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700109 // Following variables are for debugging screen wakelock only.
110 // Last window that requires screen wakelock
111 WindowState mHoldScreenWindow = null;
112 // Last window that obscures all windows below
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700113 WindowState mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700114 // Only set while traversing the default display based on its content.
115 // Affects the behavior of mirroring on secondary displays.
116 private boolean mObscureApplicationContentOnSecondaryDisplays = false;
117
118 private boolean mSustainedPerformanceModeEnabled = false;
119 private boolean mSustainedPerformanceModeCurrent = false;
120
121 boolean mWallpaperMayChange = false;
Robert Carr11c26c22016-09-23 12:40:27 -0700122 // During an orientation change, we track whether all windows have rendered
123 // at the new orientation, and this will be false from changing orientation until that occurs.
124 // For seamless rotation cases this always stays true, as the windows complete their orientation
125 // changes 1 by 1 without disturbing global state.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700126 boolean mOrientationChangeComplete = true;
127 boolean mWallpaperActionPending = false;
128
129 private final ArrayList<Integer> mChangedStackList = new ArrayList();
130
131 // State for the RemoteSurfaceTrace system used in testing. If this is enabled SurfaceControl
132 // instances will be replaced with an instance that writes a binary representation of all
133 // commands to mSurfaceTraceFd.
134 boolean mSurfaceTraceEnabled;
135 ParcelFileDescriptor mSurfaceTraceFd;
136 RemoteEventTrace mRemoteEventTrace;
137
Wale Ogunwale0303c572016-10-20 10:16:29 -0700138 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700139
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200140 private final Handler mHandler;
141
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800142 private String mCloseSystemDialogsReason;
143 private final Consumer<WindowState> mCloseSystemDialogsConsumer = w -> {
144 if (w.mHasSurface) {
145 try {
146 w.mClient.closeSystemDialogs(mCloseSystemDialogsReason);
147 } catch (RemoteException e) {
148 }
149 }
150 };
151
152 private static final Consumer<WindowState> sRemoveReplacedWindowsConsumer = w -> {
153 final AppWindowToken aToken = w.mAppToken;
154 if (aToken != null) {
155 aToken.removeReplacedWindowIfNeeded(w);
156 }
157 };
158
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700159 RootWindowContainer(WindowManagerService service) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100160 super(service);
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200161 mHandler = new MyHandler(service.mH.getLooper());
Wale Ogunwale0303c572016-10-20 10:16:29 -0700162 mWallpaperController = new WallpaperController(mService);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700163 }
164
165 WindowState computeFocusedWindow() {
David Stevens46939562017-03-24 13:04:00 -0700166 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700167 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700168 final WindowState win = dc.findFocusedWindow();
169 if (win != null) {
170 return win;
171 }
172 }
173 return null;
174 }
175
176 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800177 * Get an array with display ids ordered by focus priority - last items should be given
178 * focus first. Sparse array just maps position to displayId.
179 */
180 void getDisplaysInFocusOrder(SparseIntArray displaysInFocusOrder) {
181 displaysInFocusOrder.clear();
182
183 final int size = mChildren.size();
184 for (int i = 0; i < size; ++i) {
Andrii Kulian0214ed92017-05-16 13:44:05 -0700185 final DisplayContent displayContent = mChildren.get(i);
186 if (displayContent.isRemovalDeferred()) {
187 // Don't report displays that are going to be removed soon.
188 continue;
189 }
190 displaysInFocusOrder.put(i, displayContent.getDisplayId());
Andrii Kulian7fc22812016-12-28 13:04:11 -0800191 }
192 }
193
194 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700195 * Retrieve the DisplayContent for the specified displayId. Will create a new DisplayContent if
196 * there is a Display for the displayId.
197 *
198 * @param displayId The display the caller is interested in.
199 * @return The DisplayContent associated with displayId or null if there is no Display for it.
200 */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700201 DisplayContent getDisplayContentOrCreate(int displayId) {
202 DisplayContent dc = getDisplayContent(displayId);
203
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700204 if (dc == null) {
205 final Display display = mService.mDisplayManager.getDisplay(displayId);
206 if (display != null) {
Andrii Kulian325cac42017-04-20 16:02:47 -0700207 final long callingIdentity = Binder.clearCallingIdentity();
208 try {
209 dc = createDisplayContent(display);
210 } finally {
211 Binder.restoreCallingIdentity(callingIdentity);
212 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700213 }
214 }
215 return dc;
216 }
217
Wale Ogunwale02319a62016-09-26 15:21:22 -0700218 DisplayContent getDisplayContent(int displayId) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700219 for (int i = mChildren.size() - 1; i >= 0; --i) {
220 final DisplayContent current = mChildren.get(i);
221 if (current.getDisplayId() == displayId) {
222 return current;
223 }
224 }
225 return null;
226 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700227
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700228 private DisplayContent createDisplayContent(final Display display) {
Robert Carrb1579c82017-09-05 14:54:47 -0700229 final DisplayContent dc = new DisplayContent(display, mService,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700230 mWallpaperController);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700231 final int displayId = display.getDisplayId();
232
233 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Adding display=" + display);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700234
235 final DisplayInfo displayInfo = dc.getDisplayInfo();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700236 final Rect rect = new Rect();
237 mService.mDisplaySettings.getOverscanLocked(displayInfo.name, displayInfo.uniqueId, rect);
238 displayInfo.overscanLeft = rect.left;
239 displayInfo.overscanTop = rect.top;
240 displayInfo.overscanRight = rect.right;
241 displayInfo.overscanBottom = rect.bottom;
242 if (mService.mDisplayManagerInternal != null) {
243 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(
244 displayId, displayInfo);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000245 dc.configureDisplayPolicy();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700246
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700247 // Tap Listeners are supported for:
248 // 1. All physical displays (multi-display).
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -0700249 // 2. VirtualDisplays on VR, AA (and everything else).
250 if (mService.canDispatchPointerEvents()) {
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700251 if (DEBUG_DISPLAY) {
252 Slog.d(TAG,
253 "Registering PointerEventListener for DisplayId: " + displayId);
254 }
255 dc.mTapDetector = new TaskTapPointerEventListener(mService, dc);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700256 mService.registerPointerEventListener(dc.mTapDetector);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700257 if (displayId == DEFAULT_DISPLAY) {
258 mService.registerPointerEventListener(mService.mMousePositionTracker);
259 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700260 }
261 }
262
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700263 return dc;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700264 }
265
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700266 boolean isLayoutNeeded() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700267 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700268 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700269 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700270 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700271 return true;
272 }
273 }
274 return false;
275 }
276
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000277 void getWindowsByName(ArrayList<WindowState> output, String name) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700278 int objectId = 0;
279 // See if this is an object ID.
280 try {
281 objectId = Integer.parseInt(name, 16);
282 name = null;
283 } catch (RuntimeException e) {
284 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800285
286 getWindowsByName(output, name, objectId);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700287 }
288
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000289 private void getWindowsByName(ArrayList<WindowState> output, String name, int objectId) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800290 forAllWindows((w) -> {
291 if (name != null) {
292 if (w.mAttrs.getTitle().toString().contains(name)) {
293 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700294 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800295 } else if (System.identityHashCode(w) == objectId) {
296 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700297 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800298 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700299 }
300
Wale Ogunwale02319a62016-09-26 15:21:22 -0700301 /**
302 * Returns the app window token for the input binder if it exist in the system.
303 * NOTE: Only one AppWindowToken is allowed to exist in the system for a binder token, since
304 * AppWindowToken represents an activity which can only exist on one display.
305 */
306 AppWindowToken getAppWindowToken(IBinder binder) {
307 for (int i = mChildren.size() - 1; i >= 0; --i) {
308 final DisplayContent dc = mChildren.get(i);
309 final AppWindowToken atoken = dc.getAppWindowToken(binder);
310 if (atoken != null) {
311 return atoken;
312 }
313 }
314 return null;
315 }
316
317 /** Returns the display object the input window token is currently mapped on. */
318 DisplayContent getWindowTokenDisplay(WindowToken token) {
319 if (token == null) {
320 return null;
321 }
322
323 for (int i = mChildren.size() - 1; i >= 0; --i) {
324 final DisplayContent dc = mChildren.get(i);
325 final WindowToken current = dc.getWindowToken(token.token);
326 if (current == token) {
327 return dc;
328 }
329 }
330
331 return null;
332 }
333
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700334 /**
335 * Set new display override config and return array of ids of stacks that were changed during
336 * update. If called for the default display, global configuration will also be updated.
337 */
338 int[] setDisplayOverrideConfigurationIfNeeded(Configuration newConfiguration, int displayId) {
339 final DisplayContent displayContent = getDisplayContent(displayId);
340 if (displayContent == null) {
341 throw new IllegalArgumentException("Display not found for id: " + displayId);
342 }
343
344 final Configuration currentConfig = displayContent.getOverrideConfiguration();
345 final boolean configChanged = currentConfig.diff(newConfiguration) != 0;
346 if (!configChanged) {
347 return null;
348 }
Andrii Kuliand68501e2017-01-10 22:57:27 -0800349 displayContent.onOverrideConfigurationChanged(newConfiguration);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700350
351 if (displayId == DEFAULT_DISPLAY) {
352 // Override configuration of the default display duplicates global config. In this case
353 // we also want to update the global config.
354 return setGlobalConfigurationIfNeeded(newConfiguration);
355 } else {
356 return updateStackBoundsAfterConfigChange(displayId);
357 }
358 }
359
360 private int[] setGlobalConfigurationIfNeeded(Configuration newConfiguration) {
Andrii Kulian441e4492016-09-29 15:25:00 -0700361 final boolean configChanged = getConfiguration().diff(newConfiguration) != 0;
362 if (!configChanged) {
363 return null;
364 }
365 onConfigurationChanged(newConfiguration);
366 return updateStackBoundsAfterConfigChange();
367 }
368
369 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700370 public void onConfigurationChanged(Configuration newParentConfig) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700371 prepareFreezingTaskBounds();
Andrii Kulian441e4492016-09-29 15:25:00 -0700372 super.onConfigurationChanged(newParentConfig);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700373
374 mService.mPolicy.onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700375 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700376
Andrii Kulian441e4492016-09-29 15:25:00 -0700377 /**
378 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
379 * bounds were updated.
380 */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700381 private int[] updateStackBoundsAfterConfigChange() {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700382 mChangedStackList.clear();
383
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700384 final int numDisplays = mChildren.size();
385 for (int i = 0; i < numDisplays; ++i) {
386 final DisplayContent dc = mChildren.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700387 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700388 }
389
390 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
391 }
392
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700393 /** Same as {@link #updateStackBoundsAfterConfigChange()} but only for a specific display. */
394 private int[] updateStackBoundsAfterConfigChange(int displayId) {
395 mChangedStackList.clear();
396
397 final DisplayContent dc = getDisplayContent(displayId);
398 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
399
400 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
401 }
402
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700403 private void prepareFreezingTaskBounds() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700404 for (int i = mChildren.size() - 1; i >= 0; i--) {
405 mChildren.get(i).prepareFreezingTaskBounds();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700406 }
407 }
408
Wale Ogunwale68278562017-09-23 17:13:55 -0700409 TaskStack getStack(int windowingMode, int activityType) {
410 for (int i = mChildren.size() - 1; i >= 0; i--) {
411 final DisplayContent dc = mChildren.get(i);
412 final TaskStack stack = dc.getStack(windowingMode, activityType);
413 if (stack != null) {
414 return stack;
415 }
416 }
417 return null;
418 }
419
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700420 void setSecureSurfaceState(int userId, boolean disabled) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800421 forAllWindows((w) -> {
422 if (w.mHasSurface && userId == UserHandle.getUserId(w.mOwnerUid)) {
423 w.mWinAnimator.setSecureLocked(disabled);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700424 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800425 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700426 }
427
428 void updateAppOpsState() {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800429 forAllWindows((w) -> {
430 if (w.mAppOp == OP_NONE) {
431 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700432 }
Peter Visontay96449f62017-12-11 18:50:03 +0000433 final int mode = mService.mAppOps.noteOpNoThrow(w.mAppOp, w.getOwningUid(),
Wale Ogunwaled1880962016-11-08 10:31:59 -0800434 w.getOwningPackage());
435 w.setAppOpVisibilityLw(mode == MODE_ALLOWED || mode == MODE_DEFAULT);
436 }, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700437 }
438
439 boolean canShowStrictModeViolation(int pid) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800440 final WindowState win = getWindow((w) -> w.mSession.mPid == pid && w.isVisibleLw());
441 return win != null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700442 }
443
444 void closeSystemDialogs(String reason) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800445 mCloseSystemDialogsReason = reason;
446 forAllWindows(mCloseSystemDialogsConsumer, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700447 }
448
449 void removeReplacedWindows() {
450 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION removeReplacedWindows");
451 mService.openSurfaceTransaction();
452 try {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800453 forAllWindows(sRemoveReplacedWindowsConsumer, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700454 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200455 mService.closeSurfaceTransaction("removeReplacedWindows");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700456 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION removeReplacedWindows");
457 }
458 }
459
460 boolean hasPendingLayoutChanges(WindowAnimator animator) {
461 boolean hasChanges = false;
462
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700463 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700464 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700465 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700466 final int pendingChanges = animator.getPendingLayoutChanges(dc.getDisplayId());
467 if ((pendingChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
468 animator.mBulkUpdateParams |= SET_WALLPAPER_ACTION_PENDING;
469 }
470 if (pendingChanges != 0) {
471 hasChanges = true;
472 }
473 }
474
475 return hasChanges;
476 }
477
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700478 boolean reclaimSomeSurfaceMemory(WindowStateAnimator winAnimator, String operation,
479 boolean secure) {
480 final WindowSurfaceController surfaceController = winAnimator.mSurfaceController;
481 boolean leakedSurface = false;
482 boolean killedApps = false;
483
484 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, winAnimator.mWin.toString(),
485 winAnimator.mSession.mPid, operation);
486
487 final long callingIdentity = Binder.clearCallingIdentity();
488 try {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700489 // There was some problem...first, do a sanity check of the window list to make sure
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700490 // we haven't left any dangling surfaces around.
491
492 Slog.i(TAG_WM, "Out of memory for surface! Looking for leaks...");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700493 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700494 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700495 leakedSurface |= mChildren.get(displayNdx).destroyLeakedSurfaces();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700496 }
497
498 if (!leakedSurface) {
499 Slog.w(TAG_WM, "No leaked surfaces; killing applications!");
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700500 final SparseIntArray pidCandidates = new SparseIntArray();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700501 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800502 mChildren.get(displayNdx).forAllWindows((w) -> {
503 if (mService.mForceRemoves.contains(w)) {
504 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700505 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800506 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700507 if (wsa.mSurfaceController != null) {
508 pidCandidates.append(wsa.mSession.mPid, wsa.mSession.mPid);
509 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800510 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700511
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700512 if (pidCandidates.size() > 0) {
513 int[] pids = new int[pidCandidates.size()];
514 for (int i = 0; i < pids.length; i++) {
515 pids[i] = pidCandidates.keyAt(i);
516 }
517 try {
518 if (mService.mActivityManager.killPids(pids, "Free memory", secure)) {
519 killedApps = true;
520 }
521 } catch (RemoteException e) {
522 }
523 }
524 }
525 }
526
527 if (leakedSurface || killedApps) {
528 // We managed to reclaim some memory, so get rid of the trouble surface and ask the
529 // app to request another one.
530 Slog.w(TAG_WM,
531 "Looks like we have reclaimed some memory, clearing surface for retry.");
532 if (surfaceController != null) {
533 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) logSurface(winAnimator.mWin,
534 "RECOVER DESTROY", false);
535 winAnimator.destroySurface();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800536 if (winAnimator.mWin.mAppToken != null
537 && winAnimator.mWin.mAppToken.getController() != null) {
538 winAnimator.mWin.mAppToken.getController().removeStartingWindow();
539 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700540 }
541
542 try {
543 winAnimator.mWin.mClient.dispatchGetNewSurface();
544 } catch (RemoteException e) {
545 }
546 }
547 } finally {
548 Binder.restoreCallingIdentity(callingIdentity);
549 }
550
551 return leakedSurface || killedApps;
552 }
553
554 // "Something has changed! Let's make it correct now."
555 // TODO: Super crazy long method that should be broken down...
556 void performSurfacePlacement(boolean recoveringMemory) {
557 if (DEBUG_WINDOW_TRACE) Slog.v(TAG, "performSurfacePlacementInner: entry. Called by "
558 + Debug.getCallers(3));
559
560 int i;
561 boolean updateInputWindowsNeeded = false;
562
563 if (mService.mFocusMayChange) {
564 mService.mFocusMayChange = false;
565 updateInputWindowsNeeded = mService.updateFocusedWindowLocked(
566 UPDATE_FOCUS_WILL_PLACE_SURFACES, false /*updateInputWindows*/);
567 }
568
569 // Initialize state of exiting tokens.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700570 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700571 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700572 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800573 displayContent.setExitingTokensHasVisible(false);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700574 }
575
576 mHoldScreen = null;
577 mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700578 mUserActivityTimeout = -1;
579 mObscureApplicationContentOnSecondaryDisplays = false;
580 mSustainedPerformanceModeCurrent = false;
581 mService.mTransactionSequence++;
582
583 // TODO(multi-display):
584 final DisplayContent defaultDisplay = mService.getDefaultDisplayContentLocked();
585 final DisplayInfo defaultInfo = defaultDisplay.getDisplayInfo();
586 final int defaultDw = defaultInfo.logicalWidth;
587 final int defaultDh = defaultInfo.logicalHeight;
588
589 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
590 ">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
591 mService.openSurfaceTransaction();
592 try {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700593 applySurfaceChangesTransaction(recoveringMemory, defaultDw, defaultDh);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700594 } catch (RuntimeException e) {
595 Slog.wtf(TAG, "Unhandled exception in Window Manager", e);
596 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200597 mService.closeSurfaceTransaction("performLayoutAndPlaceSurfaces");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700598 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
599 "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
600 }
601
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700602 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
603
604 // If we are ready to perform an app transition, check through all of the app tokens to be
605 // shown and see if they are ready to go.
606 if (mService.mAppTransition.isReady()) {
607 defaultDisplay.pendingLayoutChanges |=
Wale Ogunwale0303c572016-10-20 10:16:29 -0700608 surfacePlacer.handleAppTransitionReadyLocked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700609 if (DEBUG_LAYOUT_REPEATS)
610 surfacePlacer.debugLayoutRepeats("after handleAppTransitionReadyLocked",
611 defaultDisplay.pendingLayoutChanges);
612 }
613
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200614 if (!isAppAnimating() && mService.mAppTransition.isRunning()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700615 // We have finished the animation of an app transition. To do this, we have delayed a
616 // lot of operations like showing and hiding apps, moving apps in Z-order, etc. The app
617 // token list reflects the correct Z-order, but the window list may now be out of sync
618 // with it. So here we will just rebuild the entire app window list. Fun!
619 defaultDisplay.pendingLayoutChanges |=
620 mService.handleAnimatingStoppedAndTransitionLocked();
621 if (DEBUG_LAYOUT_REPEATS)
622 surfacePlacer.debugLayoutRepeats("after handleAnimStopAndXitionLock",
623 defaultDisplay.pendingLayoutChanges);
624 }
625
626 if (mWallpaperForceHidingChanged && defaultDisplay.pendingLayoutChanges == 0
627 && !mService.mAppTransition.isReady()) {
628 // At this point, there was a window with a wallpaper that was force hiding other
629 // windows behind it, but now it is going away. This may be simple -- just animate away
630 // the wallpaper and its window -- or it may be hard -- the wallpaper now needs to be
631 // shown behind something that was hidden.
632 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
633 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
634 "after animateAwayWallpaperLocked", defaultDisplay.pendingLayoutChanges);
635 }
636 mWallpaperForceHidingChanged = false;
637
638 if (mWallpaperMayChange) {
639 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Wallpaper may change! Adjusting");
640 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
641 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("WallpaperMayChange",
642 defaultDisplay.pendingLayoutChanges);
643 }
644
645 if (mService.mFocusMayChange) {
646 mService.mFocusMayChange = false;
647 if (mService.updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
648 false /*updateInputWindows*/)) {
649 updateInputWindowsNeeded = true;
650 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
651 }
652 }
653
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700654 if (isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700655 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
656 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("mLayoutNeeded",
657 defaultDisplay.pendingLayoutChanges);
658 }
659
David Stevensf833ba92017-03-16 19:00:20 -0700660 final ArraySet<DisplayContent> touchExcludeRegionUpdateDisplays = handleResizingWindows();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700661
662 if (DEBUG_ORIENTATION && mService.mDisplayFrozen) Slog.v(TAG,
663 "With display frozen, orientationChangeComplete=" + mOrientationChangeComplete);
664 if (mOrientationChangeComplete) {
665 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
666 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
667 mService.mLastFinishedFreezeSource = mLastWindowFreezeSource;
668 mService.mH.removeMessages(WINDOW_FREEZE_TIMEOUT);
669 }
670 mService.stopFreezingDisplayLocked();
671 }
672
673 // Destroy the surface of any windows that are no longer visible.
674 boolean wallpaperDestroyed = false;
675 i = mService.mDestroySurface.size();
676 if (i > 0) {
677 do {
678 i--;
679 WindowState win = mService.mDestroySurface.get(i);
680 win.mDestroying = false;
681 if (mService.mInputMethodWindow == win) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000682 mService.setInputMethodWindowLocked(null);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700683 }
Wale Ogunwale0303c572016-10-20 10:16:29 -0700684 if (win.getDisplayContent().mWallpaperController.isWallpaperTarget(win)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700685 wallpaperDestroyed = true;
686 }
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200687 win.destroySurfaceUnchecked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700688 } while (i > 0);
689 mService.mDestroySurface.clear();
690 }
691
692 // Time to remove any exiting tokens?
693 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700694 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800695 displayContent.removeExistingTokensIfPossible();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700696 }
697
698 if (wallpaperDestroyed) {
699 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700700 defaultDisplay.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700701 }
702
703 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700704 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700705 if (displayContent.pendingLayoutChanges != 0) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700706 displayContent.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700707 }
708 }
709
710 // Finally update all input windows now that the window changes have stabilized.
711 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
712
713 mService.setHoldScreenLocked(mHoldScreen);
714 if (!mService.mDisplayFrozen) {
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200715 final int brightness = mScreenBrightness < 0 || mScreenBrightness > 1.0f
716 ? -1 : toBrightnessOverride(mScreenBrightness);
717
718 // Post these on a handler such that we don't call into power manager service while
719 // holding the window manager lock to avoid lock contention with power manager lock.
720 mHandler.obtainMessage(SET_SCREEN_BRIGHTNESS_OVERRIDE, brightness, 0).sendToTarget();
721 mHandler.obtainMessage(SET_USER_ACTIVITY_TIMEOUT, mUserActivityTimeout).sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700722 }
723
724 if (mSustainedPerformanceModeCurrent != mSustainedPerformanceModeEnabled) {
725 mSustainedPerformanceModeEnabled = mSustainedPerformanceModeCurrent;
726 mService.mPowerManagerInternal.powerHint(
Ruchi Kandoi0d434042016-10-03 09:12:02 -0700727 PowerHint.SUSTAINED_PERFORMANCE,
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700728 (mSustainedPerformanceModeEnabled ? 1 : 0));
729 }
730
731 if (mService.mTurnOnScreen) {
732 if (mService.mAllowTheaterModeWakeFromLayout
733 || Settings.Global.getInt(mService.mContext.getContentResolver(),
734 Settings.Global.THEATER_MODE_ON, 0) == 0) {
735 if (DEBUG_VISIBILITY || DEBUG_POWER) {
736 Slog.v(TAG, "Turning screen on after layout!");
737 }
738 mService.mPowerManager.wakeUp(SystemClock.uptimeMillis(),
739 "android.server.wm:TURN_ON");
740 }
741 mService.mTurnOnScreen = false;
742 }
743
744 if (mUpdateRotation) {
745 if (DEBUG_ORIENTATION) Slog.d(TAG, "Performing post-rotate rotation");
Andrii Kulian48146fd2017-08-07 11:22:50 -0700746 // TODO(multi-display): Update rotation for different displays separately.
747 final int displayId = defaultDisplay.getDisplayId();
748 if (defaultDisplay.updateRotationUnchecked(false /* inTransaction */)) {
749 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, displayId).sendToTarget();
750 } else {
751 mUpdateRotation = false;
752 }
753 // Update rotation of VR virtual display separately. Currently this is the only kind of
754 // secondary display that can be rotated because of the single-display limitations in
755 // PhoneWindowManager.
756 final DisplayContent vrDisplay = mService.mVr2dDisplayId != INVALID_DISPLAY
757 ? getDisplayContent(mService.mVr2dDisplayId) : null;
758 if (vrDisplay != null && vrDisplay.updateRotationUnchecked(false /* inTransaction */)) {
759 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mService.mVr2dDisplayId)
760 .sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700761 }
762 }
763
764 if (mService.mWaitingForDrawnCallback != null ||
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700765 (mOrientationChangeComplete && !defaultDisplay.isLayoutNeeded()
766 && !mUpdateRotation)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700767 mService.checkDrawnWindowsLocked();
768 }
769
770 final int N = mService.mPendingRemove.size();
771 if (N > 0) {
772 if (mService.mPendingRemoveTmp.length < N) {
773 mService.mPendingRemoveTmp = new WindowState[N+10];
774 }
775 mService.mPendingRemove.toArray(mService.mPendingRemoveTmp);
776 mService.mPendingRemove.clear();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700777 ArrayList<DisplayContent> displayList = new ArrayList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700778 for (i = 0; i < N; i++) {
779 final WindowState w = mService.mPendingRemoveTmp[i];
780 w.removeImmediately();
781 final DisplayContent displayContent = w.getDisplayContent();
782 if (displayContent != null && !displayList.contains(displayContent)) {
783 displayList.add(displayContent);
784 }
785 }
786
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700787 for (int j = displayList.size() - 1; j >= 0; --j) {
788 final DisplayContent dc = displayList.get(j);
789 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700790 }
791 }
792
793 // Remove all deferred displays stacks, tasks, and activities.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700794 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
795 mChildren.get(displayNdx).checkCompleteDeferredRemoval();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700796 }
797
798 if (updateInputWindowsNeeded) {
799 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
800 }
David Stevensee9e2772017-02-09 16:30:27 -0800801 mService.setFocusTaskRegionLocked(null);
David Stevensf833ba92017-03-16 19:00:20 -0700802 if (touchExcludeRegionUpdateDisplays != null) {
803 final DisplayContent focusedDc = mService.mFocusedApp != null
804 ? mService.mFocusedApp.getDisplayContent() : null;
805 for (DisplayContent dc : touchExcludeRegionUpdateDisplays) {
806 // The focused DisplayContent was recalcuated in setFocusTaskRegionLocked
807 if (focusedDc != dc) {
808 dc.setTouchExcludeRegion(null /* focusedTask */);
809 }
810 }
811 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700812
813 // Check to see if we are now in a state where the screen should
814 // be enabled, because the window obscured flags have changed.
815 mService.enableScreenIfNeededLocked();
816
817 mService.scheduleAnimationLocked();
818 mService.mWindowPlacerLocked.destroyPendingSurfaces();
819
820 if (DEBUG_WINDOW_TRACE) Slog.e(TAG,
821 "performSurfacePlacementInner exit: animating=" + mService.mAnimator.isAnimating());
822 }
823
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700824 private void applySurfaceChangesTransaction(boolean recoveringMemory, int defaultDw,
825 int defaultDh) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700826 mHoldScreenWindow = null;
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700827 mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700828
Andrii Kulian8ee72852017-03-10 10:36:45 -0800829 // TODO(multi-display): Support these features on secondary screens.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700830 if (mService.mWatermark != null) {
831 mService.mWatermark.positionSurface(defaultDw, defaultDh);
832 }
833 if (mService.mStrictModeFlash != null) {
834 mService.mStrictModeFlash.positionSurface(defaultDw, defaultDh);
835 }
836 if (mService.mCircularDisplayMask != null) {
Andrii Kulian8ee72852017-03-10 10:36:45 -0800837 mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh,
838 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700839 }
840 if (mService.mEmulatorDisplayOverlay != null) {
841 mService.mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh,
Andrii Kulian8ee72852017-03-10 10:36:45 -0800842 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700843 }
844
845 boolean focusDisplayed = false;
846
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700847 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700848 for (int j = 0; j < count; ++j) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700849 final DisplayContent dc = mChildren.get(j);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700850 focusDisplayed |= dc.applySurfaceChangesTransaction(recoveringMemory);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700851 }
852
853 if (focusDisplayed) {
854 mService.mH.sendEmptyMessage(REPORT_LOSING_FOCUS);
855 }
856
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700857 // Give the display manager a chance to adjust properties like display rotation if it needs
858 // to.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700859 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
860 }
861
862 /**
David Stevensf833ba92017-03-16 19:00:20 -0700863 * Handles resizing windows during surface placement.
864 *
865 * @return A set of any DisplayContent whose touch exclude region needs to be recalculated due
866 * to a tap-exclude window resizing, or null if no such DisplayContents were found.
867 */
868 private ArraySet<DisplayContent> handleResizingWindows() {
869 ArraySet<DisplayContent> touchExcludeRegionUpdateSet = null;
870 for (int i = mService.mResizingWindows.size() - 1; i >= 0; i--) {
871 WindowState win = mService.mResizingWindows.get(i);
872 if (win.mAppFreezing) {
873 // Don't remove this window until rotation has completed.
874 continue;
875 }
876 win.reportResized();
877 mService.mResizingWindows.remove(i);
878 if (WindowManagerService.excludeWindowTypeFromTapOutTask(win.mAttrs.type)) {
879 final DisplayContent dc = win.getDisplayContent();
880 if (touchExcludeRegionUpdateSet == null) {
881 touchExcludeRegionUpdateSet = new ArraySet<>();
882 }
883 touchExcludeRegionUpdateSet.add(dc);
884 }
885 }
886 return touchExcludeRegionUpdateSet;
887 }
888
889 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700890 * @param w WindowState this method is applied to.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700891 * @param obscured True if there is a window on top of this obscuring the display.
892 * @param syswin System window?
893 * @return True when the display contains content to show the user. When false, the display
894 * manager may choose to mirror or blank the display.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700895 */
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700896 boolean handleNotObscuredLocked(WindowState w, boolean obscured, boolean syswin) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700897 final WindowManager.LayoutParams attrs = w.mAttrs;
898 final int attrFlags = attrs.flags;
899 final boolean canBeSeen = w.isDisplayedLw();
900 final int privateflags = attrs.privateFlags;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700901 boolean displayHasContent = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700902
903 if (w.mHasSurface && canBeSeen) {
904 if ((attrFlags & FLAG_KEEP_SCREEN_ON) != 0) {
905 mHoldScreen = w.mSession;
906 mHoldScreenWindow = w;
907 } else if (DEBUG_KEEP_SCREEN_ON && w == mService.mLastWakeLockHoldingWindow) {
908 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked: " + w + " was holding "
Wale Ogunwale743fcc12017-06-06 21:36:50 +0000909 + "screen wakelock but no longer has FLAG_KEEP_SCREEN_ON!!! called by"
910 + Debug.getCallers(10));
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700911 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700912 if (!syswin && w.mAttrs.screenBrightness >= 0 && mScreenBrightness < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700913 mScreenBrightness = w.mAttrs.screenBrightness;
914 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700915 if (!syswin && w.mAttrs.userActivityTimeout >= 0 && mUserActivityTimeout < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700916 mUserActivityTimeout = w.mAttrs.userActivityTimeout;
917 }
918
919 final int type = attrs.type;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700920 // This function assumes that the contents of the default display are processed first
921 // before secondary displays.
922 final DisplayContent displayContent = w.getDisplayContent();
923 if (displayContent != null && displayContent.isDefaultDisplay) {
924 // While a dream or keyguard is showing, obscure ordinary application content on
925 // secondary displays (by forcibly enabling mirroring unless there is other content
926 // we want to show) but still allow opaque keyguard dialogs to be shown.
927 if (type == TYPE_DREAM || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
928 mObscureApplicationContentOnSecondaryDisplays = true;
929 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700930 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700931 } else if (displayContent != null &&
932 (!mObscureApplicationContentOnSecondaryDisplays
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700933 || (obscured && type == TYPE_KEYGUARD_DIALOG))) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700934 // Allow full screen keyguard presentation dialogs to be seen.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700935 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700936 }
937 if ((privateflags & PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE) != 0) {
938 mSustainedPerformanceModeCurrent = true;
939 }
940 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700941
942 return displayHasContent;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700943 }
944
945 boolean copyAnimToLayoutParams() {
946 boolean doRequest = false;
947
948 final int bulkUpdateParams = mService.mAnimator.mBulkUpdateParams;
949 if ((bulkUpdateParams & SET_UPDATE_ROTATION) != 0) {
950 mUpdateRotation = true;
951 doRequest = true;
952 }
953 if ((bulkUpdateParams & SET_WALLPAPER_MAY_CHANGE) != 0) {
954 mWallpaperMayChange = true;
955 doRequest = true;
956 }
957 if ((bulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0) {
958 mWallpaperForceHidingChanged = true;
959 doRequest = true;
960 }
961 if ((bulkUpdateParams & SET_ORIENTATION_CHANGE_COMPLETE) == 0) {
962 mOrientationChangeComplete = false;
963 } else {
964 mOrientationChangeComplete = true;
965 mLastWindowFreezeSource = mService.mAnimator.mLastWindowFreezeSource;
966 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
967 doRequest = true;
968 }
969 }
chaviw042059d2018-01-11 11:24:08 -0800970
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700971 if ((bulkUpdateParams & SET_WALLPAPER_ACTION_PENDING) != 0) {
972 mWallpaperActionPending = true;
973 }
974
975 return doRequest;
976 }
977
978 private static int toBrightnessOverride(float value) {
979 return (int)(value * PowerManager.BRIGHTNESS_ON);
980 }
981
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200982 private final class MyHandler extends Handler {
983
984 public MyHandler(Looper looper) {
985 super(looper);
986 }
987
988 @Override
989 public void handleMessage(Message msg) {
990 switch (msg.what) {
991 case SET_SCREEN_BRIGHTNESS_OVERRIDE:
992 mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(
993 msg.arg1);
994 break;
995 case SET_USER_ACTIVITY_TIMEOUT:
996 mService.mPowerManagerInternal.setUserActivityTimeoutOverrideFromWindowManager(
997 (Long) msg.obj);
998 break;
999 default:
1000 break;
1001 }
1002 }
1003 }
1004
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001005 void enableSurfaceTrace(ParcelFileDescriptor pfd) {
1006 final FileDescriptor fd = pfd.getFileDescriptor();
1007 if (mSurfaceTraceEnabled) {
1008 disableSurfaceTrace();
1009 }
1010 mSurfaceTraceEnabled = true;
1011 mRemoteEventTrace = new RemoteEventTrace(mService, fd);
1012 mSurfaceTraceFd = pfd;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001013 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1014 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001015 dc.enableSurfaceTrace(fd);
1016 }
1017 }
1018
1019 void disableSurfaceTrace() {
1020 mSurfaceTraceEnabled = false;
1021 mRemoteEventTrace = null;
1022 mSurfaceTraceFd = null;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001023 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1024 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001025 dc.disableSurfaceTrace();
1026 }
1027 }
1028
1029 void dumpDisplayContents(PrintWriter pw) {
1030 pw.println("WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)");
1031 if (mService.mDisplayReady) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001032 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001033 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001034 final DisplayContent displayContent = mChildren.get(i);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001035 displayContent.dump(pw, " ", true /* dumpAll */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001036 }
1037 } else {
1038 pw.println(" NO DISPLAY");
1039 }
1040 }
1041
1042 void dumpLayoutNeededDisplayIds(PrintWriter pw) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001043 if (!isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001044 return;
1045 }
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001046 pw.print(" mLayoutNeeded on displays=");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001047 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001048 for (int displayNdx = 0; displayNdx < count; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001049 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001050 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001051 pw.print(displayContent.getDisplayId());
1052 }
1053 }
1054 pw.println();
1055 }
1056
1057 void dumpWindowsNoHeader(PrintWriter pw, boolean dumpAll, ArrayList<WindowState> windows) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001058 final int[] index = new int[1];
1059 forAllWindows((w) -> {
1060 if (windows == null || windows.contains(w)) {
1061 pw.println(" Window #" + index[0] + " " + w + ":");
1062 w.dump(pw, " ", dumpAll || windows != null);
1063 index[0] = index[0] + 1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001064 }
Wale Ogunwaled1880962016-11-08 10:31:59 -08001065 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001066 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001067
Wale Ogunwale02319a62016-09-26 15:21:22 -07001068 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1069 pw.println(" All tokens:");
1070 for (int i = mChildren.size() - 1; i >= 0; --i) {
1071 mChildren.get(i).dumpTokens(pw, dumpAll);
1072 }
1073 }
1074
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001075 @CallSuper
1076 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02001077 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001078 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001079 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001080 if (mService.mDisplayReady) {
1081 final int count = mChildren.size();
1082 for (int i = 0; i < count; ++i) {
1083 final DisplayContent displayContent = mChildren.get(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001084 displayContent.writeToProto(proto, DISPLAYS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001085 }
1086 }
Adrian Roos4921ccf2017-09-28 16:54:06 +02001087 if (!trim) {
1088 forAllWindows((w) -> {
1089 w.writeIdentifierToProto(proto, WINDOWS);
1090 }, true);
1091 }
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001092 proto.end(token);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001093 }
1094
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001095 @Override
1096 String getName() {
1097 return "ROOT";
1098 }
Robert Carrb1579c82017-09-05 14:54:47 -07001099
1100 @Override
1101 void scheduleAnimation() {
1102 mService.scheduleAnimationLocked();
1103 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001104}