Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1387d38b authored by Alexander Dorokhine's avatar Alexander Dorokhine Committed by Android (Google) Code Review
Browse files

Merge "Implement the AppSearch.deleteAll API."

parents d529361c ede6a646
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -333,6 +333,17 @@ public class AppSearchManager {
        return AppSearchResult.newSuccessfulResult(new SearchResults(searchResultProto));
    }

    /** Deletes all documents owned by the calling app. */
    public AppSearchResult<Void> deleteAll() {
        AndroidFuture<AppSearchResult> future = new AndroidFuture<>();
        try {
            mService.deleteAll(future);
        } catch (RemoteException e) {
            future.completeExceptionally(e);
        }
        return getFutureOrThrow(future);
    }

    private static <T> T getFutureOrThrow(@NonNull AndroidFuture<T> future) {
        try {
            return future.get();
+8 −0
Original line number Diff line number Diff line
@@ -72,4 +72,12 @@ interface IAppSearchManager {
    void query(
        in byte[] searchSpecBytes, in byte[] resultSpecBytes, in byte[] scoringSpecBytes,
        in AndroidFuture<AppSearchResult> callback);

    /**
     * Deletes all documents belonging to the calling app.
     *
     * @param callback {@link AndroidFuture}&lt;{@link AppSearchResult}&lt;{@link Void}&gt;&gt;.
     *     Will be completed with the result of the call.
     */
    void deleteAll(in AndroidFuture<AppSearchResult> callback);
}
+17 −0
Original line number Diff line number Diff line
@@ -181,6 +181,23 @@ public class AppSearchManagerService extends SystemService {
            }
        }

        @Override
        public void deleteAll(@NonNull AndroidFuture<AppSearchResult> callback) {
            Preconditions.checkNotNull(callback);
            int callingUid = Binder.getCallingUidOrThrow();
            int callingUserId = UserHandle.getUserId(callingUid);
            long callingIdentity = Binder.clearCallingIdentity();
            try {
                AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
                impl.deleteAll(callingUid);
                callback.complete(AppSearchResult.newSuccessfulResult(null));
            } catch (Throwable t) {
                callback.complete(throwableToFailedResult(t));
            } finally {
                Binder.restoreCallingIdentity(callingIdentity);
            }
        }

        private <ValueType> AppSearchResult<ValueType> throwableToFailedResult(
                @NonNull Throwable t) {
            if (t instanceof AppSearchException) {
+10 −0
Original line number Diff line number Diff line
@@ -208,6 +208,16 @@ public final class AppSearchImpl {
        return searchResultsBuilder.build();
    }

    /**
     * Deletes all documents owned by the calling app.
     *
     * @param callingUid The uid of the app calling AppSearch.
     */
    public void deleteAll(int callingUid) {
        String namespace = getTypePrefix(callingUid);
        mFakeIcing.deleteByNamespace(namespace);
    }

    /**
     * Rewrites all types mentioned anywhere in {@code documentBuilder} to prepend or remove
     * {@code typePrefix}.
+12 −0
Original line number Diff line number Diff line
@@ -142,6 +142,18 @@ public class FakeIcing {
        }
    }

    /** Deletes all documents belonging to the given namespace. */
    public void deleteByNamespace(@NonNull String namespace) {
        for (int i = 0; i < mDocStore.size(); i++) {
            DocumentProto document = mDocStore.valueAt(i);
            if (namespace.equals(document.getNamespace())) {
                mDocStore.removeAt(i);
                mUriToDocIdMap.remove(document.getUri());
                i--;
            }
        }
    }

    private void indexDocument(int docId, DocumentProto document) {
        for (PropertyProto property : document.getPropertiesList()) {
            for (String stringValue : property.getStringValuesList()) {