Allow enabling classification per folder

This commit is contained in:
M66B
2021-01-13 10:22:52 +01:00
parent b117d0a3df
commit ed11ddbb27
11 changed files with 2464 additions and 50 deletions

View File

@@ -6454,9 +6454,13 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
DB db = DB.getInstance(context);
EntityFolder inbox = db.folder().getFolderByType(account, EntityFolder.INBOX);
EntityFolder junk = db.folder().getFolderByType(account, EntityFolder.JUNK);
if (junk != null) {
db.folder().setFolderAutoClassify(junk.id, filter);
if (inbox != null && junk != null) {
db.folder().setFolderAutoClassify(
inbox.id, inbox.auto_classify_source || filter, inbox.auto_classify_target);
db.folder().setFolderAutoClassify(
junk.id, junk.auto_classify_source || filter, filter);
prefs.edit().putBoolean("classification", true).apply();
}
@@ -6494,11 +6498,17 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
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 && junk.auto_classify;
return (classification &&
inbox.auto_classify_source &&
junk.auto_classify_source && junk.auto_classify_target);
}
@Override

View File

@@ -2973,7 +2973,7 @@ class Core {
runRules(context, imessage, account, folder, message, rules);
if (download && !message.ui_hide &&
MessageClassifier.isEnabled(context) && MessageClassifier.canClassify(folder.type))
MessageClassifier.isEnabled(context) && folder.auto_classify_source)
db.message().setMessageUiHide(message.id, true);
db.setTransactionSuccessful();
@@ -3006,7 +3006,7 @@ class Core {
}
if ((message.size != null && message.size < maxSize) ||
(MessageClassifier.isEnabled(context)) && MessageClassifier.canClassify(folder.type)) {
(MessageClassifier.isEnabled(context)) && folder.auto_classify_source) {
String body = parts.getHtml(context);
File file = message.getFile(context);
Helper.writeText(file, body);

View File

@@ -64,7 +64,7 @@ import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_PASSWORD;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 183,
version = 184,
entities = {
EntityIdentity.class,
EntityAccount.class,
@@ -1795,6 +1795,20 @@ public abstract class DB extends RoomDatabase {
Log.i("DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `message` ADD COLUMN `auto_classified` INTEGER NOT NULL DEFAULT 0");
}
})
.addMigrations(new Migration(183, 184) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {
Log.i("DB migration from version " + startVersion + " to " + endVersion);
db.execSQL("ALTER TABLE `folder` ADD COLUMN `auto_classify_source` INTEGER NOT NULL DEFAULT 0");
db.execSQL("ALTER TABLE `folder` RENAME COLUMN `auto_classify` TO 'auto_classify_target'");
db.execSQL("UPDATE `folder`" +
" SET auto_classify_source = 1" +
" WHERE (SELECT pop FROM account WHERE id = folder.account) = " + EntityAccount.TYPE_IMAP +
" AND (auto_classify_target" +
" OR type = '" + EntityFolder.INBOX + "'" +
" OR type = '" + EntityFolder.JUNK + "')");
}
});
}

View File

