Refactor visibility Document and upgrate schema verion to 1.

VisibilityDocument is the setting entity and store all visibility
information of a schema. It seems to be not efficient to have
multiple params like VisibilityToPackages and
VisibilityToSystem to be passed around and only convert to
VisibilityDocument at the end.

This change VisibilityDocument's schema and the logic of set and look
up Visibility setting.

Test: presubmit
Bug: 202194495
Change-Id: I73d2657891e3b163306c25ff2693874cbf57b02e
diff --git a/service/java/com/android/server/appsearch/AppSearchManagerService.java b/service/java/com/android/server/appsearch/AppSearchManagerService.java
index 4413b51..dd2a5db 100644
--- a/service/java/com/android/server/appsearch/AppSearchManagerService.java
+++ b/service/java/com/android/server/appsearch/AppSearchManagerService.java
@@ -71,6 +71,7 @@
 import com.android.server.appsearch.observer.AppSearchObserverProxy;
 import com.android.server.appsearch.stats.StatsCollector;
 import com.android.server.appsearch.util.PackageUtil;
+import com.android.server.appsearch.visibilitystore.VisibilityDocument;
 import com.android.server.usage.StorageStatsManagerLocal;
 import com.android.server.usage.StorageStatsManagerLocal.StorageStatsAugmenter;
 
@@ -359,6 +360,8 @@
                     for (int i = 0; i < schemaBundles.size(); i++) {
                         schemas.add(new AppSearchSchema(schemaBundles.get(i)));
                     }
+                    // TODO(b/202194495) move build VisibilityDocument to the SDK side and only pass
+                    //  it/s bundle via binder.
                     Map<String, List<PackageIdentifier>> schemasVisibleToPackages =
                             new ArrayMap<>(schemasVisibleToPackagesBundles.size());
                     for (Map.Entry<String, List<Bundle>> entry :
@@ -371,6 +374,9 @@
                         }
                         schemasVisibleToPackages.put(entry.getKey(), packageIdentifiers);
                     }
+                    List<VisibilityDocument> visibilityDocuments = VisibilityDocument
+                            .toVisibilityDocuments(schemas, schemasNotDisplayedBySystem,
+                                    schemasVisibleToPackages);
                     instance = mAppSearchUserInstanceManager.getUserInstance(targetUser);
                     // TODO(b/173532925): Implement logging for statsBuilder
                     SetSchemaResponse setSchemaResponse = instance.getAppSearchImpl().setSchema(
@@ -378,8 +384,7 @@
                             databaseName,
                             schemas,
                             instance.getVisibilityStore(),
-                            schemasNotDisplayedBySystem,
-                            schemasVisibleToPackages,
+                            visibilityDocuments,
                             forceOverride,
                             schemaVersion,
                             /*setSchemaStatsBuilder=*/ null);
diff --git a/service/java/com/android/server/appsearch/external/localstorage/AppSearchImpl.java b/service/java/com/android/server/appsearch/external/localstorage/AppSearchImpl.java
index aac3611..31a1bd3 100644
--- a/service/java/com/android/server/appsearch/external/localstorage/AppSearchImpl.java
+++ b/service/java/com/android/server/appsearch/external/localstorage/AppSearchImpl.java
@@ -31,7 +31,6 @@
 import android.app.appsearch.GenericDocument;
 import android.app.appsearch.GetByDocumentIdRequest;
 import android.app.appsearch.GetSchemaResponse;
-import android.app.appsearch.PackageIdentifier;
 import android.app.appsearch.SearchResultPage;
 import android.app.appsearch.SearchSpec;
 import android.app.appsearch.SetSchemaResponse;
@@ -63,6 +62,7 @@
 import com.android.server.appsearch.external.localstorage.stats.SetSchemaStats;
 import com.android.server.appsearch.external.localstorage.util.PrefixUtil;
 import com.android.server.appsearch.external.localstorage.visibilitystore.VisibilityStore;
+import com.android.server.appsearch.visibilitystore.VisibilityDocument;
 
 import com.google.android.icing.IcingSearchEngine;
 import com.google.android.icing.proto.DeleteByQueryResultProto;
@@ -410,9 +410,12 @@
      * @param schemas Schemas to set for this app.
      * @param visibilityStore If set, {@code schemasNotDisplayedBySystem} and {@code
      *     schemasVisibleToPackages} will be saved here if the schema is successfully applied.
-     * @param schemasNotDisplayedBySystem Schema types that should not be surfaced on platform
-     *     surfaces.
-     * @param schemasVisibleToPackages Schema types that are visible to the specified packages.
+     * @param visibilityDocuments {@link VisibilityDocument}s that contain all visibility setting
+     *                            information for those schemas they that has user custom settings.
+     *                            Other schemas in the list that don't has a
+     *                            {@link VisibilityDocument} will be treated as having the default
+     *                            visibility, which is accessible to the system and no other
+     *                            packages.
      * @param forceOverride Whether to force-apply the schema even if it is incompatible. Documents
      *     which do not comply with the new schema will be deleted.
      * @param version The overall version number of the request.
@@ -429,8 +432,7 @@
             @NonNull String databaseName,
             @NonNull List<AppSearchSchema> schemas,
             @Nullable VisibilityStore visibilityStore,
-            @NonNull List<String> schemasNotDisplayedBySystem,
-            @NonNull Map<String, List<PackageIdentifier>> schemasVisibleToPackages,
+            @NonNull List<VisibilityDocument> visibilityDocuments,
             boolean forceOverride,
             int version,
             @Nullable SetSchemaStats.Builder setSchemaStatsBuilder)
