blob: 7bcad9fd852f3423f165f8b69e9597348121060d [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;
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -070021import android.hardware.display.DisplayManager;
Ruchi Kandoi0d434042016-10-03 09:12:02 -070022import android.hardware.power.V1_0.PowerHint;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070023import android.os.Binder;
24import android.os.Debug;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020025import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -070026import android.os.IBinder;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020027import android.os.Looper;
28import android.os.Message;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070029import android.os.ParcelFileDescriptor;
30import android.os.PowerManager;
31import android.os.RemoteException;
32import android.os.SystemClock;
33import android.os.UserHandle;
34import android.provider.Settings;
35import android.util.EventLog;
36import android.util.Slog;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070037import android.util.SparseIntArray;
38import android.view.Display;
39import android.view.DisplayInfo;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070040import android.view.WindowManager;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020041
42import com.android.internal.os.SomeArgs;
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;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080049import java.util.function.Consumer;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070050
Wale Ogunwalef7cab102016-10-25 15:25:14 -070051import static android.app.AppOpsManager.MODE_ALLOWED;
52import static android.app.AppOpsManager.MODE_DEFAULT;
53import static android.app.AppOpsManager.OP_NONE;
Wale Ogunwale02319a62016-09-26 15:21:22 -070054import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian48146fd2017-08-07 11:22:50 -070055import static android.view.Display.INVALID_DISPLAY;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070056import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070057import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070058import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE;
59import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
60import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070061import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070062import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
63import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Jorim Jaggi879ff722016-11-04 18:08:17 -070064
Wale Ogunwalee05f5012016-09-16 16:27:29 -070065import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
66import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_KEEP_SCREEN_ON;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
68import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
69import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_POWER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
71import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
72import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_TRACE;
73import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
74import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
75import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
76import static com.android.server.wm.WindowManagerDebugConfig.TAG_KEEP_SCREEN_ON;
77import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
78import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070079import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
80import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070081import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
82import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
83import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_NONE;
84import static com.android.server.wm.WindowManagerService.H.WINDOW_FREEZE_TIMEOUT;
85import static com.android.server.wm.WindowManagerService.logSurface;
86import static com.android.server.wm.WindowSurfacePlacer.SET_FORCE_HIDING_CHANGED;
87import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
88import static com.android.server.wm.WindowSurfacePlacer.SET_TURN_ON_SCREEN;
89import static com.android.server.wm.WindowSurfacePlacer.SET_UPDATE_ROTATION;
90import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_ACTION_PENDING;
91import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
92
93/** Root {@link WindowContainer} for the device. */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -070094class RootWindowContainer extends WindowContainer<DisplayContent> {
Wale Ogunwalee05f5012016-09-16 16:27:29 -070095 private static final String TAG = TAG_WITH_CLASS_NAME ? "RootWindowContainer" : TAG_WM;
96
Jorim Jaggi86c39f92017-05-02 18:02:46 +020097 private static final int SET_SCREEN_BRIGHTNESS_OVERRIDE = 1;
98 private static final int SET_USER_ACTIVITY_TIMEOUT = 2;
99
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700100 WindowManagerService mService;
101
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700102 private boolean mWallpaperForceHidingChanged = false;
103 private Object mLastWindowFreezeSource = null;
104 private Session mHoldScreen = null;
105 private float mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700106 private long mUserActivityTimeout = -1;
107 private boolean mUpdateRotation = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700108 // Following variables are for debugging screen wakelock only.
109 // Last window that requires screen wakelock
110 WindowState mHoldScreenWindow = null;
111 // Last window that obscures all windows below
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700112 WindowState mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700113 // Only set while traversing the default display based on its content.
114 // Affects the behavior of mirroring on secondary displays.
115 private boolean mObscureApplicationContentOnSecondaryDisplays = false;
116
117 private boolean mSustainedPerformanceModeEnabled = false;
118 private boolean mSustainedPerformanceModeCurrent = false;
119
120 boolean mWallpaperMayChange = false;
Robert Carr11c26c22016-09-23 12:40:27 -0700121 // During an orientation change, we track whether all windows have rendered
122 // at the new orientation, and this will be false from changing orientation until that occurs.
123 // For seamless rotation cases this always stays true, as the windows complete their orientation
124 // changes 1 by 1 without disturbing global state.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700125 boolean mOrientationChangeComplete = true;
126 boolean mWallpaperActionPending = false;
127
128 private final ArrayList<Integer> mChangedStackList = new ArrayList();
129
130 // State for the RemoteSurfaceTrace system used in testing. If this is enabled SurfaceControl
131 // instances will be replaced with an instance that writes a binary representation of all
132 // commands to mSurfaceTraceFd.
133 boolean mSurfaceTraceEnabled;
134 ParcelFileDescriptor mSurfaceTraceFd;
135 RemoteEventTrace mRemoteEventTrace;
136
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700137 private final WindowLayersController mLayersController;
Wale Ogunwale0303c572016-10-20 10:16:29 -0700138 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700139
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200140 private final Handler mHandler;
141
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800142 private String mCloseSystemDialogsReason;
143 private final Consumer<WindowState> mCloseSystemDialogsConsumer = w -> {
144 if (w.mHasSurface) {
145 try {
146 w.mClient.closeSystemDialogs(mCloseSystemDialogsReason);
147 } catch (RemoteException e) {
148 }
149 }
150 };
151
152 private static final Consumer<WindowState> sRemoveReplacedWindowsConsumer = w -> {
153 final AppWindowToken aToken = w.mAppToken;
154 if (aToken != null) {
155 aToken.removeReplacedWindowIfNeeded(w);
156 }
157 };
158
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700159 RootWindowContainer(WindowManagerService service) {
160 mService = service;
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200161 mHandler = new MyHandler(service.mH.getLooper());
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700162 mLayersController = new WindowLayersController(mService);
Wale Ogunwale0303c572016-10-20 10:16:29 -0700163 mWallpaperController = new WallpaperController(mService);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700164 }
165
166 WindowState computeFocusedWindow() {
David Stevens46939562017-03-24 13:04:00 -0700167 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700168 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700169 final WindowState win = dc.findFocusedWindow();
170 if (win != null) {
171 return win;
172 }
173 }
174 return null;
175 }
176
177 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800178 * Get an array with display ids ordered by focus priority - last items should be given
179 * focus first. Sparse array just maps position to displayId.
180 */
181 void getDisplaysInFocusOrder(SparseIntArray displaysInFocusOrder) {
182 displaysInFocusOrder.clear();
183
184 final int size = mChildren.size();
185 for (int i = 0; i < size; ++i) {
Andrii Kulian0214ed92017-05-16 13:44:05 -0700186 final DisplayContent displayContent = mChildren.get(i);
187 if (displayContent.isRemovalDeferred()) {
188 // Don't report displays that are going to be removed soon.
189 continue;
190 }
191 displaysInFocusOrder.put(i, displayContent.getDisplayId());
Andrii Kulian7fc22812016-12-28 13:04:11 -0800192 }
193 }
194
195 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700196 * Retrieve the DisplayContent for the specified displayId. Will create a new DisplayContent if
197 * there is a Display for the displayId.
198 *
199 * @param displayId The display the caller is interested in.
200 * @return The DisplayContent associated with displayId or null if there is no Display for it.
201 */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700202 DisplayContent getDisplayContentOrCreate(int displayId) {
203 DisplayContent dc = getDisplayContent(displayId);
204
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700205 if (dc == null) {
206 final Display display = mService.mDisplayManager.getDisplay(displayId);
207 if (display != null) {
Andrii Kulian325cac42017-04-20 16:02:47 -0700208 final long callingIdentity = Binder.clearCallingIdentity();
209 try {
210 dc = createDisplayContent(display);
211 } finally {
212 Binder.restoreCallingIdentity(callingIdentity);
213 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700214 }
215 }
216 return dc;
217 }
218
Wale Ogunwale02319a62016-09-26 15:21:22 -0700219 DisplayContent getDisplayContent(int displayId) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700220 for (int i = mChildren.size() - 1; i >= 0; --i) {
221 final DisplayContent current = mChildren.get(i);
222 if (current.getDisplayId() == displayId) {
223 return current;
224 }
225 }
226 return null;
227 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700228
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700229 private DisplayContent createDisplayContent(final Display display) {
Wale Ogunwale0303c572016-10-20 10:16:29 -0700230 final DisplayContent dc = new DisplayContent(display, mService, mLayersController,
231 mWallpaperController);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700232 final int displayId = display.getDisplayId();
233
234 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Adding display=" + display);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700235
236 final DisplayInfo displayInfo = dc.getDisplayInfo();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700237 final Rect rect = new Rect();
238 mService.mDisplaySettings.getOverscanLocked(displayInfo.name, displayInfo.uniqueId, rect);
239 displayInfo.overscanLeft = rect.left;
240 displayInfo.overscanTop = rect.top;
241 displayInfo.overscanRight = rect.right;
242 displayInfo.overscanBottom = rect.bottom;
243 if (mService.mDisplayManagerInternal != null) {
244 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(
245 displayId, displayInfo);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700246 mService.configureDisplayPolicyLocked(dc);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700247
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700248 // Tap Listeners are supported for:
249 // 1. All physical displays (multi-display).
250 // 2. VirtualDisplays that support virtual touch input. (Only VR for now)
251 // TODO(multi-display): Support VirtualDisplays with no virtual touch input.
252 if ((display.getType() != Display.TYPE_VIRTUAL
253 || (display.getType() == Display.TYPE_VIRTUAL
254 // Only VR VirtualDisplays
255 && displayId == mService.mVr2dDisplayId))
256 && mService.canDispatchPointerEvents()) {
257 if (DEBUG_DISPLAY) {
258 Slog.d(TAG,
259 "Registering PointerEventListener for DisplayId: " + displayId);
260 }
261 dc.mTapDetector = new TaskTapPointerEventListener(mService, dc);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700262 mService.registerPointerEventListener(dc.mTapDetector);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700263 if (displayId == DEFAULT_DISPLAY) {
264 mService.registerPointerEventListener(mService.mMousePositionTracker);
265 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700266 }
267 }
268
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700269 return dc;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700270 }
271
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700272 boolean isLayoutNeeded() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700273 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700274 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700275 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700276 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700277 return true;
278 }
279 }
280 return false;
281 }
282
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000283 void getWindowsByName(ArrayList<WindowState> output, String name) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700284 int objectId = 0;
285 // See if this is an object ID.
286 try {
287 objectId = Integer.parseInt(name, 16);
288 name = null;
289 } catch (RuntimeException e) {
290 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800291
292 getWindowsByName(output, name, objectId);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700293 }
294
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000295 private void getWindowsByName(ArrayList<WindowState> output, String name, int objectId) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800296 forAllWindows((w) -> {
297 if (name != null) {
298 if (w.mAttrs.getTitle().toString().contains(name)) {
299 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700300 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800301 } else if (System.identityHashCode(w) == objectId) {
302 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700303 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800304 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700305 }
306
Wale Ogunwale02319a62016-09-26 15:21:22 -0700307 /**
308 * Returns the app window token for the input binder if it exist in the system.
309 * NOTE: Only one AppWindowToken is allowed to exist in the system for a binder token, since
310 * AppWindowToken represents an activity which can only exist on one display.
311 */
312 AppWindowToken getAppWindowToken(IBinder binder) {
313 for (int i = mChildren.size() - 1; i >= 0; --i) {
314 final DisplayContent dc = mChildren.get(i);
315 final AppWindowToken atoken = dc.getAppWindowToken(binder);
316 if (atoken != null) {
317 return atoken;
318 }
319 }
320 return null;
321 }
322
323 /** Returns the display object the input window token is currently mapped on. */
324 DisplayContent getWindowTokenDisplay(WindowToken token) {
325 if (token == null) {
326 return null;
327 }
328
329 for (int i = mChildren.size() - 1; i >= 0; --i) {
330 final DisplayContent dc = mChildren.get(i);
331 final WindowToken current = dc.getWindowToken(token.token);
332 if (current == token) {
333 return dc;
334 }
335 }
336
337 return null;
338 }
339
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700340 /**
341 * Set new display override config and return array of ids of stacks that were changed during
342 * update. If called for the default display, global configuration will also be updated.
343 */
344 int[] setDisplayOverrideConfigurationIfNeeded(Configuration newConfiguration, int displayId) {
345 final DisplayContent displayContent = getDisplayContent(displayId);
346 if (displayContent == null) {
347 throw new IllegalArgumentException("Display not found for id: " + displayId);
348 }
349
350 final Configuration currentConfig = displayContent.getOverrideConfiguration();
351 final boolean configChanged = currentConfig.diff(newConfiguration) != 0;
352 if (!configChanged) {
353 return null;
354 }
Andrii Kuliand68501e2017-01-10 22:57:27 -0800355 displayContent.onOverrideConfigurationChanged(newConfiguration);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700356
357 if (displayId == DEFAULT_DISPLAY) {
358 // Override configuration of the default display duplicates global config. In this case
359 // we also want to update the global config.
360 return setGlobalConfigurationIfNeeded(newConfiguration);
361 } else {
362 return updateStackBoundsAfterConfigChange(displayId);
363 }
364 }
365
366 private int[] setGlobalConfigurationIfNeeded(Configuration newConfiguration) {
Andrii Kulian441e4492016-09-29 15:25:00 -0700367 final boolean configChanged = getConfiguration().diff(newConfiguration) != 0;
368 if (!configChanged) {
369 return null;
370 }
371 onConfigurationChanged(newConfiguration);
372 return updateStackBoundsAfterConfigChange();
373 }
374
375 @Override
376 void onConfigurationChanged(Configuration newParentConfig) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700377 prepareFreezingTaskBounds();
Andrii Kulian441e4492016-09-29 15:25:00 -0700378 super.onConfigurationChanged(newParentConfig);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700379
380 mService.mPolicy.onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700381 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700382
Andrii Kulian441e4492016-09-29 15:25:00 -0700383 /**
384 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
385 * bounds were updated.
386 */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700387 private int[] updateStackBoundsAfterConfigChange() {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700388 mChangedStackList.clear();
389
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700390 final int numDisplays = mChildren.size();
391 for (int i = 0; i < numDisplays; ++i) {
392 final DisplayContent dc = mChildren.get(i);
Andrii Kulian441e4492016-09-29 15:25:00 -0700393 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700394 }
395
396 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
397 }
398
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700399 /** Same as {@link #updateStackBoundsAfterConfigChange()} but only for a specific display. */
400 private int[] updateStackBoundsAfterConfigChange(int displayId) {
401 mChangedStackList.clear();
402
403 final DisplayContent dc = getDisplayContent(displayId);
404 dc.updateStackBoundsAfterConfigChange(mChangedStackList);
405
406 return mChangedStackList.isEmpty() ? null : ArrayUtils.convertToIntArray(mChangedStackList);
407 }
408
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700409 private void prepareFreezingTaskBounds() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700410 for (int i = mChildren.size() - 1; i >= 0; i--) {
411 mChildren.get(i).prepareFreezingTaskBounds();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700412 }
413 }
414
Wale Ogunwale1666e312016-12-16 11:27:18 -0800415 TaskStack getStackById(int stackId) {
416 for (int i = mChildren.size() - 1; i >= 0; i--) {
417 final DisplayContent dc = mChildren.get(i);
418 final TaskStack stack = dc.getStackById(stackId);
419 if (stack != null) {
420 return stack;
421 }
422 }
423 return null;
424 }
425
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700426 void setSecureSurfaceState(int userId, boolean disabled) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800427 forAllWindows((w) -> {
428 if (w.mHasSurface && userId == UserHandle.getUserId(w.mOwnerUid)) {
429 w.mWinAnimator.setSecureLocked(disabled);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700430 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800431 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700432 }
433
434 void updateAppOpsState() {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800435 forAllWindows((w) -> {
436 if (w.mAppOp == OP_NONE) {
437 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700438 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800439 final int mode = mService.mAppOps.checkOpNoThrow(w.mAppOp, w.getOwningUid(),
440 w.getOwningPackage());
441 w.setAppOpVisibilityLw(mode == MODE_ALLOWED || mode == MODE_DEFAULT);
442 }, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700443 }
444
445 boolean canShowStrictModeViolation(int pid) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800446 final WindowState win = getWindow((w) -> w.mSession.mPid == pid && w.isVisibleLw());
447 return win != null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700448 }
449
450 void closeSystemDialogs(String reason) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800451 mCloseSystemDialogsReason = reason;
452 forAllWindows(mCloseSystemDialogsConsumer, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700453 }
454
455 void removeReplacedWindows() {
456 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION removeReplacedWindows");
457 mService.openSurfaceTransaction();
458 try {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800459 forAllWindows(sRemoveReplacedWindowsConsumer, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700460 } finally {
461 mService.closeSurfaceTransaction();
462 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION removeReplacedWindows");
463 }
464 }
465
466 boolean hasPendingLayoutChanges(WindowAnimator animator) {
467 boolean hasChanges = false;
468
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700469 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700470 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700471 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700472 final int pendingChanges = animator.getPendingLayoutChanges(dc.getDisplayId());
473 if ((pendingChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
474 animator.mBulkUpdateParams |= SET_WALLPAPER_ACTION_PENDING;
475 }
476 if (pendingChanges != 0) {
477 hasChanges = true;
478 }
479 }
480
481 return hasChanges;
482 }
483
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700484 boolean reclaimSomeSurfaceMemory(WindowStateAnimator winAnimator, String operation,
485 boolean secure) {
486 final WindowSurfaceController surfaceController = winAnimator.mSurfaceController;
487 boolean leakedSurface = false;
488 boolean killedApps = false;
489
490 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, winAnimator.mWin.toString(),
491 winAnimator.mSession.mPid, operation);
492
493 final long callingIdentity = Binder.clearCallingIdentity();
494 try {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700495 // There was some problem...first, do a sanity check of the window list to make sure
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700496 // we haven't left any dangling surfaces around.
497
498 Slog.i(TAG_WM, "Out of memory for surface! Looking for leaks...");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700499 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700500 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700501 leakedSurface |= mChildren.get(displayNdx).destroyLeakedSurfaces();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700502 }
503
504 if (!leakedSurface) {
505 Slog.w(TAG_WM, "No leaked surfaces; killing applications!");
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700506 final SparseIntArray pidCandidates = new SparseIntArray();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700507 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800508 mChildren.get(displayNdx).forAllWindows((w) -> {
509 if (mService.mForceRemoves.contains(w)) {
510 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700511 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800512 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700513 if (wsa.mSurfaceController != null) {
514 pidCandidates.append(wsa.mSession.mPid, wsa.mSession.mPid);
515 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800516 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700517
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700518 if (pidCandidates.size() > 0) {
519 int[] pids = new int[pidCandidates.size()];
520 for (int i = 0; i < pids.length; i++) {
521 pids[i] = pidCandidates.keyAt(i);
522 }
523 try {
524 if (mService.mActivityManager.killPids(pids, "Free memory", secure)) {
525 killedApps = true;
526 }
527 } catch (RemoteException e) {
528 }
529 }
530 }
531 }
532
533 if (leakedSurface || killedApps) {
534 // We managed to reclaim some memory, so get rid of the trouble surface and ask the
535 // app to request another one.
536 Slog.w(TAG_WM,
537 "Looks like we have reclaimed some memory, clearing surface for retry.");
538 if (surfaceController != null) {
539 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) logSurface(winAnimator.mWin,
540 "RECOVER DESTROY", false);
541 winAnimator.destroySurface();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800542 if (winAnimator.mWin.mAppToken != null
543 && winAnimator.mWin.mAppToken.getController() != null) {
544 winAnimator.mWin.mAppToken.getController().removeStartingWindow();
545 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700546 }
547
548 try {
549 winAnimator.mWin.mClient.dispatchGetNewSurface();
550 } catch (RemoteException e) {
551 }
552 }
553 } finally {
554 Binder.restoreCallingIdentity(callingIdentity);
555 }
556
557 return leakedSurface || killedApps;
558 }
559
560 // "Something has changed! Let's make it correct now."
561 // TODO: Super crazy long method that should be broken down...
562 void performSurfacePlacement(boolean recoveringMemory) {
563 if (DEBUG_WINDOW_TRACE) Slog.v(TAG, "performSurfacePlacementInner: entry. Called by "
564 + Debug.getCallers(3));
565
566 int i;
567 boolean updateInputWindowsNeeded = false;
568
569 if (mService.mFocusMayChange) {
570 mService.mFocusMayChange = false;
571 updateInputWindowsNeeded = mService.updateFocusedWindowLocked(
572 UPDATE_FOCUS_WILL_PLACE_SURFACES, false /*updateInputWindows*/);
573 }
574
575 // Initialize state of exiting tokens.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700576 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700577 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700578 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800579 displayContent.setExitingTokensHasVisible(false);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700580 }
581
582 mHoldScreen = null;
583 mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700584 mUserActivityTimeout = -1;
585 mObscureApplicationContentOnSecondaryDisplays = false;
586 mSustainedPerformanceModeCurrent = false;
587 mService.mTransactionSequence++;
588
589 // TODO(multi-display):
590 final DisplayContent defaultDisplay = mService.getDefaultDisplayContentLocked();
591 final DisplayInfo defaultInfo = defaultDisplay.getDisplayInfo();
592 final int defaultDw = defaultInfo.logicalWidth;
593 final int defaultDh = defaultInfo.logicalHeight;
594
595 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
596 ">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
597 mService.openSurfaceTransaction();
598 try {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700599 applySurfaceChangesTransaction(recoveringMemory, defaultDw, defaultDh);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700600 } catch (RuntimeException e) {
601 Slog.wtf(TAG, "Unhandled exception in Window Manager", e);
602 } finally {
603 mService.closeSurfaceTransaction();
604 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
605 "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
606 }
607
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700608 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
609
610 // If we are ready to perform an app transition, check through all of the app tokens to be
611 // shown and see if they are ready to go.
612 if (mService.mAppTransition.isReady()) {
613 defaultDisplay.pendingLayoutChanges |=
Wale Ogunwale0303c572016-10-20 10:16:29 -0700614 surfacePlacer.handleAppTransitionReadyLocked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700615 if (DEBUG_LAYOUT_REPEATS)
616 surfacePlacer.debugLayoutRepeats("after handleAppTransitionReadyLocked",
617 defaultDisplay.pendingLayoutChanges);
618 }
619
620 if (!mService.mAnimator.mAppWindowAnimating && mService.mAppTransition.isRunning()) {
621 // We have finished the animation of an app transition. To do this, we have delayed a
622 // lot of operations like showing and hiding apps, moving apps in Z-order, etc. The app
623 // token list reflects the correct Z-order, but the window list may now be out of sync
624 // with it. So here we will just rebuild the entire app window list. Fun!
625 defaultDisplay.pendingLayoutChanges |=
626 mService.handleAnimatingStoppedAndTransitionLocked();
627 if (DEBUG_LAYOUT_REPEATS)
628 surfacePlacer.debugLayoutRepeats("after handleAnimStopAndXitionLock",
629 defaultDisplay.pendingLayoutChanges);
630 }
631
632 if (mWallpaperForceHidingChanged && defaultDisplay.pendingLayoutChanges == 0
633 && !mService.mAppTransition.isReady()) {
634 // At this point, there was a window with a wallpaper that was force hiding other
635 // windows behind it, but now it is going away. This may be simple -- just animate away
636 // the wallpaper and its window -- or it may be hard -- the wallpaper now needs to be
637 // shown behind something that was hidden.
638 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
639 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
640 "after animateAwayWallpaperLocked", defaultDisplay.pendingLayoutChanges);
641 }
642 mWallpaperForceHidingChanged = false;
643
644 if (mWallpaperMayChange) {
645 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Wallpaper may change! Adjusting");
646 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
647 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("WallpaperMayChange",
648 defaultDisplay.pendingLayoutChanges);
649 }
650
651 if (mService.mFocusMayChange) {
652 mService.mFocusMayChange = false;
653 if (mService.updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
654 false /*updateInputWindows*/)) {
655 updateInputWindowsNeeded = true;
656 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
657 }
658 }
659
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700660 if (isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700661 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
662 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("mLayoutNeeded",
663 defaultDisplay.pendingLayoutChanges);
664 }
665
666 for (i = mService.mResizingWindows.size() - 1; i >= 0; i--) {
667 WindowState win = mService.mResizingWindows.get(i);
668 if (win.mAppFreezing) {
669 // Don't remove this window until rotation has completed.
670 continue;
671 }
672 // Discard the saved surface if window size is changed, it can't be reused.
673 if (win.mAppToken != null) {
674 win.mAppToken.destroySavedSurfaces();
675 }
676 win.reportResized();
677 mService.mResizingWindows.remove(i);
678 }
679
680 if (DEBUG_ORIENTATION && mService.mDisplayFrozen) Slog.v(TAG,
681 "With display frozen, orientationChangeComplete=" + mOrientationChangeComplete);
682 if (mOrientationChangeComplete) {
683 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
684 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
685 mService.mLastFinishedFreezeSource = mLastWindowFreezeSource;
686 mService.mH.removeMessages(WINDOW_FREEZE_TIMEOUT);
687 }
688 mService.stopFreezingDisplayLocked();
689 }
690
691 // Destroy the surface of any windows that are no longer visible.
692 boolean wallpaperDestroyed = false;
693 i = mService.mDestroySurface.size();
694 if (i > 0) {
695 do {
696 i--;
697 WindowState win = mService.mDestroySurface.get(i);
698 win.mDestroying = false;
699 if (mService.mInputMethodWindow == win) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000700 mService.setInputMethodWindowLocked(null);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700701 }
Wale Ogunwale0303c572016-10-20 10:16:29 -0700702 if (win.getDisplayContent().mWallpaperController.isWallpaperTarget(win)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700703 wallpaperDestroyed = true;
704 }
Robert Carr89a28ab2017-04-24 15:33:11 -0700705 win.destroyOrSaveSurfaceUnchecked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700706 } while (i > 0);
707 mService.mDestroySurface.clear();
708 }
709
710 // Time to remove any exiting tokens?
711 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700712 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800713 displayContent.removeExistingTokensIfPossible();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700714 }
715
716 if (wallpaperDestroyed) {
717 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700718 defaultDisplay.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700719 }
720
721 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700722 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700723 if (displayContent.pendingLayoutChanges != 0) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700724 displayContent.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700725 }
726 }
727
728 // Finally update all input windows now that the window changes have stabilized.
729 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
730
731 mService.setHoldScreenLocked(mHoldScreen);
732 if (!mService.mDisplayFrozen) {
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200733 final int brightness = mScreenBrightness < 0 || mScreenBrightness > 1.0f
734 ? -1 : toBrightnessOverride(mScreenBrightness);
735
736 // Post these on a handler such that we don't call into power manager service while
737 // holding the window manager lock to avoid lock contention with power manager lock.
738 mHandler.obtainMessage(SET_SCREEN_BRIGHTNESS_OVERRIDE, brightness, 0).sendToTarget();
739 mHandler.obtainMessage(SET_USER_ACTIVITY_TIMEOUT, mUserActivityTimeout).sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700740 }
741
742 if (mSustainedPerformanceModeCurrent != mSustainedPerformanceModeEnabled) {
743 mSustainedPerformanceModeEnabled = mSustainedPerformanceModeCurrent;
744 mService.mPowerManagerInternal.powerHint(
Ruchi Kandoi0d434042016-10-03 09:12:02 -0700745 PowerHint.SUSTAINED_PERFORMANCE,
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700746 (mSustainedPerformanceModeEnabled ? 1 : 0));
747 }
748
749 if (mService.mTurnOnScreen) {
750 if (mService.mAllowTheaterModeWakeFromLayout
751 || Settings.Global.getInt(mService.mContext.getContentResolver(),
752 Settings.Global.THEATER_MODE_ON, 0) == 0) {
753 if (DEBUG_VISIBILITY || DEBUG_POWER) {
754 Slog.v(TAG, "Turning screen on after layout!");
755 }
756 mService.mPowerManager.wakeUp(SystemClock.uptimeMillis(),
757 "android.server.wm:TURN_ON");
758 }
759 mService.mTurnOnScreen = false;
760 }
761
762 if (mUpdateRotation) {
763 if (DEBUG_ORIENTATION) Slog.d(TAG, "Performing post-rotate rotation");
Andrii Kulian48146fd2017-08-07 11:22:50 -0700764 // TODO(multi-display): Update rotation for different displays separately.
765 final int displayId = defaultDisplay.getDisplayId();
766 if (defaultDisplay.updateRotationUnchecked(false /* inTransaction */)) {
767 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, displayId).sendToTarget();
768 } else {
769 mUpdateRotation = false;
770 }
771 // Update rotation of VR virtual display separately. Currently this is the only kind of
772 // secondary display that can be rotated because of the single-display limitations in
773 // PhoneWindowManager.
774 final DisplayContent vrDisplay = mService.mVr2dDisplayId != INVALID_DISPLAY
775 ? getDisplayContent(mService.mVr2dDisplayId) : null;
776 if (vrDisplay != null && vrDisplay.updateRotationUnchecked(false /* inTransaction */)) {
777 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mService.mVr2dDisplayId)
778 .sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700779 }
780 }
781
782 if (mService.mWaitingForDrawnCallback != null ||
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700783 (mOrientationChangeComplete && !defaultDisplay.isLayoutNeeded()
784 && !mUpdateRotation)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700785 mService.checkDrawnWindowsLocked();
786 }
787
788 final int N = mService.mPendingRemove.size();
789 if (N > 0) {
790 if (mService.mPendingRemoveTmp.length < N) {
791 mService.mPendingRemoveTmp = new WindowState[N+10];
792 }
793 mService.mPendingRemove.toArray(mService.mPendingRemoveTmp);
794 mService.mPendingRemove.clear();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700795 ArrayList<DisplayContent> displayList = new ArrayList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700796 for (i = 0; i < N; i++) {
797 final WindowState w = mService.mPendingRemoveTmp[i];
798 w.removeImmediately();
799 final DisplayContent displayContent = w.getDisplayContent();
800 if (displayContent != null && !displayList.contains(displayContent)) {
801 displayList.add(displayContent);
802 }
803 }
804
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700805 for (int j = displayList.size() - 1; j >= 0; --j) {
806 final DisplayContent dc = displayList.get(j);
807 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700808 }
809 }
810
811 // Remove all deferred displays stacks, tasks, and activities.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700812 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
813 mChildren.get(displayNdx).checkCompleteDeferredRemoval();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700814 }
815
816 if (updateInputWindowsNeeded) {
817 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
818 }
David Stevensee9e2772017-02-09 16:30:27 -0800819 mService.setFocusTaskRegionLocked(null);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700820
821 // Check to see if we are now in a state where the screen should
822 // be enabled, because the window obscured flags have changed.
823 mService.enableScreenIfNeededLocked();
824
825 mService.scheduleAnimationLocked();
826 mService.mWindowPlacerLocked.destroyPendingSurfaces();
827
828 if (DEBUG_WINDOW_TRACE) Slog.e(TAG,
829 "performSurfacePlacementInner exit: animating=" + mService.mAnimator.isAnimating());
830 }
831
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700832 private void applySurfaceChangesTransaction(boolean recoveringMemory, int defaultDw,
833 int defaultDh) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700834 mHoldScreenWindow = null;
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700835 mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700836
Andrii Kulian8ee72852017-03-10 10:36:45 -0800837 // TODO(multi-display): Support these features on secondary screens.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700838 if (mService.mWatermark != null) {
839 mService.mWatermark.positionSurface(defaultDw, defaultDh);
840 }
841 if (mService.mStrictModeFlash != null) {
842 mService.mStrictModeFlash.positionSurface(defaultDw, defaultDh);
843 }
844 if (mService.mCircularDisplayMask != null) {
Andrii Kulian8ee72852017-03-10 10:36:45 -0800845 mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh,
846 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700847 }
848 if (mService.mEmulatorDisplayOverlay != null) {
849 mService.mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh,
Andrii Kulian8ee72852017-03-10 10:36:45 -0800850 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700851 }
852
853 boolean focusDisplayed = false;
854
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700855 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700856 for (int j = 0; j < count; ++j) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700857 final DisplayContent dc = mChildren.get(j);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700858 focusDisplayed |= dc.applySurfaceChangesTransaction(recoveringMemory);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700859 }
860
861 if (focusDisplayed) {
862 mService.mH.sendEmptyMessage(REPORT_LOSING_FOCUS);
863 }
864
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700865 // Give the display manager a chance to adjust properties like display rotation if it needs
866 // to.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700867 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
868 }
869
870 /**
871 * @param w WindowState this method is applied to.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700872 * @param obscured True if there is a window on top of this obscuring the display.
873 * @param syswin System window?
874 * @return True when the display contains content to show the user. When false, the display
875 * manager may choose to mirror or blank the display.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700876 */
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700877 boolean handleNotObscuredLocked(WindowState w, boolean obscured, boolean syswin) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700878 final WindowManager.LayoutParams attrs = w.mAttrs;
879 final int attrFlags = attrs.flags;
880 final boolean canBeSeen = w.isDisplayedLw();
881 final int privateflags = attrs.privateFlags;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700882 boolean displayHasContent = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700883
884 if (w.mHasSurface && canBeSeen) {
885 if ((attrFlags & FLAG_KEEP_SCREEN_ON) != 0) {
886 mHoldScreen = w.mSession;
887 mHoldScreenWindow = w;
888 } else if (DEBUG_KEEP_SCREEN_ON && w == mService.mLastWakeLockHoldingWindow) {
889 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked: " + w + " was holding "
Wale Ogunwale743fcc12017-06-06 21:36:50 +0000890 + "screen wakelock but no longer has FLAG_KEEP_SCREEN_ON!!! called by"
891 + Debug.getCallers(10));
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700892 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700893 if (!syswin && w.mAttrs.screenBrightness >= 0 && mScreenBrightness < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700894 mScreenBrightness = w.mAttrs.screenBrightness;
895 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700896 if (!syswin && w.mAttrs.userActivityTimeout >= 0 && mUserActivityTimeout < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700897 mUserActivityTimeout = w.mAttrs.userActivityTimeout;
898 }
899
900 final int type = attrs.type;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700901 // This function assumes that the contents of the default display are processed first
902 // before secondary displays.
903 final DisplayContent displayContent = w.getDisplayContent();
904 if (displayContent != null && displayContent.isDefaultDisplay) {
905 // While a dream or keyguard is showing, obscure ordinary application content on
906 // secondary displays (by forcibly enabling mirroring unless there is other content
907 // we want to show) but still allow opaque keyguard dialogs to be shown.
908 if (type == TYPE_DREAM || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
909 mObscureApplicationContentOnSecondaryDisplays = true;
910 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700911 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700912 } else if (displayContent != null &&
913 (!mObscureApplicationContentOnSecondaryDisplays
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700914 || (obscured && type == TYPE_KEYGUARD_DIALOG))) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700915 // Allow full screen keyguard presentation dialogs to be seen.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700916 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700917 }
918 if ((privateflags & PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE) != 0) {
919 mSustainedPerformanceModeCurrent = true;
920 }
921 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700922
923 return displayHasContent;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700924 }
925
926 boolean copyAnimToLayoutParams() {
927 boolean doRequest = false;
928
929 final int bulkUpdateParams = mService.mAnimator.mBulkUpdateParams;
930 if ((bulkUpdateParams & SET_UPDATE_ROTATION) != 0) {
931 mUpdateRotation = true;
932 doRequest = true;
933 }
934 if ((bulkUpdateParams & SET_WALLPAPER_MAY_CHANGE) != 0) {
935 mWallpaperMayChange = true;
936 doRequest = true;
937 }
938 if ((bulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0) {
939 mWallpaperForceHidingChanged = true;
940 doRequest = true;
941 }
942 if ((bulkUpdateParams & SET_ORIENTATION_CHANGE_COMPLETE) == 0) {
943 mOrientationChangeComplete = false;
944 } else {
945 mOrientationChangeComplete = true;
946 mLastWindowFreezeSource = mService.mAnimator.mLastWindowFreezeSource;
947 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
948 doRequest = true;
949 }
950 }
951 if ((bulkUpdateParams & SET_TURN_ON_SCREEN) != 0) {
952 mService.mTurnOnScreen = true;
953 }
954 if ((bulkUpdateParams & SET_WALLPAPER_ACTION_PENDING) != 0) {
955 mWallpaperActionPending = true;
956 }
957
958 return doRequest;
959 }
960
961 private static int toBrightnessOverride(float value) {
962 return (int)(value * PowerManager.BRIGHTNESS_ON);
963 }
964
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200965 private final class MyHandler extends Handler {
966
967 public MyHandler(Looper looper) {
968 super(looper);
969 }
970
971 @Override
972 public void handleMessage(Message msg) {
973 switch (msg.what) {
974 case SET_SCREEN_BRIGHTNESS_OVERRIDE:
975 mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(
976 msg.arg1);
977 break;
978 case SET_USER_ACTIVITY_TIMEOUT:
979 mService.mPowerManagerInternal.setUserActivityTimeoutOverrideFromWindowManager(
980 (Long) msg.obj);
981 break;
982 default:
983 break;
984 }
985 }
986 }
987
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700988 void enableSurfaceTrace(ParcelFileDescriptor pfd) {
989 final FileDescriptor fd = pfd.getFileDescriptor();
990 if (mSurfaceTraceEnabled) {
991 disableSurfaceTrace();
992 }
993 mSurfaceTraceEnabled = true;
994 mRemoteEventTrace = new RemoteEventTrace(mService, fd);
995 mSurfaceTraceFd = pfd;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700996 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
997 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700998 dc.enableSurfaceTrace(fd);
999 }
1000 }
1001
1002 void disableSurfaceTrace() {
1003 mSurfaceTraceEnabled = false;
1004 mRemoteEventTrace = null;
1005 mSurfaceTraceFd = null;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001006 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1007 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001008 dc.disableSurfaceTrace();
1009 }
1010 }
1011
1012 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);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001018 displayContent.dump(" ", pw);
1019 }
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 Ogunwaleba51ca22016-09-23 06:06:54 -07001058 @Override
1059 String getName() {
1060 return "ROOT";
1061 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001062}