Implement AppSearch.deleteByTypes()

Bug: 147636343
Test: atest CtsAppSearchTestCases FrameworksCoreTests:android.app.appsearch FrameworksServicesTests:com.android.server.appsearch.impl
Change-Id: I053b33744c1cec8f307ca975314c3b9415d48b68
diff --git a/service/java/com/android/server/appsearch/AppSearchManagerService.java b/service/java/com/android/server/appsearch/AppSearchManagerService.java
index 27c1419..16948b2 100644
--- a/service/java/com/android/server/appsearch/AppSearchManagerService.java
+++ b/service/java/com/android/server/appsearch/AppSearchManagerService.java
@@ -214,6 +214,41 @@
         }
 
         @Override
+        public void deleteByTypes(
+                List<String> schemaTypes, AndroidFuture<AppSearchBatchResult> callback) {
+            Preconditions.checkNotNull(schemaTypes);
+            Preconditions.checkNotNull(callback);
+            int callingUid = Binder.getCallingUidOrThrow();
+            int callingUserId = UserHandle.getUserId(callingUid);
+            long callingIdentity = Binder.clearCallingIdentity();
+            try {
+                AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
+                AppSearchBatchResult.Builder<String, Void> resultBuilder =
+                        new AppSearchBatchResult.Builder<>();
+                for (int i = 0; i < schemaTypes.size(); i++) {
+                    String schemaType = schemaTypes.get(i);
+                    try {
+                        if (!impl.deleteByType(callingUid, schemaType)) {
+                            resultBuilder.setFailure(
+                                    schemaType,
+                                    AppSearchResult.RESULT_NOT_FOUND,
+                                    /*errorMessage=*/ null);
+                        } else {
+                            resultBuilder.setSuccess(schemaType, /*value=*/ null);
+                        }
+                    } catch (Throwable t) {
+                        resultBuilder.setResult(schemaType, throwableToFailedResult(t));
+                    }
+                }
+                callback.complete(resultBuilder.build());
+            } catch (Throwable t) {
+                callback.completeExceptionally(t);
+            } finally {
+                Binder.restoreCallingIdentity(callingIdentity);
+            }
+        }
+
+        @Override
         public void deleteAll(@NonNull AndroidFuture<AppSearchResult> callback) {
             Preconditions.checkNotNull(callback);
             int callingUid = Binder.getCallingUidOrThrow();
diff --git a/service/java/com/android/server/appsearch/impl/AppSearchImpl.java b/service/java/com/android/server/appsearch/impl/AppSearchImpl.java
index a267e13..4358d20 100644
--- a/service/java/com/android/server/appsearch/impl/AppSearchImpl.java
+++ b/service/java/com/android/server/appsearch/impl/AppSearchImpl.java
@@ -230,6 +230,13 @@
         return mFakeIcing.delete(uri);
     }
 
+    /** Deletes all documents having the given {@code schemaType}. */
+    public boolean deleteByType(int callingUid, @NonNull String schemaType) {
+        String typePrefix = getTypePrefix(callingUid);
+        String qualifiedType = typePrefix + schemaType;
+        return mFakeIcing.deleteByType(qualifiedType);
+    }
+
     /**
      * Deletes all documents owned by the calling app.
      *
diff --git a/service/java/com/android/server/appsearch/impl/FakeIcing.java b/service/java/com/android/server/appsearch/impl/FakeIcing.java
index 6703559..da15734 100644
--- a/service/java/com/android/server/appsearch/impl/FakeIcing.java
+++ b/service/java/com/android/server/appsearch/impl/FakeIcing.java
@@ -157,6 +157,25 @@
         }
     }
 
+    /**
+     * Deletes all documents having the given type.
+     *
+     * @return true if any documents were deleted.
+     */
+    public boolean deleteByType(@NonNull String type) {
+        boolean deletedAny = false;
+        for (int i = 0; i < mDocStore.size(); i++) {
+            DocumentProto document = mDocStore.valueAt(i);
+            if (type.equals(document.getSchema())) {
+                mDocStore.removeAt(i);
+                mUriToDocIdMap.remove(document.getUri());
+                i--;
+                deletedAny = true;
+            }
+        }
+        return deletedAny;
+    }
+
     private void indexDocument(int docId, DocumentProto document) {
         for (PropertyProto property : document.getPropertiesList()) {
             for (String stringValue : property.getStringValuesList()) {