From 1d03233fbd587b33b92c1017f5682db1a7887692 Mon Sep 17 00:00:00 2001 From: Andy CrossGate Yan Date: Sat, 12 Aug 2023 20:11:17 +0800 Subject: [PATCH 06/10] Add runWithCleanCallingIdentity variant with both executor and return value This complements the fixup to ImsPhoneCallTracker (in fw/o/t) for U Change-Id: If444290787025e130dce4bdeaf92372ae32793fe --- .../telephony/util/TelephonyUtils.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/telephony/common/com/android/internal/telephony/util/TelephonyUtils.java b/telephony/common/com/android/internal/telephony/util/TelephonyUtils.java index aed8fb8c4503..1467e7f2fc29 100644 --- a/telephony/common/com/android/internal/telephony/util/TelephonyUtils.java +++ b/telephony/common/com/android/internal/telephony/util/TelephonyUtils.java @@ -42,7 +42,9 @@ import com.android.internal.telephony.ITelephony; import java.io.PrintWriter; import java.util.Collections; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; @@ -151,6 +153,34 @@ public final class TelephonyUtils { } } + /** + * Convenience method for running the provided action in the provided + * executor enclosed in + * {@link Binder#clearCallingIdentity}/{@link Binder#restoreCallingIdentity} and return + * the result. + * + * Any exception thrown by the given action will need to be handled by caller. + * + */ + public static T runWithCleanCallingIdentity( + @NonNull Supplier action, @NonNull Executor executor) { + if (action != null) { + if (executor != null) { + try { + return CompletableFuture.supplyAsync( + () -> runWithCleanCallingIdentity(action), executor).get(); + } catch (ExecutionException | InterruptedException e) { + Log.w(LOG_TAG, "TelephonyUtils : runWithCleanCallingIdentity exception: " + + e.getMessage()); + return null; + } + } else { + return runWithCleanCallingIdentity(action); + } + } + return null; + } + /** * Filter values in bundle to only basic types. */ -- 2.43.1