@@ -500,25 +502,29 @@
             }
 
             if (visibilityStore != null) {
-                Set<String> prefixedSchemasNotDisplayedBySystem =
-                        new ArraySet<>(schemasNotDisplayedBySystem.size());
-                for (int i = 0; i < schemasNotDisplayedBySystem.size(); i++) {
-                    prefixedSchemasNotDisplayedBySystem.add(
-                            prefix + schemasNotDisplayedBySystem.get(i));
+                // Add prefix to all visibility documents.
+                List<VisibilityDocument> prefixedVisibilityDocuments =
+                        new ArrayList<>(visibilityDocuments.size());
+                // Find out which Visibility document is deleted or changed to all-default settings.
+                // We need to remove them from Visibility Store.
+                Set<String> deprecatedVisibilityDocuments =
+                        new ArraySet<>(rewrittenSchemaResults.mRewrittenPrefixedTypes.keySet());
+                for (int i = 0; i < visibilityDocuments.size(); i++) {
+                    VisibilityDocument unPrefixedDocument = visibilityDocuments.get(i);
+                    String prefixedSchemaType = prefix + unPrefixedDocument.getId();
+                    prefixedVisibilityDocuments.add(new VisibilityDocument(
+                            unPrefixedDocument.toBuilder()
+                                    .setId(prefixedSchemaType)
+                                    .build()));
+                    // This schema is not all-default settings. We should keep it.
+                    deprecatedVisibilityDocuments.remove(prefixedSchemaType);
                 }
-
-                Map<String, List<PackageIdentifier>> prefixedSchemasVisibleToPackages =
-                        new ArrayMap<>(schemasVisibleToPackages.size());
-                for (Map.Entry<String, List<PackageIdentifier>> entry :
-                        schemasVisibleToPackages.entrySet()) {
-                    prefixedSchemasVisibleToPackages.put(prefix + entry.getKey(), entry.getValue());
-                }
-
-                visibilityStore.setVisibility(
-                        packageName,
-                        databaseName,
-                        prefixedSchemasNotDisplayedBySystem,
-                        prefixedSchemasVisibleToPackages);
+                // Now deprecatedVisibilityDocuments contains those existing schemas that has
+                // all-default visibility settings, add deleted schemas. That's all we need to
+                // remove.
+                deprecatedVisibilityDocuments.addAll(rewrittenSchemaResults.mDeletedPrefixedTypes);
+                visibilityStore.removeVisibility(deprecatedVisibilityDocuments);
+                visibilityStore.setVisibility(prefixedVisibilityDocuments);
             }
 
             return SetSchemaResponseToProtoConverter.toSetSchemaResponse(
@@ -2207,6 +2213,25 @@
     }
 
     /**
+     * Returns all prefixed schema types saved in AppSearch.
+     *
+     * <p>This method is inefficient to call repeatedly.
+     */
+    @NonNull
+    public List<String> getAllPrefixedSchemaTypes() {
+        mReadWriteLock.readLock().lock();
+        try {
+            List<String> cachedSchemaTypes = new ArrayList<>();
+            for (Map<String, SchemaTypeConfigProto> value : mSchemaMapLocked.values()) {
+                cachedSchemaTypes.addAll(value.keySet());
+            }
+            return cachedSchemaTypes;
+        } finally {
+            mReadWriteLock.readLock().unlock();
+        }
+    }
+
+    /**
      * Converts an erroneous status code from the Icing status enums to the AppSearchResult enums.
      *
      * <p>Callers should ensure that the status code is not OK or WARNING_DATA_LOSS.
diff --git a/service/java/com/android/server/appsearch/external/localstorage/converter/SearchSpecToProtoConverter.java b/service/java/com/android/server/appsearch/external/localstorage/converter/SearchSpecToProtoConverter.java
index be474f9..7ffad36 100644
--- a/service/java/com/android/server/appsearch/external/localstorage/converter/SearchSpecToProtoConverter.java
+++ b/service/java/com/android/server/appsearch/external/localstorage/converter/SearchSpecToProtoConverter.java
@@ -179,11 +179,9 @@
                 // If there's no visibility store, there's no extra access
                 allow = false;
             } else {
-                String databaseName = getDatabaseName(targetPrefixedSchemaFilter);
                 allow =
                         visibilityStore.isSchemaSearchableByCaller(
                                 packageName,
-                                databaseName,
                                 targetPrefixedSchemaFilter,
                                 callerUid,
                                 callerHasSystemAccess);
diff --git a/service/java/com/android/server/appsearch/external/localstorage/visibilitystore/VisibilityStore.java b/service/java/com/android/server/appsearch/external/localstorage/visibilitystore/VisibilityStore.java
index fb89250..114cf8b 100644
--- a/service/java/com/android/server/appsearch/external/localstorage/visibilitystore/VisibilityStore.java
+++ b/service/java/com/android/server/appsearch/external/localstorage/visibilitystore/VisibilityStore.java
@@ -16,13 +16,12 @@
 package com.android.server.appsearch.external.localstorage.visibilitystore;
 
 import android.annotation.NonNull;
-import android.app.appsearch.PackageIdentifier;
 import android.app.appsearch.exceptions.AppSearchException;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.server.appsearch.visibilitystore.VisibilityDocument;
 
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
 
 /**
@@ -40,33 +39,35 @@
     @VisibleForTesting String DATABASE_NAME = "VS#Db";
 
     /**
-     * Sets visibility settings for the given database. Any previous visibility settings will be
-     * overwritten.
+     * Sets visibility settings for the given {@link VisibilityDocument}s. Any previous
+     * {@link VisibilityDocument}s with same prefixed schema type will be overwritten.
      *
-     * @param packageName Package of app that owns the schemas.
-     * @param databaseName Database that owns the schemas.
-     * @param schemasNotDisplayedBySystem Set of prefixed schemas that should be hidden from
-     *     platform surfaces.
-     * @param schemasVisibleToPackages Map of prefixed schemas to a list of package identifiers that
-     *     have access to the schema.
+     * @param prefixedVisibilityDocuments List of prefixed {@link VisibilityDocument} which
+     *                                    contains schema type's visibility information.
      * @throws AppSearchException on AppSearchImpl error.
      */
     void setVisibility(
-            @NonNull String packageName,
-            @NonNull String databaseName,
-            @NonNull Set<String> schemasNotDisplayedBySystem,
-            @NonNull Map<String, List<PackageIdentifier>> schemasVisibleToPackages)
+            @NonNull List<VisibilityDocument> prefixedVisibilityDocuments)
             throws AppSearchException;
 
     /**
-     * Checks whether the given package has access to system-surfaceable schemas.
+     * Checks whether the given caller has access to system-surfaceable schemas.
      *
+     * @param packageName Package of app that owns the schemas.
+     * @param prefixedSchema The prefixed schema type that the caller want to access.
      * @param callerUid UID of the app that wants to see the data.
+     * @param callerHasSystemAccess whether the caller has system access.
      */
     boolean isSchemaSearchableByCaller(
             @NonNull String packageName,
-            @NonNull String databaseName,
             @NonNull String prefixedSchema,
             int callerUid,
             boolean callerHasSystemAccess);
+
+    /**
+     * Remove the visibility setting for the given prefixed schema type from both AppSearch and
+     * memory look up map.
+     */
+    void removeVisibility(@NonNull Set<String> deletedPrefixedSchemaTypes)
+            throws AppSearchException;
 }
