blob: 4ca9a141fd23bc0d209ffcc70110767dc0ef8023 [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;
Mathew Inwoode5ad5982018-08-17 15:07:52 +010020import android.annotation.UnsupportedAppUsage;
Jeff Brown98365d72012-08-19 20:30:52 -070021import android.app.ActivityManager;
22import android.content.ComponentCallbacks2;
Alan Viverette5e1565e2014-07-29 16:14:25 -070023import android.content.Context;
Alan Viveretted70b9e72015-05-27 14:29:20 -070024import android.content.pm.ApplicationInfo;
Jeff Brown98365d72012-08-19 20:30:52 -070025import android.content.res.Configuration;
Mathew Inwood45d2c252018-09-14 12:35:36 +010026import android.os.Build;
Jeff Brown98365d72012-08-19 20:30:52 -070027import android.os.IBinder;
Jeff Brown98365d72012-08-19 20:30:52 -070028import android.os.RemoteException;
29import android.os.ServiceManager;
30import android.os.SystemProperties;
31import android.util.AndroidRuntimeException;
Craig Mautner8f303ad2013-06-14 11:32:22 -070032import android.util.ArraySet;
Jeff Brown98365d72012-08-19 20:30:52 -070033import android.util.Log;
34import android.view.inputmethod.InputMethodManager;
Jorim Jaggi4846ee32016-01-07 17:39:12 +010035
Dianne Hackborn8c841092013-06-24 13:46:13 -070036import com.android.internal.util.FastPrintWriter;
Jeff Brown98365d72012-08-19 20:30:52 -070037
38import java.io.FileDescriptor;
39import java.io.FileOutputStream;
40import java.io.PrintWriter;
Craig Mautner652fdfa2013-06-06 07:51:57 -070041import java.util.ArrayList;
Jeff Brown98365d72012-08-19 20:30:52 -070042
43/**
44 * Provides low-level communication with the system window manager for
45 * operations that are not associated with any particular context.
46 *
47 * This class is only used internally to implement global functions where
48 * the caller already knows the display and relevant compatibility information
49 * for the operation. For most purposes, you should use {@link WindowManager} instead
50 * since it is bound to a context.
51 *
52 * @see WindowManagerImpl
53 * @hide
54 */
55public final class WindowManagerGlobal {
56 private static final String TAG = "WindowManager";
57
58 /**
59 * The user is navigating with keys (not the touch screen), so
60 * navigational focus should be shown.
61 */
62 public static final int RELAYOUT_RES_IN_TOUCH_MODE = 0x1;
63
64 /**
65 * This is the first time the window is being drawn,
66 * so the client must call drawingFinished() when done
67 */
68 public static final int RELAYOUT_RES_FIRST_TIME = 0x2;
69
70 /**
71 * The window manager has changed the surface from the last call.
72 */
73 public static final int RELAYOUT_RES_SURFACE_CHANGED = 0x4;
74
75 /**
Jorim Jaggi4846ee32016-01-07 17:39:12 +010076 * The window is being resized by dragging on the docked divider. The client should render
77 * at (0, 0) and extend its background to the background frame passed into
78 * {@link IWindow#resized}.
79 */
80 public static final int RELAYOUT_RES_DRAG_RESIZING_DOCKED = 0x8;
81
82 /**
Chong Zhang0275e392015-09-17 10:41:44 -070083 * The window is being resized by dragging one of the window corners,
Filip Gruszczynski63a35e22015-11-05 15:38:59 -080084 * in this case the surface would be fullscreen-sized. The client should
Chong Zhang0275e392015-09-17 10:41:44 -070085 * render to the actual frame location (instead of (0,curScrollY)).
86 */
Jorim Jaggi4846ee32016-01-07 17:39:12 +010087 public static final int RELAYOUT_RES_DRAG_RESIZING_FREEFORM = 0x10;
Chong Zhang0275e392015-09-17 10:41:44 -070088
89 /**
Chong Zhangf4abc2b2015-11-12 23:40:58 -080090 * The window manager has changed the size of the surface from the last call.
91 */
Jorim Jaggi4846ee32016-01-07 17:39:12 +010092 public static final int RELAYOUT_RES_SURFACE_RESIZED = 0x20;
Chong Zhangf4abc2b2015-11-12 23:40:58 -080093
94 /**
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -080095 * In multi-window we force show the navigation bar. Because we don't want that the surface size
96 * changes in this mode, we instead have a flag whether the navigation bar size should always be
97 * consumed, so the app is treated like there is no virtual navigation bar at all.
98 */
99 public static final int RELAYOUT_RES_CONSUME_ALWAYS_NAV_BAR = 0x40;
100
101 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700102 * Flag for relayout: the client will be later giving
103 * internal insets; as a result, the window will not impact other window
104 * layouts until the insets are given.
105 */
106 public static final int RELAYOUT_INSETS_PENDING = 0x1;
107
108 /**
109 * Flag for relayout: the client may be currently using the current surface,
110 * so if it is to be destroyed as a part of the relayout the destroy must
111 * be deferred until later. The client will call performDeferredDestroy()
112 * when it is okay.
113 */
114 public static final int RELAYOUT_DEFER_SURFACE_DESTROY = 0x2;
115
116 public static final int ADD_FLAG_APP_VISIBLE = 0x2;
117 public static final int ADD_FLAG_IN_TOUCH_MODE = RELAYOUT_RES_IN_TOUCH_MODE;
118
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -0800119 /**
120 * Like {@link #RELAYOUT_RES_CONSUME_ALWAYS_NAV_BAR}, but as a "hint" when adding the window.
121 */
122 public static final int ADD_FLAG_ALWAYS_CONSUME_NAV_BAR = 0x4;
123
Jeff Brown98365d72012-08-19 20:30:52 -0700124 public static final int ADD_OKAY = 0;
125 public static final int ADD_BAD_APP_TOKEN = -1;
126 public static final int ADD_BAD_SUBWINDOW_TOKEN = -2;
127 public static final int ADD_NOT_APP_TOKEN = -3;
128 public static final int ADD_APP_EXITING = -4;
129 public static final int ADD_DUPLICATE_ADD = -5;
130 public static final int ADD_STARTING_NOT_NEEDED = -6;
131 public static final int ADD_MULTIPLE_SINGLETON = -7;
132 public static final int ADD_PERMISSION_DENIED = -8;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700133 public static final int ADD_INVALID_DISPLAY = -9;
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800134 public static final int ADD_INVALID_TYPE = -10;
Jeff Brown98365d72012-08-19 20:30:52 -0700135
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100136 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700137 private static WindowManagerGlobal sDefaultWindowManager;
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100138 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700139 private static IWindowManager sWindowManagerService;
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100140 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700141 private static IWindowSession sWindowSession;
142
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100143 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700144 private final Object mLock = new Object();
145
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100146 @UnsupportedAppUsage
Craig Mautner652fdfa2013-06-06 07:51:57 -0700147 private final ArrayList<View> mViews = new ArrayList<View>();
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100148 @UnsupportedAppUsage
Craig Mautner652fdfa2013-06-06 07:51:57 -0700149 private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100150 @UnsupportedAppUsage
Craig Mautner652fdfa2013-06-06 07:51:57 -0700151 private final ArrayList<WindowManager.LayoutParams> mParams =
152 new ArrayList<WindowManager.LayoutParams>();
Craig Mautner8f303ad2013-06-14 11:32:22 -0700153 private final ArraySet<View> mDyingViews = new ArraySet<View>();
Jeff Brown98365d72012-08-19 20:30:52 -0700154
155 private Runnable mSystemPropertyUpdater;
156
157 private WindowManagerGlobal() {
158 }
159
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100160 @UnsupportedAppUsage
Chet Haase0d1c27a2014-11-03 18:35:16 +0000161 public static void initialize() {
162 getWindowManagerService();
163 }
164
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100165 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700166 public static WindowManagerGlobal getInstance() {
167 synchronized (WindowManagerGlobal.class) {
168 if (sDefaultWindowManager == null) {
169 sDefaultWindowManager = new WindowManagerGlobal();
170 }
171 return sDefaultWindowManager;
172 }
173 }
174
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100175 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700176 public static IWindowManager getWindowManagerService() {
177 synchronized (WindowManagerGlobal.class) {
178 if (sWindowManagerService == null) {
179 sWindowManagerService = IWindowManager.Stub.asInterface(
180 ServiceManager.getService("window"));
Chet Haase0d1c27a2014-11-03 18:35:16 +0000181 try {
Phil Weaver7eec3792017-02-14 16:51:55 -0800182 if (sWindowManagerService != null) {
183 ValueAnimator.setDurationScale(
184 sWindowManagerService.getCurrentAnimatorScale());
185 }
Chet Haase0d1c27a2014-11-03 18:35:16 +0000186 } catch (RemoteException e) {
Jeff Sharkeyd136e512016-03-09 22:30:56 -0700187 throw e.rethrowFromSystemServer();
Chet Haase0d1c27a2014-11-03 18:35:16 +0000188 }
Jeff Brown98365d72012-08-19 20:30:52 -0700189 }
190 return sWindowManagerService;
191 }
192 }
193
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100194 @UnsupportedAppUsage
Jeff Brownf9e989d2013-04-04 23:04:03 -0700195 public static IWindowSession getWindowSession() {
Jeff Brown98365d72012-08-19 20:30:52 -0700196 synchronized (WindowManagerGlobal.class) {
197 if (sWindowSession == null) {
198 try {
Jeff Brownf9e989d2013-04-04 23:04:03 -0700199 InputMethodManager imm = InputMethodManager.getInstance();
Jeff Brown98365d72012-08-19 20:30:52 -0700200 IWindowManager windowManager = getWindowManagerService();
201 sWindowSession = windowManager.openSession(
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700202 new IWindowSessionCallback.Stub() {
203 @Override
204 public void onAnimatorScaleChanged(float scale) {
205 ValueAnimator.setDurationScale(scale);
206 }
207 },
Jeff Brown98365d72012-08-19 20:30:52 -0700208 imm.getClient(), imm.getInputContext());
Jeff Brown98365d72012-08-19 20:30:52 -0700209 } catch (RemoteException e) {
Jeff Sharkeyd136e512016-03-09 22:30:56 -0700210 throw e.rethrowFromSystemServer();
Jeff Brown98365d72012-08-19 20:30:52 -0700211 }
212 }
213 return sWindowSession;
214 }
215 }
216
Mathew Inwood45d2c252018-09-14 12:35:36 +0100217 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Brown98365d72012-08-19 20:30:52 -0700218 public static IWindowSession peekWindowSession() {
219 synchronized (WindowManagerGlobal.class) {
220 return sWindowSession;
221 }
222 }
223
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100224 @UnsupportedAppUsage
Siva Velusamy945bfb62013-01-06 16:03:12 -0800225 public String[] getViewRootNames() {
226 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700227 final int numRoots = mRoots.size();
228 String[] mViewRoots = new String[numRoots];
229 for (int i = 0; i < numRoots; ++i) {
230 mViewRoots[i] = getWindowName(mRoots.get(i));
Siva Velusamy945bfb62013-01-06 16:03:12 -0800231 }
232 return mViewRoots;
233 }
234 }
235
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100236 @UnsupportedAppUsage
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800237 public ArrayList<ViewRootImpl> getRootViews(IBinder token) {
238 ArrayList<ViewRootImpl> views = new ArrayList<>();
239 synchronized (mLock) {
240 final int numRoots = mRoots.size();
241 for (int i = 0; i < numRoots; ++i) {
242 WindowManager.LayoutParams params = mParams.get(i);
243 if (params.token == null) {
244 continue;
245 }
246 if (params.token != token) {
247 boolean isChild = false;
248 if (params.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW
249 && params.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
250 for (int j = 0 ; j < numRoots; ++j) {
251 View viewj = mViews.get(j);
252 WindowManager.LayoutParams paramsj = mParams.get(j);
253 if (params.token == viewj.getWindowToken()
254 && paramsj.token == token) {
255 isChild = true;
256 break;
257 }
258 }
259 }
260 if (!isChild) {
261 continue;
262 }
263 }
264 views.add(mRoots.get(i));
265 }
266 }
267 return views;
268 }
269
Vladislav Kaznacheev7039cbc2017-01-04 10:15:31 -0800270 public View getWindowView(IBinder windowToken) {
271 synchronized (mLock) {
272 final int numViews = mViews.size();
273 for (int i = 0; i < numViews; ++i) {
274 final View view = mViews.get(i);
275 if (view.getWindowToken() == windowToken) {
276 return view;
277 }
278 }
279 }
280 return null;
281 }
282
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100283 @UnsupportedAppUsage
Siva Velusamy945bfb62013-01-06 16:03:12 -0800284 public View getRootView(String name) {
285 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700286 for (int i = mRoots.size() - 1; i >= 0; --i) {
287 final ViewRootImpl root = mRoots.get(i);
Siva Velusamy945bfb62013-01-06 16:03:12 -0800288 if (name.equals(getWindowName(root))) return root.getView();
289 }
290 }
291
292 return null;
293 }
294
Jeff Brown98365d72012-08-19 20:30:52 -0700295 public void addView(View view, ViewGroup.LayoutParams params,
296 Display display, Window parentWindow) {
297 if (view == null) {
298 throw new IllegalArgumentException("view must not be null");
299 }
300 if (display == null) {
301 throw new IllegalArgumentException("display must not be null");
302 }
303 if (!(params instanceof WindowManager.LayoutParams)) {
304 throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
305 }
306
Alan Viverette9ecb73c2014-12-15 13:40:28 -0800307 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
Jeff Brown98365d72012-08-19 20:30:52 -0700308 if (parentWindow != null) {
309 parentWindow.adjustLayoutParamsForSubWindow(wparams);
Alan Viverette9b0ab652015-03-18 14:21:04 -0700310 } else {
311 // If there's no parent, then hardware acceleration for this view is
312 // set from the application's hardware acceleration setting.
Alan Viverette5e1565e2014-07-29 16:14:25 -0700313 final Context context = view.getContext();
Alan Viverette9b0ab652015-03-18 14:21:04 -0700314 if (context != null
Alan Viveretted70b9e72015-05-27 14:29:20 -0700315 && (context.getApplicationInfo().flags
316 & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
Alan Viverette5e1565e2014-07-29 16:14:25 -0700317 wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
318 }
Jeff Brown98365d72012-08-19 20:30:52 -0700319 }
320
321 ViewRootImpl root;
322 View panelParentView = null;
323
324 synchronized (mLock) {
325 // Start watching for system property changes.
326 if (mSystemPropertyUpdater == null) {
327 mSystemPropertyUpdater = new Runnable() {
328 @Override public void run() {
329 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700330 for (int i = mRoots.size() - 1; i >= 0; --i) {
331 mRoots.get(i).loadSystemProperties();
Jeff Brown98365d72012-08-19 20:30:52 -0700332 }
333 }
334 }
335 };
336 SystemProperties.addChangeCallback(mSystemPropertyUpdater);
337 }
338
339 int index = findViewLocked(view, false);
340 if (index >= 0) {
Craig Mautner8f303ad2013-06-14 11:32:22 -0700341 if (mDyingViews.contains(view)) {
342 // Don't wait for MSG_DIE to make it's way through root's queue.
343 mRoots.get(index).doDie();
344 } else {
345 throw new IllegalStateException("View " + view
346 + " has already been added to the window manager.");
347 }
348 // The previous removeView() had not completed executing. Now it has.
Jeff Brown98365d72012-08-19 20:30:52 -0700349 }
350
351 // If this is a panel window, then find the window it is being
352 // attached to for future reference.
353 if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
354 wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700355 final int count = mViews.size();
356 for (int i = 0; i < count; i++) {
357 if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
358 panelParentView = mViews.get(i);
Jeff Brown98365d72012-08-19 20:30:52 -0700359 }
360 }
361 }
362
363 root = new ViewRootImpl(view.getContext(), display);
364
365 view.setLayoutParams(wparams);
366
Craig Mautner652fdfa2013-06-06 07:51:57 -0700367 mViews.add(view);
368 mRoots.add(root);
369 mParams.add(wparams);
Jeff Brown98365d72012-08-19 20:30:52 -0700370
Gopal Krishna Shuklaf7abcda2016-07-06 08:14:59 +0530371 // do this last because it fires off messages to start doing things
372 try {
373 root.setView(view, wparams, panelParentView);
374 } catch (RuntimeException e) {
375 // BadTokenException or InvalidDisplayException, clean up.
Craig Mautner6018aee2012-10-23 14:27:49 -0700376 if (index >= 0) {
377 removeViewLocked(index, true);
378 }
Gopal Krishna Shuklaf7abcda2016-07-06 08:14:59 +0530379 throw e;
Craig Mautner6018aee2012-10-23 14:27:49 -0700380 }
Craig Mautner6018aee2012-10-23 14:27:49 -0700381 }
Jeff Brown98365d72012-08-19 20:30:52 -0700382 }
383
384 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
385 if (view == null) {
386 throw new IllegalArgumentException("view must not be null");
387 }
388 if (!(params instanceof WindowManager.LayoutParams)) {
389 throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
390 }
391
392 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;
393
394 view.setLayoutParams(wparams);
395
396 synchronized (mLock) {
397 int index = findViewLocked(view, true);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700398 ViewRootImpl root = mRoots.get(index);
399 mParams.remove(index);
400 mParams.add(index, wparams);
Jeff Brown98365d72012-08-19 20:30:52 -0700401 root.setLayoutParams(wparams, false);
402 }
403 }
404
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100405 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700406 public void removeView(View view, boolean immediate) {
407 if (view == null) {
408 throw new IllegalArgumentException("view must not be null");
409 }
410
411 synchronized (mLock) {
412 int index = findViewLocked(view, true);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700413 View curView = mRoots.get(index).getView();
Craig Mautner05eb7302013-06-03 17:24:21 -0700414 removeViewLocked(index, immediate);
Jeff Brown98365d72012-08-19 20:30:52 -0700415 if (curView == view) {
416 return;
417 }
418
419 throw new IllegalStateException("Calling with view " + view
420 + " but the ViewAncestor is attached to " + curView);
421 }
422 }
423
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700424 /**
425 * Remove all roots with specified token.
426 *
427 * @param token app or window token.
428 * @param who name of caller, used in logs.
429 * @param what type of caller, used in logs.
430 */
Jeff Brown98365d72012-08-19 20:30:52 -0700431 public void closeAll(IBinder token, String who, String what) {
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700432 closeAllExceptView(token, null /* view */, who, what);
433 }
434
435 /**
436 * Remove all roots with specified token, except maybe one view.
437 *
438 * @param token app or window token.
439 * @param view view that should be should be preserved along with it's root.
440 * Pass null if everything should be removed.
441 * @param who name of caller, used in logs.
442 * @param what type of caller, used in logs.
443 */
444 public void closeAllExceptView(IBinder token, View view, String who, String what) {
Jeff Brown98365d72012-08-19 20:30:52 -0700445 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700446 int count = mViews.size();
Craig Mautner05eb7302013-06-03 17:24:21 -0700447 for (int i = 0; i < count; i++) {
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700448 if ((view == null || mViews.get(i) != view)
449 && (token == null || mParams.get(i).token == token)) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700450 ViewRootImpl root = mRoots.get(i);
Jeff Brown98365d72012-08-19 20:30:52 -0700451
Jeff Brown98365d72012-08-19 20:30:52 -0700452 if (who != null) {
453 WindowLeaked leak = new WindowLeaked(
454 what + " " + who + " has leaked window "
455 + root.getView() + " that was originally added here");
456 leak.setStackTrace(root.getLocation().getStackTrace());
Craig Mautner05eb7302013-06-03 17:24:21 -0700457 Log.e(TAG, "", leak);
Jeff Brown98365d72012-08-19 20:30:52 -0700458 }
459
460 removeViewLocked(i, false);
Jeff Brown98365d72012-08-19 20:30:52 -0700461 }
462 }
463 }
464 }
465
Craig Mautner05eb7302013-06-03 17:24:21 -0700466 private void removeViewLocked(int index, boolean immediate) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700467 ViewRootImpl root = mRoots.get(index);
Jeff Brown98365d72012-08-19 20:30:52 -0700468 View view = root.getView();
469
470 if (view != null) {
Jeff Brownf9e989d2013-04-04 23:04:03 -0700471 InputMethodManager imm = InputMethodManager.getInstance();
Jeff Brown98365d72012-08-19 20:30:52 -0700472 if (imm != null) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700473 imm.windowDismissed(mViews.get(index).getWindowToken());
Jeff Brown98365d72012-08-19 20:30:52 -0700474 }
475 }
Craig Mautner8f303ad2013-06-14 11:32:22 -0700476 boolean deferred = root.die(immediate);
Craig Mautner92098c72013-06-10 11:27:26 -0700477 if (view != null) {
478 view.assignParent(null);
Craig Mautner8f303ad2013-06-14 11:32:22 -0700479 if (deferred) {
480 mDyingViews.add(view);
481 }
Craig Mautner92098c72013-06-10 11:27:26 -0700482 }
Craig Mautner05eb7302013-06-03 17:24:21 -0700483 }
Jeff Brown98365d72012-08-19 20:30:52 -0700484
Craig Mautner05eb7302013-06-03 17:24:21 -0700485 void doRemoveView(ViewRootImpl root) {
486 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700487 final int index = mRoots.indexOf(root);
488 if (index >= 0) {
489 mRoots.remove(index);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700490 mParams.remove(index);
Craig Mautner8f303ad2013-06-14 11:32:22 -0700491 final View view = mViews.remove(index);
492 mDyingViews.remove(view);
Jeff Brown98365d72012-08-19 20:30:52 -0700493 }
494 }
John Reck51aaf902015-12-02 15:08:07 -0800495 if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
John Reck73840ea2014-09-22 07:39:18 -0700496 doTrimForeground();
497 }
Jeff Brown98365d72012-08-19 20:30:52 -0700498 }
499
500 private int findViewLocked(View view, boolean required) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700501 final int index = mViews.indexOf(view);
502 if (required && index < 0) {
Craig Mautner05eb7302013-06-03 17:24:21 -0700503 throw new IllegalArgumentException("View=" + view + " not attached to window manager");
Craig Mautner6018aee2012-10-23 14:27:49 -0700504 }
Craig Mautner652fdfa2013-06-06 07:51:57 -0700505 return index;
Jeff Brown98365d72012-08-19 20:30:52 -0700506 }
507
John Reckf47a5942014-06-30 16:20:04 -0700508 public static boolean shouldDestroyEglContext(int trimLevel) {
509 // On low-end gfx devices we trim when memory is moderate;
510 // on high-end devices we do this when low.
511 if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
512 return true;
513 }
514 if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_MODERATE
515 && !ActivityManager.isHighEndGfx()) {
516 return true;
517 }
518 return false;
519 }
520
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100521 @UnsupportedAppUsage
John Reckf47a5942014-06-30 16:20:04 -0700522 public void trimMemory(int level) {
John Reck51aaf902015-12-02 15:08:07 -0800523 if (ThreadedRenderer.isAvailable()) {
John Reckf47a5942014-06-30 16:20:04 -0700524 if (shouldDestroyEglContext(level)) {
Jeff Brown98365d72012-08-19 20:30:52 -0700525 // Destroy all hardware surfaces and resources associated to
526 // known windows
527 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700528 for (int i = mRoots.size() - 1; i >= 0; --i) {
Romain Guy46bfc482013-08-16 18:38:29 -0700529 mRoots.get(i).destroyHardwareResources();
Jeff Brown98365d72012-08-19 20:30:52 -0700530 }
531 }
532 // Force a full memory flush
John Reckf47a5942014-06-30 16:20:04 -0700533 level = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
Jeff Brown98365d72012-08-19 20:30:52 -0700534 }
535
John Reck51aaf902015-12-02 15:08:07 -0800536 ThreadedRenderer.trimMemory(level);
John Reck73840ea2014-09-22 07:39:18 -0700537
John Reck51aaf902015-12-02 15:08:07 -0800538 if (ThreadedRenderer.sTrimForeground) {
John Reck73840ea2014-09-22 07:39:18 -0700539 doTrimForeground();
540 }
541 }
542 }
543
544 public static void trimForeground() {
John Reck51aaf902015-12-02 15:08:07 -0800545 if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
John Reck73840ea2014-09-22 07:39:18 -0700546 WindowManagerGlobal wm = WindowManagerGlobal.getInstance();
547 wm.doTrimForeground();
548 }
549 }
550
551 private void doTrimForeground() {
552 boolean hasVisibleWindows = false;
553 synchronized (mLock) {
554 for (int i = mRoots.size() - 1; i >= 0; --i) {
John Reckccf2fa02014-09-25 08:33:05 -0700555 final ViewRootImpl root = mRoots.get(i);
556 if (root.mView != null && root.getHostVisibility() == View.VISIBLE
Stan Iliev45faba52016-06-28 13:33:15 -0400557 && root.mAttachInfo.mThreadedRenderer != null) {
John Reck73840ea2014-09-22 07:39:18 -0700558 hasVisibleWindows = true;
559 } else {
John Reckccf2fa02014-09-25 08:33:05 -0700560 root.destroyHardwareResources();
John Reck73840ea2014-09-22 07:39:18 -0700561 }
562 }
563 }
564 if (!hasVisibleWindows) {
John Reck51aaf902015-12-02 15:08:07 -0800565 ThreadedRenderer.trimMemory(
John Reck73840ea2014-09-22 07:39:18 -0700566 ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
Jeff Brown98365d72012-08-19 20:30:52 -0700567 }
568 }
569
John Reckba6adf62015-02-19 14:36:50 -0800570 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
Jeff Brown98365d72012-08-19 20:30:52 -0700571 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700572 PrintWriter pw = new FastPrintWriter(fout);
Jeff Brown98365d72012-08-19 20:30:52 -0700573 try {
574 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700575 final int count = mViews.size();
Jeff Brown98365d72012-08-19 20:30:52 -0700576
Craig Mautner652fdfa2013-06-06 07:51:57 -0700577 pw.println("Profile data in ms:");
Jeff Brown98365d72012-08-19 20:30:52 -0700578
Craig Mautner652fdfa2013-06-06 07:51:57 -0700579 for (int i = 0; i < count; i++) {
580 ViewRootImpl root = mRoots.get(i);
581 String name = getWindowName(root);
John Reck73840ea2014-09-22 07:39:18 -0700582 pw.printf("\n\t%s (visibility=%d)", name, root.getHostVisibility());
Jeff Brown98365d72012-08-19 20:30:52 -0700583
John Reck51aaf902015-12-02 15:08:07 -0800584 ThreadedRenderer renderer =
Stan Iliev45faba52016-06-28 13:33:15 -0400585 root.getView().mAttachInfo.mThreadedRenderer;
Craig Mautner652fdfa2013-06-06 07:51:57 -0700586 if (renderer != null) {
John Reckba6adf62015-02-19 14:36:50 -0800587 renderer.dumpGfxInfo(pw, fd, args);
Jeff Brown98365d72012-08-19 20:30:52 -0700588 }
Jeff Brown98365d72012-08-19 20:30:52 -0700589 }
Craig Mautner652fdfa2013-06-06 07:51:57 -0700590
591 pw.println("\nView hierarchy:\n");
592
593 int viewsCount = 0;
594 int displayListsSize = 0;
595 int[] info = new int[2];
596
597 for (int i = 0; i < count; i++) {
598 ViewRootImpl root = mRoots.get(i);
599 root.dumpGfxInfo(info);
600
601 String name = getWindowName(root);
602 pw.printf(" %s\n %d views, %.2f kB of display lists",
603 name, info[0], info[1] / 1024.0f);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700604 pw.printf("\n\n");
605
606 viewsCount += info[0];
607 displayListsSize += info[1];
608 }
609
610 pw.printf("\nTotal ViewRootImpl: %d\n", count);
611 pw.printf("Total Views: %d\n", viewsCount);
612 pw.printf("Total DisplayList: %.2f kB\n\n", displayListsSize / 1024.0f);
Jeff Brown98365d72012-08-19 20:30:52 -0700613 }
614 } finally {
615 pw.flush();
616 }
617 }
618
619 private static String getWindowName(ViewRootImpl root) {
620 return root.mWindowAttributes.getTitle() + "/" +
621 root.getClass().getName() + '@' + Integer.toHexString(root.hashCode());
622 }
623
624 public void setStoppedState(IBinder token, boolean stopped) {
625 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700626 int count = mViews.size();
Tetsutoki Shiozawa978e7f82017-11-01 11:38:34 +0900627 for (int i = count - 1; i >= 0; i--) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700628 if (token == null || mParams.get(i).token == token) {
629 ViewRootImpl root = mRoots.get(i);
Tetsutoki Shiozawa978e7f82017-11-01 11:38:34 +0900630 // Client might remove the view by "stopped" event.
George Mount41725de2015-04-09 08:23:05 -0700631 root.setWindowStopped(stopped);
Robert Carr3f7bd972018-04-02 15:09:10 -0700632 // Recursively forward stopped state to View's attached
633 // to this Window rather than the root application token,
634 // e.g. PopupWindow's.
635 setStoppedState(root.mAttachInfo.mWindowToken, stopped);
Jeff Brown98365d72012-08-19 20:30:52 -0700636 }
637 }
638 }
639 }
640
641 public void reportNewConfiguration(Configuration config) {
642 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700643 int count = mViews.size();
644 config = new Configuration(config);
645 for (int i=0; i < count; i++) {
646 ViewRootImpl root = mRoots.get(i);
647 root.requestUpdateConfiguration(config);
Jeff Brown98365d72012-08-19 20:30:52 -0700648 }
649 }
650 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700651
652 /** @hide */
653 public void changeCanvasOpacity(IBinder token, boolean opaque) {
654 if (token == null) {
655 return;
656 }
657 synchronized (mLock) {
658 for (int i = mParams.size() - 1; i >= 0; --i) {
659 if (mParams.get(i).token == token) {
660 mRoots.get(i).changeCanvasOpacity(opaque);
661 return;
662 }
663 }
664 }
665 }
Jeff Brown98365d72012-08-19 20:30:52 -0700666}
667
668final class WindowLeaked extends AndroidRuntimeException {
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100669 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700670 public WindowLeaked(String msg) {
671 super(msg);
672 }
673}