From 86e81ee7389db041b882e48592dcbf167a4f056c Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 24 Oct 2021 20:44:52 +0200 Subject: [PATCH] Refactoring --- .../eu/faircode/email/FragmentCompose.java | 81 +++++++++++-------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 64f08daf03..ca98800115 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -5753,42 +5753,10 @@ public class FragmentCompose extends FragmentBase { if (draft.ui_encrypt == null || EntityMessage.ENCRYPT_NONE.equals(draft.ui_encrypt)) { - if (recipients.size() > 0) { - if (pgpService != null && pgpService.isBound()) { - 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 { - 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); - args.putBoolean("remind_pgp", keyIds.length > 0); - } - } catch (Throwable ex) { - Log.w(ex); - } - } - - for (Address address : recipients) { - String email = ((InternetAddress) address).getAddress(); - List certs = db.certificate().getCertificateByEmail(email); - if (certs != null && certs.size() > 0) { - args.putBoolean("remind_smime", true); - break; - } - } - } + args.putBoolean("remind_pgp", hasPgpKey(context, recipients)); + args.putBoolean("remind_smime", hasSmimeKey(context, recipients)); } - if (TextUtils.isEmpty(draft.subject)) args.putBoolean("remind_subject", true); @@ -6418,6 +6386,51 @@ public class FragmentCompose extends FragmentBase { return -1; } + private boolean hasPgpKey(Context context, List
recipients) { + if (pgpService == null || !pgpService.isBound()) + return false; + if (recipients == null || recipients.size() == 0) + 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 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); + } + + return false; + } + + private boolean hasSmimeKey(Context context, List
recipients) { + if (recipients == null || recipients.size() == 0) + return false; + + DB db = DB.getInstance(context); + for (Address address : recipients) { + String email = ((InternetAddress) address).getAddress(); + List certs = db.certificate().getCertificateByEmail(email); + if (certs != null && certs.size() > 0) + return true; + } + + return false; + } + private AdapterView.OnItemSelectedListener identitySelected = new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) {