1 /* <lambda>null2 * Copyright (C) 2024 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 com.android.healthconnect.controller.exportimport.api 18 19 import android.health.connect.HealthConnectManager 20 import android.health.connect.exportimport.ScheduledExportStatus 21 import androidx.core.os.asOutcomeReceiver 22 import javax.inject.Inject 23 import javax.inject.Singleton 24 import kotlinx.coroutines.CoroutineDispatcher 25 import kotlinx.coroutines.Dispatchers 26 import kotlinx.coroutines.suspendCancellableCoroutine 27 import kotlinx.coroutines.withContext 28 29 @Singleton 30 class LoadScheduledExportStatusUseCase 31 @Inject 32 constructor( 33 private val healthDataExportManager: HealthDataExportManager, 34 private val dispatcher: CoroutineDispatcher = Dispatchers.IO 35 ) : ILoadScheduledExportStatusUseCase { 36 37 companion object { 38 private const val TAG = "LoadScheduledExportStatusUseCase" 39 } 40 41 suspend fun execute(): ScheduledExportUiState { 42 val scheduledExportStatus: ScheduledExportStatus = 43 suspendCancellableCoroutine { continuation -> 44 healthDataExportManager.getScheduledExportStatus( 45 Runnable::run, continuation.asOutcomeReceiver()) 46 } 47 val dataExportError: ScheduledExportUiState.DataExportError = 48 when (scheduledExportStatus.dataExportError) { 49 HealthConnectManager.DATA_EXPORT_ERROR_UNKNOWN -> 50 ScheduledExportUiState.DataExportError.DATA_EXPORT_ERROR_UNKNOWN 51 HealthConnectManager.DATA_EXPORT_ERROR_NONE -> 52 ScheduledExportUiState.DataExportError.DATA_EXPORT_ERROR_NONE 53 HealthConnectManager.DATA_EXPORT_LOST_FILE_ACCESS -> 54 ScheduledExportUiState.DataExportError.DATA_EXPORT_LOST_FILE_ACCESS 55 else -> { 56 ScheduledExportUiState.DataExportError.DATA_EXPORT_ERROR_UNKNOWN 57 } 58 } 59 return ScheduledExportUiState( 60 scheduledExportStatus.lastSuccessfulExportTime, 61 dataExportError, 62 scheduledExportStatus.periodInDays) 63 } 64 65 override suspend operator fun invoke(): ExportImportUseCaseResult<ScheduledExportUiState> = 66 withContext(dispatcher) { 67 try { 68 ExportImportUseCaseResult.Success(execute()) 69 } catch (exception: Exception) { 70 ExportImportUseCaseResult.Failed(exception) 71 } 72 } 73 } 74 75 interface ILoadScheduledExportStatusUseCase { 76 /** Returns the stored scheduled export status. */ invokenull77 suspend fun invoke(): ExportImportUseCaseResult<ScheduledExportUiState> 78 } 79