1 /* <lambda>null2 * Copyright 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.appfunctions.integration.tests 18 19 import android.content.Context 20 import androidx.appsearch.app.GlobalSearchSession 21 import androidx.appsearch.app.SearchSpec 22 import androidx.appsearch.platformstorage.PlatformStorage 23 import androidx.concurrent.futures.await 24 25 internal object AppSearchMetadataHelper { 26 /** Returns function IDs that belong to the given context's package. */ 27 suspend fun collectSelfFunctionIds(context: Context): Set<String> { 28 val functionIds = mutableSetOf<String>() 29 createSearchSession(context).use { session -> 30 val searchResults = 31 session.search( 32 "", 33 SearchSpec.Builder() 34 .addFilterNamespaces("app_functions_runtime") 35 .addFilterPackageNames("android") 36 .addFilterSchemas("AppFunctionRuntimeMetadata") 37 .build(), 38 ) 39 var nextPage = searchResults.nextPageAsync.await() 40 while (nextPage.isNotEmpty()) { 41 for (result in nextPage) { 42 val packageName = result.genericDocument.getPropertyString("packageName") 43 if (packageName != context.packageName) { 44 continue 45 } 46 val functionId = result.genericDocument.getPropertyString("functionId") 47 functionIds.add(checkNotNull(functionId)) 48 } 49 nextPage = searchResults.nextPageAsync.await() 50 } 51 } 52 return functionIds 53 } 54 55 private suspend fun createSearchSession(context: Context): GlobalSearchSession { 56 return PlatformStorage.createGlobalSearchSessionAsync( 57 PlatformStorage.GlobalSearchContext.Builder(context).build() 58 ) 59 .await() 60 } 61 } 62