Refactoring

pull/194/merge
M66B 3 years ago
parent 8a5a79ef41
commit 38cbfc713d

@ -3149,27 +3149,12 @@ public class FragmentCompose extends FragmentBase {
result.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, identity.sign_key);
} else {
// Call OpenPGP
OpenPgpServiceConnection pgpService = null;
try {
pgpService = PgpHelper.getConnection(context);
if (!pgpService.isBound())
throw new OperationCanceledException();
Log.i("Executing " + data.getAction());
Log.logExtras(data);
OpenPgpApi api = new OpenPgpApi(context, pgpService.getService());
result = api.executeApi(data, new FileInputStream(input), new FileOutputStream(output));
} finally {
if (pgpService != null && pgpService.isBound())
pgpService.unbindFromService();
}
result = PgpHelper.execute(context, data, new FileInputStream(input), new FileOutputStream(output));
}
// Process result
try {
int resultCode = result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
Log.i("Result action=" + data.getAction() + " code=" + resultCode);
Log.logExtras(data);
switch (resultCode) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
// Attach key, signed/encrypted data
@ -6450,12 +6435,6 @@ public class FragmentCompose extends FragmentBase {
if (recipients == null || recipients.size() == 0)
return false;
OpenPgpServiceConnection pgpService = null;
try {
pgpService = PgpHelper.getConnection(context, MAX_PGP_BIND_DELAY);
if (!pgpService.isBound())
return false;
String[] userIds = new String[recipients.size()];
for (int i = 0; i < recipients.size(); i++) {
InternetAddress recipient = (InternetAddress) recipients.get(i);
@ -6466,20 +6445,17 @@ public class FragmentCompose extends FragmentBase {
intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, userIds);
try {
OpenPgpApi api = new OpenPgpApi(context, pgpService.getService());
Intent result = api.executeApi(intent, (InputStream) null, (OutputStream) null);
Intent result = PgpHelper.execute(context, intent, null, null, MAX_PGP_BIND_DELAY);
int resultCode = result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
if (resultCode == OpenPgpApi.RESULT_CODE_SUCCESS) {
long[] keyIds = result.getLongArrayExtra(OpenPgpApi.EXTRA_KEY_IDS);
return (keyIds.length > 0);
}
} catch (OperationCanceledException ignored) {
// Do nothing
} catch (Throwable ex) {
Log.w(ex);
}
} finally {
if (pgpService != null && pgpService.isBound())
pgpService.unbindFromService();
}
return false;
}

@ -7092,28 +7092,8 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
Intent result;
try {
// Decrypt message
Log.i("Executing " + data.getAction());
Log.logExtras(data);
// Call OpenPGP
OpenPgpServiceConnection pgpService = null;
try {
pgpService = PgpHelper.getConnection(context);
if (!pgpService.isBound())
throw new OperationCanceledException();
Log.i("Executing " + data.getAction());
Log.logExtras(data);
OpenPgpApi api = new OpenPgpApi(context, pgpService.getService());
result = api.executeApi(data, in, out);
} finally {
if (pgpService != null && pgpService.isBound())
pgpService.unbindFromService();
}
result = PgpHelper.execute(context, data, in, out);
int resultCode = result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR);
Log.i("Result action=" + data.getAction() + " code=" + resultCode);
Log.logExtras(data);
switch (resultCode) {
case OpenPgpApi.RESULT_CODE_SUCCESS:
if (out != null)

@ -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 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);
}
}
static OpenPgpServiceConnection getConnection(Context context, long timeout) {
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;
}

Loading…
Cancel
Save