Added option to block sender when reporting spam from notification

pull/178/head
M66B 5 years ago
parent e873d2d780
commit c9f56cef7a

@ -540,6 +540,41 @@ public class EntityRule {
return cal; return cal;
} }
static EntityRule blockSender(Context context, EntityMessage message, EntityFolder junk, boolean block_domain) throws JSONException {
String sender = ((InternetAddress) message.from[0]).getAddress();
String name = MessageHelper.formatAddresses(new Address[]{message.from[0]});
if (block_domain) {
int at = sender.indexOf('@');
if (at > 0)
sender = sender.substring(at);
}
JSONObject jsender = new JSONObject();
jsender.put("value", sender);
jsender.put("regex", false);
JSONObject jcondition = new JSONObject();
jcondition.put("sender", jsender);
JSONObject jaction = new JSONObject();
jaction.put("type", EntityRule.TYPE_MOVE);
jaction.put("target", junk.id);
DB db = DB.getInstance(context);
EntityRule rule = new EntityRule();
rule.folder = message.folder;
rule.name = context.getString(R.string.title_block, name);
rule.order = 1000;
rule.enabled = true;
rule.stop = true;
rule.condition = jcondition.toString();
rule.action = jaction.toString();
return rule;
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj instanceof EntityRule) { if (obj instanceof EntityRule) {

@ -142,7 +142,6 @@ import org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient;
import org.bouncycastle.cms.jcajce.JceKeyTransRecipient; import org.bouncycastle.cms.jcajce.JceKeyTransRecipient;
import org.bouncycastle.util.Store; import org.bouncycastle.util.Store;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.openintents.openpgp.AutocryptPeerUpdate; import org.openintents.openpgp.AutocryptPeerUpdate;
@ -5997,37 +5996,11 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if ((block_sender || block_domain) && if ((block_sender || block_domain) &&
(message.from != null && message.from.length > 0)) { (message.from != null && message.from.length > 0)) {
String sender = ((InternetAddress) message.from[0]).getAddress(); EntityRule rule = EntityRule.blockSender(context, message, junk, block_sender);
String name = MessageHelper.formatAddresses(new Address[]{message.from[0]});
if (block_domain) {
int at = sender.indexOf('@');
if (at > 0)
sender = sender.substring(at);
}
JSONObject jsender = new JSONObject();
jsender.put("value", sender);
jsender.put("regex", false);
JSONObject jcondition = new JSONObject();
jcondition.put("sender", jsender);
JSONObject jaction = new JSONObject();
jaction.put("type", EntityRule.TYPE_MOVE);
jaction.put("target", junk.id);
EntityRule rule = new EntityRule();
rule.folder = message.folder;
rule.name = context.getString(R.string.title_block, name);
rule.order = 1000;
rule.enabled = true;
rule.stop = true;
rule.condition = jcondition.toString();
rule.action = jaction.toString();
rule.id = db.rule().insertRule(rule); rule.id = db.rule().insertRule(rule);
} }
db.setTransactionSuccessful(); db.setTransactionSuccessful();
} finally { } finally {
db.endTransaction(); db.endTransaction();

@ -54,6 +54,7 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared
private Button btnManageService; private Button btnManageService;
private CheckBox cbNotifyActionTrash; private CheckBox cbNotifyActionTrash;
private CheckBox cbNotifyActionJunk; private CheckBox cbNotifyActionJunk;
private CheckBox cbNotifyActionBlockSender;
private CheckBox cbNotifyActionArchive; private CheckBox cbNotifyActionArchive;
private CheckBox cbNotifyActionMove; private CheckBox cbNotifyActionMove;
private CheckBox cbNotifyActionReply; private CheckBox cbNotifyActionReply;
@ -82,7 +83,7 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared
private Group grpNotification; private Group grpNotification;
private final static String[] RESET_OPTIONS = new String[]{ private final static String[] RESET_OPTIONS = new String[]{
"notify_trash", "notify_junk", "notify_archive", "notify_move", "notify_trash", "notify_junk", "notify_block_sender", "notify_archive", "notify_move",
"notify_reply", "notify_reply_direct", "notify_reply", "notify_reply_direct",
"notify_flag", "notify_seen", "notify_snooze", "notify_flag", "notify_seen", "notify_snooze",
"light", "sound", "light", "sound",
@ -107,6 +108,7 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared
btnManageService = view.findViewById(R.id.btnManageService); btnManageService = view.findViewById(R.id.btnManageService);
cbNotifyActionTrash = view.findViewById(R.id.cbNotifyActionTrash); cbNotifyActionTrash = view.findViewById(R.id.cbNotifyActionTrash);
cbNotifyActionJunk = view.findViewById(R.id.cbNotifyActionJunk); cbNotifyActionJunk = view.findViewById(R.id.cbNotifyActionJunk);
cbNotifyActionBlockSender = view.findViewById(R.id.cbNotifyActionBlockSender);
cbNotifyActionArchive = view.findViewById(R.id.cbNotifyActionArchive); cbNotifyActionArchive = view.findViewById(R.id.cbNotifyActionArchive);
cbNotifyActionMove = view.findViewById(R.id.cbNotifyActionMove); cbNotifyActionMove = view.findViewById(R.id.cbNotifyActionMove);
cbNotifyActionReply = view.findViewById(R.id.cbNotifyActionReply); cbNotifyActionReply = view.findViewById(R.id.cbNotifyActionReply);
@ -189,6 +191,14 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared
@Override @Override
public void onCheckedChanged(CompoundButton buttonView, boolean checked) { public void onCheckedChanged(CompoundButton buttonView, boolean checked) {
prefs.edit().putBoolean("notify_junk", checked).apply(); prefs.edit().putBoolean("notify_junk", checked).apply();
cbNotifyActionBlockSender.setEnabled(checked);
}
});
cbNotifyActionBlockSender.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean checked) {
prefs.edit().putBoolean("notify_block_sender", checked).apply();
} }
}); });
@ -398,6 +408,7 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared
cbNotifyActionTrash.setChecked(prefs.getBoolean("notify_trash", true) || !pro); cbNotifyActionTrash.setChecked(prefs.getBoolean("notify_trash", true) || !pro);
cbNotifyActionJunk.setChecked(prefs.getBoolean("notify_junk", false) && pro); cbNotifyActionJunk.setChecked(prefs.getBoolean("notify_junk", false) && pro);
cbNotifyActionBlockSender.setChecked(prefs.getBoolean("notify_block_sender", false) && pro);
cbNotifyActionArchive.setChecked(prefs.getBoolean("notify_archive", true) || !pro); cbNotifyActionArchive.setChecked(prefs.getBoolean("notify_archive", true) || !pro);
cbNotifyActionMove.setChecked(prefs.getBoolean("notify_move", false) && pro); cbNotifyActionMove.setChecked(prefs.getBoolean("notify_move", false) && pro);
cbNotifyActionReply.setChecked(prefs.getBoolean("notify_reply", false) && pro); cbNotifyActionReply.setChecked(prefs.getBoolean("notify_reply", false) && pro);
@ -427,6 +438,7 @@ public class FragmentOptionsNotifications extends FragmentBase implements Shared
cbNotifyActionTrash.setEnabled(pro && !summary); cbNotifyActionTrash.setEnabled(pro && !summary);
cbNotifyActionJunk.setEnabled(pro && !summary); cbNotifyActionJunk.setEnabled(pro && !summary);
cbNotifyActionBlockSender.setEnabled(pro && !summary && cbNotifyActionJunk.isChecked());
cbNotifyActionArchive.setEnabled(pro && !summary); cbNotifyActionArchive.setEnabled(pro && !summary);
cbNotifyActionMove.setEnabled(pro && !summary); cbNotifyActionMove.setEnabled(pro && !summary);
cbNotifyActionReply.setEnabled(pro && !summary); cbNotifyActionReply.setEnabled(pro && !summary);

@ -471,6 +471,7 @@ public class FragmentSetup extends FragmentBase {
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == ActivitySetup.REQUEST_STILL && resultCode != Activity.RESULT_OK) if (requestCode == ActivitySetup.REQUEST_STILL && resultCode != Activity.RESULT_OK)
((FragmentBase) getParentFragment()).finish(); ((FragmentBase) getParentFragment()).finish();
else { else {
@ -492,6 +493,9 @@ public class FragmentSetup extends FragmentBase {
} }
}); });
} }
} catch (Throwable ex) {
Log.e(ex);
}
} }
@Override @Override

@ -34,6 +34,8 @@ import androidx.core.app.AlarmManagerCompat;
import androidx.core.app.RemoteInput; import androidx.core.app.RemoteInput;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import org.json.JSONException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
@ -112,7 +114,7 @@ public class ServiceUI extends IntentService {
case "junk": case "junk":
cancel(group, id); cancel(group, id);
onMove(id, EntityFolder.JUNK); onJunk(id);
break; break;
case "archive": case "archive":
@ -199,7 +201,9 @@ public class ServiceUI extends IntentService {
return; return;
EntityFolder folder = db.folder().getFolderByType(message.account, folderType); EntityFolder folder = db.folder().getFolderByType(message.account, folderType);
if (folder != null) if (folder == null)
return;
EntityOperation.queue(this, message, EntityOperation.MOVE, folder.id); EntityOperation.queue(this, message, EntityOperation.MOVE, folder.id);
db.setTransactionSuccessful(); db.setTransactionSuccessful();
@ -207,7 +211,7 @@ public class ServiceUI extends IntentService {
db.endTransaction(); db.endTransaction();
} }
ServiceSynchronize.eval(ServiceUI.this, "move"); ServiceSynchronize.eval(this, "ui/move:" + folderType);
} }
private void onMove(long id) { private void onMove(long id) {
@ -230,7 +234,38 @@ public class ServiceUI extends IntentService {
db.endTransaction(); db.endTransaction();
} }
ServiceSynchronize.eval(ServiceUI.this, "move"); ServiceSynchronize.eval(this, "ui/move:" + id);
}
private void onJunk(long id) throws JSONException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean block_sender = prefs.getBoolean("notify_block_sender", false);
DB db = DB.getInstance(this);
try {
db.beginTransaction();
EntityMessage message = db.message().getMessage(id);
if (message == null)
return;
EntityFolder junk = db.folder().getFolderByType(message.account, EntityFolder.JUNK);
if (junk == null)
return;
EntityOperation.queue(this, message, EntityOperation.MOVE, junk.id);
if (block_sender) {
EntityRule rule = EntityRule.blockSender(this, message, junk, false);
rule.id = db.rule().insertRule(rule);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
ServiceSynchronize.eval(this, "ui/junk");
} }
private void onReplyDirect(long id, Intent intent) throws IOException { private void onReplyDirect(long id, Intent intent) throws IOException {
@ -297,7 +332,7 @@ public class ServiceUI extends IntentService {
db.endTransaction(); db.endTransaction();
} }
ServiceSend.start(ServiceUI.this); ServiceSend.start(this);
ToastEx.makeText(this, R.string.title_queued, Toast.LENGTH_LONG).show(); ToastEx.makeText(this, R.string.title_queued, Toast.LENGTH_LONG).show();
} }
@ -325,7 +360,7 @@ public class ServiceUI extends IntentService {
db.endTransaction(); db.endTransaction();
} }
ServiceSynchronize.eval(ServiceUI.this, "flag"); ServiceSynchronize.eval(this, "ui/flag");
} }
private void onSeen(long id) { private void onSeen(long id) {
@ -344,7 +379,7 @@ public class ServiceUI extends IntentService {
db.endTransaction(); db.endTransaction();
} }
ServiceSynchronize.eval(ServiceUI.this, "seen"); ServiceSynchronize.eval(this, "ui/seen");
} }
private void onSnooze(long id) { private void onSnooze(long id) {
@ -446,9 +481,9 @@ public class ServiceUI extends IntentService {
} }
if (EntityFolder.OUTBOX.equals(folder.type)) if (EntityFolder.OUTBOX.equals(folder.type))
ServiceSend.start(ServiceUI.this); ServiceSend.start(this);
else else
ServiceSynchronize.eval(ServiceUI.this, "wakeup"); ServiceSynchronize.eval(this, "ui/wakeup");
} }
private void onSync(long aid, boolean reschedule) { private void onSync(long aid, boolean reschedule) {
@ -470,7 +505,7 @@ public class ServiceUI extends IntentService {
db.endTransaction(); db.endTransaction();
} }
ServiceSynchronize.eval(this, "poll"); ServiceSynchronize.eval(this, "ui/poll");
if (reschedule) { if (reschedule) {
long now = new Date().getTime(); long now = new Date().getTime();

@ -125,6 +125,16 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbNotifyActionTrash" /> app:layout_constraintTop_toBottomOf="@id/cbNotifyActionTrash" />
<CheckBox
android:id="@+id/cbNotifyActionBlockSender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="6dp"
android:text="@string/title_block_sender"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbNotifyActionJunk" />
<CheckBox <CheckBox
android:id="@+id/cbNotifyActionArchive" android:id="@+id/cbNotifyActionArchive"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -132,7 +142,7 @@
android:layout_marginTop="6dp" android:layout_marginTop="6dp"
android:text="@string/title_advanced_notify_action_archive" android:text="@string/title_advanced_notify_action_archive"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbNotifyActionJunk" /> app:layout_constraintTop_toBottomOf="@id/cbNotifyActionBlockSender" />
<CheckBox <CheckBox
android:id="@+id/cbNotifyActionMove" android:id="@+id/cbNotifyActionMove"

Loading…
Cancel
Save