blob: be3558b3f52f6046b09b4f4fa86628d646a999dc [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 Ogunwalee05f5012016-09-16 16:27:29 -070019import android.content.res.Configuration;
20import android.graphics.Rect;
Ruchi Kandoi0d434042016-10-03 09:12:02 -070021import android.hardware.power.V1_0.PowerHint;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070022import android.os.Binder;
23import android.os.Debug;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020024import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -070025import android.os.IBinder;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020026import android.os.Looper;
27import android.os.Message;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070028import android.os.ParcelFileDescriptor;
29import android.os.PowerManager;
30import android.os.RemoteException;
31import android.os.SystemClock;
32import android.os.UserHandle;
33import android.provider.Settings;
34import android.util.EventLog;
35import android.util.Slog;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070036import android.util.SparseIntArray;
37import android.view.Display;
38import android.view.DisplayInfo;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070039import android.view.WindowManager;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020040
41import com.android.internal.os.SomeArgs;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070042import com.android.internal.util.ArrayUtils;
43import com.android.server.EventLogTags;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070044
45import java.io.FileDescriptor;
46import java.io.PrintWriter;
47import java.util.ArrayList;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080048import java.util.function.Consumer;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070049
Wale Ogunwalef7cab102016-10-25 15:25:14 -070050import static android.app.AppOpsManager.MODE_ALLOWED;
51import static android.app.AppOpsManager.MODE_DEFAULT;
52import static android.app.AppOpsManager.OP_NONE;
Wale Ogunwale02319a62016-09-26 15:21:22 -070053import static android.view.Display.DEFAULT_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;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070059import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070060import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
61import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi879ff722016-11-04 18:08:17 -070062
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;
67import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_POWER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
69import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
70import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_TRACE;
71import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
72import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
73import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
74import static com.android.server.wm.WindowManagerDebugConfig.TAG_KEEP_SCREEN_ON;
75import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
76import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070077import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
78import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070079import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
80import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
81import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_NONE;
82import static com.android.server.wm.WindowManagerService.H.WINDOW_FREEZE_TIMEOUT;
83import static com.android.server.wm.WindowManagerService.logSurface;
84import static com.android.server.wm.WindowSurfacePlacer.SET_FORCE_HIDING_CHANGED;
85import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
86import static com.android.server.wm.WindowSurfacePlacer.SET_TURN_ON_SCREEN;
87import static com.android.server.wm.WindowSurfacePlacer.SET_UPDATE_ROTATION;
88import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_ACTION_PENDING;
89import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
90
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 WindowManagerService mService;
99
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700100 private boolean mWallpaperForceHidingChanged = false;
101 private Object mLastWindowFreezeSource = null;
102 private Session mHoldScreen = null;
103 private float mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700104 private long mUserActivityTimeout = -1;
105 private boolean mUpdateRotation = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700106 // Following variables are for debugging screen wakelock only.
107 // Last window that requires screen wakelock
108 WindowState mHoldScreenWindow = null;
109 // Last window that obscures all windows below
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700110 WindowState mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700111 // Only set while traversing the default display based on its content.
112 // Affects the behavior of mirroring on secondary displays.
113 private boolean mObscureApplicationContentOnSecondaryDisplays = false;
114
115 private boolean mSustainedPerformanceModeEnabled = false;
116 private boolean mSustainedPerformanceModeCurrent = false;
117
118 boolean mWallpaperMayChange = false;
Robert Carr11c26c22016-09-23 12:40:27 -0700119 // During an orientation change, we track whether all windows have rendered
120 // at the new orientation, and this will be false from changing orientation until that occurs.
121 // For seamless rotation cases this always stays true, as the windows complete their orientation
122 // changes 1 by 1 without disturbing global state.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700123 boolean mOrientationChangeComplete = true;
124 boolean mWallpaperActionPending = false;
125
126 private final ArrayList<Integer> mChangedStackList = new ArrayList();
127
128 // State for the RemoteSurfaceTrace system used in testing. If this is enabled SurfaceControl
129 // instances will be replaced with an instance that writes a binary representation of all
130 // commands to mSurfaceTraceFd.
131 boolean mSurfaceTraceEnabled;
132 ParcelFileDescriptor mSurfaceTraceFd;
133 RemoteEventTrace mRemoteEventTrace;
134
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700135 private final WindowLayersController mLayersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700136 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700137
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200138 private final Handler mHandler;
139
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800140 private String mCloseSystemDialogsReason;
141 private final Consumer<WindowState> mCloseSystemDialogsConsumer = w -> {
142 if (w.mHasSurface) {
143 try {
144 w.mClient.closeSystemDialogs(mCloseSystemDialogsReason);
145 } catch (RemoteException e) {
146 }
147 }
148 };
149
150 private static final Consumer<WindowState> sRemoveReplacedWindowsConsumer = w -> {
151 final AppWindowToken aToken = w.mAppToken;
152 if (aToken != null) {
153 aToken.removeReplacedWindowIfNeeded(w);
154 }
155 };
156
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700157 RootWindowContainer(WindowManagerService service) {
158 mService = service;
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200159 mHandler = new MyHandler(service.mH.getLooper());
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700160 mLayersController = new WindowLayersController(mService);
Wale Ogunwale0303c572016-10-20 10:16:29 -0700161 mWallpaperController = new WallpaperController(mService);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700162 }
163
164 WindowState computeFocusedWindow() {
David Stevens46939562017-03-24 13:04:00 -0700165 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700166 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700167 final WindowState win = dc.findFocusedWindow();
168 if (win != null) {
169 return win;
170 }
171 }
172 return null;
173 }
174
175 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800176 * Get an array with display ids ordered by focus priority - last items should be given
177 * focus first. Sparse array just maps position to displayId.
178 */
179 void getDisplaysInFocusOrder(SparseIntArray displaysInFocusOrder) {
180 displaysInFocusOrder.clear();
181
182 final int size = mChildren.size();
183 for (int i = 0; i < size; ++i) {
184 displaysInFocusOrder.put(i, mChildren.get(i).getDisplayId());
185 }
186 }
187
188 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700189 * Retrieve the DisplayContent for the specified displayId. Will create a new DisplayContent if
190 * there is a Display for the displayId.
191 *
192 * @param displayId The display the caller is interested in.
193 * @return The DisplayContent associated with displayId or null if there is no Display for it.
194 */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700195 DisplayContent getDisplayContentOrCreate(int displayId) {
196 DisplayContent dc = getDisplayContent(displayId);
197
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700198 if (dc == null) {
199 final Display display = mService.mDisplayManager.getDisplay(displayId);
200 if (display != null) {
Andrii Kulian325cac42017-04-20 16:02:47 -0700201 final long callingIdentity = Binder.clearCallingIdentity();
202 try {
203 dc = createDisplayContent(display);
204 } finally {
205 Binder.restoreCallingIdentity(callingIdentity);
206 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700207 }
208 }
209 return dc;
210 }
211
Wale Ogunwale02319a62016-09-26 15:21:22 -0700212 DisplayContent getDisplayContent(int displayId) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700213 for (int i = mChildren.size() - 1; i >= 0; --i) {
214 final DisplayContent current = mChildren.get(i);
215 if (current.getDisplayId() == displayId) {
216 return current;
217 }
218 }
219 return null;
220 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700221
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700222 private DisplayContent createDisplayContent(final Display display) {
Wale Ogunwale0303c572016-10-20 10:16:29 -0700223 final DisplayContent dc = new DisplayContent(display, mService, mLayersController,
224 mWallpaperController);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700225 final int displayId = display.getDisplayId();
226
227 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Adding display=" + display);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700228
229 final DisplayInfo displayInfo = dc.getDisplayInfo();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700230 final Rect rect = new Rect();
231 mService.mDisplaySettings.getOverscanLocked(displayInfo.name, displayInfo.uniqueId, rect);
232 displayInfo.overscanLeft = rect.left;
233 displayInfo.overscanTop = rect.top;
234 displayInfo.overscanRight = rect.right;
235 displayInfo.overscanBottom = rect.bottom;
236 if (mService.mDisplayManagerInternal != null) {
237 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(
238 displayId, displayInfo);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700239 mService.configureDisplayPolicyLocked(dc);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700240
241 // TODO(multi-display): Create an input channel for each display with touch capability.
Andrii Kulian4ede3e02017-01-12 11:52:31 -0800242 if (displayId == DEFAULT_DISPLAY && mService.canDispatchPointerEvents()) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700243 dc.mTapDetector = new TaskTapPointerEventListener(
244 mService, dc);
245 mService.registerPointerEventListener(dc.mTapDetector);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700246 mService.registerPointerEventListener(mService.mMousePositionTracker);
247 }
248 }
249
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700250 return dc;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700251 }
252
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700253 boolean isLayoutNeeded() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700254 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700255 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700256 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700257 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700258 return true;
259 }
260 }
261 return false;
262 }
263
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000264 void getWindowsByName(ArrayList<WindowState> output, String name) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700265 int objectId = 0;
266 // See if this is an object ID.
267 try {
268 objectId = Integer.parseInt(name, 16);
269 name = null;
270 } catch (RuntimeException e) {
271 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800272
273 getWindowsByName(output, name, objectId);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700274 }
275
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000276 private void getWindowsByName(ArrayList<WindowState> output, String name, int objectId) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800277 forAllWindows((w) -> {
278 if (name != null) {
279 if (w.mAttrs.getTitle().toString().contains(name)) {
280 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700281 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800282 } else if (System.identityHashCode(w) == objectId) {
283 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700284 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800285 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700286 }
287
Wale Ogunwale02319a62016-09-26 15:21:22 -0700288 /**
289 * Returns the app window token for the input binder if it exist in the system.
290 * NOTE: Only one AppWindowToken is allowed to exist in the system for a binder token, since
291 * AppWindowToken represents an activity which can only exist on one display.
292 */
293 AppWindowToken getAppWindowToken(IBinder binder) {
294 for (int i = mChildren.size() - 1; i >= 0; --i) {
295 final DisplayContent dc = mChildren.get(i);
296 final AppWindowToken atoken = dc.getAppWindowToken(binder);
297 if (atoken != null) {
298 return atoken;
299 }
300 }
301 return null;
302 }
303
304 /** Returns the display object the input window token is currently mapped on. */
305 DisplayContent getWindowTokenDisplay(WindowToken token) {
306 if (token == null) {
307 return null;
308 }
309
310 for (int i = mChildren.size() - 1; i >= 0; --i) {
311 final DisplayContent dc = mChildren.get(i);
312 final WindowToken current = dc.getWindowToken(token.token);
313 if (current == token) {
314 return dc;
315 }
316 }
317
318 return null;
319 }
320
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700321 /**
322 * Set new display override config and return array of ids of stacks that were changed during
323 * update. If called for the default display, global configuration will also be updated.
324 */
325 int[] setDisplayOverrideConfigurationIfNeeded(Configuration newConfiguration, int displayId) {
326 final DisplayContent displayContent = getDisplayContent(displayId);
327 if (displayContent == null) {
328 throw new IllegalArgumentException("Display not found for id: " + displayId);
329 }
330
331 final Configuration currentConfig = displayContent.getOverrideConfiguration();
332 final boolean configChanged = currentConfig.diff(newConfiguration) != 0;
333 if (!configChanged) {
334 return null;
335 }
Andrii Kuliand68501e2017-01-10 22:57:27 -0800336 displayContent.onOverrideConfigurationChanged(newConfiguration);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700337
338 if (displayId == DEFAULT_DISPLAY) {
339 // Override configuration of the default display duplicates global config. In this case
340 // we also want to update the global config.
341 return setGlobalConfigurationIfNeeded(newConfiguration);
342 } else {
343 return updateStackBoundsAfterConfigChange(displayId);
344 }
345 }
346
347 private int[] setGlobalConfigurationIfNeeded(Configuration newConfiguration) {
Andrii Kulian441e4492016-09-29 15:25:00 -0700348 final boolean configChanged = getConfiguration().diff(newConfiguration) != 0;
349 if (!configChanged) {
350 return null;
351 }
352 onConfigurationChanged(newConfiguration);
353 return updateStackBoundsAfterConfigChange();
354 }
355
356 @Override
357 void onConfigurationChanged(Configuration newParentConfig) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700358 prepareFreezingTaskBounds();
Andrii Kulian441e4492016-09-29 15:25:00 -0700359 super.onConfigurationChanged(newParentConfig);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700360
361 mService.mPolicy.onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700362 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700363
Andrii Kulian441e4492016-09-29 15:25:00 -0700364 /**
365 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
366 * bounds were updated.
367 */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700368 private int[] updateStackBoundsAfterConfigChange() {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700369 mChangedStackList.clear();
370
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700371 final int numDisplays = mChildren.size();
372 for (int i = 0; i < numDisplays; ++i) {
373 final DisplayContent dc = mChildren.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700374 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700375 }
376
377 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
378 }
379
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700380 /** Same as {@link #updateStackBoundsAfterConfigChange()} but only for a specific display. */
381 private int[] updateStackBoundsAfterConfigChange(int displayId) {
382 mChangedStackList.clear();
383
384 final DisplayContent dc = getDisplayContent(displayId);
385 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
386
387 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
388 }
389
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700390 private void prepareFreezingTaskBounds() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700391 for (int i = mChildren.size() - 1; i >= 0; i--) {
392 mChildren.get(i).prepareFreezingTaskBounds();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700393 }
394 }
395
Wale Ogunwale1666e312016-12-16 11:27:18 -0800396 TaskStack getStackById(int stackId) {
397 for (int i = mChildren.size() - 1; i >= 0; i--) {
398 final DisplayContent dc = mChildren.get(i);
399 final TaskStack stack = dc.getStackById(stackId);
400 if (stack != null) {
401 return stack;
402 }
403 }
404 return null;
405 }
406
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700407 void setSecureSurfaceState(int userId, boolean disabled) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800408 forAllWindows((w) -> {
409 if (w.mHasSurface && userId == UserHandle.getUserId(w.mOwnerUid)) {
410 w.mWinAnimator.setSecureLocked(disabled);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700411 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800412 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700413 }
414
415 void updateAppOpsState() {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800416 forAllWindows((w) -> {
417 if (w.mAppOp == OP_NONE) {
418 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700419 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800420 final int mode = mService.mAppOps.checkOpNoThrow(w.mAppOp, w.getOwningUid(),
421 w.getOwningPackage());
422 w.setAppOpVisibilityLw(mode == MODE_ALLOWED || mode == MODE_DEFAULT);
423 }, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700424 }
425
426 boolean canShowStrictModeViolation(int pid) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800427 final WindowState win = getWindow((w) -> w.mSession.mPid == pid && w.isVisibleLw());
428 return win != null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700429 }
430
431 void closeSystemDialogs(String reason) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800432 mCloseSystemDialogsReason = reason;
433 forAllWindows(mCloseSystemDialogsConsumer, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700434 }
435
436 void removeReplacedWindows() {
437 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION removeReplacedWindows");
438 mService.openSurfaceTransaction();
439 try {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800440 forAllWindows(sRemoveReplacedWindowsConsumer, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700441 } finally {
442 mService.closeSurfaceTransaction();
443 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION removeReplacedWindows");
444 }
445 }
446
447 boolean hasPendingLayoutChanges(WindowAnimator animator) {
448 boolean hasChanges = false;
449
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700450 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700451 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700452 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700453 final int pendingChanges = animator.getPendingLayoutChanges(dc.getDisplayId());
454 if ((pendingChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
455 animator.mBulkUpdateParams |= SET_WALLPAPER_ACTION_PENDING;
456 }
457 if (pendingChanges != 0) {
458 hasChanges = true;
459 }
460 }
461
462 return hasChanges;
463 }
464
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700465 boolean reclaimSomeSurfaceMemory(WindowStateAnimator winAnimator, String operation,
466 boolean secure) {
467 final WindowSurfaceController surfaceController = winAnimator.mSurfaceController;
468 boolean leakedSurface = false;
469 boolean killedApps = false;
470
471 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, winAnimator.mWin.toString(),
472 winAnimator.mSession.mPid, operation);
473
474 final long callingIdentity = Binder.clearCallingIdentity();
475 try {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700476 // There was some problem...first, do a sanity check of the window list to make sure
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700477 // we haven't left any dangling surfaces around.
478
479 Slog.i(TAG_WM, "Out of memory for surface! Looking for leaks...");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700480 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700481 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700482 leakedSurface |= mChildren.get(displayNdx).destroyLeakedSurfaces();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700483 }
484
485 if (!leakedSurface) {
486 Slog.w(TAG_WM, "No leaked surfaces; killing applications!");
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700487 final SparseIntArray pidCandidates = new SparseIntArray();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700488 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800489 mChildren.get(displayNdx).forAllWindows((w) -> {
490 if (mService.mForceRemoves.contains(w)) {
491 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700492 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800493 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700494 if (wsa.mSurfaceController != null) {
495 pidCandidates.append(wsa.mSession.mPid, wsa.mSession.mPid);
496 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800497 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700498
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700499 if (pidCandidates.size() > 0) {
500 int[] pids = new int[pidCandidates.size()];
501 for (int i = 0; i < pids.length; i++) {
502 pids[i] = pidCandidates.keyAt(i);
503 }
504 try {
505 if (mService.mActivityManager.killPids(pids, "Free memory", secure)) {
506 killedApps = true;
507 }
508 } catch (RemoteException e) {
509 }
510 }
511 }
512 }
513
514 if (leakedSurface || killedApps) {
515 // We managed to reclaim some memory, so get rid of the trouble surface and ask the
516 // app to request another one.
517 Slog.w(TAG_WM,
518 "Looks like we have reclaimed some memory, clearing surface for retry.");
519 if (surfaceController != null) {
520 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) logSurface(winAnimator.mWin,
521 "RECOVER DESTROY", false);
522 winAnimator.destroySurface();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800523 if (winAnimator.mWin.mAppToken != null
524 && winAnimator.mWin.mAppToken.getController() != null) {
525 winAnimator.mWin.mAppToken.getController().removeStartingWindow();
526 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700527 }
528
529 try {
530 winAnimator.mWin.mClient.dispatchGetNewSurface();
531 } catch (RemoteException e) {
532 }
533 }
534 } finally {
535 Binder.restoreCallingIdentity(callingIdentity);
536 }
537
538 return leakedSurface || killedApps;
539 }
540
541 // "Something has changed! Let's make it correct now."
542 // TODO: Super crazy long method that should be broken down...
543 void performSurfacePlacement(boolean recoveringMemory) {
544 if (DEBUG_WINDOW_TRACE) Slog.v(TAG, "performSurfacePlacementInner: entry. Called by "
545 + Debug.getCallers(3));
546
547 int i;
548 boolean updateInputWindowsNeeded = false;
549
550 if (mService.mFocusMayChange) {
551 mService.mFocusMayChange = false;
552 updateInputWindowsNeeded = mService.updateFocusedWindowLocked(
553 UPDATE_FOCUS_WILL_PLACE_SURFACES, false /*updateInputWindows*/);
554 }
555
556 // Initialize state of exiting tokens.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700557 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700558 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700559 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800560 displayContent.setExitingTokensHasVisible(false);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700561 }
562
563 mHoldScreen = null;
564 mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700565 mUserActivityTimeout = -1;
566 mObscureApplicationContentOnSecondaryDisplays = false;
567 mSustainedPerformanceModeCurrent = false;
568 mService.mTransactionSequence++;
569
570 // TODO(multi-display):
571 final DisplayContent defaultDisplay = mService.getDefaultDisplayContentLocked();
572 final DisplayInfo defaultInfo = defaultDisplay.getDisplayInfo();
573 final int defaultDw = defaultInfo.logicalWidth;
574 final int defaultDh = defaultInfo.logicalHeight;
575
576 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
577 ">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
578 mService.openSurfaceTransaction();
579 try {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700580 applySurfaceChangesTransaction(recoveringMemory, defaultDw, defaultDh);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700581 } catch (RuntimeException e) {
582 Slog.wtf(TAG, "Unhandled exception in Window Manager", e);
583 } finally {
584 mService.closeSurfaceTransaction();
585 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
586 "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
587 }
588
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700589 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
590
591 // If we are ready to perform an app transition, check through all of the app tokens to be
592 // shown and see if they are ready to go.
593 if (mService.mAppTransition.isReady()) {
594 defaultDisplay.pendingLayoutChanges |=
Wale Ogunwale0303c572016-10-20 10:16:29 -0700595 surfacePlacer.handleAppTransitionReadyLocked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700596 if (DEBUG_LAYOUT_REPEATS)
597 surfacePlacer.debugLayoutRepeats("after handleAppTransitionReadyLocked",
598 defaultDisplay.pendingLayoutChanges);
599 }
600
601 if (!mService.mAnimator.mAppWindowAnimating && mService.mAppTransition.isRunning()) {
602 // We have finished the animation of an app transition. To do this, we have delayed a
603 // lot of operations like showing and hiding apps, moving apps in Z-order, etc. The app
604 // token list reflects the correct Z-order, but the window list may now be out of sync
605 // with it. So here we will just rebuild the entire app window list. Fun!
606 defaultDisplay.pendingLayoutChanges |=
607 mService.handleAnimatingStoppedAndTransitionLocked();
608 if (DEBUG_LAYOUT_REPEATS)
609 surfacePlacer.debugLayoutRepeats("after handleAnimStopAndXitionLock",
610 defaultDisplay.pendingLayoutChanges);
611 }
612
613 if (mWallpaperForceHidingChanged && defaultDisplay.pendingLayoutChanges == 0
614 && !mService.mAppTransition.isReady()) {
615 // At this point, there was a window with a wallpaper that was force hiding other
616 // windows behind it, but now it is going away. This may be simple -- just animate away
617 // the wallpaper and its window -- or it may be hard -- the wallpaper now needs to be
618 // shown behind something that was hidden.
619 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
620 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
621 "after animateAwayWallpaperLocked", defaultDisplay.pendingLayoutChanges);
622 }
623 mWallpaperForceHidingChanged = false;
624
625 if (mWallpaperMayChange) {
626 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Wallpaper may change! Adjusting");
627 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
628 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("WallpaperMayChange",
629 defaultDisplay.pendingLayoutChanges);
630 }
631
632 if (mService.mFocusMayChange) {
633 mService.mFocusMayChange = false;
634 if (mService.updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
635 false /*updateInputWindows*/)) {
636 updateInputWindowsNeeded = true;
637 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
638 }
639 }
640
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700641 if (isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700642 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
643 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("mLayoutNeeded",
644 defaultDisplay.pendingLayoutChanges);
645 }
646
647 for (i = mService.mResizingWindows.size() - 1; i >= 0; i--) {
648 WindowState win = mService.mResizingWindows.get(i);
649 if (win.mAppFreezing) {
650 // Don't remove this window until rotation has completed.
651 continue;
652 }
653 // Discard the saved surface if window size is changed, it can't be reused.
654 if (win.mAppToken != null) {
655 win.mAppToken.destroySavedSurfaces();
656 }
657 win.reportResized();
658 mService.mResizingWindows.remove(i);
659 }
660
661 if (DEBUG_ORIENTATION && mService.mDisplayFrozen) Slog.v(TAG,
662 "With display frozen, orientationChangeComplete=" + mOrientationChangeComplete);
663 if (mOrientationChangeComplete) {
664 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
665 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
666 mService.mLastFinishedFreezeSource = mLastWindowFreezeSource;
667 mService.mH.removeMessages(WINDOW_FREEZE_TIMEOUT);
668 }
669 mService.stopFreezingDisplayLocked();
670 }
671
672 // Destroy the surface of any windows that are no longer visible.
673 boolean wallpaperDestroyed = false;
674 i = mService.mDestroySurface.size();
675 if (i > 0) {
676 do {
677 i--;
678 WindowState win = mService.mDestroySurface.get(i);
679 win.mDestroying = false;
680 if (mService.mInputMethodWindow == win) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000681 mService.setInputMethodWindowLocked(null);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700682 }
Wale Ogunwale0303c572016-10-20 10:16:29 -0700683 if (win.getDisplayContent().mWallpaperController.isWallpaperTarget(win)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700684 wallpaperDestroyed = true;
685 }
Robert Carr89a28ab2017-04-24 15:33:11 -0700686 win.destroyOrSaveSurfaceUnchecked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700687 } while (i > 0);
688 mService.mDestroySurface.clear();
689 }
690
691 // Time to remove any exiting tokens?
692 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700693 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800694 displayContent.removeExistingTokensIfPossible();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700695 }
696
697 if (wallpaperDestroyed) {
698 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700699 defaultDisplay.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700700 }
701
702 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700703 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700704 if (displayContent.pendingLayoutChanges != 0) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700705 displayContent.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700706 }
707 }
708
709 // Finally update all input windows now that the window changes have stabilized.
710 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
711
712 mService.setHoldScreenLocked(mHoldScreen);
713 if (!mService.mDisplayFrozen) {
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200714 final int brightness = mScreenBrightness < 0 || mScreenBrightness > 1.0f
715 ? -1 : toBrightnessOverride(mScreenBrightness);
716
717 // Post these on a handler such that we don't call into power manager service while
718 // holding the window manager lock to avoid lock contention with power manager lock.
719 mHandler.obtainMessage(SET_SCREEN_BRIGHTNESS_OVERRIDE, brightness, 0).sendToTarget();
720 mHandler.obtainMessage(SET_USER_ACTIVITY_TIMEOUT, mUserActivityTimeout).sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700721 }
722
723 if (mSustainedPerformanceModeCurrent != mSustainedPerformanceModeEnabled) {
724 mSustainedPerformanceModeEnabled = mSustainedPerformanceModeCurrent;
725 mService.mPowerManagerInternal.powerHint(
Ruchi Kandoi0d434042016-10-03 09:12:02 -0700726 PowerHint.SUSTAINED_PERFORMANCE,
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700727 (mSustainedPerformanceModeEnabled ? 1 : 0));
728 }
729
730 if (mService.mTurnOnScreen) {
731 if (mService.mAllowTheaterModeWakeFromLayout
732 || Settings.Global.getInt(mService.mContext.getContentResolver(),
733 Settings.Global.THEATER_MODE_ON, 0) == 0) {
734 if (DEBUG_VISIBILITY || DEBUG_POWER) {
735 Slog.v(TAG, "Turning screen on after layout!");
736 }
737 mService.mPowerManager.wakeUp(SystemClock.uptimeMillis(),
738 "android.server.wm:TURN_ON");
739 }
740 mService.mTurnOnScreen = false;
741 }
742
743 if (mUpdateRotation) {
744 if (DEBUG_ORIENTATION) Slog.d(TAG, "Performing post-rotate rotation");
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700745 // TODO(multi-display): Update rotation for different displays separately.
746 final int displayId = defaultDisplay.getDisplayId();
Andrii Kulian06d07d62017-03-14 11:11:47 -0700747 if (defaultDisplay.updateRotationUnchecked(false /* inTransaction */)) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700748 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, displayId).sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700749 } else {
750 mUpdateRotation = false;
751 }
752 }
753
754 if (mService.mWaitingForDrawnCallback != null ||
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700755 (mOrientationChangeComplete && !defaultDisplay.isLayoutNeeded()
756 && !mUpdateRotation)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700757 mService.checkDrawnWindowsLocked();
758 }
759
760 final int N = mService.mPendingRemove.size();
761 if (N > 0) {
762 if (mService.mPendingRemoveTmp.length < N) {
763 mService.mPendingRemoveTmp = new WindowState[N+10];
764 }
765 mService.mPendingRemove.toArray(mService.mPendingRemoveTmp);
766 mService.mPendingRemove.clear();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700767 ArrayList<DisplayContent> displayList = new ArrayList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700768 for (i = 0; i < N; i++) {
769 final WindowState w = mService.mPendingRemoveTmp[i];
770 w.removeImmediately();
771 final DisplayContent displayContent = w.getDisplayContent();
772 if (displayContent != null && !displayList.contains(displayContent)) {
773 displayList.add(displayContent);
774 }
775 }
776
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700777 for (int j = displayList.size() - 1; j >= 0; --j) {
778 final DisplayContent dc = displayList.get(j);
779 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700780 }
781 }
782
783 // Remove all deferred displays stacks, tasks, and activities.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700784 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
785 mChildren.get(displayNdx).checkCompleteDeferredRemoval();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700786 }
787
788 if (updateInputWindowsNeeded) {
789 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
790 }
David Stevensee9e2772017-02-09 16:30:27 -0800791 mService.setFocusTaskRegionLocked(null);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700792
793 // Check to see if we are now in a state where the screen should
794 // be enabled, because the window obscured flags have changed.
795 mService.enableScreenIfNeededLocked();
796
797 mService.scheduleAnimationLocked();
798 mService.mWindowPlacerLocked.destroyPendingSurfaces();
799
800 if (DEBUG_WINDOW_TRACE) Slog.e(TAG,
801 "performSurfacePlacementInner exit: animating=" + mService.mAnimator.isAnimating());
802 }
803
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700804 private void applySurfaceChangesTransaction(boolean recoveringMemory, int defaultDw,
805 int defaultDh) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700806 mHoldScreenWindow = null;
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700807 mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700808
Andrii Kulian8ee72852017-03-10 10:36:45 -0800809 // TODO(multi-display): Support these features on secondary screens.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700810 if (mService.mWatermark != null) {
811 mService.mWatermark.positionSurface(defaultDw, defaultDh);
812 }
813 if (mService.mStrictModeFlash != null) {
814 mService.mStrictModeFlash.positionSurface(defaultDw, defaultDh);
815 }
816 if (mService.mCircularDisplayMask != null) {
Andrii Kulian8ee72852017-03-10 10:36:45 -0800817 mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh,
818 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700819 }
820 if (mService.mEmulatorDisplayOverlay != null) {
821 mService.mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh,
Andrii Kulian8ee72852017-03-10 10:36:45 -0800822 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700823 }
824
825 boolean focusDisplayed = false;
826
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700827 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700828 for (int j = 0; j < count; ++j) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700829 final DisplayContent dc = mChildren.get(j);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700830 focusDisplayed |= dc.applySurfaceChangesTransaction(recoveringMemory);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700831 }
832
833 if (focusDisplayed) {
834 mService.mH.sendEmptyMessage(REPORT_LOSING_FOCUS);
835 }
836
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700837 // Give the display manager a chance to adjust properties like display rotation if it needs
838 // to.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700839 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
840 }
841
842 /**
843 * @param w WindowState this method is applied to.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700844 * @param obscured True if there is a window on top of this obscuring the display.
845 * @param syswin System window?
846 * @return True when the display contains content to show the user. When false, the display
847 * manager may choose to mirror or blank the display.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700848 */
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700849 boolean handleNotObscuredLocked(WindowState w, boolean obscured, boolean syswin) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700850 final WindowManager.LayoutParams attrs = w.mAttrs;
851 final int attrFlags = attrs.flags;
852 final boolean canBeSeen = w.isDisplayedLw();
853 final int privateflags = attrs.privateFlags;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700854 boolean displayHasContent = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700855
856 if (w.mHasSurface && canBeSeen) {
857 if ((attrFlags & FLAG_KEEP_SCREEN_ON) != 0) {
858 mHoldScreen = w.mSession;
859 mHoldScreenWindow = w;
860 } else if (DEBUG_KEEP_SCREEN_ON && w == mService.mLastWakeLockHoldingWindow) {
861 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked: " + w + " was holding "
862 + "screen wakelock but no longer has FLAG_KEEP_SCREEN_ON!!! called by"
863 + Debug.getCallers(10));
864 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700865 if (!syswin && w.mAttrs.screenBrightness >= 0 && mScreenBrightness < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700866 mScreenBrightness = w.mAttrs.screenBrightness;
867 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700868 if (!syswin && w.mAttrs.userActivityTimeout >= 0 && mUserActivityTimeout < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700869 mUserActivityTimeout = w.mAttrs.userActivityTimeout;
870 }
871
872 final int type = attrs.type;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700873 // This function assumes that the contents of the default display are processed first
874 // before secondary displays.
875 final DisplayContent displayContent = w.getDisplayContent();
876 if (displayContent != null && displayContent.isDefaultDisplay) {
877 // While a dream or keyguard is showing, obscure ordinary application content on
878 // secondary displays (by forcibly enabling mirroring unless there is other content
879 // we want to show) but still allow opaque keyguard dialogs to be shown.
880 if (type == TYPE_DREAM || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
881 mObscureApplicationContentOnSecondaryDisplays = true;
882 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700883 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700884 } else if (displayContent != null &&
885 (!mObscureApplicationContentOnSecondaryDisplays
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700886 || (obscured && type == TYPE_KEYGUARD_DIALOG))) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700887 // Allow full screen keyguard presentation dialogs to be seen.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700888 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700889 }
890 if ((privateflags & PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE) != 0) {
891 mSustainedPerformanceModeCurrent = true;
892 }
893 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700894
895 return displayHasContent;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700896 }
897
898 boolean copyAnimToLayoutParams() {
899 boolean doRequest = false;
900
901 final int bulkUpdateParams = mService.mAnimator.mBulkUpdateParams;
902 if ((bulkUpdateParams & SET_UPDATE_ROTATION) != 0) {
903 mUpdateRotation = true;
904 doRequest = true;
905 }
906 if ((bulkUpdateParams & SET_WALLPAPER_MAY_CHANGE) != 0) {
907 mWallpaperMayChange = true;
908 doRequest = true;
909 }
910 if ((bulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0) {
911 mWallpaperForceHidingChanged = true;
912 doRequest = true;
913 }
914 if ((bulkUpdateParams & SET_ORIENTATION_CHANGE_COMPLETE) == 0) {
915 mOrientationChangeComplete = false;
916 } else {
917 mOrientationChangeComplete = true;
918 mLastWindowFreezeSource = mService.mAnimator.mLastWindowFreezeSource;
919 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
920 doRequest = true;
921 }
922 }
923 if ((bulkUpdateParams & SET_TURN_ON_SCREEN) != 0) {
924 mService.mTurnOnScreen = true;
925 }
926 if ((bulkUpdateParams & SET_WALLPAPER_ACTION_PENDING) != 0) {
927 mWallpaperActionPending = true;
928 }
929
930 return doRequest;
931 }
932
933 private static int toBrightnessOverride(float value) {
934 return (int)(value * PowerManager.BRIGHTNESS_ON);
935 }
936
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200937 private final class MyHandler extends Handler {
938
939 public MyHandler(Looper looper) {
940 super(looper);
941 }
942
943 @Override
944 public void handleMessage(Message msg) {
945 switch (msg.what) {
946 case SET_SCREEN_BRIGHTNESS_OVERRIDE:
947 mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(
948 msg.arg1);
949 break;
950 case SET_USER_ACTIVITY_TIMEOUT:
951 mService.mPowerManagerInternal.setUserActivityTimeoutOverrideFromWindowManager(
952 (Long) msg.obj);
953 break;
954 default:
955 break;
956 }
957 }
958 }
959
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700960 void enableSurfaceTrace(ParcelFileDescriptor pfd) {
961 final FileDescriptor fd = pfd.getFileDescriptor();
962 if (mSurfaceTraceEnabled) {
963 disableSurfaceTrace();
964 }
965 mSurfaceTraceEnabled = true;
966 mRemoteEventTrace = new RemoteEventTrace(mService, fd);
967 mSurfaceTraceFd = pfd;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700968 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
969 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700970 dc.enableSurfaceTrace(fd);
971 }
972 }
973
974 void disableSurfaceTrace() {
975 mSurfaceTraceEnabled = false;
976 mRemoteEventTrace = null;
977 mSurfaceTraceFd = null;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700978 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
979 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700980 dc.disableSurfaceTrace();
981 }
982 }
983
984 void dumpDisplayContents(PrintWriter pw) {
985 pw.println("WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)");
986 if (mService.mDisplayReady) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700987 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700988 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700989 final DisplayContent displayContent = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700990 displayContent.dump(" ", pw);
991 }
992 } else {
993 pw.println(" NO DISPLAY");
994 }
995 }
996
997 void dumpLayoutNeededDisplayIds(PrintWriter pw) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700998 if (!isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700999 return;
1000 }
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001001 pw.print(" mLayoutNeeded on displays=");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001002 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001003 for (int displayNdx = 0; displayNdx < count; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001004 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001005 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001006 pw.print(displayContent.getDisplayId());
1007 }
1008 }
1009 pw.println();
1010 }
1011
1012 void dumpWindowsNoHeader(PrintWriter pw, boolean dumpAll, ArrayList<WindowState> windows) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001013 final int[] index = new int[1];
1014 forAllWindows((w) -> {
1015 if (windows == null || windows.contains(w)) {
1016 pw.println(" Window #" + index[0] + " " + w + ":");
1017 w.dump(pw, " ", dumpAll || windows != null);
1018 index[0] = index[0] + 1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001019 }
Wale Ogunwaled1880962016-11-08 10:31:59 -08001020 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001021 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001022
Wale Ogunwale02319a62016-09-26 15:21:22 -07001023 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1024 pw.println(" All tokens:");
1025 for (int i = mChildren.size() - 1; i >= 0; --i) {
1026 mChildren.get(i).dumpTokens(pw, dumpAll);
1027 }
1028 }
1029
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001030 @Override
1031 String getName() {
1032 return "ROOT";
1033 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001034}