blob: 453c5e32a178b959cc3c2c6ca52f45462ecf1f01 [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;
Mathew Inwooda570dee2018-08-17 14:56:00 +010021import android.annotation.UnsupportedAppUsage;
Jeff Brown98365d72012-08-19 20:30:52 -070022import android.app.ActivityManager;
23import 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
59 /**
60 * The user is navigating with keys (not the touch screen), so
61 * navigational focus should be shown.
62 */
63 public static final int RELAYOUT_RES_IN_TOUCH_MODE = 0x1;
64
65 /**
66 * This is the first time the window is being drawn,
67 * so the client must call drawingFinished() when done
68 */
69 public static final int RELAYOUT_RES_FIRST_TIME = 0x2;
70
71 /**
72 * The window manager has changed the surface from the last call.
73 */
74 public static final int RELAYOUT_RES_SURFACE_CHANGED = 0x4;
75
76 /**
Jorim Jaggi4846ee32016-01-07 17:39:12 +010077 * The window is being resized by dragging on the docked divider. The client should render
78 * at (0, 0) and extend its background to the background frame passed into
79 * {@link IWindow#resized}.
80 */
81 public static final int RELAYOUT_RES_DRAG_RESIZING_DOCKED = 0x8;
82
83 /**
Chong Zhang0275e392015-09-17 10:41:44 -070084 * The window is being resized by dragging one of the window corners,
Filip Gruszczynski63a35e22015-11-05 15:38:59 -080085 * in this case the surface would be fullscreen-sized. The client should
Chong Zhang0275e392015-09-17 10:41:44 -070086 * render to the actual frame location (instead of (0,curScrollY)).
87 */
Jorim Jaggi4846ee32016-01-07 17:39:12 +010088 public static final int RELAYOUT_RES_DRAG_RESIZING_FREEFORM = 0x10;
Chong Zhang0275e392015-09-17 10:41:44 -070089
90 /**
Chong Zhangf4abc2b2015-11-12 23:40:58 -080091 * The window manager has changed the size of the surface from the last call.
92 */
Jorim Jaggi4846ee32016-01-07 17:39:12 +010093 public static final int RELAYOUT_RES_SURFACE_RESIZED = 0x20;
Chong Zhangf4abc2b2015-11-12 23:40:58 -080094
95 /**
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -080096 * In multi-window we force show the navigation bar. Because we don't want that the surface size
97 * changes in this mode, we instead have a flag whether the navigation bar size should always be
98 * consumed, so the app is treated like there is no virtual navigation bar at all.
99 */
100 public static final int RELAYOUT_RES_CONSUME_ALWAYS_NAV_BAR = 0x40;
101
102 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700103 * Flag for relayout: the client will be later giving
104 * internal insets; as a result, the window will not impact other window
105 * layouts until the insets are given.
106 */
107 public static final int RELAYOUT_INSETS_PENDING = 0x1;
108
109 /**
110 * Flag for relayout: the client may be currently using the current surface,
111 * so if it is to be destroyed as a part of the relayout the destroy must
112 * be deferred until later. The client will call performDeferredDestroy()
113 * when it is okay.
114 */
115 public static final int RELAYOUT_DEFER_SURFACE_DESTROY = 0x2;
116
117 public static final int ADD_FLAG_APP_VISIBLE = 0x2;
118 public static final int ADD_FLAG_IN_TOUCH_MODE = RELAYOUT_RES_IN_TOUCH_MODE;
119
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -0800120 /**
121 * Like {@link #RELAYOUT_RES_CONSUME_ALWAYS_NAV_BAR}, but as a "hint" when adding the window.
122 */
123 public static final int ADD_FLAG_ALWAYS_CONSUME_NAV_BAR = 0x4;
124
Jeff Brown98365d72012-08-19 20:30:52 -0700125 public static final int ADD_OKAY = 0;
126 public static final int ADD_BAD_APP_TOKEN = -1;
127 public static final int ADD_BAD_SUBWINDOW_TOKEN = -2;
128 public static final int ADD_NOT_APP_TOKEN = -3;
129 public static final int ADD_APP_EXITING = -4;
130 public static final int ADD_DUPLICATE_ADD = -5;
131 public static final int ADD_STARTING_NOT_NEEDED = -6;
132 public static final int ADD_MULTIPLE_SINGLETON = -7;
133 public static final int ADD_PERMISSION_DENIED = -8;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700134 public static final int ADD_INVALID_DISPLAY = -9;
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800135 public static final int ADD_INVALID_TYPE = -10;
Jeff Brown98365d72012-08-19 20:30:52 -0700136
Mathew Inwooda570dee2018-08-17 14:56:00 +0100137 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700138 private static WindowManagerGlobal sDefaultWindowManager;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100139 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700140 private static IWindowManager sWindowManagerService;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100141 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700142 private static IWindowSession sWindowSession;
143
Mathew Inwooda570dee2018-08-17 14:56:00 +0100144 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700145 private final Object mLock = new Object();
146
Mathew Inwooda570dee2018-08-17 14:56:00 +0100147 @UnsupportedAppUsage
Craig Mautner652fdfa2013-06-06 07:51:57 -0700148 private final ArrayList<View> mViews = new ArrayList<View>();
Mathew Inwooda570dee2018-08-17 14:56:00 +0100149 @UnsupportedAppUsage
Craig Mautner652fdfa2013-06-06 07:51:57 -0700150 private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
Mathew Inwooda570dee2018-08-17 14:56:00 +0100151 @UnsupportedAppUsage
Craig Mautner652fdfa2013-06-06 07:51:57 -0700152 private final ArrayList<WindowManager.LayoutParams> mParams =
153 new ArrayList<WindowManager.LayoutParams>();
Craig Mautner8f303ad2013-06-14 11:32:22 -0700154 private final ArraySet<View> mDyingViews = new ArraySet<View>();
Jeff Brown98365d72012-08-19 20:30:52 -0700155
156 private Runnable mSystemPropertyUpdater;
157
158 private WindowManagerGlobal() {
159 }
160
Mathew Inwooda570dee2018-08-17 14:56:00 +0100161 @UnsupportedAppUsage
Chet Haase0d1c27a2014-11-03 18:35:16 +0000162 public static void initialize() {
163 getWindowManagerService();
164 }
165
Mathew Inwooda570dee2018-08-17 14:56:00 +0100166 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700167 public static WindowManagerGlobal getInstance() {
168 synchronized (WindowManagerGlobal.class) {
169 if (sDefaultWindowManager == null) {
170 sDefaultWindowManager = new WindowManagerGlobal();
171 }
172 return sDefaultWindowManager;
173 }
174 }
175
Mathew Inwooda570dee2018-08-17 14:56:00 +0100176 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700177 public static IWindowManager getWindowManagerService() {
178 synchronized (WindowManagerGlobal.class) {
179 if (sWindowManagerService == null) {
180 sWindowManagerService = IWindowManager.Stub.asInterface(
181 ServiceManager.getService("window"));
Chet Haase0d1c27a2014-11-03 18:35:16 +0000182 try {
Phil Weaver7eec3792017-02-14 16:51:55 -0800183 if (sWindowManagerService != null) {
184 ValueAnimator.setDurationScale(
185 sWindowManagerService.getCurrentAnimatorScale());
186 }
Chet Haase0d1c27a2014-11-03 18:35:16 +0000187 } catch (RemoteException e) {
Jeff Sharkeyd136e512016-03-09 22:30:56 -0700188 throw e.rethrowFromSystemServer();
Chet Haase0d1c27a2014-11-03 18:35:16 +0000189 }
Jeff Brown98365d72012-08-19 20:30:52 -0700190 }
191 return sWindowManagerService;
192 }
193 }
194
Mathew Inwooda570dee2018-08-17 14:56:00 +0100195 @UnsupportedAppUsage
Jeff Brownf9e989d2013-04-04 23:04:03 -0700196 public static IWindowSession getWindowSession() {
Jeff Brown98365d72012-08-19 20:30:52 -0700197 synchronized (WindowManagerGlobal.class) {
198 if (sWindowSession == null) {
199 try {
Yohei Yukawa6c075722018-09-21 14:52:12 -0700200 // Emulate the legacy behavior. The global instance of InputMethodManager
201 // was instantiated here.
202 // TODO(b/116157766): Remove this hack after cleaning up @UnsupportedAppUsage
203 InputMethodManager.ensureDefaultInstanceForDefaultDisplayIfNecessary();
Jeff Brown98365d72012-08-19 20:30:52 -0700204 IWindowManager windowManager = getWindowManagerService();
205 sWindowSession = windowManager.openSession(
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700206 new IWindowSessionCallback.Stub() {
207 @Override
208 public void onAnimatorScaleChanged(float scale) {
209 ValueAnimator.setDurationScale(scale);
210 }
Yohei Yukawaa71bb252018-09-19 19:21:24 -0700211 });
Jeff Brown98365d72012-08-19 20:30:52 -0700212 } catch (RemoteException e) {
Jeff Sharkeyd136e512016-03-09 22:30:56 -0700213 throw e.rethrowFromSystemServer();
Jeff Brown98365d72012-08-19 20:30:52 -0700214 }
215 }
216 return sWindowSession;
217 }
218 }
219
Mathew Inwood8c854f82018-09-14 12:35:36 +0100220 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Brown98365d72012-08-19 20:30:52 -0700221 public static IWindowSession peekWindowSession() {
222 synchronized (WindowManagerGlobal.class) {
223 return sWindowSession;
224 }
225 }
226
Mathew Inwooda570dee2018-08-17 14:56:00 +0100227 @UnsupportedAppUsage
Siva Velusamy945bfb62013-01-06 16:03:12 -0800228 public String[] getViewRootNames() {
229 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700230 final int numRoots = mRoots.size();
231 String[] mViewRoots = new String[numRoots];
232 for (int i = 0; i < numRoots; ++i) {
233 mViewRoots[i] = getWindowName(mRoots.get(i));
Siva Velusamy945bfb62013-01-06 16:03:12 -0800234 }
235 return mViewRoots;
236 }
237 }
238
Mathew Inwooda570dee2018-08-17 14:56:00 +0100239 @UnsupportedAppUsage
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800240 public ArrayList<ViewRootImpl> getRootViews(IBinder token) {
241 ArrayList<ViewRootImpl> views = new ArrayList<>();
242 synchronized (mLock) {
243 final int numRoots = mRoots.size();
244 for (int i = 0; i < numRoots; ++i) {
245 WindowManager.LayoutParams params = mParams.get(i);
246 if (params.token == null) {
247 continue;
248 }
249 if (params.token != token) {
250 boolean isChild = false;
251 if (params.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW
252 && params.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
253 for (int j = 0 ; j < numRoots; ++j) {
254 View viewj = mViews.get(j);
255 WindowManager.LayoutParams paramsj = mParams.get(j);
256 if (params.token == viewj.getWindowToken()
257 && paramsj.token == token) {
258 isChild = true;
259 break;
260 }
261 }
262 }
263 if (!isChild) {
264 continue;
265 }
266 }
267 views.add(mRoots.get(i));
268 }
269 }
270 return views;
271 }
272
Alan Viverette1761cfa2019-02-13 17:44:08 -0500273 /**
274 * @return the list of all views attached to the global window manager
275 */
276 @NonNull
277 public ArrayList<View> getWindowViews() {
278 synchronized (mLock) {
279 return new ArrayList<>(mViews);
280 }
281 }
282
Vladislav Kaznacheev7039cbc2017-01-04 10:15:31 -0800283 public View getWindowView(IBinder windowToken) {
284 synchronized (mLock) {
285 final int numViews = mViews.size();
286 for (int i = 0; i < numViews; ++i) {
287 final View view = mViews.get(i);
288 if (view.getWindowToken() == windowToken) {
289 return view;
290 }
291 }
292 }
293 return null;
294 }
295
Mathew Inwooda570dee2018-08-17 14:56:00 +0100296 @UnsupportedAppUsage
Siva Velusamy945bfb62013-01-06 16:03:12 -0800297 public View getRootView(String name) {
298 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700299 for (int i = mRoots.size() - 1; i >= 0; --i) {
300 final ViewRootImpl root = mRoots.get(i);
Siva Velusamy945bfb62013-01-06 16:03:12 -0800301 if (name.equals(getWindowName(root))) return root.getView();
302 }
303 }
304
305 return null;
306 }
307
Jeff Brown98365d72012-08-19 20:30:52 -0700308 public void addView(View view, ViewGroup.LayoutParams params,
309 Display display, Window parentWindow) {
310 if (view == null) {
311 throw new IllegalArgumentException("view must not be null");
312 }
313 if (display == null) {
314 throw new IllegalArgumentException("display must not be null");
315 }
316 if (!(params instanceof WindowManager.LayoutParams)) {
317 throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
318 }
319
Alan Viverette9ecb73c2014-12-15 13:40:28 -0800320 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
Jeff Brown98365d72012-08-19 20:30:52 -0700321 if (parentWindow != null) {
322 parentWindow.adjustLayoutParamsForSubWindow(wparams);
Alan Viverette9b0ab652015-03-18 14:21:04 -0700323 } else {
324 // If there's no parent, then hardware acceleration for this view is
325 // set from the application's hardware acceleration setting.
Alan Viverette5e1565e2014-07-29 16:14:25 -0700326 final Context context = view.getContext();
Alan Viverette9b0ab652015-03-18 14:21:04 -0700327 if (context != null
Alan Viveretted70b9e72015-05-27 14:29:20 -0700328 && (context.getApplicationInfo().flags
329 & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
Alan Viverette5e1565e2014-07-29 16:14:25 -0700330 wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
331 }
Jeff Brown98365d72012-08-19 20:30:52 -0700332 }
333
334 ViewRootImpl root;
335 View panelParentView = null;
336
337 synchronized (mLock) {
338 // Start watching for system property changes.
339 if (mSystemPropertyUpdater == null) {
340 mSystemPropertyUpdater = new Runnable() {
341 @Override public void run() {
342 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700343 for (int i = mRoots.size() - 1; i >= 0; --i) {
344 mRoots.get(i).loadSystemProperties();
Jeff Brown98365d72012-08-19 20:30:52 -0700345 }
346 }
347 }
348 };
349 SystemProperties.addChangeCallback(mSystemPropertyUpdater);
350 }
351
352 int index = findViewLocked(view, false);
353 if (index >= 0) {
Craig Mautner8f303ad2013-06-14 11:32:22 -0700354 if (mDyingViews.contains(view)) {
355 // Don't wait for MSG_DIE to make it's way through root's queue.
356 mRoots.get(index).doDie();
357 } else {
358 throw new IllegalStateException("View " + view
359 + " has already been added to the window manager.");
360 }
361 // The previous removeView() had not completed executing. Now it has.
Jeff Brown98365d72012-08-19 20:30:52 -0700362 }
363
364 // If this is a panel window, then find the window it is being
365 // attached to for future reference.
366 if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
367 wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700368 final int count = mViews.size();
369 for (int i = 0; i < count; i++) {
370 if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
371 panelParentView = mViews.get(i);
Jeff Brown98365d72012-08-19 20:30:52 -0700372 }
373 }
374 }
375
376 root = new ViewRootImpl(view.getContext(), display);
377
378 view.setLayoutParams(wparams);
379
Craig Mautner652fdfa2013-06-06 07:51:57 -0700380 mViews.add(view);
381 mRoots.add(root);
382 mParams.add(wparams);
Jeff Brown98365d72012-08-19 20:30:52 -0700383
Gopal Krishna Shuklaf7abcda2016-07-06 08:14:59 +0530384 // do this last because it fires off messages to start doing things
385 try {
386 root.setView(view, wparams, panelParentView);
387 } catch (RuntimeException e) {
388 // BadTokenException or InvalidDisplayException, clean up.
Craig Mautner6018aee2012-10-23 14:27:49 -0700389 if (index >= 0) {
390 removeViewLocked(index, true);
391 }
Gopal Krishna Shuklaf7abcda2016-07-06 08:14:59 +0530392 throw e;
Craig Mautner6018aee2012-10-23 14:27:49 -0700393 }
Craig Mautner6018aee2012-10-23 14:27:49 -0700394 }
Jeff Brown98365d72012-08-19 20:30:52 -0700395 }
396
397 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
398 if (view == null) {
399 throw new IllegalArgumentException("view must not be null");
400 }
401 if (!(params instanceof WindowManager.LayoutParams)) {
402 throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
403 }
404
405 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;
406
407 view.setLayoutParams(wparams);
408
409 synchronized (mLock) {
410 int index = findViewLocked(view, true);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700411 ViewRootImpl root = mRoots.get(index);
412 mParams.remove(index);
413 mParams.add(index, wparams);
Jeff Brown98365d72012-08-19 20:30:52 -0700414 root.setLayoutParams(wparams, false);
415 }
416 }
417
Mathew Inwooda570dee2018-08-17 14:56:00 +0100418 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700419 public void removeView(View view, boolean immediate) {
420 if (view == null) {
421 throw new IllegalArgumentException("view must not be null");
422 }
423
424 synchronized (mLock) {
425 int index = findViewLocked(view, true);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700426 View curView = mRoots.get(index).getView();
Craig Mautner05eb7302013-06-03 17:24:21 -0700427 removeViewLocked(index, immediate);
Jeff Brown98365d72012-08-19 20:30:52 -0700428 if (curView == view) {
429 return;
430 }
431
432 throw new IllegalStateException("Calling with view " + view
433 + " but the ViewAncestor is attached to " + curView);
434 }
435 }
436
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700437 /**
438 * Remove all roots with specified token.
439 *
440 * @param token app or window token.
441 * @param who name of caller, used in logs.
442 * @param what type of caller, used in logs.
443 */
Jeff Brown98365d72012-08-19 20:30:52 -0700444 public void closeAll(IBinder token, String who, String what) {
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700445 closeAllExceptView(token, null /* view */, who, what);
446 }
447
448 /**
449 * Remove all roots with specified token, except maybe one view.
450 *
451 * @param token app or window token.
452 * @param view view that should be should be preserved along with it's root.
453 * Pass null if everything should be removed.
454 * @param who name of caller, used in logs.
455 * @param what type of caller, used in logs.
456 */
457 public void closeAllExceptView(IBinder token, View view, String who, String what) {
Jeff Brown98365d72012-08-19 20:30:52 -0700458 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700459 int count = mViews.size();
Craig Mautner05eb7302013-06-03 17:24:21 -0700460 for (int i = 0; i < count; i++) {
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700461 if ((view == null || mViews.get(i) != view)
462 && (token == null || mParams.get(i).token == token)) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700463 ViewRootImpl root = mRoots.get(i);
Jeff Brown98365d72012-08-19 20:30:52 -0700464
Jeff Brown98365d72012-08-19 20:30:52 -0700465 if (who != null) {
466 WindowLeaked leak = new WindowLeaked(
467 what + " " + who + " has leaked window "
468 + root.getView() + " that was originally added here");
469 leak.setStackTrace(root.getLocation().getStackTrace());
Craig Mautner05eb7302013-06-03 17:24:21 -0700470 Log.e(TAG, "", leak);
Jeff Brown98365d72012-08-19 20:30:52 -0700471 }
472
473 removeViewLocked(i, false);
Jeff Brown98365d72012-08-19 20:30:52 -0700474 }
475 }
476 }
477 }
478
Craig Mautner05eb7302013-06-03 17:24:21 -0700479 private void removeViewLocked(int index, boolean immediate) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700480 ViewRootImpl root = mRoots.get(index);
Jeff Brown98365d72012-08-19 20:30:52 -0700481 View view = root.getView();
482
483 if (view != null) {
Yohei Yukawa484d4af2018-09-17 16:47:08 -0700484 InputMethodManager imm = view.getContext().getSystemService(InputMethodManager.class);
Jeff Brown98365d72012-08-19 20:30:52 -0700485 if (imm != null) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700486 imm.windowDismissed(mViews.get(index).getWindowToken());
Jeff Brown98365d72012-08-19 20:30:52 -0700487 }
488 }
Craig Mautner8f303ad2013-06-14 11:32:22 -0700489 boolean deferred = root.die(immediate);
Craig Mautner92098c72013-06-10 11:27:26 -0700490 if (view != null) {
491 view.assignParent(null);
Craig Mautner8f303ad2013-06-14 11:32:22 -0700492 if (deferred) {
493 mDyingViews.add(view);
494 }
Craig Mautner92098c72013-06-10 11:27:26 -0700495 }
Craig Mautner05eb7302013-06-03 17:24:21 -0700496 }
Jeff Brown98365d72012-08-19 20:30:52 -0700497
Craig Mautner05eb7302013-06-03 17:24:21 -0700498 void doRemoveView(ViewRootImpl root) {
499 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700500 final int index = mRoots.indexOf(root);
501 if (index >= 0) {
502 mRoots.remove(index);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700503 mParams.remove(index);
Craig Mautner8f303ad2013-06-14 11:32:22 -0700504 final View view = mViews.remove(index);
505 mDyingViews.remove(view);
Jeff Brown98365d72012-08-19 20:30:52 -0700506 }
507 }
John Reck51aaf902015-12-02 15:08:07 -0800508 if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
John Reck73840ea2014-09-22 07:39:18 -0700509 doTrimForeground();
510 }
Jeff Brown98365d72012-08-19 20:30:52 -0700511 }
512
513 private int findViewLocked(View view, boolean required) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700514 final int index = mViews.indexOf(view);
515 if (required && index < 0) {
Craig Mautner05eb7302013-06-03 17:24:21 -0700516 throw new IllegalArgumentException("View=" + view + " not attached to window manager");
Craig Mautner6018aee2012-10-23 14:27:49 -0700517 }
Craig Mautner652fdfa2013-06-06 07:51:57 -0700518 return index;
Jeff Brown98365d72012-08-19 20:30:52 -0700519 }
520
John Reckf47a5942014-06-30 16:20:04 -0700521 public static boolean shouldDestroyEglContext(int trimLevel) {
522 // On low-end gfx devices we trim when memory is moderate;
523 // on high-end devices we do this when low.
524 if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
525 return true;
526 }
527 if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_MODERATE
528 && !ActivityManager.isHighEndGfx()) {
529 return true;
530 }
531 return false;
532 }
533
Sergey Vasilinets8b8d3b82019-02-08 14:27:31 +0000534 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
John Reckf47a5942014-06-30 16:20:04 -0700535 public void trimMemory(int level) {
John Reck51aaf902015-12-02 15:08:07 -0800536 if (ThreadedRenderer.isAvailable()) {
John Reckf47a5942014-06-30 16:20:04 -0700537 if (shouldDestroyEglContext(level)) {
Jeff Brown98365d72012-08-19 20:30:52 -0700538 // Destroy all hardware surfaces and resources associated to
539 // known windows
540 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700541 for (int i = mRoots.size() - 1; i >= 0; --i) {
Romain Guy46bfc482013-08-16 18:38:29 -0700542 mRoots.get(i).destroyHardwareResources();
Jeff Brown98365d72012-08-19 20:30:52 -0700543 }
544 }
545 // Force a full memory flush
John Reckf47a5942014-06-30 16:20:04 -0700546 level = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
Jeff Brown98365d72012-08-19 20:30:52 -0700547 }
548
John Reck51aaf902015-12-02 15:08:07 -0800549 ThreadedRenderer.trimMemory(level);
John Reck73840ea2014-09-22 07:39:18 -0700550
John Reck51aaf902015-12-02 15:08:07 -0800551 if (ThreadedRenderer.sTrimForeground) {
John Reck73840ea2014-09-22 07:39:18 -0700552 doTrimForeground();
553 }
554 }
555 }
556
557 public static void trimForeground() {
John Reck51aaf902015-12-02 15:08:07 -0800558 if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
John Reck73840ea2014-09-22 07:39:18 -0700559 WindowManagerGlobal wm = WindowManagerGlobal.getInstance();
560 wm.doTrimForeground();
561 }
562 }
563
564 private void doTrimForeground() {
565 boolean hasVisibleWindows = false;
566 synchronized (mLock) {
567 for (int i = mRoots.size() - 1; i >= 0; --i) {
John Reckccf2fa02014-09-25 08:33:05 -0700568 final ViewRootImpl root = mRoots.get(i);
569 if (root.mView != null && root.getHostVisibility() == View.VISIBLE
Stan Iliev45faba52016-06-28 13:33:15 -0400570 && root.mAttachInfo.mThreadedRenderer != null) {
John Reck73840ea2014-09-22 07:39:18 -0700571 hasVisibleWindows = true;
572 } else {
John Reckccf2fa02014-09-25 08:33:05 -0700573 root.destroyHardwareResources();
John Reck73840ea2014-09-22 07:39:18 -0700574 }
575 }
576 }
577 if (!hasVisibleWindows) {
John Reck51aaf902015-12-02 15:08:07 -0800578 ThreadedRenderer.trimMemory(
John Reck73840ea2014-09-22 07:39:18 -0700579 ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
Jeff Brown98365d72012-08-19 20:30:52 -0700580 }
581 }
582
John Reckba6adf62015-02-19 14:36:50 -0800583 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
Jeff Brown98365d72012-08-19 20:30:52 -0700584 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700585 PrintWriter pw = new FastPrintWriter(fout);
Jeff Brown98365d72012-08-19 20:30:52 -0700586 try {
587 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700588 final int count = mViews.size();
Jeff Brown98365d72012-08-19 20:30:52 -0700589
Craig Mautner652fdfa2013-06-06 07:51:57 -0700590 pw.println("Profile data in ms:");
Jeff Brown98365d72012-08-19 20:30:52 -0700591
Craig Mautner652fdfa2013-06-06 07:51:57 -0700592 for (int i = 0; i < count; i++) {
593 ViewRootImpl root = mRoots.get(i);
594 String name = getWindowName(root);
John Reck73840ea2014-09-22 07:39:18 -0700595 pw.printf("\n\t%s (visibility=%d)", name, root.getHostVisibility());
Jeff Brown98365d72012-08-19 20:30:52 -0700596
John Reck51aaf902015-12-02 15:08:07 -0800597 ThreadedRenderer renderer =
Stan Iliev45faba52016-06-28 13:33:15 -0400598 root.getView().mAttachInfo.mThreadedRenderer;
Craig Mautner652fdfa2013-06-06 07:51:57 -0700599 if (renderer != null) {
John Reckba6adf62015-02-19 14:36:50 -0800600 renderer.dumpGfxInfo(pw, fd, args);
Jeff Brown98365d72012-08-19 20:30:52 -0700601 }
Jeff Brown98365d72012-08-19 20:30:52 -0700602 }
Craig Mautner652fdfa2013-06-06 07:51:57 -0700603
604 pw.println("\nView hierarchy:\n");
605
606 int viewsCount = 0;
607 int displayListsSize = 0;
608 int[] info = new int[2];
609
610 for (int i = 0; i < count; i++) {
611 ViewRootImpl root = mRoots.get(i);
612 root.dumpGfxInfo(info);
613
614 String name = getWindowName(root);
615 pw.printf(" %s\n %d views, %.2f kB of display lists",
616 name, info[0], info[1] / 1024.0f);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700617 pw.printf("\n\n");
618
619 viewsCount += info[0];
620 displayListsSize += info[1];
621 }
622
623 pw.printf("\nTotal ViewRootImpl: %d\n", count);
624 pw.printf("Total Views: %d\n", viewsCount);
625 pw.printf("Total DisplayList: %.2f kB\n\n", displayListsSize / 1024.0f);
Jeff Brown98365d72012-08-19 20:30:52 -0700626 }
627 } finally {
628 pw.flush();
629 }
630 }
631
632 private static String getWindowName(ViewRootImpl root) {
633 return root.mWindowAttributes.getTitle() + "/" +
634 root.getClass().getName() + '@' + Integer.toHexString(root.hashCode());
635 }
636
637 public void setStoppedState(IBinder token, boolean stopped) {
638 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700639 int count = mViews.size();
Tetsutoki Shiozawa978e7f82017-11-01 11:38:34 +0900640 for (int i = count - 1; i >= 0; i--) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700641 if (token == null || mParams.get(i).token == token) {
642 ViewRootImpl root = mRoots.get(i);
Tetsutoki Shiozawa978e7f82017-11-01 11:38:34 +0900643 // Client might remove the view by "stopped" event.
George Mount41725de2015-04-09 08:23:05 -0700644 root.setWindowStopped(stopped);
Robert Carr3f7bd972018-04-02 15:09:10 -0700645 // Recursively forward stopped state to View's attached
646 // to this Window rather than the root application token,
647 // e.g. PopupWindow's.
648 setStoppedState(root.mAttachInfo.mWindowToken, stopped);
Jeff Brown98365d72012-08-19 20:30:52 -0700649 }
650 }
651 }
652 }
653
654 public void reportNewConfiguration(Configuration config) {
655 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700656 int count = mViews.size();
657 config = new Configuration(config);
658 for (int i=0; i < count; i++) {
659 ViewRootImpl root = mRoots.get(i);
660 root.requestUpdateConfiguration(config);
Jeff Brown98365d72012-08-19 20:30:52 -0700661 }
662 }
663 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700664
665 /** @hide */
666 public void changeCanvasOpacity(IBinder token, boolean opaque) {
667 if (token == null) {
668 return;
669 }
670 synchronized (mLock) {
671 for (int i = mParams.size() - 1; i >= 0; --i) {
672 if (mParams.get(i).token == token) {
673 mRoots.get(i).changeCanvasOpacity(opaque);
674 return;
675 }
676 }
677 }
678 }
Jeff Brown98365d72012-08-19 20:30:52 -0700679}
680
681final class WindowLeaked extends AndroidRuntimeException {
Mathew Inwooda570dee2018-08-17 14:56:00 +0100682 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700683 public WindowLeaked(String msg) {
684 super(msg);
685 }
686}