1 /**
<lambda>null2  * Copyright (C) 2023 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  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package com.android.healthconnect.controller.datasources.api
15 
16 import android.health.connect.HealthConnectManager
17 import android.health.connect.HealthDataCategory
18 import android.health.connect.UpdateDataOriginPriorityOrderRequest
19 import android.health.connect.datatypes.DataOrigin
20 import com.android.healthconnect.controller.service.IoDispatcher
21 import com.android.healthconnect.controller.shared.HealthDataCategoryInt
22 import javax.inject.Inject
23 import javax.inject.Singleton
24 import kotlinx.coroutines.CoroutineDispatcher
25 import kotlinx.coroutines.withContext
26 
27 @Singleton
28 class UpdatePriorityListUseCase
29 @Inject
30 constructor(
31     private val healthConnectManager: HealthConnectManager,
32     @IoDispatcher private val dispatcher: CoroutineDispatcher
33 ) : IUpdatePriorityListUseCase {
34 
35     /** Updates the priority list of the stored [DataOrigin]s for given [HealthDataCategory]. */
36     override suspend operator fun invoke(
37         priorityList: List<String>,
38         category: @HealthDataCategoryInt Int
39     ) {
40         withContext(dispatcher) {
41             val dataOrigins: List<DataOrigin> =
42                 priorityList
43                     .stream()
44                     .map { packageName -> DataOrigin.Builder().setPackageName(packageName).build() }
45                     .toList()
46             healthConnectManager.updateDataOriginPriorityOrder(
47                 UpdateDataOriginPriorityOrderRequest(dataOrigins, category), Runnable::run) {}
48         }
49     }
50 }
51 
52 interface IUpdatePriorityListUseCase {
invokenull53     suspend fun invoke(priorityList: List<String>, category: @HealthDataCategoryInt Int)
54 }
55