Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
| 19 | import android.animation.ValueAnimator; |
Alan Viverette | 1761cfa | 2019-02-13 17:44:08 -0500 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Artur Satayev | ad9254c | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 22 | import android.compat.annotation.UnsupportedAppUsage; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 23 | import android.content.ComponentCallbacks2; |
Alan Viverette | 5e1565e | 2014-07-29 16:14:25 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Alan Viverette | d70b9e7 | 2015-05-27 14:29:20 -0700 | [diff] [blame] | 25 | import android.content.pm.ApplicationInfo; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 26 | import android.content.res.Configuration; |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 27 | import android.os.Build; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 28 | import android.os.IBinder; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
| 31 | import android.os.SystemProperties; |
| 32 | import android.util.AndroidRuntimeException; |
Craig Mautner | 8f303ad | 2013-06-14 11:32:22 -0700 | [diff] [blame] | 33 | import android.util.ArraySet; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 34 | import android.util.Log; |
| 35 | import android.view.inputmethod.InputMethodManager; |
Jorim Jaggi | 4846ee3 | 2016-01-07 17:39:12 +0100 | [diff] [blame] | 36 | |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 37 | import com.android.internal.util.FastPrintWriter; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 38 | |
| 39 | import java.io.FileDescriptor; |
| 40 | import java.io.FileOutputStream; |
| 41 | import java.io.PrintWriter; |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 42 | import java.util.ArrayList; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 43 | |
| 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 | */ |
| 56 | public final class WindowManagerGlobal { |
| 57 | private static final String TAG = "WindowManager"; |
| 58 | |
Valerie Hau | bdb905a | 2020-02-10 15:07:36 -0800 | [diff] [blame] | 59 | private static boolean sUseBLASTAdapter = false; |
Valerie Hau | 089e5da | 2020-01-07 13:10:25 -0800 | [diff] [blame] | 60 | |
| 61 | /** |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 62 | * 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 Jaggi | 4846ee3 | 2016-01-07 17:39:12 +0100 | [diff] [blame] | 79 | * 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 Zhang | 0275e39 | 2015-09-17 10:41:44 -0700 | [diff] [blame] | 86 | * The window is being resized by dragging one of the window corners, |
Filip Gruszczynski | 63a35e2 | 2015-11-05 15:38:59 -0800 | [diff] [blame] | 87 | * in this case the surface would be fullscreen-sized. The client should |
Chong Zhang | 0275e39 | 2015-09-17 10:41:44 -0700 | [diff] [blame] | 88 | * render to the actual frame location (instead of (0,curScrollY)). |
| 89 | */ |
Jorim Jaggi | 4846ee3 | 2016-01-07 17:39:12 +0100 | [diff] [blame] | 90 | public static final int RELAYOUT_RES_DRAG_RESIZING_FREEFORM = 0x10; |
Chong Zhang | 0275e39 | 2015-09-17 10:41:44 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
Chong Zhang | f4abc2b | 2015-11-12 23:40:58 -0800 | [diff] [blame] | 93 | * The window manager has changed the size of the surface from the last call. |
| 94 | */ |
Jorim Jaggi | 4846ee3 | 2016-01-07 17:39:12 +0100 | [diff] [blame] | 95 | public static final int RELAYOUT_RES_SURFACE_RESIZED = 0x20; |
Chong Zhang | f4abc2b | 2015-11-12 23:40:58 -0800 | [diff] [blame] | 96 | |
| 97 | /** |
Brad Stenning | e057369 | 2019-03-11 13:52:46 -0700 | [diff] [blame] | 98 | * 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 Jaggi | 0ffd49c | 2016-02-12 15:04:21 -0800 | [diff] [blame] | 101 | */ |
Brad Stenning | e057369 | 2019-03-11 13:52:46 -0700 | [diff] [blame] | 102 | public static final int RELAYOUT_RES_CONSUME_ALWAYS_SYSTEM_BARS = 0x40; |
Jorim Jaggi | 0ffd49c | 2016-02-12 15:04:21 -0800 | [diff] [blame] | 103 | |
| 104 | /** |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 105 | * 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 Jaggi | 0ffd49c | 2016-02-12 15:04:21 -0800 | [diff] [blame] | 122 | /** |
Brad Stenning | e057369 | 2019-03-11 13:52:46 -0700 | [diff] [blame] | 123 | * Like {@link #RELAYOUT_RES_CONSUME_ALWAYS_SYSTEM_BARS}, but as a "hint" when adding the |
| 124 | * window. |
Jorim Jaggi | 0ffd49c | 2016-02-12 15:04:21 -0800 | [diff] [blame] | 125 | */ |
Brad Stenning | e057369 | 2019-03-11 13:52:46 -0700 | [diff] [blame] | 126 | public static final int ADD_FLAG_ALWAYS_CONSUME_SYSTEM_BARS = 0x4; |
Jorim Jaggi | 0ffd49c | 2016-02-12 15:04:21 -0800 | [diff] [blame] | 127 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 128 | 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 Mautner | 2d5618c | 2012-10-18 13:55:47 -0700 | [diff] [blame] | 137 | public static final int ADD_INVALID_DISPLAY = -9; |
Wale Ogunwale | 74bf065 | 2015-01-12 10:24:36 -0800 | [diff] [blame] | 138 | public static final int ADD_INVALID_TYPE = -10; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 139 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 140 | @UnsupportedAppUsage |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 141 | private static WindowManagerGlobal sDefaultWindowManager; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 142 | @UnsupportedAppUsage |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 143 | private static IWindowManager sWindowManagerService; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 144 | @UnsupportedAppUsage |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 145 | private static IWindowSession sWindowSession; |
| 146 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 147 | @UnsupportedAppUsage |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 148 | private final Object mLock = new Object(); |
| 149 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 150 | @UnsupportedAppUsage |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 151 | private final ArrayList<View> mViews = new ArrayList<View>(); |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 152 | @UnsupportedAppUsage |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 153 | private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>(); |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 154 | @UnsupportedAppUsage |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 155 | private final ArrayList<WindowManager.LayoutParams> mParams = |
| 156 | new ArrayList<WindowManager.LayoutParams>(); |
Craig Mautner | 8f303ad | 2013-06-14 11:32:22 -0700 | [diff] [blame] | 157 | private final ArraySet<View> mDyingViews = new ArraySet<View>(); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 158 | |
| 159 | private Runnable mSystemPropertyUpdater; |
| 160 | |
| 161 | private WindowManagerGlobal() { |
| 162 | } |
| 163 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 164 | @UnsupportedAppUsage |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 165 | public static void initialize() { |
| 166 | getWindowManagerService(); |
| 167 | } |
| 168 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 169 | @UnsupportedAppUsage |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 170 | public static WindowManagerGlobal getInstance() { |
| 171 | synchronized (WindowManagerGlobal.class) { |
| 172 | if (sDefaultWindowManager == null) { |
| 173 | sDefaultWindowManager = new WindowManagerGlobal(); |
| 174 | } |
| 175 | return sDefaultWindowManager; |
| 176 | } |
| 177 | } |
| 178 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 179 | @UnsupportedAppUsage |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 180 | public static IWindowManager getWindowManagerService() { |
| 181 | synchronized (WindowManagerGlobal.class) { |
| 182 | if (sWindowManagerService == null) { |
| 183 | sWindowManagerService = IWindowManager.Stub.asInterface( |
| 184 | ServiceManager.getService("window")); |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 185 | try { |
Phil Weaver | 7eec379 | 2017-02-14 16:51:55 -0800 | [diff] [blame] | 186 | if (sWindowManagerService != null) { |
| 187 | ValueAnimator.setDurationScale( |
| 188 | sWindowManagerService.getCurrentAnimatorScale()); |
Valerie Hau | bdb905a | 2020-02-10 15:07:36 -0800 | [diff] [blame] | 189 | sUseBLASTAdapter = sWindowManagerService.useBLAST(); |
Phil Weaver | 7eec379 | 2017-02-14 16:51:55 -0800 | [diff] [blame] | 190 | } |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 191 | } catch (RemoteException e) { |
Jeff Sharkey | d136e51 | 2016-03-09 22:30:56 -0700 | [diff] [blame] | 192 | throw e.rethrowFromSystemServer(); |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 193 | } |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 194 | } |
| 195 | return sWindowManagerService; |
| 196 | } |
| 197 | } |
| 198 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 199 | @UnsupportedAppUsage |
Jeff Brown | f9e989d | 2013-04-04 23:04:03 -0700 | [diff] [blame] | 200 | public static IWindowSession getWindowSession() { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 201 | synchronized (WindowManagerGlobal.class) { |
| 202 | if (sWindowSession == null) { |
| 203 | try { |
Yohei Yukawa | 6c07572 | 2018-09-21 14:52:12 -0700 | [diff] [blame] | 204 | // Emulate the legacy behavior. The global instance of InputMethodManager |
| 205 | // was instantiated here. |
| 206 | // TODO(b/116157766): Remove this hack after cleaning up @UnsupportedAppUsage |
| 207 | InputMethodManager.ensureDefaultInstanceForDefaultDisplayIfNecessary(); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 208 | IWindowManager windowManager = getWindowManagerService(); |
| 209 | sWindowSession = windowManager.openSession( |
Dianne Hackborn | eb94fa7 | 2014-06-03 17:48:12 -0700 | [diff] [blame] | 210 | new IWindowSessionCallback.Stub() { |
| 211 | @Override |
| 212 | public void onAnimatorScaleChanged(float scale) { |
| 213 | ValueAnimator.setDurationScale(scale); |
| 214 | } |
Yohei Yukawa | a71bb25 | 2018-09-19 19:21:24 -0700 | [diff] [blame] | 215 | }); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 216 | } catch (RemoteException e) { |
Jeff Sharkey | d136e51 | 2016-03-09 22:30:56 -0700 | [diff] [blame] | 217 | throw e.rethrowFromSystemServer(); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | return sWindowSession; |
| 221 | } |
| 222 | } |
| 223 | |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 224 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 225 | public static IWindowSession peekWindowSession() { |
| 226 | synchronized (WindowManagerGlobal.class) { |
| 227 | return sWindowSession; |
| 228 | } |
| 229 | } |
| 230 | |
Valerie Hau | 779af29 | 2020-02-07 10:53:32 -0800 | [diff] [blame] | 231 | /** |
| 232 | * Whether or not to use BLAST for ViewRootImpl |
| 233 | */ |
Valerie Hau | bdb905a | 2020-02-10 15:07:36 -0800 | [diff] [blame] | 234 | public static boolean useBLAST() { |
| 235 | return sUseBLASTAdapter; |
Valerie Hau | 779af29 | 2020-02-07 10:53:32 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 238 | @UnsupportedAppUsage |
Siva Velusamy | 945bfb6 | 2013-01-06 16:03:12 -0800 | [diff] [blame] | 239 | public String[] getViewRootNames() { |
| 240 | synchronized (mLock) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 241 | final int numRoots = mRoots.size(); |
| 242 | String[] mViewRoots = new String[numRoots]; |
| 243 | for (int i = 0; i < numRoots; ++i) { |
| 244 | mViewRoots[i] = getWindowName(mRoots.get(i)); |
Siva Velusamy | 945bfb6 | 2013-01-06 16:03:12 -0800 | [diff] [blame] | 245 | } |
| 246 | return mViewRoots; |
| 247 | } |
| 248 | } |
| 249 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 250 | @UnsupportedAppUsage |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 251 | public ArrayList<ViewRootImpl> getRootViews(IBinder token) { |
| 252 | ArrayList<ViewRootImpl> views = new ArrayList<>(); |
| 253 | synchronized (mLock) { |
| 254 | final int numRoots = mRoots.size(); |
| 255 | for (int i = 0; i < numRoots; ++i) { |
| 256 | WindowManager.LayoutParams params = mParams.get(i); |
| 257 | if (params.token == null) { |
| 258 | continue; |
| 259 | } |
| 260 | if (params.token != token) { |
| 261 | boolean isChild = false; |
| 262 | if (params.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW |
| 263 | && params.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) { |
| 264 | for (int j = 0 ; j < numRoots; ++j) { |
| 265 | View viewj = mViews.get(j); |
| 266 | WindowManager.LayoutParams paramsj = mParams.get(j); |
| 267 | if (params.token == viewj.getWindowToken() |
| 268 | && paramsj.token == token) { |
| 269 | isChild = true; |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | if (!isChild) { |
| 275 | continue; |
| 276 | } |
| 277 | } |
| 278 | views.add(mRoots.get(i)); |
| 279 | } |
| 280 | } |
| 281 | return views; |
| 282 | } |
| 283 | |
Alan Viverette | 1761cfa | 2019-02-13 17:44:08 -0500 | [diff] [blame] | 284 | /** |
| 285 | * @return the list of all views attached to the global window manager |
| 286 | */ |
| 287 | @NonNull |
| 288 | public ArrayList<View> getWindowViews() { |
| 289 | synchronized (mLock) { |
| 290 | return new ArrayList<>(mViews); |
| 291 | } |
| 292 | } |
| 293 | |
Vladislav Kaznacheev | 7039cbc | 2017-01-04 10:15:31 -0800 | [diff] [blame] | 294 | public View getWindowView(IBinder windowToken) { |
| 295 | synchronized (mLock) { |
| 296 | final int numViews = mViews.size(); |
| 297 | for (int i = 0; i < numViews; ++i) { |
| 298 | final View view = mViews.get(i); |
| 299 | if (view.getWindowToken() == windowToken) { |
| 300 | return view; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | return null; |
| 305 | } |
| 306 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 307 | @UnsupportedAppUsage |
Siva Velusamy | 945bfb6 | 2013-01-06 16:03:12 -0800 | [diff] [blame] | 308 | public View getRootView(String name) { |
| 309 | synchronized (mLock) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 310 | for (int i = mRoots.size() - 1; i >= 0; --i) { |
| 311 | final ViewRootImpl root = mRoots.get(i); |
Siva Velusamy | 945bfb6 | 2013-01-06 16:03:12 -0800 | [diff] [blame] | 312 | if (name.equals(getWindowName(root))) return root.getView(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return null; |
| 317 | } |
| 318 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 319 | public void addView(View view, ViewGroup.LayoutParams params, |
| 320 | Display display, Window parentWindow) { |
| 321 | if (view == null) { |
| 322 | throw new IllegalArgumentException("view must not be null"); |
| 323 | } |
| 324 | if (display == null) { |
| 325 | throw new IllegalArgumentException("display must not be null"); |
| 326 | } |
| 327 | if (!(params instanceof WindowManager.LayoutParams)) { |
| 328 | throw new IllegalArgumentException("Params must be WindowManager.LayoutParams"); |
| 329 | } |
| 330 | |
Alan Viverette | 9ecb73c | 2014-12-15 13:40:28 -0800 | [diff] [blame] | 331 | final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 332 | if (parentWindow != null) { |
| 333 | parentWindow.adjustLayoutParamsForSubWindow(wparams); |
Alan Viverette | 9b0ab65 | 2015-03-18 14:21:04 -0700 | [diff] [blame] | 334 | } else { |
| 335 | // If there's no parent, then hardware acceleration for this view is |
| 336 | // set from the application's hardware acceleration setting. |
Alan Viverette | 5e1565e | 2014-07-29 16:14:25 -0700 | [diff] [blame] | 337 | final Context context = view.getContext(); |
Alan Viverette | 9b0ab65 | 2015-03-18 14:21:04 -0700 | [diff] [blame] | 338 | if (context != null |
Alan Viverette | d70b9e7 | 2015-05-27 14:29:20 -0700 | [diff] [blame] | 339 | && (context.getApplicationInfo().flags |
| 340 | & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) { |
Alan Viverette | 5e1565e | 2014-07-29 16:14:25 -0700 | [diff] [blame] | 341 | wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED; |
| 342 | } |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | ViewRootImpl root; |
| 346 | View panelParentView = null; |
| 347 | |
| 348 | synchronized (mLock) { |
| 349 | // Start watching for system property changes. |
| 350 | if (mSystemPropertyUpdater == null) { |
| 351 | mSystemPropertyUpdater = new Runnable() { |
| 352 | @Override public void run() { |
| 353 | synchronized (mLock) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 354 | for (int i = mRoots.size() - 1; i >= 0; --i) { |
| 355 | mRoots.get(i).loadSystemProperties(); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | } |
| 359 | }; |
| 360 | SystemProperties.addChangeCallback(mSystemPropertyUpdater); |
| 361 | } |
| 362 | |
| 363 | int index = findViewLocked(view, false); |
| 364 | if (index >= 0) { |
Craig Mautner | 8f303ad | 2013-06-14 11:32:22 -0700 | [diff] [blame] | 365 | if (mDyingViews.contains(view)) { |
| 366 | // Don't wait for MSG_DIE to make it's way through root's queue. |
| 367 | mRoots.get(index).doDie(); |
| 368 | } else { |
| 369 | throw new IllegalStateException("View " + view |
| 370 | + " has already been added to the window manager."); |
| 371 | } |
| 372 | // The previous removeView() had not completed executing. Now it has. |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | // If this is a panel window, then find the window it is being |
| 376 | // attached to for future reference. |
| 377 | if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW && |
| 378 | wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 379 | final int count = mViews.size(); |
| 380 | for (int i = 0; i < count; i++) { |
| 381 | if (mRoots.get(i).mWindow.asBinder() == wparams.token) { |
| 382 | panelParentView = mViews.get(i); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | root = new ViewRootImpl(view.getContext(), display); |
| 388 | |
| 389 | view.setLayoutParams(wparams); |
| 390 | |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 391 | mViews.add(view); |
| 392 | mRoots.add(root); |
| 393 | mParams.add(wparams); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 394 | |
Gopal Krishna Shukla | f7abcda | 2016-07-06 08:14:59 +0530 | [diff] [blame] | 395 | // do this last because it fires off messages to start doing things |
| 396 | try { |
| 397 | root.setView(view, wparams, panelParentView); |
| 398 | } catch (RuntimeException e) { |
| 399 | // BadTokenException or InvalidDisplayException, clean up. |
Craig Mautner | 6018aee | 2012-10-23 14:27:49 -0700 | [diff] [blame] | 400 | if (index >= 0) { |
| 401 | removeViewLocked(index, true); |
| 402 | } |
Gopal Krishna Shukla | f7abcda | 2016-07-06 08:14:59 +0530 | [diff] [blame] | 403 | throw e; |
Craig Mautner | 6018aee | 2012-10-23 14:27:49 -0700 | [diff] [blame] | 404 | } |
Craig Mautner | 6018aee | 2012-10-23 14:27:49 -0700 | [diff] [blame] | 405 | } |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | public void updateViewLayout(View view, ViewGroup.LayoutParams params) { |
| 409 | if (view == null) { |
| 410 | throw new IllegalArgumentException("view must not be null"); |
| 411 | } |
| 412 | if (!(params instanceof WindowManager.LayoutParams)) { |
| 413 | throw new IllegalArgumentException("Params must be WindowManager.LayoutParams"); |
| 414 | } |
| 415 | |
| 416 | final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params; |
| 417 | |
| 418 | view.setLayoutParams(wparams); |
| 419 | |
| 420 | synchronized (mLock) { |
| 421 | int index = findViewLocked(view, true); |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 422 | ViewRootImpl root = mRoots.get(index); |
| 423 | mParams.remove(index); |
| 424 | mParams.add(index, wparams); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 425 | root.setLayoutParams(wparams, false); |
| 426 | } |
| 427 | } |
| 428 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 429 | @UnsupportedAppUsage |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 430 | public void removeView(View view, boolean immediate) { |
| 431 | if (view == null) { |
| 432 | throw new IllegalArgumentException("view must not be null"); |
| 433 | } |
| 434 | |
| 435 | synchronized (mLock) { |
| 436 | int index = findViewLocked(view, true); |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 437 | View curView = mRoots.get(index).getView(); |
Craig Mautner | 05eb730 | 2013-06-03 17:24:21 -0700 | [diff] [blame] | 438 | removeViewLocked(index, immediate); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 439 | if (curView == view) { |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | throw new IllegalStateException("Calling with view " + view |
| 444 | + " but the ViewAncestor is attached to " + curView); |
| 445 | } |
| 446 | } |
| 447 | |
Andrii Kulian | eac0ea5 | 2016-05-11 15:50:24 -0700 | [diff] [blame] | 448 | /** |
| 449 | * Remove all roots with specified token. |
| 450 | * |
| 451 | * @param token app or window token. |
| 452 | * @param who name of caller, used in logs. |
| 453 | * @param what type of caller, used in logs. |
| 454 | */ |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 455 | public void closeAll(IBinder token, String who, String what) { |
Andrii Kulian | eac0ea5 | 2016-05-11 15:50:24 -0700 | [diff] [blame] | 456 | closeAllExceptView(token, null /* view */, who, what); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Remove all roots with specified token, except maybe one view. |
| 461 | * |
| 462 | * @param token app or window token. |
| 463 | * @param view view that should be should be preserved along with it's root. |
| 464 | * Pass null if everything should be removed. |
| 465 | * @param who name of caller, used in logs. |
| 466 | * @param what type of caller, used in logs. |
| 467 | */ |
| 468 | public void closeAllExceptView(IBinder token, View view, String who, String what) { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 469 | synchronized (mLock) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 470 | int count = mViews.size(); |
Craig Mautner | 05eb730 | 2013-06-03 17:24:21 -0700 | [diff] [blame] | 471 | for (int i = 0; i < count; i++) { |
Andrii Kulian | eac0ea5 | 2016-05-11 15:50:24 -0700 | [diff] [blame] | 472 | if ((view == null || mViews.get(i) != view) |
| 473 | && (token == null || mParams.get(i).token == token)) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 474 | ViewRootImpl root = mRoots.get(i); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 475 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 476 | if (who != null) { |
| 477 | WindowLeaked leak = new WindowLeaked( |
| 478 | what + " " + who + " has leaked window " |
| 479 | + root.getView() + " that was originally added here"); |
| 480 | leak.setStackTrace(root.getLocation().getStackTrace()); |
Craig Mautner | 05eb730 | 2013-06-03 17:24:21 -0700 | [diff] [blame] | 481 | Log.e(TAG, "", leak); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | removeViewLocked(i, false); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
Craig Mautner | 05eb730 | 2013-06-03 17:24:21 -0700 | [diff] [blame] | 490 | private void removeViewLocked(int index, boolean immediate) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 491 | ViewRootImpl root = mRoots.get(index); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 492 | View view = root.getView(); |
| 493 | |
lumark | 970d9d2 | 2019-08-18 17:45:24 +0800 | [diff] [blame] | 494 | if (root != null) { |
| 495 | root.getImeFocusController().onWindowDismissed(); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 496 | } |
Craig Mautner | 8f303ad | 2013-06-14 11:32:22 -0700 | [diff] [blame] | 497 | boolean deferred = root.die(immediate); |
Craig Mautner | 92098c7 | 2013-06-10 11:27:26 -0700 | [diff] [blame] | 498 | if (view != null) { |
| 499 | view.assignParent(null); |
Craig Mautner | 8f303ad | 2013-06-14 11:32:22 -0700 | [diff] [blame] | 500 | if (deferred) { |
| 501 | mDyingViews.add(view); |
| 502 | } |
Craig Mautner | 92098c7 | 2013-06-10 11:27:26 -0700 | [diff] [blame] | 503 | } |
Craig Mautner | 05eb730 | 2013-06-03 17:24:21 -0700 | [diff] [blame] | 504 | } |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 505 | |
Craig Mautner | 05eb730 | 2013-06-03 17:24:21 -0700 | [diff] [blame] | 506 | void doRemoveView(ViewRootImpl root) { |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 507 | boolean allViewsRemoved; |
Craig Mautner | 05eb730 | 2013-06-03 17:24:21 -0700 | [diff] [blame] | 508 | synchronized (mLock) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 509 | final int index = mRoots.indexOf(root); |
| 510 | if (index >= 0) { |
| 511 | mRoots.remove(index); |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 512 | mParams.remove(index); |
Craig Mautner | 8f303ad | 2013-06-14 11:32:22 -0700 | [diff] [blame] | 513 | final View view = mViews.remove(index); |
| 514 | mDyingViews.remove(view); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 515 | } |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 516 | allViewsRemoved = mRoots.isEmpty(); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 517 | } |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 518 | if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) { |
John Reck | 73840ea | 2014-09-22 07:39:18 -0700 | [diff] [blame] | 519 | doTrimForeground(); |
| 520 | } |
Jorim Jaggi | 6d5c801 | 2020-02-28 01:40:27 +0100 | [diff] [blame] | 521 | |
| 522 | // If we don't have any views anymore in our process, we no longer need the |
| 523 | // InsetsAnimationThread to save some resources. |
| 524 | if (allViewsRemoved) { |
| 525 | InsetsAnimationThread.release(); |
| 526 | } |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | private int findViewLocked(View view, boolean required) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 530 | final int index = mViews.indexOf(view); |
| 531 | if (required && index < 0) { |
Craig Mautner | 05eb730 | 2013-06-03 17:24:21 -0700 | [diff] [blame] | 532 | throw new IllegalArgumentException("View=" + view + " not attached to window manager"); |
Craig Mautner | 6018aee | 2012-10-23 14:27:49 -0700 | [diff] [blame] | 533 | } |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 534 | return index; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 535 | } |
| 536 | |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 537 | public static boolean shouldDestroyEglContext(int trimLevel) { |
| 538 | // On low-end gfx devices we trim when memory is moderate; |
| 539 | // on high-end devices we do this when low. |
| 540 | if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) { |
| 541 | return true; |
| 542 | } |
| 543 | if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_MODERATE |
| 544 | && !ActivityManager.isHighEndGfx()) { |
| 545 | return true; |
| 546 | } |
| 547 | return false; |
| 548 | } |
| 549 | |
Sergey Vasilinets | 8b8d3b8 | 2019-02-08 14:27:31 +0000 | [diff] [blame] | 550 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 551 | public void trimMemory(int level) { |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 552 | if (ThreadedRenderer.isAvailable()) { |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 553 | if (shouldDestroyEglContext(level)) { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 554 | // Destroy all hardware surfaces and resources associated to |
| 555 | // known windows |
| 556 | synchronized (mLock) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 557 | for (int i = mRoots.size() - 1; i >= 0; --i) { |
Romain Guy | 46bfc48 | 2013-08-16 18:38:29 -0700 | [diff] [blame] | 558 | mRoots.get(i).destroyHardwareResources(); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | // Force a full memory flush |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 562 | level = ComponentCallbacks2.TRIM_MEMORY_COMPLETE; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 563 | } |
| 564 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 565 | ThreadedRenderer.trimMemory(level); |
John Reck | 73840ea | 2014-09-22 07:39:18 -0700 | [diff] [blame] | 566 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 567 | if (ThreadedRenderer.sTrimForeground) { |
John Reck | 73840ea | 2014-09-22 07:39:18 -0700 | [diff] [blame] | 568 | doTrimForeground(); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | public static void trimForeground() { |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 574 | if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) { |
John Reck | 73840ea | 2014-09-22 07:39:18 -0700 | [diff] [blame] | 575 | WindowManagerGlobal wm = WindowManagerGlobal.getInstance(); |
| 576 | wm.doTrimForeground(); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | private void doTrimForeground() { |
| 581 | boolean hasVisibleWindows = false; |
| 582 | synchronized (mLock) { |
| 583 | for (int i = mRoots.size() - 1; i >= 0; --i) { |
John Reck | ccf2fa0 | 2014-09-25 08:33:05 -0700 | [diff] [blame] | 584 | final ViewRootImpl root = mRoots.get(i); |
| 585 | if (root.mView != null && root.getHostVisibility() == View.VISIBLE |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 586 | && root.mAttachInfo.mThreadedRenderer != null) { |
John Reck | 73840ea | 2014-09-22 07:39:18 -0700 | [diff] [blame] | 587 | hasVisibleWindows = true; |
| 588 | } else { |
John Reck | ccf2fa0 | 2014-09-25 08:33:05 -0700 | [diff] [blame] | 589 | root.destroyHardwareResources(); |
John Reck | 73840ea | 2014-09-22 07:39:18 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | } |
| 593 | if (!hasVisibleWindows) { |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 594 | ThreadedRenderer.trimMemory( |
John Reck | 73840ea | 2014-09-22 07:39:18 -0700 | [diff] [blame] | 595 | ComponentCallbacks2.TRIM_MEMORY_COMPLETE); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 599 | public void dumpGfxInfo(FileDescriptor fd, String[] args) { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 600 | FileOutputStream fout = new FileOutputStream(fd); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 601 | PrintWriter pw = new FastPrintWriter(fout); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 602 | try { |
| 603 | synchronized (mLock) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 604 | final int count = mViews.size(); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 605 | |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 606 | pw.println("Profile data in ms:"); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 607 | |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 608 | for (int i = 0; i < count; i++) { |
| 609 | ViewRootImpl root = mRoots.get(i); |
| 610 | String name = getWindowName(root); |
John Reck | 73840ea | 2014-09-22 07:39:18 -0700 | [diff] [blame] | 611 | pw.printf("\n\t%s (visibility=%d)", name, root.getHostVisibility()); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 612 | |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 613 | ThreadedRenderer renderer = |
Stan Iliev | 45faba5 | 2016-06-28 13:33:15 -0400 | [diff] [blame] | 614 | root.getView().mAttachInfo.mThreadedRenderer; |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 615 | if (renderer != null) { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 616 | renderer.dumpGfxInfo(pw, fd, args); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 617 | } |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 618 | } |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 619 | |
| 620 | pw.println("\nView hierarchy:\n"); |
| 621 | |
John Reck | 183e138 | 2019-10-09 13:41:18 -0700 | [diff] [blame] | 622 | ViewRootImpl.GfxInfo totals = new ViewRootImpl.GfxInfo(); |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 623 | |
| 624 | for (int i = 0; i < count; i++) { |
| 625 | ViewRootImpl root = mRoots.get(i); |
John Reck | 183e138 | 2019-10-09 13:41:18 -0700 | [diff] [blame] | 626 | ViewRootImpl.GfxInfo info = root.getGfxInfo(); |
| 627 | totals.add(info); |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 628 | |
| 629 | String name = getWindowName(root); |
John Reck | 183e138 | 2019-10-09 13:41:18 -0700 | [diff] [blame] | 630 | pw.printf(" %s\n %d views, %.2f kB of render nodes", |
| 631 | name, info.viewCount, info.renderNodeMemoryUsage / 1024.f); |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 632 | pw.printf("\n\n"); |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 633 | } |
| 634 | |
John Reck | 183e138 | 2019-10-09 13:41:18 -0700 | [diff] [blame] | 635 | pw.printf("\nTotal %-15s: %d\n", "ViewRootImpl", count); |
| 636 | pw.printf("Total %-15s: %d\n", "attached Views", totals.viewCount); |
| 637 | pw.printf("Total %-15s: %.2f kB (used) / %.2f kB (capacity)\n\n", "RenderNode", |
| 638 | totals.renderNodeMemoryUsage / 1024.0f, |
| 639 | totals.renderNodeMemoryAllocated / 1024.0f); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 640 | } |
| 641 | } finally { |
| 642 | pw.flush(); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | private static String getWindowName(ViewRootImpl root) { |
| 647 | return root.mWindowAttributes.getTitle() + "/" + |
| 648 | root.getClass().getName() + '@' + Integer.toHexString(root.hashCode()); |
| 649 | } |
| 650 | |
| 651 | public void setStoppedState(IBinder token, boolean stopped) { |
Riddle Hsu | 5828b41 | 2019-04-29 19:22:38 +0800 | [diff] [blame] | 652 | ArrayList<ViewRootImpl> nonCurrentThreadRoots = null; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 653 | synchronized (mLock) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 654 | int count = mViews.size(); |
Tetsutoki Shiozawa | 978e7f8 | 2017-11-01 11:38:34 +0900 | [diff] [blame] | 655 | for (int i = count - 1; i >= 0; i--) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 656 | if (token == null || mParams.get(i).token == token) { |
| 657 | ViewRootImpl root = mRoots.get(i); |
Tetsutoki Shiozawa | 978e7f8 | 2017-11-01 11:38:34 +0900 | [diff] [blame] | 658 | // Client might remove the view by "stopped" event. |
Riddle Hsu | 5828b41 | 2019-04-29 19:22:38 +0800 | [diff] [blame] | 659 | if (root.mThread == Thread.currentThread()) { |
| 660 | root.setWindowStopped(stopped); |
| 661 | } else { |
| 662 | if (nonCurrentThreadRoots == null) { |
| 663 | nonCurrentThreadRoots = new ArrayList<>(); |
| 664 | } |
| 665 | nonCurrentThreadRoots.add(root); |
| 666 | } |
Robert Carr | 3f7bd97 | 2018-04-02 15:09:10 -0700 | [diff] [blame] | 667 | // Recursively forward stopped state to View's attached |
| 668 | // to this Window rather than the root application token, |
| 669 | // e.g. PopupWindow's. |
| 670 | setStoppedState(root.mAttachInfo.mWindowToken, stopped); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | } |
Riddle Hsu | 5828b41 | 2019-04-29 19:22:38 +0800 | [diff] [blame] | 674 | |
| 675 | // Update the stopped state synchronously to ensure the surface won't be used after server |
| 676 | // side has destroyed it. This operation should be outside the lock to avoid any potential |
| 677 | // paths from setWindowStopped to WindowManagerGlobal which may cause deadlocks. |
| 678 | if (nonCurrentThreadRoots != null) { |
| 679 | for (int i = nonCurrentThreadRoots.size() - 1; i >= 0; i--) { |
| 680 | ViewRootImpl root = nonCurrentThreadRoots.get(i); |
| 681 | root.mHandler.runWithScissors(() -> root.setWindowStopped(stopped), 0); |
| 682 | } |
| 683 | } |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | public void reportNewConfiguration(Configuration config) { |
| 687 | synchronized (mLock) { |
Craig Mautner | 652fdfa | 2013-06-06 07:51:57 -0700 | [diff] [blame] | 688 | int count = mViews.size(); |
| 689 | config = new Configuration(config); |
| 690 | for (int i=0; i < count; i++) { |
| 691 | ViewRootImpl root = mRoots.get(i); |
| 692 | root.requestUpdateConfiguration(config); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | } |
Craig Mautner | bc57cd1 | 2013-08-19 15:47:42 -0700 | [diff] [blame] | 696 | |
| 697 | /** @hide */ |
| 698 | public void changeCanvasOpacity(IBinder token, boolean opaque) { |
| 699 | if (token == null) { |
| 700 | return; |
| 701 | } |
| 702 | synchronized (mLock) { |
| 703 | for (int i = mParams.size() - 1; i >= 0; --i) { |
| 704 | if (mParams.get(i).token == token) { |
| 705 | mRoots.get(i).changeCanvasOpacity(opaque); |
| 706 | return; |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | } |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | final class WindowLeaked extends AndroidRuntimeException { |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 714 | @UnsupportedAppUsage |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 715 | public WindowLeaked(String msg) { |
| 716 | super(msg); |
| 717 | } |
| 718 | } |