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