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

839 lines
30 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
import static androidx.room.ForeignKey.CASCADE;
2018-12-01 09:17:49 +00:00
import android.content.Context;
2020-05-07 07:31:32 +00:00
import android.text.TextUtils;
2018-12-01 09:17:49 +00:00
import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey;
2019-02-17 19:29:51 +00:00
import org.json.JSONArray;
2018-09-14 11:33:22 +00:00
import org.json.JSONException;
import org.json.JSONObject;
2018-10-06 21:44:34 +00:00
import java.io.Serializable;
2018-11-15 07:20:48 +00:00
import java.text.Collator;
2020-05-07 07:31:32 +00:00
import java.util.ArrayList;
2018-08-02 13:33:06 +00:00
import java.util.Arrays;
2018-11-15 07:20:48 +00:00
import java.util.Collections;
import java.util.Comparator;
2019-02-17 18:04:22 +00:00
import java.util.Date;
2019-09-13 13:04:51 +00:00
import java.util.HashMap;
2018-08-02 13:33:06 +00:00
import java.util.List;
2018-11-15 07:20:48 +00:00
import java.util.Locale;
2019-09-13 13:04:51 +00:00
import java.util.Map;
2019-02-26 10:05:21 +00:00
import java.util.Objects;
2020-05-22 11:15:57 +00:00
import java.util.regex.Pattern;
2018-08-02 13:33:06 +00:00
@Entity(
tableName = EntityFolder.TABLE_NAME,
foreignKeys = {
@ForeignKey(childColumns = "account", entity = EntityAccount.class, parentColumns = "id", onDelete = CASCADE)
2018-08-02 13:33:06 +00:00
},
indices = {
@Index(value = {"account", "name"}, unique = true),
2018-08-02 13:33:06 +00:00
@Index(value = {"account"}),
@Index(value = {"name"}),
@Index(value = {"type"}),
@Index(value = {"unified"})
2018-08-02 13:33:06 +00:00
}
)
2019-05-01 05:58:45 +00:00
public class EntityFolder extends EntityOrder implements Serializable {
2018-08-02 13:33:06 +00:00
static final String TABLE_NAME = "folder";
@PrimaryKey(autoGenerate = true)
public Long id;
public Long account; // Outbox = null
2019-03-17 19:32:57 +00:00
public Long parent;
2019-07-24 20:01:27 +00:00
public Long uidv; // UIDValidity
2021-04-06 20:25:08 +00:00
public Long modseq;
2021-09-24 07:51:57 +00:00
public String namespace;
public Character separator;
@NonNull
public String name;
@NonNull
public String type;
2022-08-07 13:27:17 +00:00
public String inherited_type;
2023-01-18 19:54:42 +00:00
public String subtype;
@NonNull
2019-03-18 08:33:40 +00:00
public Integer level = 0; // obsolete
2018-12-03 09:39:44 +00:00
@NonNull
2020-12-17 14:05:44 +00:00
public Boolean local = false;
@NonNull
public Boolean synchronize;
@NonNull
public Boolean poll = false;
@NonNull
2020-02-25 20:06:17 +00:00
public Integer poll_factor = 1;
@NonNull
public Integer poll_count = 0;
@NonNull
public Boolean download = true;
2021-01-02 21:52:41 +00:00
@NonNull
2022-10-26 17:53:40 +00:00
public Boolean auto_classify = false; // Obsolete
2022-10-25 19:45:36 +00:00
@NonNull
public Boolean auto_classify_source = false;
@NonNull
public Boolean auto_classify_target = false;
2019-04-25 15:23:56 +00:00
public Boolean subscribed;
@NonNull
2018-11-14 09:49:59 +00:00
public Integer sync_days;
@NonNull
public Integer keep_days;
@NonNull
public Boolean auto_delete = false;
2021-10-26 07:39:31 +00:00
public Boolean auto_add; // sent messages
public String display;
2019-10-07 16:15:02 +00:00
public Integer color;
@NonNull
2019-06-15 09:58:56 +00:00
public Boolean hide = false;
@NonNull
public Boolean hide_seen = false;
@NonNull
2019-03-18 08:33:40 +00:00
public Boolean collapsed = false;
@NonNull
public Boolean unified = false;
@NonNull
public Boolean navigation = false;
@NonNull
2023-02-03 08:38:06 +00:00
public Boolean count_unread = true;
@NonNull
public Boolean notify = false;
2019-03-09 08:20:46 +00:00
public Integer total; // messages on server
2023-04-18 15:59:28 +00:00
public String[] flags; // system flags
public String[] keywords; // user flags
2019-01-05 07:38:16 +00:00
2021-04-01 20:00:18 +00:00
@NonNull
public Long selected_last = 0L;
@NonNull
public Integer selected_count = 0;
2019-01-05 07:38:16 +00:00
@NonNull
2019-06-24 10:45:52 +00:00
public Integer initialize = DEFAULT_KEEP;
2018-12-22 11:40:47 +00:00
public Boolean tbc; // to be created
public Boolean tbd; // to be deleted
2019-07-29 19:53:32 +00:00
public String rename;
2018-08-13 14:44:47 +00:00
public String state;
2018-12-03 12:41:36 +00:00
public String sync_state;
2019-04-30 07:06:32 +00:00
@NonNull
public Boolean read_only = false;
2019-07-07 06:17:27 +00:00
@NonNull
2022-11-16 14:52:20 +00:00
public Boolean selectable = true; // Can contain messages
2020-02-24 08:32:03 +00:00
@NonNull
2022-11-16 14:52:20 +00:00
public Boolean inferiors = true; // Can have child folders
public String error;
2019-01-01 18:49:21 +00:00
public Long last_sync;
2021-12-07 08:16:36 +00:00
public Long last_sync_foreground;
2021-07-23 20:31:23 +00:00
public Integer last_sync_count; // POP3
2024-03-02 20:37:59 +00:00
public Long last_view;
2018-08-09 07:02:41 +00:00
static final String INBOX = "Inbox";
static final String OUTBOX = "Outbox";
static final String ARCHIVE = "All";
static final String DRAFTS = "Drafts";
static final String TRASH = "Trash";
static final String JUNK = "Junk";
static final String SENT = "Sent";
static final String SYSTEM = "System";
2018-08-09 07:02:41 +00:00
static final String USER = "User";
2018-08-02 13:33:06 +00:00
2023-01-18 19:54:42 +00:00
static final String FLAGGED = "flagged";
2020-02-20 19:17:20 +00:00
// https://tools.ietf.org/html/rfc6154
2018-12-17 08:49:36 +00:00
// https://www.iana.org/assignments/imap-mailbox-name-attributes/imap-mailbox-name-attributes.xhtml
2019-03-14 19:33:01 +00:00
private static final List<String> SYSTEM_FOLDER_ATTR = Collections.unmodifiableList(Arrays.asList(
2020-02-24 08:32:03 +00:00
"all",
"archive",
"drafts",
"trash",
"junk",
"sent",
"important",
"flagged"
2019-03-14 19:33:01 +00:00
));
private static final List<String> SYSTEM_FOLDER_TYPE = Collections.unmodifiableList(Arrays.asList(
2019-02-02 11:50:57 +00:00
ARCHIVE, // All
2018-08-09 07:02:41 +00:00
ARCHIVE,
DRAFTS,
TRASH,
JUNK,
SENT,
2019-02-02 11:50:57 +00:00
SYSTEM, // Important
SYSTEM // Flagged
2019-03-14 19:33:01 +00:00
)); // MUST match SYSTEM_FOLDER_ATTR
2018-08-02 13:33:06 +00:00
2019-03-14 19:33:01 +00:00
static final List<String> FOLDER_SORT_ORDER = Collections.unmodifiableList(Arrays.asList(
2018-12-17 08:50:11 +00:00
INBOX,
2018-12-18 15:42:28 +00:00
OUTBOX,
2018-08-09 07:02:41 +00:00
DRAFTS,
SENT,
TRASH,
JUNK,
SYSTEM,
2020-02-10 10:33:57 +00:00
USER,
ARCHIVE
2019-03-14 19:33:01 +00:00
));
2018-08-06 16:01:56 +00:00
2020-05-07 07:31:32 +00:00
private static Map<String, TypeScore> GUESS_FOLDER_TYPE = new HashMap<String, TypeScore>() {{
2020-04-08 18:33:27 +00:00
// Contains:
2021-10-11 17:15:31 +00:00
//put("all", new TypeScore(EntityFolder.ARCHIVE, 50));
//put("Tous", new TypeScore(EntityFolder.ARCHIVE, 50));
2021-04-15 10:21:01 +00:00
put("Archiv", new TypeScore(EntityFolder.ARCHIVE, 100)); // German
2020-05-07 07:31:32 +00:00
put("archive", new TypeScore(EntityFolder.ARCHIVE, 100));
2020-05-16 17:50:23 +00:00
put("archief", new TypeScore(EntityFolder.ARCHIVE, 100)); // Dutch
2021-03-29 05:58:28 +00:00
put("Архив", new TypeScore(EntityFolder.ARCHIVE, 100));
2021-10-02 13:58:11 +00:00
put("Wszystkie", new TypeScore(EntityFolder.ARCHIVE, 100)); // Polish
2023-04-07 11:36:32 +00:00
put("Arkiv", new TypeScore(EntityFolder.ARCHIVE, 100)); // Norwegian
2020-05-07 09:48:43 +00:00
2020-05-07 07:31:32 +00:00
put("draft", new TypeScore(EntityFolder.DRAFTS, 100));
put("concept", new TypeScore(EntityFolder.DRAFTS, 100));
2020-05-07 09:48:43 +00:00
put("Entwurf", new TypeScore(EntityFolder.DRAFTS, 100));
2020-05-07 07:31:32 +00:00
put("brouillon", new TypeScore(EntityFolder.DRAFTS, 100));
2020-05-07 09:48:43 +00:00
put("Черновики", new TypeScore(EntityFolder.DRAFTS, 100));
2020-07-28 19:43:39 +00:00
put("Bozze", new TypeScore(EntityFolder.DRAFTS, 100));
2021-08-03 08:03:09 +00:00
put("Szkice lokalne", new TypeScore(EntityFolder.DRAFTS, 100)); // Polish
2022-12-02 21:48:19 +00:00
put("Wersje robocze", new TypeScore(EntityFolder.DRAFTS, 100)); // Polish
2020-05-07 09:48:43 +00:00
2020-05-07 07:31:32 +00:00
put("trash", new TypeScore(EntityFolder.TRASH, 100));
2021-10-11 17:15:31 +00:00
put("Deleted", new TypeScore(EntityFolder.TRASH, 100));
2021-08-07 16:15:24 +00:00
//put("Bin", new TypeScore(EntityFolder.TRASH, 50));
2020-05-07 09:48:43 +00:00
put("Papierkorb", new TypeScore(EntityFolder.TRASH, 100));
2020-05-07 07:31:32 +00:00
put("corbeille", new TypeScore(EntityFolder.TRASH, 100));
2020-05-07 09:48:43 +00:00
put("Корзина", new TypeScore(EntityFolder.TRASH, 100));
2021-10-02 13:58:11 +00:00
put("Удаленные", new TypeScore(EntityFolder.TRASH, 50));
2021-03-30 10:07:50 +00:00
put("Eliminata", new TypeScore(EntityFolder.TRASH, 100));
2021-08-03 08:03:09 +00:00
put("Kosz", new TypeScore(EntityFolder.TRASH, 100)); // Polish
2021-10-02 13:58:11 +00:00
put("supprimé", new TypeScore(EntityFolder.TRASH, 100));
2020-05-07 09:48:43 +00:00
2020-05-07 07:31:32 +00:00
put("junk", new TypeScore(EntityFolder.JUNK, 100));
put("spam", new TypeScore(EntityFolder.JUNK, 100));
2021-10-07 14:24:49 +00:00
put("bulk", new TypeScore(EntityFolder.JUNK, 100));
2020-05-07 07:31:32 +00:00
put("pourriel", new TypeScore(EntityFolder.JUNK, 100));
put("quarantaine", new TypeScore(EntityFolder.JUNK, 50));
2020-05-07 09:48:43 +00:00
put("Спам", new TypeScore(EntityFolder.JUNK, 100));
2020-07-28 19:43:39 +00:00
put("Cestino", new TypeScore(EntityFolder.JUNK, 100));
2021-03-29 16:42:29 +00:00
put("Indesiderata", new TypeScore(EntityFolder.JUNK, 100));
2021-10-02 13:58:11 +00:00
put("indésirable", new TypeScore(EntityFolder.JUNK, 100));
2022-12-02 21:48:19 +00:00
put("Wiadomości-śmieci", new TypeScore(EntityFolder.JUNK, 100)); // Polish
2020-05-07 09:48:43 +00:00
2020-05-07 07:31:32 +00:00
put("sent", new TypeScore(EntityFolder.SENT, 100));
2020-05-07 09:48:43 +00:00
put("Gesendet", new TypeScore(EntityFolder.SENT, 100));
2020-05-07 07:31:32 +00:00
put("envoyé", new TypeScore(EntityFolder.SENT, 100));
2020-05-07 09:48:43 +00:00
put("Отправленные", new TypeScore(EntityFolder.SENT, 100));
2020-07-28 19:43:39 +00:00
put("Inviata", new TypeScore(EntityFolder.SENT, 100));
2022-12-02 21:48:19 +00:00
put("wysłane", new TypeScore(EntityFolder.SENT, 100)); // Polish
2019-09-13 13:04:51 +00:00
}};
2019-01-01 13:37:03 +00:00
static final int DEFAULT_SYNC = 7; // days
static final int DEFAULT_KEEP = 30; // days
2020-07-27 13:05:25 +00:00
static final int DEFAULT_KEEP_DRAFTS = 180; // days
2018-08-06 20:14:11 +00:00
private static final List<String> SYSTEM_FOLDER_SYNC = Collections.unmodifiableList(Arrays.asList(
2019-02-02 07:28:14 +00:00
INBOX,
2018-08-09 07:02:41 +00:00
DRAFTS,
2018-12-08 07:46:22 +00:00
SENT,
ARCHIVE,
2018-12-27 14:49:47 +00:00
TRASH,
JUNK
2019-03-14 19:33:01 +00:00
));
private static final List<Boolean> SYSTEM_FOLDER_POLL = Collections.unmodifiableList(Arrays.asList(
2020-11-17 11:42:43 +00:00
false, // inbox = push messages
true, // drafts
true, // sent
2020-10-07 12:11:55 +00:00
true, // archive
2020-09-29 19:49:38 +00:00
true, // trash
true // junk
)); // MUST match SYSTEM_FOLDER_SYNC
private static final List<Boolean> SYSTEM_FOLDER_DOWNLOAD = Collections.unmodifiableList(Arrays.asList(
2019-02-02 07:28:14 +00:00
true, // inbox
true, // drafts
true, // sent
true, // archive
false, // trash
false // junk
2019-03-14 19:33:01 +00:00
)); // MUST match SYSTEM_FOLDER_SYNC
2018-08-06 20:14:11 +00:00
2018-08-15 09:50:39 +00:00
public EntityFolder() {
}
2019-09-13 13:04:51 +00:00
public EntityFolder(String fullName, String type) {
this.name = fullName;
this.type = type;
2020-02-06 11:24:32 +00:00
setProperties();
}
2019-09-13 13:04:51 +00:00
2020-02-06 11:24:32 +00:00
void setProperties() {
2019-09-13 13:04:51 +00:00
int sync = EntityFolder.SYSTEM_FOLDER_SYNC.indexOf(type);
this.synchronize = (sync >= 0);
2020-09-29 19:49:38 +00:00
this.poll = (sync < 0 || EntityFolder.SYSTEM_FOLDER_POLL.get(sync));
2019-09-13 13:04:51 +00:00
this.download = (sync < 0 || EntityFolder.SYSTEM_FOLDER_DOWNLOAD.get(sync));
this.sync_days = EntityFolder.DEFAULT_SYNC;
this.keep_days = EntityFolder.DEFAULT_KEEP;
if (EntityFolder.INBOX.equals(type)) {
this.unified = true;
this.notify = true;
this.auto_classify_source = true;
2019-09-13 13:04:51 +00:00
}
2020-07-27 13:05:25 +00:00
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;
2019-09-13 13:04:51 +00:00
}
2020-12-01 20:07:13 +00:00
void setSpecials(EntityAccount account) {
2022-06-09 08:23:24 +00:00
if ("imap.web.de".equals(account.host) && "Unbekannt".equals(name)) {
// In den Ordner Unbekannt werden E-Mails einsortiert,
// die nicht als Spam erkannt werden
// und deren Absender nicht in Ihrem Adressbuch oder auf Ihrer Erwünschtliste stehen.
2020-12-01 20:07:13 +00:00
synchronize = true;
2022-06-09 08:23:24 +00:00
unified = true;
2022-07-01 05:44:29 +00:00
notify = true;
2022-06-09 08:23:24 +00:00
}
2023-10-04 14:48:44 +00:00
if ("poczta.o2.pl".equals(account.host) && INBOX.equals(name))
poll = true;
2020-12-01 20:07:13 +00:00
}
void inheritFrom(EntityFolder parent) {
if (parent == null)
return;
2023-04-25 14:07:39 +00:00
if (!EntityFolder.USER.equals(type))
return;
2023-09-22 12:28:42 +00:00
if (!EntityFolder.USER.equals(parent.type))
return;
this.synchronize = parent.synchronize;
this.download = parent.download;
this.sync_days = parent.sync_days;
this.keep_days = parent.keep_days;
this.notify = parent.notify;
}
2020-09-29 19:49:38 +00:00
static boolean shouldPoll(String type) {
int sync = EntityFolder.SYSTEM_FOLDER_SYNC.indexOf(type);
return (sync < 0 || EntityFolder.SYSTEM_FOLDER_POLL.get(sync));
}
2022-04-17 08:18:25 +00:00
static List<EntityFolder> getPopFolders(Context context) {
List<EntityFolder> result = new ArrayList<>();
EntityFolder inbox = new EntityFolder();
inbox.name = "INBOX";
inbox.type = EntityFolder.INBOX;
inbox.synchronize = true;
inbox.unified = true;
inbox.notify = true;
inbox.sync_days = Integer.MAX_VALUE;
inbox.keep_days = Integer.MAX_VALUE;
inbox.initialize = 0;
result.add(inbox);
EntityFolder drafts = new EntityFolder();
drafts.name = context.getString(R.string.title_folder_drafts);
drafts.type = EntityFolder.DRAFTS;
drafts.synchronize = false;
drafts.unified = false;
drafts.notify = false;
drafts.sync_days = Integer.MAX_VALUE;
drafts.keep_days = Integer.MAX_VALUE;
drafts.initialize = 0;
result.add(drafts);
EntityFolder sent = new EntityFolder();
sent.name = context.getString(R.string.title_folder_sent);
sent.type = EntityFolder.SENT;
sent.synchronize = false;
sent.unified = false;
sent.notify = false;
sent.sync_days = Integer.MAX_VALUE;
sent.keep_days = Integer.MAX_VALUE;
sent.initialize = 0;
result.add(sent);
EntityFolder trash = new EntityFolder();
trash.name = context.getString(R.string.title_folder_trash);
trash.type = EntityFolder.TRASH;
trash.synchronize = false;
trash.unified = false;
trash.notify = false;
trash.sync_days = Integer.MAX_VALUE;
trash.keep_days = Integer.MAX_VALUE;
trash.initialize = 0;
result.add(trash);
return result;
}
2023-09-04 13:38:05 +00:00
@NonNull
static EntityFolder getOutbox(Context context) {
DB db = DB.getInstance(context);
EntityFolder outbox = db.folder().getOutbox();
if (outbox != null)
return outbox;
Log.w("Outbox missing");
outbox = new EntityFolder();
2020-03-25 07:47:25 +00:00
outbox.name = "OUTBOX";
outbox.type = EntityFolder.OUTBOX;
outbox.synchronize = false;
outbox.sync_days = 0;
outbox.keep_days = 0;
2023-09-04 13:38:05 +00:00
outbox.id = db.folder().insertFolder(outbox);
2020-03-25 07:47:25 +00:00
return outbox;
}
static String getNotificationChannelId(long id) {
return "notification.folder." + id;
}
2020-12-02 15:58:32 +00:00
JSONArray getSyncArgs(boolean force) {
2019-02-17 18:04:22 +00:00
int days = sync_days;
if (last_sync != null) {
int ago_days = (int) ((new Date().getTime() - last_sync) / (24 * 3600 * 1000L)) + 1;
if (ago_days > days)
days = ago_days;
}
2019-02-17 19:29:51 +00:00
JSONArray jargs = new JSONArray();
2019-08-17 08:15:40 +00:00
jargs.put(initialize == 0 ? Math.min(days, keep_days) : Math.min(keep_days, initialize));
2019-02-17 19:29:51 +00:00
jargs.put(keep_days);
jargs.put(download);
jargs.put(auto_delete);
2019-07-19 05:11:05 +00:00
jargs.put(initialize);
2020-12-02 15:58:32 +00:00
jargs.put(force);
2019-02-17 19:29:51 +00:00
return jargs;
2019-02-17 18:04:22 +00:00
}
2021-07-30 14:23:07 +00:00
static boolean isSyncForced(String args) throws JSONException {
JSONArray jargs = new JSONArray(args);
return jargs.optBoolean(5, false);
}
2019-01-21 18:54:33 +00:00
static int getIcon(String type) {
if (EntityFolder.INBOX.equals(type))
2020-09-28 07:18:56 +00:00
return R.drawable.twotone_inbox_24;
2019-07-23 07:21:46 +00:00
if (EntityFolder.OUTBOX.equals(type))
2022-01-13 06:10:28 +00:00
return R.drawable.twotone_outbox_24;
2019-05-11 18:17:36 +00:00
if (EntityFolder.DRAFTS.equals(type))
2020-09-28 07:18:56 +00:00
return R.drawable.twotone_drafts_24;
2019-05-22 14:29:18 +00:00
if (EntityFolder.SENT.equals(type))
2022-01-13 06:10:28 +00:00
return R.drawable.twotone_send_24;
2019-01-21 18:54:33 +00:00
if (EntityFolder.ARCHIVE.equals(type))
2020-09-28 07:18:56 +00:00
return R.drawable.twotone_archive_24;
2019-01-21 18:54:33 +00:00
if (EntityFolder.TRASH.equals(type))
2020-09-28 07:18:56 +00:00
return R.drawable.twotone_delete_24;
2019-01-21 18:54:33 +00:00
if (EntityFolder.JUNK.equals(type))
2021-09-04 14:24:22 +00:00
return R.drawable.twotone_report_24;
if (EntityFolder.SYSTEM.equals(type))
2020-09-28 07:18:56 +00:00
return R.drawable.twotone_folder_special_24;
return R.drawable.twotone_folder_24;
2019-01-21 18:54:33 +00:00
}
2021-05-13 14:01:41 +00:00
static Integer getDefaultColor(String type, Context context) {
2020-12-23 08:10:07 +00:00
if (EntityFolder.TRASH.equals(type) || EntityFolder.JUNK.equals(type))
2023-07-21 05:16:41 +00:00
return Helper.resolveColor(context, androidx.appcompat.R.attr.colorError);
2020-12-23 08:10:07 +00:00
return null;
}
2021-05-13 14:01:41 +00:00
static Integer getDefaultColor(Long action, String type, Context context) {
2021-05-13 13:27:28 +00:00
if (EntityMessage.SWIPE_ACTION_DELETE.equals(action))
2023-07-21 05:16:41 +00:00
return Helper.resolveColor(context, androidx.appcompat.R.attr.colorError);
2021-05-13 14:01:41 +00:00
return getDefaultColor(type, context);
2021-05-13 13:27:28 +00:00
}
2018-12-01 09:17:49 +00:00
String getDisplayName(Context context) {
2020-06-06 08:02:11 +00:00
return (display == null ? localizeName(context, name) : display);
2018-12-01 09:17:49 +00:00
}
2019-03-18 08:33:40 +00:00
String getDisplayName(Context context, EntityFolder parent) {
String n = name;
2023-02-10 07:14:10 +00:00
if (parent != null &&
n.startsWith(parent.name) &&
n.length() > parent.name.length() + 1)
2019-03-18 08:33:40 +00:00
n = n.substring(parent.name.length() + 1);
2020-06-06 08:02:11 +00:00
return (display == null ? localizeName(context, n) : display);
2019-03-18 08:33:40 +00:00
}
2019-05-01 05:58:45 +00:00
@Override
Long getSortId() {
return id;
}
2019-05-01 11:08:15 +00:00
@Override
String[] getSortTitle(Context context) {
return new String[]{getDisplayName(context), null};
2019-05-01 05:58:45 +00:00
}
2018-11-11 07:07:41 +00:00
boolean isOutgoing() {
return isOutgoing(this.type);
}
static boolean isOutgoing(String type) {
return DRAFTS.equals(type) || OUTBOX.equals(type) || SENT.equals(type);
}
2019-07-07 06:17:27 +00:00
static String getType(String[] attrs, String fullName, boolean selectable) {
2020-02-06 10:19:44 +00:00
// https://tools.ietf.org/html/rfc3501#section-5.1
2020-02-15 14:53:11 +00:00
if ("INBOX".equals(fullName.toUpperCase(Locale.ROOT)))
2020-02-06 10:19:44 +00:00
return INBOX;
2019-06-15 17:28:57 +00:00
// https://www.iana.org/assignments/imap-mailbox-name-attributes/imap-mailbox-name-attributes.xhtml
2019-02-01 21:06:14 +00:00
for (String attr : attrs) {
2020-02-24 08:32:03 +00:00
if ((selectable && "\\NoSelect".equalsIgnoreCase(attr)) || "\\NonExistent".equalsIgnoreCase(attr))
2019-02-01 21:06:14 +00:00
return null;
if (attr.startsWith("\\")) {
2021-05-01 14:55:00 +00:00
int index = SYSTEM_FOLDER_ATTR.indexOf(attr.substring(1).toLowerCase(Locale.ROOT));
2019-02-01 21:06:14 +00:00
if (index >= 0)
return SYSTEM_FOLDER_TYPE.get(index);
}
}
return USER;
}
2023-01-18 19:54:42 +00:00
static String getSubtype(String[] attrs, String fullname) {
for (String attr : attrs)
if ("\\Flagged".equals(attr)) // Gmail
return FLAGGED;
return null;
}
2020-05-07 07:31:32 +00:00
private static class TypeScore {
2020-05-07 09:48:43 +00:00
@NonNull
private String type;
private int score;
2020-05-07 07:31:32 +00:00
2020-05-07 09:48:43 +00:00
TypeScore(@NonNull String type, int score) {
2020-05-07 07:31:32 +00:00
this.score = score;
this.type = type;
}
}
private static class FolderScore {
2020-05-07 09:48:43 +00:00
@NonNull
private EntityFolder folder;
private int score;
2020-05-07 07:31:32 +00:00
2020-05-07 09:48:43 +00:00
FolderScore(@NonNull EntityFolder folder, int score) {
2020-05-07 07:31:32 +00:00
this.score = score;
this.folder = folder;
}
@NonNull
@Override
public String toString() {
return folder.name + ":" + score;
}
}
2021-09-24 07:51:57 +00:00
static void guessTypes(List<EntityFolder> folders) {
2020-05-07 07:31:32 +00:00
List<String> types = new ArrayList<>();
Map<String, List<FolderScore>> typeFolderScore = new HashMap<>();
for (EntityFolder folder : folders)
2020-05-07 09:48:43 +00:00
if (EntityFolder.USER.equals(folder.type)) {
2020-05-07 07:31:32 +00:00
for (String guess : GUESS_FOLDER_TYPE.keySet())
2020-05-07 09:48:43 +00:00
if (folder.name.toLowerCase(Locale.ROOT).contains(guess.toLowerCase(Locale.ROOT))) {
2020-05-07 07:31:32 +00:00
TypeScore score = GUESS_FOLDER_TYPE.get(guess);
if (!typeFolderScore.containsKey(score.type))
typeFolderScore.put(score.type, new ArrayList<>());
typeFolderScore.get(score.type).add(new FolderScore(folder, score.score));
break;
}
} else
types.add(folder.type);
for (String type : typeFolderScore.keySet()) {
List<FolderScore> candidates = typeFolderScore.get(type);
Log.i("Guess type=" + type + " candidates=" + TextUtils.join(", ", candidates));
if (!types.contains(type)) {
Collections.sort(candidates, new Comparator<FolderScore>() {
@Override
public int compare(FolderScore fs1, FolderScore fs2) {
int r = Boolean.compare(fs1.folder.read_only, fs2.folder.read_only);
if (r != 0)
return r;
2020-05-07 07:31:32 +00:00
int s = Integer.compare(fs1.score, fs2.score);
if (s == 0) {
2021-09-24 07:51:57 +00:00
int len1 = (fs1.folder.separator == null ? 1
: fs1.folder.name.split(Pattern.quote(String.valueOf(fs1.folder.separator))).length);
int len2 = (fs2.folder.separator == null ? 1
: fs2.folder.name.split(Pattern.quote(String.valueOf(fs2.folder.separator))).length);
return Integer.compare(len1, len2);
2020-05-07 07:31:32 +00:00
} else
return s;
}
});
candidates.get(0).folder.type = type;
candidates.get(0).folder.setProperties();
Log.i(candidates.get(0).folder.name + " guessed type=" + type);
types.add(type);
}
}
2019-09-13 13:04:51 +00:00
}
2021-09-24 07:51:57 +00:00
String getParentName() {
2019-03-17 19:32:57 +00:00
if (separator == null)
return null;
else {
int p = name.lastIndexOf(separator);
if (p < 0)
return null;
else
return name.substring(0, p);
}
}
2020-06-06 08:02:11 +00:00
static String localizeType(Context context, String type) {
int resid = context.getResources().getIdentifier(
"title_folder_" + type.toLowerCase(Locale.ROOT),
"string",
context.getPackageName());
return (resid > 0 ? context.getString(resid) : type);
}
static String localizeName(Context context, String name) {
if (name != null && "INBOX".equals(name.toUpperCase(Locale.ROOT)))
return context.getString(R.string.title_folder_inbox);
else if ("OUTBOX".equals(name))
return context.getString(R.string.title_folder_outbox);
else
return name;
}
2018-08-02 13:33:06 +00:00
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityFolder) {
EntityFolder other = (EntityFolder) obj;
2018-08-15 19:34:22 +00:00
return (this.id.equals(other.id) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.account, other.account) &&
2019-07-24 20:02:17 +00:00
Objects.equals(this.parent, other.parent) &&
2019-07-24 20:01:27 +00:00
Objects.equals(this.uidv, other.uidv) &&
2021-09-24 07:51:57 +00:00
Objects.equals(this.namespace, other.namespace) &&
Objects.equals(this.separator, other.separator) &&
2018-08-02 13:33:06 +00:00
this.name.equals(other.name) &&
this.type.equals(other.type) &&
2019-07-24 20:02:17 +00:00
this.level.equals(other.level) &&
2018-08-02 13:33:06 +00:00
this.synchronize.equals(other.synchronize) &&
this.poll.equals(other.poll) &&
2019-01-05 07:38:16 +00:00
this.download.equals(other.download) &&
this.auto_classify_source.equals(other.auto_classify_source) &&
this.auto_classify_target.equals(other.auto_classify_target) &&
2019-04-25 15:23:56 +00:00
Objects.equals(this.subscribed, other.subscribed) &&
this.sync_days.equals(other.sync_days) &&
this.keep_days.equals(other.keep_days) &&
2019-07-24 20:02:17 +00:00
this.auto_delete.equals(other.auto_delete) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.display, other.display) &&
2019-10-07 16:15:02 +00:00
Objects.equals(this.color, other.color) &&
2019-04-29 11:09:11 +00:00
Objects.equals(this.order, other.order) &&
2019-06-15 09:58:56 +00:00
this.hide == other.hide &&
this.hide_seen == other.hide_seen &&
2019-03-18 08:33:40 +00:00
this.collapsed == other.collapsed &&
this.unified == other.unified &&
2019-07-24 20:02:17 +00:00
this.navigation == other.navigation &&
this.count_unread == other.count_unread &&
this.notify == other.notify &&
2019-03-09 08:20:46 +00:00
Objects.equals(this.total, other.total) &&
Helper.equal(this.keywords, other.keywords) &&
2019-07-24 20:02:17 +00:00
this.initialize.equals(other.initialize) &&
2019-04-29 11:09:11 +00:00
Objects.equals(this.tbc, other.tbc) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.tbd, other.tbd) &&
2019-10-07 16:15:02 +00:00
Objects.equals(this.rename, other.rename) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.state, other.state) &&
Objects.equals(this.sync_state, other.sync_state) &&
2019-07-24 20:02:17 +00:00
this.read_only == other.read_only &&
this.selectable == other.selectable &&
2020-03-25 21:40:50 +00:00
this.inferiors == other.inferiors &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.error, other.error) &&
Objects.equals(this.last_sync, other.last_sync));
2018-08-02 13:33:06 +00:00
} else
return false;
}
2018-08-10 09:45:36 +00:00
@Override
public String toString() {
2019-05-28 11:42:19 +00:00
return name;
2018-08-10 09:45:36 +00:00
}
2018-08-15 09:50:39 +00:00
2018-09-14 11:33:22 +00:00
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put("id", id);
2019-06-13 20:08:43 +00:00
json.put("order", order);
2021-09-24 07:51:57 +00:00
json.put("namespace", namespace);
if (separator != null)
json.put("separator", (int) separator);
2018-09-14 11:33:22 +00:00
json.put("name", name);
json.put("type", type);
json.put("synchronize", synchronize);
json.put("poll", poll);
2020-04-23 12:13:16 +00:00
json.put("poll_factor", poll_factor);
2019-01-05 07:38:16 +00:00
json.put("download", download);
json.put("auto_classify_source", auto_classify_source);
json.put("auto_classify_target", auto_classify_target);
2018-11-14 09:49:59 +00:00
json.put("sync_days", sync_days);
json.put("keep_days", keep_days);
json.put("auto_delete", auto_delete);
2018-10-06 21:36:11 +00:00
json.put("display", display);
2019-10-07 16:15:02 +00:00
json.put("color", color);
2019-06-15 09:58:56 +00:00
json.put("hide", hide);
json.put("hide_seen", hide_seen);
2019-03-18 08:33:40 +00:00
json.put("collapsed", collapsed);
2018-10-06 21:36:11 +00:00
json.put("unified", unified);
json.put("navigation", navigation);
json.put("count_unread", count_unread);
json.put("notify", notify);
2018-09-14 11:33:22 +00:00
return json;
}
public static EntityFolder fromJSON(JSONObject json) throws JSONException {
EntityFolder folder = new EntityFolder();
2019-02-28 11:04:34 +00:00
if (json.has("id"))
folder.id = json.getLong("id");
2019-06-13 20:08:43 +00:00
if (json.has("order"))
folder.order = json.getInt("order");
2021-09-24 07:51:57 +00:00
if (json.has("namespace"))
folder.namespace = json.getString("namespace");
if (json.has("separator"))
folder.separator = (char) json.getInt("separator");
2018-09-14 11:33:22 +00:00
folder.name = json.getString("name");
folder.type = json.getString("type");
2018-12-03 09:39:44 +00:00
2018-09-14 11:33:22 +00:00
folder.synchronize = json.getBoolean("synchronize");
2019-01-05 07:38:16 +00:00
if (json.has("poll"))
folder.poll = json.getBoolean("poll");
2020-04-23 12:13:16 +00:00
if (json.has("poll_factor"))
folder.poll_factor = json.getInt("poll_factor");
2019-01-05 07:38:16 +00:00
if (json.has("download"))
folder.download = json.getBoolean("download");
2018-11-14 09:49:59 +00:00
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");
2021-01-04 09:46:20 +00:00
2018-11-14 09:49:59 +00:00
if (json.has("after"))
folder.sync_days = json.getInt("after");
else
folder.sync_days = json.getInt("sync_days");
if (json.has("keep_days"))
folder.keep_days = json.getInt("keep_days");
else
folder.keep_days = folder.sync_days;
if (json.has("auto_delete"))
folder.auto_delete = json.getBoolean("auto_delete");
2019-03-14 17:52:15 +00:00
if (json.has("display") && !json.isNull("display"))
2018-10-06 21:36:11 +00:00
folder.display = json.getString("display");
2019-10-07 16:15:02 +00:00
if (json.has("color") && !json.isNull("color"))
folder.color = json.getInt("color");
2019-06-15 09:58:56 +00:00
if (json.has("hide"))
folder.hide = json.getBoolean("hide");
if (json.has("hide_seen"))
folder.hide_seen = json.getBoolean("hide_seen");
2019-03-18 08:33:40 +00:00
if (json.has("collapsed"))
folder.collapsed = json.getBoolean("collapsed");
2018-10-06 21:36:11 +00:00
folder.unified = json.getBoolean("unified");
if (json.has("navigation"))
folder.navigation = json.getBoolean("navigation");
if (json.has("count_unread"))
folder.count_unread = json.getBoolean("count_unread");
if (json.has("notify"))
folder.notify = json.getBoolean("notify");
2018-09-14 11:33:22 +00:00
return folder;
}
2018-11-15 07:20:48 +00:00
2019-05-06 20:34:18 +00:00
@Override
Comparator getComparator(final Context context) {
2018-11-15 07:20:48 +00:00
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
2019-05-06 20:34:18 +00:00
return new Comparator() {
2018-11-15 07:20:48 +00:00
@Override
2019-05-06 20:34:18 +00:00
public int compare(Object o1, Object o2) {
EntityFolder f1 = (EntityFolder) o1;
EntityFolder f2 = (EntityFolder) o2;
int o = Integer.compare(
f1.order == null ? -1 : f1.order,
f2.order == null ? -1 : f2.order);
if (o != 0)
return o;
2018-12-22 10:44:44 +00:00
int i1 = EntityFolder.FOLDER_SORT_ORDER.indexOf(f1.type);
int i2 = EntityFolder.FOLDER_SORT_ORDER.indexOf(f2.type);
int s = Integer.compare(i1, i2);
2018-11-15 07:20:48 +00:00
if (s != 0)
return s;
2019-03-04 07:48:36 +00:00
2019-05-06 20:34:18 +00:00
int c = -f1.synchronize.compareTo(f2.synchronize);
if (c != 0)
return c;
2019-03-04 07:48:36 +00:00
2019-06-28 07:46:18 +00:00
if (context == null)
return collator.compare(f1.name, f2.name);
2018-12-22 15:13:52 +00:00
String name1 = f1.getDisplayName(context);
String name2 = f2.getDisplayName(context);
return collator.compare(name1, name2);
2018-11-15 07:20:48 +00:00
}
2019-05-06 20:34:18 +00:00
};
2018-11-15 07:20:48 +00:00
}
2018-08-02 13:33:06 +00:00
}