Added clear spam rules

This commit is contained in:
M66B 2020-09-05 22:55:34 +02:00
parent c8a89faa8d
commit 34575454f3
7 changed files with 110 additions and 9 deletions

View File

@ -728,8 +728,9 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
lbm.sendBroadcast( lbm.sendBroadcast(
new Intent(ActivityView.ACTION_EDIT_RULES) new Intent(ActivityView.ACTION_EDIT_RULES)
.putExtra("account", folder.account) .putExtra("account", folder.account)
.putExtra("protocol", folder.accountProtocol)
.putExtra("folder", folder.id) .putExtra("folder", folder.id)
.putExtra("protocol", folder.accountProtocol)); .putExtra("type", folder.type));
} }
private void onActionEditProperties() { private void onActionEditProperties() {

View File

@ -3798,6 +3798,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
aargs.putLong("account", message.account); aargs.putLong("account", message.account);
aargs.putInt("protocol", message.accountProtocol); aargs.putInt("protocol", message.accountProtocol);
aargs.putLong("folder", message.folder); aargs.putLong("folder", message.folder);
aargs.putString("type", message.folderType);
aargs.putString("from", MessageHelper.formatAddresses(message.from)); aargs.putString("from", MessageHelper.formatAddresses(message.from));
FragmentDialogJunk ask = new FragmentDialogJunk(); FragmentDialogJunk ask = new FragmentDialogJunk();
@ -6182,8 +6183,9 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Bundle args = getArguments(); Bundle args = getArguments();
final long account = args.getLong("account"); final long account = args.getLong("account");
final long folder = args.getLong("folder");
final int protocol = args.getInt("protocol"); final int protocol = args.getInt("protocol");
final long folder = args.getLong("folder");
final String type = args.getString("type");
final String from = args.getString("from"); final String from = args.getString("from");
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_junk, null); View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_junk, null);
@ -6219,8 +6221,9 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
lbm.sendBroadcast( lbm.sendBroadcast(
new Intent(ActivityView.ACTION_EDIT_RULES) new Intent(ActivityView.ACTION_EDIT_RULES)
.putExtra("account", account) .putExtra("account", account)
.putExtra("protocol", protocol)
.putExtra("folder", folder) .putExtra("folder", folder)
.putExtra("protocol", protocol)); .putExtra("type", type));
dismiss(); dismiss();
} }
}); });

View File

@ -2180,7 +2180,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
Bundle aargs = new Bundle(); Bundle aargs = new Bundle();
aargs.putLong("id", message.id); aargs.putLong("id", message.id);
aargs.putLong("account", message.account); aargs.putLong("account", message.account);
aargs.putInt("protocol", message.accountProtocol);
aargs.putLong("folder", message.folder); aargs.putLong("folder", message.folder);
aargs.putString("type", message.folderType);
aargs.putString("from", MessageHelper.formatAddresses(message.from)); aargs.putString("from", MessageHelper.formatAddresses(message.from));
AdapterMessage.FragmentDialogJunk ask = new AdapterMessage.FragmentDialogJunk(); AdapterMessage.FragmentDialogJunk ask = new AdapterMessage.FragmentDialogJunk();

View File

@ -162,8 +162,8 @@ public class FragmentRule extends FragmentBase {
private long id = -1; private long id = -1;
private long copy = -1; private long copy = -1;
private long account = -1; private long account = -1;
private long folder = -1;
private int protocol = -1; private int protocol = -1;
private long folder = -1;
private final static int MAX_CHECK = 10; private final static int MAX_CHECK = 10;
@ -188,8 +188,8 @@ public class FragmentRule extends FragmentBase {
else else
id = args.getLong("id", -1); id = args.getLong("id", -1);
account = args.getLong("account", -1); account = args.getLong("account", -1);
folder = args.getLong("folder", -1);
protocol = args.getInt("protocol", EntityAccount.TYPE_IMAP); protocol = args.getInt("protocol", EntityAccount.TYPE_IMAP);
folder = args.getLong("folder", -1);
} }
@Override @Override

View File

