Added option to auto encrypt when all keys available

This commit is contained in:
M66B 2022-10-30 10:25:31 +01:00
parent aa8c674a5f
commit a53c33595f
4 changed files with 126 additions and 64 deletions

View File

@ -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<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() {
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<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() {

View File

@ -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));

View File

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

View File

@ -711,6 +711,7 @@
<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_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_undo_decrypt">Undo decryption on closing conversation</string>