From 1d4ae3cb46f65622e192a1c4a7b16b6b131f2192 Mon Sep 17 00:00:00 2001 From: David Perez Date: Wed, 6 Nov 2024 10:54:17 -0600 Subject: [PATCH] PM-14411: Allow accessibility autofill to run when app is already in background --- .../main/java/com/x8bit/bitwarden/MainViewModel.kt | 2 ++ .../manager/AccessibilityCompletionManagerImpl.kt | 8 ++++---- .../data/autofill/util/AutofillIntentUtils.kt | 2 +- .../data/tiles/BitwardenAutofillTileService.kt | 2 +- .../manager/AccessibilityCompletionManagerTest.kt | 14 +++++++------- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/MainViewModel.kt b/app/src/main/java/com/x8bit/bitwarden/MainViewModel.kt index f16b9fd974a..8432a388d70 100644 --- a/app/src/main/java/com/x8bit/bitwarden/MainViewModel.kt +++ b/app/src/main/java/com/x8bit/bitwarden/MainViewModel.kt @@ -190,12 +190,14 @@ class MainViewModel @Inject constructor( private fun handleAccessibilitySelectionReceive( action: MainAction.Internal.AccessibilitySelectionReceive, ) { + specialCircumstanceManager.specialCircumstance = null sendEvent(MainEvent.CompleteAccessibilityAutofill(cipherView = action.cipherView)) } private fun handleAutofillSelectionReceive( action: MainAction.Internal.AutofillSelectionReceive, ) { + specialCircumstanceManager.specialCircumstance = null sendEvent(MainEvent.CompleteAutofill(cipherView = action.cipherView)) } diff --git a/app/src/main/java/com/x8bit/bitwarden/data/autofill/accessibility/manager/AccessibilityCompletionManagerImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/autofill/accessibility/manager/AccessibilityCompletionManagerImpl.kt index 3fa8dff8d7b..07f76ffe14b 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/autofill/accessibility/manager/AccessibilityCompletionManagerImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/autofill/accessibility/manager/AccessibilityCompletionManagerImpl.kt @@ -26,18 +26,18 @@ class AccessibilityCompletionManagerImpl( .intent ?.getAutofillSelectionDataOrNull() ?: run { - activity.finish() + activity.finishAndRemoveTask() return } if (autofillSelectionData.framework != AutofillSelectionData.Framework.ACCESSIBILITY) { - activity.finish() + activity.finishAndRemoveTask() return } val uri = autofillSelectionData .uri ?.toUriOrNull() ?: run { - activity.finish() + activity.finishAndRemoveTask() return } @@ -47,7 +47,7 @@ class AccessibilityCompletionManagerImpl( ) mainScope.launch { totpManager.tryCopyTotpToClipboard(cipherView = cipherView) - activity.finish() } + activity.finishAndRemoveTask() } } diff --git a/app/src/main/java/com/x8bit/bitwarden/data/autofill/util/AutofillIntentUtils.kt b/app/src/main/java/com/x8bit/bitwarden/data/autofill/util/AutofillIntentUtils.kt index dea17c41d0b..2125cc903d8 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/autofill/util/AutofillIntentUtils.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/autofill/util/AutofillIntentUtils.kt @@ -38,7 +38,7 @@ fun createAutofillSelectionIntent( .apply { // This helps prevent a crash when using the accessibility framework if (framework == AutofillSelectionData.Framework.ACCESSIBILITY) { - setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) } putExtra( AUTOFILL_BUNDLE_KEY, diff --git a/app/src/main/java/com/x8bit/bitwarden/data/tiles/BitwardenAutofillTileService.kt b/app/src/main/java/com/x8bit/bitwarden/data/tiles/BitwardenAutofillTileService.kt index 3303a6185eb..f9248ab08fb 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/tiles/BitwardenAutofillTileService.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/tiles/BitwardenAutofillTileService.kt @@ -44,7 +44,7 @@ class BitwardenAutofillTileService : TileService() { } accessibilityAutofillManager.accessibilityAction = AccessibilityAction.AttemptParseUri val intent = Intent(applicationContext, AccessibilityActivity::class.java) - .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) if (isBuildVersionBelow(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)) { @Suppress("DEPRECATION") startActivityAndCollapse(intent) diff --git a/app/src/test/java/com/x8bit/bitwarden/data/autofill/accessibility/manager/AccessibilityCompletionManagerTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/autofill/accessibility/manager/AccessibilityCompletionManagerTest.kt index 46669b560f3..ad6e82dc980 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/autofill/accessibility/manager/AccessibilityCompletionManagerTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/autofill/accessibility/manager/AccessibilityCompletionManagerTest.kt @@ -26,7 +26,7 @@ import org.junit.jupiter.api.Test class AccessibilityCompletionManagerTest { private val activity: Activity = mockk { - every { finish() } just runs + every { finishAndRemoveTask() } just runs } private val accessibilityAutofillManager: AccessibilityAutofillManager = mockk() private val totpManager: AutofillTotpManager = mockk() @@ -68,7 +68,7 @@ class AccessibilityCompletionManagerTest { verify(exactly = 1) { activity.intent - activity.finish() + activity.finishAndRemoveTask() } } @@ -87,7 +87,7 @@ class AccessibilityCompletionManagerTest { verify(exactly = 1) { activity.intent mockIntent.getAutofillSelectionDataOrNull() - activity.finish() + activity.finishAndRemoveTask() } } @@ -111,7 +111,7 @@ class AccessibilityCompletionManagerTest { verify(exactly = 1) { activity.intent mockIntent.getAutofillSelectionDataOrNull() - activity.finish() + activity.finishAndRemoveTask() } } @@ -135,7 +135,7 @@ class AccessibilityCompletionManagerTest { verify(exactly = 1) { activity.intent mockIntent.getAutofillSelectionDataOrNull() - activity.finish() + activity.finishAndRemoveTask() } } @@ -162,7 +162,7 @@ class AccessibilityCompletionManagerTest { verify(exactly = 1) { activity.intent mockIntent.getAutofillSelectionDataOrNull() - activity.finish() + activity.finishAndRemoveTask() } } @@ -201,7 +201,7 @@ class AccessibilityCompletionManagerTest { cipherView = cipherView, uri = uri, ) - activity.finish() + activity.finishAndRemoveTask() } coVerify(exactly = 1) { totpManager.tryCopyTotpToClipboard(cipherView = cipherView)