@@ -287,7 +287,8 @@ public interface DaoFolder {
", poll = :poll" +
", poll_factor = :poll_factor" +
", download = :download" +
", auto_classify = :auto_classify" +
", auto_classify_source = :auto_classify_source" +
", auto_classify_target = :auto_classify_target" +
", `sync_days` = :sync_days" +
", `keep_days` = :keep_days" +
", auto_delete = :auto_delete" +
@@ -295,7 +296,8 @@ public interface DaoFolder {
int setFolderProperties(
long id, String rename,
String display, Integer color, boolean unified, boolean navigation, boolean notify, boolean hide,
boolean synchronize, boolean poll, int poll_factor, boolean download, boolean auto_classify,
boolean synchronize, boolean poll, int poll_factor, boolean download,
boolean auto_classify_source, boolean auto_classify_target,
int sync_days, int keep_days, boolean auto_delete);
@Query("UPDATE folder" +
@@ -344,8 +346,11 @@ public interface DaoFolder {
@Query("UPDATE folder SET poll_count = :count WHERE id = :id AND NOT (poll_count IS :count)")
int setFolderPollCount(long id, int count);
@Query("UPDATE folder SET auto_classify = :auto_classify WHERE id = :id AND NOT (auto_classify IS :auto_classify)")
int setFolderAutoClassify(long id, boolean auto_classify);
@Query("UPDATE folder" +
" SET auto_classify_source = :source, auto_classify_target = :target" +
" WHERE id = :id" +
" AND NOT (auto_classify_source IS :source AND auto_classify_target IS :target)")
int setFolderAutoClassify(long id, boolean source, boolean target);
@Query("DELETE FROM folder WHERE id = :id")
void deleteFolder(long id);

View File

@@ -90,7 +90,9 @@ public class EntityFolder extends EntityOrder implements Serializable {
@NonNull
public Boolean download = true;
@NonNull
public Boolean auto_classify = false;
public Boolean auto_classify_source = false;
@NonNull
public Boolean auto_classify_target = false;
public Boolean subscribed;
@NonNull
public Integer sync_days;
@@ -257,12 +259,16 @@ public class EntityFolder extends EntityOrder implements Serializable {
if (EntityFolder.INBOX.equals(type)) {
this.unified = true;
this.notify = true;
this.auto_classify_source = true;
}
if (EntityFolder.DRAFTS.equals(type)) {
this.initialize = EntityFolder.DEFAULT_KEEP_DRAFTS;
this.keep_days = EntityFolder.DEFAULT_KEEP_DRAFTS;
}
if (EntityFolder.JUNK.equals(type))
this.auto_classify_source = true;
}
void setSpecials(EntityAccount account) {
@@ -503,6 +509,8 @@ public class EntityFolder extends EntityOrder implements Serializable {
this.synchronize.equals(other.synchronize) &&
this.poll.equals(other.poll) &&
this.download.equals(other.download) &&
this.auto_classify_source.equals(other.auto_classify_source) &&
this.auto_classify_target.equals(other.auto_classify_target) &&
Objects.equals(this.subscribed, other.subscribed) &&
this.sync_days.equals(other.sync_days) &&
this.keep_days.equals(other.keep_days) &&
@@ -547,7 +555,8 @@ public class EntityFolder extends EntityOrder implements Serializable {
json.put("poll", poll);
json.put("poll_factor", poll_factor);
json.put("download", download);
json.put("auto_classify", auto_classify);
json.put("auto_classify_source", auto_classify_source);
json.put("auto_classify_target", auto_classify_target);
json.put("sync_days", sync_days);
json.put("keep_days", keep_days);
json.put("auto_delete", auto_delete);
@@ -582,8 +591,14 @@ public class EntityFolder extends EntityOrder implements Serializable {
if (json.has("download"))
folder.download = json.getBoolean("download");
if (json.has("auto_classify"))
folder.auto_classify = json.getBoolean("auto_classify");
if (json.has("auto_classify_source"))
folder.auto_classify_source = json.getBoolean("auto_classify_source");
else
folder.auto_classify_source =
(EntityFolder.INBOX.equals(folder.type) ||
EntityFolder.JUNK.equals(folder.type));
if (json.has("auto_classify_target"))
folder.auto_classify_target = json.getBoolean("auto_classify_target");
if (json.has("after"))
folder.sync_days = json.getInt("after");

View File

@@ -69,7 +69,8 @@ public class FragmentFolder extends FragmentBase {
private EditText etPoll;
private TextView tvPoll;
private CheckBox cbDownload;
private CheckBox cbAutoClassify;
private CheckBox cbAutoClassifySource;
private CheckBox cbAutoClassifyTarget;
private TextView tvAutoClassifyPro;
private Button btnInfo;
private EditText etSyncDays;
@@ -131,7 +132,8 @@ public class FragmentFolder extends FragmentBase {
etPoll = view.findViewById(R.id.etPoll);
tvPoll = view.findViewById(R.id.tvPoll);
cbDownload = view.findViewById(R.id.cbDownload);
cbAutoClassify = view.findViewById(R.id.cbAutoClassify);
cbAutoClassifySource = view.findViewById(R.id.cbAutoClassifySource);
cbAutoClassifyTarget = view.findViewById(R.id.cbAutoClassifyTarget);
tvAutoClassifyPro = view.findViewById(R.id.tvAutoClassifyPro);
btnInfo = view.findViewById(R.id.btnInfo);
etSyncDays = view.findViewById(R.id.etSyncDays);
@@ -179,6 +181,14 @@ public class FragmentFolder extends FragmentBase {
}
});
cbAutoClassifyTarget.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
cbAutoClassifySource.setChecked(true);
}
});
Helper.linkPro(tvAutoClassifyPro);
btnInfo.setOnClickListener(new View.OnClickListener() {
@@ -221,7 +231,8 @@ public class FragmentFolder extends FragmentBase {
grpImap.setVisibility(imap ? View.VISIBLE : View.GONE);
tvParent.setText(parent);
grpParent.setVisibility(parent == null ? View.GONE : View.VISIBLE);
cbAutoClassify.setVisibility(View.GONE);
cbAutoClassifySource.setVisibility(View.GONE);
cbAutoClassifyTarget.setVisibility(View.GONE);
tvAutoClassifyPro.setVisibility(View.GONE);
grpAutoDelete.setVisibility(View.GONE);
btnSave.setEnabled(false);
@@ -293,7 +304,8 @@ public class FragmentFolder extends FragmentBase {
etPoll.setText(folder == null ? null : Integer.toString(folder.poll_factor));
tvPoll.setText(getString(R.string.title_factor_minutes, interval));
cbDownload.setChecked(folder == null ? true : folder.download);
cbAutoClassify.setChecked(folder == null ? false : folder.auto_classify);
cbAutoClassifySource.setChecked(folder == null ? false : folder.auto_classify_source);
cbAutoClassifyTarget.setChecked(folder == null ? false : folder.auto_classify_target);
etSyncDays.setText(Integer.toString(folder == null ? EntityFolder.DEFAULT_SYNC : folder.sync_days));
if (folder != null && folder.keep_days == Integer.MAX_VALUE)
cbKeepAll.setChecked(true);
@@ -305,9 +317,7 @@ public class FragmentFolder extends FragmentBase {
Helper.setViewsEnabled(view, true);
boolean always = (!ondemand && (pollInterval == 0 || exempted));
boolean canAutoClassify = (imap &&
MessageClassifier.isEnabled(getContext()) &&
(folder == null || MessageClassifier.canClassify(folder.type)));
boolean canAutoClassify = (imap && MessageClassifier.isEnabled(getContext()));
boolean pro = (ActivityBilling.isPro(getContext()) ||
(folder != null && EntityFolder.JUNK.equals(folder.type)));
@@ -316,8 +326,9 @@ public class FragmentFolder extends FragmentBase {
etPoll.setEnabled(cbSynchronize.isChecked() && always);
tvPoll.setEnabled(cbSynchronize.isChecked() && always);
grpPoll.setVisibility(imap && cbPoll.isEnabled() && cbPoll.isChecked() ? View.VISIBLE : View.GONE);
cbAutoClassify.setEnabled(pro);
cbAutoClassify.setVisibility(canAutoClassify ? View.VISIBLE : View.GONE);
cbAutoClassifyTarget.setEnabled(pro);
cbAutoClassifySource.setVisibility(canAutoClassify ? View.VISIBLE : View.GONE);
cbAutoClassifyTarget.setVisibility(canAutoClassify ? View.VISIBLE : View.GONE);
tvAutoClassifyPro.setVisibility(canAutoClassify && !pro ? View.VISIBLE : View.GONE);
etKeepDays.setEnabled(!cbKeepAll.isChecked());
cbAutoDelete.setEnabled(!cbKeepAll.isChecked());
@@ -426,7 +437,8 @@ public class FragmentFolder extends FragmentBase {
args.putBoolean("poll", cbPoll.isChecked());
args.putString("factor", etPoll.getText().toString());
args.putBoolean("download", cbDownload.isChecked());
args.putBoolean("auto_classify", cbAutoClassify.isChecked());
args.putBoolean("auto_classify_source", cbAutoClassifySource.isChecked());
args.putBoolean("auto_classify_target", cbAutoClassifyTarget.isChecked());
args.putString("sync", etSyncDays.getText().toString());
args.putString("keep", cbKeepAll.isChecked()
? Integer.toString(Integer.MAX_VALUE)
@@ -469,7 +481,8 @@ public class FragmentFolder extends FragmentBase {
boolean poll = args.getBoolean("poll");
String factor = args.getString("factor");
boolean download = args.getBoolean("download");
boolean auto_classify = args.getBoolean("auto_classify");
boolean auto_classify_source = args.getBoolean("auto_classify_source");
boolean auto_classify_target = args.getBoolean("auto_classify_target");
String sync = args.getString("sync");
String keep = args.getString("keep");
boolean auto_delete = args.getBoolean("auto_delete");
@@ -530,7 +543,9 @@ public class FragmentFolder extends FragmentBase {
return true;
if (!Objects.equals(folder.download, download))
return true;
if (!Objects.equals(folder.auto_classify, auto_classify))
if (!Objects.equals(folder.auto_classify_source, auto_classify_source))
return true;
if (!Objects.equals(folder.auto_classify_target, auto_classify_target))
return true;
if (!Objects.equals(folder.sync_days, sync_days))
return true;
@@ -575,7 +590,8 @@ public class FragmentFolder extends FragmentBase {
create.poll = poll;
create.poll_factor = poll_factor;
create.download = download;
create.auto_classify = auto_classify;
create.auto_classify_source = auto_classify_source;
create.auto_classify_target = auto_classify_target;
create.sync_days = sync_days;
create.keep_days = keep_days;
create.auto_delete = auto_delete;
@@ -595,7 +611,8 @@ public class FragmentFolder extends FragmentBase {
db.folder().setFolderProperties(id,
folder.name.equals(name) ? null : name,
display, color, unified, navigation, notify, hide,
synchronize, poll, poll_factor, download, auto_classify,
synchronize, poll, poll_factor, download,
auto_classify_source, auto_classify_target,
sync_days, keep_days, auto_delete);
db.folder().setFolderError(id, null);

View File

@@ -61,10 +61,7 @@ public class MessageClassifier {
if (!isEnabled(context))
return;
if (!canClassify(folder.type))
return;
if (target != null && !canClassify(target.type))
if (!folder.auto_classify_source)
return;
long start = new Date().getTime();
@@ -111,7 +108,7 @@ public class MessageClassifier {
db.beginTransaction();
EntityFolder dest = db.folder().getFolderByName(folder.account, classified);
if (dest != null && dest.auto_classify &&
if (dest != null && dest.auto_classify_target &&
(pro || EntityFolder.JUNK.equals(dest.type))) {
EntityOperation.queue(context, message, EntityOperation.MOVE, dest.id, false, true);
message.ui_hide = true;
@@ -443,12 +440,6 @@ public class MessageClassifier {
return prefs.getBoolean("classification", false);
}
static boolean canClassify(@NonNull String folderType) {
return EntityFolder.INBOX.equals(folderType) ||
EntityFolder.JUNK.equals(folderType) ||
EntityFolder.USER.equals(folderType);
}
static File getFile(@NonNull Context context) {
return new File(context.getFilesDir(), "classifier.json");
}