diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ee1d1ea36..0bdba939a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -294,7 +294,9 @@ Backup error Error occurred during backup: %s Backup already in progress + Unknown error + Authentication was cancelled Back Up With Seed Phrase Carefully write these 12 words down in order (left to right) on a piece of paper, and hide it somewhere others won’t access it. Do not take a screenshot. diff --git a/app/src/regular/java/com/tari/android/wallet/ui/fragment/restore/RestorationWithCloudFragment.kt b/app/src/regular/java/com/tari/android/wallet/ui/fragment/restore/RestorationWithCloudFragment.kt index e6a773599..e4b9d36c0 100644 --- a/app/src/regular/java/com/tari/android/wallet/ui/fragment/restore/RestorationWithCloudFragment.kt +++ b/app/src/regular/java/com/tari/android/wallet/ui/fragment/restore/RestorationWithCloudFragment.kt @@ -32,6 +32,8 @@ */ package com.tari.android.wallet.ui.fragment.restore +import android.app.Activity +import android.content.Intent import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -39,9 +41,9 @@ import android.view.ViewGroup import androidx.activity.OnBackPressedCallback import androidx.fragment.app.Fragment import androidx.lifecycle.* +import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException import com.orhanobut.logger.Logger -import com.tari.android.wallet.R.string.restore_wallet_with_cloud_error_desc -import com.tari.android.wallet.R.string.restore_wallet_with_cloud_error_title +import com.tari.android.wallet.R.string.* import com.tari.android.wallet.databinding.FragmentWalletRestoringBinding import com.tari.android.wallet.infrastructure.backup.WalletRestoration import com.tari.android.wallet.ui.activity.restore.WalletRestoreRouter @@ -79,30 +81,60 @@ UI tree rebuild on configuration changes""" state = ViewModelProvider(requireActivity()).get() state.state.observe(viewLifecycleOwner, Observer { if (it.status == RestorationStatus.FAILURE) { - ErrorDialog( - requireContext(), - title = string(restore_wallet_with_cloud_error_title), - description = string( - restore_wallet_with_cloud_error_desc, - it.exception?.message ?: "" - ), - cancelable = false, - canceledOnTouchOutside = false, - onClose = { - blockingBackPressDispatcher.isEnabled = false - requireActivity().onBackPressed() - state.reset() - } - ).show() + handleRestorationException(it) } else if (it.status == RestorationStatus.SUCCESS) { (requireActivity() as WalletRestoreRouter).onBackupCompleted() } }) } + private fun handleRestorationException(state: RestorationState) { + val exception = state.exception!! + if (exception is UserRecoverableAuthIOException) { + startActivityForResult(exception.intent, REQUEST_CODE_REAUTH) + } else { + showUnrecoverableExceptionDialog( + exception.message ?: string( + back_up_wallet_status_check_unknown_error + ) + ) + } + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + if (requestCode == REQUEST_CODE_REAUTH) { + if (resultCode == Activity.RESULT_OK) { + state.reset() + state.restoreWallet() + } else { + showUnrecoverableExceptionDialog( + string(back_up_wallet_status_check_authentication_cancellation) + ) + } + } + } + + + private fun showUnrecoverableExceptionDialog(message: String) { + ErrorDialog( + requireContext(), + title = string(restore_wallet_with_cloud_error_title), + description = string(restore_wallet_with_cloud_error_desc, message), + cancelable = false, + canceledOnTouchOutside = false, + onClose = { + blockingBackPressDispatcher.isEnabled = false + requireActivity().onBackPressed() + this.state.reset() + } + ).show() + } + companion object { @Suppress("DEPRECATION") fun newInstance() = RestorationWithCloudFragment() + + private const val REQUEST_CODE_REAUTH = 11222 } class RestorationWithCloudState(private val restoration: WalletRestoration) : ViewModel() { diff --git a/app/src/regular/java/com/tari/android/wallet/ui/fragment/settings/backup/BackupProcess.kt b/app/src/regular/java/com/tari/android/wallet/ui/fragment/settings/backup/BackupProcess.kt index 90eebe39f..2898df64f 100644 --- a/app/src/regular/java/com/tari/android/wallet/ui/fragment/settings/backup/BackupProcess.kt +++ b/app/src/regular/java/com/tari/android/wallet/ui/fragment/settings/backup/BackupProcess.kt @@ -49,6 +49,11 @@ class StorageBackupViewModel(private val storage: BackupStorage, private val bac private val currentState get() = _state.value!! init { + checkBackupStatus() + } + + fun checkBackupStatus() { + if (_state.value?.backupStatus == StorageBackupStatus.CHECKING_STATUS) return _state.value = StorageBackupState.checkingBackupStatus() viewModelScope.launch(Dispatchers.Main) { try { @@ -61,7 +66,7 @@ class StorageBackupViewModel(private val storage: BackupStorage, private val bac Logger.e(e, "Error occurred during backup check") _state.value = currentState.copy( backupStatus = StorageBackupStatus.STATUS_CHECK_FAILURE, - processException = e + statusCheckException = e ) } } diff --git a/app/src/regular/java/com/tari/android/wallet/ui/fragment/settings/backup/WalletBackupSettingsFragment.kt b/app/src/regular/java/com/tari/android/wallet/ui/fragment/settings/backup/WalletBackupSettingsFragment.kt index b5f343ec6..fe22a7943 100644 --- a/app/src/regular/java/com/tari/android/wallet/ui/fragment/settings/backup/WalletBackupSettingsFragment.kt +++ b/app/src/regular/java/com/tari/android/wallet/ui/fragment/settings/backup/WalletBackupSettingsFragment.kt @@ -32,7 +32,9 @@ */ package com.tari.android.wallet.ui.fragment.settings.backup +import android.app.Activity import android.content.Context +import android.content.Intent import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -45,6 +47,7 @@ import androidx.fragment.app.Fragment import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.lifecycleScope +import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException import com.orhanobut.logger.Logger import com.tari.android.wallet.R.string.* import com.tari.android.wallet.databinding.FragmentWalletBackupSettingsBinding @@ -113,8 +116,7 @@ framework for UI tree rebuild on configuration changes""" ui.cloudBackUpStatusProgressView.visible() } StorageBackupStatus.STATUS_CHECK_FAILURE -> { - displayStatusCheckFailureDialog(state.statusCheckException) - vm.clearStatusCheckFailure() + handleStatusCheckFailure(state) } StorageBackupStatus.BACKED_UP -> ui.cloudBackUpStatusSuccessView.visible() StorageBackupStatus.NOT_BACKED_UP, StorageBackupStatus.UNKNOWN -> { @@ -123,13 +125,39 @@ framework for UI tree rebuild on configuration changes""" } } - private fun displayStatusCheckFailureDialog(e: Exception?) { + private fun handleStatusCheckFailure(state: StorageBackupState) { + val exception = state.statusCheckException!! + if (exception is UserRecoverableAuthIOException) { + startActivityForResult(exception.intent, REQUEST_CODE_REAUTH) + } else { + displayStatusCheckFailureDialog( + exception.message ?: string(back_up_wallet_status_check_unknown_error) + ) + vm.clearStatusCheckFailure() + } + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + if (requestCode == REQUEST_CODE_REAUTH) { + if (resultCode == Activity.RESULT_OK) { + vm.clearStatusCheckFailure() + vm.checkBackupStatus() + } else { + displayStatusCheckFailureDialog( + string(back_up_wallet_status_check_authentication_cancellation) + ) + vm.clearStatusCheckFailure() + } + } + } + + private fun displayStatusCheckFailureDialog(message: String) { ErrorDialog( requireContext(), title = string(back_up_wallet_back_up_check_error_title), description = string( back_up_wallet_back_up_check_error_desc, - e?.message ?: "" + message ) ).show() } @@ -222,6 +250,8 @@ framework for UI tree rebuild on configuration changes""" companion object { @Suppress("DEPRECATION") fun newInstance() = WalletBackupSettingsFragment() + + private const val REQUEST_CODE_REAUTH = 1355 } }