diff --git a/service/java/com/android/server/appsearch/visibilitystore/NotDisplayedBySystemMap.java b/service/java/com/android/server/appsearch/visibilitystore/NotDisplayedBySystemMap.java
deleted file mode 100644
index 95905af..0000000
--- a/service/java/com/android/server/appsearch/visibilitystore/NotDisplayedBySystemMap.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.server.appsearch.visibilitystore;
-
-import android.annotation.NonNull;
-import android.util.ArrayMap;
-
-import java.util.Map;
-import java.util.Set;
-
-/**
- * Stores information about what types are hidden from platform surfaces through the
- * {@link android.app.appsearch.SetSchemaRequest.Builder#setSchemaTypeDisplayedBySystem} API.
- *
- * This object is not thread safe.
- */
-class NotDisplayedBySystemMap {
-    /**
-     * Maps packages to databases to the set of prefixed schemas that are platform-hidden within
-     * that database.
-     */
-    private final Map<String, Map<String, Set<String>>> mMap = new ArrayMap<>();
-
-    /**
-     * Sets the prefixed schemas that are opted out of platform surfacing for the database.
-     *
-     * <p>Any existing mappings for this prefix are overwritten.
-     */
-    public void setNotDisplayedBySystem(
-            @NonNull String packageName,
-            @NonNull String databaseName,
-            @NonNull Set<String> prefixedSchemas) {
-        Map<String, Set<String>> databaseToSchemas = mMap.get(packageName);
-        if (databaseToSchemas == null) {
-            databaseToSchemas = new ArrayMap<>();
-            mMap.put(packageName, databaseToSchemas);
-        }
-        databaseToSchemas.put(databaseName, prefixedSchemas);
-    }
-
-    /**
-     * Returns whether the given prefixed schema is platform surfaceable (has not opted out) in the
-     * given database.
-     */
-    public boolean isSchemaDisplayedBySystem(
-            @NonNull String packageName,
-            @NonNull String databaseName,
-            @NonNull String prefixedSchema) {
-        Map<String, Set<String>> databaseToSchemaType = mMap.get(packageName);
-        if (databaseToSchemaType == null) {
-            // No opt-outs for this package
-            return true;
-        }
-        Set<String> schemaTypes = databaseToSchemaType.get(databaseName);
-        if (schemaTypes == null) {
-            // No opt-outs for this database
-            return true;
-        }
-        // Some schemas were opted out of being platform-surfaced. As long as this schema
-        // isn't one of those opt-outs, it's surfaceable.
-        return !schemaTypes.contains(prefixedSchema);
-    }
-}
diff --git a/service/java/com/android/server/appsearch/visibilitystore/VisibilityDocument.java b/service/java/com/android/server/appsearch/visibilitystore/VisibilityDocument.java
index b010870..7050afa 100644
--- a/service/java/com/android/server/appsearch/visibilitystore/VisibilityDocument.java
+++ b/service/java/com/android/server/appsearch/visibilitystore/VisibilityDocument.java
@@ -16,36 +16,61 @@
 package com.android.server.appsearch.visibilitystore;
 
 import android.annotation.NonNull;
-import android.annotation.Nullable;
 import android.app.appsearch.AppSearchSchema;
 import android.app.appsearch.GenericDocument;
+import android.app.appsearch.PackageIdentifier;
+import android.app.appsearch.SetSchemaRequest;
+import android.os.Bundle;
+import android.util.ArrayMap;
+import android.util.ArraySet;
 
