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

Commit a7583fcc authored by Desh's avatar Desh Committed by Oluwarotimi Adesina
Browse files

Make SearchSpec Internal when fetching metadata properties.

Flag: android.app.appfunctions.flags.enable_app_function_manager
Test: atest FrameworksAppFunctionsTests -c
Bug: 357551503
Change-Id: I69774ec52a642c8c421266a40d091d29961f9712
parent 50caf3e0
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ public class AppFunctionStaticMetadataHelper {
    public static final String STATIC_PROPERTY_ENABLED_BY_DEFAULT = "enabledByDefault";

    public static final String APP_FUNCTION_STATIC_NAMESPACE = "app_functions";
    public static final String PROPERTY_FUNCTION_ID = "functionId";
    public static final String PROPERTY_PACKAGE_NAME = "packageName";

    // These are constants that has to be kept the same with {@code
    // com.android.server.appsearch.appsindexer.appsearchtypes.AppSearchHelper}.
+36 −9
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.appfunctions;

import android.annotation.NonNull;
import android.annotation.WorkerThread;
import android.app.appsearch.PropertyPath;
import android.app.appsearch.SearchResult;
import android.app.appsearch.SearchSpec;
import android.util.ArrayMap;
@@ -110,26 +111,29 @@ public class MetadataSyncAdapter {
    }

    /**
     * This method returns a map of package names to a set of function ids.
     * This method returns a map of package names to a set of function ids from the AppFunction
     * metadata.
     *
     * @param queryExpression The query expression to use when searching for AppFunction metadata.
     * @param metadataSearchSpec The search spec to use when searching for AppFunction metadata.
     * @return A map of package names to a set of function ids.
     * @throws ExecutionException If the future search results fail to execute.
     * @throws InterruptedException If the future search results are interrupted.
     * @param schemaType The name space of the AppFunction metadata.
     * @return A map of package names to a set of function ids from the AppFunction metadata.
     */
    @NonNull
    @VisibleForTesting
    @WorkerThread
    ArrayMap<String, ArraySet<String>> getPackageToFunctionIdMap(
            @NonNull String queryExpression,
            @NonNull SearchSpec metadataSearchSpec,
            @NonNull String schemaType,
            @NonNull String propertyFunctionId,
            @NonNull String propertyPackageName)
            throws ExecutionException, InterruptedException {
        ArrayMap<String, ArraySet<String>> packageToFunctionIds = new ArrayMap<>();

        FutureSearchResults futureSearchResults =
                mFutureAppSearchSession.search(queryExpression, metadataSearchSpec).get();
                mFutureAppSearchSession
                        .search(
                                "",
                                buildMetadataSearchSpec(
                                        schemaType, propertyFunctionId, propertyPackageName))
                        .get();
        List<SearchResult> searchResultsList = futureSearchResults.getNextPage().get();
        // TODO(b/357551503): This could be expensive if we have more functions
        while (!searchResultsList.isEmpty()) {
@@ -146,4 +150,27 @@ public class MetadataSyncAdapter {
        }
        return packageToFunctionIds;
    }

    /**
     * This method returns a {@link SearchSpec} for searching the AppFunction metadata.
     *
     * @param schemaType The schema type of the AppFunction metadata.
     * @param propertyFunctionId The property name of the function id in the AppFunction metadata.
     * @param propertyPackageName The property name of the package name in the AppFunction metadata.
     * @return A {@link SearchSpec} for searching the AppFunction metadata.
     */
    @NonNull
    private static SearchSpec buildMetadataSearchSpec(
            @NonNull String schemaType,
            @NonNull String propertyFunctionId,
            @NonNull String propertyPackageName) {
        return new SearchSpec.Builder()
                .addFilterSchemas(schemaType)
                .addProjectionPaths(
                        schemaType,
                        List.of(
                                new PropertyPath(propertyFunctionId),
                                new PropertyPath(propertyPackageName)))
                .build();
    }
}
+6 −28
Original line number Diff line number Diff line
@@ -16,12 +16,9 @@
package com.android.server.appfunctions

import android.app.appfunctions.AppFunctionRuntimeMetadata
import android.app.appfunctions.AppFunctionRuntimeMetadata.PROPERTY_FUNCTION_ID
import android.app.appfunctions.AppFunctionRuntimeMetadata.PROPERTY_PACKAGE_NAME
import android.app.appsearch.AppSearchManager
import android.app.appsearch.AppSearchManager.SearchContext
import android.app.appsearch.PutDocumentsRequest
import android.app.appsearch.SearchSpec
import android.app.appsearch.SetSchemaRequest
import android.util.ArrayMap
import android.util.ArraySet
@@ -76,20 +73,11 @@ class MetadataSyncAdapterTest {
                testExecutor,
                FutureAppSearchSession(appSearchManager, testExecutor, searchContext),
            )
        val searchSpec: SearchSpec =
            SearchSpec.Builder()
                .addFilterSchemas(
                    AppFunctionRuntimeMetadata.RUNTIME_SCHEMA_TYPE,
                    AppFunctionRuntimeMetadata.createAppFunctionRuntimeSchema(TEST_TARGET_PKG_NAME)
                        .schemaType,
                )
                .build()
        val packageToFunctionIdMap =
            metadataSyncAdapter.getPackageToFunctionIdMap(
                "",
                searchSpec,
                PROPERTY_FUNCTION_ID,
                PROPERTY_PACKAGE_NAME,
                AppFunctionRuntimeMetadata.RUNTIME_SCHEMA_TYPE,
                AppFunctionRuntimeMetadata.PROPERTY_FUNCTION_ID,
                AppFunctionRuntimeMetadata.PROPERTY_PACKAGE_NAME,
            )

        assertThat(packageToFunctionIdMap).isNotNull()
@@ -135,21 +123,11 @@ class MetadataSyncAdapterTest {
                testExecutor,
                FutureAppSearchSession(appSearchManager, testExecutor, searchContext),
            )
        val searchSpec: SearchSpec =
            SearchSpec.Builder()
                .setResultCountPerPage(1)
                .addFilterSchemas(
                    AppFunctionRuntimeMetadata.RUNTIME_SCHEMA_TYPE,
                    AppFunctionRuntimeMetadata.createAppFunctionRuntimeSchema(TEST_TARGET_PKG_NAME)
                        .schemaType,
                )
                .build()
        val packageToFunctionIdMap =
            metadataSyncAdapter.getPackageToFunctionIdMap(
                "",
                searchSpec,
                PROPERTY_FUNCTION_ID,
                PROPERTY_PACKAGE_NAME,
                AppFunctionRuntimeMetadata.RUNTIME_SCHEMA_TYPE,
                AppFunctionRuntimeMetadata.PROPERTY_FUNCTION_ID,
                AppFunctionRuntimeMetadata.PROPERTY_PACKAGE_NAME,
            )

        assertThat(packageToFunctionIdMap).isNotNull()