mirror of
https://github.com/android-password-store/Android-Password-Store
synced 2025-08-29 13:27:46 +00:00
Refactor openpgp-ktx to leverage coroutines (#1398)
* openpgp-ktx: leverage coroutines for async IPC * Unwind nested dispatchers * Fix name shadowing warning Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
4880e1db27
commit
f834c754e6
@ -165,55 +165,51 @@ class DecryptActivity : BasePgpActivity(), OpenPgpServiceConnection.OnBound {
|
|||||||
val inputStream = File(fullPath).inputStream()
|
val inputStream = File(fullPath).inputStream()
|
||||||
val outputStream = ByteArrayOutputStream()
|
val outputStream = ByteArrayOutputStream()
|
||||||
|
|
||||||
lifecycleScope.launch(Dispatchers.IO) {
|
lifecycleScope.launch(Dispatchers.Main) {
|
||||||
api?.executeApiAsync(data, inputStream, outputStream) { result ->
|
val result = withContext(Dispatchers.IO) { checkNotNull(api).executeApi(data, inputStream, outputStream) }
|
||||||
when (result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
|
when (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
|
||||||
OpenPgpApi.RESULT_CODE_SUCCESS -> {
|
OpenPgpApi.RESULT_CODE_SUCCESS -> {
|
||||||
startAutoDismissTimer()
|
startAutoDismissTimer()
|
||||||
runCatching {
|
runCatching {
|
||||||
val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true)
|
val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true)
|
||||||
val entry = passwordEntryFactory.create(lifecycleScope, outputStream.toByteArray())
|
val entry = passwordEntryFactory.create(lifecycleScope, outputStream.toByteArray())
|
||||||
val items = arrayListOf<FieldItem>()
|
val items = arrayListOf<FieldItem>()
|
||||||
val adapter = FieldItemAdapter(emptyList(), showPassword) { text -> copyTextToClipboard(text) }
|
val adapter = FieldItemAdapter(emptyList(), showPassword) { text -> copyTextToClipboard(text) }
|
||||||
|
|
||||||
if (settings.getBoolean(PreferenceKeys.COPY_ON_DECRYPT, false)) {
|
if (settings.getBoolean(PreferenceKeys.COPY_ON_DECRYPT, false)) {
|
||||||
copyPasswordToClipboard(entry.password)
|
copyPasswordToClipboard(entry.password)
|
||||||
}
|
|
||||||
|
|
||||||
passwordEntry = entry
|
|
||||||
invalidateOptionsMenu()
|
|
||||||
|
|
||||||
if (!entry.password.isNullOrBlank()) {
|
|
||||||
items.add(FieldItem.createPasswordField(entry.password!!))
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entry.hasTotp()) {
|
|
||||||
launch(Dispatchers.IO) {
|
|
||||||
withContext(Dispatchers.Main) {
|
|
||||||
val code = entry.totp.value
|
|
||||||
items.add(FieldItem.createOtpField(code))
|
|
||||||
}
|
|
||||||
entry.totp.collect { code -> withContext(Dispatchers.Main) { adapter.updateOTPCode(code) } }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!entry.username.isNullOrBlank()) {
|
|
||||||
items.add(FieldItem.createUsernameField(entry.username!!))
|
|
||||||
}
|
|
||||||
|
|
||||||
entry.extraContent.forEach { (key, value) -> items.add(FieldItem(key, value, FieldItem.ActionType.COPY)) }
|
|
||||||
|
|
||||||
binding.recyclerView.adapter = adapter
|
|
||||||
adapter.updateItems(items)
|
|
||||||
}
|
}
|
||||||
.onFailure { e -> e(e) }
|
|
||||||
|
passwordEntry = entry
|
||||||
|
invalidateOptionsMenu()
|
||||||
|
|
||||||
|
if (!entry.password.isNullOrBlank()) {
|
||||||
|
items.add(FieldItem.createPasswordField(entry.password!!))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entry.hasTotp()) {
|
||||||
|
launch {
|
||||||
|
items.add(FieldItem.createOtpField(entry.totp.value))
|
||||||
|
entry.totp.collect { code -> withContext(Dispatchers.Main) { adapter.updateOTPCode(code) } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!entry.username.isNullOrBlank()) {
|
||||||
|
items.add(FieldItem.createUsernameField(entry.username!!))
|
||||||
|
}
|
||||||
|
|
||||||
|
entry.extraContent.forEach { (key, value) -> items.add(FieldItem(key, value, FieldItem.ActionType.COPY)) }
|
||||||
|
|
||||||
|
binding.recyclerView.adapter = adapter
|
||||||
|
adapter.updateItems(items)
|
||||||
}
|
}
|
||||||
OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> {
|
.onFailure { e -> e(e) }
|
||||||
val sender = getUserInteractionRequestIntent(result)
|
|
||||||
userInteractionRequiredResult.launch(IntentSenderRequest.Builder(sender).build())
|
|
||||||
}
|
|
||||||
OpenPgpApi.RESULT_CODE_ERROR -> handleError(result)
|
|
||||||
}
|
}
|
||||||
|
OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> {
|
||||||
|
val sender = getUserInteractionRequestIntent(result)
|
||||||
|
userInteractionRequiredResult.launch(IntentSenderRequest.Builder(sender).build())
|
||||||
|
}
|
||||||
|
OpenPgpApi.RESULT_CODE_ERROR -> handleError(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ import com.github.michaelbull.result.onFailure
|
|||||||
import com.github.michaelbull.result.runCatching
|
import com.github.michaelbull.result.runCatching
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
import me.msfjarvis.openpgpktx.util.OpenPgpApi
|
import me.msfjarvis.openpgpktx.util.OpenPgpApi
|
||||||
import me.msfjarvis.openpgpktx.util.OpenPgpUtils
|
import me.msfjarvis.openpgpktx.util.OpenPgpUtils
|
||||||
import org.openintents.openpgp.IOpenPgpService2
|
import org.openintents.openpgp.IOpenPgpService2
|
||||||
@ -48,26 +49,25 @@ class GetKeyIdsActivity : BasePgpActivity() {
|
|||||||
/** Get the Key ids from OpenKeychain */
|
/** Get the Key ids from OpenKeychain */
|
||||||
private fun getKeyIds(data: Intent = Intent()) {
|
private fun getKeyIds(data: Intent = Intent()) {
|
||||||
data.action = OpenPgpApi.ACTION_GET_KEY_IDS
|
data.action = OpenPgpApi.ACTION_GET_KEY_IDS
|
||||||
lifecycleScope.launch(Dispatchers.IO) {
|
lifecycleScope.launch(Dispatchers.Main) {
|
||||||
api?.executeApiAsync(data, null, null) { result ->
|
val result = withContext(Dispatchers.IO) { checkNotNull(api).executeApi(data, null, null) }
|
||||||
when (result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
|
when (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
|
||||||
OpenPgpApi.RESULT_CODE_SUCCESS -> {
|
OpenPgpApi.RESULT_CODE_SUCCESS -> {
|
||||||
runCatching {
|
runCatching {
|
||||||
val ids =
|
val ids =
|
||||||
result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS)?.map { OpenPgpUtils.convertKeyIdToHex(it) }
|
result.getLongArrayExtra(OpenPgpApi.RESULT_KEY_IDS)?.map { OpenPgpUtils.convertKeyIdToHex(it) }
|
||||||
?: emptyList()
|
?: emptyList()
|
||||||
val keyResult = Intent().putExtra(OpenPgpApi.EXTRA_KEY_IDS, ids.toTypedArray())
|
val keyResult = Intent().putExtra(OpenPgpApi.EXTRA_KEY_IDS, ids.toTypedArray())
|
||||||
setResult(RESULT_OK, keyResult)
|
setResult(RESULT_OK, keyResult)
|
||||||
finish()
|
finish()
|
||||||
}
|
|
||||||
.onFailure { e -> e(e) }
|
|
||||||
}
|
}
|
||||||
OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> {
|
.onFailure { e -> e(e) }
|
||||||
val sender = getUserInteractionRequestIntent(result)
|
|
||||||
userInteractionRequiredResult.launch(IntentSenderRequest.Builder(sender).build())
|
|
||||||
}
|
|
||||||
OpenPgpApi.RESULT_CODE_ERROR -> handleError(result)
|
|
||||||
}
|
}
|
||||||
|
OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> {
|
||||||
|
val sender = getUserInteractionRequestIntent(result)
|
||||||
|
userInteractionRequiredResult.launch(IntentSenderRequest.Builder(sender).build())
|
||||||
|
}
|
||||||
|
OpenPgpApi.RESULT_CODE_ERROR -> handleError(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -394,97 +394,97 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB
|
|||||||
else -> "$fullPath/$editName.gpg"
|
else -> "$fullPath/$editName.gpg"
|
||||||
}
|
}
|
||||||
|
|
||||||
lifecycleScope.launch(Dispatchers.IO) {
|
lifecycleScope.launch(Dispatchers.Main) {
|
||||||
api?.executeApiAsync(encryptionIntent, inputStream, outputStream) { result ->
|
val result =
|
||||||
when (result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
|
withContext(Dispatchers.IO) { checkNotNull(api).executeApi(encryptionIntent, inputStream, outputStream) }
|
||||||
OpenPgpApi.RESULT_CODE_SUCCESS -> {
|
when (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) {
|
||||||
runCatching {
|
OpenPgpApi.RESULT_CODE_SUCCESS -> {
|
||||||
val file = File(path)
|
runCatching {
|
||||||
// If we're not editing, this file should not already exist!
|
val file = File(path)
|
||||||
// Additionally, if we were editing and the incoming and outgoing
|
// If we're not editing, this file should not already exist!
|
||||||
// filenames differ, it means we renamed. Ensure that the target
|
// Additionally, if we were editing and the incoming and outgoing
|
||||||
// doesn't already exist to prevent an accidental overwrite.
|
// filenames differ, it means we renamed. Ensure that the target
|
||||||
if ((!editing || (editing && suggestedName != file.nameWithoutExtension)) && file.exists()) {
|
// doesn't already exist to prevent an accidental overwrite.
|
||||||
snackbar(message = getString(R.string.password_creation_duplicate_error))
|
if ((!editing || (editing && suggestedName != file.nameWithoutExtension)) && file.exists()) {
|
||||||
return@executeApiAsync
|
snackbar(message = getString(R.string.password_creation_duplicate_error))
|
||||||
}
|
return@runCatching
|
||||||
|
}
|
||||||
|
|
||||||
if (!file.isInsideRepository()) {
|
if (!file.isInsideRepository()) {
|
||||||
snackbar(message = getString(R.string.message_error_destination_outside_repo))
|
snackbar(message = getString(R.string.message_error_destination_outside_repo))
|
||||||
return@executeApiAsync
|
return@runCatching
|
||||||
}
|
}
|
||||||
|
|
||||||
file.outputStream().use { it.write(outputStream.toByteArray()) }
|
withContext(Dispatchers.IO) { file.outputStream().use { it.write(outputStream.toByteArray()) } }
|
||||||
|
|
||||||
// associate the new password name with the last name's timestamp in
|
// associate the new password name with the last name's timestamp in
|
||||||
// history
|
// history
|
||||||
val preference = getSharedPreferences("recent_password_history", Context.MODE_PRIVATE)
|
val preference = getSharedPreferences("recent_password_history", Context.MODE_PRIVATE)
|
||||||
val oldFilePathHash = "$repoPath/${oldCategory?.trim('/')}/$oldFileName.gpg".base64()
|
val oldFilePathHash = "$repoPath/${oldCategory?.trim('/')}/$oldFileName.gpg".base64()
|
||||||
val timestamp = preference.getString(oldFilePathHash)
|
val timestamp = preference.getString(oldFilePathHash)
|
||||||
if (timestamp != null) {
|
if (timestamp != null) {
|
||||||
preference.edit {
|
preference.edit {
|
||||||
remove(oldFilePathHash)
|
remove(oldFilePathHash)
|
||||||
putString(file.absolutePath.base64(), timestamp)
|
putString(file.absolutePath.base64(), timestamp)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val returnIntent = Intent()
|
|
||||||
returnIntent.putExtra(RETURN_EXTRA_CREATED_FILE, path)
|
|
||||||
returnIntent.putExtra(RETURN_EXTRA_NAME, editName)
|
|
||||||
returnIntent.putExtra(RETURN_EXTRA_LONG_NAME, getLongName(fullPath, repoPath, editName))
|
|
||||||
|
|
||||||
if (shouldGeneratePassword) {
|
|
||||||
val directoryStructure = AutofillPreferences.directoryStructure(applicationContext)
|
|
||||||
val entry = passwordEntryFactory.create(lifecycleScope, content.encodeToByteArray())
|
|
||||||
returnIntent.putExtra(RETURN_EXTRA_PASSWORD, entry.password)
|
|
||||||
val username = entry.username ?: directoryStructure.getUsernameFor(file)
|
|
||||||
returnIntent.putExtra(RETURN_EXTRA_USERNAME, username)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (directoryInputLayout.isVisible && directoryInputLayout.isEnabled && oldFileName != null) {
|
|
||||||
val oldFile = File("$repoPath/${oldCategory?.trim('/')}/$oldFileName.gpg")
|
|
||||||
if (oldFile.path != file.path && !oldFile.delete()) {
|
|
||||||
setResult(RESULT_CANCELED)
|
|
||||||
MaterialAlertDialogBuilder(this@PasswordCreationActivity)
|
|
||||||
.setTitle(R.string.password_creation_file_fail_title)
|
|
||||||
.setMessage(getString(R.string.password_creation_file_delete_fail_message, oldFileName))
|
|
||||||
.setCancelable(false)
|
|
||||||
.setPositiveButton(android.R.string.ok) { _, _ -> finish() }
|
|
||||||
.show()
|
|
||||||
return@executeApiAsync
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val commitMessageRes = if (editing) R.string.git_commit_edit_text else R.string.git_commit_add_text
|
|
||||||
lifecycleScope.launch {
|
|
||||||
commitChange(resources.getString(commitMessageRes, getLongName(fullPath, repoPath, editName)))
|
|
||||||
.onSuccess {
|
|
||||||
setResult(RESULT_OK, returnIntent)
|
|
||||||
finish()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onFailure { e ->
|
|
||||||
if (e is IOException) {
|
val returnIntent = Intent()
|
||||||
e(e) { "Failed to write password file" }
|
returnIntent.putExtra(RETURN_EXTRA_CREATED_FILE, path)
|
||||||
setResult(RESULT_CANCELED)
|
returnIntent.putExtra(RETURN_EXTRA_NAME, editName)
|
||||||
MaterialAlertDialogBuilder(this@PasswordCreationActivity)
|
returnIntent.putExtra(RETURN_EXTRA_LONG_NAME, getLongName(fullPath, repoPath, editName))
|
||||||
.setTitle(getString(R.string.password_creation_file_fail_title))
|
|
||||||
.setMessage(getString(R.string.password_creation_file_write_fail_message))
|
if (shouldGeneratePassword) {
|
||||||
.setCancelable(false)
|
val directoryStructure = AutofillPreferences.directoryStructure(applicationContext)
|
||||||
.setPositiveButton(android.R.string.ok) { _, _ -> finish() }
|
val entry = passwordEntryFactory.create(lifecycleScope, content.encodeToByteArray())
|
||||||
.show()
|
returnIntent.putExtra(RETURN_EXTRA_PASSWORD, entry.password)
|
||||||
} else {
|
val username = entry.username ?: directoryStructure.getUsernameFor(file)
|
||||||
e(e)
|
returnIntent.putExtra(RETURN_EXTRA_USERNAME, username)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (directoryInputLayout.isVisible && directoryInputLayout.isEnabled && oldFileName != null) {
|
||||||
|
val oldFile = File("$repoPath/${oldCategory?.trim('/')}/$oldFileName.gpg")
|
||||||
|
if (oldFile.path != file.path && !oldFile.delete()) {
|
||||||
|
setResult(RESULT_CANCELED)
|
||||||
|
MaterialAlertDialogBuilder(this@PasswordCreationActivity)
|
||||||
|
.setTitle(R.string.password_creation_file_fail_title)
|
||||||
|
.setMessage(getString(R.string.password_creation_file_delete_fail_message, oldFileName))
|
||||||
|
.setCancelable(false)
|
||||||
|
.setPositiveButton(android.R.string.ok) { _, _ -> finish() }
|
||||||
|
.show()
|
||||||
|
return@runCatching
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val commitMessageRes = if (editing) R.string.git_commit_edit_text else R.string.git_commit_add_text
|
||||||
|
lifecycleScope.launch {
|
||||||
|
commitChange(resources.getString(commitMessageRes, getLongName(fullPath, repoPath, editName)))
|
||||||
|
.onSuccess {
|
||||||
|
setResult(RESULT_OK, returnIntent)
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> {
|
.onFailure { e ->
|
||||||
val sender = getUserInteractionRequestIntent(result)
|
if (e is IOException) {
|
||||||
userInteractionRequiredResult.launch(IntentSenderRequest.Builder(sender).build())
|
e(e) { "Failed to write password file" }
|
||||||
}
|
setResult(RESULT_CANCELED)
|
||||||
OpenPgpApi.RESULT_CODE_ERROR -> handleError(result)
|
MaterialAlertDialogBuilder(this@PasswordCreationActivity)
|
||||||
|
.setTitle(getString(R.string.password_creation_file_fail_title))
|
||||||
|
.setMessage(getString(R.string.password_creation_file_write_fail_message))
|
||||||
|
.setCancelable(false)
|
||||||
|
.setPositiveButton(android.R.string.ok) { _, _ -> finish() }
|
||||||
|
.show()
|
||||||
|
} else {
|
||||||
|
e(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> {
|
||||||
|
val sender = getUserInteractionRequestIntent(result)
|
||||||
|
userInteractionRequiredResult.launch(IntentSenderRequest.Builder(sender).build())
|
||||||
|
}
|
||||||
|
OpenPgpApi.RESULT_CODE_ERROR -> handleError(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,8 @@ All notable changes to this project will be documented in this file.
|
|||||||
|
|
||||||
- The library now requires Kotlin 1.5.0 configured with `kotlinOptions.languageVersion = "1.5"`.
|
- The library now requires Kotlin 1.5.0 configured with `kotlinOptions.languageVersion = "1.5"`.
|
||||||
|
|
||||||
|
- The synchronous and callback based APIs in `OpenPgpApi` have been removed in favor of a singular coroutines-based entrypoint.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- Fix build warning from undeclared unsigned type use.
|
- Fix build warning from undeclared unsigned type use.
|
||||||
|
@ -88,8 +88,7 @@ public final class me/msfjarvis/openpgpktx/util/OpenPgpApi {
|
|||||||
public static final field RESULT_SIGNATURE_MICALG Ljava/lang/String;
|
public static final field RESULT_SIGNATURE_MICALG Ljava/lang/String;
|
||||||
public static final field SERVICE_INTENT_2 Ljava/lang/String;
|
public static final field SERVICE_INTENT_2 Ljava/lang/String;
|
||||||
public fun <init> (Landroid/content/Context;Lorg/openintents/openpgp/IOpenPgpService2;)V
|
public fun <init> (Landroid/content/Context;Lorg/openintents/openpgp/IOpenPgpService2;)V
|
||||||
public final fun executeApi (Landroid/content/Intent;Ljava/io/InputStream;Ljava/io/OutputStream;)Landroid/content/Intent;
|
public final fun executeApi (Landroid/content/Intent;Ljava/io/InputStream;Ljava/io/OutputStream;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public final fun executeApiAsync (Landroid/content/Intent;Ljava/io/InputStream;Ljava/io/OutputStream;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class me/msfjarvis/openpgpktx/util/OpenPgpApi$Companion {
|
public final class me/msfjarvis/openpgpktx/util/OpenPgpApi$Companion {
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
#
|
#
|
||||||
|
|
||||||
VERSION_NAME=3.0.0
|
VERSION_NAME=4.0.0-SNAPSHOT
|
||||||
POM_ARTIFACT_ID=openpgp-ktx
|
POM_ARTIFACT_ID=openpgp-ktx
|
||||||
POM_NAME=openpgp-ktx
|
POM_NAME=openpgp-ktx
|
||||||
POM_DESCRIPTION=Reimplementation of OpenKeychain's integration library in Kotlin
|
POM_DESCRIPTION=Reimplementation of OpenKeychain's integration library in Kotlin
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
|
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@file:Suppress("Unused")
|
@file:Suppress("BlockingMethodInNonBlockingContext", "Unused")
|
||||||
|
|
||||||
package me.msfjarvis.openpgpktx.util
|
package me.msfjarvis.openpgpktx.util
|
||||||
|
|
||||||
@ -14,8 +14,6 @@ import java.io.IOException
|
|||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
import java.util.concurrent.atomic.AtomicInteger
|
import java.util.concurrent.atomic.AtomicInteger
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import org.openintents.openpgp.IOpenPgpService2
|
import org.openintents.openpgp.IOpenPgpService2
|
||||||
import org.openintents.openpgp.OpenPgpError
|
import org.openintents.openpgp.OpenPgpError
|
||||||
|
|
||||||
@ -23,17 +21,7 @@ public class OpenPgpApi(private val context: Context, private val service: IOpen
|
|||||||
|
|
||||||
private val pipeIdGen: AtomicInteger = AtomicInteger()
|
private val pipeIdGen: AtomicInteger = AtomicInteger()
|
||||||
|
|
||||||
public suspend fun executeApiAsync(
|
public suspend fun executeApi(data: Intent, inputStream: InputStream?, outputStream: OutputStream?): Intent {
|
||||||
data: Intent?,
|
|
||||||
inputStream: InputStream?,
|
|
||||||
outputStream: OutputStream?,
|
|
||||||
callback: (intent: Intent?) -> Unit
|
|
||||||
) {
|
|
||||||
val result = executeApi(data, inputStream, outputStream)
|
|
||||||
withContext(Dispatchers.Main) { callback.invoke(result) }
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun executeApi(data: Intent?, inputStream: InputStream?, outputStream: OutputStream?): Intent? {
|
|
||||||
var input: ParcelFileDescriptor? = null
|
var input: ParcelFileDescriptor? = null
|
||||||
return try {
|
return try {
|
||||||
if (inputStream != null) {
|
if (inputStream != null) {
|
||||||
@ -57,37 +45,38 @@ public class OpenPgpApi(private val context: Context, private val service: IOpen
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** InputStream and OutputStreams are always closed after operating on them! */
|
private suspend fun executeApi(
|
||||||
private fun executeApi(data: Intent?, input: ParcelFileDescriptor?, os: OutputStream?): Intent? {
|
data: Intent,
|
||||||
|
inputFd: ParcelFileDescriptor?,
|
||||||
|
outputStream: OutputStream?,
|
||||||
|
): Intent {
|
||||||
var output: ParcelFileDescriptor? = null
|
var output: ParcelFileDescriptor? = null
|
||||||
return try {
|
return try {
|
||||||
// always send version from client
|
// always send version from client
|
||||||
data?.putExtra(EXTRA_API_VERSION, API_VERSION)
|
data.putExtra(EXTRA_API_VERSION, API_VERSION)
|
||||||
val result: Intent
|
val result: Intent
|
||||||
var pumpThread: Thread? = null
|
|
||||||
var outputPipeId = 0
|
var outputPipeId = 0
|
||||||
if (os != null) {
|
if (outputStream != null) {
|
||||||
outputPipeId = pipeIdGen.incrementAndGet()
|
outputPipeId = pipeIdGen.incrementAndGet()
|
||||||
output = service.createOutputPipe(outputPipeId)
|
output = service.createOutputPipe(outputPipeId)
|
||||||
pumpThread = ParcelFileDescriptorUtil.pipeTo(os, output)
|
|
||||||
}
|
}
|
||||||
// blocks until result is ready
|
// blocks until result is ready
|
||||||
result = service.execute(data, input, outputPipeId)
|
result = service.execute(data, inputFd, outputPipeId)
|
||||||
// set class loader to current context to allow unparcelling
|
// set class loader to current context to allow unparcelling
|
||||||
// of OpenPgpError and OpenPgpSignatureResult
|
// of OpenPgpError and OpenPgpSignatureResult
|
||||||
// http://stackoverflow.com/a/3806769
|
// http://stackoverflow.com/a/3806769
|
||||||
result.setExtrasClassLoader(context.classLoader)
|
result.setExtrasClassLoader(context.classLoader)
|
||||||
// wait for ALL data being pumped from remote side
|
if (outputStream != null) {
|
||||||
pumpThread?.join()
|
ParcelFileDescriptorUtil.pipeTo(outputStream, output)
|
||||||
|
}
|
||||||
result
|
result
|
||||||
} catch (e: Exception) {
|
} catch (e: Throwable) {
|
||||||
Log.e(TAG, "Exception in executeApi call", e)
|
Log.e(TAG, "Exception in executeApi call", e)
|
||||||
val result = Intent()
|
val result = Intent()
|
||||||
result.putExtra(RESULT_CODE, RESULT_CODE_ERROR)
|
result.putExtra(RESULT_CODE, RESULT_CODE_ERROR)
|
||||||
result.putExtra(RESULT_ERROR, OpenPgpError(OpenPgpError.CLIENT_SIDE_ERROR, e.message))
|
result.putExtra(RESULT_ERROR, OpenPgpError(OpenPgpError.CLIENT_SIDE_ERROR, e.message))
|
||||||
result
|
result
|
||||||
} finally {
|
} finally {
|
||||||
// close() is required to halt the TransferThread
|
|
||||||
if (output != null) {
|
if (output != null) {
|
||||||
try {
|
try {
|
||||||
output.close()
|
output.close()
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
|
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
@file:Suppress("BlockingMethodInNonBlockingContext")
|
||||||
|
|
||||||
package me.msfjarvis.openpgpktx.util
|
package me.msfjarvis.openpgpktx.util
|
||||||
|
|
||||||
import android.os.ParcelFileDescriptor
|
import android.os.ParcelFileDescriptor
|
||||||
@ -11,30 +13,27 @@ import android.util.Log
|
|||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
internal object ParcelFileDescriptorUtil {
|
internal object ParcelFileDescriptorUtil {
|
||||||
|
|
||||||
private const val TAG = "PFDUtils"
|
private const val TAG = "PFDUtils"
|
||||||
|
|
||||||
@Throws(IOException::class)
|
internal suspend fun pipeFrom(inputStream: InputStream): ParcelFileDescriptor {
|
||||||
internal fun pipeFrom(inputStream: InputStream): ParcelFileDescriptor {
|
|
||||||
val pipe = ParcelFileDescriptor.createPipe()
|
val pipe = ParcelFileDescriptor.createPipe()
|
||||||
val readSide = pipe[0]
|
val readSide = pipe[0]
|
||||||
val writeSide = pipe[1]
|
val writeSide = pipe[1]
|
||||||
TransferThread(inputStream, AutoCloseOutputStream(writeSide)).start()
|
transferStreams(inputStream, AutoCloseOutputStream(writeSide))
|
||||||
return readSide
|
return readSide
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(IOException::class)
|
internal suspend fun pipeTo(outputStream: OutputStream, output: ParcelFileDescriptor?) {
|
||||||
internal fun pipeTo(outputStream: OutputStream, output: ParcelFileDescriptor?): TransferThread {
|
transferStreams(AutoCloseInputStream(output), outputStream)
|
||||||
val t = TransferThread(AutoCloseInputStream(output), outputStream)
|
|
||||||
t.start()
|
|
||||||
return t
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class TransferThread(val `in`: InputStream, private val out: OutputStream) : Thread("IPC Transfer Thread") {
|
private suspend fun transferStreams(`in`: InputStream, `out`: OutputStream) {
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
override fun run() {
|
|
||||||
val buf = ByteArray(4096)
|
val buf = ByteArray(4096)
|
||||||
var len: Int
|
var len: Int
|
||||||
try {
|
try {
|
||||||
@ -52,9 +51,5 @@ internal object ParcelFileDescriptorUtil {
|
|||||||
} catch (ignored: IOException) {}
|
} catch (ignored: IOException) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
|
||||||
isDaemon = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user