diff options
| author | 2024-03-27 09:36:52 +0000 | |
|---|---|---|
| committer | 2024-03-27 09:36:52 +0000 | |
| commit | 13b1479b7be878ac2c326f680c08a6d871336499 (patch) | |
| tree | 45a2488efc7c81dbd432fc94cc8ba949e9a631bd | |
| parent | 3a17b4d7f2318aa80f9efec2535605065722b859 (diff) | |
| parent | 4ac594892213e7595850fe3e98634d48cd6fa2af (diff) | |
Merge "Fix ContextImpl leak in ActivityThread." into main
| -rw-r--r-- | core/java/android/app/ActivityThread.java | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java index 4bf887962dfa..8913d6d15dc0 100644 --- a/core/java/android/app/ActivityThread.java +++ b/core/java/android/app/ActivityThread.java @@ -365,7 +365,7 @@ public final class ActivityThread extends ClientTransactionHandler @UnsupportedAppUsage private ContextImpl mSystemContext; @GuardedBy("this") - private SparseArray<ContextImpl> mDisplaySystemUiContexts; + private ArrayList<WeakReference<ContextImpl>> mDisplaySystemUiContexts; @UnsupportedAppUsage static volatile IPackageManager sPackageManager; @@ -3033,14 +3033,19 @@ public final class ActivityThread extends ClientTransactionHandler public ContextImpl getSystemUiContext(int displayId) { synchronized (this) { if (mDisplaySystemUiContexts == null) { - mDisplaySystemUiContexts = new SparseArray<>(); + mDisplaySystemUiContexts = new ArrayList<>(); } - ContextImpl systemUiContext = mDisplaySystemUiContexts.get(displayId); - if (systemUiContext == null) { - systemUiContext = ContextImpl.createSystemUiContext(getSystemContext(), displayId); - mDisplaySystemUiContexts.put(displayId, systemUiContext); + + mDisplaySystemUiContexts.removeIf(contextRef -> contextRef.refersTo(null)); + + ContextImpl context = getSystemUiContextNoCreateLocked(displayId); + if (context != null) { + return context; } - return systemUiContext; + + context = ContextImpl.createSystemUiContext(getSystemContext(), displayId); + mDisplaySystemUiContexts.add(new WeakReference<>(context)); + return context; } } @@ -3048,18 +3053,30 @@ public final class ActivityThread extends ClientTransactionHandler @Override public ContextImpl getSystemUiContextNoCreate() { synchronized (this) { - if (mDisplaySystemUiContexts == null) return null; - return mDisplaySystemUiContexts.get(DEFAULT_DISPLAY); + if (mDisplaySystemUiContexts == null) { + return null; + } + return getSystemUiContextNoCreateLocked(DEFAULT_DISPLAY); + } + } + + @GuardedBy("this") + @Nullable + private ContextImpl getSystemUiContextNoCreateLocked(int displayId) { + for (int i = 0; i < mDisplaySystemUiContexts.size(); i++) { + ContextImpl context = mDisplaySystemUiContexts.get(i).get(); + if (context != null && context.getDisplayId() == displayId) { + return context; + } } + return null; } void onSystemUiContextCleanup(ContextImpl context) { synchronized (this) { if (mDisplaySystemUiContexts == null) return; - final int index = mDisplaySystemUiContexts.indexOfValue(context); - if (index >= 0) { - mDisplaySystemUiContexts.removeAt(index); - } + mDisplaySystemUiContexts.removeIf( + contextRef -> contextRef.refersTo(null) || contextRef.refersTo(context)); } } |