From c56787aa136e3ca6805fc2db8f9f359bc1c297e2 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 17 Mar 2019 12:49:10 +0000 Subject: [PATCH] Added automation rule --- .../java/eu/faircode/email/EntityRule.java | 37 +++++++++++++++++++ .../java/eu/faircode/email/FragmentRule.java | 14 +++++++ app/src/main/res/layout/fragment_rule.xml | 15 ++++++++ app/src/main/res/values/strings.xml | 2 + 4 files changed, 68 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/EntityRule.java b/app/src/main/java/eu/faircode/email/EntityRule.java index 71fcbdba29..4319a7b3dd 100644 --- a/app/src/main/java/eu/faircode/email/EntityRule.java +++ b/app/src/main/java/eu/faircode/email/EntityRule.java @@ -20,6 +20,9 @@ package eu.faircode.email; */ import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.net.Uri; import org.json.JSONException; @@ -40,6 +43,7 @@ import javax.mail.MessagingException; import javax.mail.internet.InternetAddress; import androidx.annotation.NonNull; +import androidx.core.content.ContextCompat; import androidx.room.Entity; import androidx.room.ForeignKey; import androidx.room.Index; @@ -81,6 +85,12 @@ public class EntityRule { static final int TYPE_UNSEEN = 2; static final int TYPE_MOVE = 3; static final int TYPE_ANSWER = 4; + static final int TYPE_AUTOMATION = 5; + + static final String ACTION_AUTOMATION = BuildConfig.APPLICATION_ID + ".AUTOMATION"; + static final String EXTRA_RULE = "rule"; + static final String EXTRA_SENDER = "sender"; + static final String EXTRA_SUBJECT = "subject"; boolean matches(Context context, EntityMessage message, Message imessage) throws MessagingException { try { @@ -205,6 +215,9 @@ public class EntityRule { case TYPE_ANSWER: onActionAnswer(context, db, message, jargs); break; + case TYPE_AUTOMATION: + onActionAutomation(context, db, message, jargs); + break; } } catch (JSONException ex) { Log.e(ex); @@ -260,6 +273,30 @@ public class EntityRule { EntityOperation.queue(context, db, reply, EntityOperation.SEND); } + private void onActionAutomation(Context context, DB db, EntityMessage message, JSONObject jargs) throws JSONException { + String sender = (message.from == null || message.from.length == 0 + ? null : ((InternetAddress) message.from[0]).getAddress()); + + Intent automation = new Intent(ACTION_AUTOMATION); + automation.putExtra(EXTRA_RULE, name); + automation.putExtra(EXTRA_SENDER, sender); + automation.putExtra(EXTRA_SUBJECT, message.subject); + + PackageManager pm = context.getPackageManager(); + ResolveInfo ri = pm.resolveService(automation, 0); + if (ri == null) + Log.w("Unable to resolve " + automation); + else { + automation.setPackage(ri.serviceInfo.packageName); + Log.i("Sending " + automation); + try { + ContextCompat.startForegroundService(context, automation); + } catch (Throwable ex) { + Log.e(ex); + } + } + } + @Override public boolean equals(Object obj) { if (obj instanceof EntityRule) { diff --git a/app/src/main/java/eu/faircode/email/FragmentRule.java b/app/src/main/java/eu/faircode/email/FragmentRule.java index 404cd8a1f5..3ce4a7c957 100644 --- a/app/src/main/java/eu/faircode/email/FragmentRule.java +++ b/app/src/main/java/eu/faircode/email/FragmentRule.java @@ -89,6 +89,7 @@ public class FragmentRule extends FragmentBase { private Spinner spIdent; private Spinner spAnswer; private CheckBox cbCc; + private TextView tvAutomation; private BottomNavigationView bottom_navigation; private ContentLoadingProgressBar pbWait; @@ -96,6 +97,7 @@ public class FragmentRule extends FragmentBase { private Group grpReady; private Group grpMove; private Group grpAnswer; + private Group grpAutomation; private ArrayAdapter adapterAction; private ArrayAdapter adapterTarget; @@ -154,6 +156,7 @@ public class FragmentRule extends FragmentBase { spIdent = view.findViewById(R.id.spIdent); spAnswer = view.findViewById(R.id.spAnswer); cbCc = view.findViewById(R.id.cbCc); + tvAutomation = view.findViewById(R.id.tvAutomation); bottom_navigation = view.findViewById(R.id.bottom_navigation); pbWait = view.findViewById(R.id.pbWait); @@ -161,6 +164,7 @@ public class FragmentRule extends FragmentBase { grpReady = view.findViewById(R.id.grpReady); grpMove = view.findViewById(R.id.grpMove); grpAnswer = view.findViewById(R.id.grpAnswer); + grpAutomation = view.findViewById(R.id.grpAutomation); ivSender.setOnClickListener(new View.OnClickListener() { @Override @@ -205,6 +209,7 @@ public class FragmentRule extends FragmentBase { actions.add(new Action(EntityRule.TYPE_UNSEEN, getString(R.string.title_unseen))); actions.add(new Action(EntityRule.TYPE_MOVE, getString(R.string.title_move))); actions.add(new Action(EntityRule.TYPE_ANSWER, getString(R.string.title_answer_reply))); + actions.add(new Action(EntityRule.TYPE_AUTOMATION, getString(R.string.title_rule_automation))); adapterAction.addAll(actions); spAction.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @@ -237,6 +242,13 @@ public class FragmentRule extends FragmentBase { tvActionRemark.setVisibility(View.GONE); + tvAutomation.setText(getString(R.string.title_rule_automation_hint, + EntityRule.ACTION_AUTOMATION, + TextUtils.join(",", new String[]{ + EntityRule.EXTRA_RULE, + EntityRule.EXTRA_SENDER, + EntityRule.EXTRA_SUBJECT}))); + bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { @@ -259,6 +271,7 @@ public class FragmentRule extends FragmentBase { grpReady.setVisibility(View.GONE); grpMove.setVisibility(View.GONE); grpAnswer.setVisibility(View.GONE); + grpAutomation.setVisibility(View.GONE); pbWait.setVisibility(View.VISIBLE); return view; @@ -607,6 +620,7 @@ public class FragmentRule extends FragmentBase { private void showActionParameters(int type) { grpMove.setVisibility(type == EntityRule.TYPE_MOVE ? View.VISIBLE : View.GONE); grpAnswer.setVisibility(type == EntityRule.TYPE_ANSWER ? View.VISIBLE : View.GONE); + grpAutomation.setVisibility(type == EntityRule.TYPE_AUTOMATION ? View.VISIBLE : View.GONE); } private JSONObject getCondition() throws JSONException { diff --git a/app/src/main/res/layout/fragment_rule.xml b/app/src/main/res/layout/fragment_rule.xml index 5c6c40575c..af0f96530e 100644 --- a/app/src/main/res/layout/fragment_rule.xml +++ b/app/src/main/res/layout/fragment_rule.xml @@ -416,6 +416,15 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/spAnswer" /> + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3baa6c6d5c..651d2f91c5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -463,6 +463,8 @@ Reply to CC addresses Rule name missing Condition missing + Automation + This will send the intent \'%1$s\' with the extras \'%2$s\' Mark read Archive