blob: 4008811b2dc843c1bfcde6303fdffcc4a232a92d [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;
89import static com.android.server.wm.WindowSurfacePlacer.SET_TURN_ON_SCREEN;
90import static com.android.server.wm.WindowSurfacePlacer.SET_UPDATE_ROTATION;
91import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_ACTION_PENDING;
92import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070093import static com.android.server.wm.proto.RootWindowContainerProto.DISPLAYS;
94import static com.android.server.wm.proto.RootWindowContainerProto.WINDOWS;
95import static com.android.server.wm.proto.RootWindowContainerProto.WINDOW_CONTAINER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070096
97/** Root {@link WindowContainer} for the device. */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -070098class RootWindowContainer extends WindowContainer<DisplayContent> {
Wale Ogunwalee05f5012016-09-16 16:27:29 -070099 private static final String TAG = TAG_WITH_CLASS_NAME ? "RootWindowContainer" : TAG_WM;
100
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200101 private static final int SET_SCREEN_BRIGHTNESS_OVERRIDE = 1;
102 private static final int SET_USER_ACTIVITY_TIMEOUT = 2;
103
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700104 WindowManagerService mService;
105
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700106 private boolean mWallpaperForceHidingChanged = false;
107 private Object mLastWindowFreezeSource = null;
108 private Session mHoldScreen = null;
109 private float mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700110 private long mUserActivityTimeout = -1;
111 private boolean mUpdateRotation = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700112 // Following variables are for debugging screen wakelock only.
113 // Last window that requires screen wakelock
114 WindowState mHoldScreenWindow = null;
115 // Last window that obscures all windows below
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700116 WindowState mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700117 // Only set while traversing the default display based on its content.
118 // Affects the behavior of mirroring on secondary displays.
119 private boolean mObscureApplicationContentOnSecondaryDisplays = false;
120
121 private boolean mSustainedPerformanceModeEnabled = false;
122 private boolean mSustainedPerformanceModeCurrent = false;
123
124 boolean mWallpaperMayChange = false;
Robert Carr11c26c22016-09-23 12:40:27 -0700125 // During an orientation change, we track whether all windows have rendered
126 // at the new orientation, and this will be false from changing orientation until that occurs.
127 // For seamless rotation cases this always stays true, as the windows complete their orientation
128 // changes 1 by 1 without disturbing global state.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700129 boolean mOrientationChangeComplete = true;
130 boolean mWallpaperActionPending = false;
131
132 private final ArrayList<Integer> mChangedStackList = new ArrayList();
133
134 // State for the RemoteSurfaceTrace system used in testing. If this is enabled SurfaceControl
135 // instances will be replaced with an instance that writes a binary representation of all
136 // commands to mSurfaceTraceFd.
137 boolean mSurfaceTraceEnabled;
138 ParcelFileDescriptor mSurfaceTraceFd;
139 RemoteEventTrace mRemoteEventTrace;
140
Wale Ogunwale0303c572016-10-20 10:16:29 -0700141 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700142
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200143 private final Handler mHandler;
144
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800145 private String mCloseSystemDialogsReason;
146 private final Consumer<WindowState> mCloseSystemDialogsConsumer = w -> {
147 if (w.mHasSurface) {
148 try {
149 w.mClient.closeSystemDialogs(mCloseSystemDialogsReason);
150 } catch (RemoteException e) {
151 }
152 }
153 };
154
155 private static final Consumer<WindowState> sRemoveReplacedWindowsConsumer = w -> {
156 final AppWindowToken aToken = w.mAppToken;
157 if (aToken != null) {
158 aToken.removeReplacedWindowIfNeeded(w);
159 }
160 };
161
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700162 RootWindowContainer(WindowManagerService service) {
163 mService = service;
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200164 mHandler = new MyHandler(service.mH.getLooper());
Wale Ogunwale0303c572016-10-20 10:16:29 -0700165 mWallpaperController = new WallpaperController(mService);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700166 }
167
168 WindowState computeFocusedWindow() {
David Stevens46939562017-03-24 13:04:00 -0700169 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700170 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700171 final WindowState win = dc.findFocusedWindow();
172 if (win != null) {
173 return win;
174 }
175 }
176 return null;
177 }
178
179 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800180 * Get an array with display ids ordered by focus priority - last items should be given
181 * focus first. Sparse array just maps position to displayId.
182 */
183 void getDisplaysInFocusOrder(SparseIntArray displaysInFocusOrder) {
184 displaysInFocusOrder.clear();
185
186 final int size = mChildren.size();
187 for (int i = 0; i < size; ++i) {
Andrii Kulian0214ed92017-05-16 13:44:05 -0700188 final DisplayContent displayContent = mChildren.get(i);
189 if (displayContent.isRemovalDeferred()) {
190 // Don't report displays that are going to be removed soon.
191 continue;
192 }
193 displaysInFocusOrder.put(i, displayContent.getDisplayId());
Andrii Kulian7fc22812016-12-28 13:04:11 -0800194 }
195 }
196
197 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700198 * Retrieve the DisplayContent for the specified displayId. Will create a new DisplayContent if
199 * there is a Display for the displayId.
200 *
201 * @param displayId The display the caller is interested in.
202 * @return The DisplayContent associated with displayId or null if there is no Display for it.
203 */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700204 DisplayContent getDisplayContentOrCreate(int displayId) {
205 DisplayContent dc = getDisplayContent(displayId);
206
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700207 if (dc == null) {
208 final Display display = mService.mDisplayManager.getDisplay(displayId);
209 if (display != null) {
Andrii Kulian325cac42017-04-20 16:02:47 -0700210 final long callingIdentity = Binder.clearCallingIdentity();
211 try {
212 dc = createDisplayContent(display);
213 } finally {
214 Binder.restoreCallingIdentity(callingIdentity);
215 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700216 }
217 }
218 return dc;
219 }
220
Wale Ogunwale02319a62016-09-26 15:21:22 -0700221 DisplayContent getDisplayContent(int displayId) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700222 for (int i = mChildren.size() - 1; i >= 0; --i) {
223 final DisplayContent current = mChildren.get(i);
224 if (current.getDisplayId() == displayId) {
225 return current;
226 }
227 }
228 return null;
229 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700230
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700231 private DisplayContent createDisplayContent(final Display display) {
Robert Carrb1579c82017-09-05 14:54:47 -0700232 final DisplayContent dc = new DisplayContent(display, mService,
Wale Ogunwale0303c572016-10-20 10:16:29 -0700233 mWallpaperController);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700234 final int displayId = display.getDisplayId();
235
236 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Adding display=" + display);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700237
238 final DisplayInfo displayInfo = dc.getDisplayInfo();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700239 final Rect rect = new Rect();
240 mService.mDisplaySettings.getOverscanLocked(displayInfo.name, displayInfo.uniqueId, rect);
241 displayInfo.overscanLeft = rect.left;
242 displayInfo.overscanTop = rect.top;
243 displayInfo.overscanRight = rect.right;
244 displayInfo.overscanBottom = rect.bottom;
245 if (mService.mDisplayManagerInternal != null) {
246 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(
247 displayId, displayInfo);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000248 dc.configureDisplayPolicy();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700249
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700250 // Tap Listeners are supported for:
251 // 1. All physical displays (multi-display).
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -0700252 // 2. VirtualDisplays on VR, AA (and everything else).
253 if (mService.canDispatchPointerEvents()) {
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700254 if (DEBUG_DISPLAY) {
255 Slog.d(TAG,
256 "Registering PointerEventListener for DisplayId: " + displayId);
257 }
258 dc.mTapDetector = new TaskTapPointerEventListener(mService, dc);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700259 mService.registerPointerEventListener(dc.mTapDetector);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700260 if (displayId == DEFAULT_DISPLAY) {
261 mService.registerPointerEventListener(mService.mMousePositionTracker);
262 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700263 }
264 }
265
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700266 return dc;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700267 }
268
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700269 boolean isLayoutNeeded() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700270 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700271 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700272 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700273 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700274 return true;
275 }
276 }
277 return false;
278 }
279
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000280 void getWindowsByName(ArrayList<WindowState> output, String name) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700281 int objectId = 0;
282 // See if this is an object ID.
283 try {
284 objectId = Integer.parseInt(name, 16);
285 name = null;
286 } catch (RuntimeException e) {
287 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800288
289 getWindowsByName(output, name, objectId);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700290 }
291
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000292 private void getWindowsByName(ArrayList<WindowState> output, String name, int objectId) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800293 forAllWindows((w) -> {
294 if (name != null) {
295 if (w.mAttrs.getTitle().toString().contains(name)) {
296 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700297 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800298 } else if (System.identityHashCode(w) == objectId) {
299 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700300 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800301 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700302 }
303
Wale Ogunwale02319a62016-09-26 15:21:22 -0700304 /**
305 * Returns the app window token for the input binder if it exist in the system.
306 * NOTE: Only one AppWindowToken is allowed to exist in the system for a binder token, since
307 * AppWindowToken represents an activity which can only exist on one display.
308 */
309 AppWindowToken getAppWindowToken(IBinder binder) {
310 for (int i = mChildren.size() - 1; i >= 0; --i) {
311 final DisplayContent dc = mChildren.get(i);
312 final AppWindowToken atoken = dc.getAppWindowToken(binder);
313 if (atoken != null) {
314 return atoken;
315 }
316 }
317 return null;
318 }
319
320 /** Returns the display object the input window token is currently mapped on. */
321 DisplayContent getWindowTokenDisplay(WindowToken token) {
322 if (token == null) {
323 return null;
324 }
325
326 for (int i = mChildren.size() - 1; i >= 0; --i) {
327 final DisplayContent dc = mChildren.get(i);
328 final WindowToken current = dc.getWindowToken(token.token);
329 if (current == token) {
330 return dc;
331 }
332 }
333
334 return null;
335 }
336
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700337 /**
338 * Set new display override config and return array of ids of stacks that were changed during
339 * update. If called for the default display, global configuration will also be updated.
340 */
341 int[] setDisplayOverrideConfigurationIfNeeded(Configuration newConfiguration, int displayId) {
342 final DisplayContent displayContent = getDisplayContent(displayId);
343 if (displayContent == null) {
344 throw new IllegalArgumentException("Display not found for id: " + displayId);
345 }
346
347 final Configuration currentConfig = displayContent.getOverrideConfiguration();
348 final boolean configChanged = currentConfig.diff(newConfiguration) != 0;
349 if (!configChanged) {
350 return null;
351 }
Andrii Kuliand68501e2017-01-10 22:57:27 -0800352 displayContent.onOverrideConfigurationChanged(newConfiguration);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700353
354 if (displayId == DEFAULT_DISPLAY) {
355 // Override configuration of the default display duplicates global config. In this case
356 // we also want to update the global config.
357 return setGlobalConfigurationIfNeeded(newConfiguration);
358 } else {
359 return updateStackBoundsAfterConfigChange(displayId);
360 }
361 }
362
363 private int[] setGlobalConfigurationIfNeeded(Configuration newConfiguration) {
Andrii Kulian441e4492016-09-29 15:25:00 -0700364 final boolean configChanged = getConfiguration().diff(newConfiguration) != 0;
365 if (!configChanged) {
366 return null;
367 }
368 onConfigurationChanged(newConfiguration);
369 return updateStackBoundsAfterConfigChange();
370 }
371
372 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700373 public void onConfigurationChanged(Configuration newParentConfig) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700374 prepareFreezingTaskBounds();
Andrii Kulian441e4492016-09-29 15:25:00 -0700375 super.onConfigurationChanged(newParentConfig);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700376
377 mService.mPolicy.onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700378 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700379
Andrii Kulian441e4492016-09-29 15:25:00 -0700380 /**
381 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
382 * bounds were updated.
383 */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700384 private int[] updateStackBoundsAfterConfigChange() {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700385 mChangedStackList.clear();
386
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700387 final int numDisplays = mChildren.size();
388 for (int i = 0; i < numDisplays; ++i) {
389 final DisplayContent dc = mChildren.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700390 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700391 }
392
393 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
394 }
395
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700396 /** Same as {@link #updateStackBoundsAfterConfigChange()} but only for a specific display. */
397 private int[] updateStackBoundsAfterConfigChange(int displayId) {
398 mChangedStackList.clear();
399
400 final DisplayContent dc = getDisplayContent(displayId);
401 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
402
403 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
404 }
405
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700406 private void prepareFreezingTaskBounds() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700407 for (int i = mChildren.size() - 1; i >= 0; i--) {
408 mChildren.get(i).prepareFreezingTaskBounds();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700409 }
410 }
411
Wale Ogunwale68278562017-09-23 17:13:55 -0700412 TaskStack getStack(int windowingMode, int activityType) {
413 for (int i = mChildren.size() - 1; i >= 0; i--) {
414 final DisplayContent dc = mChildren.get(i);
415 final TaskStack stack = dc.getStack(windowingMode, activityType);
416 if (stack != null) {
417 return stack;
418 }
419 }
420 return null;
421 }
422
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700423 void setSecureSurfaceState(int userId, boolean disabled) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800424 forAllWindows((w) -> {
425 if (w.mHasSurface && userId == UserHandle.getUserId(w.mOwnerUid)) {
426 w.mWinAnimator.setSecureLocked(disabled);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700427 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800428 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700429 }
430
431 void updateAppOpsState() {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800432 forAllWindows((w) -> {
433 if (w.mAppOp == OP_NONE) {
434 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700435 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800436 final int mode = mService.mAppOps.checkOpNoThrow(w.mAppOp, w.getOwningUid(),
437 w.getOwningPackage());
438 w.setAppOpVisibilityLw(mode == MODE_ALLOWED || mode == MODE_DEFAULT);
439 }, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700440 }
441
442 boolean canShowStrictModeViolation(int pid) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800443 final WindowState win = getWindow((w) -> w.mSession.mPid == pid && w.isVisibleLw());
444 return win != null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700445 }
446
447 void closeSystemDialogs(String reason) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800448 mCloseSystemDialogsReason = reason;
449 forAllWindows(mCloseSystemDialogsConsumer, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700450 }
451
452 void removeReplacedWindows() {
453 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION removeReplacedWindows");
454 mService.openSurfaceTransaction();
455 try {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800456 forAllWindows(sRemoveReplacedWindowsConsumer, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700457 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200458 mService.closeSurfaceTransaction("removeReplacedWindows");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700459 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION removeReplacedWindows");
460 }
461 }
462
463 boolean hasPendingLayoutChanges(WindowAnimator animator) {
464 boolean hasChanges = false;
465
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700466 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700467 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700468 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700469 final int pendingChanges = animator.getPendingLayoutChanges(dc.getDisplayId());
470 if ((pendingChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
471 animator.mBulkUpdateParams |= SET_WALLPAPER_ACTION_PENDING;
472 }
473 if (pendingChanges != 0) {
474 hasChanges = true;
475 }
476 }
477
478 return hasChanges;
479 }
480
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700481 boolean reclaimSomeSurfaceMemory(WindowStateAnimator winAnimator, String operation,
482 boolean secure) {
483 final WindowSurfaceController surfaceController = winAnimator.mSurfaceController;
484 boolean leakedSurface = false;
485 boolean killedApps = false;
486
487 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, winAnimator.mWin.toString(),
488 winAnimator.mSession.mPid, operation);
489
490 final long callingIdentity = Binder.clearCallingIdentity();
491 try {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700492 // There was some problem...first, do a sanity check of the window list to make sure
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700493 // we haven't left any dangling surfaces around.
494
495 Slog.i(TAG_WM, "Out of memory for surface! Looking for leaks...");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700496 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700497 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700498 leakedSurface |= mChildren.get(displayNdx).destroyLeakedSurfaces();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700499 }
500
501 if (!leakedSurface) {
502 Slog.w(TAG_WM, "No leaked surfaces; killing applications!");
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700503 final SparseIntArray pidCandidates = new SparseIntArray();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700504 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800505 mChildren.get(displayNdx).forAllWindows((w) -> {
506 if (mService.mForceRemoves.contains(w)) {
507 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700508 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800509 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700510 if (wsa.mSurfaceController != null) {
511 pidCandidates.append(wsa.mSession.mPid, wsa.mSession.mPid);
512 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800513 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700514
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700515 if (pidCandidates.size() > 0) {
516 int[] pids = new int[pidCandidates.size()];
517 for (int i = 0; i < pids.length; i++) {
518 pids[i] = pidCandidates.keyAt(i);
519 }
520 try {
521 if (mService.mActivityManager.killPids(pids, "Free memory", secure)) {
522 killedApps = true;
523 }
524 } catch (RemoteException e) {
525 }
526 }
527 }
528 }
529
530 if (leakedSurface || killedApps) {
531 // We managed to reclaim some memory, so get rid of the trouble surface and ask the
532 // app to request another one.
533 Slog.w(TAG_WM,
534 "Looks like we have reclaimed some memory, clearing surface for retry.");
535 if (surfaceController != null) {
536 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) logSurface(winAnimator.mWin,
537 "RECOVER DESTROY", false);
538 winAnimator.destroySurface();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800539 if (winAnimator.mWin.mAppToken != null
540 && winAnimator.mWin.mAppToken.getController() != null) {
541 winAnimator.mWin.mAppToken.getController().removeStartingWindow();
542 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700543 }
544
545 try {
546 winAnimator.mWin.mClient.dispatchGetNewSurface();
547 } catch (RemoteException e) {
548 }
549 }
550 } finally {
551 Binder.restoreCallingIdentity(callingIdentity);
552 }
553
554 return leakedSurface || killedApps;
555 }
556
557 // "Something has changed! Let's make it correct now."
558 // TODO: Super crazy long method that should be broken down...
559 void performSurfacePlacement(boolean recoveringMemory) {
560 if (DEBUG_WINDOW_TRACE) Slog.v(TAG, "performSurfacePlacementInner: entry. Called by "
561 + Debug.getCallers(3));
562
563 int i;
564 boolean updateInputWindowsNeeded = false;
565
566 if (mService.mFocusMayChange) {
567 mService.mFocusMayChange = false;
568 updateInputWindowsNeeded = mService.updateFocusedWindowLocked(
569 UPDATE_FOCUS_WILL_PLACE_SURFACES, false /*updateInputWindows*/);
570 }
571
572 // Initialize state of exiting tokens.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700573 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700574 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700575 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800576 displayContent.setExitingTokensHasVisible(false);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700577 }
578
579 mHoldScreen = null;
580 mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700581 mUserActivityTimeout = -1;
582 mObscureApplicationContentOnSecondaryDisplays = false;
583 mSustainedPerformanceModeCurrent = false;
584 mService.mTransactionSequence++;
585
586 // TODO(multi-display):
587 final DisplayContent defaultDisplay = mService.getDefaultDisplayContentLocked();
588 final DisplayInfo defaultInfo = defaultDisplay.getDisplayInfo();
589 final int defaultDw = defaultInfo.logicalWidth;
590 final int defaultDh = defaultInfo.logicalHeight;
591
592 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
593 ">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
594 mService.openSurfaceTransaction();
595 try {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700596 applySurfaceChangesTransaction(recoveringMemory, defaultDw, defaultDh);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700597 } catch (RuntimeException e) {
598 Slog.wtf(TAG, "Unhandled exception in Window Manager", e);
599 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200600 mService.closeSurfaceTransaction("performLayoutAndPlaceSurfaces");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700601 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
602 "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
603 }
604
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700605 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
606
607 // If we are ready to perform an app transition, check through all of the app tokens to be
608 // shown and see if they are ready to go.
609 if (mService.mAppTransition.isReady()) {
610 defaultDisplay.pendingLayoutChanges |=
Wale Ogunwale0303c572016-10-20 10:16:29 -0700611 surfacePlacer.handleAppTransitionReadyLocked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700612 if (DEBUG_LAYOUT_REPEATS)
613 surfacePlacer.debugLayoutRepeats("after handleAppTransitionReadyLocked",
614 defaultDisplay.pendingLayoutChanges);
615 }
616
617 if (!mService.mAnimator.mAppWindowAnimating && mService.mAppTransition.isRunning()) {
618 // We have finished the animation of an app transition. To do this, we have delayed a
619 // lot of operations like showing and hiding apps, moving apps in Z-order, etc. The app
620 // token list reflects the correct Z-order, but the window list may now be out of sync
621 // with it. So here we will just rebuild the entire app window list. Fun!
622 defaultDisplay.pendingLayoutChanges |=
623 mService.handleAnimatingStoppedAndTransitionLocked();
624 if (DEBUG_LAYOUT_REPEATS)
625 surfacePlacer.debugLayoutRepeats("after handleAnimStopAndXitionLock",
626 defaultDisplay.pendingLayoutChanges);
627 }
628
629 if (mWallpaperForceHidingChanged && defaultDisplay.pendingLayoutChanges == 0
630 && !mService.mAppTransition.isReady()) {
631 // At this point, there was a window with a wallpaper that was force hiding other
632 // windows behind it, but now it is going away. This may be simple -- just animate away
633 // the wallpaper and its window -- or it may be hard -- the wallpaper now needs to be
634 // shown behind something that was hidden.
635 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
636 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
637 "after animateAwayWallpaperLocked", defaultDisplay.pendingLayoutChanges);
638 }
639 mWallpaperForceHidingChanged = false;
640
641 if (mWallpaperMayChange) {
642 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Wallpaper may change! Adjusting");
643 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
644 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("WallpaperMayChange",
645 defaultDisplay.pendingLayoutChanges);
646 }
647
648 if (mService.mFocusMayChange) {
649 mService.mFocusMayChange = false;
650 if (mService.updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
651 false /*updateInputWindows*/)) {
652 updateInputWindowsNeeded = true;
653 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
654 }
655 }
656
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700657 if (isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700658 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
659 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("mLayoutNeeded",
660 defaultDisplay.pendingLayoutChanges);
661 }
662
David Stevensf833ba92017-03-16 19:00:20 -0700663 final ArraySet<DisplayContent> touchExcludeRegionUpdateDisplays = handleResizingWindows();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700664
665 if (DEBUG_ORIENTATION && mService.mDisplayFrozen) Slog.v(TAG,
666 "With display frozen, orientationChangeComplete=" + mOrientationChangeComplete);
667 if (mOrientationChangeComplete) {
668 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
669 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
670 mService.mLastFinishedFreezeSource = mLastWindowFreezeSource;
671 mService.mH.removeMessages(WINDOW_FREEZE_TIMEOUT);
672 }
673 mService.stopFreezingDisplayLocked();
674 }
675
676 // Destroy the surface of any windows that are no longer visible.
677 boolean wallpaperDestroyed = false;
678 i = mService.mDestroySurface.size();
679 if (i > 0) {
680 do {
681 i--;
682 WindowState win = mService.mDestroySurface.get(i);
683 win.mDestroying = false;
684 if (mService.mInputMethodWindow == win) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000685 mService.setInputMethodWindowLocked(null);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700686 }
Wale Ogunwale0303c572016-10-20 10:16:29 -0700687 if (win.getDisplayContent().mWallpaperController.isWallpaperTarget(win)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700688 wallpaperDestroyed = true;
689 }
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200690 win.destroySurfaceUnchecked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700691 } while (i > 0);
692 mService.mDestroySurface.clear();
693 }
694
695 // Time to remove any exiting tokens?
696 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700697 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800698 displayContent.removeExistingTokensIfPossible();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700699 }
700
701 if (wallpaperDestroyed) {
702 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700703 defaultDisplay.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700704 }
705
706 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700707 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700708 if (displayContent.pendingLayoutChanges != 0) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700709 displayContent.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700710 }
711 }
712
713 // Finally update all input windows now that the window changes have stabilized.
714 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
715
716 mService.setHoldScreenLocked(mHoldScreen);
717 if (!mService.mDisplayFrozen) {
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200718 final int brightness = mScreenBrightness < 0 || mScreenBrightness > 1.0f
719 ? -1 : toBrightnessOverride(mScreenBrightness);
720
721 // Post these on a handler such that we don't call into power manager service while
722 // holding the window manager lock to avoid lock contention with power manager lock.
723 mHandler.obtainMessage(SET_SCREEN_BRIGHTNESS_OVERRIDE, brightness, 0).sendToTarget();
724 mHandler.obtainMessage(SET_USER_ACTIVITY_TIMEOUT, mUserActivityTimeout).sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700725 }
726
727 if (mSustainedPerformanceModeCurrent != mSustainedPerformanceModeEnabled) {
728 mSustainedPerformanceModeEnabled = mSustainedPerformanceModeCurrent;
729 mService.mPowerManagerInternal.powerHint(
Ruchi Kandoi0d434042016-10-03 09:12:02 -0700730 PowerHint.SUSTAINED_PERFORMANCE,
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700731 (mSustainedPerformanceModeEnabled ? 1 : 0));
732 }
733
734 if (mService.mTurnOnScreen) {
735 if (mService.mAllowTheaterModeWakeFromLayout
736 || Settings.Global.getInt(mService.mContext.getContentResolver(),
737 Settings.Global.THEATER_MODE_ON, 0) == 0) {
738 if (DEBUG_VISIBILITY || DEBUG_POWER) {
739 Slog.v(TAG, "Turning screen on after layout!");
740 }
741 mService.mPowerManager.wakeUp(SystemClock.uptimeMillis(),
742 "android.server.wm:TURN_ON");
743 }
744 mService.mTurnOnScreen = false;
745 }
746
747 if (mUpdateRotation) {
748 if (DEBUG_ORIENTATION) Slog.d(TAG, "Performing post-rotate rotation");
Andrii Kulian48146fd2017-08-07 11:22:50 -0700749 // TODO(multi-display): Update rotation for different displays separately.
750 final int displayId = defaultDisplay.getDisplayId();
751 if (defaultDisplay.updateRotationUnchecked(false /* inTransaction */)) {
752 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, displayId).sendToTarget();
753 } else {
754 mUpdateRotation = false;
755 }
756 // Update rotation of VR virtual display separately. Currently this is the only kind of
757 // secondary display that can be rotated because of the single-display limitations in
758 // PhoneWindowManager.
759 final DisplayContent vrDisplay = mService.mVr2dDisplayId != INVALID_DISPLAY
760 ? getDisplayContent(mService.mVr2dDisplayId) : null;
761 if (vrDisplay != null && vrDisplay.updateRotationUnchecked(false /* inTransaction */)) {
762 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mService.mVr2dDisplayId)
763 .sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700764 }
765 }
766
767 if (mService.mWaitingForDrawnCallback != null ||
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700768 (mOrientationChangeComplete && !defaultDisplay.isLayoutNeeded()
769 && !mUpdateRotation)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700770 mService.checkDrawnWindowsLocked();
771 }
772
773 final int N = mService.mPendingRemove.size();
774 if (N > 0) {
775 if (mService.mPendingRemoveTmp.length < N) {
776 mService.mPendingRemoveTmp = new WindowState[N+10];
777 }
778 mService.mPendingRemove.toArray(mService.mPendingRemoveTmp);
779 mService.mPendingRemove.clear();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700780 ArrayList<DisplayContent> displayList = new ArrayList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700781 for (i = 0; i < N; i++) {
782 final WindowState w = mService.mPendingRemoveTmp[i];
783 w.removeImmediately();
784 final DisplayContent displayContent = w.getDisplayContent();
785 if (displayContent != null && !displayList.contains(displayContent)) {
786 displayList.add(displayContent);
787 }
788 }
789
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700790 for (int j = displayList.size() - 1; j >= 0; --j) {
791 final DisplayContent dc = displayList.get(j);
792 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700793 }
794 }
795
796 // Remove all deferred displays stacks, tasks, and activities.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700797 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
798 mChildren.get(displayNdx).checkCompleteDeferredRemoval();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700799 }
800
801 if (updateInputWindowsNeeded) {
802 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
803 }
David Stevensee9e2772017-02-09 16:30:27 -0800804 mService.setFocusTaskRegionLocked(null);
David Stevensf833ba92017-03-16 19:00:20 -0700805 if (touchExcludeRegionUpdateDisplays != null) {
806 final DisplayContent focusedDc = mService.mFocusedApp != null
807 ? mService.mFocusedApp.getDisplayContent() : null;
808 for (DisplayContent dc : touchExcludeRegionUpdateDisplays) {
809 // The focused DisplayContent was recalcuated in setFocusTaskRegionLocked
810 if (focusedDc != dc) {
811 dc.setTouchExcludeRegion(null /* focusedTask */);
812 }
813 }
814 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700815
816 // Check to see if we are now in a state where the screen should
817 // be enabled, because the window obscured flags have changed.
818 mService.enableScreenIfNeededLocked();
819
820 mService.scheduleAnimationLocked();
821 mService.mWindowPlacerLocked.destroyPendingSurfaces();
822
823 if (DEBUG_WINDOW_TRACE) Slog.e(TAG,
824 "performSurfacePlacementInner exit: animating=" + mService.mAnimator.isAnimating());
825 }
826
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700827 private void applySurfaceChangesTransaction(boolean recoveringMemory, int defaultDw,
828 int defaultDh) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700829 mHoldScreenWindow = null;
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700830 mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700831
Andrii Kulian8ee72852017-03-10 10:36:45 -0800832 // TODO(multi-display): Support these features on secondary screens.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700833 if (mService.mWatermark != null) {
834 mService.mWatermark.positionSurface(defaultDw, defaultDh);
835 }
836 if (mService.mStrictModeFlash != null) {
837 mService.mStrictModeFlash.positionSurface(defaultDw, defaultDh);
838 }
839 if (mService.mCircularDisplayMask != null) {
Andrii Kulian8ee72852017-03-10 10:36:45 -0800840 mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh,
841 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700842 }
843 if (mService.mEmulatorDisplayOverlay != null) {
844 mService.mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh,
Andrii Kulian8ee72852017-03-10 10:36:45 -0800845 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700846 }
847
848 boolean focusDisplayed = false;
849
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700850 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700851 for (int j = 0; j < count; ++j) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700852 final DisplayContent dc = mChildren.get(j);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700853 focusDisplayed |= dc.applySurfaceChangesTransaction(recoveringMemory);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700854 }
855
856 if (focusDisplayed) {
857 mService.mH.sendEmptyMessage(REPORT_LOSING_FOCUS);
858 }
859
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700860 // Give the display manager a chance to adjust properties like display rotation if it needs
861 // to.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700862 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
863 }
864
865 /**
David Stevensf833ba92017-03-16 19:00:20 -0700866 * Handles resizing windows during surface placement.
867 *
868 * @return A set of any DisplayContent whose touch exclude region needs to be recalculated due
869 * to a tap-exclude window resizing, or null if no such DisplayContents were found.
870 */
871 private ArraySet<DisplayContent> handleResizingWindows() {
872 ArraySet<DisplayContent> touchExcludeRegionUpdateSet = null;
873 for (int i = mService.mResizingWindows.size() - 1; i >= 0; i--) {
874 WindowState win = mService.mResizingWindows.get(i);
875 if (win.mAppFreezing) {
876 // Don't remove this window until rotation has completed.
877 continue;
878 }
879 win.reportResized();
880 mService.mResizingWindows.remove(i);
881 if (WindowManagerService.excludeWindowTypeFromTapOutTask(win.mAttrs.type)) {
882 final DisplayContent dc = win.getDisplayContent();
883 if (touchExcludeRegionUpdateSet == null) {
884 touchExcludeRegionUpdateSet = new ArraySet<>();
885 }
886 touchExcludeRegionUpdateSet.add(dc);
887 }
888 }
889 return touchExcludeRegionUpdateSet;
890 }
891
892 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700893 * @param w WindowState this method is applied to.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700894 * @param obscured True if there is a window on top of this obscuring the display.
895 * @param syswin System window?
896 * @return True when the display contains content to show the user. When false, the display
897 * manager may choose to mirror or blank the display.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700898 */
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700899 boolean handleNotObscuredLocked(WindowState w, boolean obscured, boolean syswin) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700900 final WindowManager.LayoutParams attrs = w.mAttrs;
901 final int attrFlags = attrs.flags;
902 final boolean canBeSeen = w.isDisplayedLw();
903 final int privateflags = attrs.privateFlags;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700904 boolean displayHasContent = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700905
906 if (w.mHasSurface && canBeSeen) {
907 if ((attrFlags & FLAG_KEEP_SCREEN_ON) != 0) {
908 mHoldScreen = w.mSession;
909 mHoldScreenWindow = w;
910 } else if (DEBUG_KEEP_SCREEN_ON && w == mService.mLastWakeLockHoldingWindow) {
911 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked: " + w + " was holding "
Wale Ogunwale743fcc12017-06-06 21:36:50 +0000912 + "screen wakelock but no longer has FLAG_KEEP_SCREEN_ON!!! called by"
913 + Debug.getCallers(10));
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700914 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700915 if (!syswin && w.mAttrs.screenBrightness >= 0 && mScreenBrightness < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700916 mScreenBrightness = w.mAttrs.screenBrightness;
917 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700918 if (!syswin && w.mAttrs.userActivityTimeout >= 0 && mUserActivityTimeout < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700919 mUserActivityTimeout = w.mAttrs.userActivityTimeout;
920 }
921
922 final int type = attrs.type;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700923 // This function assumes that the contents of the default display are processed first
924 // before secondary displays.
925 final DisplayContent displayContent = w.getDisplayContent();
926 if (displayContent != null && displayContent.isDefaultDisplay) {
927 // While a dream or keyguard is showing, obscure ordinary application content on
928 // secondary displays (by forcibly enabling mirroring unless there is other content
929 // we want to show) but still allow opaque keyguard dialogs to be shown.
930 if (type == TYPE_DREAM || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
931 mObscureApplicationContentOnSecondaryDisplays = true;
932 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700933 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700934 } else if (displayContent != null &&
935 (!mObscureApplicationContentOnSecondaryDisplays
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700936 || (obscured && type == TYPE_KEYGUARD_DIALOG))) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700937 // Allow full screen keyguard presentation dialogs to be seen.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700938 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700939 }
940 if ((privateflags & PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE) != 0) {
941 mSustainedPerformanceModeCurrent = true;
942 }
943 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700944
945 return displayHasContent;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700946 }
947
948 boolean copyAnimToLayoutParams() {
949 boolean doRequest = false;
950
951 final int bulkUpdateParams = mService.mAnimator.mBulkUpdateParams;
952 if ((bulkUpdateParams & SET_UPDATE_ROTATION) != 0) {
953 mUpdateRotation = true;
954 doRequest = true;
955 }
956 if ((bulkUpdateParams & SET_WALLPAPER_MAY_CHANGE) != 0) {
957 mWallpaperMayChange = true;
958 doRequest = true;
959 }
960 if ((bulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0) {
961 mWallpaperForceHidingChanged = true;
962 doRequest = true;
963 }
964 if ((bulkUpdateParams & SET_ORIENTATION_CHANGE_COMPLETE) == 0) {
965 mOrientationChangeComplete = false;
966 } else {
967 mOrientationChangeComplete = true;
968 mLastWindowFreezeSource = mService.mAnimator.mLastWindowFreezeSource;
969 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
970 doRequest = true;
971 }
972 }
973 if ((bulkUpdateParams & SET_TURN_ON_SCREEN) != 0) {
974 mService.mTurnOnScreen = true;
975 }
976 if ((bulkUpdateParams & SET_WALLPAPER_ACTION_PENDING) != 0) {
977 mWallpaperActionPending = true;
978 }
979
980 return doRequest;
981 }
982
983 private static int toBrightnessOverride(float value) {
984 return (int)(value * PowerManager.BRIGHTNESS_ON);
985 }
986
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200987 private final class MyHandler extends Handler {
988
989 public MyHandler(Looper looper) {
990 super(looper);
991 }
992
993 @Override
994 public void handleMessage(Message msg) {
995 switch (msg.what) {
996 case SET_SCREEN_BRIGHTNESS_OVERRIDE:
997 mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(
998 msg.arg1);
999 break;
1000 case SET_USER_ACTIVITY_TIMEOUT:
1001 mService.mPowerManagerInternal.setUserActivityTimeoutOverrideFromWindowManager(
1002 (Long) msg.obj);
1003 break;
1004 default:
1005 break;
1006 }
1007 }
1008 }
1009
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001010 void enableSurfaceTrace(ParcelFileDescriptor pfd) {
1011 final FileDescriptor fd = pfd.getFileDescriptor();
1012 if (mSurfaceTraceEnabled) {
1013 disableSurfaceTrace();
1014 }
1015 mSurfaceTraceEnabled = true;
1016 mRemoteEventTrace = new RemoteEventTrace(mService, fd);
1017 mSurfaceTraceFd = pfd;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001018 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1019 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001020 dc.enableSurfaceTrace(fd);
1021 }
1022 }
1023
1024 void disableSurfaceTrace() {
1025 mSurfaceTraceEnabled = false;
1026 mRemoteEventTrace = null;
1027 mSurfaceTraceFd = null;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001028 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1029 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001030 dc.disableSurfaceTrace();
1031 }
1032 }
1033
1034 void dumpDisplayContents(PrintWriter pw) {
1035 pw.println("WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)");
1036 if (mService.mDisplayReady) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001037 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001038 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001039 final DisplayContent displayContent = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001040 displayContent.dump(" ", pw);
1041 }
1042 } else {
1043 pw.println(" NO DISPLAY");
1044 }
1045 }
1046
1047 void dumpLayoutNeededDisplayIds(PrintWriter pw) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001048 if (!isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001049 return;
1050 }
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001051 pw.print(" mLayoutNeeded on displays=");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001052 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001053 for (int displayNdx = 0; displayNdx < count; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001054 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001055 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001056 pw.print(displayContent.getDisplayId());
1057 }
1058 }
1059 pw.println();
1060 }
1061
1062 void dumpWindowsNoHeader(PrintWriter pw, boolean dumpAll, ArrayList<WindowState> windows) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001063 final int[] index = new int[1];
1064 forAllWindows((w) -> {
1065 if (windows == null || windows.contains(w)) {
1066 pw.println(" Window #" + index[0] + " " + w + ":");
1067 w.dump(pw, " ", dumpAll || windows != null);
1068 index[0] = index[0] + 1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001069 }
Wale Ogunwaled1880962016-11-08 10:31:59 -08001070 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001071 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001072
Wale Ogunwale02319a62016-09-26 15:21:22 -07001073 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1074 pw.println(" All tokens:");
1075 for (int i = mChildren.size() - 1; i >= 0; --i) {
1076 mChildren.get(i).dumpTokens(pw, dumpAll);
1077 }
1078 }
1079
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001080 @CallSuper
1081 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02001082 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001083 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001084 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001085 if (mService.mDisplayReady) {
1086 final int count = mChildren.size();
1087 for (int i = 0; i < count; ++i) {
1088 final DisplayContent displayContent = mChildren.get(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001089 displayContent.writeToProto(proto, DISPLAYS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001090 }
1091 }
Adrian Roos4921ccf2017-09-28 16:54:06 +02001092 if (!trim) {
1093 forAllWindows((w) -> {
1094 w.writeIdentifierToProto(proto, WINDOWS);
1095 }, true);
1096 }
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001097 proto.end(token);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001098 }
1099
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001100 @Override
1101 String getName() {
1102 return "ROOT";
1103 }
Robert Carrb1579c82017-09-05 14:54:47 -07001104
1105 @Override
1106 void scheduleAnimation() {
1107 mService.scheduleAnimationLocked();
1108 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001109}