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 
19 /**
20  * Copyright (C) 2022 The Android Open Source Project
21  *
22  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23  * in compliance with the License. You may obtain a copy of the License at
24  *
25  * ```
26  *      http://www.apache.org/licenses/LICENSE-2.0
27  * ```
28  *
29  * Unless required by applicable law or agreed to in writing, software distributed under the License
30  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
31  * or implied. See the License for the specific language governing permissions and limitations under
32  * the License.
33  */
34 package com.android.healthconnect.controller.permissions.shared
35 
36 import android.health.connect.HealthConnectManager
37 import android.health.connect.accesslog.AccessLog
38 import android.util.Log
39 import androidx.core.os.asOutcomeReceiver
40 import com.android.healthconnect.controller.service.IoDispatcher
41 import java.time.Instant
42 import javax.inject.Inject
43 import javax.inject.Singleton
44 import kotlinx.coroutines.CoroutineDispatcher
45 import kotlinx.coroutines.suspendCancellableCoroutine
46 import kotlinx.coroutines.withContext
47 
48 /** Query recent access logs for health connect connected apps. */
49 @Singleton
50 class QueryRecentAccessLogsUseCase
51 @Inject
52 constructor(
53     private val manager: HealthConnectManager,
54     @IoDispatcher private val dispatcher: CoroutineDispatcher
55 ) : IQueryRecentAccessLogsUseCase {
56 
57     companion object {
58         private const val TAG = "QueryRecentAccessLogsUseCase"
59     }
60 
61     override suspend fun invoke(): Map<String, Instant> =
62         withContext(dispatcher) {
63             try {
64                 val accessLogs =
65                     suspendCancellableCoroutine<List<AccessLog>> { continuation ->
66                         manager.queryAccessLogs(Runnable::run, continuation.asOutcomeReceiver())
67                     }
68                 accessLogs.associate { it.packageName to it.accessTime }
69             } catch (e: Exception) {
70                 Log.e(TAG, "QueryRecentAccessLogsUseCase ", e)
71                 emptyMap()
72             }
73         }
74 }
75 
76 interface IQueryRecentAccessLogsUseCase {
invokenull77     suspend fun invoke(): Map<String, Instant>
78 }
79