aboutsummaryrefslogtreecommitdiffstats
path: root/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpApi.kt
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2021-04-30 23:22:01 +0530
committerGitHub <noreply@github.com>2021-04-30 23:22:01 +0530
commitf834c754e68eaea8cb32e1368f61f36eaf573aaa (patch)
tree5a4e77697eb1a28e0bd328d019c2440063b63c62 /openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpApi.kt
parent4880e1db276b4cf2ec29eeffbd38539ca643b4be (diff)
downloadHardenedPass-f834c754e68eaea8cb32e1368f61f36eaf573aaa.tar.gz
HardenedPass-f834c754e68eaea8cb32e1368f61f36eaf573aaa.zip
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>
Diffstat (limited to 'openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpApi.kt')
-rw-r--r--openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpApi.kt39
1 files changed, 14 insertions, 25 deletions
diff --git a/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpApi.kt b/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpApi.kt
index 42ee94bb..c855f619 100644
--- a/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpApi.kt
+++ b/openpgp-ktx/src/main/java/me/msfjarvis/openpgpktx/util/OpenPgpApi.kt
@@ -2,7 +2,7 @@
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
-@file:Suppress("Unused")
+@file:Suppress("BlockingMethodInNonBlockingContext", "Unused")
package me.msfjarvis.openpgpktx.util
@@ -14,8 +14,6 @@ import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import java.util.concurrent.atomic.AtomicInteger
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.withContext
import org.openintents.openpgp.IOpenPgpService2
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()
- public suspend fun executeApiAsync(
- 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? {
+ public suspend fun executeApi(data: Intent, inputStream: InputStream?, outputStream: OutputStream?): Intent {
var input: ParcelFileDescriptor? = null
return try {
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 fun executeApi(data: Intent?, input: ParcelFileDescriptor?, os: OutputStream?): Intent? {
+ private suspend fun executeApi(
+ data: Intent,
+ inputFd: ParcelFileDescriptor?,
+ outputStream: OutputStream?,
+ ): Intent {
var output: ParcelFileDescriptor? = null
return try {
// always send version from client
- data?.putExtra(EXTRA_API_VERSION, API_VERSION)
+ data.putExtra(EXTRA_API_VERSION, API_VERSION)
val result: Intent
- var pumpThread: Thread? = null
var outputPipeId = 0
- if (os != null) {
+ if (outputStream != null) {
outputPipeId = pipeIdGen.incrementAndGet()
output = service.createOutputPipe(outputPipeId)
- pumpThread = ParcelFileDescriptorUtil.pipeTo(os, output)
}
// 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
// of OpenPgpError and OpenPgpSignatureResult
// http://stackoverflow.com/a/3806769
result.setExtrasClassLoader(context.classLoader)
- // wait for ALL data being pumped from remote side
- pumpThread?.join()
+ if (outputStream != null) {
+ ParcelFileDescriptorUtil.pipeTo(outputStream, output)
+ }
result
- } catch (e: Exception) {
+ } catch (e: Throwable) {
Log.e(TAG, "Exception in executeApi call", e)
val result = Intent()
result.putExtra(RESULT_CODE, RESULT_CODE_ERROR)
result.putExtra(RESULT_ERROR, OpenPgpError(OpenPgpError.CLIENT_SIDE_ERROR, e.message))
result
} finally {
- // close() is required to halt the TransferThread
if (output != null) {
try {
output.close()