Merge "Add withCleanCallingIdentity with Supplier to module utils" am: d7bcdba4a9

Original change: https://android-review.googlesource.com/c/platform/frameworks/libs/net/+/2319733

Change-Id: I93fa1be241dd0699723204d9bef8483bd5393877
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Chiachang Wang 2022-12-05 01:23:38 +00:00 • committed by Automerger Merge Worker
commit bbaa80b686

View file

@ -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.
*
* <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;
}
}