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
|
|
|
|
|
|
|
Copyright 2018 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2018-11-17 08:07:24 +00:00
|
|
|
import android.Manifest;
|
|
|
|
import android.content.ContentResolver;
|
2018-08-19 06:53:56 +00:00
|
|
|
import android.content.Context;
|
2018-11-17 08:07:24 +00:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.provider.ContactsContract;
|
2018-11-16 12:48:45 +00:00
|
|
|
import android.text.Html;
|
2018-11-17 08:07:24 +00:00
|
|
|
import android.text.TextUtils;
|
2018-08-19 06:53:56 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
2018-08-26 13:24:16 +00:00
|
|
|
import java.io.Serializable;
|
2018-08-14 05:32:17 +00:00
|
|
|
import java.util.Date;
|
2018-08-13 11:22:57 +00:00
|
|
|
import java.util.Random;
|
|
|
|
|
2018-08-07 16:25:57 +00:00
|
|
|
import javax.mail.Address;
|
2018-11-17 08:07:24 +00:00
|
|
|
import javax.mail.internet.InternetAddress;
|
2018-08-07 16:25:57 +00:00
|
|
|
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2018-11-17 08:07:24 +00:00
|
|
|
import androidx.core.content.ContextCompat;
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.room.Entity;
|
|
|
|
import androidx.room.ForeignKey;
|
|
|
|
import androidx.room.Index;
|
|
|
|
import androidx.room.PrimaryKey;
|
|
|
|
|
|
|
|
import static androidx.room.ForeignKey.CASCADE;
|
2018-11-12 13:45:02 +00:00
|
|
|
import static androidx.room.ForeignKey.SET_NULL;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
|
|
|
// https://developer.android.com/training/data-storage/room/defining-data
|
|
|
|
|
|
|
|
@Entity(
|
|
|
|
tableName = EntityMessage.TABLE_NAME,
|
|
|
|
foreignKeys = {
|
2018-08-08 06:55:47 +00:00
|
|
|
@ForeignKey(childColumns = "account", entity = EntityAccount.class, parentColumns = "id", onDelete = CASCADE),
|
|
|
|
@ForeignKey(childColumns = "folder", entity = EntityFolder.class, parentColumns = "id", onDelete = CASCADE),
|
2018-11-12 13:45:02 +00:00
|
|
|
@ForeignKey(childColumns = "identity", entity = EntityIdentity.class, parentColumns = "id", onDelete = SET_NULL),
|
2018-11-16 12:48:45 +00:00
|
|
|
@ForeignKey(childColumns = "replying", entity = EntityMessage.class, parentColumns = "id", onDelete = SET_NULL),
|
|
|
|
@ForeignKey(childColumns = "forwarding", entity = EntityMessage.class, parentColumns = "id", onDelete = SET_NULL)
|
2018-08-02 13:33:06 +00:00
|
|
|
},
|
|
|
|
indices = {
|
|
|
|
@Index(value = {"account"}),
|
|
|
|
@Index(value = {"folder"}),
|
|
|
|
@Index(value = {"identity"}),
|
|
|
|
@Index(value = {"replying"}),
|
2018-11-16 12:48:45 +00:00
|
|
|
@Index(value = {"forwarding"}),
|
2018-10-20 17:56:09 +00:00
|
|
|
@Index(value = {"folder", "uid", "ui_found"}, unique = true),
|
|
|
|
@Index(value = {"msgid", "folder", "ui_found"}, unique = true),
|
2018-08-02 13:33:06 +00:00
|
|
|
@Index(value = {"thread"}),
|
2018-08-05 11:44:46 +00:00
|
|
|
@Index(value = {"received"}),
|
|
|
|
@Index(value = {"ui_seen"}),
|
2018-09-04 14:07:50 +00:00
|
|
|
@Index(value = {"ui_hide"}),
|
2018-10-16 11:29:12 +00:00
|
|
|
@Index(value = {"ui_found"}),
|
|
|
|
@Index(value = {"ui_ignored"})
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
)
|
2018-08-26 13:24:16 +00:00
|
|
|
public class EntityMessage implements Serializable {
|
2018-08-02 13:33:06 +00:00
|
|
|
static final String TABLE_NAME = "message";
|
|
|
|
|
|
|
|
@PrimaryKey(autoGenerate = true)
|
|
|
|
public Long id;
|
2018-11-12 13:45:02 +00:00
|
|
|
@NonNull
|
2018-09-13 07:06:06 +00:00
|
|
|
public Long account; // performance
|
2018-08-02 13:33:06 +00:00
|
|
|
@NonNull
|
|
|
|
public Long folder;
|
|
|
|
public Long identity;
|
2018-11-09 07:22:44 +00:00
|
|
|
public String extra; // plus
|
2018-08-02 13:33:06 +00:00
|
|
|
public Long replying;
|
2018-11-16 12:48:45 +00:00
|
|
|
public Long forwarding;
|
2018-08-02 13:33:06 +00:00
|
|
|
public Long uid; // compose = null
|
|
|
|
public String msgid;
|
|
|
|
public String references;
|
2018-09-18 08:55:59 +00:00
|
|
|
public String deliveredto;
|
2018-08-02 13:33:06 +00:00
|
|
|
public String inreplyto;
|
|
|
|
public String thread; // compose = null
|
2018-11-06 12:22:31 +00:00
|
|
|
public String avatar; // Contact lookup URI
|
2018-08-07 16:25:57 +00:00
|
|
|
public Address[] from;
|
|
|
|
public Address[] to;
|
|
|
|
public Address[] cc;
|
|
|
|
public Address[] bcc;
|
|
|
|
public Address[] reply;
|
2018-09-05 07:23:51 +00:00
|
|
|
public String headers;
|
2018-08-02 13:33:06 +00:00
|
|
|
public String subject;
|
2018-09-16 10:44:13 +00:00
|
|
|
public Integer size;
|
2018-09-15 07:22:42 +00:00
|
|
|
@NonNull
|
2018-09-16 10:44:13 +00:00
|
|
|
public Boolean content = false;
|
2018-11-04 15:34:30 +00:00
|
|
|
public String preview;
|
2018-08-02 13:33:06 +00:00
|
|
|
public Long sent; // compose = null
|
|
|
|
@NonNull
|
|
|
|
public Long received; // compose = stored
|
|
|
|
@NonNull
|
2018-08-14 05:32:17 +00:00
|
|
|
public Long stored = new Date().getTime();
|
|
|
|
@NonNull
|
2018-08-02 13:33:06 +00:00
|
|
|
public Boolean seen;
|
|
|
|
@NonNull
|
2018-11-24 18:14:28 +00:00
|
|
|
public Boolean answered;
|
|
|
|
@NonNull
|
2018-09-07 15:12:43 +00:00
|
|
|
public Boolean flagged;
|
2018-11-25 16:52:30 +00:00
|
|
|
public String[] keywords; // user flags
|
2018-09-07 15:12:43 +00:00
|
|
|
@NonNull
|
2018-08-02 13:33:06 +00:00
|
|
|
public Boolean ui_seen;
|
|
|
|
@NonNull
|
2018-11-24 18:14:28 +00:00
|
|
|
public Boolean ui_answered;
|
|
|
|
@NonNull
|
2018-09-07 15:12:43 +00:00
|
|
|
public Boolean ui_flagged;
|
|
|
|
@NonNull
|
2018-08-02 13:33:06 +00:00
|
|
|
public Boolean ui_hide;
|
2018-09-01 16:34:16 +00:00
|
|
|
@NonNull
|
|
|
|
public Boolean ui_found;
|
2018-10-16 11:29:12 +00:00
|
|
|
@NonNull
|
|
|
|
public Boolean ui_ignored;
|
2018-08-11 08:28:54 +00:00
|
|
|
public String error;
|
2018-11-19 13:14:02 +00:00
|
|
|
public Long last_attempt; // send
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-13 11:22:57 +00:00
|
|
|
static String generateMessageId() {
|
2018-08-11 14:13:29 +00:00
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
sb.append('<')
|
2018-08-13 11:22:57 +00:00
|
|
|
.append(Math.abs(new Random().nextInt())).append('.')
|
2018-08-11 14:13:29 +00:00
|
|
|
.append(System.currentTimeMillis()).append('.')
|
2018-08-13 11:22:57 +00:00
|
|
|
.append(BuildConfig.APPLICATION_ID).append("@localhost")
|
2018-08-11 14:13:29 +00:00
|
|
|
.append('>');
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
2018-08-21 14:25:42 +00:00
|
|
|
static File getFile(Context context, Long id) {
|
2018-08-19 06:53:56 +00:00
|
|
|
File dir = new File(context.getFilesDir(), "messages");
|
|
|
|
dir.mkdir();
|
2018-08-21 14:25:42 +00:00
|
|
|
return new File(dir, id.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(Context context, String body) throws IOException {
|
|
|
|
File file = getFile(context, id);
|
2018-08-19 06:53:56 +00:00
|
|
|
BufferedWriter out = null;
|
|
|
|
try {
|
|
|
|
out = new BufferedWriter(new FileWriter(file));
|
2018-10-28 12:49:13 +00:00
|
|
|
out.write(body == null ? "" : body);
|
2018-08-19 06:53:56 +00:00
|
|
|
} finally {
|
|
|
|
if (out != null)
|
|
|
|
try {
|
|
|
|
out.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String read(Context context) throws IOException {
|
2018-10-28 12:49:13 +00:00
|
|
|
return read(context, this.id);
|
2018-08-19 06:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static String read(Context context, Long id) throws IOException {
|
2018-08-21 14:25:42 +00:00
|
|
|
File file = getFile(context, id);
|
2018-08-19 06:53:56 +00:00
|
|
|
BufferedReader in = null;
|
|
|
|
try {
|
|
|
|
in = new BufferedReader(new FileReader(file));
|
|
|
|
StringBuilder body = new StringBuilder();
|
|
|
|
String line;
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
body.append(line);
|
|
|
|
body.append('\n');
|
|
|
|
}
|
|
|
|
return body.toString();
|
|
|
|
} finally {
|
|
|
|
if (in != null) {
|
|
|
|
try {
|
|
|
|
in.close();
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-24 16:22:47 +00:00
|
|
|
void getAvatar(Context context) {
|
2018-11-17 08:07:24 +00:00
|
|
|
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
|
|
|
|
== PackageManager.PERMISSION_GRANTED) {
|
|
|
|
try {
|
2018-11-24 16:22:47 +00:00
|
|
|
if (this.from != null)
|
|
|
|
for (int i = 0; i < this.from.length; i++) {
|
|
|
|
String email = ((InternetAddress) this.from[i]).getAddress();
|
2018-11-17 08:07:24 +00:00
|
|
|
Cursor cursor = null;
|
|
|
|
try {
|
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
cursor = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
|
|
|
|
new String[]{
|
|
|
|
ContactsContract.CommonDataKinds.Photo.CONTACT_ID,
|
|
|
|
ContactsContract.Contacts.LOOKUP_KEY,
|
|
|
|
ContactsContract.Contacts.DISPLAY_NAME
|
|
|
|
},
|
|
|
|
ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
|
|
|
|
new String[]{email}, null);
|
|
|
|
if (cursor.moveToNext()) {
|
|
|
|
int colContactId = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.CONTACT_ID);
|
|
|
|
int colLookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
|
|
|
|
int colDisplayName = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
|
|
|
|
|
|
|
|
long contactId = cursor.getLong(colContactId);
|
|
|
|
String lookupKey = cursor.getString(colLookupKey);
|
|
|
|
String displayName = cursor.getString(colDisplayName);
|
|
|
|
|
|
|
|
this.avatar = ContactsContract.Contacts.getLookupUri(contactId, lookupKey).toString();
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(displayName))
|
2018-11-24 16:22:47 +00:00
|
|
|
((InternetAddress) this.from[i]).setPersonal(displayName);
|
2018-11-17 08:07:24 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:48:45 +00:00
|
|
|
static String getQuote(Context context, long id) throws IOException {
|
|
|
|
EntityMessage message = DB.getInstance(context).message().getMessage(id);
|
|
|
|
return String.format("<p>%s %s:</p><blockquote>%s</blockquote>",
|
2018-11-24 16:22:47 +00:00
|
|
|
Html.escapeHtml(new Date(message.received).toString()),
|
2018-11-16 12:48:45 +00:00
|
|
|
Html.escapeHtml(MessageHelper.getFormattedAddresses(message.from, true)),
|
|
|
|
HtmlHelper.sanitize(EntityMessage.read(context, id)));
|
|
|
|
}
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (obj instanceof EntityMessage) {
|
|
|
|
EntityMessage other = (EntityMessage) obj;
|
2018-08-04 19:54:33 +00:00
|
|
|
return ((this.account == null ? other.account == null : this.account.equals(other.account)) &&
|
2018-08-02 13:33:06 +00:00
|
|
|
this.folder.equals(other.folder) &&
|
2018-08-04 19:54:33 +00:00
|
|
|
(this.identity == null ? other.identity == null : this.identity.equals(other.identity)) &&
|
|
|
|
(this.replying == null ? other.replying == null : this.replying.equals(other.replying)) &&
|
2018-11-18 07:55:51 +00:00
|
|
|
(this.forwarding == null ? other.forwarding == null : this.forwarding.equals(other.forwarding)) &&
|
2018-08-04 19:54:33 +00:00
|
|
|
(this.uid == null ? other.uid == null : this.uid.equals(other.uid)) &&
|
|
|
|
(this.msgid == null ? other.msgid == null : this.msgid.equals(other.msgid)) &&
|
|
|
|
(this.references == null ? other.references == null : this.references.equals(other.references)) &&
|
2018-10-15 10:05:42 +00:00
|
|
|
(this.deliveredto == null ? other.deliveredto == null : this.deliveredto.equals(other.deliveredto)) &&
|
2018-08-04 19:54:33 +00:00
|
|
|
(this.inreplyto == null ? other.inreplyto == null : this.inreplyto.equals(other.inreplyto)) &&
|
2018-08-14 09:22:53 +00:00
|
|
|
(this.thread == null ? other.thread == null : this.thread.equals(other.thread)) &&
|
2018-09-08 17:04:10 +00:00
|
|
|
(this.avatar == null ? other.avatar == null : this.avatar.equals(other.avatar)) &&
|
2018-08-14 09:22:53 +00:00
|
|
|
equal(this.from, other.from) &&
|
|
|
|
equal(this.to, other.to) &&
|
|
|
|
equal(this.cc, other.cc) &&
|
|
|
|
equal(this.bcc, other.bcc) &&
|
|
|
|
equal(this.reply, other.reply) &&
|
2018-09-07 15:12:43 +00:00
|
|
|
(this.headers == null ? other.headers == null : this.headers.equals(other.headers)) &&
|
2018-08-04 19:54:33 +00:00
|
|
|
(this.subject == null ? other.subject == null : this.subject.equals(other.subject)) &&
|
2018-10-15 10:05:42 +00:00
|
|
|
(this.size == null ? other.size == null : this.size.equals(other.size)) &&
|
|
|
|
this.content == other.content &&
|
2018-11-04 15:34:30 +00:00
|
|
|
(this.preview == null ? other.preview == null : this.preview.equals(other.preview)) &&
|
2018-08-04 19:54:33 +00:00
|
|
|
(this.sent == null ? other.sent == null : this.sent.equals(other.sent)) &&
|
2018-08-02 13:33:06 +00:00
|
|
|
this.received.equals(other.received) &&
|
2018-09-07 15:12:43 +00:00
|
|
|
this.stored.equals(other.stored) &&
|
2018-08-02 13:33:06 +00:00
|
|
|
this.seen.equals(other.seen) &&
|
2018-11-24 18:14:28 +00:00
|
|
|
this.answered.equals(other.answered) &&
|
2018-09-07 15:12:43 +00:00
|
|
|
this.flagged.equals(other.flagged) &&
|
2018-10-15 10:05:42 +00:00
|
|
|
this.ui_seen.equals(other.ui_seen) &&
|
2018-11-24 18:14:28 +00:00
|
|
|
this.ui_answered.equals(other.ui_answered) &&
|
2018-09-07 15:12:43 +00:00
|
|
|
this.ui_flagged.equals(other.ui_flagged) &&
|
2018-08-11 08:28:54 +00:00
|
|
|
this.ui_hide.equals(other.ui_hide) &&
|
2018-09-07 15:12:43 +00:00
|
|
|
this.ui_found.equals(other.ui_found) &&
|
2018-10-16 11:29:12 +00:00
|
|
|
this.ui_ignored.equals(other.ui_ignored) &&
|
2018-08-11 08:28:54 +00:00
|
|
|
(this.error == null ? other.error == null : this.error.equals(other.error)));
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-14 09:22:53 +00:00
|
|
|
|
|
|
|
private static boolean equal(Address[] a1, Address[] a2) {
|
|
|
|
if (a1 == null && a2 == null)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (a1 == null || a2 == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (a1.length != a2.length)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int i = 0; i < a1.length; i++)
|
|
|
|
if (!a1[i].toString().equals(a2[i].toString()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|