Added option to auto encrypt when all keys available

pull/209/head
M66B 2 years ago
parent aa8c674a5f
commit a53c33595f

@ -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.setMaxLines(Integer.MAX_VALUE);
etTo.setHorizontallyScrolling(false); etTo.setHorizontallyScrolling(false);
etTo.setOnTouchListener(onTouchListener); etTo.setOnTouchListener(onTouchListener);
etTo.setOnLongClickListener(longClickListener); etTo.setOnLongClickListener(longClickListener);
etTo.setOnFocusChangeListener(focusListener);
etCc.setMaxLines(Integer.MAX_VALUE); etCc.setMaxLines(Integer.MAX_VALUE);
etCc.setHorizontallyScrolling(false); etCc.setHorizontallyScrolling(false);
etCc.setOnTouchListener(onTouchListener); etCc.setOnTouchListener(onTouchListener);
etCc.setOnLongClickListener(longClickListener); etCc.setOnLongClickListener(longClickListener);
etCc.setOnFocusChangeListener(focusListener);
etBcc.setMaxLines(Integer.MAX_VALUE); etBcc.setMaxLines(Integer.MAX_VALUE);
etBcc.setHorizontallyScrolling(false); etBcc.setHorizontallyScrolling(false);
etBcc.setOnTouchListener(onTouchListener); etBcc.setOnTouchListener(onTouchListener);
etBcc.setOnLongClickListener(longClickListener); etBcc.setOnLongClickListener(longClickListener);
etBcc.setOnFocusChangeListener(focusListener);
etSubject.setMaxLines(Integer.MAX_VALUE); etSubject.setMaxLines(Integer.MAX_VALUE);
etSubject.setHorizontallyScrolling(false); etSubject.setHorizontallyScrolling(false);
@ -1526,6 +1537,95 @@ public class FragmentCompose extends FragmentBase {
}.execute(FragmentCompose.this, args, "compose:contact"); }.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<Integer>() {
@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<Address> 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() { private void onReferenceEdit() {
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(getContext(), getViewLifecycleOwner(), ibReferenceEdit); PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(getContext(), getViewLifecycleOwner(), ibReferenceEdit);
@ -7201,67 +7301,6 @@ public class FragmentCompose extends FragmentBase {
updateEncryption(null); 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<Integer>() {
@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() { private ActivityBase.IKeyPressedListener onKeyPressedListener = new ActivityBase.IKeyPressedListener() {

@ -86,6 +86,7 @@ public class FragmentOptionsEncryption extends FragmentBase
private ImageButton ibInfo; private ImageButton ibInfo;
private SwitchCompat swSign; private SwitchCompat swSign;
private SwitchCompat swEncrypt; private SwitchCompat swEncrypt;
private SwitchCompat swEncryptAuto;
private SwitchCompat swAutoDecrypt; private SwitchCompat swAutoDecrypt;
private SwitchCompat swAutoUndoDecrypt; private SwitchCompat swAutoUndoDecrypt;
@ -115,7 +116,8 @@ public class FragmentOptionsEncryption extends FragmentBase
static final int REQUEST_IMPORT_CERTIFICATE = 1; static final int REQUEST_IMPORT_CERTIFICATE = 1;
private final static String[] RESET_OPTIONS = new String[]{ 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", "openpgp_provider", "autocrypt", "autocrypt_mutual", "encrypt_subject",
"sign_algo_smime", "encrypt_algo_smime", "check_certificate" "sign_algo_smime", "encrypt_algo_smime", "check_certificate"
}; };
@ -135,6 +137,7 @@ public class FragmentOptionsEncryption extends FragmentBase
ibInfo = view.findViewById(R.id.ibInfo); ibInfo = view.findViewById(R.id.ibInfo);
swSign = view.findViewById(R.id.swSign); swSign = view.findViewById(R.id.swSign);
swEncrypt = view.findViewById(R.id.swEncrypt); swEncrypt = view.findViewById(R.id.swEncrypt);
swEncryptAuto = view.findViewById(R.id.swEncryptAuto);
swAutoDecrypt = view.findViewById(R.id.swAutoDecrypt); swAutoDecrypt = view.findViewById(R.id.swAutoDecrypt);
swAutoUndoDecrypt = view.findViewById(R.id.swAutoUndoDecrypt); 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() { swAutoDecrypt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
@ -606,6 +616,7 @@ public class FragmentOptionsEncryption extends FragmentBase
swSign.setChecked(prefs.getBoolean("sign_default", false)); swSign.setChecked(prefs.getBoolean("sign_default", false));
swEncrypt.setChecked(prefs.getBoolean("encrypt_default", false)); swEncrypt.setChecked(prefs.getBoolean("encrypt_default", false));
swSign.setEnabled(!swEncrypt.isChecked()); swSign.setEnabled(!swEncrypt.isChecked());
swEncryptAuto.setChecked(prefs.getBoolean("encrypt_auto", false));
swAutoDecrypt.setChecked(prefs.getBoolean("auto_decrypt", false)); swAutoDecrypt.setChecked(prefs.getBoolean("auto_decrypt", false));
swAutoUndoDecrypt.setChecked(prefs.getBoolean("auto_undecrypt", false)); swAutoUndoDecrypt.setChecked(prefs.getBoolean("auto_undecrypt", false));

@ -108,6 +108,17 @@
app:layout_constraintTop_toBottomOf="@id/swSign" app:layout_constraintTop_toBottomOf="@id/swSign"
app:switchPadding="12dp" /> app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swEncryptAuto"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_advanced_encrypt_auto"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swEncrypt"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat <androidx.appcompat.widget.SwitchCompat
android:id="@+id/swAutoDecrypt" android:id="@+id/swAutoDecrypt"
android:layout_width="0dp" android:layout_width="0dp"
@ -116,7 +127,7 @@
android:text="@string/title_advanced_auto_decrypt" android:text="@string/title_advanced_auto_decrypt"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swEncrypt" app:layout_constraintTop_toBottomOf="@id/swEncryptAuto"
app:switchPadding="12dp" /> app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat <androidx.appcompat.widget.SwitchCompat
@ -454,11 +465,11 @@
android:drawableStart="@drawable/twotone_warning_24" android:drawableStart="@drawable/twotone_warning_24"
android:drawableEnd="@drawable/twotone_warning_24" android:drawableEnd="@drawable/twotone_warning_24"
android:drawablePadding="6dp" android:drawablePadding="6dp"
app:drawableTint="?attr/colorWarning"
android:gravity="center" android:gravity="center"
android:text="@string/title_advanced_caption_debug" android:text="@string/title_advanced_caption_debug"
android:textAppearance="@style/TextAppearance.AppCompat.Large" android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textStyle="bold" android:textStyle="bold"
app:drawableTint="?attr/colorWarning"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />

@ -711,6 +711,7 @@
<string name="title_advanced_sign_default">Sign by default</string> <string name="title_advanced_sign_default">Sign by default</string>
<string name="title_advanced_encrypt_default">Encrypt by default</string> <string name="title_advanced_encrypt_default">Encrypt by default</string>
<string name="title_advanced_encrypt_auto">Automatically encrypt when all public keys are available</string>
<string name="title_advanced_auto_decrypt">Automatically decrypt messages</string> <string name="title_advanced_auto_decrypt">Automatically decrypt messages</string>
<string name="title_advanced_auto_undo_decrypt">Undo decryption on closing conversation</string> <string name="title_advanced_auto_undo_decrypt">Undo decryption on closing conversation</string>

Loading…
Cancel
Save