Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 23158ce

Browse files
FabianHennekemsfjarvis
authored andcommitted
Fix two SMS Autofill crashes (#985)
SMS OTP Autofill currently crashes for two reasons: 1. Tasks.await has a precondition of not running on the UI thread. 2. Exceptions thrown from Tasks are always wrapped into ExecutionExceptions and need to be unwrapped before they can be identified as ResolvableApiException. This commit addresses both issues by making waitForSms a proper coroutine using withContext and a custom wrapper around Task<T> that relies on suspendCoroutine and automatically unwraps exceptions. (cherry picked from commit 3afeff4)
1 parent c132cc9 commit 23158ce

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
88

99
- Properly handle cases where files contain only TOTP secrets and no password
1010
- Correctly hide TOTP import button when TOTP secret/OTPAUTH URL is already present in extra content
11+
- SMS OTP Autofill no longer crashes when invoked and correctly asks for the required permission on first use
1112

1213
## [1.10.1] - 2020-07-23
1314

app/src/nonFree/java/com/zeapo/pwdstore/autofill/oreo/ui/AutofillSmsActivity.kt

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,30 @@ import com.google.android.gms.auth.api.phone.SmsRetriever
2424
import com.google.android.gms.common.ConnectionResult
2525
import com.google.android.gms.common.GoogleApiAvailability
2626
import com.google.android.gms.common.api.ResolvableApiException
27-
import com.google.android.gms.tasks.Tasks
27+
import com.google.android.gms.tasks.Task
2828
import com.zeapo.pwdstore.autofill.oreo.AutofillAction
2929
import com.zeapo.pwdstore.autofill.oreo.Credentials
3030
import com.zeapo.pwdstore.autofill.oreo.FillableForm
3131
import com.zeapo.pwdstore.databinding.ActivityOreoAutofillSmsBinding
3232
import com.zeapo.pwdstore.utils.viewBinding
33+
import java.util.concurrent.ExecutionException
34+
import kotlin.coroutines.resume
35+
import kotlin.coroutines.resumeWithException
36+
import kotlin.coroutines.suspendCoroutine
37+
import kotlinx.coroutines.Dispatchers
3338
import kotlinx.coroutines.launch
39+
import kotlinx.coroutines.withContext
40+
41+
suspend fun <T> Task<T>.suspendableAwait() = suspendCoroutine<T> { cont ->
42+
addOnSuccessListener { result: T ->
43+
cont.resume(result)
44+
}
45+
addOnFailureListener { e ->
46+
// Unwrap specific exceptions (e.g. ResolvableApiException) from ExecutionException.
47+
val cause = (e as? ExecutionException)?.cause ?: e
48+
cont.resumeWithException(cause)
49+
}
50+
}
3451

3552
@RequiresApi(Build.VERSION_CODES.O)
3653
class AutofillSmsActivity : AppCompatActivity() {
@@ -105,15 +122,21 @@ class AutofillSmsActivity : AppCompatActivity() {
105122
}
106123
}
107124

108-
private fun waitForSms() {
125+
private suspend fun waitForSms() {
109126
val smsClient = SmsCodeRetriever.getAutofillClient(this@AutofillSmsActivity)
110127
try {
111-
Tasks.await(smsClient.startSmsCodeRetriever())
128+
withContext(Dispatchers.IO) {
129+
smsClient.startSmsCodeRetriever().suspendableAwait()
130+
}
112131
} catch (e: ResolvableApiException) {
113-
e.startResolutionForResult(this, 1)
132+
withContext(Dispatchers.Main) {
133+
e.startResolutionForResult(this@AutofillSmsActivity, 1)
134+
}
114135
} catch (e: Exception) {
115136
e(e)
116-
finish()
137+
withContext(Dispatchers.Main) {
138+
finish()
139+
}
117140
}
118141
}
119142

0 commit comments

Comments
 (0)