Loading services/appfunctions/java/com/android/server/appfunctions/SyncAppSearchCallHelper.java +66 −50 Original line number Original line Diff line number Diff line Loading @@ -38,7 +38,9 @@ import java.util.Objects; import java.util.concurrent.Executor; 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) @FlaggedApi(FLAG_ENABLE_APP_FUNCTION_MANAGER) public class SyncAppSearchCallHelper implements Closeable { public class SyncAppSearchCallHelper implements Closeable { Loading @@ -47,7 +49,8 @@ public class SyncAppSearchCallHelper implements Closeable { private final AppSearchManager mAppSearchManager; private final AppSearchManager mAppSearchManager; private final AndroidFuture<AppSearchResult<AppSearchSession>> mSettableSessionFuture; private final AndroidFuture<AppSearchResult<AppSearchSession>> mSettableSessionFuture; public SyncAppSearchCallHelper(@NonNull AppSearchManager appSearchManager, public SyncAppSearchCallHelper( @NonNull AppSearchManager appSearchManager, @NonNull Executor executor, @NonNull Executor executor, @NonNull SearchContext appSearchContext) { @NonNull SearchContext appSearchContext) { Objects.requireNonNull(appSearchManager); Objects.requireNonNull(appSearchManager); Loading @@ -61,68 +64,81 @@ public class SyncAppSearchCallHelper implements Closeable { appSearchContext, mExecutor, mSettableSessionFuture::complete); 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 @NonNull private static Exception failedResultToException(@NonNull AppSearchResult appSearchResult) { private static Exception failedResultToException(@NonNull AppSearchResult appSearchResult) { return switch (appSearchResult.getResultCode()) { return switch (appSearchResult.getResultCode()) { case AppSearchResult.RESULT_INVALID_ARGUMENT -> new IllegalArgumentException( case AppSearchResult.RESULT_INVALID_ARGUMENT -> appSearchResult.getErrorMessage()); new IllegalArgumentException(appSearchResult.getErrorMessage()); case AppSearchResult.RESULT_IO_ERROR -> new IOException( case AppSearchResult.RESULT_IO_ERROR -> appSearchResult.getErrorMessage()); new IOException(appSearchResult.getErrorMessage()); case AppSearchResult.RESULT_SECURITY_ERROR -> new SecurityException( case AppSearchResult.RESULT_SECURITY_ERROR -> appSearchResult.getErrorMessage()); new SecurityException(appSearchResult.getErrorMessage()); default -> new IllegalStateException(appSearchResult.getErrorMessage()); default -> new IllegalStateException(appSearchResult.getErrorMessage()); }; }; } } private AppSearchSession getSession() throws Exception { private AndroidFuture<AppSearchSession> getSessionAsync() { AppSearchResult<AppSearchSession> sessionResult = mSettableSessionFuture.get(); return mSettableSessionFuture.thenApply( if (!sessionResult.isSuccess()) { result -> { throw failedResultToException(sessionResult); if (result.isSuccess()) { return result.getResultValue(); } else { throw new RuntimeException(failedResultToException(result)); } } return sessionResult.getResultValue(); }); } } /** /** Gets the schema for a given app search session. */ * Gets the schema for a given app search session. public AndroidFuture<GetSchemaResponse> getSchema() { */ return getSessionAsync() @WorkerThread .thenComposeAsync( public GetSchemaResponse getSchema() throws Exception { session -> { AndroidFuture<AppSearchResult<GetSchemaResponse>> settableSchemaResponse = AndroidFuture<AppSearchResult<GetSchemaResponse>> new AndroidFuture<>(); settableSchemaResponse = new AndroidFuture<>(); getSession().getSchema(mExecutor, settableSchemaResponse::complete); session.getSchema(mExecutor, settableSchemaResponse::complete); AppSearchResult<GetSchemaResponse> schemaResponse = settableSchemaResponse.get(); return settableSchemaResponse.thenApply( if (schemaResponse.isSuccess()) { result -> { return schemaResponse.getResultValue(); if (result.isSuccess()) { return result.getResultValue(); } else { } else { throw failedResultToException(schemaResponse); throw new RuntimeException( failedResultToException(result)); } } }); }, mExecutor); } } /** /** Sets the schema for a given app search session. */ * Sets the schema for a given app search session. public AndroidFuture<SetSchemaResponse> setSchema(@NonNull SetSchemaRequest setSchemaRequest) { */ return getSessionAsync() @WorkerThread .thenComposeAsync( public SetSchemaResponse setSchema( session -> { @NonNull SetSchemaRequest setSchemaRequest) throws Exception { AndroidFuture<AppSearchResult<SetSchemaResponse>> AndroidFuture<AppSearchResult<SetSchemaResponse>> settableSchemaResponse = settableSchemaResponse = new AndroidFuture<>(); new AndroidFuture<>(); session.setSchema( getSession().setSchema( setSchemaRequest, setSchemaRequest, mExecutor, mExecutor, settableSchemaResponse::complete); mExecutor, AppSearchResult<SetSchemaResponse> schemaResponse = settableSchemaResponse.get(); mExecutor, if (schemaResponse.isSuccess()) { settableSchemaResponse::complete); return schemaResponse.getResultValue(); return settableSchemaResponse.thenApply( result -> { if (result.isSuccess()) { return result.getResultValue(); } else { } else { throw failedResultToException(schemaResponse); throw new RuntimeException( failedResultToException(result)); } } }); }, mExecutor); } } @Override @Override public void close() throws IOException { public void close() throws IOException { try { try { getSession().close(); getSessionAsync().get().close(); } catch (Exception ex) { } catch (Exception ex) { Slog.e(TAG, "Failed to close app search session", ex); Slog.e(TAG, "Failed to close app search session", ex); } } Loading Loading
services/appfunctions/java/com/android/server/appfunctions/SyncAppSearchCallHelper.java +66 −50 Original line number Original line Diff line number Diff line Loading @@ -38,7 +38,9 @@ import java.util.Objects; import java.util.concurrent.Executor; 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) @FlaggedApi(FLAG_ENABLE_APP_FUNCTION_MANAGER) public class SyncAppSearchCallHelper implements Closeable { public class SyncAppSearchCallHelper implements Closeable { Loading @@ -47,7 +49,8 @@ public class SyncAppSearchCallHelper implements Closeable { private final AppSearchManager mAppSearchManager; private final AppSearchManager mAppSearchManager; private final AndroidFuture<AppSearchResult<AppSearchSession>> mSettableSessionFuture; private final AndroidFuture<AppSearchResult<AppSearchSession>> mSettableSessionFuture; public SyncAppSearchCallHelper(@NonNull AppSearchManager appSearchManager, public SyncAppSearchCallHelper( @NonNull AppSearchManager appSearchManager, @NonNull Executor executor, @NonNull Executor executor, @NonNull SearchContext appSearchContext) { @NonNull SearchContext appSearchContext) { Objects.requireNonNull(appSearchManager); Objects.requireNonNull(appSearchManager); Loading @@ -61,68 +64,81 @@ public class SyncAppSearchCallHelper implements Closeable { appSearchContext, mExecutor, mSettableSessionFuture::complete); 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 @NonNull private static Exception failedResultToException(@NonNull AppSearchResult appSearchResult) { private static Exception failedResultToException(@NonNull AppSearchResult appSearchResult) { return switch (appSearchResult.getResultCode()) { return switch (appSearchResult.getResultCode()) { case AppSearchResult.RESULT_INVALID_ARGUMENT -> new IllegalArgumentException( case AppSearchResult.RESULT_INVALID_ARGUMENT -> appSearchResult.getErrorMessage()); new IllegalArgumentException(appSearchResult.getErrorMessage()); case AppSearchResult.RESULT_IO_ERROR -> new IOException( case AppSearchResult.RESULT_IO_ERROR -> appSearchResult.getErrorMessage()); new IOException(appSearchResult.getErrorMessage()); case AppSearchResult.RESULT_SECURITY_ERROR -> new SecurityException( case AppSearchResult.RESULT_SECURITY_ERROR -> appSearchResult.getErrorMessage()); new SecurityException(appSearchResult.getErrorMessage()); default -> new IllegalStateException(appSearchResult.getErrorMessage()); default -> new IllegalStateException(appSearchResult.getErrorMessage()); }; }; } } private AppSearchSession getSession() throws Exception { private AndroidFuture<AppSearchSession> getSessionAsync() { AppSearchResult<AppSearchSession> sessionResult = mSettableSessionFuture.get(); return mSettableSessionFuture.thenApply( if (!sessionResult.isSuccess()) { result -> { throw failedResultToException(sessionResult); if (result.isSuccess()) { return result.getResultValue(); } else { throw new RuntimeException(failedResultToException(result)); } } return sessionResult.getResultValue(); }); } } /** /** Gets the schema for a given app search session. */ * Gets the schema for a given app search session. public AndroidFuture<GetSchemaResponse> getSchema() { */ return getSessionAsync() @WorkerThread .thenComposeAsync( public GetSchemaResponse getSchema() throws Exception { session -> { AndroidFuture<AppSearchResult<GetSchemaResponse>> settableSchemaResponse = AndroidFuture<AppSearchResult<GetSchemaResponse>> new AndroidFuture<>(); settableSchemaResponse = new AndroidFuture<>(); getSession().getSchema(mExecutor, settableSchemaResponse::complete); session.getSchema(mExecutor, settableSchemaResponse::complete); AppSearchResult<GetSchemaResponse> schemaResponse = settableSchemaResponse.get(); return settableSchemaResponse.thenApply( if (schemaResponse.isSuccess()) { result -> { return schemaResponse.getResultValue(); if (result.isSuccess()) { return result.getResultValue(); } else { } else { throw failedResultToException(schemaResponse); throw new RuntimeException( failedResultToException(result)); } } }); }, mExecutor); } } /** /** Sets the schema for a given app search session. */ * Sets the schema for a given app search session. public AndroidFuture<SetSchemaResponse> setSchema(@NonNull SetSchemaRequest setSchemaRequest) { */ return getSessionAsync() @WorkerThread .thenComposeAsync( public SetSchemaResponse setSchema( session -> { @NonNull SetSchemaRequest setSchemaRequest) throws Exception { AndroidFuture<AppSearchResult<SetSchemaResponse>> AndroidFuture<AppSearchResult<SetSchemaResponse>> settableSchemaResponse = settableSchemaResponse = new AndroidFuture<>(); new AndroidFuture<>(); session.setSchema( getSession().setSchema( setSchemaRequest, setSchemaRequest, mExecutor, mExecutor, settableSchemaResponse::complete); mExecutor, AppSearchResult<SetSchemaResponse> schemaResponse = settableSchemaResponse.get(); mExecutor, if (schemaResponse.isSuccess()) { settableSchemaResponse::complete); return schemaResponse.getResultValue(); return settableSchemaResponse.thenApply( result -> { if (result.isSuccess()) { return result.getResultValue(); } else { } else { throw failedResultToException(schemaResponse); throw new RuntimeException( failedResultToException(result)); } } }); }, mExecutor); } } @Override @Override public void close() throws IOException { public void close() throws IOException { try { try { getSession().close(); getSessionAsync().get().close(); } catch (Exception ex) { } catch (Exception ex) { Slog.e(TAG, "Failed to close app search session", ex); Slog.e(TAG, "Failed to close app search session", ex); } } Loading