blob: 3d60ee4b5e1ee2c756c5f3f214fb4b205b50e94c [file] [log] [blame]
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070019import android.annotation.CallSuper;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070020import android.content.res.Configuration;
21import android.graphics.Rect;
Ruchi Kandoi0d434042016-10-03 09:12:02 -070022import android.hardware.power.V1_0.PowerHint;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070023import android.os.Binder;
24import android.os.Debug;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020025import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -070026import android.os.IBinder;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020027import android.os.Looper;
28import android.os.Message;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070029import android.os.ParcelFileDescriptor;
30import android.os.PowerManager;
31import android.os.RemoteException;
32import android.os.SystemClock;
33import android.os.UserHandle;
34import android.provider.Settings;
David Stevensf833ba92017-03-16 19:00:20 -070035import android.util.ArraySet;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070036import android.util.EventLog;
37import android.util.Slog;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070038import android.util.SparseIntArray;
Steven Timotiusaf03df62017-07-18 16:56:43 -070039import android.util.proto.ProtoOutputStream;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070040import android.view.Display;
41import android.view.DisplayInfo;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070042import android.view.WindowManager;
Jorim Jaggi86c39f92017-05-02 18:02:46 +020043
Wale Ogunwalee05f5012016-09-16 16:27:29 -070044import com.android.internal.util.ArrayUtils;
45import com.android.server.EventLogTags;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070046
47import java.io.FileDescriptor;
48import java.io.PrintWriter;
49import java.util.ArrayList;
Bryce Leeaea60d22018-01-31 14:42:15 -080050import java.util.List;
Wale Ogunwale1e129a42016-11-21 13:03:47 -080051import java.util.function.Consumer;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070052
Wale Ogunwalef7cab102016-10-25 15:25:14 -070053import static android.app.AppOpsManager.MODE_ALLOWED;
54import static android.app.AppOpsManager.MODE_DEFAULT;
55import static android.app.AppOpsManager.OP_NONE;
Wale Ogunwale02319a62016-09-26 15:21:22 -070056import static android.view.Display.DEFAULT_DISPLAY;
Andrii Kulian48146fd2017-08-07 11:22:50 -070057import static android.view.Display.INVALID_DISPLAY;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070058import static android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070059import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070060import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE;
61import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
62import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
Jorim Jaggi879ff722016-11-04 18:08:17 -070063
Adrian Roose99bc052017-11-20 17:55:31 +010064import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
65import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
66import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070067import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
68import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_KEEP_SCREEN_ON;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
70import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
71import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_POWER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070072import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
73import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
74import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WINDOW_TRACE;
75import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
76import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
77import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
78import static com.android.server.wm.WindowManagerDebugConfig.TAG_KEEP_SCREEN_ON;
79import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
80import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070081import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
82import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070083import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
84import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
85import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_NONE;
86import static com.android.server.wm.WindowManagerService.H.WINDOW_FREEZE_TIMEOUT;
87import static com.android.server.wm.WindowManagerService.logSurface;
88import static com.android.server.wm.WindowSurfacePlacer.SET_FORCE_HIDING_CHANGED;
89import static com.android.server.wm.WindowSurfacePlacer.SET_ORIENTATION_CHANGE_COMPLETE;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070090import static com.android.server.wm.WindowSurfacePlacer.SET_UPDATE_ROTATION;
91import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_ACTION_PENDING;
92import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -070093import static com.android.server.wm.proto.RootWindowContainerProto.DISPLAYS;
94import static com.android.server.wm.proto.RootWindowContainerProto.WINDOWS;
95import static com.android.server.wm.proto.RootWindowContainerProto.WINDOW_CONTAINER;
Wale Ogunwalee05f5012016-09-16 16:27:29 -070096
97/** Root {@link WindowContainer} for the device. */
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -070098class RootWindowContainer extends WindowContainer<DisplayContent> {
Wale Ogunwalee05f5012016-09-16 16:27:29 -070099 private static final String TAG = TAG_WITH_CLASS_NAME ? "RootWindowContainer" : TAG_WM;
100
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200101 private static final int SET_SCREEN_BRIGHTNESS_OVERRIDE = 1;
102 private static final int SET_USER_ACTIVITY_TIMEOUT = 2;
103
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700104 private boolean mWallpaperForceHidingChanged = false;
105 private Object mLastWindowFreezeSource = null;
106 private Session mHoldScreen = null;
107 private float mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700108 private long mUserActivityTimeout = -1;
109 private boolean mUpdateRotation = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700110 // Following variables are for debugging screen wakelock only.
111 // Last window that requires screen wakelock
112 WindowState mHoldScreenWindow = null;
113 // Last window that obscures all windows below
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700114 WindowState mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700115 // Only set while traversing the default display based on its content.
116 // Affects the behavior of mirroring on secondary displays.
117 private boolean mObscureApplicationContentOnSecondaryDisplays = false;
118
119 private boolean mSustainedPerformanceModeEnabled = false;
120 private boolean mSustainedPerformanceModeCurrent = false;
121
122 boolean mWallpaperMayChange = false;
Robert Carr11c26c22016-09-23 12:40:27 -0700123 // During an orientation change, we track whether all windows have rendered
124 // at the new orientation, and this will be false from changing orientation until that occurs.
125 // For seamless rotation cases this always stays true, as the windows complete their orientation
126 // changes 1 by 1 without disturbing global state.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700127 boolean mOrientationChangeComplete = true;
128 boolean mWallpaperActionPending = false;
129
Bryce Leeaea60d22018-01-31 14:42:15 -0800130 private final ArrayList<TaskStack> mTmpStackList = new ArrayList();
131 private final ArrayList<Integer> mTmpStackIds = new ArrayList<>();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700132
133 // State for the RemoteSurfaceTrace system used in testing. If this is enabled SurfaceControl
134 // instances will be replaced with an instance that writes a binary representation of all
135 // commands to mSurfaceTraceFd.
136 boolean mSurfaceTraceEnabled;
137 ParcelFileDescriptor mSurfaceTraceFd;
138 RemoteEventTrace mRemoteEventTrace;
139
Wale Ogunwale0303c572016-10-20 10:16:29 -0700140 final WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700141
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200142 private final Handler mHandler;
143
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800144 private String mCloseSystemDialogsReason;
145 private final Consumer<WindowState> mCloseSystemDialogsConsumer = w -> {
146 if (w.mHasSurface) {
147 try {
148 w.mClient.closeSystemDialogs(mCloseSystemDialogsReason);
149 } catch (RemoteException e) {
150 }
151 }
152 };
153
154 private static final Consumer<WindowState> sRemoveReplacedWindowsConsumer = w -> {
155 final AppWindowToken aToken = w.mAppToken;
156 if (aToken != null) {
157 aToken.removeReplacedWindowIfNeeded(w);
158 }
159 };
160
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700161 RootWindowContainer(WindowManagerService service) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100162 super(service);
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200163 mHandler = new MyHandler(service.mH.getLooper());
Wale Ogunwale0303c572016-10-20 10:16:29 -0700164 mWallpaperController = new WallpaperController(mService);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700165 }
166
167 WindowState computeFocusedWindow() {
David Stevens46939562017-03-24 13:04:00 -0700168 for (int i = mChildren.size() - 1; i >= 0; i--) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700169 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700170 final WindowState win = dc.findFocusedWindow();
171 if (win != null) {
172 return win;
173 }
174 }
175 return null;
176 }
177
178 /**
Andrii Kulian7fc22812016-12-28 13:04:11 -0800179 * Get an array with display ids ordered by focus priority - last items should be given
180 * focus first. Sparse array just maps position to displayId.
181 */
182 void getDisplaysInFocusOrder(SparseIntArray displaysInFocusOrder) {
183 displaysInFocusOrder.clear();
184
185 final int size = mChildren.size();
186 for (int i = 0; i < size; ++i) {
Andrii Kulian0214ed92017-05-16 13:44:05 -0700187 final DisplayContent displayContent = mChildren.get(i);
188 if (displayContent.isRemovalDeferred()) {
189 // Don't report displays that are going to be removed soon.
190 continue;
191 }
192 displaysInFocusOrder.put(i, displayContent.getDisplayId());
Andrii Kulian7fc22812016-12-28 13:04:11 -0800193 }
194 }
195
Wale Ogunwale02319a62016-09-26 15:21:22 -0700196 DisplayContent getDisplayContent(int displayId) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700197 for (int i = mChildren.size() - 1; i >= 0; --i) {
198 final DisplayContent current = mChildren.get(i);
199 if (current.getDisplayId() == displayId) {
200 return current;
201 }
202 }
203 return null;
204 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700205
Bryce Leef19cbe22018-02-02 15:09:21 -0800206 DisplayContent createDisplayContent(final Display display, DisplayWindowController controller) {
207 final DisplayContent dc =
208 new DisplayContent(display, mService, mWallpaperController, controller);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700209 final int displayId = display.getDisplayId();
210
211 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Adding display=" + display);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700212
213 final DisplayInfo displayInfo = dc.getDisplayInfo();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700214 final Rect rect = new Rect();
215 mService.mDisplaySettings.getOverscanLocked(displayInfo.name, displayInfo.uniqueId, rect);
216 displayInfo.overscanLeft = rect.left;
217 displayInfo.overscanTop = rect.top;
218 displayInfo.overscanRight = rect.right;
219 displayInfo.overscanBottom = rect.bottom;
220 if (mService.mDisplayManagerInternal != null) {
221 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(
222 displayId, displayInfo);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000223 dc.configureDisplayPolicy();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700224
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700225 // Tap Listeners are supported for:
226 // 1. All physical displays (multi-display).
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -0700227 // 2. VirtualDisplays on VR, AA (and everything else).
228 if (mService.canDispatchPointerEvents()) {
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700229 if (DEBUG_DISPLAY) {
230 Slog.d(TAG,
231 "Registering PointerEventListener for DisplayId: " + displayId);
232 }
233 dc.mTapDetector = new TaskTapPointerEventListener(mService, dc);
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700234 mService.registerPointerEventListener(dc.mTapDetector);
Tarandeep Singhe1cfcf42017-07-10 18:50:00 -0700235 if (displayId == DEFAULT_DISPLAY) {
236 mService.registerPointerEventListener(mService.mMousePositionTracker);
237 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700238 }
239 }
240
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700241 return dc;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700242 }
243
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700244 boolean isLayoutNeeded() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700245 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700246 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700247 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700248 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700249 return true;
250 }
251 }
252 return false;
253 }
254
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000255 void getWindowsByName(ArrayList<WindowState> output, String name) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700256 int objectId = 0;
257 // See if this is an object ID.
258 try {
259 objectId = Integer.parseInt(name, 16);
260 name = null;
261 } catch (RuntimeException e) {
262 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800263
264 getWindowsByName(output, name, objectId);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700265 }
266
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000267 private void getWindowsByName(ArrayList<WindowState> output, String name, int objectId) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800268 forAllWindows((w) -> {
269 if (name != null) {
270 if (w.mAttrs.getTitle().toString().contains(name)) {
271 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700272 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800273 } else if (System.identityHashCode(w) == objectId) {
274 output.add(w);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700275 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800276 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700277 }
278
Wale Ogunwale02319a62016-09-26 15:21:22 -0700279 /**
280 * Returns the app window token for the input binder if it exist in the system.
281 * NOTE: Only one AppWindowToken is allowed to exist in the system for a binder token, since
282 * AppWindowToken represents an activity which can only exist on one display.
283 */
284 AppWindowToken getAppWindowToken(IBinder binder) {
285 for (int i = mChildren.size() - 1; i >= 0; --i) {
286 final DisplayContent dc = mChildren.get(i);
287 final AppWindowToken atoken = dc.getAppWindowToken(binder);
288 if (atoken != null) {
289 return atoken;
290 }
291 }
292 return null;
293 }
294
295 /** Returns the display object the input window token is currently mapped on. */
296 DisplayContent getWindowTokenDisplay(WindowToken token) {
297 if (token == null) {
298 return null;
299 }
300
301 for (int i = mChildren.size() - 1; i >= 0; --i) {
302 final DisplayContent dc = mChildren.get(i);
303 final WindowToken current = dc.getWindowToken(token.token);
304 if (current == token) {
305 return dc;
306 }
307 }
308
309 return null;
310 }
311
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700312 /**
313 * Set new display override config and return array of ids of stacks that were changed during
Bryce Leeaea60d22018-01-31 14:42:15 -0800314 * update. If called for the default display, global configuration will also be updated. Stacks
315 * that are marked for deferred removal are excluded from the returned array.
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700316 */
317 int[] setDisplayOverrideConfigurationIfNeeded(Configuration newConfiguration, int displayId) {
318 final DisplayContent displayContent = getDisplayContent(displayId);
319 if (displayContent == null) {
320 throw new IllegalArgumentException("Display not found for id: " + displayId);
321 }
322
323 final Configuration currentConfig = displayContent.getOverrideConfiguration();
324 final boolean configChanged = currentConfig.diff(newConfiguration) != 0;
325 if (!configChanged) {
326 return null;
327 }
Bryce Leeaea60d22018-01-31 14:42:15 -0800328
Andrii Kuliand68501e2017-01-10 22:57:27 -0800329 displayContent.onOverrideConfigurationChanged(newConfiguration);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700330
Bryce Leeaea60d22018-01-31 14:42:15 -0800331 mTmpStackList.clear();
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700332 if (displayId == DEFAULT_DISPLAY) {
333 // Override configuration of the default display duplicates global config. In this case
334 // we also want to update the global config.
Bryce Leeaea60d22018-01-31 14:42:15 -0800335 setGlobalConfigurationIfNeeded(newConfiguration, mTmpStackList);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700336 } else {
Bryce Leeaea60d22018-01-31 14:42:15 -0800337 updateStackBoundsAfterConfigChange(displayId, mTmpStackList);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700338 }
Bryce Leeaea60d22018-01-31 14:42:15 -0800339
340 mTmpStackIds.clear();
341 final int stackCount = mTmpStackList.size();
342
343 for (int i = 0; i < stackCount; ++i) {
344 final TaskStack stack = mTmpStackList.get(i);
345
346 // We only include stacks that are not marked for removal as they do not exist outside
347 // of WindowManager at this point.
348 if (!stack.mDeferRemoval) {
349 mTmpStackIds.add(stack.mStackId);
350 }
351 }
352
353 return mTmpStackIds.isEmpty() ? null : ArrayUtils.convertToIntArray(mTmpStackIds);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700354 }
355
Bryce Leeaea60d22018-01-31 14:42:15 -0800356 private void setGlobalConfigurationIfNeeded(Configuration newConfiguration,
357 List<TaskStack> changedStacks) {
Andrii Kulian441e4492016-09-29 15:25:00 -0700358 final boolean configChanged = getConfiguration().diff(newConfiguration) != 0;
359 if (!configChanged) {
Bryce Leeaea60d22018-01-31 14:42:15 -0800360 return;
Andrii Kulian441e4492016-09-29 15:25:00 -0700361 }
362 onConfigurationChanged(newConfiguration);
Bryce Leeaea60d22018-01-31 14:42:15 -0800363 updateStackBoundsAfterConfigChange(changedStacks);
Andrii Kulian441e4492016-09-29 15:25:00 -0700364 }
365
366 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -0700367 public void onConfigurationChanged(Configuration newParentConfig) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700368 prepareFreezingTaskBounds();
Andrii Kulian441e4492016-09-29 15:25:00 -0700369 super.onConfigurationChanged(newParentConfig);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700370
371 mService.mPolicy.onConfigurationChanged();
Andrii Kulian441e4492016-09-29 15:25:00 -0700372 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700373
Andrii Kulian441e4492016-09-29 15:25:00 -0700374 /**
375 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
376 * bounds were updated.
377 */
Bryce Leeaea60d22018-01-31 14:42:15 -0800378 private void updateStackBoundsAfterConfigChange(List<TaskStack> changedStacks) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700379 final int numDisplays = mChildren.size();
380 for (int i = 0; i < numDisplays; ++i) {
381 final DisplayContent dc = mChildren.get(i);
Bryce Leeaea60d22018-01-31 14:42:15 -0800382 dc.updateStackBoundsAfterConfigChange(changedStacks);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700383 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700384 }
385
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700386 /** Same as {@link #updateStackBoundsAfterConfigChange()} but only for a specific display. */
Bryce Leeaea60d22018-01-31 14:42:15 -0800387 private void updateStackBoundsAfterConfigChange(int displayId, List<TaskStack> changedStacks) {
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700388 final DisplayContent dc = getDisplayContent(displayId);
Bryce Leeaea60d22018-01-31 14:42:15 -0800389 dc.updateStackBoundsAfterConfigChange(changedStacks);
Andrii Kulian5406e7a2016-10-21 11:55:23 -0700390 }
391
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700392 private void prepareFreezingTaskBounds() {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700393 for (int i = mChildren.size() - 1; i >= 0; i--) {
394 mChildren.get(i).prepareFreezingTaskBounds();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700395 }
396 }
397
Wale Ogunwale68278562017-09-23 17:13:55 -0700398 TaskStack getStack(int windowingMode, int activityType) {
399 for (int i = mChildren.size() - 1; i >= 0; i--) {
400 final DisplayContent dc = mChildren.get(i);
401 final TaskStack stack = dc.getStack(windowingMode, activityType);
402 if (stack != null) {
403 return stack;
404 }
405 }
406 return null;
407 }
408
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700409 void setSecureSurfaceState(int userId, boolean disabled) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800410 forAllWindows((w) -> {
411 if (w.mHasSurface && userId == UserHandle.getUserId(w.mOwnerUid)) {
412 w.mWinAnimator.setSecureLocked(disabled);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700413 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800414 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700415 }
416
417 void updateAppOpsState() {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800418 forAllWindows((w) -> {
419 if (w.mAppOp == OP_NONE) {
420 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700421 }
Peter Visontay96449f62017-12-11 18:50:03 +0000422 final int mode = mService.mAppOps.noteOpNoThrow(w.mAppOp, w.getOwningUid(),
Wale Ogunwaled1880962016-11-08 10:31:59 -0800423 w.getOwningPackage());
424 w.setAppOpVisibilityLw(mode == MODE_ALLOWED || mode == MODE_DEFAULT);
425 }, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700426 }
427
428 boolean canShowStrictModeViolation(int pid) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800429 final WindowState win = getWindow((w) -> w.mSession.mPid == pid && w.isVisibleLw());
430 return win != null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700431 }
432
433 void closeSystemDialogs(String reason) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800434 mCloseSystemDialogsReason = reason;
435 forAllWindows(mCloseSystemDialogsConsumer, false /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700436 }
437
438 void removeReplacedWindows() {
439 if (SHOW_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION removeReplacedWindows");
440 mService.openSurfaceTransaction();
441 try {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800442 forAllWindows(sRemoveReplacedWindowsConsumer, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700443 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200444 mService.closeSurfaceTransaction("removeReplacedWindows");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700445 if (SHOW_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION removeReplacedWindows");
446 }
447 }
448
449 boolean hasPendingLayoutChanges(WindowAnimator animator) {
450 boolean hasChanges = false;
451
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700452 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700453 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700454 final DisplayContent dc = mChildren.get(i);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700455 final int pendingChanges = animator.getPendingLayoutChanges(dc.getDisplayId());
456 if ((pendingChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
457 animator.mBulkUpdateParams |= SET_WALLPAPER_ACTION_PENDING;
458 }
459 if (pendingChanges != 0) {
460 hasChanges = true;
461 }
462 }
463
464 return hasChanges;
465 }
466
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700467 boolean reclaimSomeSurfaceMemory(WindowStateAnimator winAnimator, String operation,
468 boolean secure) {
469 final WindowSurfaceController surfaceController = winAnimator.mSurfaceController;
470 boolean leakedSurface = false;
471 boolean killedApps = false;
472
473 EventLog.writeEvent(EventLogTags.WM_NO_SURFACE_MEMORY, winAnimator.mWin.toString(),
474 winAnimator.mSession.mPid, operation);
475
476 final long callingIdentity = Binder.clearCallingIdentity();
477 try {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700478 // There was some problem...first, do a sanity check of the window list to make sure
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700479 // we haven't left any dangling surfaces around.
480
481 Slog.i(TAG_WM, "Out of memory for surface! Looking for leaks...");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700482 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700483 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700484 leakedSurface |= mChildren.get(displayNdx).destroyLeakedSurfaces();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700485 }
486
487 if (!leakedSurface) {
488 Slog.w(TAG_WM, "No leaked surfaces; killing applications!");
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700489 final SparseIntArray pidCandidates = new SparseIntArray();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700490 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaled1880962016-11-08 10:31:59 -0800491 mChildren.get(displayNdx).forAllWindows((w) -> {
492 if (mService.mForceRemoves.contains(w)) {
493 return;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700494 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800495 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700496 if (wsa.mSurfaceController != null) {
497 pidCandidates.append(wsa.mSession.mPid, wsa.mSession.mPid);
498 }
Wale Ogunwaled1880962016-11-08 10:31:59 -0800499 }, false /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700500
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700501 if (pidCandidates.size() > 0) {
502 int[] pids = new int[pidCandidates.size()];
503 for (int i = 0; i < pids.length; i++) {
504 pids[i] = pidCandidates.keyAt(i);
505 }
506 try {
507 if (mService.mActivityManager.killPids(pids, "Free memory", secure)) {
508 killedApps = true;
509 }
510 } catch (RemoteException e) {
511 }
512 }
513 }
514 }
515
516 if (leakedSurface || killedApps) {
517 // We managed to reclaim some memory, so get rid of the trouble surface and ask the
518 // app to request another one.
519 Slog.w(TAG_WM,
520 "Looks like we have reclaimed some memory, clearing surface for retry.");
521 if (surfaceController != null) {
522 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) logSurface(winAnimator.mWin,
523 "RECOVER DESTROY", false);
524 winAnimator.destroySurface();
Jorim Jaggiba41f4b2016-12-14 17:43:07 -0800525 if (winAnimator.mWin.mAppToken != null
526 && winAnimator.mWin.mAppToken.getController() != null) {
527 winAnimator.mWin.mAppToken.getController().removeStartingWindow();
528 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700529 }
530
531 try {
532 winAnimator.mWin.mClient.dispatchGetNewSurface();
533 } catch (RemoteException e) {
534 }
535 }
536 } finally {
537 Binder.restoreCallingIdentity(callingIdentity);
538 }
539
540 return leakedSurface || killedApps;
541 }
542
543 // "Something has changed! Let's make it correct now."
544 // TODO: Super crazy long method that should be broken down...
545 void performSurfacePlacement(boolean recoveringMemory) {
546 if (DEBUG_WINDOW_TRACE) Slog.v(TAG, "performSurfacePlacementInner: entry. Called by "
547 + Debug.getCallers(3));
548
549 int i;
550 boolean updateInputWindowsNeeded = false;
551
552 if (mService.mFocusMayChange) {
553 mService.mFocusMayChange = false;
554 updateInputWindowsNeeded = mService.updateFocusedWindowLocked(
555 UPDATE_FOCUS_WILL_PLACE_SURFACES, false /*updateInputWindows*/);
556 }
557
558 // Initialize state of exiting tokens.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700559 final int numDisplays = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700560 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700561 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800562 displayContent.setExitingTokensHasVisible(false);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700563 }
564
565 mHoldScreen = null;
566 mScreenBrightness = -1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700567 mUserActivityTimeout = -1;
568 mObscureApplicationContentOnSecondaryDisplays = false;
569 mSustainedPerformanceModeCurrent = false;
570 mService.mTransactionSequence++;
571
572 // TODO(multi-display):
573 final DisplayContent defaultDisplay = mService.getDefaultDisplayContentLocked();
574 final DisplayInfo defaultInfo = defaultDisplay.getDisplayInfo();
575 final int defaultDw = defaultInfo.logicalWidth;
576 final int defaultDh = defaultInfo.logicalHeight;
577
578 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
579 ">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
580 mService.openSurfaceTransaction();
581 try {
Wale Ogunwale9d9d8f12016-09-28 15:29:59 -0700582 applySurfaceChangesTransaction(recoveringMemory, defaultDw, defaultDh);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700583 } catch (RuntimeException e) {
584 Slog.wtf(TAG, "Unhandled exception in Window Manager", e);
585 } finally {
Adrian Roos111aff92017-09-27 18:11:46 +0200586 mService.closeSurfaceTransaction("performLayoutAndPlaceSurfaces");
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700587 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
588 "<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
589 }
590
Chavi Weingarten16d0d072018-02-12 23:50:28 +0000591 mService.mAnimator.executeAfterPrepareSurfacesRunnables();
592
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700593 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
594
595 // If we are ready to perform an app transition, check through all of the app tokens to be
596 // shown and see if they are ready to go.
597 if (mService.mAppTransition.isReady()) {
598 defaultDisplay.pendingLayoutChanges |=
Wale Ogunwale0303c572016-10-20 10:16:29 -0700599 surfacePlacer.handleAppTransitionReadyLocked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700600 if (DEBUG_LAYOUT_REPEATS)
601 surfacePlacer.debugLayoutRepeats("after handleAppTransitionReadyLocked",
602 defaultDisplay.pendingLayoutChanges);
603 }
604
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200605 if (!isAppAnimating() && mService.mAppTransition.isRunning()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700606 // We have finished the animation of an app transition. To do this, we have delayed a
607 // lot of operations like showing and hiding apps, moving apps in Z-order, etc. The app
608 // token list reflects the correct Z-order, but the window list may now be out of sync
609 // with it. So here we will just rebuild the entire app window list. Fun!
610 defaultDisplay.pendingLayoutChanges |=
611 mService.handleAnimatingStoppedAndTransitionLocked();
612 if (DEBUG_LAYOUT_REPEATS)
613 surfacePlacer.debugLayoutRepeats("after handleAnimStopAndXitionLock",
614 defaultDisplay.pendingLayoutChanges);
615 }
616
Winson Chunge2d72172018-01-25 17:46:20 +0000617 // Defer starting the recents animation until the wallpaper has drawn
618 final RecentsAnimationController recentsAnimationController =
619 mService.getRecentsAnimationController();
620 if (recentsAnimationController != null) {
621 recentsAnimationController.checkAnimationReady(mWallpaperController);
622 }
623
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700624 if (mWallpaperForceHidingChanged && defaultDisplay.pendingLayoutChanges == 0
625 && !mService.mAppTransition.isReady()) {
626 // At this point, there was a window with a wallpaper that was force hiding other
627 // windows behind it, but now it is going away. This may be simple -- just animate away
628 // the wallpaper and its window -- or it may be hard -- the wallpaper now needs to be
629 // shown behind something that was hidden.
630 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
631 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
632 "after animateAwayWallpaperLocked", defaultDisplay.pendingLayoutChanges);
633 }
634 mWallpaperForceHidingChanged = false;
635
636 if (mWallpaperMayChange) {
637 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Wallpaper may change! Adjusting");
638 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
639 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("WallpaperMayChange",
640 defaultDisplay.pendingLayoutChanges);
641 }
642
643 if (mService.mFocusMayChange) {
644 mService.mFocusMayChange = false;
645 if (mService.updateFocusedWindowLocked(UPDATE_FOCUS_PLACING_SURFACES,
646 false /*updateInputWindows*/)) {
647 updateInputWindowsNeeded = true;
648 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
649 }
650 }
651
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700652 if (isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700653 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
654 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("mLayoutNeeded",
655 defaultDisplay.pendingLayoutChanges);
656 }
657
David Stevensf833ba92017-03-16 19:00:20 -0700658 final ArraySet<DisplayContent> touchExcludeRegionUpdateDisplays = handleResizingWindows();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700659
660 if (DEBUG_ORIENTATION && mService.mDisplayFrozen) Slog.v(TAG,
661 "With display frozen, orientationChangeComplete=" + mOrientationChangeComplete);
662 if (mOrientationChangeComplete) {
663 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
664 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_NONE;
665 mService.mLastFinishedFreezeSource = mLastWindowFreezeSource;
666 mService.mH.removeMessages(WINDOW_FREEZE_TIMEOUT);
667 }
668 mService.stopFreezingDisplayLocked();
669 }
670
671 // Destroy the surface of any windows that are no longer visible.
672 boolean wallpaperDestroyed = false;
673 i = mService.mDestroySurface.size();
674 if (i > 0) {
675 do {
676 i--;
677 WindowState win = mService.mDestroySurface.get(i);
678 win.mDestroying = false;
679 if (mService.mInputMethodWindow == win) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000680 mService.setInputMethodWindowLocked(null);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700681 }
Wale Ogunwale0303c572016-10-20 10:16:29 -0700682 if (win.getDisplayContent().mWallpaperController.isWallpaperTarget(win)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700683 wallpaperDestroyed = true;
684 }
Jorim Jaggie7d2b852017-08-28 17:55:15 +0200685 win.destroySurfaceUnchecked();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700686 } while (i > 0);
687 mService.mDestroySurface.clear();
688 }
689
690 // Time to remove any exiting tokens?
691 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700692 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800693 displayContent.removeExistingTokensIfPossible();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700694 }
695
696 if (wallpaperDestroyed) {
697 defaultDisplay.pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700698 defaultDisplay.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700699 }
700
701 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700702 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700703 if (displayContent.pendingLayoutChanges != 0) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700704 displayContent.setLayoutNeeded();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700705 }
706 }
707
708 // Finally update all input windows now that the window changes have stabilized.
709 mService.mInputMonitor.updateInputWindowsLw(true /*force*/);
710
711 mService.setHoldScreenLocked(mHoldScreen);
712 if (!mService.mDisplayFrozen) {
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200713 final int brightness = mScreenBrightness < 0 || mScreenBrightness > 1.0f
714 ? -1 : toBrightnessOverride(mScreenBrightness);
715
716 // Post these on a handler such that we don't call into power manager service while
717 // holding the window manager lock to avoid lock contention with power manager lock.
718 mHandler.obtainMessage(SET_SCREEN_BRIGHTNESS_OVERRIDE, brightness, 0).sendToTarget();
719 mHandler.obtainMessage(SET_USER_ACTIVITY_TIMEOUT, mUserActivityTimeout).sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700720 }
721
722 if (mSustainedPerformanceModeCurrent != mSustainedPerformanceModeEnabled) {
723 mSustainedPerformanceModeEnabled = mSustainedPerformanceModeCurrent;
724 mService.mPowerManagerInternal.powerHint(
Ruchi Kandoi0d434042016-10-03 09:12:02 -0700725 PowerHint.SUSTAINED_PERFORMANCE,
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700726 (mSustainedPerformanceModeEnabled ? 1 : 0));
727 }
728
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700729 if (mUpdateRotation) {
730 if (DEBUG_ORIENTATION) Slog.d(TAG, "Performing post-rotate rotation");
Andrii Kulian48146fd2017-08-07 11:22:50 -0700731 // TODO(multi-display): Update rotation for different displays separately.
732 final int displayId = defaultDisplay.getDisplayId();
733 if (defaultDisplay.updateRotationUnchecked(false /* inTransaction */)) {
734 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, displayId).sendToTarget();
735 } else {
736 mUpdateRotation = false;
737 }
738 // Update rotation of VR virtual display separately. Currently this is the only kind of
739 // secondary display that can be rotated because of the single-display limitations in
740 // PhoneWindowManager.
741 final DisplayContent vrDisplay = mService.mVr2dDisplayId != INVALID_DISPLAY
742 ? getDisplayContent(mService.mVr2dDisplayId) : null;
743 if (vrDisplay != null && vrDisplay.updateRotationUnchecked(false /* inTransaction */)) {
744 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mService.mVr2dDisplayId)
745 .sendToTarget();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700746 }
747 }
748
749 if (mService.mWaitingForDrawnCallback != null ||
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700750 (mOrientationChangeComplete && !defaultDisplay.isLayoutNeeded()
751 && !mUpdateRotation)) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700752 mService.checkDrawnWindowsLocked();
753 }
754
755 final int N = mService.mPendingRemove.size();
756 if (N > 0) {
757 if (mService.mPendingRemoveTmp.length < N) {
758 mService.mPendingRemoveTmp = new WindowState[N+10];
759 }
760 mService.mPendingRemove.toArray(mService.mPendingRemoveTmp);
761 mService.mPendingRemove.clear();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700762 ArrayList<DisplayContent> displayList = new ArrayList();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700763 for (i = 0; i < N; i++) {
764 final WindowState w = mService.mPendingRemoveTmp[i];
765 w.removeImmediately();
766 final DisplayContent displayContent = w.getDisplayContent();
767 if (displayContent != null && !displayList.contains(displayContent)) {
768 displayList.add(displayContent);
769 }
770 }
771
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700772 for (int j = displayList.size() - 1; j >= 0; --j) {
773 final DisplayContent dc = displayList.get(j);
774 dc.assignWindowLayers(true /*setLayoutNeeded*/);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700775 }
776 }
777
778 // Remove all deferred displays stacks, tasks, and activities.
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700779 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
780 mChildren.get(displayNdx).checkCompleteDeferredRemoval();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700781 }
782
783 if (updateInputWindowsNeeded) {
784 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
785 }
David Stevensee9e2772017-02-09 16:30:27 -0800786 mService.setFocusTaskRegionLocked(null);
David Stevensf833ba92017-03-16 19:00:20 -0700787 if (touchExcludeRegionUpdateDisplays != null) {
788 final DisplayContent focusedDc = mService.mFocusedApp != null
789 ? mService.mFocusedApp.getDisplayContent() : null;
790 for (DisplayContent dc : touchExcludeRegionUpdateDisplays) {
791 // The focused DisplayContent was recalcuated in setFocusTaskRegionLocked
792 if (focusedDc != dc) {
793 dc.setTouchExcludeRegion(null /* focusedTask */);
794 }
795 }
796 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700797
798 // Check to see if we are now in a state where the screen should
799 // be enabled, because the window obscured flags have changed.
800 mService.enableScreenIfNeededLocked();
801
802 mService.scheduleAnimationLocked();
803 mService.mWindowPlacerLocked.destroyPendingSurfaces();
804
805 if (DEBUG_WINDOW_TRACE) Slog.e(TAG,
806 "performSurfacePlacementInner exit: animating=" + mService.mAnimator.isAnimating());
807 }
808
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700809 private void applySurfaceChangesTransaction(boolean recoveringMemory, int defaultDw,
810 int defaultDh) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700811 mHoldScreenWindow = null;
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700812 mObscuringWindow = null;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700813
Andrii Kulian8ee72852017-03-10 10:36:45 -0800814 // TODO(multi-display): Support these features on secondary screens.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700815 if (mService.mWatermark != null) {
816 mService.mWatermark.positionSurface(defaultDw, defaultDh);
817 }
818 if (mService.mStrictModeFlash != null) {
819 mService.mStrictModeFlash.positionSurface(defaultDw, defaultDh);
820 }
821 if (mService.mCircularDisplayMask != null) {
Andrii Kulian8ee72852017-03-10 10:36:45 -0800822 mService.mCircularDisplayMask.positionSurface(defaultDw, defaultDh,
823 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700824 }
825 if (mService.mEmulatorDisplayOverlay != null) {
826 mService.mEmulatorDisplayOverlay.positionSurface(defaultDw, defaultDh,
Andrii Kulian8ee72852017-03-10 10:36:45 -0800827 mService.getDefaultDisplayRotation());
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700828 }
829
830 boolean focusDisplayed = false;
831
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700832 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700833 for (int j = 0; j < count; ++j) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -0700834 final DisplayContent dc = mChildren.get(j);
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700835 focusDisplayed |= dc.applySurfaceChangesTransaction(recoveringMemory);
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700836 }
837
838 if (focusDisplayed) {
839 mService.mH.sendEmptyMessage(REPORT_LOSING_FOCUS);
840 }
841
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700842 // Give the display manager a chance to adjust properties like display rotation if it needs
843 // to.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700844 mService.mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
845 }
846
847 /**
David Stevensf833ba92017-03-16 19:00:20 -0700848 * Handles resizing windows during surface placement.
849 *
850 * @return A set of any DisplayContent whose touch exclude region needs to be recalculated due
851 * to a tap-exclude window resizing, or null if no such DisplayContents were found.
852 */
853 private ArraySet<DisplayContent> handleResizingWindows() {
854 ArraySet<DisplayContent> touchExcludeRegionUpdateSet = null;
855 for (int i = mService.mResizingWindows.size() - 1; i >= 0; i--) {
856 WindowState win = mService.mResizingWindows.get(i);
857 if (win.mAppFreezing) {
858 // Don't remove this window until rotation has completed.
859 continue;
860 }
861 win.reportResized();
862 mService.mResizingWindows.remove(i);
863 if (WindowManagerService.excludeWindowTypeFromTapOutTask(win.mAttrs.type)) {
864 final DisplayContent dc = win.getDisplayContent();
865 if (touchExcludeRegionUpdateSet == null) {
866 touchExcludeRegionUpdateSet = new ArraySet<>();
867 }
868 touchExcludeRegionUpdateSet.add(dc);
869 }
870 }
871 return touchExcludeRegionUpdateSet;
872 }
873
874 /**
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700875 * @param w WindowState this method is applied to.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700876 * @param obscured True if there is a window on top of this obscuring the display.
877 * @param syswin System window?
878 * @return True when the display contains content to show the user. When false, the display
879 * manager may choose to mirror or blank the display.
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700880 */
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700881 boolean handleNotObscuredLocked(WindowState w, boolean obscured, boolean syswin) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700882 final WindowManager.LayoutParams attrs = w.mAttrs;
883 final int attrFlags = attrs.flags;
Aaron Whyte8cbdf042018-01-23 12:06:02 -0800884 final boolean onScreen = w.isOnScreen();
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700885 final boolean canBeSeen = w.isDisplayedLw();
886 final int privateflags = attrs.privateFlags;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700887 boolean displayHasContent = false;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700888
Aaron Whyte8cbdf042018-01-23 12:06:02 -0800889 if (DEBUG_KEEP_SCREEN_ON) {
890 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked w: " + w
891 + ", w.mHasSurface: " + w.mHasSurface
892 + ", w.isOnScreen(): " + onScreen
893 + ", w.isDisplayedLw(): " + w.isDisplayedLw()
894 + ", w.mAttrs.userActivityTimeout: " + w.mAttrs.userActivityTimeout);
895 }
896 if (w.mHasSurface && onScreen) {
897 if (!syswin && w.mAttrs.userActivityTimeout >= 0 && mUserActivityTimeout < 0) {
898 mUserActivityTimeout = w.mAttrs.userActivityTimeout;
899 if (DEBUG_KEEP_SCREEN_ON) {
900 Slog.d(TAG, "mUserActivityTimeout set to " + mUserActivityTimeout);
901 }
902 }
903 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700904 if (w.mHasSurface && canBeSeen) {
905 if ((attrFlags & FLAG_KEEP_SCREEN_ON) != 0) {
906 mHoldScreen = w.mSession;
907 mHoldScreenWindow = w;
908 } else if (DEBUG_KEEP_SCREEN_ON && w == mService.mLastWakeLockHoldingWindow) {
909 Slog.d(TAG_KEEP_SCREEN_ON, "handleNotObscuredLocked: " + w + " was holding "
Wale Ogunwale743fcc12017-06-06 21:36:50 +0000910 + "screen wakelock but no longer has FLAG_KEEP_SCREEN_ON!!! called by"
911 + Debug.getCallers(10));
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700912 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700913 if (!syswin && w.mAttrs.screenBrightness >= 0 && mScreenBrightness < 0) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700914 mScreenBrightness = w.mAttrs.screenBrightness;
915 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700916
917 final int type = attrs.type;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700918 // This function assumes that the contents of the default display are processed first
919 // before secondary displays.
920 final DisplayContent displayContent = w.getDisplayContent();
921 if (displayContent != null && displayContent.isDefaultDisplay) {
922 // While a dream or keyguard is showing, obscure ordinary application content on
923 // secondary displays (by forcibly enabling mirroring unless there is other content
924 // we want to show) but still allow opaque keyguard dialogs to be shown.
925 if (type == TYPE_DREAM || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
926 mObscureApplicationContentOnSecondaryDisplays = true;
927 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700928 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700929 } else if (displayContent != null &&
930 (!mObscureApplicationContentOnSecondaryDisplays
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700931 || (obscured && type == TYPE_KEYGUARD_DIALOG))) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700932 // Allow full screen keyguard presentation dialogs to be seen.
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700933 displayHasContent = true;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700934 }
935 if ((privateflags & PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE) != 0) {
936 mSustainedPerformanceModeCurrent = true;
937 }
938 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700939
940 return displayHasContent;
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700941 }
942
943 boolean copyAnimToLayoutParams() {
944 boolean doRequest = false;
945
946 final int bulkUpdateParams = mService.mAnimator.mBulkUpdateParams;
947 if ((bulkUpdateParams & SET_UPDATE_ROTATION) != 0) {
948 mUpdateRotation = true;
949 doRequest = true;
950 }
951 if ((bulkUpdateParams & SET_WALLPAPER_MAY_CHANGE) != 0) {
952 mWallpaperMayChange = true;
953 doRequest = true;
954 }
955 if ((bulkUpdateParams & SET_FORCE_HIDING_CHANGED) != 0) {
956 mWallpaperForceHidingChanged = true;
957 doRequest = true;
958 }
959 if ((bulkUpdateParams & SET_ORIENTATION_CHANGE_COMPLETE) == 0) {
960 mOrientationChangeComplete = false;
961 } else {
962 mOrientationChangeComplete = true;
963 mLastWindowFreezeSource = mService.mAnimator.mLastWindowFreezeSource;
964 if (mService.mWindowsFreezingScreen != WINDOWS_FREEZING_SCREENS_NONE) {
965 doRequest = true;
966 }
967 }
chaviw042059d2018-01-11 11:24:08 -0800968
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700969 if ((bulkUpdateParams & SET_WALLPAPER_ACTION_PENDING) != 0) {
970 mWallpaperActionPending = true;
971 }
972
973 return doRequest;
974 }
975
976 private static int toBrightnessOverride(float value) {
977 return (int)(value * PowerManager.BRIGHTNESS_ON);
978 }
979
Jorim Jaggi86c39f92017-05-02 18:02:46 +0200980 private final class MyHandler extends Handler {
981
982 public MyHandler(Looper looper) {
983 super(looper);
984 }
985
986 @Override
987 public void handleMessage(Message msg) {
988 switch (msg.what) {
989 case SET_SCREEN_BRIGHTNESS_OVERRIDE:
990 mService.mPowerManagerInternal.setScreenBrightnessOverrideFromWindowManager(
991 msg.arg1);
992 break;
993 case SET_USER_ACTIVITY_TIMEOUT:
994 mService.mPowerManagerInternal.setUserActivityTimeoutOverrideFromWindowManager(
995 (Long) msg.obj);
996 break;
997 default:
998 break;
999 }
1000 }
1001 }
1002
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001003 void enableSurfaceTrace(ParcelFileDescriptor pfd) {
1004 final FileDescriptor fd = pfd.getFileDescriptor();
1005 if (mSurfaceTraceEnabled) {
1006 disableSurfaceTrace();
1007 }
1008 mSurfaceTraceEnabled = true;
1009 mRemoteEventTrace = new RemoteEventTrace(mService, fd);
1010 mSurfaceTraceFd = pfd;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001011 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1012 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001013 dc.enableSurfaceTrace(fd);
1014 }
1015 }
1016
1017 void disableSurfaceTrace() {
1018 mSurfaceTraceEnabled = false;
1019 mRemoteEventTrace = null;
1020 mSurfaceTraceFd = null;
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001021 for (int displayNdx = mChildren.size() - 1; displayNdx >= 0; --displayNdx) {
1022 final DisplayContent dc = mChildren.get(displayNdx);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001023 dc.disableSurfaceTrace();
1024 }
1025 }
1026
1027 void dumpDisplayContents(PrintWriter pw) {
1028 pw.println("WINDOW MANAGER DISPLAY CONTENTS (dumpsys window displays)");
1029 if (mService.mDisplayReady) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001030 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001031 for (int i = 0; i < count; ++i) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001032 final DisplayContent displayContent = mChildren.get(i);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02001033 displayContent.dump(pw, " ", true /* dumpAll */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001034 }
1035 } else {
1036 pw.println(" NO DISPLAY");
1037 }
1038 }
1039
1040 void dumpLayoutNeededDisplayIds(PrintWriter pw) {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001041 if (!isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001042 return;
1043 }
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001044 pw.print(" mLayoutNeeded on displays=");
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001045 final int count = mChildren.size();
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001046 for (int displayNdx = 0; displayNdx < count; ++displayNdx) {
Wale Ogunwaleeaabfab2016-09-16 17:19:52 -07001047 final DisplayContent displayContent = mChildren.get(displayNdx);
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07001048 if (displayContent.isLayoutNeeded()) {
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001049 pw.print(displayContent.getDisplayId());
1050 }
1051 }
1052 pw.println();
1053 }
1054
1055 void dumpWindowsNoHeader(PrintWriter pw, boolean dumpAll, ArrayList<WindowState> windows) {
Wale Ogunwaled1880962016-11-08 10:31:59 -08001056 final int[] index = new int[1];
1057 forAllWindows((w) -> {
1058 if (windows == null || windows.contains(w)) {
1059 pw.println(" Window #" + index[0] + " " + w + ":");
1060 w.dump(pw, " ", dumpAll || windows != null);
1061 index[0] = index[0] + 1;
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001062 }
Wale Ogunwaled1880962016-11-08 10:31:59 -08001063 }, true /* traverseTopToBottom */);
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001064 }
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001065
Wale Ogunwale02319a62016-09-26 15:21:22 -07001066 void dumpTokens(PrintWriter pw, boolean dumpAll) {
1067 pw.println(" All tokens:");
1068 for (int i = mChildren.size() - 1; i >= 0; --i) {
1069 mChildren.get(i).dumpTokens(pw, dumpAll);
1070 }
1071 }
1072
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001073 @CallSuper
1074 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02001075 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001076 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001077 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001078 if (mService.mDisplayReady) {
1079 final int count = mChildren.size();
1080 for (int i = 0; i < count; ++i) {
1081 final DisplayContent displayContent = mChildren.get(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02001082 displayContent.writeToProto(proto, DISPLAYS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001083 }
1084 }
Adrian Roos4921ccf2017-09-28 16:54:06 +02001085 if (!trim) {
1086 forAllWindows((w) -> {
1087 w.writeIdentifierToProto(proto, WINDOWS);
1088 }, true);
1089 }
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07001090 proto.end(token);
Steven Timotiusaf03df62017-07-18 16:56:43 -07001091 }
1092
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001093 @Override
1094 String getName() {
1095 return "ROOT";
1096 }
Robert Carrb1579c82017-09-05 14:54:47 -07001097
1098 @Override
1099 void scheduleAnimation() {
1100 mService.scheduleAnimationLocked();
1101 }
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001102}