diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index d6f20454f8..5bd353bd88 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -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,35 +6435,26 @@ public class FragmentCompose extends FragmentBase { if (recipients == null || recipients.size() == 0) return false; - OpenPgpServiceConnection pgpService = null; + String[] userIds = new String[recipients.size()]; + for (int i = 0; i < recipients.size(); i++) { + InternetAddress recipient = (InternetAddress) recipients.get(i); + userIds[i] = recipient.getAddress(); + } + + Intent intent = new Intent(OpenPgpApi.ACTION_GET_KEY_IDS); + intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, userIds); + 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); - userIds[i] = recipient.getAddress(); + 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); } - - Intent intent = new Intent(OpenPgpApi.ACTION_GET_KEY_IDS); - intent.putExtra(OpenPgpApi.EXTRA_USER_IDS, userIds); - - try { - OpenPgpApi api = new OpenPgpApi(context, pgpService.getService()); - Intent result = api.executeApi(intent, (InputStream) null, (OutputStream) null); - 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 (Throwable ex) { - Log.w(ex); - } - } finally { - if (pgpService != null && pgpService.isBound()) - pgpService.unbindFromService(); + } catch (OperationCanceledException ignored) { + // Do nothing + } catch (Throwable ex) { + Log.w(ex); } return false; diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index b2f68c5c5b..6d35259ae8 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -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) diff --git a/app/src/main/java/eu/faircode/email/PgpHelper.java b/app/src/main/java/eu/faircode/email/PgpHelper.java index 72fe6b81b3..276187203a 100644 --- a/app/src/main/java/eu/faircode/email/PgpHelper.java +++ b/app/src/main/java/eu/faircode/email/PgpHelper.java @@ -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; }