diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 3ecb64b212..929aba4b46 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -6453,215 +6453,6 @@ public class AdapterMessage extends RecyclerView.Adapter() { - @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(getContext()); - 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(getContext()); - 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) { - args.putBoolean("filter", isChecked); - - new SimpleTask() { - @Override - protected Void onExecute(Context context, Bundle args) throws Throwable { - long account = args.getLong("account"); - boolean filter = args.getBoolean("filter"); - - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - - DB db = DB.getInstance(context); - - EntityFolder inbox = db.folder().getFolderByType(account, EntityFolder.INBOX); - if (inbox == null) - return null; - - EntityFolder junk = db.folder().getFolderByType(account, EntityFolder.JUNK); - if (junk == null) - return null; - - try { - db.beginTransaction(); - - db.folder().setFolderDownload( - inbox.id, inbox.download || filter); - db.folder().setFolderAutoClassify( - inbox.id, inbox.auto_classify_source || filter, inbox.auto_classify_target); - - db.folder().setFolderDownload( - junk.id, junk.download || filter); - db.folder().setFolderAutoClassify( - junk.id, junk.auto_classify_source || filter, filter); - - 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); - } - }); - - grpInJunk.setVisibility(inJunk ? View.GONE : View.VISIBLE); - - new SimpleTask() { - @Override - protected void onPreExecute(Bundle args) { - cbJunkFilter.setEnabled(false); - } - - @Override - protected Boolean onExecute(Context context, Bundle args) throws Throwable { - long account = args.getLong("account"); - - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - boolean classification = prefs.getBoolean("classification", false); - - DB db = DB.getInstance(context); - EntityFolder inbox = db.folder().getFolderByType(account, EntityFolder.INBOX); - if (inbox == null) - return false; - - EntityFolder junk = db.folder().getFolderByType(account, EntityFolder.JUNK); - if (junk == null) - return false; - - return (classification && - inbox.download && inbox.auto_classify_source && - 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"); - - return new AlertDialog.Builder(getContext()) - .setView(view) - .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - getArguments().putBoolean("block_sender", cbBlockSender.isChecked()); - getArguments().putBoolean("block_domain", cbBlockDomain.isChecked()); - sendResult(RESULT_OK); - } - }) - .setNegativeButton(android.R.string.cancel, null) - .create(); - } - } - public static class FragmentDialogNotes extends FragmentDialogBase { private ViewButtonColor btnColor; diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogJunk.java b/app/src/main/java/eu/faircode/email/FragmentDialogJunk.java new file mode 100644 index 0000000000..c9629a87f7 --- /dev/null +++ b/app/src/main/java/eu/faircode/email/FragmentDialogJunk.java @@ -0,0 +1,251 @@ +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 . + + Copyright 2018-2021 by Marcel Bokhorst (M66B) +*/ + +import android.app.Activity; +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +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 androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.constraintlayout.widget.Group; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import androidx.preference.PreferenceManager; + +public class FragmentDialogJunk extends FragmentDialogBase { + @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"); + final String from = args.getString("from"); + final boolean inJunk = args.getBoolean("inJunk"); + final boolean canBlock = args.getBoolean("canBlock"); + + View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_junk, null); + 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); + 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 Group grpInJunk = view.findViewById(R.id.grpInJunk); + + tvMessage.setText(getString(R.string.title_ask_spam_who, from)); + + ibInfoProvider.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Helper.viewFAQ(v.getContext(), 92); + } + }); + + cbBlockSender.setEnabled(canBlock && ActivityBilling.isPro(getContext())); + cbBlockDomain.setEnabled(false); + + cbBlockSender.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + cbBlockDomain.setEnabled(isChecked); + } + }); + + btnEditRules.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (inJunk) { + new SimpleTask() { + @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(getContext()); + 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(getContext()); + 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) { + args.putBoolean("filter", isChecked); + + new SimpleTask() { + @Override + protected Void onExecute(Context context, Bundle args) throws Throwable { + long account = args.getLong("account"); + boolean filter = args.getBoolean("filter"); + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + + DB db = DB.getInstance(context); + + EntityFolder inbox = db.folder().getFolderByType(account, EntityFolder.INBOX); + if (inbox == null) + return null; + + EntityFolder junk = db.folder().getFolderByType(account, EntityFolder.JUNK); + if (junk == null) + return null; + + try { + db.beginTransaction(); + + db.folder().setFolderDownload( + inbox.id, inbox.download || filter); + db.folder().setFolderAutoClassify( + inbox.id, inbox.auto_classify_source || filter, inbox.auto_classify_target); + + db.folder().setFolderDownload( + junk.id, junk.download || filter); + db.folder().setFolderAutoClassify( + junk.id, junk.auto_classify_source || filter, filter); + + 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); + } + }); + + grpInJunk.setVisibility(inJunk ? View.GONE : View.VISIBLE); + + new SimpleTask() { + @Override + protected void onPreExecute(Bundle args) { + cbJunkFilter.setEnabled(false); + } + + @Override + protected Boolean onExecute(Context context, Bundle args) throws Throwable { + long account = args.getLong("account"); + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean classification = prefs.getBoolean("classification", false); + + DB db = DB.getInstance(context); + EntityFolder inbox = db.folder().getFolderByType(account, EntityFolder.INBOX); + if (inbox == null) + return false; + + EntityFolder junk = db.folder().getFolderByType(account, EntityFolder.JUNK); + if (junk == null) + return false; + + return (classification && + inbox.download && inbox.auto_classify_source && + 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"); + + return new AlertDialog.Builder(getContext()) + .setView(view) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + getArguments().putBoolean("block_sender", cbBlockSender.isChecked()); + getArguments().putBoolean("block_domain", cbBlockDomain.isChecked()); + sendResult(Activity.RESULT_OK); + } + }) + .setNegativeButton(android.R.string.cancel, null) + .create(); + } +} diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 286dc5b551..9d19493279 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -2342,7 +2342,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. aargs.putBoolean("inJunk", EntityFolder.JUNK.equals(message.folderType)); aargs.putBoolean("canBlock", canBlock); - AdapterMessage.FragmentDialogJunk ask = new AdapterMessage.FragmentDialogJunk(); + FragmentDialogJunk ask = new FragmentDialogJunk(); ask.setArguments(aargs); ask.setTargetFragment(FragmentMessages.this, REQUEST_MESSAGE_JUNK); ask.show(getParentFragmentManager(), "swipe:junk");