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

405 lines
14 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
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
import android.app.Notification;
import android.app.NotificationChannel;
2019-05-28 14:55:03 +00:00
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
2019-11-23 12:48:59 +00:00
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
2018-09-14 11:33:22 +00:00
import org.json.JSONException;
import org.json.JSONObject;
2019-01-23 18:52:52 +00:00
import java.io.Serializable;
2019-05-06 20:34:18 +00:00
import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;
2019-02-26 10:05:21 +00:00
import java.util.Objects;
2019-01-23 18:52:52 +00:00
2018-08-02 13:33:06 +00:00
@Entity(
tableName = EntityAccount.TABLE_NAME,
indices = {
}
)
2019-05-01 05:58:45 +00:00
public class EntityAccount extends EntityOrder implements Serializable {
2018-08-02 13:33:06 +00:00
static final String TABLE_NAME = "account";
2019-07-08 17:23:07 +00:00
// https://tools.ietf.org/html/rfc2177
2019-12-28 17:36:15 +00:00
static final int DEFAULT_KEEP_ALIVE_INTERVAL = 15; // minutes
static final int DEFAULT_POLL_INTERVAL = 15; // minutes
2020-04-16 15:09:33 +00:00
static final int DEFAULT_MAX_MESSAGES = 100; // POP3
2019-11-23 12:48:59 +00:00
static final int TYPE_IMAP = 0;
static final int TYPE_POP = 1;
2018-08-02 13:33:06 +00:00
@PrimaryKey(autoGenerate = true)
public Long id;
2018-12-26 11:34:05 +00:00
2018-08-02 13:33:06 +00:00
@NonNull
2019-11-23 12:48:59 +00:00
@ColumnInfo(name = "pop")
public Integer protocol = TYPE_IMAP;
2019-02-09 21:03:53 +00:00
@NonNull
public String host; // POP3/IMAP
2018-08-02 13:33:06 +00:00
@NonNull
2020-08-23 07:28:10 +00:00
@ColumnInfo(name = "starttls")
public Integer encryption;
2018-10-23 07:52:20 +00:00
@NonNull
2019-09-18 14:34:07 +00:00
public Boolean insecure = false;
2018-10-23 07:52:20 +00:00
@NonNull
2018-08-02 13:33:06 +00:00
public Integer port;
@NonNull
public Integer auth_type; // immutable
2019-12-21 15:03:57 +00:00
public String provider;
2019-09-18 14:34:07 +00:00
@NonNull
2018-08-02 13:33:06 +00:00
public String user;
@NonNull
public String password;
@NonNull
2020-02-10 16:16:06 +00:00
public Boolean certificate = false; // obsolete
2020-02-10 15:35:14 +00:00
public String certificate_alias;
2019-01-10 18:24:20 +00:00
public String realm;
2019-12-16 18:09:49 +00:00
public String fingerprint;
2018-12-26 11:34:05 +00:00
public String name;
public String signature; // obsolete
public Integer color;
2018-08-27 14:31:45 +00:00
@NonNull
2018-08-02 13:33:06 +00:00
public Boolean synchronize;
2018-10-07 06:53:09 +00:00
@NonNull
2019-12-08 14:00:15 +00:00
public Boolean ondemand = false;
2019-02-17 18:29:00 +00:00
@NonNull
public Boolean poll_exempted = false;
@NonNull
2018-10-07 06:53:09 +00:00
public Boolean primary;
@NonNull
2019-09-18 14:34:07 +00:00
public Boolean notify = false;
@NonNull
2020-01-21 14:15:48 +00:00
public Boolean browse = true;
@NonNull
public Boolean leave_on_server = true;
@NonNull
public Boolean leave_deleted = false;
@NonNull
2020-01-21 14:15:48 +00:00
public Boolean leave_on_device = false;
public Integer max_messages; // POP3
@NonNull
public Boolean auto_seen = true;
2019-06-26 06:13:39 +00:00
public Character separator;
2019-01-20 15:22:21 +00:00
public Long swipe_left;
public Long swipe_right;
2019-10-23 10:51:20 +00:00
public Long move_to;
2018-12-26 11:34:05 +00:00
@NonNull
public Integer poll_interval = DEFAULT_KEEP_ALIVE_INTERVAL;
@NonNull
public Boolean keep_alive_ok = false;
@NonNull
public Integer keep_alive_failed = 0;
@NonNull
2020-04-01 06:57:27 +00:00
public Integer keep_alive_succeeded = 0;
@NonNull
public Boolean partial_fetch = true;
@NonNull
public Boolean ignore_size = false;
@NonNull
2020-07-22 12:57:47 +00:00
public Boolean use_date = false; // Date header
@NonNull
public Boolean use_received = false; // Received header
2019-05-28 11:42:19 +00:00
public String prefix; // namespace, obsolete
2018-12-26 11:34:05 +00:00
2020-01-07 16:43:27 +00:00
public Long quota_usage;
public Long quota_limit;
public Long created;
public Boolean tbd;
2020-03-08 10:08:28 +00:00
public Long thread;
2018-08-13 14:44:47 +00:00
public String state;
2019-03-27 08:19:11 +00:00
public String warning;
public String error;
2018-11-19 13:14:02 +00:00
public Long last_connected;
2020-07-03 07:57:43 +00:00
public Long max_size;
2018-08-02 13:33:06 +00:00
2020-04-26 16:02:18 +00:00
boolean isGmail() {
return "imap.gmail.com".equalsIgnoreCase(host);
}
2019-02-09 21:03:53 +00:00
String getProtocol() {
2019-11-23 12:48:59 +00:00
switch (protocol) {
case TYPE_IMAP:
2020-05-01 19:47:59 +00:00
if (isGmail())
return "gimaps";
else
2020-08-23 07:28:10 +00:00
return "imap" + (encryption == EmailService.ENCRYPTION_SSL ? "s" : "");
2019-11-23 12:48:59 +00:00
case TYPE_POP:
2020-08-23 07:28:10 +00:00
return "pop3" + (encryption == EmailService.ENCRYPTION_SSL ? "s" : "");
2019-11-23 12:48:59 +00:00
default:
throw new IllegalArgumentException("Unknown protocol=" + protocol);
}
2019-02-09 21:03:53 +00:00
}
2019-05-16 07:08:06 +00:00
static String getNotificationChannelId(long id) {
return "notification" + (id == 0 ? "" : "." + id);
}
@RequiresApi(api = Build.VERSION_CODES.O)
void createNotificationChannel(Context context) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
2019-06-08 07:51:00 +00:00
NotificationChannelGroup group = new NotificationChannelGroup("group." + id, name);
nm.createNotificationChannelGroup(group);
NotificationChannel channel = new NotificationChannel(
getNotificationChannelId(id), name,
NotificationManager.IMPORTANCE_HIGH);
2019-06-08 07:51:00 +00:00
channel.setGroup(group.getId());
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channel.enableLights(true);
nm.createNotificationChannel(channel);
}
@RequiresApi(api = Build.VERSION_CODES.O)
void deleteNotificationChannel(Context context) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.deleteNotificationChannel(getNotificationChannelId(id));
}
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[]{name, null};
}
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);
2019-11-23 12:48:59 +00:00
json.put("protocol", protocol);
2018-09-14 11:33:22 +00:00
json.put("host", host);
2020-08-23 07:28:10 +00:00
json.put("encryption", encryption);
2018-10-24 11:14:56 +00:00
json.put("insecure", insecure);
2018-09-14 11:33:22 +00:00
json.put("port", port);
2019-09-18 14:34:07 +00:00
json.put("auth_type", auth_type);
2020-01-13 07:51:37 +00:00
json.put("provider", provider);
2018-09-14 11:33:22 +00:00
json.put("user", user);
json.put("password", password);
2020-02-10 15:35:14 +00:00
json.put("certificate_alias", certificate_alias);
2019-02-17 11:34:29 +00:00
json.put("realm", realm);
2020-01-13 07:51:37 +00:00
json.put("fingerprint", fingerprint);
json.put("name", name);
json.put("color", color);
json.put("synchronize", synchronize);
2019-12-10 19:37:12 +00:00
json.put("ondemand", ondemand);
2020-01-13 07:51:37 +00:00
json.put("poll_exempted", poll_exempted);
2018-10-07 08:08:01 +00:00
json.put("primary", primary);
json.put("notify", notify);
json.put("browse", browse);
2020-01-21 14:15:48 +00:00
json.put("leave_on_server", leave_on_server);
2020-05-03 09:24:20 +00:00
json.put("leave_deleted", leave_deleted);
2020-01-21 14:15:48 +00:00
json.put("leave_on_device", leave_on_device);
2020-04-16 13:54:10 +00:00
json.put("max_messages", max_messages);
2019-09-23 15:39:45 +00:00
json.put("auto_seen", auto_seen);
// not separator
2019-01-20 15:22:21 +00:00
json.put("swipe_left", swipe_left);
json.put("swipe_right", swipe_right);
2019-10-23 13:29:22 +00:00
json.put("move_to", move_to);
2018-09-14 11:33:22 +00:00
json.put("poll_interval", poll_interval);
json.put("partial_fetch", partial_fetch);
json.put("ignore_size", ignore_size);
2020-01-13 07:51:37 +00:00
json.put("use_date", use_date);
2020-07-22 12:57:47 +00:00
json.put("use_received", use_received);
2019-09-23 15:39:45 +00:00
// not prefix
2018-11-10 10:45:03 +00:00
// not created
2019-09-23 15:39:45 +00:00
// not tbd
2018-11-10 10:45:03 +00:00
// not state
2019-09-23 15:39:45 +00:00
// not warning
2018-11-10 10:45:03 +00:00
// not error
2018-11-19 13:14:02 +00:00
// not last connected
2018-09-14 11:33:22 +00:00
return json;
}
public static EntityAccount fromJSON(JSONObject json) throws JSONException {
EntityAccount account = new EntityAccount();
if (json.has("id"))
account.id = json.getLong("id");
2019-06-13 20:08:43 +00:00
if (json.has("order"))
account.order = json.getInt("order");
2019-11-23 12:48:59 +00:00
if (json.has("protocol"))
account.protocol = json.getInt("protocol");
else if (json.has("pop"))
account.protocol = (json.getBoolean("pop") ? TYPE_POP : TYPE_IMAP);
2019-09-19 15:41:26 +00:00
2018-09-14 11:33:22 +00:00
account.host = json.getString("host");
2020-08-23 07:28:10 +00:00
if (json.has("starttls"))
account.encryption = (json.getBoolean("starttls")
? EmailService.ENCRYPTION_STARTTLS : EmailService.ENCRYPTION_SSL);
else
account.encryption = json.getInt("encryption");
2018-10-24 11:14:56 +00:00
account.insecure = (json.has("insecure") && json.getBoolean("insecure"));
2018-09-14 11:33:22 +00:00
account.port = json.getInt("port");
2019-09-18 14:34:07 +00:00
account.auth_type = json.getInt("auth_type");
2020-01-13 07:51:37 +00:00
if (json.has("provider"))
account.provider = json.getString("provider");
2018-09-14 11:33:22 +00:00
account.user = json.getString("user");
account.password = json.getString("password");
2020-02-11 07:10:01 +00:00
if (json.has("certificate_alias"))
account.certificate_alias = json.getString("certificate_alias");
if (json.has("realm"))
account.realm = json.getString("realm");
2020-01-13 07:51:37 +00:00
if (json.has("fingerprint"))
account.fingerprint = json.getString("fingerprint");
2019-03-14 17:52:15 +00:00
if (json.has("name") && !json.isNull("name"))
account.name = json.getString("name");
if (json.has("color"))
account.color = json.getInt("color");
2018-09-14 11:33:22 +00:00
account.synchronize = json.getBoolean("synchronize");
2019-12-10 19:37:12 +00:00
if (json.has("ondemand"))
account.ondemand = json.getBoolean("ondemand");
2020-01-13 07:51:37 +00:00
if (json.has("poll_exempted"))
account.poll_exempted = json.getBoolean("poll_exempted");
2018-10-07 08:08:01 +00:00
account.primary = json.getBoolean("primary");
if (json.has("notify"))
account.notify = json.getBoolean("notify");
if (json.has("browse"))
account.browse = json.getBoolean("browse");
2020-01-21 14:15:48 +00:00
if (json.has("leave_on_server"))
account.leave_on_server = json.getBoolean("leave_on_server");
2020-05-03 09:24:20 +00:00
if (json.has("leave_deleted"))
account.leave_deleted = json.getBoolean("leave_deleted");
2020-01-21 14:15:48 +00:00
if (json.has("leave_on_device"))
account.leave_on_device = json.getBoolean("leave_on_device");
2020-04-16 13:54:10 +00:00
if (json.has("max_messages"))
account.max_messages = json.getInt("max_messages");
2019-09-23 15:39:45 +00:00
if (json.has("auto_seen"))
account.auto_seen = json.getBoolean("auto_seen");
2019-01-20 15:22:21 +00:00
if (json.has("swipe_left"))
account.swipe_left = json.getLong("swipe_left");
if (json.has("swipe_right"))
account.swipe_right = json.getLong("swipe_right");
2019-10-23 13:29:22 +00:00
if (json.has("move_to"))
account.move_to = json.getLong("move_to");
2018-09-14 11:33:22 +00:00
account.poll_interval = json.getInt("poll_interval");
account.partial_fetch = json.optBoolean("partial_fetch", true);
account.ignore_size = json.optBoolean("ignore_size", false);
2020-01-13 07:51:37 +00:00
account.use_date = json.optBoolean("use_date", false);
2020-07-22 12:57:47 +00:00
account.use_received = json.optBoolean("use_received", false);
2018-09-14 11:33:22 +00:00
return account;
}
2018-11-10 10:45:03 +00:00
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityAccount) {
EntityAccount other = (EntityAccount) obj;
2019-09-23 15:39:45 +00:00
return (Objects.equals(this.order, other.order) &&
2019-11-23 12:48:59 +00:00
this.protocol.equals(other.protocol) &&
2019-09-23 15:39:45 +00:00
this.host.equals(other.host) &&
2020-08-23 07:28:10 +00:00
this.encryption.equals(other.encryption) &&
2018-11-10 10:45:03 +00:00
this.insecure == other.insecure &&
this.port.equals(other.port) &&
2019-09-18 14:34:07 +00:00
this.auth_type.equals(other.auth_type) &&
2018-11-10 10:45:03 +00:00
this.user.equals(other.user) &&
this.password.equals(other.password) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.realm, other.realm) &&
Objects.equals(this.name, other.name) &&
Objects.equals(this.color, other.color) &&
2018-11-10 10:45:03 +00:00
this.synchronize.equals(other.synchronize) &&
this.primary.equals(other.primary) &&
this.notify.equals(other.notify) &&
2019-01-23 09:16:33 +00:00
this.browse.equals(other.browse) &&
2020-01-21 14:15:48 +00:00
this.leave_on_server.equals(other.leave_on_server) &&
this.leave_on_device.equals(other.leave_on_device) &&
2020-04-16 13:54:10 +00:00
Objects.equals(this.max_messages, other.max_messages) &&
2019-09-23 15:39:45 +00:00
this.auto_seen.equals(other.auto_seen) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.swipe_left, other.swipe_left) &&
Objects.equals(this.swipe_right, other.swipe_right) &&
2018-11-10 10:45:03 +00:00
this.poll_interval.equals(other.poll_interval) &&
this.partial_fetch == other.partial_fetch &&
this.ignore_size == other.ignore_size &&
2020-07-21 18:47:47 +00:00
this.use_date == other.use_date &&
2020-07-22 12:57:47 +00:00
this.use_received == other.use_received &&
2020-01-07 16:43:27 +00:00
Objects.equals(this.quota_usage, other.quota_usage) &&
Objects.equals(this.quota_limit, other.quota_limit) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.created, other.created) &&
Objects.equals(this.tbd, other.tbd) &&
Objects.equals(this.state, other.state) &&
2019-03-27 08:19:11 +00:00
Objects.equals(this.warning, other.warning) &&
2019-02-26 10:05:21 +00:00
Objects.equals(this.error, other.error) &&
2020-07-03 07:57:43 +00:00
Objects.equals(this.last_connected, other.last_connected) &&
Objects.equals(this.max_size, other.max_size));
2018-11-10 10:45:03 +00:00
} else
return false;
}
2018-12-28 07:40:27 +00:00
2019-05-06 20:34:18 +00:00
@Override
Comparator getComparator(final Context context) {
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
return new Comparator() {
@Override
public int compare(Object o1, Object o2) {
EntityAccount a1 = (EntityAccount) o1;
EntityAccount a2 = (EntityAccount) o2;
int o = Integer.compare(
a1.order == null ? -1 : a1.order,
a2.order == null ? -1 : a2.order);
if (o != 0)
return o;
String name1 = (a1.name == null ? "" : a1.name);
String name2 = (a2.name == null ? "" : a2.name);
return collator.compare(name1, name2);
}
};
}
2018-12-28 07:40:27 +00:00
@NonNull
@Override
public String toString() {
return name + (primary ? "" : "");
}
2018-08-02 13:33:06 +00:00
}