-/** Holds the visibility settings that apply to a package's databases. */
-class VisibilityDocument extends GenericDocument {
-    /** Schema type for documents that hold AppSearch's metadata, e.g. visibility settings */
+import com.android.server.appsearch.external.localstorage.util.PrefixUtil;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Holds the visibility settings that apply to a schema type.
+ * @hide
+ */
+public class VisibilityDocument extends GenericDocument {
+    /**
+     * Prefixed Schema type for documents that hold AppSearch's metadata, e.g. visibility settings
+     */
     public static final String SCHEMA_TYPE = "VisibilityType";
+    /** Namespace of documents that contain visibility settings */
+    public static final String NAMESPACE = "";
 
     /**
      * Property that holds the list of platform-hidden schemas, as part of the visibility settings.
      */
     private static final String NOT_DISPLAYED_BY_SYSTEM_PROPERTY = "notPlatformSurfaceable";
 
-    /** Property that holds nested documents of package accessible schemas. */
-    private static final String VISIBLE_TO_PACKAGES_PROPERTY = "packageAccessible";
+    /** Property that holds the package name that can access a schema. */
+    private static final String PACKAGE_NAME_PROPERTY = "packageName";
+
+    /** Property that holds the SHA 256 certificate of the app that can access a schema. */
+    private static final String SHA_256_CERT_PROPERTY = "sha256Cert";
 
     /**
      * Schema for the VisibilityStore's documents.
      *
      * <p>NOTE: If you update this, also update
-     * {@link com.android.server.appsearch.external.localstorage.VisibilityStore#SCHEMA_VERSION}
+     * {@link com.android.server.appsearch.visibilitystore.VisibilityStoreImpl#SCHEMA_VERSION}
      */
     public static final AppSearchSchema SCHEMA = new AppSearchSchema.Builder(SCHEMA_TYPE)
-            .addProperty(new AppSearchSchema.StringPropertyConfig.Builder(
+            .addProperty(new AppSearchSchema.BooleanPropertyConfig.Builder(
                     NOT_DISPLAYED_BY_SYSTEM_PROPERTY)
+                    .setCardinality(AppSearchSchema.PropertyConfig.CARDINALITY_OPTIONAL)
+                    .build())
+            .addProperty(new AppSearchSchema.StringPropertyConfig.Builder(PACKAGE_NAME_PROPERTY)
                     .setCardinality(AppSearchSchema.PropertyConfig.CARDINALITY_REPEATED)
                     .build())
-            .addProperty(new AppSearchSchema.DocumentPropertyConfig.Builder(
-                    VISIBLE_TO_PACKAGES_PROPERTY, VisibleToPackagesDocument.SCHEMA_TYPE)
+            .addProperty(new AppSearchSchema.BytesPropertyConfig.Builder(SHA_256_CERT_PROPERTY)
                     .setCardinality(AppSearchSchema.PropertyConfig.CARDINALITY_REPEATED)
                     .build())
             .build();
@@ -54,33 +79,144 @@
         super(genericDocument);
     }
 
-    @Nullable
-    public String[] getNotDisplayedBySystem() {
-        return getPropertyStringArray(NOT_DISPLAYED_BY_SYSTEM_PROPERTY);
+    public VisibilityDocument(@NonNull Bundle bundle) {
+        super(bundle);
     }
 
-    @Nullable
-    public GenericDocument[] getVisibleToPackages() {
-        return getPropertyDocumentArray(VISIBLE_TO_PACKAGES_PROPERTY);
+    /** Returns whether this schema is visible to the system. */
+    public boolean isNotDisplayedBySystem() {
+        return getPropertyBoolean(NOT_DISPLAYED_BY_SYSTEM_PROPERTY);
+    }
+
+    /**
+     * Returns a package name array which could access this schema. Use {@link #getSha256Certs()}
+     * to get package's sha 256 certs. The same index of package names array and sha256Certs array
+     * represents same package.
+     */
+    @NonNull
+    public String[] getPackageNames() {
+        return getPropertyStringArray(PACKAGE_NAME_PROPERTY);
+    }
+
+    /**
+     * Returns a package sha256Certs array which could access this schema. Use
+     * {@link #getPackageNames()} to get package's name. The same index of package names array
+     * and sha256Certs array represents same package.
+     */
+    @NonNull
+    public byte[][] getSha256Certs() {
+        return getPropertyBytesArray(SHA_256_CERT_PROPERTY);
     }
 
     /** Builder for {@link VisibilityDocument}. */
     public static class Builder extends GenericDocument.Builder<VisibilityDocument.Builder> {
-        public Builder(@NonNull String namespace, @NonNull String id) {
-            super(namespace, id, SCHEMA_TYPE);
+        private final Set<PackageIdentifier> mPackageIdentifiers = new ArraySet<>();
+
+        /**
+         * Creates a {@link Builder} for a {@link VisibilityDocument}.
+         *
+         * @param id The SchemaType of the {@link AppSearchSchema} that this
+         *           {@link VisibilityDocument} represents. The package and database prefix will be
+         *           added in server side. We are using prefixed schema type to be the final id of
+         *           this {@link VisibilityDocument}.
+         */
+        public Builder(@NonNull String id) {
+            super(NAMESPACE, id, SCHEMA_TYPE);
         }
 
-        /** Sets which prefixed schemas have opted out of platform surfacing. */
+        /** Sets whether this schema has opted out of platform surfacing. */
         @NonNull
-        public Builder setNotDisplayedBySystem(@NonNull String[] notDisplayedBySystemSchemas) {
-            return setPropertyString(NOT_DISPLAYED_BY_SYSTEM_PROPERTY, notDisplayedBySystemSchemas);
+        public Builder setNotDisplayedBySystem(boolean notDisplayedBySystem) {
+            return setPropertyBoolean(NOT_DISPLAYED_BY_SYSTEM_PROPERTY,
+                    notDisplayedBySystem);
         }
 
-        /** Sets which prefixed schemas have configured package access. */
+        /** Add {@link PackageIdentifier} of packages which has access to this schema. */
         @NonNull
-        public Builder setVisibleToPackages(
-                @NonNull VisibleToPackagesDocument[] visibleToPackagesDocuments) {
-            return setPropertyDocument(VISIBLE_TO_PACKAGES_PROPERTY, visibleToPackagesDocuments);
+        public Builder addVisibleToPackages(@NonNull Collection<PackageIdentifier>
+                packageIdentifiers) {
+            Objects.requireNonNull(packageIdentifiers);
+            mPackageIdentifiers.addAll(packageIdentifiers);
+            return this;
         }
+
+        /** Add {@link PackageIdentifier} of packages which has access to this schema. */
+        @NonNull
+        public Builder addVisibleToPackage(@NonNull PackageIdentifier packageIdentifier) {
+            Objects.requireNonNull(packageIdentifier);
+            mPackageIdentifiers.add(packageIdentifier);
+            return this;
+        }
+
+        @NonNull
+        public VisibilityDocument build() {
+            String[] packageNames = new String[mPackageIdentifiers.size()];
+            byte[][] sha256Certs = new byte[mPackageIdentifiers.size()][32];
+            int i = 0;
+            for (PackageIdentifier packageIdentifier : mPackageIdentifiers) {
+                packageNames[i] = packageIdentifier.getPackageName();
+                sha256Certs[i] = packageIdentifier.getSha256Certificate();
+                ++i;
+            }
+            setPropertyString(PACKAGE_NAME_PROPERTY, packageNames);
+            setPropertyBytes(SHA_256_CERT_PROPERTY, sha256Certs);
+            return new VisibilityDocument(super.build());
+        }
+    }
+
+    /**
+     * Build the List of {@link VisibilityDocument} from visibility settings.
+     *
+     * @param schemas                     List of {@link AppSearchSchema}.
+     * @param schemasNotDisplayedBySystem Non-prefixed Schema types that should not be surfaced on
+     *                                    platform surfaces.
+     * @param schemasVisibleToPackages    Non-prefixed Schema types that are visible to the
+     *                                    specified packages. The value List contains
+     *                                    PackageIdentifier.
+     */
+    //TODO(b/202194495) move this class to the sdk side and convert from SetSchemaRequest.
+    @NonNull
+    public static List<VisibilityDocument> toVisibilityDocuments(
+            @NonNull List<AppSearchSchema> schemas,
+            @NonNull List<String> schemasNotDisplayedBySystem,
+            @NonNull Map<String, List<PackageIdentifier>> schemasVisibleToPackages) {
+        Map<String, VisibilityDocument.Builder> documentBuilderMap = new ArrayMap<>(schemas.size());
+
+        // Set all visibility information into documentBuilderMap. All invoked schema types must
+        // present in schemas. This is checked in SetSchemaRequest.Builder.Build();
+
+        // Save schemas not displayed by system into documentBuilderMap
+        for (int i = 0; i < schemasNotDisplayedBySystem.size(); i++) {
+            VisibilityDocument.Builder visibilityBuilder = getOrCreateBuilder(
+                    documentBuilderMap, schemasNotDisplayedBySystem.get(i));
+            visibilityBuilder.setNotDisplayedBySystem(true);
+        }
+
+        // Save schemas visible package identifier into documentBuilderMap
+        for (Map.Entry<String, List<PackageIdentifier>> entry :
+                schemasVisibleToPackages.entrySet()) {
+            VisibilityDocument.Builder visibilityBuilder = getOrCreateBuilder(
+                    documentBuilderMap, entry.getKey());
+            visibilityBuilder.addVisibleToPackages(entry.getValue());
+        }
+
+        // Convert Map<Schema, document.builder> into list of document.
+        List<VisibilityDocument> visibilityDocuments = new ArrayList<>(documentBuilderMap.size());
+        for (VisibilityDocument.Builder builder : documentBuilderMap.values()) {
+            visibilityDocuments.add(builder.build());
+        }
+        return visibilityDocuments;
+    }
+
+    @NonNull
+    private static Builder getOrCreateBuilder(
+            @NonNull Map<String, VisibilityDocument.Builder> documentBuilderMap,
+            @NonNull String schemaType) {
+        Builder builder = documentBuilderMap.get(schemaType);
+        if (builder == null) {
+            builder = new VisibilityDocument.Builder(/*id=*/ schemaType);
+            documentBuilderMap.put(schemaType, builder);
+        }
+        return builder;
     }
 }
diff --git a/service/java/com/android/server/appsearch/visibilitystore/VisibilityStoreImpl.java b/service/java/com/android/server/appsearch/visibilitystore/VisibilityStoreImpl.java
index 120fd76..01379db 100644
--- a/service/java/com/android/server/appsearch/visibilitystore/VisibilityStoreImpl.java
+++ b/service/java/com/android/server/appsearch/visibilitystore/VisibilityStoreImpl.java
@@ -16,19 +16,17 @@
 package com.android.server.appsearch.visibilitystore;
 
 import static android.Manifest.permission.READ_GLOBAL_APP_SEARCH_DATA;
+import static android.app.appsearch.AppSearchResult.RESULT_NOT_FOUND;
 
 import android.annotation.NonNull;
-import android.app.appsearch.AppSearchResult;
 import android.app.appsearch.AppSearchSchema;
-import android.app.appsearch.GenericDocument;
 import android.app.appsearch.GetSchemaResponse;
-import android.app.appsearch.PackageIdentifier;
 import android.app.appsearch.exceptions.AppSearchException;
 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.os.UserHandle;
 import android.util.ArrayMap;
-import android.util.ArraySet;
+import android.util.Log;
 
 import com.android.server.appsearch.external.localstorage.AppSearchImpl;
 import com.android.server.appsearch.external.localstorage.util.PrefixUtil;
@@ -37,8 +35,6 @@
 
 import com.google.android.icing.proto.PersistType;
 
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
@@ -63,25 +59,20 @@
  * @hide
  */
 public class VisibilityStoreImpl implements VisibilityStore {
+    private static final String TAG = "AppSearchVisibilityStor";
     /** Version for the visibility schema */
-    private static final int SCHEMA_VERSION = 0;
-
-    /** Namespace of documents that contain visibility settings */
-    private static final String NAMESPACE = "";
-
-    /** Prefix to add to all visibility document ids. IcingSearchEngine doesn't allow empty ids. */
-    private static final String ID_PREFIX = "uri:";
+    private static final int SCHEMA_VERSION = 1;
 
     private final AppSearchImpl mAppSearchImpl;
 
     // Context of the user that the call is being made as.
     private final Context mUserContext;
 
-    /** Stores the schemas that are platform-hidden. All values are prefixed. */
-    private final NotDisplayedBySystemMap mNotDisplayedBySystemMap = new NotDisplayedBySystemMap();
-
-    /** Stores the schemas that are visible to 3p packages. All values are prefixed. */
-    private final VisibleToPackagesMap mVisibleToPackagesMap = new VisibleToPackagesMap();
+    /**
+     * Map of PrefixedSchemaType and VisibilityDocument stores visibility information for each
+     * schema type.
+     */
+    private final Map<String, VisibilityDocument> mVisibilityDocumentMap = new ArrayMap<>();
 
     /**
      * Creates and initializes VisibilityStore.
@@ -101,153 +92,51 @@
         mAppSearchImpl = Objects.requireNonNull(appSearchImpl);
         mUserContext = Objects.requireNonNull(userContext);
 
+        // TODO(b/202194495) handle schema migration from version 0 to 1.
         GetSchemaResponse getSchemaResponse =
             mAppSearchImpl.getSchema(PACKAGE_NAME, PACKAGE_NAME, DATABASE_NAME);
         boolean hasVisibilityType = false;
-        boolean hasVisibleToPackagesType = false;
         for (AppSearchSchema schema : getSchemaResponse.getSchemas()) {
             if (schema.getSchemaType().equals(VisibilityDocument.SCHEMA_TYPE)) {
                 hasVisibilityType = true;
-            } else if (schema.getSchemaType().equals(VisibleToPackagesDocument.SCHEMA_TYPE)) {
-                hasVisibleToPackagesType = true;
-            }
-
-            if (hasVisibilityType && hasVisibleToPackagesType) {
-                // Found both our types, can exit early.
+                // Found our type, can exit early.
                 break;
             }
         }
-        if (!hasVisibilityType || !hasVisibleToPackagesType) {
-            // Schema type doesn't exist yet. Add it.
+        if (!hasVisibilityType) {
+            // The latest schema type doesn't exist yet. Add it.
             mAppSearchImpl.setSchema(
                     PACKAGE_NAME,
                     DATABASE_NAME,
-                    Arrays.asList(VisibilityDocument.SCHEMA, VisibleToPackagesDocument.SCHEMA),
+                    Collections.singletonList(VisibilityDocument.SCHEMA),
                     /*visibilityStore=*/ null,  // Avoid recursive calls
-                    /*schemasNotDisplayedBySystem=*/ Collections.emptyList(),
-                    /*schemasVisibleToPackages=*/ Collections.emptyMap(),
+                    /*prefixedVisibilityDocuments=*/ Collections.emptyList(),
                     /*forceOverride=*/ false,
                     /*version=*/ SCHEMA_VERSION,
                     /*setSchemaStatsBuilder=*/ null);
-        }
-
-        // Populate visibility settings set
-        for (Map.Entry<String, Set<String>> entry :
-                mAppSearchImpl.getPackageToDatabases().entrySet()) {
-            String packageName = entry.getKey();
-            if (packageName.equals(PACKAGE_NAME)) {
-                continue; // Our own package. Skip.
-            }
-
-            for (String databaseName : entry.getValue()) {
-                VisibilityDocument visibilityDocument;
-                try {
-                    // Note: We use the other clients' prefixed names as ids
-                    visibilityDocument =
-                            new VisibilityDocument(
-                                    mAppSearchImpl.getDocument(
-                                            PACKAGE_NAME,
-                                            DATABASE_NAME,
-                                            NAMESPACE,
-                                            /*id=*/ getVisibilityDocumentId(
-                                                    packageName, databaseName),
-                                            /*typePropertyPaths=*/ Collections.emptyMap()));
-                } catch (AppSearchException e) {
-                    if (e.getResultCode() == AppSearchResult.RESULT_NOT_FOUND) {
-                        // TODO(b/172068212): This indicates some desync error. We were expecting a
-                        //  document, but didn't find one. Should probably reset AppSearch instead
-                        //  of ignoring it.
-                        continue;
-                    }
-                    // Otherwise, this is some other error we should pass up.
-                    throw e;
-                }
-
-                // Update platform visibility settings
-                String[] notDisplayedBySystemSchemas = visibilityDocument.getNotDisplayedBySystem();
-                if (notDisplayedBySystemSchemas != null) {
-                    mNotDisplayedBySystemMap.setNotDisplayedBySystem(
-                            packageName,
-                            databaseName,
-                            new ArraySet<>(notDisplayedBySystemSchemas));
-                }
-
-                // Update 3p package visibility settings
-                Map<String, Set<PackageIdentifier>> schemaToPackageIdentifierMap = new ArrayMap<>();
-                GenericDocument[] visibleToPackagesDocuments =
-                        visibilityDocument.getVisibleToPackages();
-                if (visibleToPackagesDocuments != null) {
-                    for (int i = 0; i < visibleToPackagesDocuments.length; i++) {
-                        VisibleToPackagesDocument visibleToPackagesDocument =
-                                new VisibleToPackagesDocument(visibleToPackagesDocuments[i]);
-                        PackageIdentifier packageIdentifier =
-                                visibleToPackagesDocument.getPackageIdentifier();
-                        String prefixedSchema = visibleToPackagesDocument.getAccessibleSchemaType();
-                        Set<PackageIdentifier> packageIdentifiers =
-                                schemaToPackageIdentifierMap.get(prefixedSchema);
-                        if (packageIdentifiers == null) {
-                            packageIdentifiers = new ArraySet<>();
-                        }
-                        packageIdentifiers.add(packageIdentifier);
-                        schemaToPackageIdentifierMap.put(prefixedSchema, packageIdentifiers);
-                    }
-                }
-                mVisibleToPackagesMap.setVisibleToPackages(
-                        packageName, databaseName, schemaToPackageIdentifierMap);
-            }
+        } else {
+            loadVisibilityDocumentMap();
         }
     }
 
     @Override
-    public void setVisibility(
-            @NonNull String packageName,
-            @NonNull String databaseName,
-            @NonNull Set<String> schemasNotDisplayedBySystem,
-            @NonNull Map<String, List<PackageIdentifier>> schemasVisibleToPackages)
+    public void setVisibility(@NonNull List<VisibilityDocument> prefixedVisibilityDocuments)
             throws AppSearchException {
-        Objects.requireNonNull(packageName);
-        Objects.requireNonNull(databaseName);
-        Objects.requireNonNull(schemasNotDisplayedBySystem);
-        Objects.requireNonNull(schemasVisibleToPackages);
-
-        // Persist the document
-        VisibilityDocument.Builder visibilityDocument =
-                new VisibilityDocument.Builder(
-                        NAMESPACE, /*id=*/ getVisibilityDocumentId(packageName, databaseName));
-        if (!schemasNotDisplayedBySystem.isEmpty()) {
-            visibilityDocument.setNotDisplayedBySystem(
-                    schemasNotDisplayedBySystem.toArray(new String[0]));
+        Objects.requireNonNull(prefixedVisibilityDocuments);
+        // Save new setting.
+        for (int i = 0; i < prefixedVisibilityDocuments.size(); i++) {
+            // put VisibilityDocument to AppSearchImpl and mVisibilityDocumentMap. If there is a
+            // VisibilityDocument with same prefixed schema exists, it will be replaced by new
+            // VisibilityDocument in both AppSearch and memory look up map.
+            VisibilityDocument prefixedVisibilityDocument = prefixedVisibilityDocuments.get(i);
+            mAppSearchImpl.putDocument(PACKAGE_NAME, DATABASE_NAME,
+                    prefixedVisibilityDocument, /*logger=*/ null);
+            mVisibilityDocumentMap.put(prefixedVisibilityDocument.getId(),
+                    prefixedVisibilityDocument);
         }
 
-        Map<String, Set<PackageIdentifier>> schemaToPackageIdentifierMap = new ArrayMap<>();
-        List<VisibleToPackagesDocument> visibleToPackagesDocuments = new ArrayList<>();
-        for (Map.Entry<String, List<PackageIdentifier>> entry :
-                schemasVisibleToPackages.entrySet()) {
-            for (int i = 0; i < entry.getValue().size(); i++) {
-                VisibleToPackagesDocument visibleToPackagesDocument =
-                        new VisibleToPackagesDocument.Builder(NAMESPACE, /*id=*/ "")
-                                .setAccessibleSchemaType(entry.getKey())
-                                .setPackageIdentifier(entry.getValue().get(i))
-                                .build();
-                visibleToPackagesDocuments.add(visibleToPackagesDocument);
-            }
-            schemaToPackageIdentifierMap.put(entry.getKey(), new ArraySet<>(entry.getValue()));
-        }
-        if (!visibleToPackagesDocuments.isEmpty()) {
-            visibilityDocument.setVisibleToPackages(
-                    visibleToPackagesDocuments.toArray(new VisibleToPackagesDocument[0]));
-        }
-
-        mAppSearchImpl.putDocument(
-                PACKAGE_NAME, DATABASE_NAME, visibilityDocument.build(), /*logger=*/ null);
         // Now that the visibility document has been written. Persist the newly written data.
         mAppSearchImpl.persistToDisk(PersistType.Code.LITE);
-
-        // Update derived data structures.
-        mNotDisplayedBySystemMap.setNotDisplayedBySystem(
-                packageName, databaseName, schemasNotDisplayedBySystem);
-        mVisibleToPackagesMap.setVisibleToPackages(
-                packageName, databaseName, schemaToPackageIdentifierMap);
     }
 
     /**
@@ -265,26 +154,54 @@
     @Override
     public boolean isSchemaSearchableByCaller(
             @NonNull String packageName,
-            @NonNull String databaseName,
             @NonNull String prefixedSchema,
             int callerUid,
             boolean callerHasSystemAccess) {
         Objects.requireNonNull(packageName);
-        Objects.requireNonNull(databaseName);
         Objects.requireNonNull(prefixedSchema);
 
         if (packageName.equals(PACKAGE_NAME)) {
             return false; // VisibilityStore schemas are for internal bookkeeping.
         }
 
-        if (callerHasSystemAccess
-                && mNotDisplayedBySystemMap.isSchemaDisplayedBySystem(
-                packageName, databaseName, prefixedSchema)) {
+        VisibilityDocument visibilityDocument = mVisibilityDocumentMap.get(prefixedSchema);
+
+        if (visibilityDocument == null) {
+            // The target schema doesn't exist yet. We will treat it as default setting and the only
+            // accessible case is that the caller has system access.
+            return callerHasSystemAccess;
+        }
+
+        if (callerHasSystemAccess && !visibilityDocument.isNotDisplayedBySystem()) {
             return true;
         }
 
         // May not be platform surfaceable, but might still be accessible through 3p access.
-        return isSchemaVisibleToPackages(packageName, databaseName, prefixedSchema, callerUid);
+        return isSchemaVisibleToPackages(visibilityDocument, callerUid);
+    }
+
+    @Override
+    public void removeVisibility(@NonNull Set<String> deletedPrefixedSchemaTypes)
+            throws AppSearchException {
+        for (String prefixedSchemaType : deletedPrefixedSchemaTypes) {
+            if (mVisibilityDocumentMap.remove(prefixedSchemaType) == null) {
+                // The deleted schema is not all-default setting, we need to remove its
+                // VisibilityDocument from Icing.
+                try {
+                    mAppSearchImpl.remove(PACKAGE_NAME, DATABASE_NAME, VisibilityDocument.NAMESPACE,
+                            prefixedSchemaType, /*removeStatsBuilder=*/null);
+                } catch (AppSearchException e) {
+                    if (e.getResultCode() == RESULT_NOT_FOUND) {
+                        // We are trying to remove this visibility setting, so it's weird but seems
+                        // to be fine if we cannot find it.
+                        Log.e(TAG, "Cannot find visibility document for " + prefixedSchemaType
+                                + " to remove.");
+                        return;
+                    }
+                    throw e;
+                }
+            }
+        }
     }
 
     /**
@@ -296,18 +213,15 @@
      * certificate was once used to sign the package, the package will still be granted access. This
      * does not handle packages that have been signed by multiple certificates.
      */
-    private boolean isSchemaVisibleToPackages(
-            @NonNull String packageName,
-            @NonNull String databaseName,
-            @NonNull String prefixedSchema,
+    private boolean isSchemaVisibleToPackages(@NonNull VisibilityDocument visibilityDocument,
             int callerUid) {
-        Set<PackageIdentifier> packageIdentifiers =
-                mVisibleToPackagesMap.getAccessiblePackages(
-                        packageName, databaseName, prefixedSchema);
-        if (packageIdentifiers.isEmpty()) {
-            return false;
+        String[] packageNames = visibilityDocument.getPackageNames();
+        byte[][] sha256Certs = visibilityDocument.getSha256Certs();
+        if (packageNames.length != sha256Certs.length) {
+            // We always set them in pair, So this must has something wrong.
+            throw new IllegalArgumentException("Package names and sha 256 certs doesn't match!");
         }
-        for (PackageIdentifier packageIdentifier : packageIdentifiers) {
+        for (int i = 0; i < packageNames.length; i++) {
             // TODO(b/169883602): Consider caching the UIDs of packages. Looking this up in the
             // package manager could be costly. We would also need to update the cache on
             // package-removals.
@@ -317,8 +231,7 @@
             // make calls to us. So just check if the appId portion of the uid is the same. This is
             // essentially UserHandle.isSameApp, but that's not a system API for us to use.
             int callerAppId = UserHandle.getAppId(callerUid);
-            int packageUid =
-                    PackageUtil.getPackageUid(mUserContext, packageIdentifier.getPackageName());
+            int packageUid = PackageUtil.getPackageUid(mUserContext, packageNames[i]);
             int userAppId = UserHandle.getAppId(packageUid);
             if (callerAppId != userAppId) {
                 continue;
@@ -328,8 +241,8 @@
             if (mUserContext
                     .getPackageManager()
                     .hasSigningCertificate(
-                            packageIdentifier.getPackageName(),
-                            packageIdentifier.getSha256Certificate(),
+                            packageNames[i],
+                            sha256Certs[i],
                             PackageManager.CERT_INPUT_SHA256)) {
                 // The caller has the right package name and right certificate!
                 return true;
@@ -340,15 +253,39 @@
     }
 
     /**
-     * Adds a prefix to create a visibility store document's id.
-     *
-     * @param packageName Package to which the visibility doc refers
-     * @param databaseName Database to which the visibility doc refers
-     * @return Prefixed id
+     * Loads all stored latest {@link VisibilityDocument} from Icing, and put them into
+     * {@link #mVisibilityDocumentMap}.
      */
-    @NonNull
-    private static String getVisibilityDocumentId(
-            @NonNull String packageName, @NonNull String databaseName) {
-        return ID_PREFIX + PrefixUtil.createPrefix(packageName, databaseName);
+    private void loadVisibilityDocumentMap() throws AppSearchException {
+        // Populate visibility settings set
+        List<String> cachedSchemaTypes = mAppSearchImpl.getAllPrefixedSchemaTypes();
+        for (int i = 0; i < cachedSchemaTypes.size(); i++) {
+            String prefixedSchemaType = cachedSchemaTypes.get(i);
+            String packageName = PrefixUtil.getPackageName(prefixedSchemaType);
+            if (packageName.equals(PACKAGE_NAME)) {
+                continue; // Our own package. Skip.
+            }
+
+            VisibilityDocument visibilityDocument;
+            try {
+                // Note: We use the other clients' prefixed schema type as ids
+                visibilityDocument = new VisibilityDocument(
+                        mAppSearchImpl.getDocument(
+                                PACKAGE_NAME,
+                                DATABASE_NAME,
+                                VisibilityDocument.NAMESPACE,
+                                /*id=*/ prefixedSchemaType,
+                                /*typePropertyPaths=*/ Collections.emptyMap()));
+            } catch (AppSearchException e) {
+                if (e.getResultCode() == RESULT_NOT_FOUND) {
+                    // The schema has all default setting and we won't have a VisibilityDocument for
+                    // it.
+                    continue;
+                }
+                // Otherwise, this is some other error we should pass up.
+                throw e;
+            }
+            mVisibilityDocumentMap.put(prefixedSchemaType, visibilityDocument);
+        }
     }
 }
diff --git a/service/java/com/android/server/appsearch/visibilitystore/VisibleToPackagesDocument.java b/service/java/com/android/server/appsearch/visibilitystore/VisibleToPackagesDocument.java
deleted file mode 100644
index 8d50339..0000000
--- a/service/java/com/android/server/appsearch/visibilitystore/VisibleToPackagesDocument.java
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.server.appsearch.visibilitystore;
-
-import android.annotation.NonNull;
-import android.annotation.Nullable;
-import android.app.appsearch.AppSearchSchema;
-import android.app.appsearch.GenericDocument;
-import android.app.appsearch.PackageIdentifier;
-
-/**
- * Holds configuration about a package+cert that can access a schema.
- *
- * @see android.app.appsearch.SetSchemaRequest.Builder#setSchemaTypeVisibilityForPackage
- */
-class VisibleToPackagesDocument extends GenericDocument {
-    /** Schema type for nested documents that hold package accessible information. */
-    public static final String SCHEMA_TYPE = "PackageAccessibleType";
-
-    /** Property that holds the package name that can access a schema. */
-    private static final String PACKAGE_NAME_PROPERTY = "packageName";
-
-    /** Property that holds the SHA 256 certificate of the app that can access a schema. */
-    private static final String SHA_256_CERT_PROPERTY = "sha256Cert";
-
-    /** Property that holds the prefixed schema type that is accessible by some package. */
-    private static final String ACCESSIBLE_SCHEMA_PROPERTY = "accessibleSchema";
-
-    /**
-     * Schema for package accessible documents, these will be nested in a top-level
-     * {@link VisibilityDocument}.
-     *
-     * <p>NOTE: If you update this, also update
-     * {@link com.android.server.appsearch.external.localstorage.VisibilityStore#SCHEMA_VERSION}
-     */
-    public static final AppSearchSchema SCHEMA = new AppSearchSchema.Builder(SCHEMA_TYPE)
-            .addProperty(new AppSearchSchema.StringPropertyConfig.Builder(PACKAGE_NAME_PROPERTY)
-                    .setCardinality(AppSearchSchema.PropertyConfig.CARDINALITY_OPTIONAL)
-                    .build())
-            .addProperty(new AppSearchSchema.BytesPropertyConfig.Builder(SHA_256_CERT_PROPERTY)
-                    .setCardinality(AppSearchSchema.PropertyConfig.CARDINALITY_OPTIONAL)
-                    .build())
-            .addProperty(new AppSearchSchema.StringPropertyConfig.Builder(
-                    ACCESSIBLE_SCHEMA_PROPERTY)
-                    .setCardinality(AppSearchSchema.PropertyConfig.CARDINALITY_OPTIONAL)
-                    .build())
-            .build();
-
-    VisibleToPackagesDocument(@NonNull GenericDocument genericDocument) {
-        super(genericDocument);
-    }
-
-    @Nullable
-    public String getAccessibleSchemaType() {
-        return getPropertyString(ACCESSIBLE_SCHEMA_PROPERTY);
-    }
-
-    /** Gets which package is able to access {@link #getAccessibleSchemaType} */
-    @NonNull
-    public PackageIdentifier getPackageIdentifier() {
-        String packageName = getPropertyString(PACKAGE_NAME_PROPERTY);
-        byte[] sha256Cert = getPropertyBytes(SHA_256_CERT_PROPERTY);
-        return new PackageIdentifier(packageName, sha256Cert);
-    }
-
-    /** Builder for {@link VisibleToPackagesDocument} instances. */
-    public static class Builder extends GenericDocument.Builder<VisibleToPackagesDocument.Builder> {
-        Builder(@NonNull String namespace, @NonNull String id) {
-            super(namespace, id, SCHEMA_TYPE);
-        }
-
-        /** Sets which prefixed schema type is accessible by the package */
-        @NonNull
-        public Builder setAccessibleSchemaType(@NonNull String schemaType) {
-            return setPropertyString(ACCESSIBLE_SCHEMA_PROPERTY, schemaType);
-        }
-
-        /** Sets which package is able to access the {@link #setAccessibleSchemaType}. */
-        @NonNull
-        public Builder setPackageIdentifier(@NonNull PackageIdentifier packageIdentifier) {
-            return setPropertyString(PACKAGE_NAME_PROPERTY, packageIdentifier.getPackageName())
-                    .setPropertyBytes(SHA_256_CERT_PROPERTY,
-                            packageIdentifier.getSha256Certificate());
-        }
-
-        @Override
-        @NonNull
-        public VisibleToPackagesDocument build() {
-            return new VisibleToPackagesDocument(super.build());
-        }
-    }
-}
diff --git a/service/java/com/android/server/appsearch/visibilitystore/VisibleToPackagesMap.java b/service/java/com/android/server/appsearch/visibilitystore/VisibleToPackagesMap.java
deleted file mode 100644
index e2b51a7..0000000
--- a/service/java/com/android/server/appsearch/visibilitystore/VisibleToPackagesMap.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.server.appsearch.visibilitystore;
-
-import android.annotation.NonNull;
-import android.app.appsearch.PackageIdentifier;
-import android.util.ArrayMap;
-
-import java.util.Collections;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * Stores information about what types are accessible to which packages through the
- * {@link android.app.appsearch.SetSchemaRequest.Builder#setSchemaTypeVisibilityForPackage} API.
- *
- * This object is not thread safe.
- */
-class VisibleToPackagesMap {
-    /**
-     * Maps packages to databases to prefixed schemas to PackageIdentifiers that have access to that
-     * schema.
-     */
-    private final Map<String, Map<String, Map<String, Set<PackageIdentifier>>>> mMap =
-            new ArrayMap<>();
-
-    /**
-     * Sets the prefixed schemas that have package visibility in the given database.
-     *
-     * <p>Any existing mappings for this prefix are overwritten.
-     */
-    public void setVisibleToPackages(
-            @NonNull String packageName,
-            @NonNull String databaseName,
-            @NonNull Map<String, Set<PackageIdentifier>> schemaToPackageIdentifier) {
-        Map<String, Map<String, Set<PackageIdentifier>>> databaseToSchemaTypeToVisibility =
-                mMap.get(packageName);
-        if (databaseToSchemaTypeToVisibility == null) {
-            databaseToSchemaTypeToVisibility = new ArrayMap<>();
-            mMap.put(packageName, databaseToSchemaTypeToVisibility);
-        }
-        databaseToSchemaTypeToVisibility.put(databaseName, schemaToPackageIdentifier);
-    }
-
-    /**
-     * Returns the set of all {@link android.app.appsearch.PackageIdentifier}s which can access the
-     * given schema type.
-     *
-     * <p>If no such settings exist, returns the empty set.
-     */
-    @NonNull
-    public Set<PackageIdentifier> getAccessiblePackages(
-            @NonNull String packageName,
-            @NonNull String databaseName,
-            @NonNull String prefixedSchema) {
-        Map<String, Map<String, Set<PackageIdentifier>>> databaseToSchemaTypeToVisibility =
-                mMap.get(packageName);
-        if (databaseToSchemaTypeToVisibility == null) {
-            return Collections.emptySet();
-        }
-        Map<String, Set<PackageIdentifier>> schemaTypeToVisibility =
-                databaseToSchemaTypeToVisibility.get(databaseName);
-        if (schemaTypeToVisibility == null) {
-            return Collections.emptySet();
-        }
-        Set<PackageIdentifier> accessiblePackages = schemaTypeToVisibility.get(prefixedSchema);
-        if (accessiblePackages == null) {
-            return Collections.emptySet();
-        }
-        return accessiblePackages;
-    }
-}