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 17 18 import android.app.Activity 19 import android.content.Context 20 import android.content.DialogInterface 21 import android.content.Intent 22 import android.os.Bundle 23 import androidx.fragment.app.FragmentActivity 24 import androidx.navigation.findNavController 25 import com.android.healthconnect.controller.R 26 import com.android.healthconnect.controller.migration.MigrationPausedFragment.Companion.INTEGRATION_PAUSED_SEEN_KEY 27 import com.android.healthconnect.controller.migration.api.MigrationState 28 import com.android.healthconnect.controller.shared.dialog.AlertDialogBuilder 29 import com.android.healthconnect.controller.utils.logging.MigrationElement 30 import dagger.hilt.android.AndroidEntryPoint 31 32 @AndroidEntryPoint(FragmentActivity::class) 33 class MigrationActivity : Hilt_MigrationActivity() { 34 35 companion object { 36 const val MIGRATION_COMPLETE_KEY = "migration_complete_key" 37 const val MIGRATION_ACTIVITY_INTENT = "android.health.connect.action.MIGRATION" 38 39 private fun isMigrationComplete(activity: Activity): Boolean { 40 val sharedPreference = 41 activity.getSharedPreferences("USER_ACTIVITY_TRACKER", Context.MODE_PRIVATE) 42 return sharedPreference.getBoolean(MIGRATION_COMPLETE_KEY, false) 43 } 44 45 fun maybeRedirectToMigrationActivity( 46 activity: Activity, 47 migrationState: MigrationState 48 ): Boolean { 49 50 val sharedPreference = 51 activity.getSharedPreferences("USER_ACTIVITY_TRACKER", Context.MODE_PRIVATE) 52 53 if (migrationState == MigrationState.MODULE_UPGRADE_REQUIRED) { 54 val moduleUpdateSeen = 55 sharedPreference.getBoolean( 56 activity.getString(R.string.module_update_needed_seen), false) 57 58 if (!moduleUpdateSeen) { 59 activity.startActivity(createMigrationActivityIntent(activity)) 60 activity.finish() 61 return true 62 } 63 } else if (migrationState == MigrationState.APP_UPGRADE_REQUIRED) { 64 val appUpdateSeen = 65 sharedPreference.getBoolean( 66 activity.getString(R.string.app_update_needed_seen), false) 67 68 if (!appUpdateSeen) { 69 activity.startActivity(createMigrationActivityIntent(activity)) 70 activity.finish() 71 return true 72 } 73 } else if (migrationState == MigrationState.ALLOWED_PAUSED || 74 migrationState == MigrationState.ALLOWED_NOT_STARTED) { 75 val allowedPausedSeen = 76 sharedPreference.getBoolean(INTEGRATION_PAUSED_SEEN_KEY, false) 77 78 if (!allowedPausedSeen) { 79 activity.startActivity(createMigrationActivityIntent(activity)) 80 activity.finish() 81 return true 82 } 83 } 84 85 else if (migrationState == MigrationState.IN_PROGRESS) { 86 activity.startActivity(createMigrationActivityIntent(activity)) 87 activity.finish() 88 return true 89 } 90 91 return false 92 } 93 94 fun showMigrationPendingDialog( 95 context: Context, 96 message: String, 97 positiveButtonAction: DialogInterface.OnClickListener? = null, 98 negativeButtonAction: DialogInterface.OnClickListener? = null 99 ) { 100 AlertDialogBuilder(context) 101 .setLogName(MigrationElement.MIGRATION_PENDING_DIALOG_CONTAINER) 102 .setTitle(R.string.migration_pending_permissions_dialog_title) 103 .setMessage(message) 104 .setCancelable(false) 105 .setNegativeButton( 106 R.string.migration_pending_permissions_dialog_button_start_integration, 107 MigrationElement.MIGRATION_PENDING_DIALOG_CANCEL_BUTTON, 108 negativeButtonAction) 109 .setPositiveButton( 110 R.string.migration_pending_permissions_dialog_button_continue, 111 MigrationElement.MIGRATION_PENDING_DIALOG_CONTINUE_BUTTON, 112 positiveButtonAction) 113 .create() 114 .show() 115 } 116 117 fun showMigrationInProgressDialog( 118 context: Context, 119 message: String, 120 negativeButtonAction: DialogInterface.OnClickListener? = null 121 ) { 122 AlertDialogBuilder(context) 123 .setLogName(MigrationElement.MIGRATION_IN_PROGRESS_DIALOG_CONTAINER) 124 .setTitle(R.string.migration_in_progress_permissions_dialog_title) 125 .setMessage(message) 126 .setCancelable(false) 127 .setNegativeButton( 128 R.string.migration_in_progress_permissions_dialog_button_got_it, 129 MigrationElement.MIGRATION_IN_PROGRESS_DIALOG_BUTTON, 130 negativeButtonAction) 131 .create() 132 .show() 133 } 134 135 fun maybeShowWhatsNewDialog(context: Context, 136 negativeButtonAction: DialogInterface.OnClickListener? = null) { 137 val sharedPreference = 138 context.getSharedPreferences("USER_ACTIVITY_TRACKER", Context.MODE_PRIVATE) 139 val dialogSeen = 140 sharedPreference.getBoolean(context.getString(R.string.whats_new_dialog_seen), false) 141 142 if (!dialogSeen) { 143 AlertDialogBuilder(context) 144 .setLogName(MigrationElement.MIGRATION_DONE_DIALOG_CONTAINER) 145 .setTitle(R.string.migration_whats_new_dialog_title) 146 .setMessage(R.string.migration_whats_new_dialog_content) 147 .setCancelable(false) 148 .setNegativeButton( 149 R.string.migration_whats_new_dialog_button, MigrationElement.MIGRATION_DONE_DIALOG_BUTTON) { 150 unusedDialogInterface, unusedInt -> 151 sharedPreference.edit().apply { 152 putBoolean(context.getString(R.string.whats_new_dialog_seen), true) 153 apply() 154 } 155 negativeButtonAction?.onClick(unusedDialogInterface, unusedInt) 156 } 157 .create() 158 .show() 159 } 160 } 161 162 private fun createMigrationActivityIntent(context: Context): Intent { 163 return Intent(context, MigrationActivity::class.java).apply { 164 addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) 165 addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) 166 } 167 } 168 } 169 170 override fun onCreate(savedInstanceState: Bundle?) { 171 super.onCreate(savedInstanceState) 172 setContentView(R.layout.activity_migration) 173 } 174 175 override fun onBackPressed() { 176 val navController = findNavController(R.id.nav_host_fragment) 177 if (!navController.popBackStack()) { 178 finish() 179 } 180 } 181 182 override fun onNavigateUp(): Boolean { 183 val navController = findNavController(R.id.nav_host_fragment) 184 if (!navController.popBackStack()) { 185 finish() 186 } 187 return true 188 } 189 190 } 191