Added automation rule

This commit is contained in:
M66B
2019-03-17 12:49:10 +00:00
parent cec0a2735b
commit c56787aa13
4 changed files with 68 additions and 0 deletions

View File

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

View File

@@ -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<Action> adapterAction;
private ArrayAdapter<EntityFolder> 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 {