1 /*
<lambda>null2  * Copyright (C) 2023 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  */
18 package com.android.healthconnect.controller.permissions.app
19 
20 import com.android.healthconnect.controller.permissions.api.IGetGrantedHealthPermissionsUseCase
21 import com.android.healthconnect.controller.permissions.data.HealthPermission
22 import com.android.healthconnect.controller.service.IoDispatcher
23 import com.android.healthconnect.controller.shared.HealthPermissionReader
24 import javax.inject.Inject
25 import javax.inject.Singleton
26 import kotlinx.coroutines.CoroutineDispatcher
27 import kotlinx.coroutines.withContext
28 
29 @Singleton
30 class LoadAppPermissionsStatusUseCase
31 @Inject
32 constructor(
33     private val loadGrantedHealthPermissionsUseCase: IGetGrantedHealthPermissionsUseCase,
34     private val healthPermissionReader: HealthPermissionReader,
35     @IoDispatcher private val dispatcher: CoroutineDispatcher
36 ) {
37     suspend operator fun invoke(packageName: String): List<HealthPermissionStatus> =
38         withContext(dispatcher) {
39             val permissions = healthPermissionReader.getValidHealthPermissions(packageName)
40             val grantedPermissions = loadGrantedHealthPermissionsUseCase(packageName)
41             permissions.map { permission ->
42                 HealthPermissionStatus(
43                     permission, grantedPermissions.contains(permission.toString()))
44             }
45         }
46 }
47 
48 data class HealthPermissionStatus(val healthPermission: HealthPermission, val isGranted: Boolean)
49