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 package com.android.healthconnect.controller.migration.api
17 
18 import android.health.connect.HealthConnectDataState
19 import android.health.connect.migration.HealthConnectMigrationUiState
20 import android.util.Log
21 import androidx.core.os.asOutcomeReceiver
22 import com.android.healthconnect.controller.migration.api.MigrationRestoreState.DataRestoreUiState
23 import com.android.healthconnect.controller.migration.api.MigrationRestoreState.MigrationUiState
24 import javax.inject.Inject
25 import javax.inject.Singleton
26 import kotlinx.coroutines.Dispatchers
27 import kotlinx.coroutines.suspendCancellableCoroutine
28 import kotlinx.coroutines.withContext
29 
30 @Singleton
31 class LoadMigrationRestoreStateUseCase
32 @Inject
33 constructor(private val manager: HealthMigrationManager) {
34 
35     suspend operator fun invoke(): MigrationRestoreState {
36         return withContext(Dispatchers.IO) {
37             try {
38                 // Gets the data restore state
39                 val migrationRestoreState = suspendCancellableCoroutine { continuation ->
40                     manager.getHealthDataState(Runnable::run, continuation.asOutcomeReceiver())
41                 }
42 
43                 // Gets the migration UI state
44                 val migrationUiState =
45                     suspendCancellableCoroutine { continuation ->
46                             manager.getHealthConnectMigrationUiState(
47                                 Runnable::run, continuation.asOutcomeReceiver())
48                         }
49                         .healthConnectMigrationUiState
50 
51                 MigrationRestoreState(
52                     migrationUiState =
53                         migrationUiStateMapping.getOrDefault(
54                             migrationUiState, MigrationUiState.IDLE),
55                     dataRestoreState =
56                         dataRestoreUiStateMapping.getOrDefault(
57                             migrationRestoreState.dataRestoreState, DataRestoreUiState.IDLE),
58                     dataRestoreError =
59                         dataRestoreUiErrorMapping.getOrDefault(
60                             migrationRestoreState.dataRestoreError,
61                             MigrationRestoreState.DataRestoreUiError.ERROR_NONE))
62             } catch (e: Exception) {
63                 Log.e(TAG, "Load error ", e)
64                 defaultMigrationRestoreState
65             }
66         }
67     }
68 
69     companion object {
70         private const val TAG = "LoadMigrationState"
71 
72         private val migrationUiStateMapping =
73             mapOf(
74                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_IDLE to MigrationUiState.IDLE,
75                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_ALLOWED_MIGRATOR_DISABLED to
76                     MigrationUiState.ALLOWED_MIGRATOR_DISABLED,
77                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_ALLOWED_NOT_STARTED to
78                     MigrationUiState.ALLOWED_NOT_STARTED,
79                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_ALLOWED_PAUSED to
80                     MigrationUiState.ALLOWED_PAUSED,
81                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_ALLOWED_ERROR to
82                     MigrationUiState.ALLOWED_ERROR,
83                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_IN_PROGRESS to
84                     MigrationUiState.IN_PROGRESS,
85                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_APP_UPGRADE_REQUIRED to
86                     MigrationUiState.APP_UPGRADE_REQUIRED,
87                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_MODULE_UPGRADE_REQUIRED to
88                     MigrationUiState.MODULE_UPGRADE_REQUIRED,
89                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_COMPLETE to
90                     MigrationUiState.COMPLETE,
91                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_COMPLETE_IDLE to
92                     MigrationUiState.COMPLETE_IDLE,
93             )
94 
95         private val dataRestoreUiStateMapping =
96             mapOf(
97                 HealthConnectDataState.RESTORE_STATE_IDLE to DataRestoreUiState.IDLE,
98                 HealthConnectDataState.RESTORE_STATE_PENDING to DataRestoreUiState.PENDING,
99                 HealthConnectDataState.RESTORE_STATE_IN_PROGRESS to DataRestoreUiState.IN_PROGRESS,
100             )
101 
102         private val dataRestoreUiErrorMapping =
103             mapOf(
104                 HealthConnectDataState.RESTORE_ERROR_NONE to
105                     MigrationRestoreState.DataRestoreUiError.ERROR_NONE,
106                 HealthConnectDataState.RESTORE_ERROR_UNKNOWN to
107                     MigrationRestoreState.DataRestoreUiError.ERROR_UNKNOWN,
108                 HealthConnectDataState.RESTORE_ERROR_FETCHING_DATA to
109                     MigrationRestoreState.DataRestoreUiError.ERROR_FETCHING_DATA,
110                 HealthConnectDataState.RESTORE_ERROR_VERSION_DIFF to
111                     MigrationRestoreState.DataRestoreUiError.ERROR_VERSION_DIFF)
112 
113         private val defaultMigrationRestoreState =
114             MigrationRestoreState(
115                 migrationUiState = MigrationUiState.IDLE,
116                 dataRestoreState = DataRestoreUiState.IDLE,
117                 dataRestoreError = MigrationRestoreState.DataRestoreUiError.ERROR_NONE)
118     }
119 }
120