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

447 lines
19 KiB
Java
Raw Normal View History

2021-06-20 05:07:45 +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/>.
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 by Marcel Bokhorst (M66B)
2021-06-20 05:07:45 +00:00
*/
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
2021-10-12 07:20:47 +00:00
import android.content.res.ColorStateList;
import android.os.Build;
2021-06-20 05:07:45 +00:00
import android.os.Bundle;
2022-01-29 14:37:24 +00:00
import android.text.SpannableStringBuilder;
2021-06-20 12:20:29 +00:00
import android.text.TextUtils;
2022-01-29 14:37:24 +00:00
import android.text.style.ForegroundColorSpan;
2021-06-20 05:07:45 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
2021-06-20 05:07:45 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.FragmentTransaction;
2021-06-20 05:07:45 +00:00
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
import org.json.JSONObject;
2021-12-17 14:03:19 +00:00
import java.util.ArrayList;
2022-01-29 14:49:00 +00:00
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
2021-10-12 07:20:47 +00:00
import java.util.Locale;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
2021-06-20 05:07:45 +00:00
public class FragmentDialogJunk extends FragmentDialogBase {
2022-01-29 14:49:00 +00:00
private static final List<String> COMMON_DOMAINS = Collections.unmodifiableList(Arrays.asList(
"amazon\\.com",
"facebook\\.com",
"google\\.com",
"microsoft\\.com",
"twitter\\.com"
));
2021-06-20 05:07:45 +00:00
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Bundle args = getArguments();
final long account = args.getLong("account");
final int protocol = args.getInt("protocol");
final long folder = args.getLong("folder");
final String type = args.getString("type");
2021-12-17 14:03:19 +00:00
final Address[] froms = DB.Converters.decodeAddresses(args.getString("from"));
2021-06-20 05:07:45 +00:00
final boolean inJunk = args.getBoolean("inJunk");
2021-06-20 07:04:12 +00:00
final Context context = getContext();
final View view = LayoutInflater.from(context).inflate(R.layout.dialog_junk, null);
2021-06-20 05:07:45 +00:00
final TextView tvMessage = view.findViewById(R.id.tvMessage);
final ImageButton ibInfoProvider = view.findViewById(R.id.ibInfoProvider);
final CheckBox cbBlockSender = view.findViewById(R.id.cbBlockSender);
final CheckBox cbBlockDomain = view.findViewById(R.id.cbBlockDomain);
2021-06-20 05:33:36 +00:00
final ImageButton ibMore = view.findViewById(R.id.ibMore);
final TextView tvMore = view.findViewById(R.id.tvMore);
2021-06-20 05:07:45 +00:00
final CheckBox cbJunkFilter = view.findViewById(R.id.cbJunkFilter);
final ImageButton ibInfoFilter = view.findViewById(R.id.ibInfoFilter);
2021-06-20 07:04:12 +00:00
final CheckBox cbBlocklist = view.findViewById(R.id.cbBlocklist);
2021-06-20 12:20:29 +00:00
final TextView tvBlocklist = view.findViewById(R.id.tvBlocklist);
2021-06-20 07:04:12 +00:00
final ImageButton ibInfoBlocklist = view.findViewById(R.id.ibInfoBlocklist);
final Button btnClear = view.findViewById(R.id.btnClear);
final ImageButton ibRules = view.findViewById(R.id.ibRules);
final ImageButton ibManage = view.findViewById(R.id.ibManage);
2021-06-20 05:07:45 +00:00
final Group grpInJunk = view.findViewById(R.id.grpInJunk);
2021-06-20 05:33:36 +00:00
final Group grpMore = view.findViewById(R.id.grpMore);
2021-06-20 05:07:45 +00:00
2021-06-20 07:04:12 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2021-12-31 14:55:47 +00:00
boolean block_sender = prefs.getBoolean("block_sender", true);
2021-06-20 07:04:12 +00:00
boolean check_blocklist = prefs.getBoolean("check_blocklist", false);
boolean use_blocklist = prefs.getBoolean("use_blocklist", false);
2021-12-31 14:38:29 +00:00
boolean canBlock = false;
if (froms != null && froms.length > 0) {
String email = ((InternetAddress) froms[0]).getAddress();
canBlock = !TextUtils.isEmpty(email);
}
2021-06-20 05:33:36 +00:00
// Wire controls
2021-06-20 05:07:45 +00:00
ibInfoProvider.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.viewFAQ(v.getContext(), 92);
}
});
cbBlockSender.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
2021-09-18 16:15:44 +00:00
cbBlockDomain.setEnabled(isChecked && ActivityBilling.isPro(context));
2021-06-20 05:07:45 +00:00
}
});
2021-06-20 05:33:36 +00:00
View.OnClickListener onMore = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (grpMore.getVisibility() == View.VISIBLE) {
ibMore.setImageLevel(1);
grpMore.setVisibility(View.GONE);
} else {
ibMore.setImageLevel(0);
grpMore.setVisibility(View.VISIBLE);
}
}
};
ibMore.setOnClickListener(onMore);
tvMore.setOnClickListener(onMore);
2021-06-20 05:07:45 +00:00
cbJunkFilter.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
args.putBoolean("filter", isChecked);
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
long aid = args.getLong("account");
long fid = args.getLong("folder");
2021-06-20 05:07:45 +00:00
boolean filter = args.getBoolean("filter");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
DB db = DB.getInstance(context);
EntityFolder folder = db.folder().getFolder(fid);
if (folder == null)
2021-06-20 05:07:45 +00:00
return null;
EntityFolder junk = db.folder().getFolderByType(aid, EntityFolder.JUNK);
2021-06-20 05:07:45 +00:00
if (junk == null)
return null;
try {
db.beginTransaction();
db.folder().setFolderDownload(folder.id,
folder.download || filter);
db.folder().setFolderAutoClassify(folder.id,
folder.auto_classify_source || filter,
folder.auto_classify_target);
2021-06-20 05:07:45 +00:00
db.folder().setFolderDownload(junk.id,
junk.download || filter);
db.folder().setFolderAutoClassify(junk.id,
junk.auto_classify_source || filter,
filter);
2021-06-20 05:07:45 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
prefs.edit().putBoolean("classification", true).apply();
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(FragmentDialogJunk.this, args, "junk:filter");
}
});
ibInfoFilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.viewFAQ(v.getContext(), 163);
}
});
2021-06-20 07:04:12 +00:00
cbBlocklist.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
prefs.edit()
.putBoolean("check_blocklist", isChecked)
.putBoolean("use_blocklist", isChecked)
.apply();
}
});
ibInfoBlocklist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.viewFAQ(v.getContext(), 168, true);
}
});
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
2021-09-19 05:44:40 +00:00
new AlertDialog.Builder(v.getContext())
2021-10-26 10:57:23 +00:00
.setIcon(R.drawable.twotone_warning_24)
2021-09-19 05:44:40 +00:00
.setTitle(R.string.title_junk_clear)
.setMessage(R.string.title_junk_clear_hint)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
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");
}
2021-09-19 05:44:40 +00:00
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
})
.show();
}
});
ibRules.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(v.getContext());
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_EDIT_RULES)
.putExtra("account", account)
.putExtra("protocol", protocol)
.putExtra("folder", folder)
.putExtra("type", type));
dismiss();
}
});
ibManage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
args.putBoolean("junk", true);
FragmentContacts fragment = new FragmentContacts();
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = getParentFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("contacts");
fragmentTransaction.commit();
dismiss();
}
});
2022-01-30 16:26:07 +00:00
List<String> regex = new ArrayList<>();
regex.addAll(COMMON_DOMAINS);
regex.addAll(EmailProvider.getDomainNames(context));
2021-12-17 14:03:19 +00:00
boolean common = false;
List<String> domains = new ArrayList<>();
if (froms != null)
for (Address from : froms) {
String email = ((InternetAddress) from).getAddress();
2022-01-30 16:26:07 +00:00
String domain = UriHelper.getEmailDomain(email);
if (TextUtils.isEmpty(domain) || domains.contains(domain))
2021-12-17 14:03:19 +00:00
continue;
domains.add(domain);
2022-01-29 14:49:00 +00:00
for (String r : regex)
if (domain.matches(r)) {
2021-10-12 07:20:47 +00:00
common = true;
2022-01-29 14:49:00 +00:00
break;
}
2021-10-12 07:20:47 +00:00
}
2021-06-20 05:33:36 +00:00
// Initialize
2022-01-29 14:37:24 +00:00
if (inJunk)
tvMessage.setText(R.string.title_folder_junk);
else {
String who = MessageHelper.formatAddresses(froms);
String title = getString(R.string.title_ask_spam_who, who);
SpannableStringBuilder ssb = new SpannableStringBuilderEx(title);
if (who.length() > 0) {
int start = title.indexOf(who);
if (start > 0) {
int textColorPrimary = Helper.resolveColor(context, android.R.attr.textColorPrimary);
ssb.setSpan(new ForegroundColorSpan(textColorPrimary), start, start + who.length(), 0);
}
}
tvMessage.setText(ssb);
}
2021-09-18 16:15:44 +00:00
cbBlockSender.setEnabled(canBlock);
2021-06-20 05:33:36 +00:00
cbBlockDomain.setEnabled(false);
2021-12-31 14:55:47 +00:00
cbBlockSender.setChecked(canBlock && block_sender);
2021-12-17 14:03:19 +00:00
cbBlockDomain.setText(getString(R.string.title_block_sender_domain, TextUtils.join(",", domains)));
if (common) {
int dp6 = Helper.dp2pixels(context, 6);
int colorWarning = Helper.resolveColor(context, R.attr.colorWarning);
cbBlockDomain.setTextColor(colorWarning);
cbBlockDomain.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.twotone_warning_24, 0);
cbBlockDomain.setCompoundDrawablePadding(dp6);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
cbBlockDomain.setCompoundDrawableTintList(ColorStateList.valueOf(colorWarning));
}
cbBlockDomain.setVisibility(domains.size() > 0 ? View.VISIBLE : View.GONE);
2021-06-20 05:33:36 +00:00
ibMore.setImageLevel(1);
2021-06-20 07:04:12 +00:00
cbBlocklist.setChecked(check_blocklist && use_blocklist);
2021-07-01 16:59:05 +00:00
tvBlocklist.setText(TextUtils.join(", ", DnsBlockList.getNamesEnabled(context)));
2021-06-20 05:07:45 +00:00
grpInJunk.setVisibility(inJunk ? View.GONE : View.VISIBLE);
2021-06-20 05:33:36 +00:00
grpMore.setVisibility(inJunk ? View.VISIBLE : View.GONE);
2021-06-20 05:07:45 +00:00
new SimpleTask<Boolean>() {
@Override
protected void onPreExecute(Bundle args) {
cbJunkFilter.setEnabled(false);
}
@Override
2021-09-18 16:15:44 +00:00
protected Boolean onExecute(Context context, Bundle args) {
long aid = args.getLong("account");
long fid = args.getLong("folder");
2021-06-20 05:07:45 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean classification = prefs.getBoolean("classification", false);
DB db = DB.getInstance(context);
EntityFolder folder = db.folder().getFolder(fid);
if (folder == null)
return null;
2021-06-20 05:07:45 +00:00
EntityFolder junk = db.folder().getFolderByType(aid, EntityFolder.JUNK);
2021-06-20 05:07:45 +00:00
if (junk == null)
return null;
2021-06-20 05:07:45 +00:00
return (classification &&
folder.download && folder.auto_classify_source &&
2021-06-20 05:07:45 +00:00
junk.download && junk.auto_classify_source && junk.auto_classify_target);
}
@Override
protected void onExecuted(Bundle args, Boolean filter) {
if (filter != null) {
cbJunkFilter.setChecked(filter);
cbJunkFilter.setEnabled(true);
}
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(FragmentDialogJunk.this, args, "junk:filter");
2021-06-20 07:04:12 +00:00
AlertDialog.Builder builder = new AlertDialog.Builder(context)
2021-06-20 05:07:45 +00:00
.setView(view)
2021-06-20 05:33:36 +00:00
.setNegativeButton(android.R.string.cancel, null);
if (!inJunk)
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
2021-12-31 14:55:47 +00:00
prefs.edit().putBoolean("block_sender", cbBlockSender.isChecked()).apply();
2021-06-20 05:33:36 +00:00
getArguments().putBoolean("block_sender", cbBlockSender.isChecked());
getArguments().putBoolean("block_domain", cbBlockDomain.isChecked());
sendResult(Activity.RESULT_OK);
}
});
return builder.create();
2021-06-20 05:07:45 +00:00
}
}