From ebc40787ac53503640d462075dae6020ad639ce4 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 2 Oct 2019 18:56:39 +0200 Subject: [PATCH] Added encryption toggle to compose action bar --- .../eu/faircode/email/FragmentCompose.java | 45 +++++++++++++++++-- app/src/main/res/menu/menu_compose.xml | 6 +++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index cef024e899..f5e48de728 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -193,6 +193,7 @@ public class FragmentCompose extends FragmentBase { private boolean prefix_once = false; private boolean monospaced = false; + private boolean encrypt = false; private boolean media = true; private boolean compact = false; @@ -910,6 +911,7 @@ public class FragmentCompose extends FragmentBase { public void onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); + menu.findItem(R.id.menu_encrypt).setVisible(state == State.LOADED); menu.findItem(R.id.menu_zoom).setVisible(state == State.LOADED); menu.findItem(R.id.menu_media).setVisible(state == State.LOADED); menu.findItem(R.id.menu_compact).setVisible(state == State.LOADED); @@ -918,6 +920,7 @@ public class FragmentCompose extends FragmentBase { menu.findItem(R.id.menu_answer).setVisible(state == State.LOADED); menu.findItem(R.id.menu_send).setVisible(state == State.LOADED); + menu.findItem(R.id.menu_encrypt).setEnabled(!busy); menu.findItem(R.id.menu_zoom).setEnabled(!busy); menu.findItem(R.id.menu_media).setEnabled(!busy); menu.findItem(R.id.menu_compact).setEnabled(!busy); @@ -926,6 +929,7 @@ public class FragmentCompose extends FragmentBase { menu.findItem(R.id.menu_answer).setEnabled(!busy); menu.findItem(R.id.menu_send).setEnabled(!busy); + menu.findItem(R.id.menu_encrypt).setIcon(encrypt ? R.drawable.baseline_lock_24 : R.drawable.baseline_no_encryption_24); menu.findItem(R.id.menu_media).setChecked(media); menu.findItem(R.id.menu_compact).setChecked(compact); } @@ -937,6 +941,9 @@ public class FragmentCompose extends FragmentBase { if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) onExit(); return true; + case R.id.menu_encrypt: + onMenuEncrypt(); + return true; case R.id.menu_zoom: onMenuZoom(); return true; @@ -977,6 +984,33 @@ public class FragmentCompose extends FragmentBase { }); } + private void onMenuEncrypt() { + encrypt = !encrypt; + getActivity().invalidateOptionsMenu(); + + Bundle args = new Bundle(); + args.putLong("id", working); + args.putBoolean("encrypt", encrypt); + + new SimpleTask() { + @Override + protected Void onExecute(Context context, Bundle args) { + long id = args.getLong("id"); + boolean encrypt = args.getBoolean("encrypt"); + + DB db = DB.getInstance(context); + db.message().setMessageEncrypt(id, encrypt); + + return null; + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(getFragmentManager(), ex); + } + }.execute(this, args, "compose:encrypt"); + } + private void onMenuZoom() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); boolean compact = prefs.getBoolean("compact", false); @@ -2399,11 +2433,13 @@ public class FragmentCompose extends FragmentBase { @Override protected void onExecuted(Bundle args, final DraftData data) { - working = data.draft.id; - final String action = getArguments().getString("action"); Log.i("Loaded draft id=" + data.draft.id + " action=" + action); + working = data.draft.id; + encrypt = (data.draft.encrypt != null && data.draft.encrypt); + getActivity().invalidateOptionsMenu(); + // Show identities AdapterIdentitySelect iadapter = new AdapterIdentitySelect(getContext(), data.identities); spIdentity.setAdapter(iadapter); @@ -2436,8 +2472,6 @@ public class FragmentCompose extends FragmentBase { bottom_navigation.getMenu().findItem(R.id.action_redo).setVisible( data.draft.revision != null && !data.draft.revision.equals(data.draft.revisions)); - getActivity().invalidateOptionsMenu(); - if (args.getBoolean("incomplete")) Snackbar.make(view, R.string.title_attachments_incomplete, Snackbar.LENGTH_LONG).show(); @@ -2485,6 +2519,9 @@ public class FragmentCompose extends FragmentBase { if (draft == null || draft.ui_hide) finish(); else { + encrypt = (draft.encrypt != null && draft.encrypt); + getActivity().invalidateOptionsMenu(); + Log.i("Draft content=" + draft.content); if (draft.content && state == State.NONE) showDraft(draft); diff --git a/app/src/main/res/menu/menu_compose.xml b/app/src/main/res/menu/menu_compose.xml index b44d6cb09b..8927f79a82 100644 --- a/app/src/main/res/menu/menu_compose.xml +++ b/app/src/main/res/menu/menu_compose.xml @@ -1,6 +1,12 @@ + +