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.exportimport.ImportStatus 20 import android.health.connect.exportimport.ImportStatus.DATA_IMPORT_ERROR_NONE 21 import android.health.connect.exportimport.ImportStatus.DATA_IMPORT_ERROR_UNKNOWN 22 import android.health.connect.exportimport.ImportStatus.DATA_IMPORT_ERROR_VERSION_MISMATCH 23 import android.health.connect.exportimport.ImportStatus.DATA_IMPORT_ERROR_WRONG_FILE 24 import androidx.core.os.asOutcomeReceiver 25 import javax.inject.Inject 26 import javax.inject.Singleton 27 import kotlinx.coroutines.CoroutineDispatcher 28 import kotlinx.coroutines.Dispatchers 29 import kotlinx.coroutines.suspendCancellableCoroutine 30 import kotlinx.coroutines.withContext 31 32 @Singleton 33 class LoadImportStatusUseCase 34 @Inject 35 constructor( 36 private val healthDataImportManager: HealthDataImportManager, 37 private val dispatcher: CoroutineDispatcher = Dispatchers.IO 38 ) : ILoadImportStatusUseCase { 39 suspend fun execute(): ImportUiState { 40 val importStatus: ImportStatus = suspendCancellableCoroutine { continuation -> 41 healthDataImportManager.getImportStatus(Runnable::run, continuation.asOutcomeReceiver()) 42 } 43 val dataImportError: ImportUiState.DataImportError = 44 when (importStatus.dataImportError) { 45 DATA_IMPORT_ERROR_UNKNOWN -> ImportUiState.DataImportError.DATA_IMPORT_ERROR_UNKNOWN 46 DATA_IMPORT_ERROR_NONE -> ImportUiState.DataImportError.DATA_IMPORT_ERROR_NONE 47 DATA_IMPORT_ERROR_WRONG_FILE -> 48 ImportUiState.DataImportError.DATA_IMPORT_ERROR_WRONG_FILE 49 DATA_IMPORT_ERROR_VERSION_MISMATCH -> 50 ImportUiState.DataImportError.DATA_IMPORT_ERROR_VERSION_MISMATCH 51 else -> { 52 ImportUiState.DataImportError.DATA_IMPORT_ERROR_UNKNOWN 53 } 54 } 55 return ImportUiState(dataImportError, importStatus.isImportOngoing) 56 } 57 58 override suspend fun invoke(): ExportImportUseCaseResult<ImportUiState> = 59 withContext(dispatcher) { 60 try { 61 ExportImportUseCaseResult.Success(execute()) 62 } catch (exception: Exception) { 63 ExportImportUseCaseResult.Failed(exception) 64 } 65 } 66 } 67 68 interface ILoadImportStatusUseCase { 69 /** Returns the stored import status. */ invokenull70 suspend fun invoke(): ExportImportUseCaseResult<ImportUiState> 71 } 72