mirror of
https://github.com/M66B/FairEmail.git
synced 2026-03-31 22:26:06 +02:00
Refactoring
This commit is contained in:
@@ -19,11 +19,21 @@ package eu.faircode.email;
|
||||
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import static org.openintents.openpgp.util.OpenPgpApi.RESULT_CODE_ERROR;
|
||||
import static org.openintents.openpgp.util.OpenPgpApi.RESULT_CODE_SUCCESS;
|
||||
import static org.openintents.openpgp.util.OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.OperationCanceledException;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import org.openintents.openpgp.IOpenPgpService2;
|
||||
import org.openintents.openpgp.util.OpenPgpApi;
|
||||
import org.openintents.openpgp.util.OpenPgpServiceConnection;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -32,11 +42,43 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
public class PgpHelper {
|
||||
private static final long CONNECT_TIMEOUT = 5000L;
|
||||
|
||||
static OpenPgpServiceConnection getConnection(Context context) {
|
||||
return getConnection(context, CONNECT_TIMEOUT);
|
||||
static Intent execute(Context context, Intent data, InputStream is, OutputStream os) {
|
||||
return execute(context, data, is, os, CONNECT_TIMEOUT);
|
||||
}
|
||||
|
||||
static OpenPgpServiceConnection getConnection(Context context, long timeout) {
|
||||
static Intent execute(Context context, Intent data, InputStream is, OutputStream os, long timeout) {
|
||||
OpenPgpServiceConnection pgpService = null;
|
||||
try {
|
||||
pgpService = getConnection(context, timeout);
|
||||
|
||||
Log.i("Executing " + data.getAction() +
|
||||
" " + TextUtils.join(", ", Log.getExtras(data.getExtras())));
|
||||
OpenPgpApi api = new OpenPgpApi(context, pgpService.getService());
|
||||
Intent result = api.executeApi(data, is, os);
|
||||
int resultCode = result.getIntExtra(OpenPgpApi.RESULT_CODE, RESULT_CODE_ERROR);
|
||||
Log.i("Result action=" + data.getAction() + " code=" + getResultName(resultCode) +
|
||||
" " + TextUtils.join(", ", Log.getExtras(result.getExtras())));
|
||||
return result;
|
||||
} finally {
|
||||
if (pgpService != null && pgpService.isBound())
|
||||
pgpService.unbindFromService();
|
||||
}
|
||||
}
|
||||
|
||||
private static String getResultName(int code) {
|
||||
switch (code) {
|
||||
case RESULT_CODE_ERROR:
|
||||
return "error";
|
||||
case RESULT_CODE_SUCCESS:
|
||||
return "success";
|
||||
case RESULT_CODE_USER_INTERACTION_REQUIRED:
|
||||
return "interaction";
|
||||
default:
|
||||
return Integer.toString(code);
|
||||
}
|
||||
}
|
||||
|
||||
private static OpenPgpServiceConnection getConnection(Context context, long timeout) {
|
||||
final String pkg = Helper.getOpenKeychainPackage(context);
|
||||
Log.i("PGP binding to " + pkg + " timeout=" + timeout);
|
||||
|
||||
@@ -64,6 +106,8 @@ public class PgpHelper {
|
||||
}
|
||||
|
||||
Log.i("PGP bound=" + pgpService.isBound());
|
||||
if (!pgpService.isBound())
|
||||
throw new OperationCanceledException();
|
||||
|
||||
return pgpService;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user