diff options
| author | Charles Chen <charlesccchen@google.com> | 2020-04-15 14:28:13 +0800 |
|---|---|---|
| committer | Charles Chen <charlesccchen@google.com> | 2020-04-22 18:16:56 +0800 |
| commit | 2622d3ef7ea02aaf254e15b54358d83e0066f17c (patch) | |
| tree | 0ca4e52ddf74886d2ea4f7ac9dbd65342180425d /core/java/android/content/Context.java | |
| parent | 1b8c7de05d71f0e68e06a6227b873f14cd615fa6 (diff) | |
Limit number of window context without any window
This change is to prevent misuse of window context from app
and leads to performance drop on system by limit the numer of window
context an app can use. Code snippet below is a sample to cause
this issue:
```
Rect getBounds() {
Context windowContext = context.createWindowContext(...);
return windowContext.getSystemService(WindowManager.class)
.getCuttentWindowMetrics().getBounds()
}
```
This method could be invoked dozens of times and produce dozens of window
tokens. It would slow down the speed of window traversalling. These
token won't be removed until system server has been GC'd.
Test: atest WindowContextTests WindowContextPolicyTests
fixes: 152934797
Bug: 153369119
Change-Id: I927e85a45c05c4d90b51a624ea408ff3a3ffce93
Diffstat (limited to 'core/java/android/content/Context.java')
| -rw-r--r-- | core/java/android/content/Context.java | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index 7c1b62fc9b8e..09c684971549 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -5812,6 +5812,12 @@ public abstract class Context { * display.</b> If there is a need to add different window types, or non-associated windows, * separate Contexts should be used. * </p> + * <p> + * Creating a window context is an expensive operation. Misuse of this API may lead to a huge + * performance drop. The best practice is to use the same window context when possible. + * An approach is to create one window context with specific window type and display and + * use it everywhere it's needed.. + * </p> * * @param type Window type in {@link WindowManager.LayoutParams} * @param options Bundle used to pass window-related options. @@ -5824,7 +5830,9 @@ public abstract class Context { * @see #WINDOW_SERVICE * @see #LAYOUT_INFLATER_SERVICE * @see #WALLPAPER_SERVICE - * @throws IllegalArgumentException if token is invalid + * @throws UnsupportedOperationException if this {@link Context} does not attach to a display or + * the current number of window contexts without adding any view by + * {@link WindowManager#addView} <b>exceeds five</b>. */ public @NonNull Context createWindowContext(@WindowType int type, @Nullable Bundle options) { throw new RuntimeException("Not implemented. Must override in a subclass."); |
