diff --git a/apex/appsearch/framework/java/android/app/appsearch/AppSearchManager.java b/apex/appsearch/framework/java/android/app/appsearch/AppSearchManager.java index da446bf1e7c6e..a62bb504debc4 100644 --- a/apex/appsearch/framework/java/android/app/appsearch/AppSearchManager.java +++ b/apex/appsearch/framework/java/android/app/appsearch/AppSearchManager.java @@ -19,19 +19,10 @@ import android.annotation.CallbackExecutor; import android.annotation.NonNull; import android.annotation.SystemService; import android.content.Context; -import android.os.Bundle; -import android.os.ParcelableException; -import android.os.RemoteException; -import com.android.internal.infra.AndroidFuture; import com.android.internal.util.Preconditions; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; import java.util.Objects; -import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.function.Consumer; @@ -246,349 +237,9 @@ public class AppSearchManager { mService, mContext.getUserId(), getPackageName(), executor, callback); } - /** - * Sets the schema being used by documents provided to the {@link #putDocuments} method. - * - *

The schema provided here is compared to the stored copy of the schema previously supplied - * to {@link #setSchema}, if any, to determine how to treat existing documents. The following - * types of schema modifications are always safe and are made without deleting any existing - * documents: - * - *

- * - *

The following types of schema changes are not backwards-compatible: - * - *

- * - *

Supplying a schema with such changes will result in this call returning an {@link - * AppSearchResult} with a code of {@link AppSearchResult#RESULT_INVALID_SCHEMA} and an error - * message describing the incompatibility. In this case the previously set schema will remain - * active. - * - *

If you need to make non-backwards-compatible changes as described above, instead use the - * {@link #setSchema(List, boolean)} method with the {@code forceOverride} parameter set to - * {@code true}. - * - *

It is a no-op to set the same schema as has been previously set; this is handled - * efficiently. - * - * @param request The schema update request. - * @return the result of performing this operation. - * @hide - * @deprecated use {@link AppSearchSession#setSchema} instead. - */ - @NonNull - public AppSearchResult setSchema(@NonNull SetSchemaRequest request) { - Preconditions.checkNotNull(request); - // TODO: This should use com.android.internal.infra.RemoteStream or another mechanism to - // avoid binder limits. - List schemaBundles = new ArrayList<>(request.getSchemas().size()); - for (AppSearchSchema schema : request.getSchemas()) { - schemaBundles.add(schema.getBundle()); - } - AndroidFuture future = new AndroidFuture<>(); - try { - mService.setSchema( - getPackageName(), - DEFAULT_DATABASE_NAME, - schemaBundles, - new ArrayList<>(request.getSchemasNotDisplayedBySystem()), - /*schemasPackageAccessible=*/ Collections.emptyMap(), - request.isForceOverride(), - mContext.getUserId(), - new IAppSearchResultCallback.Stub() { - public void onResult(AppSearchResult result) { - future.complete(result); - } - }); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } - return getFutureOrThrow(future); - } - - /** - * Index {@link GenericDocument}s into AppSearch. - * - *

You should not call this method directly; instead, use the {@code - * AppSearch#putDocuments()} API provided by JetPack. - * - *

Each {@link GenericDocument}'s {@code schemaType} field must be set to the name of a - * schema type previously registered via the {@link #setSchema} method. - * - * @param request {@link PutDocumentsRequest} containing documents to be indexed - * @return The pending result of performing this operation. The keys of the returned {@link - * AppSearchBatchResult} are the URIs of the input documents. The values are {@code null} if - * they were successfully indexed, or a failed {@link AppSearchResult} otherwise. - * @throws RuntimeException If an error occurred during the execution. - * @hide - * @deprecated use {@link AppSearchSession#put} instead. - */ - public AppSearchBatchResult putDocuments(@NonNull PutDocumentsRequest request) { - // TODO(b/146386470): Transmit these documents as a RemoteStream instead of sending them in - // one big list. - List documents = request.getGenericDocuments(); - List documentBundles = new ArrayList<>(documents.size()); - for (int i = 0; i < documents.size(); i++) { - documentBundles.add(documents.get(i).getBundle()); - } - AndroidFuture future = new AndroidFuture<>(); - try { - mService.putDocuments( - getPackageName(), - DEFAULT_DATABASE_NAME, - documentBundles, - mContext.getUserId(), - new IAppSearchBatchResultCallback.Stub() { - public void onResult(AppSearchBatchResult result) { - future.complete(result); - } - - public void onSystemError(ParcelableException exception) { - future.completeExceptionally(exception); - } - }); - } catch (RemoteException e) { - future.completeExceptionally(e); - } - return getFutureOrThrow(future); - } - - /** - * Retrieves {@link GenericDocument}s by URI. - * - *

You should not call this method directly; instead, use the {@code - * AppSearch#getDocuments()} API provided by JetPack. - * - * @param request {@link GetByUriRequest} containing URIs to be retrieved. - * @return The pending result of performing this operation. The keys of the returned {@link - * AppSearchBatchResult} are the input URIs. The values are the returned {@link - * GenericDocument}s on success, or a failed {@link AppSearchResult} otherwise. URIs that - * are not found will return a failed {@link AppSearchResult} with a result code of {@link - * AppSearchResult#RESULT_NOT_FOUND}. - * @throws RuntimeException If an error occurred during the execution. - * @hide - * @deprecated use {@link AppSearchSession#getByUri} instead. - */ - public AppSearchBatchResult getByUri( - @NonNull GetByUriRequest request) { - // TODO(b/146386470): Transmit the result documents as a RemoteStream instead of sending - // them in one big list. - List uris = new ArrayList<>(request.getUris()); - AndroidFuture future = new AndroidFuture<>(); - try { - mService.getDocuments( - getPackageName(), - DEFAULT_DATABASE_NAME, - request.getNamespace(), - uris, - request.getProjectionsInternal(), - mContext.getUserId(), - new IAppSearchBatchResultCallback.Stub() { - public void onResult(AppSearchBatchResult result) { - future.complete(result); - } - - public void onSystemError(ParcelableException exception) { - future.completeExceptionally(exception); - } - }); - } catch (RemoteException e) { - future.completeExceptionally(e); - } - - // Translate from document bundles to GenericDocument instances - AppSearchBatchResult bundleResult = getFutureOrThrow(future); - AppSearchBatchResult.Builder documentResultBuilder = - new AppSearchBatchResult.Builder<>(); - - // Translate successful results - for (Map.Entry bundleEntry : bundleResult.getSuccesses().entrySet()) { - GenericDocument document; - try { - document = new GenericDocument(bundleEntry.getValue()); - } catch (Throwable t) { - // These documents went through validation, so how could this fail? We must have - // done something wrong. - documentResultBuilder.setFailure( - bundleEntry.getKey(), - AppSearchResult.RESULT_INTERNAL_ERROR, - t.getMessage()); - continue; - } - documentResultBuilder.setSuccess(bundleEntry.getKey(), document); - } - - // Translate failed results - for (Map.Entry> bundleEntry : - bundleResult.getFailures().entrySet()) { - documentResultBuilder.setFailure( - bundleEntry.getKey(), - bundleEntry.getValue().getResultCode(), - bundleEntry.getValue().getErrorMessage()); - } - - return documentResultBuilder.build(); - } - - /** - * Searches a document based on a given query string. - * - *

You should not call this method directly; instead, use the {@code AppSearch#query()} API - * provided by JetPack. - * - *

Currently we support following features in the raw query format: - * - *

- * - * @param queryExpression Query String to search. - * @param searchSpec Spec for setting filters, raw query etc. - * @throws RuntimeException If an error occurred during the execution. - * @hide - * @deprecated use AppSearchSession#query instead. - */ - @NonNull - public AppSearchResult> query( - @NonNull String queryExpression, @NonNull SearchSpec searchSpec) { - // TODO(b/146386470): Transmit the result documents as a RemoteStream instead of sending - // them in one big list. - AndroidFuture future = new AndroidFuture<>(); - try { - mService.query( - getPackageName(), - DEFAULT_DATABASE_NAME, - queryExpression, - searchSpec.getBundle(), - mContext.getUserId(), - new IAppSearchResultCallback.Stub() { - public void onResult(AppSearchResult result) { - future.complete(result); - } - }); - AppSearchResult bundleResult = getFutureOrThrow(future); - if (!bundleResult.isSuccess()) { - return AppSearchResult.newFailedResult( - bundleResult.getResultCode(), bundleResult.getErrorMessage()); - } - SearchResultPage searchResultPage = new SearchResultPage(bundleResult.getResultValue()); - return AppSearchResult.newSuccessfulResult(searchResultPage.getResults()); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } catch (Throwable t) { - return AppSearchResult.throwableToFailedResult(t); - } - } - - /** - * Removes {@link GenericDocument}s by URI. - * - *

You should not call this method directly; instead, use the {@code AppSearch#delete()} API - * provided by JetPack. - * - * @param request Request containing URIs to be removed. - * @return The pending result of performing this operation. The keys of the returned {@link - * AppSearchBatchResult} are the input URIs. The values are {@code null} on success, or a - * failed {@link AppSearchResult} otherwise. URIs that are not found will return a failed - * {@link AppSearchResult} with a result code of {@link AppSearchResult#RESULT_NOT_FOUND}. - * @throws RuntimeException If an error occurred during the execution. - * @hide - * @deprecated use {@link AppSearchSession#remove} instead. - */ - public AppSearchBatchResult removeByUri(@NonNull RemoveByUriRequest request) { - List uris = new ArrayList<>(request.getUris()); - AndroidFuture future = new AndroidFuture<>(); - try { - mService.removeByUri( - getPackageName(), - DEFAULT_DATABASE_NAME, - request.getNamespace(), - uris, - mContext.getUserId(), - new IAppSearchBatchResultCallback.Stub() { - public void onResult(AppSearchBatchResult result) { - future.complete(result); - } - - public void onSystemError(ParcelableException exception) { - future.completeExceptionally(exception); - } - }); - } catch (RemoteException e) { - future.completeExceptionally(e); - } - return getFutureOrThrow(future); - } - /** Returns the package name that should be used for uid verification. */ @NonNull private String getPackageName() { return mContext.getOpPackageName(); } - - private static T getFutureOrThrow(@NonNull AndroidFuture future) { - try { - return future.get(); - } catch (Throwable e) { - if (e instanceof ExecutionException) { - e = e.getCause(); - } - if (e instanceof RuntimeException) { - throw (RuntimeException) e; - } - if (e instanceof Error) { - throw (Error) e; - } - throw new RuntimeException(e); - } - } }