From 6afefb61b72d03a5346d80d04afcd5f35aada9f8 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 20 Jun 2020 09:35:05 +0200 Subject: [PATCH] Allow rules for specific attachment mime types --- .../java/eu/faircode/email/EntityRule.java | 17 ++++++++++++- .../java/eu/faircode/email/FragmentRule.java | 12 ++++++++++ app/src/main/res/layout/fragment_rule.xml | 24 ++++++++++++++++++- app/src/main/res/values/strings.xml | 1 + 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/EntityRule.java b/app/src/main/java/eu/faircode/email/EntityRule.java index e42f65ac6b..7a98395d62 100644 --- a/app/src/main/java/eu/faircode/email/EntityRule.java +++ b/app/src/main/java/eu/faircode/email/EntityRule.java @@ -194,8 +194,23 @@ public class EntityRule { // Attachments if (jcondition.optBoolean("attachments")) { DB db = DB.getInstance(context); - if (db.attachment().getAttachments(message.id).size() == 0) + List attachments = db.attachment().getAttachments(message.id); + if (attachments.size() == 0) return false; + + if (jcondition.has("mimetype")) { + String mimeType = jcondition.getString("mimetype"); + + boolean found = false; + for (EntityAttachment attachment : attachments) + if (mimeType.equalsIgnoreCase(attachment.type)) { + found = true; + break; + } + + if (!found) + return false; + } } // Header diff --git a/app/src/main/java/eu/faircode/email/FragmentRule.java b/app/src/main/java/eu/faircode/email/FragmentRule.java index be8edc3242..0bd888d703 100644 --- a/app/src/main/java/eu/faircode/email/FragmentRule.java +++ b/app/src/main/java/eu/faircode/email/FragmentRule.java @@ -100,6 +100,7 @@ public class FragmentRule extends FragmentBase { private CheckBox cbSubject; private CheckBox cbAttachments; + private EditText etMimeType; private EditText etHeader; private CheckBox cbHeader; @@ -211,6 +212,7 @@ public class FragmentRule extends FragmentBase { cbSubject = view.findViewById(R.id.cbSubject); cbAttachments = view.findViewById(R.id.cbAttachments); + etMimeType = view.findViewById(R.id.etMimeType); etHeader = view.findViewById(R.id.etHeader); cbHeader = view.findViewById(R.id.cbHeader); @@ -291,6 +293,13 @@ public class FragmentRule extends FragmentBase { } }); + cbAttachments.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { + etMimeType.setEnabled(isChecked); + } + }); + adapterDay = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, new ArrayList()); adapterDay.setDropDownViewResource(R.layout.spinner_item1_dropdown); spScheduleDayStart.setAdapter(adapterDay); @@ -737,6 +746,8 @@ public class FragmentRule extends FragmentBase { cbSubject.setChecked(jsubject != null && jsubject.getBoolean("regex")); cbAttachments.setChecked(jcondition.optBoolean("attachments")); + etMimeType.setText(jcondition.optString("mimetype")); + etMimeType.setEnabled(cbAttachments.isChecked()); etHeader.setText(jheader == null ? null : jheader.getString("value")); cbHeader.setChecked(jheader != null && jheader.getBoolean("regex")); @@ -1037,6 +1048,7 @@ public class FragmentRule extends FragmentBase { } jcondition.put("attachments", cbAttachments.isChecked()); + jcondition.put("mimetype", etMimeType.getText().toString().trim()); String header = etHeader.getText().toString(); if (!TextUtils.isEmpty(header)) { diff --git a/app/src/main/res/layout/fragment_rule.xml b/app/src/main/res/layout/fragment_rule.xml index 190623eedb..f7729b2f6c 100644 --- a/app/src/main/res/layout/fragment_rule.xml +++ b/app/src/main/res/layout/fragment_rule.xml @@ -319,6 +319,28 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/vSeparatorAttachments" /> + + + + + app:layout_constraintTop_toBottomOf="@+id/etMimeType" /> Recipient contains Subject contains Has attachments + Mime type Header contains Time between Regex