diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 2c610f98da..b29cd48c07 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -498,20 +498,31 @@ public class FragmentCompose extends FragmentBase { } }; + View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + if (!hasFocus) + updateEncryption((EntityIdentity) spIdentity.getSelectedItem()); + } + }; + etTo.setMaxLines(Integer.MAX_VALUE); etTo.setHorizontallyScrolling(false); etTo.setOnTouchListener(onTouchListener); etTo.setOnLongClickListener(longClickListener); + etTo.setOnFocusChangeListener(focusListener); etCc.setMaxLines(Integer.MAX_VALUE); etCc.setHorizontallyScrolling(false); etCc.setOnTouchListener(onTouchListener); etCc.setOnLongClickListener(longClickListener); + etCc.setOnFocusChangeListener(focusListener); etBcc.setMaxLines(Integer.MAX_VALUE); etBcc.setHorizontallyScrolling(false); etBcc.setOnTouchListener(onTouchListener); etBcc.setOnLongClickListener(longClickListener); + etBcc.setOnFocusChangeListener(focusListener); etSubject.setMaxLines(Integer.MAX_VALUE); etSubject.setHorizontallyScrolling(false); @@ -1526,6 +1537,95 @@ public class FragmentCompose extends FragmentBase { }.execute(FragmentCompose.this, args, "compose:contact"); } + private void updateEncryption(EntityIdentity identity) { + if (identity == null) + return; + + Bundle args = new Bundle(); + args.putLong("id", working); + args.putLong("identity", identity.id); + args.putString("to", etTo.getText().toString().trim()); + args.putString("cc", etCc.getText().toString().trim()); + args.putString("bcc", etBcc.getText().toString().trim()); + + new SimpleTask() { + @Override + protected Integer onExecute(Context context, Bundle args) { + long id = args.getLong("id"); + long iid = args.getLong("identity"); + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean sign_default = prefs.getBoolean("sign_default", false); + boolean encrypt_default = prefs.getBoolean("encrypt_default", false); + boolean encrypt_auto = prefs.getBoolean("encrypt_auto", false); + + DB db = DB.getInstance(context); + + EntityMessage draft = db.message().getMessage(id); + if (draft == null) + return null; + + if (draft.dsn != null && !EntityMessage.DSN_NONE.equals(draft.dsn)) + return null; + + EntityIdentity identity = db.identity().getIdentity(iid); + if (identity == null) + return draft.ui_encrypt; + + if (identity.encrypt == 0 && !Helper.isOpenKeychainInstalled(context)) + draft.ui_encrypt = null; + else if (encrypt_default || identity.encrypt_default) + draft.ui_encrypt = (identity.encrypt == 0 + ? EntityMessage.PGP_SIGNENCRYPT + : EntityMessage.SMIME_SIGNENCRYPT); + else if (sign_default || identity.sign_default) + draft.ui_encrypt = (identity.encrypt == 0 + ? EntityMessage.PGP_SIGNONLY + : EntityMessage.SMIME_SIGNONLY); + else + draft.ui_encrypt = null; + + if (encrypt_auto) + try { + InternetAddress[] to = MessageHelper.parseAddresses(context, args.getString("to")); + InternetAddress[] cc = MessageHelper.parseAddresses(context, args.getString("cc")); + InternetAddress[] bcc = MessageHelper.parseAddresses(context, args.getString("bcc")); + + List
recipients = new ArrayList<>(); + if (to != null) + recipients.addAll(Arrays.asList(to)); + if (cc != null) + recipients.addAll(Arrays.asList(cc)); + if (bcc != null) + recipients.addAll(Arrays.asList(bcc)); + + if (identity.encrypt == 0 && PgpHelper.hasPgpKey(context, recipients, true)) + draft.ui_encrypt = EntityMessage.PGP_SIGNENCRYPT; + else if (identity.encrypt == 1 && SmimeHelper.hasSmimeKey(context, recipients, true)) + draft.ui_encrypt = EntityMessage.SMIME_SIGNENCRYPT; + else + draft.ui_encrypt = null; + } catch (Throwable ex) { + Log.w(ex); + } + + db.message().setMessageUiEncrypt(draft.id, draft.ui_encrypt); + + return draft.ui_encrypt; + } + + @Override + protected void onExecuted(Bundle args, Integer encrypt) { + FragmentCompose.this.encrypt = encrypt; + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Log.unexpectedError(getParentFragmentManager(), ex); + } + }.setExecutor(executor).execute(FragmentCompose.this, args, "compose:identity"); + } + private void onReferenceEdit() { PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(getContext(), getViewLifecycleOwner(), ibReferenceEdit); @@ -7201,67 +7301,6 @@ public class FragmentCompose extends FragmentBase { updateEncryption(null); } - - private void updateEncryption(EntityIdentity identity) { - if (identity == null) - return; - - Bundle args = new Bundle(); - args.putLong("id", working); - args.putLong("identity", identity.id); - - new SimpleTask() { - @Override - protected Integer onExecute(Context context, Bundle args) { - long id = args.getLong("id"); - long iid = args.getLong("identity"); - - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - boolean sign_default = prefs.getBoolean("sign_default", false); - boolean encrypt_default = prefs.getBoolean("encrypt_default", false); - - DB db = DB.getInstance(context); - - EntityMessage draft = db.message().getMessage(id); - if (draft == null) - return null; - - if (draft.dsn != null && !EntityMessage.DSN_NONE.equals(draft.dsn)) - return null; - - EntityIdentity identity = db.identity().getIdentity(iid); - if (identity == null) - return draft.ui_encrypt; - - if (identity.encrypt == 0 && !Helper.isOpenKeychainInstalled(context)) - draft.ui_encrypt = null; - else if (encrypt_default || identity.encrypt_default) - draft.ui_encrypt = (identity.encrypt == 0 - ? EntityMessage.PGP_SIGNENCRYPT - : EntityMessage.SMIME_SIGNENCRYPT); - else if (sign_default || identity.sign_default) - draft.ui_encrypt = (identity.encrypt == 0 - ? EntityMessage.PGP_SIGNONLY - : EntityMessage.SMIME_SIGNONLY); - else - draft.ui_encrypt = null; - - db.message().setMessageUiEncrypt(draft.id, draft.ui_encrypt); - - return draft.ui_encrypt; - } - - @Override - protected void onExecuted(Bundle args, Integer encrypt) { - FragmentCompose.this.encrypt = encrypt; - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Log.unexpectedError(getParentFragmentManager(), ex); - } - }.setExecutor(executor).execute(FragmentCompose.this, args, "compose:identity"); - } }; private ActivityBase.IKeyPressedListener onKeyPressedListener = new ActivityBase.IKeyPressedListener() { diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsEncryption.java b/app/src/main/java/eu/faircode/email/FragmentOptionsEncryption.java index 53e49fc616..2607391dcf 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsEncryption.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsEncryption.java @@ -86,6 +86,7 @@ public class FragmentOptionsEncryption extends FragmentBase private ImageButton ibInfo; private SwitchCompat swSign; private SwitchCompat swEncrypt; + private SwitchCompat swEncryptAuto; private SwitchCompat swAutoDecrypt; private SwitchCompat swAutoUndoDecrypt; @@ -115,7 +116,8 @@ public class FragmentOptionsEncryption extends FragmentBase static final int REQUEST_IMPORT_CERTIFICATE = 1; private final static String[] RESET_OPTIONS = new String[]{ - "sign_default", "encrypt_default", "auto_decrypt", "auto_undecrypt", + "sign_default", "encrypt_default", "encrypt_auto", + "auto_decrypt", "auto_undecrypt", "openpgp_provider", "autocrypt", "autocrypt_mutual", "encrypt_subject", "sign_algo_smime", "encrypt_algo_smime", "check_certificate" }; @@ -135,6 +137,7 @@ public class FragmentOptionsEncryption extends FragmentBase ibInfo = view.findViewById(R.id.ibInfo); swSign = view.findViewById(R.id.swSign); swEncrypt = view.findViewById(R.id.swEncrypt); + swEncryptAuto = view.findViewById(R.id.swEncryptAuto); swAutoDecrypt = view.findViewById(R.id.swAutoDecrypt); swAutoUndoDecrypt = view.findViewById(R.id.swAutoUndoDecrypt); @@ -213,6 +216,13 @@ public class FragmentOptionsEncryption extends FragmentBase } }); + swEncryptAuto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { + prefs.edit().putBoolean("encrypt_auto", checked).apply(); + } + }); + swAutoDecrypt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { @@ -606,6 +616,7 @@ public class FragmentOptionsEncryption extends FragmentBase swSign.setChecked(prefs.getBoolean("sign_default", false)); swEncrypt.setChecked(prefs.getBoolean("encrypt_default", false)); swSign.setEnabled(!swEncrypt.isChecked()); + swEncryptAuto.setChecked(prefs.getBoolean("encrypt_auto", false)); swAutoDecrypt.setChecked(prefs.getBoolean("auto_decrypt", false)); swAutoUndoDecrypt.setChecked(prefs.getBoolean("auto_undecrypt", false)); diff --git a/app/src/main/res/layout/fragment_options_encryption.xml b/app/src/main/res/layout/fragment_options_encryption.xml index 96ce2f36c0..28752a7055 100644 --- a/app/src/main/res/layout/fragment_options_encryption.xml +++ b/app/src/main/res/layout/fragment_options_encryption.xml @@ -108,6 +108,17 @@ app:layout_constraintTop_toBottomOf="@id/swSign" app:switchPadding="12dp" /> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3115e1b1ca..f73f2f3ff2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -711,6 +711,7 @@ Sign by default Encrypt by default + Automatically encrypt when all public keys are available Automatically decrypt messages Undo decryption on closing conversation