diff --git a/staticlibs/framework/com/android/net/module/util/BinderUtils.java b/staticlibs/framework/com/android/net/module/util/BinderUtils.java index eb695d1f9d..e4d14ea8d6 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 @@ package com.android.net.module.util; 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 @@ public class BinderUtils { /** @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. + * + *

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 withCleanCallingIdentity( + @NonNull ThrowingSupplier action) throws E { + final long callingIdentity = Binder.clearCallingIdentity(); + try { + return action.get(); + } finally { + Binder.restoreCallingIdentity(callingIdentity); + } + } + + /** + * An equivalent of {@link Supplier} + * + * @param The class which is declared to be returned. + * @param The exception class which is declared to be thrown. + */ + @FunctionalInterface + public interface ThrowingSupplier { + /** @see java.util.function.Supplier */ + T get() throws E; + } }