@ -24,6 +24,9 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -40,15 +43,19 @@ import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
import static eu.faircode.email.EntityRule.TYPE_MOVE;
public class FragmentRules extends FragmentBase { public class FragmentRules extends FragmentBase {
private long account; private long account;
private long folder;
private int protocol; private int protocol;
private long folder;
private String type;
private boolean cards; private boolean cards;
@ -60,6 +67,7 @@ public class FragmentRules extends FragmentBase {
private AdapterRule adapter; private AdapterRule adapter;
static final int REQUEST_MOVE = 1; static final int REQUEST_MOVE = 1;
private static final int REQUEST_CLEAR = 2;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -68,8 +76,9 @@ public class FragmentRules extends FragmentBase {
// Get arguments // Get arguments
Bundle args = getArguments(); Bundle args = getArguments();
account = args.getLong("account", -1); account = args.getLong("account", -1);
folder = args.getLong("folder", -1);
protocol = args.getInt("protocol", -1); protocol = args.getInt("protocol", -1);
folder = args.getLong("folder", -1);
type = args.getString("type");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
cards = prefs.getBoolean("cards", true); cards = prefs.getBoolean("cards", true);
@ -79,6 +88,7 @@ public class FragmentRules extends FragmentBase {
@Nullable @Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(R.string.title_edit_rules); setSubtitle(R.string.title_edit_rules);
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_rules, container, false); View view = inflater.inflate(R.layout.fragment_rules, container, false);
@ -108,8 +118,8 @@ public class FragmentRules extends FragmentBase {
public void onClick(View view) { public void onClick(View view) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("account", account); args.putLong("account", account);
args.putLong("folder", folder);
args.putInt("protocol", protocol); args.putInt("protocol", protocol);
args.putLong("folder", folder);
FragmentRule fragment = new FragmentRule(); FragmentRule fragment = new FragmentRule();
fragment.setArguments(args); fragment.setArguments(args);
@ -160,12 +170,51 @@ public class FragmentRules extends FragmentBase {
if (resultCode == RESULT_OK && data != null) if (resultCode == RESULT_OK && data != null)
onMove(data.getBundleExtra("args")); onMove(data.getBundleExtra("args"));
break; break;
case REQUEST_CLEAR:
if (resultCode == RESULT_OK && data != null)
onClear(data.getBundleExtra("args"));
break;
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.e(ex);
} }
} }
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_rules, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_clear).setVisible(!EntityFolder.JUNK.equals(type));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_clear:
onMenuClear();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void onMenuClear() {
Bundle aargs = new Bundle();
aargs.putString("question", getString(R.string.title_rules_clear_confirm));
aargs.putLong("folder", folder);
FragmentDialogAsk ask = new FragmentDialogAsk();
ask.setArguments(aargs);
ask.setTargetFragment(this, REQUEST_CLEAR);
ask.show(getParentFragmentManager(), "rules:clear");
}
private void onMove(Bundle args) { private void onMove(Bundle args) {
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
@ -180,8 +229,44 @@ public class FragmentRules extends FragmentBase {
@Override @Override
protected void onException(Bundle args, Throwable ex) { protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
} }
}.execute(this, args, "rule:move"); }.execute(this, args, "rule:move");
} }
private void onClear(Bundle args) {
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long fid = args.getLong("folder");
DB db = DB.getInstance(context);
EntityFolder folder = db.folder().getFolder(fid);
if (folder == null)
return null;
EntityFolder junk = db.folder().getFolderByType(folder.account, EntityFolder.JUNK);
if (junk == null)
return null;
List<EntityRule> rules = db.rule().getRules(fid);
if (rules == null)
return null;
for (EntityRule rule : rules) {
JSONObject jaction = new JSONObject(rule.action);
if (jaction.optInt("type", -1) == TYPE_MOVE &&
jaction.optInt("target", -1) == junk.id)
db.rule().deleteRule(rule.id);
}
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "rules:clear");
}
} }

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_clear"
android:title="@string/title_rules_clear"
app:showAsAction="never" />
</menu>

View File

@ -1369,6 +1369,8 @@
<string name="title_log">Log</string> <string name="title_log">Log</string>
<string name="title_auto_scroll">Auto scroll</string> <string name="title_auto_scroll">Auto scroll</string>
<string name="title_log_clear">Clear</string> <string name="title_log_clear">Clear</string>
<string name="title_rules_clear">Clear spam rules</string>
<string name="title_rules_clear_confirm">Delete all rules moving messages to the spam folder?</string>
<string name="title_debug_info">Debug info</string> <string name="title_debug_info">Debug info</string>
<string name="title_debug_info_remark">Please describe the problem and indicate the time of the problem:</string> <string name="title_debug_info_remark">Please describe the problem and indicate the time of the problem:</string>
<string name="title_crash_info_remark">Please describe what you were doing when the app crashed:</string> <string name="title_crash_info_remark">Please describe what you were doing when the app crashed:</string>