Added button to delete block sender rules

This commit is contained in:
M66B
2021-09-18 20:37:59 +02:00
parent 7904267e9b
commit 2369f6dbea
7 changed files with 101 additions and 111 deletions

View File

@@ -34,14 +34,21 @@ import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
import org.json.JSONObject;
import java.util.List;
public class FragmentDialogJunk extends FragmentDialogBase {
@NonNull
@Override
@@ -63,12 +70,12 @@ public class FragmentDialogJunk extends FragmentDialogBase {
final CheckBox cbBlockDomain = view.findViewById(R.id.cbBlockDomain);
final ImageButton ibMore = view.findViewById(R.id.ibMore);
final TextView tvMore = view.findViewById(R.id.tvMore);
final Button btnEditRules = view.findViewById(R.id.btnEditRules);
final CheckBox cbJunkFilter = view.findViewById(R.id.cbJunkFilter);
final ImageButton ibInfoFilter = view.findViewById(R.id.ibInfoFilter);
final CheckBox cbBlocklist = view.findViewById(R.id.cbBlocklist);
final TextView tvBlocklist = view.findViewById(R.id.tvBlocklist);
final ImageButton ibInfoBlocklist = view.findViewById(R.id.ibInfoBlocklist);
final Button btnClear = view.findViewById(R.id.btnClear);
final Group grpInJunk = view.findViewById(R.id.grpInJunk);
final Group grpMore = view.findViewById(R.id.grpMore);
@@ -108,54 +115,6 @@ public class FragmentDialogJunk extends FragmentDialogBase {
ibMore.setOnClickListener(onMore);
tvMore.setOnClickListener(onMore);
btnEditRules.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inJunk) {
new SimpleTask<EntityFolder>() {
@Override
protected EntityFolder onExecute(Context context, Bundle args) throws Throwable {
long account = args.getLong("account");
DB db = DB.getInstance(context);
EntityFolder inbox = db.folder().getFolderByType(account, EntityFolder.INBOX);
if (inbox == null)
throw new IllegalArgumentException(context.getString(R.string.title_no_inbox));
return inbox;
}
@Override
protected void onExecuted(Bundle args, EntityFolder inbox) {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_EDIT_RULES)
.putExtra("account", account)
.putExtra("protocol", protocol)
.putExtra("folder", inbox.id)
.putExtra("type", inbox.type));
dismiss();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(FragmentDialogJunk.this, args, "junk:rules");
} else {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_EDIT_RULES)
.putExtra("account", account)
.putExtra("protocol", protocol)
.putExtra("folder", folder)
.putExtra("type", type));
dismiss();
}
}
});
cbJunkFilter.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@@ -237,6 +196,59 @@ public class FragmentDialogJunk extends FragmentDialogBase {
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
args.putLong("folder", folder);
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);
int type = jaction.optInt("type", -1);
long target = jaction.optLong("target", -1);
if (type == EntityRule.TYPE_MOVE && target == junk.id) {
EntityLog.log(context, "Deleting junk rule=" + rule.id);
db.rule().deleteRule(rule.id);
}
}
int count = db.contact().deleteContact(account, EntityContact.TYPE_JUNK);
EntityLog.log(context, "Deleted junk contacts=" + count);
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
ToastEx.makeText(getContext(), R.string.title_completed, Toast.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(FragmentDialogJunk.this, args, "junk:clear");
}
});
// Initialize
tvMessage.setText(inJunk
? getString(R.string.title_folder_junk)