blob: fd965fbc8ac7fb9860101a2694e66dd0a95719c7 [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;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070032import android.os.UserHandle;
David Stevensf833ba92017-03-16 19:00:20 -070033import android.util.ArraySet;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070034import android.util.EventLog;
35import android.util.Slog;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070036import android.util.SparseIntArray;
Steven Timotiusaf03df62017-07-18 16:56:43 -070037import android.util.proto.ProtoOutputStream;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070038import android.view.Display;
39import android.view.DisplayInfo;
Robert Carrae606b42018-02-15 15:36:23 -080040import android.view.SurfaceControl;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070041import android.view.WindowManager;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020042
Wale Ogunwalee05f5012016-09-16 16:27:29 -070043import com.android.internal.util.ArrayUtils;
44import com.android.server.EventLogTags;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070045
46import java.io.FileDescriptor;
47import java.io.PrintWriter;
48import java.util.ArrayList;
Bryce Leeaea60d22018-01-31 14:42:15 -080049import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080050import java.util.function.Consumer;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070051
Wale Ogunwale02319a62016-09-26 15:21:22 -070052import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian48146fd2017-08-07 11:22:50 -070053import static android.view.Display.INVALID_DISPLAY;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070054import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070055import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070056import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE;
57import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
58import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
Jorim Jaggi879ff722016-11-04 18:08:17 -070059
Adrian Roose99bc052017-11-20 17:55:31 +010060import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
61import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
62import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070063import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
64import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_KEEP_SCREEN_ON;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
66import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
68import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_TRACE;
69import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
70import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
71import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
72import static com.android.server.wm.WindowManagerDebugConfig.TAG_KEEP_SCREEN_ON;
73import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
74import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070075import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
76import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070077import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
78import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
79import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_NONE;
80import static com.android.server.wm.WindowManagerService.H.WINDOW_FREEZE_TIMEOUT;
81import static com.android.server.wm.WindowManagerService.logSurface;
82import static com.android.server.wm.WindowSurfacePlacer.SET_FORCE_HIDING_CHANGED;
83import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070084import static com.android.server.wm.WindowSurfacePlacer.SET_UPDATE_ROTATION;
85import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_ACTION_PENDING;
86import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Yi Jin6c6e9ca2018-03-20 16:53:35 -070087import static com.android.server.wm.RootWindowContainerProto.DISPLAYS;
88import static com.android.server.wm.RootWindowContainerProto.WINDOWS;
89import static com.android.server.wm.RootWindowContainerProto.WINDOW_CONTAINER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070090
91/** Root {@link WindowContainer} for the device. */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -070092class RootWindowContainer extends WindowContainer<DisplayContent> {
Wale Ogunwalee05f5012016-09-16 16:27:29 -070093 private static final String TAG = TAG_WITH_CLASS_NAME ? "RootWindowContainer" : TAG_WM;
94
Jorim Jaggi86c39f92017-05-02 18:02:46 +020095 private static final int SET_SCREEN_BRIGHTNESS_OVERRIDE = 1;
96 private static final int SET_USER_ACTIVITY_TIMEOUT = 2;
97
Wale Ogunwalee05f5012016-09-16 16:27:29 -070098 private boolean mWallpaperForceHidingChanged = false;
99 private Object mLastWindowFreezeSource = null;
100 private Session mHoldScreen = null;
101 private float mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700102 private long mUserActivityTimeout = -1;
103 private boolean mUpdateRotation = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700104 // Following variables are for debugging screen wakelock only.
105 // Last window that requires screen wakelock
106 WindowState mHoldScreenWindow = null;
107 // Last window that obscures all windows below
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700108 WindowState mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700109 // Only set while traversing the default display based on its content.
110 // Affects the behavior of mirroring on secondary displays.
111 private boolean mObscureApplicationContentOnSecondaryDisplays = false;
112
113 private boolean mSustainedPerformanceModeEnabled = false;
114 private boolean mSustainedPerformanceModeCurrent = false;
115
116 boolean mWallpaperMayChange = false;
Robert Carr11c26c22016-09-23 12:40:27 -0700117 // During an orientation change, we track whether all windows have rendered
118 // at the new orientation, and this will be false from changing orientation until that occurs.
119 // For seamless rotation cases this always stays true, as the windows complete their orientation
120 // changes 1 by 1 without disturbing global state.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700121 boolean mOrientationChangeComplete = true;
122 boolean mWallpaperActionPending = false;
123
Bryce Leeaea60d22018-01-31 14:42:15 -0800124 private final ArrayList<TaskStack> mTmpStackList = new ArrayList();
125 private final ArrayList<Integer> mTmpStackIds = new ArrayList<>();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700126
Wale Ogunwale0303c572016-10-20 10:16:29 -0700127 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700128
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200129 private final Handler mHandler;
130
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800131 private String mCloseSystemDialogsReason;
Robert Carrae606b42018-02-15 15:36:23 -0800132
133 // Only a seperate transaction until we seperate the apply surface changes
134 // transaction from the global transaction.
135 private final SurfaceControl.Transaction mDisplayTransaction = new SurfaceControl.Transaction();
136
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800137 private final Consumer<WindowState> mCloseSystemDialogsConsumer = w -> {
138 if (w.mHasSurface) {
139 try {
140 w.mClient.closeSystemDialogs(mCloseSystemDialogsReason);
141 } catch (RemoteException e) {
142 }
143 }
144 };
145
146 private static final Consumer<WindowState> sRemoveReplacedWindowsConsumer = w -> {
147 final AppWindowToken aToken = w.mAppToken;
148 if (aToken != null) {
149 aToken.removeReplacedWindowIfNeeded(w);
150 }
151 };
152
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700153 RootWindowContainer(WindowManagerService service) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100154 super(service);
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200155 mHandler = new MyHandler(service.mH.getLooper());
Wale Ogunwale0303c572016-10-20 10:16:29 -0700156 mWallpaperController = new WallpaperController(mService);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700157 }
158
159 WindowState computeFocusedWindow() {
David Stevens46939562017-03-24 13:04:00 -0700160 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700161 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700162 final WindowState win = dc.findFocusedWindow();
163 if (win != null) {
164 return win;
165 }
166 }
167 return null;
168 }
169
170 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800171 * Get an array with display ids ordered by focus priority - last items should be given
172 * focus first. Sparse array just maps position to displayId.
173 */
174 void getDisplaysInFocusOrder(SparseIntArray displaysInFocusOrder) {
175 displaysInFocusOrder.clear();
176
177 final int size = mChildren.size();
178 for (int i = 0; i < size; ++i) {
Andrii Kulian0214ed92017-05-16 13:44:05 -0700179 final DisplayContent displayContent = mChildren.get(i);
180 if (displayContent.isRemovalDeferred()) {
181 // Don't report displays that are going to be removed soon.
182 continue;
183 }
184 displaysInFocusOrder.put(i, displayContent.getDisplayId());
Andrii Kulian7fc22812016-12-28 13:04:11 -0800185 }
186 }
187
Wale Ogunwale02319a62016-09-26 15:21:22 -0700188 DisplayContent getDisplayContent(int displayId) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700189 for (int i = mChildren.size() - 1; i >= 0; --i) {
190 final DisplayContent current = mChildren.get(i);
191 if (current.getDisplayId() == displayId) {
192 return current;
193 }
194 }
195 return null;
196 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700197
Bryce Leef19cbe22018-02-02 15:09:21 -0800198 DisplayContent createDisplayContent(final Display display, DisplayWindowController controller) {
Bryce Lee1cddfdc2018-02-20 10:41:58 -0800199 final int displayId = display.getDisplayId();
200
201 // In select scenarios, it is possible that a DisplayContent will be created on demand
202 // rather than waiting for the controller. In this case, associate the controller and return
203 // the existing display.
204 final DisplayContent existing = getDisplayContent(displayId);
205
206 if (existing != null) {
207 existing.setController(controller);
208 return existing;
209 }
210
Bryce Leef19cbe22018-02-02 15:09:21 -0800211 final DisplayContent dc =
212 new DisplayContent(display, mService, mWallpaperController, controller);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700213
214 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Adding display=" + display);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700215
216 final DisplayInfo displayInfo = dc.getDisplayInfo();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700217 final Rect rect = new Rect();
218 mService.mDisplaySettings.getOverscanLocked(displayInfo.name, displayInfo.uniqueId, rect);
219 displayInfo.overscanLeft = rect.left;
220 displayInfo.overscanTop = rect.top;
221 displayInfo.overscanRight = rect.right;
222 displayInfo.overscanBottom = rect.bottom;
223 if (mService.mDisplayManagerInternal != null) {
224 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(
225 displayId, displayInfo);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000226 dc.configureDisplayPolicy();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700227
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700228 // Tap Listeners are supported for:
229 // 1. All physical displays (multi-display).
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -0700230 // 2. VirtualDisplays on VR, AA (and everything else).
231 if (mService.canDispatchPointerEvents()) {
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700232 if (DEBUG_DISPLAY) {
233 Slog.d(TAG,
234 "Registering PointerEventListener for DisplayId: " + displayId);
235 }
236 dc.mTapDetector = new TaskTapPointerEventListener(mService, dc);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700237 mService.registerPointerEventListener(dc.mTapDetector);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700238 if (displayId == DEFAULT_DISPLAY) {
239 mService.registerPointerEventListener(mService.mMousePositionTracker);
240 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700241 }
242 }
243
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700244 return dc;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700245 }
246
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700247 boolean isLayoutNeeded() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700248 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700249 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700250 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700251 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700252 return true;
253 }
254 }
255 return false;
256 }
257
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000258 void getWindowsByName(ArrayList<WindowState> output, String name) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700259 int objectId = 0;
260 // See if this is an object ID.
261 try {
262 objectId = Integer.parseInt(name, 16);
263 name = null;
264 } catch (RuntimeException e) {
265 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800266
267 getWindowsByName(output, name, objectId);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700268 }
269
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000270 private void getWindowsByName(ArrayList<WindowState> output, String name, int objectId) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800271 forAllWindows((w) -> {
272 if (name != null) {
273 if (w.mAttrs.getTitle().toString().contains(name)) {
274 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700275 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800276 } else if (System.identityHashCode(w) == objectId) {
277 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700278 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800279 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700280 }
281
Wale Ogunwale02319a62016-09-26 15:21:22 -0700282 /**
283 * Returns the app window token for the input binder if it exist in the system.
284 * NOTE: Only one AppWindowToken is allowed to exist in the system for a binder token, since
285 * AppWindowToken represents an activity which can only exist on one display.
286 */
287 AppWindowToken getAppWindowToken(IBinder binder) {
288 for (int i = mChildren.size() - 1; i >= 0; --i) {
289 final DisplayContent dc = mChildren.get(i);
290 final AppWindowToken atoken = dc.getAppWindowToken(binder);
291 if (atoken != null) {
292 return atoken;
293 }
294 }
295 return null;
296 }
297
298 /** Returns the display object the input window token is currently mapped on. */
299 DisplayContent getWindowTokenDisplay(WindowToken token) {
300 if (token == null) {
301 return null;
302 }
303
304 for (int i = mChildren.size() - 1; i >= 0; --i) {
305 final DisplayContent dc = mChildren.get(i);
306 final WindowToken current = dc.getWindowToken(token.token);
307 if (current == token) {
308 return dc;
309 }
310 }
311
312 return null;
313 }
314
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700315 /**
316 * Set new display override config and return array of ids of stacks that were changed during
Bryce Leeaea60d22018-01-31 14:42:15 -0800317 * update. If called for the default display, global configuration will also be updated. Stacks
318 * that are marked for deferred removal are excluded from the returned array.
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700319 */
320 int[] setDisplayOverrideConfigurationIfNeeded(Configuration newConfiguration, int displayId) {
321 final DisplayContent displayContent = getDisplayContent(displayId);
322 if (displayContent == null) {
323 throw new IllegalArgumentException("Display not found for id: " + displayId);
324 }
325
326 final Configuration currentConfig = displayContent.getOverrideConfiguration();
327 final boolean configChanged = currentConfig.diff(newConfiguration) != 0;
328 if (!configChanged) {
329 return null;
330 }
Bryce Leeaea60d22018-01-31 14:42:15 -0800331
Andrii Kuliand68501e2017-01-10 22:57:27 -0800332 displayContent.onOverrideConfigurationChanged(newConfiguration);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700333
Bryce Leeaea60d22018-01-31 14:42:15 -0800334 mTmpStackList.clear();
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700335 if (displayId == DEFAULT_DISPLAY) {
336 // Override configuration of the default display duplicates global config. In this case
337 // we also want to update the global config.
Bryce Leeaea60d22018-01-31 14:42:15 -0800338 setGlobalConfigurationIfNeeded(newConfiguration, mTmpStackList);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700339 } else {
Bryce Leeaea60d22018-01-31 14:42:15 -0800340 updateStackBoundsAfterConfigChange(displayId, mTmpStackList);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700341 }
Bryce Leeaea60d22018-01-31 14:42:15 -0800342
343 mTmpStackIds.clear();
344 final int stackCount = mTmpStackList.size();
345
346 for (int i = 0; i < stackCount; ++i) {
347 final TaskStack stack = mTmpStackList.get(i);
348
349 // We only include stacks that are not marked for removal as they do not exist outside
350 // of WindowManager at this point.
351 if (!stack.mDeferRemoval) {
352 mTmpStackIds.add(stack.mStackId);
353 }
354 }
355
356 return mTmpStackIds.isEmpty() ? null : ArrayUtils.convertToIntArray(mTmpStackIds);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700357 }
358
Bryce Leeaea60d22018-01-31 14:42:15 -0800359 private void setGlobalConfigurationIfNeeded(Configuration newConfiguration,
360 List<TaskStack> changedStacks) {
Andrii Kulian441e4492016-09-29 15:25:00 -0700361 final boolean configChanged = getConfiguration().diff(newConfiguration) != 0;
362 if (!configChanged) {
Bryce Leeaea60d22018-01-31 14:42:15 -0800363 return;
Andrii Kulian441e4492016-09-29 15:25:00 -0700364 }
365 onConfigurationChanged(newConfiguration);
Bryce Leeaea60d22018-01-31 14:42:15 -0800366 updateStackBoundsAfterConfigChange(changedStacks);
Andrii Kulian441e4492016-09-29 15:25:00 -0700367 }
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 */
Bryce Leeaea60d22018-01-31 14:42:15 -0800381 private void updateStackBoundsAfterConfigChange(List<TaskStack> changedStacks) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700382 final int numDisplays = mChildren.size();
383 for (int i = 0; i < numDisplays; ++i) {
384 final DisplayContent dc = mChildren.get(i);
Bryce Leeaea60d22018-01-31 14:42:15 -0800385 dc.updateStackBoundsAfterConfigChange(changedStacks);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700386 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700387 }
388
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700389 /** Same as {@link #updateStackBoundsAfterConfigChange()} but only for a specific display. */
Bryce Leeaea60d22018-01-31 14:42:15 -0800390 private void updateStackBoundsAfterConfigChange(int displayId, List<TaskStack> changedStacks) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700391 final DisplayContent dc = getDisplayContent(displayId);
Bryce Leeaea60d22018-01-31 14:42:15 -0800392 dc.updateStackBoundsAfterConfigChange(changedStacks);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700393 }
394
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700395 private void prepareFreezingTaskBounds() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700396 for (int i = mChildren.size() - 1; i >= 0; i--) {
397 mChildren.get(i).prepareFreezingTaskBounds();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700398 }
399 }
400
Wale Ogunwale68278562017-09-23 17:13:55 -0700401 TaskStack getStack(int windowingMode, int activityType) {
402 for (int i = mChildren.size() - 1; i >= 0; i--) {
403 final DisplayContent dc = mChildren.get(i);
404 final TaskStack stack = dc.getStack(windowingMode, activityType);
405 if (stack != null) {
406 return stack;
407 }
408 }
409 return null;
410 }
411
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700412 void setSecureSurfaceState(int userId, boolean disabled) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800413 forAllWindows((w) -> {
414 if (w.mHasSurface && userId == UserHandle.getUserId(w.mOwnerUid)) {
415 w.mWinAnimator.setSecureLocked(disabled);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700416 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800417 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700418 }
419
Suprabh Shukla69c71422018-04-02 18:39:01 -0700420 void updateHiddenWhileSuspendedState(final ArraySet<String> packages, final boolean suspended) {
421 forAllWindows((w) -> {
422 if (packages.contains(w.getOwningPackage())) {
423 w.setHiddenWhileSuspended(suspended);
424 }
425 }, false);
426 }
427
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700428 void updateAppOpsState() {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800429 forAllWindows((w) -> {
Svet Ganovf7b47252018-02-26 11:11:27 -0800430 w.updateAppOpsState();
Wale Ogunwaled1880962016-11-08 10:31:59 -0800431 }, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700432 }
433
434 boolean canShowStrictModeViolation(int pid) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800435 final WindowState win = getWindow((w) -> w.mSession.mPid == pid && w.isVisibleLw());
436 return win != null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700437 }
438
439 void closeSystemDialogs(String reason) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800440 mCloseSystemDialogsReason = reason;
441 forAllWindows(mCloseSystemDialogsConsumer, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700442 }
443
444 void removeReplacedWindows() {
445 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION removeReplacedWindows");
446 mService.openSurfaceTransaction();
447 try {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800448 forAllWindows(sRemoveReplacedWindowsConsumer, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700449 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200450 mService.closeSurfaceTransaction("removeReplacedWindows");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700451 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION removeReplacedWindows");
452 }
453 }
454
455 boolean hasPendingLayoutChanges(WindowAnimator animator) {
456 boolean hasChanges = false;
457
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700458 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700459 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700460 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700461 final int pendingChanges = animator.getPendingLayoutChanges(dc.getDisplayId());
462 if ((pendingChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
463 animator.mBulkUpdateParams |= SET_WALLPAPER_ACTION_PENDING;
464 }
465 if (pendingChanges != 0) {
466 hasChanges = true;
467 }
468 }
469
470 return hasChanges;
471 }
472
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700473 boolean reclaimSomeSurfaceMemory(WindowStateAnimator winAnimator, String operation,
474 boolean secure) {
475 final WindowSurfaceController surfaceController = winAnimator.mSurfaceController;
476 boolean leakedSurface = false;
477 boolean killedApps = false;
478
479 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, winAnimator.mWin.toString(),
480 winAnimator.mSession.mPid, operation);
481
482 final long callingIdentity = Binder.clearCallingIdentity();
483 try {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700484 // There was some problem...first, do a sanity check of the window list to make sure
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700485 // we haven't left any dangling surfaces around.
486
487 Slog.i(TAG_WM, "Out of memory for surface! Looking for leaks...");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700488 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700489 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700490 leakedSurface |= mChildren.get(displayNdx).destroyLeakedSurfaces();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700491 }
492
493 if (!leakedSurface) {
494 Slog.w(TAG_WM, "No leaked surfaces; killing applications!");
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700495 final SparseIntArray pidCandidates = new SparseIntArray();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700496 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800497 mChildren.get(displayNdx).forAllWindows((w) -> {
498 if (mService.mForceRemoves.contains(w)) {
499 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700500 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800501 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700502 if (wsa.mSurfaceController != null) {
503 pidCandidates.append(wsa.mSession.mPid, wsa.mSession.mPid);
504 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800505 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700506
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700507 if (pidCandidates.size() > 0) {
508 int[] pids = new int[pidCandidates.size()];
509 for (int i = 0; i < pids.length; i++) {
510 pids[i] = pidCandidates.keyAt(i);
511 }
512 try {
513 if (mService.mActivityManager.killPids(pids, "Free memory", secure)) {
514 killedApps = true;
515 }
516 } catch (RemoteException e) {
517 }
518 }
519 }
520 }
521
522 if (leakedSurface || killedApps) {
523 // We managed to reclaim some memory, so get rid of the trouble surface and ask the
524 // app to request another one.
525 Slog.w(TAG_WM,
526 "Looks like we have reclaimed some memory, clearing surface for retry.");
527 if (surfaceController != null) {
528 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) logSurface(winAnimator.mWin,
529 "RECOVER DESTROY", false);
530 winAnimator.destroySurface();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800531 if (winAnimator.mWin.mAppToken != null
532 && winAnimator.mWin.mAppToken.getController() != null) {
533 winAnimator.mWin.mAppToken.getController().removeStartingWindow();
534 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700535 }
536
537 try {
538 winAnimator.mWin.mClient.dispatchGetNewSurface();
539 } catch (RemoteException e) {
540 }
541 }
542 } finally {
543 Binder.restoreCallingIdentity(callingIdentity);
544 }
545
546 return leakedSurface || killedApps;
547 }
548
549 // "Something has changed! Let's make it correct now."
550 // TODO: Super crazy long method that should be broken down...
551 void performSurfacePlacement(boolean recoveringMemory) {
552 if (DEBUG_WINDOW_TRACE) Slog.v(TAG, "performSurfacePlacementInner: entry. Called by "
553 + Debug.getCallers(3));
554
555 int i;
556 boolean updateInputWindowsNeeded = false;
557
558 if (mService.mFocusMayChange) {
559 mService.mFocusMayChange = false;
560 updateInputWindowsNeeded = mService.updateFocusedWindowLocked(
561 UPDATE_FOCUS_WILL_PLACE_SURFACES, false /*updateInputWindows*/);
562 }
563
564 // Initialize state of exiting tokens.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700565 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700566 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700567 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800568 displayContent.setExitingTokensHasVisible(false);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700569 }
570
571 mHoldScreen = null;
572 mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700573 mUserActivityTimeout = -1;
574 mObscureApplicationContentOnSecondaryDisplays = false;
575 mSustainedPerformanceModeCurrent = false;
576 mService.mTransactionSequence++;
577
578 // TODO(multi-display):
579 final DisplayContent defaultDisplay = mService.getDefaultDisplayContentLocked();
580 final DisplayInfo defaultInfo = defaultDisplay.getDisplayInfo();
581 final int defaultDw = defaultInfo.logicalWidth;
582 final int defaultDh = defaultInfo.logicalHeight;
583
584 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
585 ">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
586 mService.openSurfaceTransaction();
587 try {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700588 applySurfaceChangesTransaction(recoveringMemory, defaultDw, defaultDh);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700589 } catch (RuntimeException e) {
590 Slog.wtf(TAG, "Unhandled exception in Window Manager", e);
591 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200592 mService.closeSurfaceTransaction("performLayoutAndPlaceSurfaces");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700593 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
594 "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
595 }
596
Chavi Weingarten16d0d072018-02-12 23:50:28 +0000597 mService.mAnimator.executeAfterPrepareSurfacesRunnables();
598
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700599 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
600
601 // If we are ready to perform an app transition, check through all of the app tokens to be
602 // shown and see if they are ready to go.
603 if (mService.mAppTransition.isReady()) {
Jorim Jaggi82c17862018-02-21 17:50:18 +0100604 // This needs to be split into two expressions, as handleAppTransitionReadyLocked may
605 // modify dc.pendingLayoutChanges, which would get lost when writing
606 // defaultDisplay.pendingLayoutChanges |= handleAppTransitionReadyLocked()
607 final int layoutChanges = surfacePlacer.handleAppTransitionReadyLocked();
608 defaultDisplay.pendingLayoutChanges |= layoutChanges;
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
Winson Chunge2d72172018-01-25 17:46:20 +0000626 // Defer starting the recents animation until the wallpaper has drawn
627 final RecentsAnimationController recentsAnimationController =
628 mService.getRecentsAnimationController();
629 if (recentsAnimationController != null) {
630 recentsAnimationController.checkAnimationReady(mWallpaperController);
631 }
632
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700633 if (mWallpaperForceHidingChanged && defaultDisplay.pendingLayoutChanges == 0
634 && !mService.mAppTransition.isReady()) {
635 // At this point, there was a window with a wallpaper that was force hiding other
636 // windows behind it, but now it is going away. This may be simple -- just animate away
637 // the wallpaper and its window -- or it may be hard -- the wallpaper now needs to be
638 // shown behind something that was hidden.
639 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
640 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
641 "after animateAwayWallpaperLocked", defaultDisplay.pendingLayoutChanges);
642 }
643 mWallpaperForceHidingChanged = false;
644
645 if (mWallpaperMayChange) {
646 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Wallpaper may change! Adjusting");
647 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
648 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("WallpaperMayChange",
649 defaultDisplay.pendingLayoutChanges);
650 }
651
652 if (mService.mFocusMayChange) {
653 mService.mFocusMayChange = false;
654 if (mService.updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
655 false /*updateInputWindows*/)) {
656 updateInputWindowsNeeded = true;
657 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
658 }
659 }
660
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700661 if (isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700662 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
663 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("mLayoutNeeded",
664 defaultDisplay.pendingLayoutChanges);
665 }
666
David Stevensf833ba92017-03-16 19:00:20 -0700667 final ArraySet<DisplayContent> touchExcludeRegionUpdateDisplays = handleResizingWindows();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700668
669 if (DEBUG_ORIENTATION && mService.mDisplayFrozen) Slog.v(TAG,
670 "With display frozen, orientationChangeComplete=" + mOrientationChangeComplete);
671 if (mOrientationChangeComplete) {
672 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
673 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
674 mService.mLastFinishedFreezeSource = mLastWindowFreezeSource;
675 mService.mH.removeMessages(WINDOW_FREEZE_TIMEOUT);
676 }
677 mService.stopFreezingDisplayLocked();
678 }
679
680 // Destroy the surface of any windows that are no longer visible.
681 boolean wallpaperDestroyed = false;
682 i = mService.mDestroySurface.size();
683 if (i > 0) {
684 do {
685 i--;
686 WindowState win = mService.mDestroySurface.get(i);
687 win.mDestroying = false;
688 if (mService.mInputMethodWindow == win) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000689 mService.setInputMethodWindowLocked(null);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700690 }
Wale Ogunwale0303c572016-10-20 10:16:29 -0700691 if (win.getDisplayContent().mWallpaperController.isWallpaperTarget(win)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700692 wallpaperDestroyed = true;
693 }
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200694 win.destroySurfaceUnchecked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700695 } while (i > 0);
696 mService.mDestroySurface.clear();
697 }
698
699 // Time to remove any exiting tokens?
700 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700701 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800702 displayContent.removeExistingTokensIfPossible();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700703 }
704
705 if (wallpaperDestroyed) {
706 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700707 defaultDisplay.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700708 }
709
710 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700711 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700712 if (displayContent.pendingLayoutChanges != 0) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700713 displayContent.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700714 }
715 }
716
717 // Finally update all input windows now that the window changes have stabilized.
718 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
719
720 mService.setHoldScreenLocked(mHoldScreen);
721 if (!mService.mDisplayFrozen) {
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200722 final int brightness = mScreenBrightness < 0 || mScreenBrightness > 1.0f
723 ? -1 : toBrightnessOverride(mScreenBrightness);
724
725 // Post these on a handler such that we don't call into power manager service while
726 // holding the window manager lock to avoid lock contention with power manager lock.
727 mHandler.obtainMessage(SET_SCREEN_BRIGHTNESS_OVERRIDE, brightness, 0).sendToTarget();
728 mHandler.obtainMessage(SET_USER_ACTIVITY_TIMEOUT, mUserActivityTimeout).sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700729 }
730
731 if (mSustainedPerformanceModeCurrent != mSustainedPerformanceModeEnabled) {
732 mSustainedPerformanceModeEnabled = mSustainedPerformanceModeCurrent;
733 mService.mPowerManagerInternal.powerHint(
Ruchi Kandoi0d434042016-10-03 09:12:02 -0700734 PowerHint.SUSTAINED_PERFORMANCE,
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700735 (mSustainedPerformanceModeEnabled ? 1 : 0));
736 }
737
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700738 if (mUpdateRotation) {
739 if (DEBUG_ORIENTATION) Slog.d(TAG, "Performing post-rotate rotation");
Andrii Kulian48146fd2017-08-07 11:22:50 -0700740 // TODO(multi-display): Update rotation for different displays separately.
741 final int displayId = defaultDisplay.getDisplayId();
Robert Carrae606b42018-02-15 15:36:23 -0800742 if (defaultDisplay.updateRotationUnchecked()) {
Andrii Kulian48146fd2017-08-07 11:22:50 -0700743 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, displayId).sendToTarget();
744 } else {
745 mUpdateRotation = false;
746 }
747 // Update rotation of VR virtual display separately. Currently this is the only kind of
748 // secondary display that can be rotated because of the single-display limitations in
749 // PhoneWindowManager.
750 final DisplayContent vrDisplay = mService.mVr2dDisplayId != INVALID_DISPLAY
751 ? getDisplayContent(mService.mVr2dDisplayId) : null;
Robert Carrae606b42018-02-15 15:36:23 -0800752 if (vrDisplay != null && vrDisplay.updateRotationUnchecked()) {
Andrii Kulian48146fd2017-08-07 11:22:50 -0700753 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mService.mVr2dDisplayId)
754 .sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700755 }
756 }
757
758 if (mService.mWaitingForDrawnCallback != null ||
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700759 (mOrientationChangeComplete && !defaultDisplay.isLayoutNeeded()
760 && !mUpdateRotation)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700761 mService.checkDrawnWindowsLocked();
762 }
763
764 final int N = mService.mPendingRemove.size();
765 if (N > 0) {
766 if (mService.mPendingRemoveTmp.length < N) {
767 mService.mPendingRemoveTmp = new WindowState[N+10];
768 }
769 mService.mPendingRemove.toArray(mService.mPendingRemoveTmp);
770 mService.mPendingRemove.clear();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700771 ArrayList<DisplayContent> displayList = new ArrayList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700772 for (i = 0; i < N; i++) {
773 final WindowState w = mService.mPendingRemoveTmp[i];
774 w.removeImmediately();
775 final DisplayContent displayContent = w.getDisplayContent();
776 if (displayContent != null && !displayList.contains(displayContent)) {
777 displayList.add(displayContent);
778 }
779 }
780
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700781 for (int j = displayList.size() - 1; j >= 0; --j) {
782 final DisplayContent dc = displayList.get(j);
783 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700784 }
785 }
786
787 // Remove all deferred displays stacks, tasks, and activities.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700788 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
789 mChildren.get(displayNdx).checkCompleteDeferredRemoval();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700790 }
791
792 if (updateInputWindowsNeeded) {
793 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
794 }
David Stevensee9e2772017-02-09 16:30:27 -0800795 mService.setFocusTaskRegionLocked(null);
David Stevensf833ba92017-03-16 19:00:20 -0700796 if (touchExcludeRegionUpdateDisplays != null) {
797 final DisplayContent focusedDc = mService.mFocusedApp != null
798 ? mService.mFocusedApp.getDisplayContent() : null;
799 for (DisplayContent dc : touchExcludeRegionUpdateDisplays) {
800 // The focused DisplayContent was recalcuated in setFocusTaskRegionLocked
801 if (focusedDc != dc) {
802 dc.setTouchExcludeRegion(null /* focusedTask */);
803 }
804 }
805 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700806
807 // Check to see if we are now in a state where the screen should
808 // be enabled, because the window obscured flags have changed.
809 mService.enableScreenIfNeededLocked();
810
811 mService.scheduleAnimationLocked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700812
813 if (DEBUG_WINDOW_TRACE) Slog.e(TAG,
814 "performSurfacePlacementInner exit: animating=" + mService.mAnimator.isAnimating());
815 }
816
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700817 private void applySurfaceChangesTransaction(boolean recoveringMemory, int defaultDw,
818 int defaultDh) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700819 mHoldScreenWindow = null;
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700820 mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700821
Andrii Kulian8ee72852017-03-10 10:36:45 -0800822 // TODO(multi-display): Support these features on secondary screens.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700823 if (mService.mWatermark != null) {
824 mService.mWatermark.positionSurface(defaultDw, defaultDh);
825 }
826 if (mService.mStrictModeFlash != null) {
827 mService.mStrictModeFlash.positionSurface(defaultDw, defaultDh);
828 }
829 if (mService.mCircularDisplayMask != null) {
Andrii Kulian8ee72852017-03-10 10:36:45 -0800830 mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh,
831 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700832 }
833 if (mService.mEmulatorDisplayOverlay != null) {
834 mService.mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh,
Andrii Kulian8ee72852017-03-10 10:36:45 -0800835 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700836 }
837
838 boolean focusDisplayed = false;
839
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700840 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700841 for (int j = 0; j < count; ++j) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700842 final DisplayContent dc = mChildren.get(j);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700843 focusDisplayed |= dc.applySurfaceChangesTransaction(recoveringMemory);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700844 }
845
846 if (focusDisplayed) {
847 mService.mH.sendEmptyMessage(REPORT_LOSING_FOCUS);
848 }
849
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700850 // Give the display manager a chance to adjust properties like display rotation if it needs
851 // to.
Robert Carrae606b42018-02-15 15:36:23 -0800852 mService.mDisplayManagerInternal.performTraversal(mDisplayTransaction);
853 SurfaceControl.mergeToGlobalTransaction(mDisplayTransaction);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700854 }
855
856 /**
David Stevensf833ba92017-03-16 19:00:20 -0700857 * Handles resizing windows during surface placement.
858 *
859 * @return A set of any DisplayContent whose touch exclude region needs to be recalculated due
860 * to a tap-exclude window resizing, or null if no such DisplayContents were found.
861 */
862 private ArraySet<DisplayContent> handleResizingWindows() {
863 ArraySet<DisplayContent> touchExcludeRegionUpdateSet = null;
864 for (int i = mService.mResizingWindows.size() - 1; i >= 0; i--) {
865 WindowState win = mService.mResizingWindows.get(i);
866 if (win.mAppFreezing) {
867 // Don't remove this window until rotation has completed.
868 continue;
869 }
870 win.reportResized();
871 mService.mResizingWindows.remove(i);
872 if (WindowManagerService.excludeWindowTypeFromTapOutTask(win.mAttrs.type)) {
873 final DisplayContent dc = win.getDisplayContent();
874 if (touchExcludeRegionUpdateSet == null) {
875 touchExcludeRegionUpdateSet = new ArraySet<>();
876 }
877 touchExcludeRegionUpdateSet.add(dc);
878 }
879 }
880 return touchExcludeRegionUpdateSet;
881 }
882
883 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700884 * @param w WindowState this method is applied to.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700885 * @param obscured True if there is a window on top of this obscuring the display.
886 * @param syswin System window?
887 * @return True when the display contains content to show the user. When false, the display
888 * manager may choose to mirror or blank the display.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700889 */
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700890 boolean handleNotObscuredLocked(WindowState w, boolean obscured, boolean syswin) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700891 final WindowManager.LayoutParams attrs = w.mAttrs;
892 final int attrFlags = attrs.flags;
Aaron Whyte8cbdf042018-01-23 12:06:02 -0800893 final boolean onScreen = w.isOnScreen();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700894 final boolean canBeSeen = w.isDisplayedLw();
895 final int privateflags = attrs.privateFlags;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700896 boolean displayHasContent = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700897
Aaron Whyte8cbdf042018-01-23 12:06:02 -0800898 if (DEBUG_KEEP_SCREEN_ON) {
899 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked w: " + w
900 + ", w.mHasSurface: " + w.mHasSurface
901 + ", w.isOnScreen(): " + onScreen
902 + ", w.isDisplayedLw(): " + w.isDisplayedLw()
903 + ", w.mAttrs.userActivityTimeout: " + w.mAttrs.userActivityTimeout);
904 }
905 if (w.mHasSurface && onScreen) {
906 if (!syswin && w.mAttrs.userActivityTimeout >= 0 && mUserActivityTimeout < 0) {
907 mUserActivityTimeout = w.mAttrs.userActivityTimeout;
908 if (DEBUG_KEEP_SCREEN_ON) {
909 Slog.d(TAG, "mUserActivityTimeout set to " + mUserActivityTimeout);
910 }
911 }
912 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700913 if (w.mHasSurface && canBeSeen) {
914 if ((attrFlags & FLAG_KEEP_SCREEN_ON) != 0) {
915 mHoldScreen = w.mSession;
916 mHoldScreenWindow = w;
917 } else if (DEBUG_KEEP_SCREEN_ON && w == mService.mLastWakeLockHoldingWindow) {
918 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked: " + w + " was holding "
Wale Ogunwale743fcc12017-06-06 21:36:50 +0000919 + "screen wakelock but no longer has FLAG_KEEP_SCREEN_ON!!! called by"
920 + Debug.getCallers(10));
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700921 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700922 if (!syswin && w.mAttrs.screenBrightness >= 0 && mScreenBrightness < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700923 mScreenBrightness = w.mAttrs.screenBrightness;
924 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700925
926 final int type = attrs.type;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700927 // This function assumes that the contents of the default display are processed first
928 // before secondary displays.
929 final DisplayContent displayContent = w.getDisplayContent();
930 if (displayContent != null && displayContent.isDefaultDisplay) {
931 // While a dream or keyguard is showing, obscure ordinary application content on
932 // secondary displays (by forcibly enabling mirroring unless there is other content
933 // we want to show) but still allow opaque keyguard dialogs to be shown.
934 if (type == TYPE_DREAM || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
935 mObscureApplicationContentOnSecondaryDisplays = true;
936 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700937 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700938 } else if (displayContent != null &&
939 (!mObscureApplicationContentOnSecondaryDisplays
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700940 || (obscured && type == TYPE_KEYGUARD_DIALOG))) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700941 // Allow full screen keyguard presentation dialogs to be seen.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700942 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700943 }
944 if ((privateflags & PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE) != 0) {
945 mSustainedPerformanceModeCurrent = true;
946 }
947 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700948
949 return displayHasContent;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700950 }
951
952 boolean copyAnimToLayoutParams() {
953 boolean doRequest = false;
954
955 final int bulkUpdateParams = mService.mAnimator.mBulkUpdateParams;
956 if ((bulkUpdateParams & SET_UPDATE_ROTATION) != 0) {
957 mUpdateRotation = true;
958 doRequest = true;
959 }
960 if ((bulkUpdateParams & SET_WALLPAPER_MAY_CHANGE) != 0) {
961 mWallpaperMayChange = true;
962 doRequest = true;
963 }
964 if ((bulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0) {
965 mWallpaperForceHidingChanged = true;
966 doRequest = true;
967 }
968 if ((bulkUpdateParams & SET_ORIENTATION_CHANGE_COMPLETE) == 0) {
969 mOrientationChangeComplete = false;
970 } else {
971 mOrientationChangeComplete = true;
972 mLastWindowFreezeSource = mService.mAnimator.mLastWindowFreezeSource;
973 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
974 doRequest = true;
975 }
976 }
chaviw042059d2018-01-11 11:24:08 -0800977
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700978 if ((bulkUpdateParams & SET_WALLPAPER_ACTION_PENDING) != 0) {
979 mWallpaperActionPending = true;
980 }
981
982 return doRequest;
983 }
984
985 private static int toBrightnessOverride(float value) {
986 return (int)(value * PowerManager.BRIGHTNESS_ON);
987 }
988
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200989 private final class MyHandler extends Handler {
990
991 public MyHandler(Looper looper) {
992 super(looper);
993 }
994
995 @Override
996 public void handleMessage(Message msg) {
997 switch (msg.what) {
998 case SET_SCREEN_BRIGHTNESS_OVERRIDE:
999 mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(
1000 msg.arg1);
1001 break;
1002 case SET_USER_ACTIVITY_TIMEOUT:
1003 mService.mPowerManagerInternal.setUserActivityTimeoutOverrideFromWindowManager(
1004 (Long) msg.obj);
1005 break;
1006 default:
1007 break;
1008 }
1009 }
1010 }
1011
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001012 void dumpDisplayContents(PrintWriter pw) {
1013 pw.println("WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)");
1014 if (mService.mDisplayReady) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001015 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001016 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001017 final DisplayContent displayContent = mChildren.get(i);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001018 displayContent.dump(pw, " ", true /* dumpAll */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001019 }
1020 } else {
1021 pw.println(" NO DISPLAY");
1022 }
1023 }
1024
1025 void dumpLayoutNeededDisplayIds(PrintWriter pw) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001026 if (!isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001027 return;
1028 }
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001029 pw.print(" mLayoutNeeded on displays=");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001030 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001031 for (int displayNdx = 0; displayNdx < count; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001032 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001033 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001034 pw.print(displayContent.getDisplayId());
1035 }
1036 }
1037 pw.println();
1038 }
1039
1040 void dumpWindowsNoHeader(PrintWriter pw, boolean dumpAll, ArrayList<WindowState> windows) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001041 final int[] index = new int[1];
1042 forAllWindows((w) -> {
1043 if (windows == null || windows.contains(w)) {
1044 pw.println(" Window #" + index[0] + " " + w + ":");
1045 w.dump(pw, " ", dumpAll || windows != null);
1046 index[0] = index[0] + 1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001047 }
Wale Ogunwaled1880962016-11-08 10:31:59 -08001048 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001049 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001050
Wale Ogunwale02319a62016-09-26 15:21:22 -07001051 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1052 pw.println(" All tokens:");
1053 for (int i = mChildren.size() - 1; i >= 0; --i) {
1054 mChildren.get(i).dumpTokens(pw, dumpAll);
1055 }
1056 }
1057
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001058 @CallSuper
1059 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02001060 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001061 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001062 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001063 if (mService.mDisplayReady) {
1064 final int count = mChildren.size();
1065 for (int i = 0; i < count; ++i) {
1066 final DisplayContent displayContent = mChildren.get(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001067 displayContent.writeToProto(proto, DISPLAYS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001068 }
1069 }
Adrian Roos4921ccf2017-09-28 16:54:06 +02001070 if (!trim) {
1071 forAllWindows((w) -> {
1072 w.writeIdentifierToProto(proto, WINDOWS);
1073 }, true);
1074 }
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001075 proto.end(token);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001076 }
1077
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001078 @Override
1079 String getName() {
1080 return "ROOT";
1081 }
Robert Carrb1579c82017-09-05 14:54:47 -07001082
1083 @Override
1084 void scheduleAnimation() {
1085 mService.scheduleAnimationLocked();
1086 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001087}