blob: 410d9afe73daaf6a77ba27829ccf9d74a87c15ea [file] [log] [blame]
Jeff Brown98365d72012-08-19 20:30:52 -07001/*
2 * Copyright (C) 2012 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 android.view;
18
19import android.animation.ValueAnimator;
Alan Viverette1761cfa2019-02-13 17:44:08 -050020import android.annotation.NonNull;
Jeff Brown98365d72012-08-19 20:30:52 -070021import android.app.ActivityManager;
Artur Satayevad9254c2019-12-10 17:47:54 +000022import android.compat.annotation.UnsupportedAppUsage;
Jeff Brown98365d72012-08-19 20:30:52 -070023import android.content.ComponentCallbacks2;
Alan Viverette5e1565e2014-07-29 16:14:25 -070024import android.content.Context;
Alan Viveretted70b9e72015-05-27 14:29:20 -070025import android.content.pm.ApplicationInfo;
Jeff Brown98365d72012-08-19 20:30:52 -070026import android.content.res.Configuration;
Mathew Inwood8c854f82018-09-14 12:35:36 +010027import android.os.Build;
Jeff Brown98365d72012-08-19 20:30:52 -070028import android.os.IBinder;
Jeff Brown98365d72012-08-19 20:30:52 -070029import android.os.RemoteException;
30import android.os.ServiceManager;
31import android.os.SystemProperties;
32import android.util.AndroidRuntimeException;
Craig Mautner8f303ad2013-06-14 11:32:22 -070033import android.util.ArraySet;
Jeff Brown98365d72012-08-19 20:30:52 -070034import android.util.Log;
35import android.view.inputmethod.InputMethodManager;
Jorim Jaggi4846ee32016-01-07 17:39:12 +010036
Dianne Hackborn8c841092013-06-24 13:46:13 -070037import com.android.internal.util.FastPrintWriter;
Jeff Brown98365d72012-08-19 20:30:52 -070038
39import java.io.FileDescriptor;
40import java.io.FileOutputStream;
41import java.io.PrintWriter;
Craig Mautner652fdfa2013-06-06 07:51:57 -070042import java.util.ArrayList;
Jeff Brown98365d72012-08-19 20:30:52 -070043
44/**
45 * Provides low-level communication with the system window manager for
46 * operations that are not associated with any particular context.
47 *
48 * This class is only used internally to implement global functions where
49 * the caller already knows the display and relevant compatibility information
50 * for the operation. For most purposes, you should use {@link WindowManager} instead
51 * since it is bound to a context.
52 *
53 * @see WindowManagerImpl
54 * @hide
55 */
56public final class WindowManagerGlobal {
57 private static final String TAG = "WindowManager";
58
Valerie Haubdb905a2020-02-10 15:07:36 -080059 private static boolean sUseBLASTAdapter = false;
Valerie Hau089e5da2020-01-07 13:10:25 -080060
61 /**
Jeff Brown98365d72012-08-19 20:30:52 -070062 * The user is navigating with keys (not the touch screen), so
63 * navigational focus should be shown.
64 */
65 public static final int RELAYOUT_RES_IN_TOUCH_MODE = 0x1;
66
67 /**
68 * This is the first time the window is being drawn,
69 * so the client must call drawingFinished() when done
70 */
71 public static final int RELAYOUT_RES_FIRST_TIME = 0x2;
72
73 /**
74 * The window manager has changed the surface from the last call.
75 */
76 public static final int RELAYOUT_RES_SURFACE_CHANGED = 0x4;
77
78 /**
Jorim Jaggi4846ee32016-01-07 17:39:12 +010079 * The window is being resized by dragging on the docked divider. The client should render
80 * at (0, 0) and extend its background to the background frame passed into
81 * {@link IWindow#resized}.
82 */
83 public static final int RELAYOUT_RES_DRAG_RESIZING_DOCKED = 0x8;
84
85 /**
Chong Zhang0275e392015-09-17 10:41:44 -070086 * The window is being resized by dragging one of the window corners,
Filip Gruszczynski63a35e22015-11-05 15:38:59 -080087 * in this case the surface would be fullscreen-sized. The client should
Chong Zhang0275e392015-09-17 10:41:44 -070088 * render to the actual frame location (instead of (0,curScrollY)).
89 */
Jorim Jaggi4846ee32016-01-07 17:39:12 +010090 public static final int RELAYOUT_RES_DRAG_RESIZING_FREEFORM = 0x10;
Chong Zhang0275e392015-09-17 10:41:44 -070091
92 /**
Chong Zhangf4abc2b2015-11-12 23:40:58 -080093 * The window manager has changed the size of the surface from the last call.
94 */
Jorim Jaggi4846ee32016-01-07 17:39:12 +010095 public static final int RELAYOUT_RES_SURFACE_RESIZED = 0x20;
Chong Zhangf4abc2b2015-11-12 23:40:58 -080096
97 /**
Brad Stenninge0573692019-03-11 13:52:46 -070098 * In multi-window we force show the system bars. Because we don't want that the surface size
99 * changes in this mode, we instead have a flag whether the system bar sizes should always be
100 * consumed, so the app is treated like there is no virtual system bars at all.
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -0800101 */
Brad Stenninge0573692019-03-11 13:52:46 -0700102 public static final int RELAYOUT_RES_CONSUME_ALWAYS_SYSTEM_BARS = 0x40;
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -0800103
104 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700105 * Flag for relayout: the client will be later giving
106 * internal insets; as a result, the window will not impact other window
107 * layouts until the insets are given.
108 */
109 public static final int RELAYOUT_INSETS_PENDING = 0x1;
110
111 /**
112 * Flag for relayout: the client may be currently using the current surface,
113 * so if it is to be destroyed as a part of the relayout the destroy must
114 * be deferred until later. The client will call performDeferredDestroy()
115 * when it is okay.
116 */
117 public static final int RELAYOUT_DEFER_SURFACE_DESTROY = 0x2;
118
119 public static final int ADD_FLAG_APP_VISIBLE = 0x2;
120 public static final int ADD_FLAG_IN_TOUCH_MODE = RELAYOUT_RES_IN_TOUCH_MODE;
121
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -0800122 /**
Brad Stenninge0573692019-03-11 13:52:46 -0700123 * Like {@link #RELAYOUT_RES_CONSUME_ALWAYS_SYSTEM_BARS}, but as a "hint" when adding the
124 * window.
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -0800125 */
Brad Stenninge0573692019-03-11 13:52:46 -0700126 public static final int ADD_FLAG_ALWAYS_CONSUME_SYSTEM_BARS = 0x4;
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -0800127
Jeff Brown98365d72012-08-19 20:30:52 -0700128 public static final int ADD_OKAY = 0;
129 public static final int ADD_BAD_APP_TOKEN = -1;
130 public static final int ADD_BAD_SUBWINDOW_TOKEN = -2;
131 public static final int ADD_NOT_APP_TOKEN = -3;
132 public static final int ADD_APP_EXITING = -4;
133 public static final int ADD_DUPLICATE_ADD = -5;
134 public static final int ADD_STARTING_NOT_NEEDED = -6;
135 public static final int ADD_MULTIPLE_SINGLETON = -7;
136 public static final int ADD_PERMISSION_DENIED = -8;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700137 public static final int ADD_INVALID_DISPLAY = -9;
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800138 public static final int ADD_INVALID_TYPE = -10;
Jeff Brown98365d72012-08-19 20:30:52 -0700139
Mathew Inwooda570dee2018-08-17 14:56:00 +0100140 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700141 private static WindowManagerGlobal sDefaultWindowManager;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100142 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700143 private static IWindowManager sWindowManagerService;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100144 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700145 private static IWindowSession sWindowSession;
146
Mathew Inwooda570dee2018-08-17 14:56:00 +0100147 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700148 private final Object mLock = new Object();
149
Mathew Inwooda570dee2018-08-17 14:56:00 +0100150 @UnsupportedAppUsage
Craig Mautner652fdfa2013-06-06 07:51:57 -0700151 private final ArrayList<View> mViews = new ArrayList<View>();
Mathew Inwooda570dee2018-08-17 14:56:00 +0100152 @UnsupportedAppUsage
Craig Mautner652fdfa2013-06-06 07:51:57 -0700153 private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
Mathew Inwooda570dee2018-08-17 14:56:00 +0100154 @UnsupportedAppUsage
Craig Mautner652fdfa2013-06-06 07:51:57 -0700155 private final ArrayList<WindowManager.LayoutParams> mParams =
156 new ArrayList<WindowManager.LayoutParams>();
Craig Mautner8f303ad2013-06-14 11:32:22 -0700157 private final ArraySet<View> mDyingViews = new ArraySet<View>();
Jeff Brown98365d72012-08-19 20:30:52 -0700158
159 private Runnable mSystemPropertyUpdater;
160
161 private WindowManagerGlobal() {
162 }
163
Mathew Inwooda570dee2018-08-17 14:56:00 +0100164 @UnsupportedAppUsage
Chet Haase0d1c27a2014-11-03 18:35:16 +0000165 public static void initialize() {
166 getWindowManagerService();
167 }
168
Mathew Inwooda570dee2018-08-17 14:56:00 +0100169 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700170 public static WindowManagerGlobal getInstance() {
171 synchronized (WindowManagerGlobal.class) {
172 if (sDefaultWindowManager == null) {
173 sDefaultWindowManager = new WindowManagerGlobal();
174 }
175 return sDefaultWindowManager;
176 }
177 }
178
Mathew Inwooda570dee2018-08-17 14:56:00 +0100179 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700180 public static IWindowManager getWindowManagerService() {
181 synchronized (WindowManagerGlobal.class) {
182 if (sWindowManagerService == null) {
183 sWindowManagerService = IWindowManager.Stub.asInterface(
184 ServiceManager.getService("window"));
Chet Haase0d1c27a2014-11-03 18:35:16 +0000185 try {
Phil Weaver7eec3792017-02-14 16:51:55 -0800186 if (sWindowManagerService != null) {
187 ValueAnimator.setDurationScale(
188 sWindowManagerService.getCurrentAnimatorScale());
Valerie Haubdb905a2020-02-10 15:07:36 -0800189 sUseBLASTAdapter = sWindowManagerService.useBLAST();
Phil Weaver7eec3792017-02-14 16:51:55 -0800190 }
Chet Haase0d1c27a2014-11-03 18:35:16 +0000191 } catch (RemoteException e) {
Jeff Sharkeyd136e512016-03-09 22:30:56 -0700192 throw e.rethrowFromSystemServer();
Chet Haase0d1c27a2014-11-03 18:35:16 +0000193 }
Jeff Brown98365d72012-08-19 20:30:52 -0700194 }
195 return sWindowManagerService;
196 }
197 }
198
Mathew Inwooda570dee2018-08-17 14:56:00 +0100199 @UnsupportedAppUsage
Jeff Brownf9e989d2013-04-04 23:04:03 -0700200 public static IWindowSession getWindowSession() {
Jeff Brown98365d72012-08-19 20:30:52 -0700201 synchronized (WindowManagerGlobal.class) {
202 if (sWindowSession == null) {
203 try {
Yohei Yukawa6c075722018-09-21 14:52:12 -0700204 // Emulate the legacy behavior. The global instance of InputMethodManager
205 // was instantiated here.
206 // TODO(b/116157766): Remove this hack after cleaning up @UnsupportedAppUsage
207 InputMethodManager.ensureDefaultInstanceForDefaultDisplayIfNecessary();
Jeff Brown98365d72012-08-19 20:30:52 -0700208 IWindowManager windowManager = getWindowManagerService();
209 sWindowSession = windowManager.openSession(
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700210 new IWindowSessionCallback.Stub() {
211 @Override
212 public void onAnimatorScaleChanged(float scale) {
213 ValueAnimator.setDurationScale(scale);
214 }
Yohei Yukawaa71bb252018-09-19 19:21:24 -0700215 });
Jeff Brown98365d72012-08-19 20:30:52 -0700216 } catch (RemoteException e) {
Jeff Sharkeyd136e512016-03-09 22:30:56 -0700217 throw e.rethrowFromSystemServer();
Jeff Brown98365d72012-08-19 20:30:52 -0700218 }
219 }
220 return sWindowSession;
221 }
222 }
223
Mathew Inwood8c854f82018-09-14 12:35:36 +0100224 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Brown98365d72012-08-19 20:30:52 -0700225 public static IWindowSession peekWindowSession() {
226 synchronized (WindowManagerGlobal.class) {
227 return sWindowSession;
228 }
229 }
230
Valerie Hau779af292020-02-07 10:53:32 -0800231 /**
232 * Whether or not to use BLAST for ViewRootImpl
233 */
Valerie Haubdb905a2020-02-10 15:07:36 -0800234 public static boolean useBLAST() {
235 return sUseBLASTAdapter;
Valerie Hau779af292020-02-07 10:53:32 -0800236 }
237
Mathew Inwooda570dee2018-08-17 14:56:00 +0100238 @UnsupportedAppUsage
Siva Velusamy945bfb62013-01-06 16:03:12 -0800239 public String[] getViewRootNames() {
240 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700241 final int numRoots = mRoots.size();
242 String[] mViewRoots = new String[numRoots];
243 for (int i = 0; i < numRoots; ++i) {
244 mViewRoots[i] = getWindowName(mRoots.get(i));
Siva Velusamy945bfb62013-01-06 16:03:12 -0800245 }
246 return mViewRoots;
247 }
248 }
249
Mathew Inwooda570dee2018-08-17 14:56:00 +0100250 @UnsupportedAppUsage
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800251 public ArrayList<ViewRootImpl> getRootViews(IBinder token) {
252 ArrayList<ViewRootImpl> views = new ArrayList<>();
253 synchronized (mLock) {
254 final int numRoots = mRoots.size();
255 for (int i = 0; i < numRoots; ++i) {
256 WindowManager.LayoutParams params = mParams.get(i);
257 if (params.token == null) {
258 continue;
259 }
260 if (params.token != token) {
261 boolean isChild = false;
262 if (params.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW
263 && params.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
264 for (int j = 0 ; j < numRoots; ++j) {
265 View viewj = mViews.get(j);
266 WindowManager.LayoutParams paramsj = mParams.get(j);
267 if (params.token == viewj.getWindowToken()
268 && paramsj.token == token) {
269 isChild = true;
270 break;
271 }
272 }
273 }
274 if (!isChild) {
275 continue;
276 }
277 }
278 views.add(mRoots.get(i));
279 }
280 }
281 return views;
282 }
283
Alan Viverette1761cfa2019-02-13 17:44:08 -0500284 /**
285 * @return the list of all views attached to the global window manager
286 */
287 @NonNull
288 public ArrayList<View> getWindowViews() {
289 synchronized (mLock) {
290 return new ArrayList<>(mViews);
291 }
292 }
293
Vladislav Kaznacheev7039cbc2017-01-04 10:15:31 -0800294 public View getWindowView(IBinder windowToken) {
295 synchronized (mLock) {
296 final int numViews = mViews.size();
297 for (int i = 0; i < numViews; ++i) {
298 final View view = mViews.get(i);
299 if (view.getWindowToken() == windowToken) {
300 return view;
301 }
302 }
303 }
304 return null;
305 }
306
Mathew Inwooda570dee2018-08-17 14:56:00 +0100307 @UnsupportedAppUsage
Siva Velusamy945bfb62013-01-06 16:03:12 -0800308 public View getRootView(String name) {
309 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700310 for (int i = mRoots.size() - 1; i >= 0; --i) {
311 final ViewRootImpl root = mRoots.get(i);
Siva Velusamy945bfb62013-01-06 16:03:12 -0800312 if (name.equals(getWindowName(root))) return root.getView();
313 }
314 }
315
316 return null;
317 }
318
Jeff Brown98365d72012-08-19 20:30:52 -0700319 public void addView(View view, ViewGroup.LayoutParams params,
320 Display display, Window parentWindow) {
321 if (view == null) {
322 throw new IllegalArgumentException("view must not be null");
323 }
324 if (display == null) {
325 throw new IllegalArgumentException("display must not be null");
326 }
327 if (!(params instanceof WindowManager.LayoutParams)) {
328 throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
329 }
330
Alan Viverette9ecb73c2014-12-15 13:40:28 -0800331 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
Jeff Brown98365d72012-08-19 20:30:52 -0700332 if (parentWindow != null) {
333 parentWindow.adjustLayoutParamsForSubWindow(wparams);
Alan Viverette9b0ab652015-03-18 14:21:04 -0700334 } else {
335 // If there's no parent, then hardware acceleration for this view is
336 // set from the application's hardware acceleration setting.
Alan Viverette5e1565e2014-07-29 16:14:25 -0700337 final Context context = view.getContext();
Alan Viverette9b0ab652015-03-18 14:21:04 -0700338 if (context != null
Alan Viveretted70b9e72015-05-27 14:29:20 -0700339 && (context.getApplicationInfo().flags
340 & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
Alan Viverette5e1565e2014-07-29 16:14:25 -0700341 wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
342 }
Jeff Brown98365d72012-08-19 20:30:52 -0700343 }
344
345 ViewRootImpl root;
346 View panelParentView = null;
347
348 synchronized (mLock) {
349 // Start watching for system property changes.
350 if (mSystemPropertyUpdater == null) {
351 mSystemPropertyUpdater = new Runnable() {
352 @Override public void run() {
353 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700354 for (int i = mRoots.size() - 1; i >= 0; --i) {
355 mRoots.get(i).loadSystemProperties();
Jeff Brown98365d72012-08-19 20:30:52 -0700356 }
357 }
358 }
359 };
360 SystemProperties.addChangeCallback(mSystemPropertyUpdater);
361 }
362
363 int index = findViewLocked(view, false);
364 if (index >= 0) {
Craig Mautner8f303ad2013-06-14 11:32:22 -0700365 if (mDyingViews.contains(view)) {
366 // Don't wait for MSG_DIE to make it's way through root's queue.
367 mRoots.get(index).doDie();
368 } else {
369 throw new IllegalStateException("View " + view
370 + " has already been added to the window manager.");
371 }
372 // The previous removeView() had not completed executing. Now it has.
Jeff Brown98365d72012-08-19 20:30:52 -0700373 }
374
375 // If this is a panel window, then find the window it is being
376 // attached to for future reference.
377 if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
378 wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700379 final int count = mViews.size();
380 for (int i = 0; i < count; i++) {
381 if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
382 panelParentView = mViews.get(i);
Jeff Brown98365d72012-08-19 20:30:52 -0700383 }
384 }
385 }
386
387 root = new ViewRootImpl(view.getContext(), display);
388
389 view.setLayoutParams(wparams);
390
Craig Mautner652fdfa2013-06-06 07:51:57 -0700391 mViews.add(view);
392 mRoots.add(root);
393 mParams.add(wparams);
Jeff Brown98365d72012-08-19 20:30:52 -0700394
Gopal Krishna Shuklaf7abcda2016-07-06 08:14:59 +0530395 // do this last because it fires off messages to start doing things
396 try {
397 root.setView(view, wparams, panelParentView);
398 } catch (RuntimeException e) {
399 // BadTokenException or InvalidDisplayException, clean up.
Craig Mautner6018aee2012-10-23 14:27:49 -0700400 if (index >= 0) {
401 removeViewLocked(index, true);
402 }
Gopal Krishna Shuklaf7abcda2016-07-06 08:14:59 +0530403 throw e;
Craig Mautner6018aee2012-10-23 14:27:49 -0700404 }
Craig Mautner6018aee2012-10-23 14:27:49 -0700405 }
Jeff Brown98365d72012-08-19 20:30:52 -0700406 }
407
408 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
409 if (view == null) {
410 throw new IllegalArgumentException("view must not be null");
411 }
412 if (!(params instanceof WindowManager.LayoutParams)) {
413 throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
414 }
415
416 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;
417
418 view.setLayoutParams(wparams);
419
420 synchronized (mLock) {
421 int index = findViewLocked(view, true);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700422 ViewRootImpl root = mRoots.get(index);
423 mParams.remove(index);
424 mParams.add(index, wparams);
Jeff Brown98365d72012-08-19 20:30:52 -0700425 root.setLayoutParams(wparams, false);
426 }
427 }
428
Mathew Inwooda570dee2018-08-17 14:56:00 +0100429 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700430 public void removeView(View view, boolean immediate) {
431 if (view == null) {
432 throw new IllegalArgumentException("view must not be null");
433 }
434
435 synchronized (mLock) {
436 int index = findViewLocked(view, true);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700437 View curView = mRoots.get(index).getView();
Craig Mautner05eb7302013-06-03 17:24:21 -0700438 removeViewLocked(index, immediate);
Jeff Brown98365d72012-08-19 20:30:52 -0700439 if (curView == view) {
440 return;
441 }
442
443 throw new IllegalStateException("Calling with view " + view
444 + " but the ViewAncestor is attached to " + curView);
445 }
446 }
447
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700448 /**
449 * Remove all roots with specified token.
450 *
451 * @param token app or window token.
452 * @param who name of caller, used in logs.
453 * @param what type of caller, used in logs.
454 */
Jeff Brown98365d72012-08-19 20:30:52 -0700455 public void closeAll(IBinder token, String who, String what) {
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700456 closeAllExceptView(token, null /* view */, who, what);
457 }
458
459 /**
460 * Remove all roots with specified token, except maybe one view.
461 *
462 * @param token app or window token.
463 * @param view view that should be should be preserved along with it's root.
464 * Pass null if everything should be removed.
465 * @param who name of caller, used in logs.
466 * @param what type of caller, used in logs.
467 */
468 public void closeAllExceptView(IBinder token, View view, String who, String what) {
Jeff Brown98365d72012-08-19 20:30:52 -0700469 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700470 int count = mViews.size();
Craig Mautner05eb7302013-06-03 17:24:21 -0700471 for (int i = 0; i < count; i++) {
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700472 if ((view == null || mViews.get(i) != view)
473 && (token == null || mParams.get(i).token == token)) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700474 ViewRootImpl root = mRoots.get(i);
Jeff Brown98365d72012-08-19 20:30:52 -0700475
Jeff Brown98365d72012-08-19 20:30:52 -0700476 if (who != null) {
477 WindowLeaked leak = new WindowLeaked(
478 what + " " + who + " has leaked window "
479 + root.getView() + " that was originally added here");
480 leak.setStackTrace(root.getLocation().getStackTrace());
Craig Mautner05eb7302013-06-03 17:24:21 -0700481 Log.e(TAG, "", leak);
Jeff Brown98365d72012-08-19 20:30:52 -0700482 }
483
484 removeViewLocked(i, false);
Jeff Brown98365d72012-08-19 20:30:52 -0700485 }
486 }
487 }
488 }
489
Craig Mautner05eb7302013-06-03 17:24:21 -0700490 private void removeViewLocked(int index, boolean immediate) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700491 ViewRootImpl root = mRoots.get(index);
Jeff Brown98365d72012-08-19 20:30:52 -0700492 View view = root.getView();
493
lumark970d9d22019-08-18 17:45:24 +0800494 if (root != null) {
495 root.getImeFocusController().onWindowDismissed();
Jeff Brown98365d72012-08-19 20:30:52 -0700496 }
Craig Mautner8f303ad2013-06-14 11:32:22 -0700497 boolean deferred = root.die(immediate);
Craig Mautner92098c72013-06-10 11:27:26 -0700498 if (view != null) {
499 view.assignParent(null);
Craig Mautner8f303ad2013-06-14 11:32:22 -0700500 if (deferred) {
501 mDyingViews.add(view);
502 }
Craig Mautner92098c72013-06-10 11:27:26 -0700503 }
Craig Mautner05eb7302013-06-03 17:24:21 -0700504 }
Jeff Brown98365d72012-08-19 20:30:52 -0700505
Craig Mautner05eb7302013-06-03 17:24:21 -0700506 void doRemoveView(ViewRootImpl root) {
Jorim Jaggi6d5c8012020-02-28 01:40:27 +0100507 boolean allViewsRemoved;
Craig Mautner05eb7302013-06-03 17:24:21 -0700508 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700509 final int index = mRoots.indexOf(root);
510 if (index >= 0) {
511 mRoots.remove(index);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700512 mParams.remove(index);
Craig Mautner8f303ad2013-06-14 11:32:22 -0700513 final View view = mViews.remove(index);
514 mDyingViews.remove(view);
Jeff Brown98365d72012-08-19 20:30:52 -0700515 }
Jorim Jaggi6d5c8012020-02-28 01:40:27 +0100516 allViewsRemoved = mRoots.isEmpty();
Jeff Brown98365d72012-08-19 20:30:52 -0700517 }
John Reck51aaf902015-12-02 15:08:07 -0800518 if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
John Reck73840ea2014-09-22 07:39:18 -0700519 doTrimForeground();
520 }
Jorim Jaggi6d5c8012020-02-28 01:40:27 +0100521
522 // If we don't have any views anymore in our process, we no longer need the
523 // InsetsAnimationThread to save some resources.
524 if (allViewsRemoved) {
525 InsetsAnimationThread.release();
526 }
Jeff Brown98365d72012-08-19 20:30:52 -0700527 }
528
529 private int findViewLocked(View view, boolean required) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700530 final int index = mViews.indexOf(view);
531 if (required && index < 0) {
Craig Mautner05eb7302013-06-03 17:24:21 -0700532 throw new IllegalArgumentException("View=" + view + " not attached to window manager");
Craig Mautner6018aee2012-10-23 14:27:49 -0700533 }
Craig Mautner652fdfa2013-06-06 07:51:57 -0700534 return index;
Jeff Brown98365d72012-08-19 20:30:52 -0700535 }
536
John Reckf47a5942014-06-30 16:20:04 -0700537 public static boolean shouldDestroyEglContext(int trimLevel) {
538 // On low-end gfx devices we trim when memory is moderate;
539 // on high-end devices we do this when low.
540 if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
541 return true;
542 }
543 if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_MODERATE
544 && !ActivityManager.isHighEndGfx()) {
545 return true;
546 }
547 return false;
548 }
549
Sergey Vasilinets8b8d3b82019-02-08 14:27:31 +0000550 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
John Reckf47a5942014-06-30 16:20:04 -0700551 public void trimMemory(int level) {
John Reck51aaf902015-12-02 15:08:07 -0800552 if (ThreadedRenderer.isAvailable()) {
John Reckf47a5942014-06-30 16:20:04 -0700553 if (shouldDestroyEglContext(level)) {
Jeff Brown98365d72012-08-19 20:30:52 -0700554 // Destroy all hardware surfaces and resources associated to
555 // known windows
556 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700557 for (int i = mRoots.size() - 1; i >= 0; --i) {
Romain Guy46bfc482013-08-16 18:38:29 -0700558 mRoots.get(i).destroyHardwareResources();
Jeff Brown98365d72012-08-19 20:30:52 -0700559 }
560 }
561 // Force a full memory flush
John Reckf47a5942014-06-30 16:20:04 -0700562 level = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
Jeff Brown98365d72012-08-19 20:30:52 -0700563 }
564
John Reck51aaf902015-12-02 15:08:07 -0800565 ThreadedRenderer.trimMemory(level);
John Reck73840ea2014-09-22 07:39:18 -0700566
John Reck51aaf902015-12-02 15:08:07 -0800567 if (ThreadedRenderer.sTrimForeground) {
John Reck73840ea2014-09-22 07:39:18 -0700568 doTrimForeground();
569 }
570 }
571 }
572
573 public static void trimForeground() {
John Reck51aaf902015-12-02 15:08:07 -0800574 if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
John Reck73840ea2014-09-22 07:39:18 -0700575 WindowManagerGlobal wm = WindowManagerGlobal.getInstance();
576 wm.doTrimForeground();
577 }
578 }
579
580 private void doTrimForeground() {
581 boolean hasVisibleWindows = false;
582 synchronized (mLock) {
583 for (int i = mRoots.size() - 1; i >= 0; --i) {
John Reckccf2fa02014-09-25 08:33:05 -0700584 final ViewRootImpl root = mRoots.get(i);
585 if (root.mView != null && root.getHostVisibility() == View.VISIBLE
Stan Iliev45faba52016-06-28 13:33:15 -0400586 && root.mAttachInfo.mThreadedRenderer != null) {
John Reck73840ea2014-09-22 07:39:18 -0700587 hasVisibleWindows = true;
588 } else {
John Reckccf2fa02014-09-25 08:33:05 -0700589 root.destroyHardwareResources();
John Reck73840ea2014-09-22 07:39:18 -0700590 }
591 }
592 }
593 if (!hasVisibleWindows) {
John Reck51aaf902015-12-02 15:08:07 -0800594 ThreadedRenderer.trimMemory(
John Reck73840ea2014-09-22 07:39:18 -0700595 ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
Jeff Brown98365d72012-08-19 20:30:52 -0700596 }
597 }
598
John Reckba6adf62015-02-19 14:36:50 -0800599 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
Jeff Brown98365d72012-08-19 20:30:52 -0700600 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700601 PrintWriter pw = new FastPrintWriter(fout);
Jeff Brown98365d72012-08-19 20:30:52 -0700602 try {
603 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700604 final int count = mViews.size();
Jeff Brown98365d72012-08-19 20:30:52 -0700605
Craig Mautner652fdfa2013-06-06 07:51:57 -0700606 pw.println("Profile data in ms:");
Jeff Brown98365d72012-08-19 20:30:52 -0700607
Craig Mautner652fdfa2013-06-06 07:51:57 -0700608 for (int i = 0; i < count; i++) {
609 ViewRootImpl root = mRoots.get(i);
610 String name = getWindowName(root);
John Reck73840ea2014-09-22 07:39:18 -0700611 pw.printf("\n\t%s (visibility=%d)", name, root.getHostVisibility());
Jeff Brown98365d72012-08-19 20:30:52 -0700612
John Reck51aaf902015-12-02 15:08:07 -0800613 ThreadedRenderer renderer =
Stan Iliev45faba52016-06-28 13:33:15 -0400614 root.getView().mAttachInfo.mThreadedRenderer;
Craig Mautner652fdfa2013-06-06 07:51:57 -0700615 if (renderer != null) {
John Reckba6adf62015-02-19 14:36:50 -0800616 renderer.dumpGfxInfo(pw, fd, args);
Jeff Brown98365d72012-08-19 20:30:52 -0700617 }
Jeff Brown98365d72012-08-19 20:30:52 -0700618 }
Craig Mautner652fdfa2013-06-06 07:51:57 -0700619
620 pw.println("\nView hierarchy:\n");
621
John Reck183e1382019-10-09 13:41:18 -0700622 ViewRootImpl.GfxInfo totals = new ViewRootImpl.GfxInfo();
Craig Mautner652fdfa2013-06-06 07:51:57 -0700623
624 for (int i = 0; i < count; i++) {
625 ViewRootImpl root = mRoots.get(i);
John Reck183e1382019-10-09 13:41:18 -0700626 ViewRootImpl.GfxInfo info = root.getGfxInfo();
627 totals.add(info);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700628
629 String name = getWindowName(root);
John Reck183e1382019-10-09 13:41:18 -0700630 pw.printf(" %s\n %d views, %.2f kB of render nodes",
631 name, info.viewCount, info.renderNodeMemoryUsage / 1024.f);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700632 pw.printf("\n\n");
Craig Mautner652fdfa2013-06-06 07:51:57 -0700633 }
634
John Reck183e1382019-10-09 13:41:18 -0700635 pw.printf("\nTotal %-15s: %d\n", "ViewRootImpl", count);
636 pw.printf("Total %-15s: %d\n", "attached Views", totals.viewCount);
637 pw.printf("Total %-15s: %.2f kB (used) / %.2f kB (capacity)\n\n", "RenderNode",
638 totals.renderNodeMemoryUsage / 1024.0f,
639 totals.renderNodeMemoryAllocated / 1024.0f);
Jeff Brown98365d72012-08-19 20:30:52 -0700640 }
641 } finally {
642 pw.flush();
643 }
644 }
645
646 private static String getWindowName(ViewRootImpl root) {
647 return root.mWindowAttributes.getTitle() + "/" +
648 root.getClass().getName() + '@' + Integer.toHexString(root.hashCode());
649 }
650
651 public void setStoppedState(IBinder token, boolean stopped) {
Riddle Hsu5828b412019-04-29 19:22:38 +0800652 ArrayList<ViewRootImpl> nonCurrentThreadRoots = null;
Jeff Brown98365d72012-08-19 20:30:52 -0700653 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700654 int count = mViews.size();
Tetsutoki Shiozawa978e7f82017-11-01 11:38:34 +0900655 for (int i = count - 1; i >= 0; i--) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700656 if (token == null || mParams.get(i).token == token) {
657 ViewRootImpl root = mRoots.get(i);
Tetsutoki Shiozawa978e7f82017-11-01 11:38:34 +0900658 // Client might remove the view by "stopped" event.
Riddle Hsu5828b412019-04-29 19:22:38 +0800659 if (root.mThread == Thread.currentThread()) {
660 root.setWindowStopped(stopped);
661 } else {
662 if (nonCurrentThreadRoots == null) {
663 nonCurrentThreadRoots = new ArrayList<>();
664 }
665 nonCurrentThreadRoots.add(root);
666 }
Robert Carr3f7bd972018-04-02 15:09:10 -0700667 // Recursively forward stopped state to View's attached
668 // to this Window rather than the root application token,
669 // e.g. PopupWindow's.
670 setStoppedState(root.mAttachInfo.mWindowToken, stopped);
Jeff Brown98365d72012-08-19 20:30:52 -0700671 }
672 }
673 }
Riddle Hsu5828b412019-04-29 19:22:38 +0800674
675 // Update the stopped state synchronously to ensure the surface won't be used after server
676 // side has destroyed it. This operation should be outside the lock to avoid any potential
677 // paths from setWindowStopped to WindowManagerGlobal which may cause deadlocks.
678 if (nonCurrentThreadRoots != null) {
679 for (int i = nonCurrentThreadRoots.size() - 1; i >= 0; i--) {
680 ViewRootImpl root = nonCurrentThreadRoots.get(i);
681 root.mHandler.runWithScissors(() -> root.setWindowStopped(stopped), 0);
682 }
683 }
Jeff Brown98365d72012-08-19 20:30:52 -0700684 }
685
686 public void reportNewConfiguration(Configuration config) {
687 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700688 int count = mViews.size();
689 config = new Configuration(config);
690 for (int i=0; i < count; i++) {
691 ViewRootImpl root = mRoots.get(i);
692 root.requestUpdateConfiguration(config);
Jeff Brown98365d72012-08-19 20:30:52 -0700693 }
694 }
695 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700696
697 /** @hide */
698 public void changeCanvasOpacity(IBinder token, boolean opaque) {
699 if (token == null) {
700 return;
701 }
702 synchronized (mLock) {
703 for (int i = mParams.size() - 1; i >= 0; --i) {
704 if (mParams.get(i).token == token) {
705 mRoots.get(i).changeCanvasOpacity(opaque);
706 return;
707 }
708 }
709 }
710 }
Jeff Brown98365d72012-08-19 20:30:52 -0700711}
712
713final class WindowLeaked extends AndroidRuntimeException {
Mathew Inwooda570dee2018-08-17 14:56:00 +0100714 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700715 public WindowLeaked(String msg) {
716 super(msg);
717 }
718}