Added automation rule

pull/153/head
M66B 6 years ago
parent cec0a2735b
commit c56787aa13

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

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

@ -416,6 +416,15 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/spAnswer" />
<TextView
android:id="@+id/tvAutomation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_rule_automation_hint"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cbCc" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpReady"
android:layout_width="0dp"
@ -442,6 +451,12 @@
android:layout_height="0dp"
app:constraint_referenced_ids="
tvAnswerIdentity,spIdent,tvAnswerTemplate,spAnswer,cbCc" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpAutomation"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="tvAutomation" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

@ -463,6 +463,8 @@
<string name="title_rule_cc">Reply to CC addresses</string>
<string name="title_rule_name_missing">Rule name missing</string>
<string name="title_rule_condition_missing">Condition missing</string>
<string name="title_rule_automation">Automation</string>
<string name="title_rule_automation_hint">This will send the intent \'%1$s\' with the extras \'%2$s\'</string>
<string name="title_action_seen">Mark read</string>
<string name="title_action_archive">Archive</string>

Loading…
Cancel
Save