Pull upstream changes from AndroidX to support removeByQuery.

Changes included:
#87c01a9: Replace AppSearch removeByNamespace, removeBySchemaType and
removeAll by removeByQuery.

Bug: 162450968
Test: presubmit
Change-Id: I47f8c1bef07e0ea4c9e92d21483bf4bd52d9944a
This commit is contained in:
Alexander Dorokhine
2020-10-20 17:40:49 -07:00
committed by Terry Wang
parent b71cb7a596
commit 66940ba56d
3 changed files with 44 additions and 8 deletions

View File

@@ -307,6 +307,41 @@ public final class AppSearchSession {
}
}
/**
* Removes {@link GenericDocument}s from the index by Query. Documents will be removed if they
* match the {@code queryExpression} in given namespaces and schemaTypes which is set via
* {@link SearchSpec.Builder#addNamespace} and {@link SearchSpec.Builder#addSchema}.
*
* <p> An empty {@code queryExpression} matches all documents.
*
* <p> An empty set of namespaces or schemaTypes matches all namespaces or schemaTypes in
* the current database.
*
* @param queryExpression Query String to search.
* @param searchSpec Defines what and how to remove
* @param executor Executor on which to invoke the callback.
* @param callback Callback to receive errors resulting from removing the documents. If the
* operation succeeds, the callback will be invoked with {@code null}.
*/
public void removeByQuery(@NonNull String queryExpression,
@NonNull SearchSpec searchSpec,
@NonNull @CallbackExecutor Executor executor,
@NonNull Consumer<AppSearchResult<Void>> callback) {
Objects.requireNonNull(queryExpression);
Objects.requireNonNull(searchSpec);
Objects.requireNonNull(executor);
Objects.requireNonNull(callback);
try {
mService.removeByQuery(mDatabaseName, queryExpression, searchSpec.getBundle(),
new IAppSearchResultCallback.Stub() {
public void onResult(AppSearchResult result) {
executor.execute(() -> callback.accept(result));
}
});
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
// TODO(b/162450968) port query() and SearchResults.java to platform.
// TODO(b/162450968) port removeByQuery() to platform.
}

View File

@@ -119,13 +119,14 @@ interface IAppSearchManager {
* @param databaseName The databaseName this query for.
* @param queryExpression String to search for
* @param searchSpecBundle SearchSpec bundle
* @param callback {@link AndroidFuture}&lt;{@link AppSearchResult}&lt;{@link SearchResults}&gt;&gt;
* @param callback {@link IAppSearchResultCallback#onResult} will be called with an
* {@link AppSearchResult}&lt;{@link Void}&gt;.
*/
void removeByQuery(
in String databaseName,
in String queryExpression,
in Bundle searchSpecBundle,
in AndroidFuture<AppSearchResult> callback);
in IAppSearchResultCallback callback);
/**
* Creates and initializes AppSearchImpl for the calling app.

View File

@@ -225,21 +225,21 @@ public class AppSearchManagerService extends SystemService {
@NonNull String databaseName,
@NonNull String queryExpression,
@NonNull Bundle searchSpecBundle,
@NonNull AndroidFuture<AppSearchResult> callback) {
@NonNull IAppSearchResultCallback callback) {
Preconditions.checkNotNull(databaseName);
Preconditions.checkNotNull(queryExpression);
Preconditions.checkNotNull(searchSpecBundle);
Preconditions.checkNotNull(callback);
int callingUid = Binder.getCallingUidOrThrow();
int callingUserId = UserHandle.getUserId(callingUid);
final long callingIdentity = Binder.clearCallingIdentity();
try {
AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid);
impl.removeByQuery(databaseName, queryExpression, new SearchSpec(searchSpecBundle));
callback.complete(AppSearchResult.newSuccessfulResult(/*result= */null));
impl.removeByQuery(databaseName, queryExpression,
new SearchSpec(searchSpecBundle));
invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null));
} catch (Throwable t) {
callback.complete(throwableToFailedResult(t));
invokeCallbackOnError(callback, t);
} finally {
Binder.restoreCallingIdentity(callingIdentity);
}