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
|
|
|
|
2018-12-31 08:04:33 +00:00
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
2018-08-02 13:33:06 +00:00
|
|
|
*/
|
|
|
|
|
2018-12-10 11:26:50 +00:00
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationChannel;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.Build;
|
|
|
|
|
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-02-26 10:05:21 +00:00
|
|
|
import java.util.Objects;
|
2019-01-23 18:52:52 +00:00
|
|
|
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2018-12-10 11:26:50 +00:00
|
|
|
import androidx.annotation.RequiresApi;
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.room.Entity;
|
|
|
|
import androidx.room.PrimaryKey;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
@Entity(
|
|
|
|
tableName = EntityAccount.TABLE_NAME,
|
|
|
|
indices = {
|
|
|
|
}
|
|
|
|
)
|
2019-01-23 18:52:52 +00:00
|
|
|
public class EntityAccount implements Serializable {
|
2018-08-02 13:33:06 +00:00
|
|
|
static final String TABLE_NAME = "account";
|
|
|
|
|
|
|
|
@PrimaryKey(autoGenerate = true)
|
|
|
|
public Long id;
|
2018-12-26 11:34:05 +00:00
|
|
|
|
|
|
|
@NonNull
|
|
|
|
public Integer auth_type;
|
2018-08-02 13:33:06 +00:00
|
|
|
@NonNull
|
2019-02-11 21:46:12 +00:00
|
|
|
public Boolean pop = false; // obsolete
|
2019-02-09 21:03:53 +00:00
|
|
|
@NonNull
|
|
|
|
public String host; // POP3/IMAP
|
2018-08-02 13:33:06 +00:00
|
|
|
@NonNull
|
2018-10-23 07:52:20 +00:00
|
|
|
public Boolean starttls;
|
|
|
|
@NonNull
|
|
|
|
public Boolean insecure;
|
|
|
|
@NonNull
|
2018-08-02 13:33:06 +00:00
|
|
|
public Integer port;
|
|
|
|
@NonNull
|
|
|
|
public String user;
|
|
|
|
@NonNull
|
|
|
|
public String password;
|
2019-01-10 18:24:20 +00:00
|
|
|
public String realm;
|
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-03-01 14:32:18 +00:00
|
|
|
public Boolean ondemand = false; // obsolete
|
2019-02-17 18:29:00 +00:00
|
|
|
@NonNull
|
2018-10-07 06:53:09 +00:00
|
|
|
public Boolean primary;
|
2018-12-23 18:20:08 +00:00
|
|
|
@NonNull
|
2018-11-24 08:26:34 +00:00
|
|
|
public Boolean notify;
|
|
|
|
@NonNull
|
2019-01-20 15:22:21 +00:00
|
|
|
public Boolean browse = true;
|
|
|
|
public Long swipe_left;
|
|
|
|
public Long swipe_right;
|
2018-12-26 11:34:05 +00:00
|
|
|
@NonNull
|
2018-10-07 06:53:09 +00:00
|
|
|
public Integer poll_interval; // keep-alive interval
|
2018-12-26 11:34:05 +00:00
|
|
|
public String prefix; // namespace
|
|
|
|
|
2018-11-06 16:45:52 +00:00
|
|
|
public Long created;
|
2018-12-06 12:43:00 +00:00
|
|
|
public Boolean tbd;
|
2018-08-13 14:44:47 +00:00
|
|
|
public String state;
|
2018-08-11 08:28:54 +00:00
|
|
|
public String error;
|
2018-11-19 13:14:02 +00:00
|
|
|
public Long last_connected;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-02-09 21:03:53 +00:00
|
|
|
String getProtocol() {
|
2019-02-11 21:46:12 +00:00
|
|
|
return "imap" + (starttls ? "" : "s");
|
2019-02-09 21:03:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 11:26:50 +00:00
|
|
|
static String getNotificationChannelName(long account) {
|
2019-03-07 09:55:47 +00:00
|
|
|
return "notification" + (account == 0 ? "" : "." + account);
|
2018-12-10 11:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
|
|
|
void createNotificationChannel(Context context) {
|
|
|
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
NotificationChannel notification = new NotificationChannel(
|
|
|
|
getNotificationChannelName(id), name,
|
|
|
|
NotificationManager.IMPORTANCE_HIGH);
|
|
|
|
notification.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
|
|
|
|
nm.createNotificationChannel(notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.O)
|
|
|
|
void deleteNotificationChannel(Context context) {
|
|
|
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
nm.deleteNotificationChannel(getNotificationChannelName(id));
|
|
|
|
}
|
|
|
|
|
2018-09-14 11:33:22 +00:00
|
|
|
public JSONObject toJSON() throws JSONException {
|
|
|
|
JSONObject json = new JSONObject();
|
2019-02-07 08:29:39 +00:00
|
|
|
json.put("id", id);
|
2019-02-02 19:49:25 +00:00
|
|
|
json.put("auth_type", auth_type);
|
2018-09-14 11:33:22 +00:00
|
|
|
json.put("host", host);
|
2018-10-24 11:14:56 +00:00
|
|
|
json.put("starttls", starttls);
|
|
|
|
json.put("insecure", insecure);
|
2018-09-14 11:33:22 +00:00
|
|
|
json.put("port", port);
|
|
|
|
json.put("user", user);
|
2018-11-14 19:00:38 +00:00
|
|
|
json.put("password", password);
|
2019-02-17 11:34:29 +00:00
|
|
|
json.put("realm", realm);
|
2019-02-02 19:49:25 +00:00
|
|
|
|
|
|
|
json.put("name", name);
|
|
|
|
json.put("color", color);
|
|
|
|
|
2018-11-14 19:00:38 +00:00
|
|
|
json.put("synchronize", synchronize);
|
2018-10-07 08:08:01 +00:00
|
|
|
json.put("primary", primary);
|
2019-02-02 19:49:25 +00:00
|
|
|
json.put("notify", notify);
|
2018-12-23 18:20:08 +00:00
|
|
|
json.put("browse", browse);
|
2019-02-02 19:49:25 +00:00
|
|
|
|
2019-01-20 15:22:21 +00:00
|
|
|
json.put("swipe_left", swipe_left);
|
|
|
|
json.put("swipe_right", swipe_right);
|
2019-02-02 19:49:25 +00:00
|
|
|
|
2018-09-14 11:33:22 +00:00
|
|
|
json.put("poll_interval", poll_interval);
|
2019-02-02 19:49:25 +00:00
|
|
|
json.put("prefix", prefix);
|
2018-11-10 10:45:03 +00:00
|
|
|
// not created
|
|
|
|
// not state
|
|
|
|
// 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();
|
2019-02-07 08:29:39 +00:00
|
|
|
// id
|
2019-02-02 19:49:25 +00:00
|
|
|
account.auth_type = json.getInt("auth_type");
|
2018-09-14 11:33:22 +00:00
|
|
|
account.host = json.getString("host");
|
2018-10-24 11:14:56 +00:00
|
|
|
account.starttls = (json.has("starttls") && json.getBoolean("starttls"));
|
|
|
|
account.insecure = (json.has("insecure") && json.getBoolean("insecure"));
|
2018-09-14 11:33:22 +00:00
|
|
|
account.port = json.getInt("port");
|
|
|
|
account.user = json.getString("user");
|
|
|
|
account.password = json.getString("password");
|
2019-02-02 19:49:25 +00:00
|
|
|
if (json.has("realm"))
|
|
|
|
account.realm = json.getString("realm");
|
|
|
|
|
|
|
|
if (json.has("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");
|
2018-10-07 08:08:01 +00:00
|
|
|
account.primary = json.getBoolean("primary");
|
2019-02-02 19:49:25 +00:00
|
|
|
if (json.has("notify"))
|
|
|
|
account.notify = json.getBoolean("notify");
|
2018-12-23 18:20:08 +00:00
|
|
|
if (json.has("browse"))
|
|
|
|
account.browse = json.getBoolean("browse");
|
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");
|
|
|
|
|
2018-09-14 11:33:22 +00:00
|
|
|
account.poll_interval = json.getInt("poll_interval");
|
2019-02-02 19:49:25 +00:00
|
|
|
if (json.has("prefix"))
|
|
|
|
account.prefix = json.getString("prefix");
|
|
|
|
|
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-01-23 09:16:33 +00:00
|
|
|
return (this.auth_type.equals(other.auth_type) &&
|
2018-11-10 10:45:03 +00:00
|
|
|
this.host.equals(other.host) &&
|
|
|
|
this.starttls == other.starttls &&
|
|
|
|
this.insecure == other.insecure &&
|
|
|
|
this.port.equals(other.port) &&
|
|
|
|
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) &&
|
2018-11-24 08:26:34 +00:00
|
|
|
this.notify.equals(other.notify) &&
|
2019-01-23 09:16:33 +00:00
|
|
|
this.browse.equals(other.browse) &&
|
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) &&
|
2019-02-26 10:05:21 +00:00
|
|
|
Objects.equals(this.prefix, other.prefix) &&
|
|
|
|
Objects.equals(this.created, other.created) &&
|
|
|
|
Objects.equals(this.tbd, other.tbd) &&
|
|
|
|
Objects.equals(this.state, other.state) &&
|
|
|
|
Objects.equals(this.error, other.error) &&
|
|
|
|
Objects.equals(this.last_connected, other.last_connected));
|
2018-11-10 10:45:03 +00:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-28 07:40:27 +00:00
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return name + (primary ? " ★" : "");
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|