AutofillDecryptActivity: use runCatching to replace exception handling
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
8ed6dad23c
commit
0bc60999c3
1 changed files with 70 additions and 65 deletions
|
@ -18,6 +18,10 @@ import androidx.annotation.RequiresApi
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import com.github.ajalt.timberkt.d
|
import com.github.ajalt.timberkt.d
|
||||||
import com.github.ajalt.timberkt.e
|
import com.github.ajalt.timberkt.e
|
||||||
|
import com.github.michaelbull.result.getOrElse
|
||||||
|
import com.github.michaelbull.result.onFailure
|
||||||
|
import com.github.michaelbull.result.onSuccess
|
||||||
|
import com.github.michaelbull.result.runCatching
|
||||||
import com.zeapo.pwdstore.autofill.oreo.AutofillAction
|
import com.zeapo.pwdstore.autofill.oreo.AutofillAction
|
||||||
import com.zeapo.pwdstore.autofill.oreo.AutofillPreferences
|
import com.zeapo.pwdstore.autofill.oreo.AutofillPreferences
|
||||||
import com.zeapo.pwdstore.autofill.oreo.Credentials
|
import com.zeapo.pwdstore.autofill.oreo.Credentials
|
||||||
|
@ -27,10 +31,8 @@ import com.zeapo.pwdstore.model.PasswordEntry
|
||||||
import com.zeapo.pwdstore.utils.OPENPGP_PROVIDER
|
import com.zeapo.pwdstore.utils.OPENPGP_PROVIDER
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileNotFoundException
|
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
import java.io.UnsupportedEncodingException
|
|
||||||
import kotlin.coroutines.Continuation
|
import kotlin.coroutines.Continuation
|
||||||
import kotlin.coroutines.resume
|
import kotlin.coroutines.resume
|
||||||
import kotlin.coroutines.resumeWithException
|
import kotlin.coroutines.resumeWithException
|
||||||
|
@ -162,37 +164,37 @@ class AutofillDecryptActivity : AppCompatActivity(), CoroutineScope {
|
||||||
val command = resumeIntent ?: Intent().apply {
|
val command = resumeIntent ?: Intent().apply {
|
||||||
action = OpenPgpApi.ACTION_DECRYPT_VERIFY
|
action = OpenPgpApi.ACTION_DECRYPT_VERIFY
|
||||||
}
|
}
|
||||||
val encryptedInput = try {
|
runCatching {
|
||||||
file.inputStream()
|
file.inputStream()
|
||||||
} catch (e: FileNotFoundException) {
|
}.onFailure { e ->
|
||||||
e(e) { "File to decrypt not found" }
|
e(e) { "File to decrypt not found" }
|
||||||
return null
|
return null
|
||||||
}
|
}.onSuccess { encryptedInput ->
|
||||||
val decryptedOutput = ByteArrayOutputStream()
|
val decryptedOutput = ByteArrayOutputStream()
|
||||||
val result = try {
|
runCatching {
|
||||||
executeOpenPgpApi(command, encryptedInput, decryptedOutput)
|
executeOpenPgpApi(command, encryptedInput, decryptedOutput)
|
||||||
} catch (e: Exception) {
|
}.onFailure { e ->
|
||||||
e(e) { "OpenPgpApi ACTION_DECRYPT_VERIFY failed" }
|
e(e) { "OpenPgpApi ACTION_DECRYPT_VERIFY failed" }
|
||||||
return null
|
return null
|
||||||
}
|
}.onSuccess { result ->
|
||||||
return when (val resultCode =
|
return when (val resultCode =
|
||||||
result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
|
result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
|
||||||
OpenPgpApi.RESULT_CODE_SUCCESS -> {
|
OpenPgpApi.RESULT_CODE_SUCCESS -> {
|
||||||
try {
|
runCatching {
|
||||||
val entry = withContext(Dispatchers.IO) {
|
val entry = withContext(Dispatchers.IO) {
|
||||||
@Suppress("BlockingMethodInNonBlockingContext")
|
@Suppress("BlockingMethodInNonBlockingContext")
|
||||||
PasswordEntry(decryptedOutput)
|
PasswordEntry(decryptedOutput)
|
||||||
}
|
}
|
||||||
Credentials.fromStoreEntry(this, file, entry, directoryStructure)
|
Credentials.fromStoreEntry(this, file, entry, directoryStructure)
|
||||||
} catch (e: UnsupportedEncodingException) {
|
}.getOrElse { e ->
|
||||||
e(e) { "Failed to parse password entry" }
|
e(e) { "Failed to parse password entry" }
|
||||||
null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> {
|
OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> {
|
||||||
val pendingIntent: PendingIntent =
|
val pendingIntent: PendingIntent =
|
||||||
result.getParcelableExtra(OpenPgpApi.RESULT_INTENT)!!
|
result.getParcelableExtra(OpenPgpApi.RESULT_INTENT)!!
|
||||||
try {
|
runCatching {
|
||||||
val intentToResume = withContext(Dispatchers.Main) {
|
val intentToResume = withContext(Dispatchers.Main) {
|
||||||
suspendCoroutine<Intent> { cont ->
|
suspendCoroutine<Intent> { cont ->
|
||||||
continueAfterUserInteraction = cont
|
continueAfterUserInteraction = cont
|
||||||
|
@ -210,9 +212,9 @@ class AutofillDecryptActivity : AppCompatActivity(), CoroutineScope {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
decryptCredential(file, intentToResume)
|
decryptCredential(file, intentToResume)
|
||||||
} catch (e: Exception) {
|
}.getOrElse { e ->
|
||||||
e(e) { "OpenPgpApi ACTION_DECRYPT_VERIFY failed with user interaction" }
|
e(e) { "OpenPgpApi ACTION_DECRYPT_VERIFY failed with user interaction" }
|
||||||
null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
OpenPgpApi.RESULT_CODE_ERROR -> {
|
OpenPgpApi.RESULT_CODE_ERROR -> {
|
||||||
|
@ -235,4 +237,7 @@ class AutofillDecryptActivity : AppCompatActivity(), CoroutineScope {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue