From fa5a61c8db11ca98151c2b3dd93713d84370da63 Mon Sep 17 00:00:00 2001 From: Vinit Nayak Date: Mon, 15 May 2023 12:53:47 -0700 Subject: [PATCH] Don't throw exception for SafeCloseable objects * SafeCloseable objects will have their close() method called when SandboxContext is destroyed, so we can leave them out of the allowList. * Long term we may want to remove the allowList altogether and just rely on safeCloseable Test: Locally passes Flag: not needed Bug: 280463854 Change-Id: I317a9d6639b25a522fe9dbcf2655b55ec68fded6 --- .../launcher3/util/MainThreadInitializedObject.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/com/android/launcher3/util/MainThreadInitializedObject.java b/src/com/android/launcher3/util/MainThreadInitializedObject.java index badcd35c23..6a4e528368 100644 --- a/src/com/android/launcher3/util/MainThreadInitializedObject.java +++ b/src/com/android/launcher3/util/MainThreadInitializedObject.java @@ -136,16 +136,19 @@ public class MainThreadInitializedObject { if (mDestroyed) { Log.e(TAG, "Static object access with a destroyed context"); } - if (!mAllowedObjects.contains(object)) { - throw new IllegalStateException( - "Leaking unknown objects " + object + " " + provider); - } + T t = (T) mObjectMap.get(object); if (t != null) { return t; } if (Looper.myLooper() == Looper.getMainLooper()) { t = createObject(provider); + // Check if we've explicitly allowed the object or if it's a SafeCloseable, + // it will get destroyed in onDestroy() + if (!mAllowedObjects.contains(object) && !(t instanceof SafeCloseable)) { + throw new IllegalStateException( + "Leaking unknown objects " + object + " " + provider + " " + t); + } mObjectMap.put(object, t); mOrderedObjects.add(t); return t;