blob: c22b8921390cbd42c689fc0fade56e1a17821c59 [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 Hau779af292020-02-07 10:53:32 -080059 private final boolean mUseBLASTAdapter;
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() {
Valerie Hau779af292020-02-07 10:53:32 -0800162 try {
163 mUseBLASTAdapter = getWindowManagerService().useBLAST();
164 } catch (RemoteException e) {
165 throw e.rethrowFromSystemServer();
166 }
Jeff Brown98365d72012-08-19 20:30:52 -0700167 }
168
Mathew Inwooda570dee2018-08-17 14:56:00 +0100169 @UnsupportedAppUsage
Chet Haase0d1c27a2014-11-03 18:35:16 +0000170 public static void initialize() {
171 getWindowManagerService();
172 }
173
Mathew Inwooda570dee2018-08-17 14:56:00 +0100174 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700175 public static WindowManagerGlobal getInstance() {
176 synchronized (WindowManagerGlobal.class) {
177 if (sDefaultWindowManager == null) {
178 sDefaultWindowManager = new WindowManagerGlobal();
179 }
180 return sDefaultWindowManager;
181 }
182 }
183
Mathew Inwooda570dee2018-08-17 14:56:00 +0100184 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700185 public static IWindowManager getWindowManagerService() {
186 synchronized (WindowManagerGlobal.class) {
187 if (sWindowManagerService == null) {
188 sWindowManagerService = IWindowManager.Stub.asInterface(
189 ServiceManager.getService("window"));
Chet Haase0d1c27a2014-11-03 18:35:16 +0000190 try {
Phil Weaver7eec3792017-02-14 16:51:55 -0800191 if (sWindowManagerService != null) {
192 ValueAnimator.setDurationScale(
193 sWindowManagerService.getCurrentAnimatorScale());
194 }
Chet Haase0d1c27a2014-11-03 18:35:16 +0000195 } catch (RemoteException e) {
Jeff Sharkeyd136e512016-03-09 22:30:56 -0700196 throw e.rethrowFromSystemServer();
Chet Haase0d1c27a2014-11-03 18:35:16 +0000197 }
Jeff Brown98365d72012-08-19 20:30:52 -0700198 }
199 return sWindowManagerService;
200 }
201 }
202
Mathew Inwooda570dee2018-08-17 14:56:00 +0100203 @UnsupportedAppUsage
Jeff Brownf9e989d2013-04-04 23:04:03 -0700204 public static IWindowSession getWindowSession() {
Jeff Brown98365d72012-08-19 20:30:52 -0700205 synchronized (WindowManagerGlobal.class) {
206 if (sWindowSession == null) {
207 try {
Yohei Yukawa6c075722018-09-21 14:52:12 -0700208 // Emulate the legacy behavior. The global instance of InputMethodManager
209 // was instantiated here.
210 // TODO(b/116157766): Remove this hack after cleaning up @UnsupportedAppUsage
211 InputMethodManager.ensureDefaultInstanceForDefaultDisplayIfNecessary();
Jeff Brown98365d72012-08-19 20:30:52 -0700212 IWindowManager windowManager = getWindowManagerService();
213 sWindowSession = windowManager.openSession(
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700214 new IWindowSessionCallback.Stub() {
215 @Override
216 public void onAnimatorScaleChanged(float scale) {
217 ValueAnimator.setDurationScale(scale);
218 }
Yohei Yukawaa71bb252018-09-19 19:21:24 -0700219 });
Jeff Brown98365d72012-08-19 20:30:52 -0700220 } catch (RemoteException e) {
Jeff Sharkeyd136e512016-03-09 22:30:56 -0700221 throw e.rethrowFromSystemServer();
Jeff Brown98365d72012-08-19 20:30:52 -0700222 }
223 }
224 return sWindowSession;
225 }
226 }
227
Mathew Inwood8c854f82018-09-14 12:35:36 +0100228 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Brown98365d72012-08-19 20:30:52 -0700229 public static IWindowSession peekWindowSession() {
230 synchronized (WindowManagerGlobal.class) {
231 return sWindowSession;
232 }
233 }
234
Valerie Hau779af292020-02-07 10:53:32 -0800235 /**
236 * Whether or not to use BLAST for ViewRootImpl
237 */
238 public boolean useBLAST() {
239 return mUseBLASTAdapter;
240 }
241
Mathew Inwooda570dee2018-08-17 14:56:00 +0100242 @UnsupportedAppUsage
Siva Velusamy945bfb62013-01-06 16:03:12 -0800243 public String[] getViewRootNames() {
244 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700245 final int numRoots = mRoots.size();
246 String[] mViewRoots = new String[numRoots];
247 for (int i = 0; i < numRoots; ++i) {
248 mViewRoots[i] = getWindowName(mRoots.get(i));
Siva Velusamy945bfb62013-01-06 16:03:12 -0800249 }
250 return mViewRoots;
251 }
252 }
253
Mathew Inwooda570dee2018-08-17 14:56:00 +0100254 @UnsupportedAppUsage
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800255 public ArrayList<ViewRootImpl> getRootViews(IBinder token) {
256 ArrayList<ViewRootImpl> views = new ArrayList<>();
257 synchronized (mLock) {
258 final int numRoots = mRoots.size();
259 for (int i = 0; i < numRoots; ++i) {
260 WindowManager.LayoutParams params = mParams.get(i);
261 if (params.token == null) {
262 continue;
263 }
264 if (params.token != token) {
265 boolean isChild = false;
266 if (params.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW
267 && params.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
268 for (int j = 0 ; j < numRoots; ++j) {
269 View viewj = mViews.get(j);
270 WindowManager.LayoutParams paramsj = mParams.get(j);
271 if (params.token == viewj.getWindowToken()
272 && paramsj.token == token) {
273 isChild = true;
274 break;
275 }
276 }
277 }
278 if (!isChild) {
279 continue;
280 }
281 }
282 views.add(mRoots.get(i));
283 }
284 }
285 return views;
286 }
287
Alan Viverette1761cfa2019-02-13 17:44:08 -0500288 /**
289 * @return the list of all views attached to the global window manager
290 */
291 @NonNull
292 public ArrayList<View> getWindowViews() {
293 synchronized (mLock) {
294 return new ArrayList<>(mViews);
295 }
296 }
297
Vladislav Kaznacheev7039cbc2017-01-04 10:15:31 -0800298 public View getWindowView(IBinder windowToken) {
299 synchronized (mLock) {
300 final int numViews = mViews.size();
301 for (int i = 0; i < numViews; ++i) {
302 final View view = mViews.get(i);
303 if (view.getWindowToken() == windowToken) {
304 return view;
305 }
306 }
307 }
308 return null;
309 }
310
Mathew Inwooda570dee2018-08-17 14:56:00 +0100311 @UnsupportedAppUsage
Siva Velusamy945bfb62013-01-06 16:03:12 -0800312 public View getRootView(String name) {
313 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700314 for (int i = mRoots.size() - 1; i >= 0; --i) {
315 final ViewRootImpl root = mRoots.get(i);
Siva Velusamy945bfb62013-01-06 16:03:12 -0800316 if (name.equals(getWindowName(root))) return root.getView();
317 }
318 }
319
320 return null;
321 }
322
Jeff Brown98365d72012-08-19 20:30:52 -0700323 public void addView(View view, ViewGroup.LayoutParams params,
324 Display display, Window parentWindow) {
325 if (view == null) {
326 throw new IllegalArgumentException("view must not be null");
327 }
328 if (display == null) {
329 throw new IllegalArgumentException("display must not be null");
330 }
331 if (!(params instanceof WindowManager.LayoutParams)) {
332 throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
333 }
334
Alan Viverette9ecb73c2014-12-15 13:40:28 -0800335 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
Jeff Brown98365d72012-08-19 20:30:52 -0700336 if (parentWindow != null) {
337 parentWindow.adjustLayoutParamsForSubWindow(wparams);
Alan Viverette9b0ab652015-03-18 14:21:04 -0700338 } else {
339 // If there's no parent, then hardware acceleration for this view is
340 // set from the application's hardware acceleration setting.
Alan Viverette5e1565e2014-07-29 16:14:25 -0700341 final Context context = view.getContext();
Alan Viverette9b0ab652015-03-18 14:21:04 -0700342 if (context != null
Alan Viveretted70b9e72015-05-27 14:29:20 -0700343 && (context.getApplicationInfo().flags
344 & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
Alan Viverette5e1565e2014-07-29 16:14:25 -0700345 wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
346 }
Jeff Brown98365d72012-08-19 20:30:52 -0700347 }
348
349 ViewRootImpl root;
350 View panelParentView = null;
351
352 synchronized (mLock) {
353 // Start watching for system property changes.
354 if (mSystemPropertyUpdater == null) {
355 mSystemPropertyUpdater = new Runnable() {
356 @Override public void run() {
357 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700358 for (int i = mRoots.size() - 1; i >= 0; --i) {
359 mRoots.get(i).loadSystemProperties();
Jeff Brown98365d72012-08-19 20:30:52 -0700360 }
361 }
362 }
363 };
364 SystemProperties.addChangeCallback(mSystemPropertyUpdater);
365 }
366
367 int index = findViewLocked(view, false);
368 if (index >= 0) {
Craig Mautner8f303ad2013-06-14 11:32:22 -0700369 if (mDyingViews.contains(view)) {
370 // Don't wait for MSG_DIE to make it's way through root's queue.
371 mRoots.get(index).doDie();
372 } else {
373 throw new IllegalStateException("View " + view
374 + " has already been added to the window manager.");
375 }
376 // The previous removeView() had not completed executing. Now it has.
Jeff Brown98365d72012-08-19 20:30:52 -0700377 }
378
379 // If this is a panel window, then find the window it is being
380 // attached to for future reference.
381 if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
382 wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700383 final int count = mViews.size();
384 for (int i = 0; i < count; i++) {
385 if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
386 panelParentView = mViews.get(i);
Jeff Brown98365d72012-08-19 20:30:52 -0700387 }
388 }
389 }
390
391 root = new ViewRootImpl(view.getContext(), display);
392
393 view.setLayoutParams(wparams);
394
Craig Mautner652fdfa2013-06-06 07:51:57 -0700395 mViews.add(view);
396 mRoots.add(root);
397 mParams.add(wparams);
Jeff Brown98365d72012-08-19 20:30:52 -0700398
Gopal Krishna Shuklaf7abcda2016-07-06 08:14:59 +0530399 // do this last because it fires off messages to start doing things
400 try {
401 root.setView(view, wparams, panelParentView);
402 } catch (RuntimeException e) {
403 // BadTokenException or InvalidDisplayException, clean up.
Craig Mautner6018aee2012-10-23 14:27:49 -0700404 if (index >= 0) {
405 removeViewLocked(index, true);
406 }
Gopal Krishna Shuklaf7abcda2016-07-06 08:14:59 +0530407 throw e;
Craig Mautner6018aee2012-10-23 14:27:49 -0700408 }
Craig Mautner6018aee2012-10-23 14:27:49 -0700409 }
Jeff Brown98365d72012-08-19 20:30:52 -0700410 }
411
412 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
413 if (view == null) {
414 throw new IllegalArgumentException("view must not be null");
415 }
416 if (!(params instanceof WindowManager.LayoutParams)) {
417 throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
418 }
419
420 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;
421
422 view.setLayoutParams(wparams);
423
424 synchronized (mLock) {
425 int index = findViewLocked(view, true);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700426 ViewRootImpl root = mRoots.get(index);
427 mParams.remove(index);
428 mParams.add(index, wparams);
Jeff Brown98365d72012-08-19 20:30:52 -0700429 root.setLayoutParams(wparams, false);
430 }
431 }
432
Mathew Inwooda570dee2018-08-17 14:56:00 +0100433 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700434 public void removeView(View view, boolean immediate) {
435 if (view == null) {
436 throw new IllegalArgumentException("view must not be null");
437 }
438
439 synchronized (mLock) {
440 int index = findViewLocked(view, true);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700441 View curView = mRoots.get(index).getView();
Craig Mautner05eb7302013-06-03 17:24:21 -0700442 removeViewLocked(index, immediate);
Jeff Brown98365d72012-08-19 20:30:52 -0700443 if (curView == view) {
444 return;
445 }
446
447 throw new IllegalStateException("Calling with view " + view
448 + " but the ViewAncestor is attached to " + curView);
449 }
450 }
451
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700452 /**
453 * Remove all roots with specified token.
454 *
455 * @param token app or window token.
456 * @param who name of caller, used in logs.
457 * @param what type of caller, used in logs.
458 */
Jeff Brown98365d72012-08-19 20:30:52 -0700459 public void closeAll(IBinder token, String who, String what) {
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700460 closeAllExceptView(token, null /* view */, who, what);
461 }
462
463 /**
464 * Remove all roots with specified token, except maybe one view.
465 *
466 * @param token app or window token.
467 * @param view view that should be should be preserved along with it's root.
468 * Pass null if everything should be removed.
469 * @param who name of caller, used in logs.
470 * @param what type of caller, used in logs.
471 */
472 public void closeAllExceptView(IBinder token, View view, String who, String what) {
Jeff Brown98365d72012-08-19 20:30:52 -0700473 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700474 int count = mViews.size();
Craig Mautner05eb7302013-06-03 17:24:21 -0700475 for (int i = 0; i < count; i++) {
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700476 if ((view == null || mViews.get(i) != view)
477 && (token == null || mParams.get(i).token == token)) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700478 ViewRootImpl root = mRoots.get(i);
Jeff Brown98365d72012-08-19 20:30:52 -0700479
Jeff Brown98365d72012-08-19 20:30:52 -0700480 if (who != null) {
481 WindowLeaked leak = new WindowLeaked(
482 what + " " + who + " has leaked window "
483 + root.getView() + " that was originally added here");
484 leak.setStackTrace(root.getLocation().getStackTrace());
Craig Mautner05eb7302013-06-03 17:24:21 -0700485 Log.e(TAG, "", leak);
Jeff Brown98365d72012-08-19 20:30:52 -0700486 }
487
488 removeViewLocked(i, false);
Jeff Brown98365d72012-08-19 20:30:52 -0700489 }
490 }
491 }
492 }
493
Craig Mautner05eb7302013-06-03 17:24:21 -0700494 private void removeViewLocked(int index, boolean immediate) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700495 ViewRootImpl root = mRoots.get(index);
Jeff Brown98365d72012-08-19 20:30:52 -0700496 View view = root.getView();
497
lumark970d9d22019-08-18 17:45:24 +0800498 if (root != null) {
499 root.getImeFocusController().onWindowDismissed();
Jeff Brown98365d72012-08-19 20:30:52 -0700500 }
Craig Mautner8f303ad2013-06-14 11:32:22 -0700501 boolean deferred = root.die(immediate);
Craig Mautner92098c72013-06-10 11:27:26 -0700502 if (view != null) {
503 view.assignParent(null);
Craig Mautner8f303ad2013-06-14 11:32:22 -0700504 if (deferred) {
505 mDyingViews.add(view);
506 }
Craig Mautner92098c72013-06-10 11:27:26 -0700507 }
Craig Mautner05eb7302013-06-03 17:24:21 -0700508 }
Jeff Brown98365d72012-08-19 20:30:52 -0700509
Craig Mautner05eb7302013-06-03 17:24:21 -0700510 void doRemoveView(ViewRootImpl root) {
511 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700512 final int index = mRoots.indexOf(root);
513 if (index >= 0) {
514 mRoots.remove(index);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700515 mParams.remove(index);
Craig Mautner8f303ad2013-06-14 11:32:22 -0700516 final View view = mViews.remove(index);
517 mDyingViews.remove(view);
Jeff Brown98365d72012-08-19 20:30:52 -0700518 }
519 }
John Reck51aaf902015-12-02 15:08:07 -0800520 if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
John Reck73840ea2014-09-22 07:39:18 -0700521 doTrimForeground();
522 }
Jeff Brown98365d72012-08-19 20:30:52 -0700523 }
524
525 private int findViewLocked(View view, boolean required) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700526 final int index = mViews.indexOf(view);
527 if (required && index < 0) {
Craig Mautner05eb7302013-06-03 17:24:21 -0700528 throw new IllegalArgumentException("View=" + view + " not attached to window manager");
Craig Mautner6018aee2012-10-23 14:27:49 -0700529 }
Craig Mautner652fdfa2013-06-06 07:51:57 -0700530 return index;
Jeff Brown98365d72012-08-19 20:30:52 -0700531 }
532
John Reckf47a5942014-06-30 16:20:04 -0700533 public static boolean shouldDestroyEglContext(int trimLevel) {
534 // On low-end gfx devices we trim when memory is moderate;
535 // on high-end devices we do this when low.
536 if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
537 return true;
538 }
539 if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_MODERATE
540 && !ActivityManager.isHighEndGfx()) {
541 return true;
542 }
543 return false;
544 }
545
Sergey Vasilinets8b8d3b82019-02-08 14:27:31 +0000546 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
John Reckf47a5942014-06-30 16:20:04 -0700547 public void trimMemory(int level) {
John Reck51aaf902015-12-02 15:08:07 -0800548 if (ThreadedRenderer.isAvailable()) {
John Reckf47a5942014-06-30 16:20:04 -0700549 if (shouldDestroyEglContext(level)) {
Jeff Brown98365d72012-08-19 20:30:52 -0700550 // Destroy all hardware surfaces and resources associated to
551 // known windows
552 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700553 for (int i = mRoots.size() - 1; i >= 0; --i) {
Romain Guy46bfc482013-08-16 18:38:29 -0700554 mRoots.get(i).destroyHardwareResources();
Jeff Brown98365d72012-08-19 20:30:52 -0700555 }
556 }
557 // Force a full memory flush
John Reckf47a5942014-06-30 16:20:04 -0700558 level = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
Jeff Brown98365d72012-08-19 20:30:52 -0700559 }
560
John Reck51aaf902015-12-02 15:08:07 -0800561 ThreadedRenderer.trimMemory(level);
John Reck73840ea2014-09-22 07:39:18 -0700562
John Reck51aaf902015-12-02 15:08:07 -0800563 if (ThreadedRenderer.sTrimForeground) {
John Reck73840ea2014-09-22 07:39:18 -0700564 doTrimForeground();
565 }
566 }
567 }
568
569 public static void trimForeground() {
John Reck51aaf902015-12-02 15:08:07 -0800570 if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
John Reck73840ea2014-09-22 07:39:18 -0700571 WindowManagerGlobal wm = WindowManagerGlobal.getInstance();
572 wm.doTrimForeground();
573 }
574 }
575
576 private void doTrimForeground() {
577 boolean hasVisibleWindows = false;
578 synchronized (mLock) {
579 for (int i = mRoots.size() - 1; i >= 0; --i) {
John Reckccf2fa02014-09-25 08:33:05 -0700580 final ViewRootImpl root = mRoots.get(i);
581 if (root.mView != null && root.getHostVisibility() == View.VISIBLE
Stan Iliev45faba52016-06-28 13:33:15 -0400582 && root.mAttachInfo.mThreadedRenderer != null) {
John Reck73840ea2014-09-22 07:39:18 -0700583 hasVisibleWindows = true;
584 } else {
John Reckccf2fa02014-09-25 08:33:05 -0700585 root.destroyHardwareResources();
John Reck73840ea2014-09-22 07:39:18 -0700586 }
587 }
588 }
589 if (!hasVisibleWindows) {
John Reck51aaf902015-12-02 15:08:07 -0800590 ThreadedRenderer.trimMemory(
John Reck73840ea2014-09-22 07:39:18 -0700591 ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
Jeff Brown98365d72012-08-19 20:30:52 -0700592 }
593 }
594
John Reckba6adf62015-02-19 14:36:50 -0800595 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
Jeff Brown98365d72012-08-19 20:30:52 -0700596 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700597 PrintWriter pw = new FastPrintWriter(fout);
Jeff Brown98365d72012-08-19 20:30:52 -0700598 try {
599 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700600 final int count = mViews.size();
Jeff Brown98365d72012-08-19 20:30:52 -0700601
Craig Mautner652fdfa2013-06-06 07:51:57 -0700602 pw.println("Profile data in ms:");
Jeff Brown98365d72012-08-19 20:30:52 -0700603
Craig Mautner652fdfa2013-06-06 07:51:57 -0700604 for (int i = 0; i < count; i++) {
605 ViewRootImpl root = mRoots.get(i);
606 String name = getWindowName(root);
John Reck73840ea2014-09-22 07:39:18 -0700607 pw.printf("\n\t%s (visibility=%d)", name, root.getHostVisibility());
Jeff Brown98365d72012-08-19 20:30:52 -0700608
John Reck51aaf902015-12-02 15:08:07 -0800609 ThreadedRenderer renderer =
Stan Iliev45faba52016-06-28 13:33:15 -0400610 root.getView().mAttachInfo.mThreadedRenderer;
Craig Mautner652fdfa2013-06-06 07:51:57 -0700611 if (renderer != null) {
John Reckba6adf62015-02-19 14:36:50 -0800612 renderer.dumpGfxInfo(pw, fd, args);
Jeff Brown98365d72012-08-19 20:30:52 -0700613 }
Jeff Brown98365d72012-08-19 20:30:52 -0700614 }
Craig Mautner652fdfa2013-06-06 07:51:57 -0700615
616 pw.println("\nView hierarchy:\n");
617
John Reck183e1382019-10-09 13:41:18 -0700618 ViewRootImpl.GfxInfo totals = new ViewRootImpl.GfxInfo();
Craig Mautner652fdfa2013-06-06 07:51:57 -0700619
620 for (int i = 0; i < count; i++) {
621 ViewRootImpl root = mRoots.get(i);
John Reck183e1382019-10-09 13:41:18 -0700622 ViewRootImpl.GfxInfo info = root.getGfxInfo();
623 totals.add(info);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700624
625 String name = getWindowName(root);
John Reck183e1382019-10-09 13:41:18 -0700626 pw.printf(" %s\n %d views, %.2f kB of render nodes",
627 name, info.viewCount, info.renderNodeMemoryUsage / 1024.f);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700628 pw.printf("\n\n");
Craig Mautner652fdfa2013-06-06 07:51:57 -0700629 }
630
John Reck183e1382019-10-09 13:41:18 -0700631 pw.printf("\nTotal %-15s: %d\n", "ViewRootImpl", count);
632 pw.printf("Total %-15s: %d\n", "attached Views", totals.viewCount);
633 pw.printf("Total %-15s: %.2f kB (used) / %.2f kB (capacity)\n\n", "RenderNode",
634 totals.renderNodeMemoryUsage / 1024.0f,
635 totals.renderNodeMemoryAllocated / 1024.0f);
Jeff Brown98365d72012-08-19 20:30:52 -0700636 }
637 } finally {
638 pw.flush();
639 }
640 }
641
642 private static String getWindowName(ViewRootImpl root) {
643 return root.mWindowAttributes.getTitle() + "/" +
644 root.getClass().getName() + '@' + Integer.toHexString(root.hashCode());
645 }
646
647 public void setStoppedState(IBinder token, boolean stopped) {
Riddle Hsu5828b412019-04-29 19:22:38 +0800648 ArrayList<ViewRootImpl> nonCurrentThreadRoots = null;
Jeff Brown98365d72012-08-19 20:30:52 -0700649 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700650 int count = mViews.size();
Tetsutoki Shiozawa978e7f82017-11-01 11:38:34 +0900651 for (int i = count - 1; i >= 0; i--) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700652 if (token == null || mParams.get(i).token == token) {
653 ViewRootImpl root = mRoots.get(i);
Tetsutoki Shiozawa978e7f82017-11-01 11:38:34 +0900654 // Client might remove the view by "stopped" event.
Riddle Hsu5828b412019-04-29 19:22:38 +0800655 if (root.mThread == Thread.currentThread()) {
656 root.setWindowStopped(stopped);
657 } else {
658 if (nonCurrentThreadRoots == null) {
659 nonCurrentThreadRoots = new ArrayList<>();
660 }
661 nonCurrentThreadRoots.add(root);
662 }
Robert Carr3f7bd972018-04-02 15:09:10 -0700663 // Recursively forward stopped state to View's attached
664 // to this Window rather than the root application token,
665 // e.g. PopupWindow's.
666 setStoppedState(root.mAttachInfo.mWindowToken, stopped);
Jeff Brown98365d72012-08-19 20:30:52 -0700667 }
668 }
669 }
Riddle Hsu5828b412019-04-29 19:22:38 +0800670
671 // Update the stopped state synchronously to ensure the surface won't be used after server
672 // side has destroyed it. This operation should be outside the lock to avoid any potential
673 // paths from setWindowStopped to WindowManagerGlobal which may cause deadlocks.
674 if (nonCurrentThreadRoots != null) {
675 for (int i = nonCurrentThreadRoots.size() - 1; i >= 0; i--) {
676 ViewRootImpl root = nonCurrentThreadRoots.get(i);
677 root.mHandler.runWithScissors(() -> root.setWindowStopped(stopped), 0);
678 }
679 }
Jeff Brown98365d72012-08-19 20:30:52 -0700680 }
681
682 public void reportNewConfiguration(Configuration config) {
683 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700684 int count = mViews.size();
685 config = new Configuration(config);
686 for (int i=0; i < count; i++) {
687 ViewRootImpl root = mRoots.get(i);
688 root.requestUpdateConfiguration(config);
Jeff Brown98365d72012-08-19 20:30:52 -0700689 }
690 }
691 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700692
693 /** @hide */
694 public void changeCanvasOpacity(IBinder token, boolean opaque) {
695 if (token == null) {
696 return;
697 }
698 synchronized (mLock) {
699 for (int i = mParams.size() - 1; i >= 0; --i) {
700 if (mParams.get(i).token == token) {
701 mRoots.get(i).changeCanvasOpacity(opaque);
702 return;
703 }
704 }
705 }
706 }
Jeff Brown98365d72012-08-19 20:30:52 -0700707}
708
709final class WindowLeaked extends AndroidRuntimeException {
Mathew Inwooda570dee2018-08-17 14:56:00 +0100710 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700711 public WindowLeaked(String msg) {
712 super(msg);
713 }
714}