Added menu to execute rule on existing messages

This commit is contained in:
M66B 2019-06-09 14:48:43 +02:00
parent ddf6e3ce35
commit cf35d058ad
2 changed files with 65 additions and 0 deletions

View File

@ -30,6 +30,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.PopupMenu;
@ -39,11 +40,15 @@ import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.mail.MessagingException;
public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
private Context context;
private LifecycleOwner owner;
@ -168,6 +173,8 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
popupMenu.getMenu().add(Menu.NONE, R.string.title_rule_enabled, 1, R.string.title_rule_enabled)
.setCheckable(true).setChecked(rule.enabled);
popupMenu.getMenu().add(Menu.NONE, R.string.title_rule_execute, 2, R.string.title_rule_execute)
.setEnabled(Helper.isPro(context));
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
@ -176,6 +183,9 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
case R.string.title_rule_enabled:
onActionEnabled(!item.isChecked());
return true;
case R.string.title_rule_execute:
onActionExecute();
return true;
default:
return false;
}
@ -204,6 +214,58 @@ public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
}
}.execute(context, owner, args, "rule:enable");
}
private void onActionExecute() {
Bundle args = new Bundle();
args.putLong("id", rule.id);
new SimpleTask<Integer>() {
@Override
protected Integer onExecute(Context context, Bundle args) throws JSONException, MessagingException, IOException {
long id = args.getLong("id");
DB db = DB.getInstance(context);
EntityRule rule = db.rule().getRule(id);
if (rule == null)
return 0;
JSONObject jcondition = new JSONObject(rule.condition);
JSONObject jheader = jcondition.optJSONObject("header");
if (jheader != null)
return 0;
int applied = 0;
List<Long> ids = db.message().getMessageIdsByFolder(rule.folder);
for (long mid : ids)
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(mid);
if (rule.matches(context, message, null)) {
rule.execute(context, message);
applied++;
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return applied;
}
@Override
protected void onExecuted(Bundle args, Integer applied) {
Toast.makeText(context, context.getString(R.string.title_rule_applied, applied), Toast.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(context, owner, ex);
}
}.execute(context, owner, args, "rule:execute");
}
});
popupMenu.show();

View File

@ -574,6 +574,9 @@
<string name="title_rule_condition_missing">Condition missing</string>
<string name="title_rule_automation_hint">This will send the intent \'%1$s\' with the extras \'%2$s\'</string>
<string name="title_rule_execute">Execute now</string>
<string name="title_rule_applied">Affected messages: %1$d</string>
<string name="title_rule_check">Check</string>
<string name="title_rule_no_headers">Header conditions cannot be checked</string>
<string name="title_rule_matched">Matching messages</string>