• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 javax.inject.Inject
23 import javax.inject.Singleton
24 import kotlinx.coroutines.Dispatchers
25 import kotlinx.coroutines.suspendCancellableCoroutine
26 import kotlinx.coroutines.withContext
27 
28 @Singleton
29 class LoadMigrationStateUseCase @Inject constructor(private val manager: HealthMigrationManager) {
30 
31     companion object {
32         private const val TAG = "LoadMigrationState"
33 
34         private val migrationStateMapping =
35             mapOf(
36                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_IDLE to MigrationState.IDLE,
37                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_ALLOWED_MIGRATOR_DISABLED to
38                     MigrationState.ALLOWED_MIGRATOR_DISABLED,
39                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_ALLOWED_NOT_STARTED to
40                     MigrationState.ALLOWED_NOT_STARTED,
41                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_ALLOWED_PAUSED to
42                     MigrationState.ALLOWED_PAUSED,
43                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_ALLOWED_ERROR to
44                     MigrationState.ALLOWED_ERROR,
45                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_IN_PROGRESS to
46                     MigrationState.IN_PROGRESS,
47                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_APP_UPGRADE_REQUIRED to
48                     MigrationState.APP_UPGRADE_REQUIRED,
49                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_MODULE_UPGRADE_REQUIRED to
50                     MigrationState.MODULE_UPGRADE_REQUIRED,
51                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_COMPLETE to
52                     MigrationState.COMPLETE,
53                 HealthConnectMigrationUiState.MIGRATION_UI_STATE_COMPLETE_IDLE to
54                     MigrationState.COMPLETE_IDLE,
55             )
56     }
57 
58     suspend operator fun invoke(): MigrationState {
59         return withContext(Dispatchers.IO) {
60             try {
61                 // check if module faced an error migrating data and user action is required.
62                 val dataRestoreState = suspendCancellableCoroutine { continuation ->
63                     manager.getHealthDataState(Runnable::run, continuation.asOutcomeReceiver())
64                 }
65                 if (HealthConnectDataState.MIGRATION_STATE_MODULE_UPGRADE_REQUIRED ==
66                     dataRestoreState.dataMigrationState) {
67                     return@withContext MigrationState.MODULE_UPGRADE_REQUIRED
68                 }
69 
70                 // check apk migration state.
71                 val migrationState =
72                     suspendCancellableCoroutine { continuation ->
73                             manager.getHealthConnectMigrationUiState(
74                                 Runnable::run, continuation.asOutcomeReceiver())
75                         }
76                         .healthConnectMigrationUiState
77 
78                 migrationStateMapping.getOrDefault(migrationState, MigrationState.IDLE)
79             } catch (e: Exception) {
80                 Log.e(TAG, "Load error ", e)
81                 MigrationState.IDLE
82             }
83         }
84     }
85 }
86