From 28cd1a43c63151391142a54fe6c3067a0f40f73b Mon Sep 17 00:00:00 2001 From: Andy CrossGate Yan Date: Sat, 12 Aug 2023 20:11:17 +0800 Subject: [PATCH 08/11] 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 | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/telephony/common/com/android/internal/telephony/util/TelephonyUtils.java b/telephony/common/com/android/internal/telephony/util/TelephonyUtils.java index 9a8c9655375d..454080144d35 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. */ @@ -319,4 +349,4 @@ public final class TelephonyUtils { return false; } -} \ No newline at end of file +} -- 2.41.0