From 4bf24baf5e5a521190db2d4de70646318f55e415 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 9 Oct 2021 17:39:34 +0200 Subject: [PATCH] Bulk block senders --- .../eu/faircode/email/FragmentMessages.java | 77 +++++++++++++++---- app/src/main/res/layout/dialog_ask_spam.xml | 38 +++++++++ 2 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 app/src/main/res/layout/dialog_ask_spam.xml diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index cede5be5bc..88e3d4b1e3 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -3249,16 +3249,16 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. onActionRaw(); return true; } else if (itemId == R.string.title_folder_inbox) { - onActionMoveSelection(EntityFolder.INBOX); + onActionMoveSelection(EntityFolder.INBOX, false); return true; } else if (itemId == R.string.title_archive) { - onActionMoveSelection(EntityFolder.ARCHIVE); + onActionMoveSelection(EntityFolder.ARCHIVE, false); return true; } else if (itemId == R.string.title_spam) { onActionJunkSelection(); return true; } else if (itemId == R.string.title_trash) { - onActionMoveSelection(EntityFolder.TRASH); + onActionMoveSelection(EntityFolder.TRASH, false); return true; } else if (itemId == R.string.title_delete_permanently) { onActionDeleteSelection( @@ -3634,21 +3634,19 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. } private void onActionJunkSelection() { - int count = selectionTracker.getSelection().size(); - Bundle aargs = new Bundle(); - aargs.putString("question", getResources() - .getQuantityString(R.plurals.title_ask_spam, count, count)); + aargs.putInt("count", selectionTracker.getSelection().size()); - FragmentDialogAsk ask = new FragmentDialogAsk(); + FragmentDialogAskSpam ask = new FragmentDialogAskSpam(); ask.setArguments(aargs); ask.setTargetFragment(FragmentMessages.this, REQUEST_MESSAGES_JUNK); ask.show(getParentFragmentManager(), "messages:junk"); } - private void onActionMoveSelection(final String type) { + private void onActionMoveSelection(final String type, boolean block) { Bundle args = new Bundle(); args.putString("type", type); + args.putBoolean("block", block); args.putLongArray("ids", getSelection()); new SimpleTask>() { @@ -3683,7 +3681,8 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. if (sourceFolder == null || sourceFolder.read_only) continue; - result.add(new MessageTarget(context, threaded, account, sourceFolder, account, targetFolder)); + result.add(new MessageTarget(context, threaded, account, sourceFolder, account, targetFolder) + .setBlock(block)); } } @@ -5503,7 +5502,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. for (MessageTarget target : targets) if (message.id.equals(target.id)) { Log.i("Eval thread target id=" + target.id); - if (!target.isAccross()) { + if (!target.isAcross()) { found = true; if (target.targetFolder.id == folder) count++; @@ -5517,7 +5516,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. } for (MessageTarget target : mt) - if (!target.isAccross() && target.targetFolder.id == folder && + if (!target.isAcross() && target.targetFolder.id == folder && (removed == null || !removed.contains(target.id))) count++; @@ -5922,6 +5921,12 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. EntityOperation.queue(context, message, EntityOperation.COPY, target.targetFolder.id); else EntityOperation.queue(context, message, EntityOperation.MOVE, target.targetFolder.id); + + if (target.block && + EntityFolder.JUNK.equals(target.targetFolder.type)) + EntityContact.update(context, + message.account, message.from, + EntityContact.TYPE_JUNK, message.received); } db.setTransactionSuccessful(); @@ -6087,7 +6092,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. private static String getNames(ArrayList result, boolean dest) { boolean across = false; for (MessageTarget target : result) - if (target.isAccross()) + if (target.isAcross()) across = true; Map nameCount = new HashMap<>(); @@ -6663,7 +6668,8 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. break; case REQUEST_MESSAGES_JUNK: if (resultCode == RESULT_OK) - onActionMoveSelection(EntityFolder.JUNK); + onActionMoveSelection(EntityFolder.JUNK, + data.getBundleExtra("args").getBoolean("block")); break; case REQUEST_ASKED_MOVE: if (resultCode == RESULT_OK && data != null) @@ -8044,7 +8050,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. private void onMoveAskAcross(final ArrayList result) { boolean across = false; for (MessageTarget target : result) - if (target.isAccross()) { + if (target.isAcross()) { across = true; break; } @@ -8715,6 +8721,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. Account targetAccount; Folder targetFolder; boolean copy; + boolean block; MessageTarget(Context context, EntityMessage message, EntityAccount sourceAccount, EntityFolder sourceFolder, @@ -8731,7 +8738,12 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. return this; } - boolean isAccross() { + MessageTarget setBlock(boolean block) { + this.block = block; + return this; + } + + boolean isAcross() { return (sourceAccount.id != targetAccount.id); } @@ -8742,6 +8754,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. targetAccount = (Account) in.readSerializable(); targetFolder = (Folder) in.readSerializable(); copy = (in.readInt() != 0); + block = (in.readInt() != 0); } @Override @@ -8752,6 +8765,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. dest.writeSerializable(targetAccount); dest.writeSerializable(targetFolder); dest.writeInt(copy ? 1 : 0); + dest.writeInt(block ? 1 : 0); } @Override @@ -8798,6 +8812,37 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. } } + public static class FragmentDialogAskSpam extends FragmentDialogBase { + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + Bundle args = getArguments(); + int count = args.getInt("count"); + + String text = getResources().getQuantityString(R.plurals.title_ask_spam, count, count); + + final Context context = getContext(); + View dview = LayoutInflater.from(context).inflate(R.layout.dialog_ask_spam, null); + TextView tvMessage = dview.findViewById(R.id.tvMessage); + CheckBox cbBlockSender = dview.findViewById(R.id.cbBlockSender); + + tvMessage.setText(text); + cbBlockSender.setChecked(true); + + return new AlertDialog.Builder(context) + .setView(dview) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + getArguments().putBoolean("block", cbBlockSender.isChecked()); + sendResult(Activity.RESULT_OK); + } + }) + .setNegativeButton(android.R.string.cancel, null) + .create(); + } + } + public static class FragmentDialogReporting extends FragmentDialogBase { @NonNull @Override diff --git a/app/src/main/res/layout/dialog_ask_spam.xml b/app/src/main/res/layout/dialog_ask_spam.xml new file mode 100644 index 0000000000..597de0490f --- /dev/null +++ b/app/src/main/res/layout/dialog_ask_spam.xml @@ -0,0 +1,38 @@ + + + + + + + + + + \ No newline at end of file