Add withCleanCallingIdentity with Supplier to module utils

Binder#withCleanCallingIdentity with Supplier is a very useful
utility but it's not public.  Thus, make one in the connectivity
module utils.

Bug: 255231779
Test: m ; adb shell dumpsys connectivity
Change-Id: I029a34a96b1107111a605b91a54c8fdb071e9fc0
diff --git a/staticlibs/framework/com/android/net/module/util/BinderUtils.java b/staticlibs/framework/com/android/net/module/util/BinderUtils.java
index eb695d1..e4d14ea 100644
--- a/staticlibs/framework/com/android/net/module/util/BinderUtils.java
+++ b/staticlibs/framework/com/android/net/module/util/BinderUtils.java
@@ -19,6 +19,8 @@
 import android.annotation.NonNull;
 import android.os.Binder;
 
+import java.util.function.Supplier;
+
 /**
  * Collection of utilities for {@link Binder} and related classes.
  * @hide
@@ -56,4 +58,39 @@
         /** @see java.lang.Runnable */
         void run() throws T;
     }
+
+    /**
+     * Convenience method for running the provided action enclosed in
+     * {@link Binder#clearCallingIdentity}/{@link Binder#restoreCallingIdentity} returning the
+     * result.
+     *
+     * <p>Any exception thrown by the given action will be caught and rethrown after
+     * the call to {@link Binder#restoreCallingIdentity}.
+     *
+     * Note that this is copied from Binder#withCleanCallingIdentity with minor changes
+     * since it is not public.
+     *
+     * @hide
+     */
+    public static final <T, E extends Exception> T withCleanCallingIdentity(
+            @NonNull ThrowingSupplier<T, E> action) throws E {
+        final long callingIdentity = Binder.clearCallingIdentity();
+        try {
+            return action.get();
+        } finally {
+            Binder.restoreCallingIdentity(callingIdentity);
+        }
+    }
+
+    /**
+     * An equivalent of {@link Supplier}
+     *
+     * @param <T> The class which is declared to be returned.
+     * @param <E> The exception class which is declared to be thrown.
+     */
+    @FunctionalInterface
+    public interface ThrowingSupplier<T, E extends Exception> {
+        /** @see java.util.function.Supplier */
+        T get() throws E;
+    }
 }