FairEmail/app/src/main/java/eu/faircode/email/AdapterRule.java

791 lines
33 KiB
Java
Raw Normal View History

2019-01-17 13:29:35 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2019-01-17 13:29:35 +00:00
*/
import android.content.Context;
import android.content.Intent;
2023-08-14 15:11:24 +00:00
import android.content.SharedPreferences;
2020-01-21 09:52:34 +00:00
import android.graphics.Typeface;
2019-05-03 19:30:11 +00:00
import android.os.Bundle;
2020-01-21 09:52:34 +00:00
import android.text.SpannableString;
2022-03-20 11:10:10 +00:00
import android.text.SpannableStringBuilder;
2019-05-02 14:32:24 +00:00
import android.text.TextUtils;
2020-01-21 10:41:07 +00:00
import android.text.style.RelativeSizeSpan;
2020-01-21 09:52:34 +00:00
import android.text.style.StyleSpan;
2019-01-17 13:29:35 +00:00
import android.view.LayoutInflater;
2019-05-03 19:30:11 +00:00
import android.view.Menu;
import android.view.MenuItem;
2019-01-17 13:29:35 +00:00
import android.view.View;
import android.view.ViewGroup;
2019-01-18 11:26:02 +00:00
import android.widget.ImageView;
2019-01-17 13:29:35 +00:00
import android.widget.TextView;
2020-02-02 19:22:41 +00:00
import android.widget.Toast;
2019-01-17 13:29:35 +00:00
import androidx.annotation.NonNull;
2019-05-03 19:30:11 +00:00
import androidx.appcompat.widget.PopupMenu;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
2019-01-17 13:29:35 +00:00
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
2019-01-17 13:29:35 +00:00
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
2023-08-14 15:11:24 +00:00
import androidx.preference.PreferenceManager;
2019-01-17 13:29:35 +00:00
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
import org.json.JSONException;
2019-05-02 11:48:02 +00:00
import org.json.JSONObject;
import java.io.IOException;
2023-02-19 10:09:31 +00:00
import java.text.Collator;
2020-12-24 07:30:03 +00:00
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
2023-08-14 15:11:24 +00:00
import java.util.Arrays;
2023-02-19 10:09:31 +00:00
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
2023-02-19 10:09:31 +00:00
import java.util.Locale;
import javax.mail.MessagingException;
2019-01-17 13:29:35 +00:00
public class AdapterRule extends RecyclerView.Adapter<AdapterRule.ViewHolder> {
private Fragment parentFragment;
2019-01-17 13:29:35 +00:00
private Context context;
private LifecycleOwner owner;
private LayoutInflater inflater;
2023-08-14 15:11:24 +00:00
private boolean debug;
2020-12-24 07:30:03 +00:00
private DateFormat DF;
private NumberFormat NF = NumberFormat.getNumberInstance();
2020-07-01 06:57:17 +00:00
private int protocol = -1;
2023-02-19 10:09:31 +00:00
private String sort;
2020-09-06 07:21:38 +00:00
private String search = null;
private List<TupleRuleEx> all = new ArrayList<>();
private List<TupleRuleEx> selected = new ArrayList<>();
2019-01-17 13:29:35 +00:00
2019-05-03 19:30:11 +00:00
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
2019-03-15 11:53:22 +00:00
private View view;
2022-12-23 13:06:25 +00:00
private ImageView ivDaily;
2023-08-14 15:11:24 +00:00
private ImageView ivHeaders;
private ImageView ivBody;
2019-01-17 13:29:35 +00:00
private TextView tvName;
2019-01-18 08:23:53 +00:00
private TextView tvOrder;
2019-01-18 11:26:02 +00:00
private ImageView ivStop;
2019-05-02 14:32:24 +00:00
private TextView tvCondition;
2019-05-02 11:48:02 +00:00
private TextView tvAction;
2020-12-24 07:30:03 +00:00
private TextView tvLastApplied;
private TextView tvApplied;
2019-01-17 13:29:35 +00:00
2019-05-03 19:30:11 +00:00
private TwoStateOwner powner = new TwoStateOwner(owner, "RulePopup");
2019-01-17 13:29:35 +00:00
ViewHolder(View itemView) {
super(itemView);
2019-03-15 11:53:22 +00:00
view = itemView.findViewById(R.id.clItem);
2022-12-23 13:06:25 +00:00
ivDaily = itemView.findViewById(R.id.ivDaily);
2023-08-14 15:11:24 +00:00
ivHeaders = itemView.findViewById(R.id.ivHeaders);
ivBody = itemView.findViewById(R.id.ivBody);
2019-01-17 13:29:35 +00:00
tvName = itemView.findViewById(R.id.tvName);
2019-01-18 08:23:53 +00:00
tvOrder = itemView.findViewById(R.id.tvOrder);
2019-01-18 11:26:02 +00:00
ivStop = itemView.findViewById(R.id.ivStop);
2019-05-02 14:32:24 +00:00
tvCondition = itemView.findViewById(R.id.tvCondition);
2019-05-02 11:48:02 +00:00
tvAction = itemView.findViewById(R.id.tvAction);
2020-12-24 07:30:03 +00:00
tvLastApplied = itemView.findViewById(R.id.tvLastApplied);
tvApplied = itemView.findViewById(R.id.tvApplied);
2019-01-17 13:29:35 +00:00
}
private void wire() {
2019-03-15 11:53:22 +00:00
view.setOnClickListener(this);
2019-05-03 19:30:11 +00:00
view.setOnLongClickListener(this);
2019-01-17 13:29:35 +00:00
}
private void unwire() {
2019-03-15 11:53:22 +00:00
view.setOnClickListener(null);
2019-05-03 19:30:11 +00:00
view.setOnLongClickListener(null);
2019-01-17 13:29:35 +00:00
}
2019-01-17 21:41:00 +00:00
private void bindTo(TupleRuleEx rule) {
2023-08-14 15:11:24 +00:00
boolean needsHeaders = (debug || BuildConfig.DEBUG) &&
EntityRule.needsHeaders(Arrays.asList(rule));
boolean needsBody = (debug || BuildConfig.DEBUG) &&
EntityRule.needsBody(Arrays.asList(rule));
2019-03-15 11:53:22 +00:00
view.setActivated(!rule.enabled);
2022-12-23 13:06:25 +00:00
ivDaily.setVisibility(rule.daily ? View.VISIBLE : View.GONE);
2023-08-14 15:11:24 +00:00
ivHeaders.setVisibility(needsHeaders ? View.VISIBLE : View.GONE);
ivBody.setVisibility(needsBody ? View.VISIBLE : View.GONE);
2019-01-17 13:29:35 +00:00
tvName.setText(rule.name);
2019-01-18 08:23:53 +00:00
tvOrder.setText(Integer.toString(rule.order));
2019-12-10 19:37:00 +00:00
ivStop.setVisibility(rule.stop ? View.VISIBLE : View.INVISIBLE);
2019-05-02 11:48:02 +00:00
2019-05-02 14:32:24 +00:00
try {
2022-04-18 12:23:30 +00:00
List<Condition> conditions = new ArrayList<>();
2019-05-02 14:32:24 +00:00
JSONObject jcondition = new JSONObject(rule.condition);
if (jcondition.has("sender"))
2022-04-18 12:23:30 +00:00
conditions.add(new Condition(context.getString(R.string.title_rule_sender),
jcondition.getJSONObject("sender").optString("value"),
jcondition.getJSONObject("sender").optBoolean("regex")));
2019-05-02 14:32:24 +00:00
if (jcondition.has("recipient"))
2022-04-18 12:23:30 +00:00
conditions.add(new Condition(context.getString(R.string.title_rule_recipient),
jcondition.getJSONObject("recipient").optString("value"),
jcondition.getJSONObject("recipient").optBoolean("regex")));
2019-05-02 14:32:24 +00:00
if (jcondition.has("subject"))
2022-04-18 12:23:30 +00:00
conditions.add(new Condition(context.getString(R.string.title_rule_subject),
jcondition.getJSONObject("subject").optString("value"),
jcondition.getJSONObject("subject").optBoolean("regex")));
2022-03-22 10:23:05 +00:00
if (jcondition.optBoolean("attachments"))
2022-04-18 12:23:30 +00:00
conditions.add(new Condition(context.getString(R.string.title_rule_attachments),
null, null));
2019-05-02 14:32:24 +00:00
if (jcondition.has("header"))
2022-04-18 12:23:30 +00:00
conditions.add(new Condition(context.getString(R.string.title_rule_header),
jcondition.getJSONObject("header").optString("value"),
jcondition.getJSONObject("header").optBoolean("regex")));
if (jcondition.has("body"))
2022-04-18 12:23:30 +00:00
conditions.add(new Condition(context.getString(R.string.title_rule_body),
jcondition.getJSONObject("body").optString("value"),
jcondition.getJSONObject("body").optBoolean("regex")));
2021-03-27 19:02:54 +00:00
if (jcondition.has("date"))
2022-04-18 12:23:30 +00:00
conditions.add(new Condition(context.getString(R.string.title_rule_time_abs),
null, null));
2019-08-04 17:14:53 +00:00
if (jcondition.has("schedule"))
2022-04-18 12:23:30 +00:00
conditions.add(new Condition(context.getString(R.string.title_rule_time_rel),
null, null));
2022-03-20 11:10:10 +00:00
SpannableStringBuilder ssb = new SpannableStringBuilderEx();
2022-04-18 12:23:30 +00:00
for (Condition condition : conditions) {
2022-03-20 11:10:10 +00:00
if (ssb.length() > 0)
ssb.append("\n");
2022-04-18 12:23:30 +00:00
ssb.append(condition.name);
if (!TextUtils.isEmpty(condition.condition)) {
2022-03-20 11:10:10 +00:00
ssb.append(" \"");
int start = ssb.length();
2022-04-18 12:23:30 +00:00
ssb.append(condition.condition);
2022-03-20 11:10:10 +00:00
ssb.setSpan(new StyleSpan(Typeface.ITALIC), start, ssb.length(), 0);
ssb.append("\"");
2022-04-18 12:23:30 +00:00
if (Boolean.TRUE.equals(condition.regex))
ssb.append(" (*)");
2022-03-20 11:10:10 +00:00
}
}
tvCondition.setText(ssb);
2019-05-02 14:32:24 +00:00
} catch (Throwable ex) {
2023-12-11 21:04:19 +00:00
tvCondition.setText(new ThrowableWrapper(ex).getSafeMessage());
2019-05-02 14:32:24 +00:00
}
2019-05-02 11:48:02 +00:00
try {
JSONObject jaction = new JSONObject(rule.action);
2022-04-01 10:32:53 +00:00
String to = null;
2022-04-04 08:56:57 +00:00
boolean resend = false;
2019-05-02 11:48:02 +00:00
int type = jaction.getInt("type");
2022-03-22 12:22:27 +00:00
if (type == EntityRule.TYPE_SNOOZE) {
int duration = jaction.optInt("duration", 0);
2022-04-04 08:56:57 +00:00
setAction(getAction(type), Integer.toString(duration));
2022-03-22 12:22:27 +00:00
} else if (type == EntityRule.TYPE_IMPORTANCE) {
int importance = jaction.optInt("value");
String value = null;
if (importance == EntityMessage.PRIORITIY_LOW)
value = context.getString(R.string.title_importance_low);
else if (importance == EntityMessage.PRIORITIY_NORMAL)
value = context.getString(R.string.title_importance_normal);
else if (importance == EntityMessage.PRIORITIY_HIGH)
value = context.getString(R.string.title_importance_high);
2022-04-04 08:56:57 +00:00
setAction(getAction(type), value);
2022-03-22 12:22:27 +00:00
} else if (type == EntityRule.TYPE_KEYWORD) {
2023-03-13 16:31:44 +00:00
boolean set = jaction.optBoolean("set", true);
setAction(getAction(type), (set ? "+" : "-") + jaction.optString("keyword"));
2022-04-01 10:32:53 +00:00
} else if (type == EntityRule.TYPE_ANSWER) {
to = jaction.optString("to");
2022-04-04 08:56:57 +00:00
if (!TextUtils.isEmpty(to)) {
resend = jaction.optBoolean("resend");
setAction(resend ? R.string.title_rule_resend : getAction(type), to);
}
2023-08-14 07:39:53 +00:00
} else if (type == EntityRule.TYPE_NOTES) {
String notes = jaction.getString("notes");
setAction(getAction(type), notes);
2023-10-02 16:45:50 +00:00
} else if (type == EntityRule.TYPE_URL) {
String url = jaction.getString("url");
2023-10-03 19:07:22 +00:00
String method = jaction.optString("method");
if (TextUtils.isEmpty(method))
method = "GET";
setAction(getAction(type), method + " " + url);
2022-03-22 12:22:27 +00:00
} else
2022-04-04 08:56:57 +00:00
setAction(getAction(type), null);
2022-03-22 12:22:27 +00:00
2022-04-01 10:32:53 +00:00
if (type == EntityRule.TYPE_MOVE || type == EntityRule.TYPE_COPY ||
(type == EntityRule.TYPE_ANSWER && TextUtils.isEmpty(to))) {
2022-03-22 10:19:31 +00:00
Bundle args = new Bundle();
args.putLong("id", rule.id);
args.putInt("type", type);
args.putLong("target", jaction.optLong("target", -1));
2022-03-22 12:22:27 +00:00
args.putLong("answer", jaction.optLong("answer", -1));
2022-03-22 10:19:31 +00:00
2022-03-22 12:22:27 +00:00
new SimpleTask<String>() {
2022-03-22 10:19:31 +00:00
@Override
2022-03-22 12:22:27 +00:00
protected String onExecute(Context context, Bundle args) throws Throwable {
2022-03-22 10:19:31 +00:00
DB db = DB.getInstance(context);
2022-03-22 12:22:27 +00:00
int type = args.getInt("type");
if (type == EntityRule.TYPE_MOVE || type == EntityRule.TYPE_COPY) {
long id = args.getLong("target");
EntityFolder folder = db.folder().getFolder(id);
return (folder == null ? null : folder.name);
} else if (type == EntityRule.TYPE_ANSWER) {
long id = args.getLong("answer");
EntityAnswer answer = db.answer().getAnswer(id);
return (answer == null ? null : answer.name);
} else
return null;
2022-03-22 10:19:31 +00:00
}
@Override
2022-03-22 12:22:27 +00:00
protected void onExecuted(Bundle args, String value) {
2022-03-22 10:19:31 +00:00
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
long id = args.getLong("id");
if (id != AdapterRule.this.getItemId(pos))
return;
2022-04-04 08:56:57 +00:00
setAction(getAction(args.getInt("type")), value);
2022-03-22 10:19:31 +00:00
}
@Override
protected void onException(Bundle args, Throwable ex) {
// Ignored
}
}.execute(context, owner, args, "rule:folder");
2019-05-02 11:48:02 +00:00
}
} catch (Throwable ex) {
2023-12-11 21:04:19 +00:00
tvAction.setText(new ThrowableWrapper(ex).getSafeMessage());
2019-05-02 11:48:02 +00:00
}
2022-03-22 12:22:27 +00:00
tvLastApplied.setText(rule.last_applied == null ? "-" : DF.format(rule.last_applied));
tvApplied.setText(NF.format(rule.applied));
2019-01-17 13:29:35 +00:00
}
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
2020-09-06 07:21:38 +00:00
TupleRuleEx rule = selected.get(pos);
2019-01-17 13:29:35 +00:00
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_EDIT_RULE)
2019-01-18 08:23:53 +00:00
.putExtra("id", rule.id)
.putExtra("account", rule.account)
2020-07-01 06:57:17 +00:00
.putExtra("folder", rule.folder)
.putExtra("protocol", protocol));
2019-01-17 13:29:35 +00:00
}
2019-05-03 19:30:11 +00:00
@Override
public boolean onLongClick(View v) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return false;
2020-09-06 07:21:38 +00:00
final TupleRuleEx rule = selected.get(pos);
2019-05-03 19:30:11 +00:00
PopupMenuLifecycle popupMenu = new PopupMenuLifecycle(context, powner, view);
2020-01-21 09:52:34 +00:00
SpannableString ss = new SpannableString(rule.name);
ss.setSpan(new StyleSpan(Typeface.ITALIC), 0, ss.length(), 0);
2020-01-21 10:41:07 +00:00
ss.setSpan(new RelativeSizeSpan(0.9f), 0, ss.length(), 0);
2020-01-21 09:52:34 +00:00
popupMenu.getMenu().add(Menu.NONE, 0, 0, ss).setEnabled(false);
2019-06-25 08:53:06 +00:00
2019-05-16 08:42:01 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_rule_enabled, 1, R.string.title_rule_enabled)
2019-05-03 19:30:11 +00:00
.setCheckable(true).setChecked(rule.enabled);
popupMenu.getMenu().add(Menu.NONE, R.string.title_rule_execute, 2, R.string.title_rule_execute)
2019-08-13 08:27:17 +00:00
.setEnabled(ActivityBilling.isPro(context));
popupMenu.getMenu().add(Menu.NONE, R.string.title_reset, 3, R.string.title_reset);
2023-04-14 10:00:43 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_rule_edit_group, 4, R.string.title_rule_edit_group);
2020-07-01 06:57:17 +00:00
if (protocol == EntityAccount.TYPE_IMAP) {
2023-04-14 08:07:33 +00:00
popupMenu.getMenu().add(Menu.NONE, R.string.title_move_to_folder, 5, R.string.title_move_to_folder);
popupMenu.getMenu().add(Menu.NONE, R.string.title_copy, 6, R.string.title_copy);
2020-07-01 06:57:17 +00:00
}
2019-05-03 19:30:11 +00:00
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
2021-02-05 10:15:02 +00:00
int itemId = item.getItemId();
if (itemId == R.string.title_rule_enabled) {
onActionEnabled(!item.isChecked());
return true;
} else if (itemId == R.string.title_rule_execute) {
onActionExecute();
return true;
} else if (itemId == R.string.title_reset) {
onActionReset();
return true;
2023-04-14 10:00:43 +00:00
} else if (itemId == R.string.title_rule_edit_group) {
2023-04-14 08:07:33 +00:00
onActionGroup();
return true;
2021-02-05 10:15:02 +00:00
} else if (itemId == R.string.title_move_to_folder) {
onActionMove();
return true;
} else if (itemId == R.string.title_copy) {
onActionCopy();
return true;
2019-05-03 19:30:11 +00:00
}
2021-02-05 10:15:02 +00:00
return false;
2019-05-03 19:30:11 +00:00
}
private void onActionEnabled(boolean enabled) {
Bundle args = new Bundle();
args.putLong("id", rule.id);
args.putBoolean("enabled", enabled);
new SimpleTask<Boolean>() {
@Override
protected Boolean onExecute(Context context, Bundle args) {
long id = args.getLong("id");
boolean enabled = args.getBoolean("enabled");
DB db = DB.getInstance(context);
db.rule().setRuleEnabled(id, enabled);
return enabled;
}
@Override
protected void onException(Bundle args, Throwable ex) {
2019-12-06 07:50:46 +00:00
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
2019-05-03 19:30:11 +00:00
}
}.execute(context, owner, args, "rule:enable");
}
private void onActionExecute() {
Bundle args = new Bundle();
args.putLong("id", rule.id);
new SimpleTask<Integer>() {
2022-04-28 08:57:13 +00:00
private Toast toast = null;
2020-02-02 19:22:41 +00:00
@Override
protected void onPreExecute(Bundle args) {
2022-04-28 08:57:13 +00:00
toast = ToastEx.makeText(context, R.string.title_executing, Toast.LENGTH_LONG);
toast.show();
}
@Override
protected void onPostExecute(Bundle args) {
if (toast != null)
toast.cancel();
2020-02-02 19:22:41 +00:00
}
@Override
protected Integer onExecute(Context context, Bundle args) throws JSONException, MessagingException, IOException {
long id = args.getLong("id");
DB db = DB.getInstance(context);
2021-04-14 18:13:15 +00:00
EntityRule rule = db.rule().getRule(id);
if (rule == null)
return 0;
2021-04-14 18:13:15 +00:00
List<Long> ids = db.message().getMessageIdsByFolder(rule.folder);
if (ids == null)
return 0;
2022-12-31 09:13:09 +00:00
// Check header conditions
for (long mid : ids) {
EntityMessage message = db.message().getMessage(mid);
if (message == null || message.ui_hide)
continue;
rule.matches(context, message, null, null);
}
int applied = 0;
for (long mid : ids)
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(mid);
2022-12-24 10:00:50 +00:00
if (message == null || message.ui_hide)
2019-10-16 14:57:43 +00:00
continue;
if (rule.matches(context, message, null, null))
2023-08-14 08:58:36 +00:00
if (rule.execute(context, message, null))
2019-07-16 06:25:30 +00:00
applied++;
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2021-03-27 15:03:58 +00:00
if (applied > 0)
ServiceSynchronize.eval(context, "rules/manual");
return applied;
}
@Override
protected void onExecuted(Bundle args, Integer applied) {
2021-04-14 18:13:15 +00:00
ToastEx.makeText(context,
context.getString(R.string.title_rule_applied, applied),
Toast.LENGTH_LONG).show();
}
2022-05-14 19:48:19 +00:00
@Override
protected void onDestroyed(Bundle args) {
2022-05-29 11:57:54 +00:00
if (toast != null) {
toast.cancel();
toast = null;
}
2022-05-14 19:48:19 +00:00
}
@Override
protected void onException(Bundle args, Throwable ex) {
2021-04-14 18:13:15 +00:00
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex, false);
}
}.execute(context, owner, args, "rule:execute");
}
2019-08-20 20:08:38 +00:00
private void onActionReset() {
Bundle args = new Bundle();
args.putLong("id", rule.id);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB db = DB.getInstance(context);
db.rule().resetRule(id);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
2019-12-06 07:50:46 +00:00
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
2021-05-13 15:49:12 +00:00
}.execute(context, owner, args, "rule:reset");
}
2023-04-14 08:07:33 +00:00
private void onActionGroup() {
Bundle args = new Bundle();
args.putLong("rule", rule.id);
args.putString("name", rule.group);
FragmentDialogRuleGroup fragment = new FragmentDialogRuleGroup();
fragment.setArguments(args);
fragment.setTargetFragment(parentFragment, FragmentRules.REQUEST_GROUP);
fragment.show(parentFragment.getParentFragmentManager(), "rule:group");
}
2019-09-08 07:54:03 +00:00
private void onActionMove() {
Bundle args = new Bundle();
2022-09-06 20:05:55 +00:00
args.putInt("icon", R.drawable.twotone_drive_file_move_24);
2019-09-08 07:54:03 +00:00
args.putString("title", context.getString(R.string.title_move_to_folder));
args.putLong("account", rule.account);
args.putLongArray("disabled", new long[]{rule.folder});
args.putLong("rule", rule.id);
2023-03-27 16:03:32 +00:00
FragmentDialogSelectFolder fragment = new FragmentDialogSelectFolder();
2019-09-08 07:54:03 +00:00
fragment.setArguments(args);
fragment.setTargetFragment(parentFragment, FragmentRules.REQUEST_MOVE);
2019-10-12 15:16:53 +00:00
fragment.show(parentFragment.getParentFragmentManager(), "rule:move");
2019-09-08 07:54:03 +00:00
}
2019-08-20 20:08:38 +00:00
private void onActionCopy() {
2023-03-31 06:43:11 +00:00
Bundle args = new Bundle();
args.putLong("rule", rule.id);
args.putInt("type", protocol); // account selector
FragmentDialogSelectAccount fragment = new FragmentDialogSelectAccount();
fragment.setArguments(args);
fragment.setTargetFragment(parentFragment, FragmentRules.REQUEST_RULE_COPY_ACCOUNT);
fragment.show(parentFragment.getParentFragmentManager(), "rule:copy:account");
2019-08-20 20:08:38 +00:00
}
2019-05-03 19:30:11 +00:00
});
popupMenu.show();
return true;
}
2022-03-22 10:19:31 +00:00
2022-04-04 08:56:57 +00:00
private void setAction(int resid, String value) {
if (TextUtils.isEmpty(value))
tvAction.setText(resid);
else {
SpannableStringBuilder ssb = new SpannableStringBuilderEx();
ssb.append(context.getString(resid));
ssb.append(" \"");
int start = ssb.length();
ssb.append(value);
ssb.setSpan(new StyleSpan(Typeface.ITALIC), start, ssb.length(), 0);
ssb.append("\"");
tvAction.setText(ssb);
}
}
private int getAction(int type) {
2022-03-22 10:19:31 +00:00
switch (type) {
case EntityRule.TYPE_NOOP:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_noop;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_SEEN:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_seen;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_UNSEEN:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_unseen;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_HIDE:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_hide;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_IGNORE:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_ignore;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_SNOOZE:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_snooze;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_FLAG:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_flag;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_IMPORTANCE:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_importance;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_KEYWORD:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_keyword;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_MOVE:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_move;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_COPY:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_copy;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_ANSWER:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_answer;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_TTS:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_tts;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_AUTOMATION:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_automation;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_DELETE:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_delete;
2022-03-22 10:19:31 +00:00
case EntityRule.TYPE_SOUND:
2022-04-04 08:56:57 +00:00
return R.string.title_rule_sound;
case EntityRule.TYPE_LOCAL_ONLY:
return R.string.title_rule_local_only;
2023-08-14 07:39:53 +00:00
case EntityRule.TYPE_NOTES:
return R.string.title_rule_notes;
2023-10-02 16:45:50 +00:00
case EntityRule.TYPE_URL:
return R.string.title_rule_url;
2022-03-22 10:19:31 +00:00
default:
throw new IllegalArgumentException("Unknown action type=" + type);
}
}
2022-04-18 12:23:30 +00:00
private class Condition {
private final String name;
private final String condition;
private final Boolean regex;
Condition(String name, String condition, Boolean regex) {
this.name = name;
this.condition = condition;
this.regex = regex;
}
}
2019-01-17 13:29:35 +00:00
}
AdapterRule(Fragment parentFragment) {
this.parentFragment = parentFragment;
this.context = parentFragment.getContext();
this.owner = parentFragment.getViewLifecycleOwner();
2019-01-17 13:29:35 +00:00
this.inflater = LayoutInflater.from(context);
2023-08-14 15:11:24 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.debug = prefs.getBoolean("debug", false);
2020-12-24 07:30:03 +00:00
this.DF = Helper.getDateTimeInstance(this.context);
2019-01-17 13:29:35 +00:00
setHasStableIds(true);
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroyed() {
2019-12-07 16:02:42 +00:00
Log.d(AdapterRule.this + " parent destroyed");
AdapterRule.this.parentFragment = null;
2021-09-28 08:18:20 +00:00
owner.getLifecycle().removeObserver(this);
}
});
2019-01-17 13:29:35 +00:00
}
2023-02-19 10:09:31 +00:00
public void set(int protocol, String sort, @NonNull List<TupleRuleEx> rules) {
2020-07-01 06:57:17 +00:00
this.protocol = protocol;
2023-02-19 10:09:31 +00:00
this.sort = sort;
Log.i("Set protocol=" + protocol + " rules=" + rules.size() + " sort=" + sort + " search=" + search);
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(rules, new Comparator<TupleRuleEx>() {
@Override
public int compare(TupleRuleEx r1, TupleRuleEx r2) {
int order;
if ("last_applied".equals(sort))
order = -Long.compare(
r1.last_applied == null ? 0 : r1.last_applied,
r2.last_applied == null ? 0 : r2.last_applied);
else if ("applied".equals(sort)) {
order = -Integer.compare(
r1.applied == null ? 0 : r1.applied,
r2.applied == null ? 0 : r2.applied);
} else
order = Integer.compare(r1.order, r2.order);
if (order == 0)
2023-11-22 06:55:34 +00:00
order = collator.compare(
r1.group == null ? "" : r1.group,
r2.group == null ? "" : r2.group);
if (order == 0)
order = collator.compare(
2023-02-19 10:09:31 +00:00
r1.name == null ? "" : r1.name,
r2.name == null ? "" : r2.name);
2023-11-22 06:55:34 +00:00
return order;
2023-02-19 10:09:31 +00:00
}
});
2019-01-17 13:29:35 +00:00
2020-09-06 07:21:38 +00:00
all = rules;
List<TupleRuleEx> items;
if (TextUtils.isEmpty(search))
items = all;
else {
items = new ArrayList<>();
String query = search.toLowerCase().trim();
2022-04-01 06:49:40 +00:00
for (TupleRuleEx rule : rules)
if (rule.matches(query))
2020-09-06 07:21:38 +00:00
items.add(rule);
}
2019-01-17 13:29:35 +00:00
2020-09-06 07:21:38 +00:00
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(selected, items), false);
selected = items;
2019-01-17 13:29:35 +00:00
diff.dispatchUpdatesTo(new ListUpdateCallback() {
@Override
public void onInserted(int position, int count) {
2019-12-07 16:02:42 +00:00
Log.d("Inserted @" + position + " #" + count);
2019-01-17 13:29:35 +00:00
}
@Override
public void onRemoved(int position, int count) {
2019-12-07 16:02:42 +00:00
Log.d("Removed @" + position + " #" + count);
2019-01-17 13:29:35 +00:00
}
@Override
public void onMoved(int fromPosition, int toPosition) {
2019-12-07 16:02:42 +00:00
Log.d("Moved " + fromPosition + ">" + toPosition);
2019-01-17 13:29:35 +00:00
}
@Override
public void onChanged(int position, int count, Object payload) {
2019-12-07 16:02:42 +00:00
Log.d("Changed @" + position + " #" + count);
2019-01-17 13:29:35 +00:00
}
});
2022-03-23 17:19:14 +00:00
try {
diff.dispatchUpdatesTo(this);
} catch (Throwable ex) {
Log.e(ex);
}
2019-01-17 13:29:35 +00:00
}
2023-02-19 10:09:31 +00:00
public void setSort(String sort) {
this.sort = sort;
set(protocol, sort, all);
notifyDataSetChanged();
}
2020-09-06 07:21:38 +00:00
public void search(String query) {
2021-03-27 07:14:21 +00:00
Log.i("Rules query=" + query);
2020-09-06 07:21:38 +00:00
search = query;
2023-02-19 10:09:31 +00:00
set(protocol, sort, all);
2020-09-06 07:21:38 +00:00
}
2020-12-30 18:29:20 +00:00
private static class DiffCallback extends DiffUtil.Callback {
2019-03-15 11:53:22 +00:00
private List<TupleRuleEx> prev = new ArrayList<>();
private List<TupleRuleEx> next = new ArrayList<>();
2019-01-17 13:29:35 +00:00
2019-02-10 18:02:55 +00:00
DiffCallback(List<TupleRuleEx> prev, List<TupleRuleEx> next) {
2019-03-15 11:53:22 +00:00
this.prev.addAll(prev);
this.next.addAll(next);
2019-01-17 13:29:35 +00:00
}
@Override
public int getOldListSize() {
return prev.size();
}
@Override
public int getNewListSize() {
return next.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
2019-01-17 21:41:00 +00:00
TupleRuleEx r1 = prev.get(oldItemPosition);
TupleRuleEx r2 = next.get(newItemPosition);
2019-01-17 13:29:35 +00:00
return r1.id.equals(r2.id);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
2019-01-17 21:41:00 +00:00
TupleRuleEx r1 = prev.get(oldItemPosition);
TupleRuleEx r2 = next.get(newItemPosition);
2019-01-17 13:29:35 +00:00
return r1.equals(r2);
}
}
@Override
public long getItemId(int position) {
2020-09-06 07:21:38 +00:00
return selected.get(position).id;
2019-01-17 13:29:35 +00:00
}
2023-04-01 16:46:05 +00:00
public EntityRule getItemAtPosition(int pos) {
if (pos >= 0 && pos < selected.size())
return selected.get(pos);
else
return null;
}
2019-01-17 13:29:35 +00:00
@Override
public int getItemCount() {
2020-09-06 07:21:38 +00:00
return selected.size();
2019-01-17 13:29:35 +00:00
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(inflater.inflate(R.layout.item_rule, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
2020-09-06 07:21:38 +00:00
TupleRuleEx rule = selected.get(position);
2020-11-12 07:07:30 +00:00
holder.powner.recreate(rule == null ? null : rule.id);
holder.unwire();
2019-01-17 13:29:35 +00:00
holder.bindTo(rule);
holder.wire();
}
}