1 /** <lambda>null2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * ``` 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * ``` 10 * 11 * Unless required by applicable law or agreed to in writing, software distributed under the License 12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 13 * or implied. See the License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.healthconnect.controller.permissiontypes.api 17 18 import android.health.connect.FetchDataOriginsPriorityOrderResponse 19 import android.health.connect.HealthConnectManager 20 import android.health.connect.HealthDataCategory 21 import androidx.core.os.asOutcomeReceiver 22 import com.android.healthconnect.controller.service.IoDispatcher 23 import com.android.healthconnect.controller.shared.HealthDataCategoryInt 24 import com.android.healthconnect.controller.shared.app.AppInfoReader 25 import com.android.healthconnect.controller.shared.app.AppMetadata 26 import com.android.healthconnect.controller.shared.usecase.BaseUseCase 27 import com.android.healthconnect.controller.shared.usecase.UseCaseResults 28 import javax.inject.Inject 29 import javax.inject.Singleton 30 import kotlinx.coroutines.CoroutineDispatcher 31 import kotlinx.coroutines.suspendCancellableCoroutine 32 33 @Singleton 34 class LoadPriorityListUseCase 35 @Inject 36 constructor( 37 private val healthConnectManager: HealthConnectManager, 38 private val appInfoReader: AppInfoReader, 39 @IoDispatcher private val dispatcher: CoroutineDispatcher 40 ) : BaseUseCase<@HealthDataCategoryInt Int, List<AppMetadata>>(dispatcher), ILoadPriorityListUseCase { 41 42 /** Returns list of [AppMetadata]s for given [HealthDataCategory] in priority order. */ 43 override suspend fun execute(input: @HealthDataCategoryInt Int): List<AppMetadata> { 44 val dataOriginPriorityOrderResponse: FetchDataOriginsPriorityOrderResponse = 45 suspendCancellableCoroutine { continuation -> 46 healthConnectManager.fetchDataOriginsPriorityOrder( 47 input, Runnable::run, continuation.asOutcomeReceiver()) 48 } 49 return dataOriginPriorityOrderResponse.dataOriginsPriorityOrder.map { dataOrigin -> 50 appInfoReader.getAppMetadata(dataOrigin.packageName) 51 } 52 } 53 } 54 55 interface ILoadPriorityListUseCase { invokenull56 suspend fun invoke(input: @HealthDataCategoryInt Int) : UseCaseResults<List<AppMetadata>> 57 58 suspend fun execute(input: @HealthDataCategoryInt Int) : List<AppMetadata> 59 } 60