blob: 46aea80efe7ac45b61e073c7855254b7d3ef23e0 [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 Inwooda570dee2018-08-17 14:56:00 +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 Inwood8c854f82018-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 Inwooda570dee2018-08-17 14:56:00 +0100136 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700137 private static WindowManagerGlobal sDefaultWindowManager;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100138 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700139 private static IWindowManager sWindowManagerService;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100140 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700141 private static IWindowSession sWindowSession;
142
Mathew Inwooda570dee2018-08-17 14:56:00 +0100143 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700144 private final Object mLock = new Object();
145
Mathew Inwooda570dee2018-08-17 14:56:00 +0100146 @UnsupportedAppUsage
Craig Mautner652fdfa2013-06-06 07:51:57 -0700147 private final ArrayList<View> mViews = new ArrayList<View>();
Mathew Inwooda570dee2018-08-17 14:56:00 +0100148 @UnsupportedAppUsage
Craig Mautner652fdfa2013-06-06 07:51:57 -0700149 private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
Mathew Inwooda570dee2018-08-17 14:56:00 +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 Inwooda570dee2018-08-17 14:56:00 +0100160 @UnsupportedAppUsage
Chet Haase0d1c27a2014-11-03 18:35:16 +0000161 public static void initialize() {
162 getWindowManagerService();
163 }
164
Mathew Inwooda570dee2018-08-17 14:56:00 +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 Inwooda570dee2018-08-17 14:56:00 +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 Inwooda570dee2018-08-17 14:56:00 +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 {
Yohei Yukawa6c075722018-09-21 14:52:12 -0700199 // Emulate the legacy behavior. The global instance of InputMethodManager
200 // was instantiated here.
201 // TODO(b/116157766): Remove this hack after cleaning up @UnsupportedAppUsage
202 InputMethodManager.ensureDefaultInstanceForDefaultDisplayIfNecessary();
Jeff Brown98365d72012-08-19 20:30:52 -0700203 IWindowManager windowManager = getWindowManagerService();
204 sWindowSession = windowManager.openSession(
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700205 new IWindowSessionCallback.Stub() {
206 @Override
207 public void onAnimatorScaleChanged(float scale) {
208 ValueAnimator.setDurationScale(scale);
209 }
Yohei Yukawaa71bb252018-09-19 19:21:24 -0700210 });
Jeff Brown98365d72012-08-19 20:30:52 -0700211 } catch (RemoteException e) {
Jeff Sharkeyd136e512016-03-09 22:30:56 -0700212 throw e.rethrowFromSystemServer();
Jeff Brown98365d72012-08-19 20:30:52 -0700213 }
214 }
215 return sWindowSession;
216 }
217 }
218
Mathew Inwood8c854f82018-09-14 12:35:36 +0100219 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Brown98365d72012-08-19 20:30:52 -0700220 public static IWindowSession peekWindowSession() {
221 synchronized (WindowManagerGlobal.class) {
222 return sWindowSession;
223 }
224 }
225
Mathew Inwooda570dee2018-08-17 14:56:00 +0100226 @UnsupportedAppUsage
Siva Velusamy945bfb62013-01-06 16:03:12 -0800227 public String[] getViewRootNames() {
228 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700229 final int numRoots = mRoots.size();
230 String[] mViewRoots = new String[numRoots];
231 for (int i = 0; i < numRoots; ++i) {
232 mViewRoots[i] = getWindowName(mRoots.get(i));
Siva Velusamy945bfb62013-01-06 16:03:12 -0800233 }
234 return mViewRoots;
235 }
236 }
237
Mathew Inwooda570dee2018-08-17 14:56:00 +0100238 @UnsupportedAppUsage
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800239 public ArrayList<ViewRootImpl> getRootViews(IBinder token) {
240 ArrayList<ViewRootImpl> views = new ArrayList<>();
241 synchronized (mLock) {
242 final int numRoots = mRoots.size();
243 for (int i = 0; i < numRoots; ++i) {
244 WindowManager.LayoutParams params = mParams.get(i);
245 if (params.token == null) {
246 continue;
247 }
248 if (params.token != token) {
249 boolean isChild = false;
250 if (params.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW
251 && params.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
252 for (int j = 0 ; j < numRoots; ++j) {
253 View viewj = mViews.get(j);
254 WindowManager.LayoutParams paramsj = mParams.get(j);
255 if (params.token == viewj.getWindowToken()
256 && paramsj.token == token) {
257 isChild = true;
258 break;
259 }
260 }
261 }
262 if (!isChild) {
263 continue;
264 }
265 }
266 views.add(mRoots.get(i));
267 }
268 }
269 return views;
270 }
271
Vladislav Kaznacheev7039cbc2017-01-04 10:15:31 -0800272 public View getWindowView(IBinder windowToken) {
273 synchronized (mLock) {
274 final int numViews = mViews.size();
275 for (int i = 0; i < numViews; ++i) {
276 final View view = mViews.get(i);
277 if (view.getWindowToken() == windowToken) {
278 return view;
279 }
280 }
281 }
282 return null;
283 }
284
Mathew Inwooda570dee2018-08-17 14:56:00 +0100285 @UnsupportedAppUsage
Siva Velusamy945bfb62013-01-06 16:03:12 -0800286 public View getRootView(String name) {
287 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700288 for (int i = mRoots.size() - 1; i >= 0; --i) {
289 final ViewRootImpl root = mRoots.get(i);
Siva Velusamy945bfb62013-01-06 16:03:12 -0800290 if (name.equals(getWindowName(root))) return root.getView();
291 }
292 }
293
294 return null;
295 }
296
Jeff Brown98365d72012-08-19 20:30:52 -0700297 public void addView(View view, ViewGroup.LayoutParams params,
298 Display display, Window parentWindow) {
299 if (view == null) {
300 throw new IllegalArgumentException("view must not be null");
301 }
302 if (display == null) {
303 throw new IllegalArgumentException("display must not be null");
304 }
305 if (!(params instanceof WindowManager.LayoutParams)) {
306 throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
307 }
308
Alan Viverette9ecb73c2014-12-15 13:40:28 -0800309 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
Jeff Brown98365d72012-08-19 20:30:52 -0700310 if (parentWindow != null) {
311 parentWindow.adjustLayoutParamsForSubWindow(wparams);
Alan Viverette9b0ab652015-03-18 14:21:04 -0700312 } else {
313 // If there's no parent, then hardware acceleration for this view is
314 // set from the application's hardware acceleration setting.
Alan Viverette5e1565e2014-07-29 16:14:25 -0700315 final Context context = view.getContext();
Alan Viverette9b0ab652015-03-18 14:21:04 -0700316 if (context != null
Alan Viveretted70b9e72015-05-27 14:29:20 -0700317 && (context.getApplicationInfo().flags
318 & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
Alan Viverette5e1565e2014-07-29 16:14:25 -0700319 wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
320 }
Jeff Brown98365d72012-08-19 20:30:52 -0700321 }
322
323 ViewRootImpl root;
324 View panelParentView = null;
325
326 synchronized (mLock) {
327 // Start watching for system property changes.
328 if (mSystemPropertyUpdater == null) {
329 mSystemPropertyUpdater = new Runnable() {
330 @Override public void run() {
331 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700332 for (int i = mRoots.size() - 1; i >= 0; --i) {
333 mRoots.get(i).loadSystemProperties();
Jeff Brown98365d72012-08-19 20:30:52 -0700334 }
335 }
336 }
337 };
338 SystemProperties.addChangeCallback(mSystemPropertyUpdater);
339 }
340
341 int index = findViewLocked(view, false);
342 if (index >= 0) {
Craig Mautner8f303ad2013-06-14 11:32:22 -0700343 if (mDyingViews.contains(view)) {
344 // Don't wait for MSG_DIE to make it's way through root's queue.
345 mRoots.get(index).doDie();
346 } else {
347 throw new IllegalStateException("View " + view
348 + " has already been added to the window manager.");
349 }
350 // The previous removeView() had not completed executing. Now it has.
Jeff Brown98365d72012-08-19 20:30:52 -0700351 }
352
353 // If this is a panel window, then find the window it is being
354 // attached to for future reference.
355 if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
356 wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700357 final int count = mViews.size();
358 for (int i = 0; i < count; i++) {
359 if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
360 panelParentView = mViews.get(i);
Jeff Brown98365d72012-08-19 20:30:52 -0700361 }
362 }
363 }
364
365 root = new ViewRootImpl(view.getContext(), display);
366
367 view.setLayoutParams(wparams);
368
Craig Mautner652fdfa2013-06-06 07:51:57 -0700369 mViews.add(view);
370 mRoots.add(root);
371 mParams.add(wparams);
Jeff Brown98365d72012-08-19 20:30:52 -0700372
Gopal Krishna Shuklaf7abcda2016-07-06 08:14:59 +0530373 // do this last because it fires off messages to start doing things
374 try {
375 root.setView(view, wparams, panelParentView);
376 } catch (RuntimeException e) {
377 // BadTokenException or InvalidDisplayException, clean up.
Craig Mautner6018aee2012-10-23 14:27:49 -0700378 if (index >= 0) {
379 removeViewLocked(index, true);
380 }
Gopal Krishna Shuklaf7abcda2016-07-06 08:14:59 +0530381 throw e;
Craig Mautner6018aee2012-10-23 14:27:49 -0700382 }
Craig Mautner6018aee2012-10-23 14:27:49 -0700383 }
Jeff Brown98365d72012-08-19 20:30:52 -0700384 }
385
386 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
387 if (view == null) {
388 throw new IllegalArgumentException("view must not be null");
389 }
390 if (!(params instanceof WindowManager.LayoutParams)) {
391 throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
392 }
393
394 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;
395
396 view.setLayoutParams(wparams);
397
398 synchronized (mLock) {
399 int index = findViewLocked(view, true);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700400 ViewRootImpl root = mRoots.get(index);
401 mParams.remove(index);
402 mParams.add(index, wparams);
Jeff Brown98365d72012-08-19 20:30:52 -0700403 root.setLayoutParams(wparams, false);
404 }
405 }
406
Mathew Inwooda570dee2018-08-17 14:56:00 +0100407 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700408 public void removeView(View view, boolean immediate) {
409 if (view == null) {
410 throw new IllegalArgumentException("view must not be null");
411 }
412
413 synchronized (mLock) {
414 int index = findViewLocked(view, true);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700415 View curView = mRoots.get(index).getView();
Craig Mautner05eb7302013-06-03 17:24:21 -0700416 removeViewLocked(index, immediate);
Jeff Brown98365d72012-08-19 20:30:52 -0700417 if (curView == view) {
418 return;
419 }
420
421 throw new IllegalStateException("Calling with view " + view
422 + " but the ViewAncestor is attached to " + curView);
423 }
424 }
425
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700426 /**
427 * Remove all roots with specified token.
428 *
429 * @param token app or window token.
430 * @param who name of caller, used in logs.
431 * @param what type of caller, used in logs.
432 */
Jeff Brown98365d72012-08-19 20:30:52 -0700433 public void closeAll(IBinder token, String who, String what) {
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700434 closeAllExceptView(token, null /* view */, who, what);
435 }
436
437 /**
438 * Remove all roots with specified token, except maybe one view.
439 *
440 * @param token app or window token.
441 * @param view view that should be should be preserved along with it's root.
442 * Pass null if everything should be removed.
443 * @param who name of caller, used in logs.
444 * @param what type of caller, used in logs.
445 */
446 public void closeAllExceptView(IBinder token, View view, String who, String what) {
Jeff Brown98365d72012-08-19 20:30:52 -0700447 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700448 int count = mViews.size();
Craig Mautner05eb7302013-06-03 17:24:21 -0700449 for (int i = 0; i < count; i++) {
Andrii Kulianeac0ea52016-05-11 15:50:24 -0700450 if ((view == null || mViews.get(i) != view)
451 && (token == null || mParams.get(i).token == token)) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700452 ViewRootImpl root = mRoots.get(i);
Jeff Brown98365d72012-08-19 20:30:52 -0700453
Jeff Brown98365d72012-08-19 20:30:52 -0700454 if (who != null) {
455 WindowLeaked leak = new WindowLeaked(
456 what + " " + who + " has leaked window "
457 + root.getView() + " that was originally added here");
458 leak.setStackTrace(root.getLocation().getStackTrace());
Craig Mautner05eb7302013-06-03 17:24:21 -0700459 Log.e(TAG, "", leak);
Jeff Brown98365d72012-08-19 20:30:52 -0700460 }
461
462 removeViewLocked(i, false);
Jeff Brown98365d72012-08-19 20:30:52 -0700463 }
464 }
465 }
466 }
467
Craig Mautner05eb7302013-06-03 17:24:21 -0700468 private void removeViewLocked(int index, boolean immediate) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700469 ViewRootImpl root = mRoots.get(index);
Jeff Brown98365d72012-08-19 20:30:52 -0700470 View view = root.getView();
471
472 if (view != null) {
Yohei Yukawa484d4af2018-09-17 16:47:08 -0700473 InputMethodManager imm = view.getContext().getSystemService(InputMethodManager.class);
Jeff Brown98365d72012-08-19 20:30:52 -0700474 if (imm != null) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700475 imm.windowDismissed(mViews.get(index).getWindowToken());
Jeff Brown98365d72012-08-19 20:30:52 -0700476 }
477 }
Craig Mautner8f303ad2013-06-14 11:32:22 -0700478 boolean deferred = root.die(immediate);
Craig Mautner92098c72013-06-10 11:27:26 -0700479 if (view != null) {
480 view.assignParent(null);
Craig Mautner8f303ad2013-06-14 11:32:22 -0700481 if (deferred) {
482 mDyingViews.add(view);
483 }
Craig Mautner92098c72013-06-10 11:27:26 -0700484 }
Craig Mautner05eb7302013-06-03 17:24:21 -0700485 }
Jeff Brown98365d72012-08-19 20:30:52 -0700486
Craig Mautner05eb7302013-06-03 17:24:21 -0700487 void doRemoveView(ViewRootImpl root) {
488 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700489 final int index = mRoots.indexOf(root);
490 if (index >= 0) {
491 mRoots.remove(index);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700492 mParams.remove(index);
Craig Mautner8f303ad2013-06-14 11:32:22 -0700493 final View view = mViews.remove(index);
494 mDyingViews.remove(view);
Jeff Brown98365d72012-08-19 20:30:52 -0700495 }
496 }
John Reck51aaf902015-12-02 15:08:07 -0800497 if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
John Reck73840ea2014-09-22 07:39:18 -0700498 doTrimForeground();
499 }
Jeff Brown98365d72012-08-19 20:30:52 -0700500 }
501
502 private int findViewLocked(View view, boolean required) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700503 final int index = mViews.indexOf(view);
504 if (required && index < 0) {
Craig Mautner05eb7302013-06-03 17:24:21 -0700505 throw new IllegalArgumentException("View=" + view + " not attached to window manager");
Craig Mautner6018aee2012-10-23 14:27:49 -0700506 }
Craig Mautner652fdfa2013-06-06 07:51:57 -0700507 return index;
Jeff Brown98365d72012-08-19 20:30:52 -0700508 }
509
John Reckf47a5942014-06-30 16:20:04 -0700510 public static boolean shouldDestroyEglContext(int trimLevel) {
511 // On low-end gfx devices we trim when memory is moderate;
512 // on high-end devices we do this when low.
513 if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
514 return true;
515 }
516 if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_MODERATE
517 && !ActivityManager.isHighEndGfx()) {
518 return true;
519 }
520 return false;
521 }
522
Mathew Inwooda570dee2018-08-17 14:56:00 +0100523 @UnsupportedAppUsage
John Reckf47a5942014-06-30 16:20:04 -0700524 public void trimMemory(int level) {
John Reck51aaf902015-12-02 15:08:07 -0800525 if (ThreadedRenderer.isAvailable()) {
John Reckf47a5942014-06-30 16:20:04 -0700526 if (shouldDestroyEglContext(level)) {
Jeff Brown98365d72012-08-19 20:30:52 -0700527 // Destroy all hardware surfaces and resources associated to
528 // known windows
529 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700530 for (int i = mRoots.size() - 1; i >= 0; --i) {
Romain Guy46bfc482013-08-16 18:38:29 -0700531 mRoots.get(i).destroyHardwareResources();
Jeff Brown98365d72012-08-19 20:30:52 -0700532 }
533 }
534 // Force a full memory flush
John Reckf47a5942014-06-30 16:20:04 -0700535 level = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
Jeff Brown98365d72012-08-19 20:30:52 -0700536 }
537
John Reck51aaf902015-12-02 15:08:07 -0800538 ThreadedRenderer.trimMemory(level);
John Reck73840ea2014-09-22 07:39:18 -0700539
John Reck51aaf902015-12-02 15:08:07 -0800540 if (ThreadedRenderer.sTrimForeground) {
John Reck73840ea2014-09-22 07:39:18 -0700541 doTrimForeground();
542 }
543 }
544 }
545
546 public static void trimForeground() {
John Reck51aaf902015-12-02 15:08:07 -0800547 if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
John Reck73840ea2014-09-22 07:39:18 -0700548 WindowManagerGlobal wm = WindowManagerGlobal.getInstance();
549 wm.doTrimForeground();
550 }
551 }
552
553 private void doTrimForeground() {
554 boolean hasVisibleWindows = false;
555 synchronized (mLock) {
556 for (int i = mRoots.size() - 1; i >= 0; --i) {
John Reckccf2fa02014-09-25 08:33:05 -0700557 final ViewRootImpl root = mRoots.get(i);
558 if (root.mView != null && root.getHostVisibility() == View.VISIBLE
Stan Iliev45faba52016-06-28 13:33:15 -0400559 && root.mAttachInfo.mThreadedRenderer != null) {
John Reck73840ea2014-09-22 07:39:18 -0700560 hasVisibleWindows = true;
561 } else {
John Reckccf2fa02014-09-25 08:33:05 -0700562 root.destroyHardwareResources();
John Reck73840ea2014-09-22 07:39:18 -0700563 }
564 }
565 }
566 if (!hasVisibleWindows) {
John Reck51aaf902015-12-02 15:08:07 -0800567 ThreadedRenderer.trimMemory(
John Reck73840ea2014-09-22 07:39:18 -0700568 ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
Jeff Brown98365d72012-08-19 20:30:52 -0700569 }
570 }
571
John Reckba6adf62015-02-19 14:36:50 -0800572 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
Jeff Brown98365d72012-08-19 20:30:52 -0700573 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700574 PrintWriter pw = new FastPrintWriter(fout);
Jeff Brown98365d72012-08-19 20:30:52 -0700575 try {
576 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700577 final int count = mViews.size();
Jeff Brown98365d72012-08-19 20:30:52 -0700578
Craig Mautner652fdfa2013-06-06 07:51:57 -0700579 pw.println("Profile data in ms:");
Jeff Brown98365d72012-08-19 20:30:52 -0700580
Craig Mautner652fdfa2013-06-06 07:51:57 -0700581 for (int i = 0; i < count; i++) {
582 ViewRootImpl root = mRoots.get(i);
583 String name = getWindowName(root);
John Reck73840ea2014-09-22 07:39:18 -0700584 pw.printf("\n\t%s (visibility=%d)", name, root.getHostVisibility());
Jeff Brown98365d72012-08-19 20:30:52 -0700585
John Reck51aaf902015-12-02 15:08:07 -0800586 ThreadedRenderer renderer =
Stan Iliev45faba52016-06-28 13:33:15 -0400587 root.getView().mAttachInfo.mThreadedRenderer;
Craig Mautner652fdfa2013-06-06 07:51:57 -0700588 if (renderer != null) {
John Reckba6adf62015-02-19 14:36:50 -0800589 renderer.dumpGfxInfo(pw, fd, args);
Jeff Brown98365d72012-08-19 20:30:52 -0700590 }
Jeff Brown98365d72012-08-19 20:30:52 -0700591 }
Craig Mautner652fdfa2013-06-06 07:51:57 -0700592
593 pw.println("\nView hierarchy:\n");
594
595 int viewsCount = 0;
596 int displayListsSize = 0;
597 int[] info = new int[2];
598
599 for (int i = 0; i < count; i++) {
600 ViewRootImpl root = mRoots.get(i);
601 root.dumpGfxInfo(info);
602
603 String name = getWindowName(root);
604 pw.printf(" %s\n %d views, %.2f kB of display lists",
605 name, info[0], info[1] / 1024.0f);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700606 pw.printf("\n\n");
607
608 viewsCount += info[0];
609 displayListsSize += info[1];
610 }
611
612 pw.printf("\nTotal ViewRootImpl: %d\n", count);
613 pw.printf("Total Views: %d\n", viewsCount);
614 pw.printf("Total DisplayList: %.2f kB\n\n", displayListsSize / 1024.0f);
Jeff Brown98365d72012-08-19 20:30:52 -0700615 }
616 } finally {
617 pw.flush();
618 }
619 }
620
621 private static String getWindowName(ViewRootImpl root) {
622 return root.mWindowAttributes.getTitle() + "/" +
623 root.getClass().getName() + '@' + Integer.toHexString(root.hashCode());
624 }
625
626 public void setStoppedState(IBinder token, boolean stopped) {
627 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700628 int count = mViews.size();
Tetsutoki Shiozawa978e7f82017-11-01 11:38:34 +0900629 for (int i = count - 1; i >= 0; i--) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700630 if (token == null || mParams.get(i).token == token) {
631 ViewRootImpl root = mRoots.get(i);
Tetsutoki Shiozawa978e7f82017-11-01 11:38:34 +0900632 // Client might remove the view by "stopped" event.
George Mount41725de2015-04-09 08:23:05 -0700633 root.setWindowStopped(stopped);
Robert Carr3f7bd972018-04-02 15:09:10 -0700634 // Recursively forward stopped state to View's attached
635 // to this Window rather than the root application token,
636 // e.g. PopupWindow's.
637 setStoppedState(root.mAttachInfo.mWindowToken, stopped);
Jeff Brown98365d72012-08-19 20:30:52 -0700638 }
639 }
640 }
641 }
642
643 public void reportNewConfiguration(Configuration config) {
644 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700645 int count = mViews.size();
646 config = new Configuration(config);
647 for (int i=0; i < count; i++) {
648 ViewRootImpl root = mRoots.get(i);
649 root.requestUpdateConfiguration(config);
Jeff Brown98365d72012-08-19 20:30:52 -0700650 }
651 }
652 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700653
654 /** @hide */
655 public void changeCanvasOpacity(IBinder token, boolean opaque) {
656 if (token == null) {
657 return;
658 }
659 synchronized (mLock) {
660 for (int i = mParams.size() - 1; i >= 0; --i) {
661 if (mParams.get(i).token == token) {
662 mRoots.get(i).changeCanvasOpacity(opaque);
663 return;
664 }
665 }
666 }
667 }
Jeff Brown98365d72012-08-19 20:30:52 -0700668}
669
670final class WindowLeaked extends AndroidRuntimeException {
Mathew Inwooda570dee2018-08-17 14:56:00 +0100671 @UnsupportedAppUsage
Jeff Brown98365d72012-08-19 20:30:52 -0700672 public WindowLeaked(String msg) {
673 super(msg);
674 }
675}