summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Utkarsh Nigam <utkarshnigam@google.com> 2024-09-03 16:44:01 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2024-09-03 16:44:01 +0000
commit1b6dd331d61be731a514b95d03aaba8cbc1e66d4 (patch)
tree9e2ef545e9a2c1e379259a8953bf1e2317f0f8ab
parent2e618a14355b66d208fbe5392cc2a8ae179d52ca (diff)
parent07851bd3a4e97db0ee82ca524b4b7ea741442f64 (diff)
Merge "Make SyncAppSearchCallHelper asynchronous." into main
-rw-r--r--services/appfunctions/java/com/android/server/appfunctions/SyncAppSearchCallHelper.java116
1 files changed, 66 insertions, 50 deletions
diff --git a/services/appfunctions/java/com/android/server/appfunctions/SyncAppSearchCallHelper.java b/services/appfunctions/java/com/android/server/appfunctions/SyncAppSearchCallHelper.java
index c01fe311e9ca..5dd4c250af58 100644
--- a/services/appfunctions/java/com/android/server/appfunctions/SyncAppSearchCallHelper.java
+++ b/services/appfunctions/java/com/android/server/appfunctions/SyncAppSearchCallHelper.java
@@ -38,7 +38,9 @@ import java.util.Objects;
import java.util.concurrent.Executor;
/**
- * Helper class for interacting with a system server local appsearch session synchronously.
+ * Helper class for interacting with a system server local appsearch session asynchronously.
+ *
+ * <p>Converts the AppSearch Callback API to {@link AndroidFuture}.
*/
@FlaggedApi(FLAG_ENABLE_APP_FUNCTION_MANAGER)
public class SyncAppSearchCallHelper implements Closeable {
@@ -47,9 +49,10 @@ public class SyncAppSearchCallHelper implements Closeable {
private final AppSearchManager mAppSearchManager;
private final AndroidFuture<AppSearchResult<AppSearchSession>> mSettableSessionFuture;
- public SyncAppSearchCallHelper(@NonNull AppSearchManager appSearchManager,
- @NonNull Executor executor,
- @NonNull SearchContext appSearchContext) {
+ public SyncAppSearchCallHelper(
+ @NonNull AppSearchManager appSearchManager,
+ @NonNull Executor executor,
+ @NonNull SearchContext appSearchContext) {
Objects.requireNonNull(appSearchManager);
Objects.requireNonNull(executor);
Objects.requireNonNull(appSearchContext);
@@ -61,68 +64,81 @@ public class SyncAppSearchCallHelper implements Closeable {
appSearchContext, mExecutor, mSettableSessionFuture::complete);
}
- /**
- * Converts a failed app search result codes into an exception.
- */
+ /** Converts a failed app search result codes into an exception. */
@NonNull
private static Exception failedResultToException(@NonNull AppSearchResult appSearchResult) {
return switch (appSearchResult.getResultCode()) {
- case AppSearchResult.RESULT_INVALID_ARGUMENT -> new IllegalArgumentException(
- appSearchResult.getErrorMessage());
- case AppSearchResult.RESULT_IO_ERROR -> new IOException(
- appSearchResult.getErrorMessage());
- case AppSearchResult.RESULT_SECURITY_ERROR -> new SecurityException(
- appSearchResult.getErrorMessage());
+ case AppSearchResult.RESULT_INVALID_ARGUMENT ->
+ new IllegalArgumentException(appSearchResult.getErrorMessage());
+ case AppSearchResult.RESULT_IO_ERROR ->
+ new IOException(appSearchResult.getErrorMessage());
+ case AppSearchResult.RESULT_SECURITY_ERROR ->
+ new SecurityException(appSearchResult.getErrorMessage());
default -> new IllegalStateException(appSearchResult.getErrorMessage());
};
}
- private AppSearchSession getSession() throws Exception {
- AppSearchResult<AppSearchSession> sessionResult = mSettableSessionFuture.get();
- if (!sessionResult.isSuccess()) {
- throw failedResultToException(sessionResult);
- }
- return sessionResult.getResultValue();
+ private AndroidFuture<AppSearchSession> getSessionAsync() {
+ return mSettableSessionFuture.thenApply(
+ result -> {
+ if (result.isSuccess()) {
+ return result.getResultValue();
+ } else {
+ throw new RuntimeException(failedResultToException(result));
+ }
+ });
}
- /**
- * Gets the schema for a given app search session.
- */
- @WorkerThread
- public GetSchemaResponse getSchema() throws Exception {
- AndroidFuture<AppSearchResult<GetSchemaResponse>> settableSchemaResponse =
- new AndroidFuture<>();
- getSession().getSchema(mExecutor, settableSchemaResponse::complete);
- AppSearchResult<GetSchemaResponse> schemaResponse = settableSchemaResponse.get();
- if (schemaResponse.isSuccess()) {
- return schemaResponse.getResultValue();
- } else {
- throw failedResultToException(schemaResponse);
- }
+ /** Gets the schema for a given app search session. */
+ public AndroidFuture<GetSchemaResponse> getSchema() {
+ return getSessionAsync()
+ .thenComposeAsync(
+ session -> {
+ AndroidFuture<AppSearchResult<GetSchemaResponse>>
+ settableSchemaResponse = new AndroidFuture<>();
+ session.getSchema(mExecutor, settableSchemaResponse::complete);
+ return settableSchemaResponse.thenApply(
+ result -> {
+ if (result.isSuccess()) {
+ return result.getResultValue();
+ } else {
+ throw new RuntimeException(
+ failedResultToException(result));
+ }
+ });
+ },
+ mExecutor);
}
- /**
- * Sets the schema for a given app search session.
- */
- @WorkerThread
- public SetSchemaResponse setSchema(
- @NonNull SetSchemaRequest setSchemaRequest) throws Exception {
- AndroidFuture<AppSearchResult<SetSchemaResponse>> settableSchemaResponse =
- new AndroidFuture<>();
- getSession().setSchema(
- setSchemaRequest, mExecutor, mExecutor, settableSchemaResponse::complete);
- AppSearchResult<SetSchemaResponse> schemaResponse = settableSchemaResponse.get();
- if (schemaResponse.isSuccess()) {
- return schemaResponse.getResultValue();
- } else {
- throw failedResultToException(schemaResponse);
- }
+ /** Sets the schema for a given app search session. */
+ public AndroidFuture<SetSchemaResponse> setSchema(@NonNull SetSchemaRequest setSchemaRequest) {
+ return getSessionAsync()
+ .thenComposeAsync(
+ session -> {
+ AndroidFuture<AppSearchResult<SetSchemaResponse>>
+ settableSchemaResponse = new AndroidFuture<>();
+ session.setSchema(
+ setSchemaRequest,
+ mExecutor,
+ mExecutor,
+ settableSchemaResponse::complete);
+ return settableSchemaResponse.thenApply(
+ result -> {
+ if (result.isSuccess()) {
+ return result.getResultValue();
+ } else {
+ throw new RuntimeException(
+ failedResultToException(result));
+ }
+ });
+ },
+ mExecutor);
}
@Override
public void close() throws IOException {
try {
- getSession().close();
+ getSessionAsync().get().close();
} catch (Exception ex) {
Slog.e(TAG, "Failed to close app search session", ex);
}