From d5a55f9e27191bf8ede0ef3255a27549f39ff526 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 7 Aug 2018 16:25:57 +0000 Subject: [PATCH] Simplify address handling by using type converters --- .../java/eu/faircode/email/ApplicationEx.java | 5 +- app/src/main/java/eu/faircode/email/DB.java | 61 ++++++++++ .../java/eu/faircode/email/EntityMessage.java | 12 +- .../java/eu/faircode/email/FragmentAbout.java | 2 +- .../eu/faircode/email/FragmentCompose.java | 94 +++++++-------- .../java/eu/faircode/email/MessageHelper.java | 111 +++++------------- 6 files changed, 143 insertions(+), 142 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 5cb56eff78..8690c47309 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -46,13 +46,14 @@ public class ApplicationEx extends Application { EntityFolder drafts = db.folder().getPrimaryFolder(EntityFolder.TYPE_DRAFTS); if (drafts != null) { Address to = new InternetAddress("marcel+email@faircode.eu" , "FairCode"); + String body = ex + "\n" + Log.getStackTraceString(ex); EntityMessage draft = new EntityMessage(); draft.account = drafts.account; draft.folder = drafts.id; - draft.to = MessageHelper.encodeAddresses(new Address[]{to}); + draft.to = new Address[]{to}; draft.subject = BuildConfig.APPLICATION_ID + " crash info"; - draft.body = "
" + ex.toString().replaceAll("\\r?\\n" , "
") + "
"; + draft.body = "
" + body.replaceAll("\\r?\\n" , "
") + "
"; draft.received = new Date().getTime(); draft.seen = false; draft.ui_seen = false; diff --git a/app/src/main/java/eu/faircode/email/DB.java b/app/src/main/java/eu/faircode/email/DB.java index 1e1fbd6a33..7b9f23bc30 100644 --- a/app/src/main/java/eu/faircode/email/DB.java +++ b/app/src/main/java/eu/faircode/email/DB.java @@ -11,6 +11,16 @@ import android.content.Context; import android.text.TextUtils; import android.util.Log; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +import javax.mail.Address; +import javax.mail.internet.InternetAddress; + /* This file is part of Safe email. @@ -132,6 +142,57 @@ public abstract class DB extends RoomDatabase { public static String toStringArray(String[] value) { return TextUtils.join("," , value); } + + @TypeConverter + public static String encodeAddresses(Address[] addresses) { + if (addresses == null) + return null; + JSONArray jaddresses = new JSONArray(); + if (addresses != null) + for (Address address : addresses) + try { + if (address instanceof InternetAddress) { + String a = ((InternetAddress) address).getAddress(); + String p = ((InternetAddress) address).getPersonal(); + JSONObject jaddress = new JSONObject(); + if (a != null) + jaddress.put("address" , a); + if (p != null) + jaddress.put("personal" , p); + jaddresses.put(jaddress); + } else { + JSONObject jaddress = new JSONObject(); + jaddress.put("address" , address.toString()); + jaddresses.put(jaddress); + } + } catch (JSONException ex) { + Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); + } + return jaddresses.toString(); + } + + @TypeConverter + public static Address[] decodeAddresses(String json) { + if (json == null) + return null; + List
result = new ArrayList<>(); + try { + JSONArray jaddresses = new JSONArray(json); + for (int i = 0; i < jaddresses.length(); i++) { + JSONObject jaddress = (JSONObject) jaddresses.get(i); + if (jaddress.has("personal")) + result.add(new InternetAddress( + jaddress.getString("address"), + jaddress.getString("personal"))); + else + result.add(new InternetAddress( + jaddress.getString("address"))); + } + } catch (Throwable ex) { + Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); + } + return result.toArray(new Address[0]); + } } } diff --git a/app/src/main/java/eu/faircode/email/EntityMessage.java b/app/src/main/java/eu/faircode/email/EntityMessage.java index f3c82199ac..0c628aebae 100644 --- a/app/src/main/java/eu/faircode/email/EntityMessage.java +++ b/app/src/main/java/eu/faircode/email/EntityMessage.java @@ -25,6 +25,8 @@ import android.arch.persistence.room.Index; import android.arch.persistence.room.PrimaryKey; import android.support.annotation.NonNull; +import javax.mail.Address; + import static android.arch.persistence.room.ForeignKey.CASCADE; // https://developer.android.com/training/data-storage/room/defining-data @@ -64,11 +66,11 @@ public class EntityMessage { public String references; public String inreplyto; public String thread; // compose = null - public String from; - public String to; - public String cc; - public String bcc; - public String reply; + public Address[] from; + public Address[] to; + public Address[] cc; + public Address[] bcc; + public Address[] reply; public String subject; public String body; public Long sent; // compose = null diff --git a/app/src/main/java/eu/faircode/email/FragmentAbout.java b/app/src/main/java/eu/faircode/email/FragmentAbout.java index 674757db4b..5ca5a7d1d3 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAbout.java +++ b/app/src/main/java/eu/faircode/email/FragmentAbout.java @@ -72,7 +72,7 @@ public class FragmentAbout extends FragmentEx { EntityMessage draft = new EntityMessage(); draft.account = drafts.account; draft.folder = drafts.id; - draft.to = MessageHelper.encodeAddresses(new Address[]{to}); + draft.to = new Address[]{to}; draft.subject = BuildConfig.APPLICATION_ID + " debug info"; draft.body = "
" + info.toString().replaceAll("\\r?\\n" , "
") + "
"; draft.received = new Date().getTime(); diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 4cf686c467..81fd101b10 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -339,8 +339,8 @@ public class FragmentCompose extends FragmentEx { result.putLong("iid" , msg.identity); if (msg.replying != null) result.putLong("rid" , msg.replying); - result.putString("cc" , msg.cc); - result.putString("bcc" , msg.bcc); + result.putSerializable("cc" , msg.cc); + result.putSerializable("bcc" , msg.bcc); result.putString("thread" , msg.thread); result.putString("subject" , msg.subject); result.putString("body" , msg.body); @@ -348,50 +348,37 @@ public class FragmentCompose extends FragmentEx { if (TextUtils.isEmpty(action)) { if (msg != null) { - result.putString("from" , msg.from); - result.putString("to" , msg.to); + result.putSerializable("from" , msg.from); + result.putSerializable("to" , msg.to); } } else if ("reply".equals(action)) { - String to = null; + Address[] to = null; if (msg != null) - try { - Address[] reply = MessageHelper.decodeAddresses(msg.reply); - to = (reply.length == 0 ? msg.from : msg.reply); - } catch (Throwable ex) { - Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - } + to = (msg.reply == null || msg.reply.length == 0 ? msg.from : msg.reply); result.putLong("rid" , msg.id); - result.putString("from" , msg.to); - result.putString("to" , to); + result.putSerializable("from" , msg.to); + result.putSerializable("to" , to); } else if ("reply_all".equals(action)) { - String to = null; + Address[] to = null; if (msg != null) { - try { - Address[] from = MessageHelper.decodeAddresses(msg.from); - Address[] reply = MessageHelper.decodeAddresses(msg.reply); - Address[] cc = MessageHelper.decodeAddresses(msg.cc); - List
addresses = new ArrayList<>(); - addresses.addAll(Arrays.asList(reply.length == 0 ? from : reply)); - addresses.addAll(Arrays.asList(cc)); - to = MessageHelper.encodeAddresses(addresses.toArray(new Address[0])); - } catch (Throwable ex) { - Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - } + List
addresses = new ArrayList<>(); + if (msg.reply != null) + addresses.addAll(Arrays.asList(msg.reply)); + else if (msg.from != null) + addresses.addAll(Arrays.asList(msg.from)); + if (msg.cc != null) + addresses.addAll(Arrays.asList(msg.cc)); + to = addresses.toArray(new Address[0]); } result.putLong("rid" , msg.id); - result.putString("from" , msg.to); - result.putString("to" , to); + result.putSerializable("from" , msg.to); + result.putSerializable("to" , to); } else if ("forward".equals(action)) { - String to = null; + Address[] to = null; if (msg != null) - try { - Address[] reply = MessageHelper.decodeAddresses(msg.reply); - to = (reply.length == 0 ? msg.from : msg.reply); - } catch (Throwable ex) { - Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - } - result.putString("from" , msg.to); - result.putString("to" , to); + to = (msg.reply == null || msg.reply.length == 0 ? msg.from : msg.reply); + result.putSerializable("from" , msg.to); + result.putSerializable("to" , to); } } catch (Throwable ex) { Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); @@ -416,10 +403,10 @@ public class FragmentCompose extends FragmentEx { long iid = result.getLong("iid" , -1); long rid = result.getLong("rid" , -1); String thread = result.getString("thread"); - String from = result.getString("from"); - String to = result.getString("to"); - String cc = result.getString("cc"); - String bcc = result.getString("bcc"); + Address[] from = (Address[]) result.getSerializable("from"); + Address[] to = (Address[]) result.getSerializable("to"); + Address[] cc = (Address[]) result.getSerializable("cc"); + Address[] bcc = (Address[]) result.getSerializable("bcc"); String subject = result.getString("subject"); String body = result.getString("body"); String action = result.getString("action"); @@ -433,10 +420,11 @@ public class FragmentCompose extends FragmentEx { ArrayAdapter adapter = (ArrayAdapter) spFrom.getAdapter(); if (adapter != null) { - InternetAddress[] afrom = MessageHelper.decodeAddresses(from); for (int pos = 0; pos < adapter.getCount(); pos++) { EntityIdentity identity = (EntityIdentity) adapter.getItem(pos); - if (iid < 0 ? afrom.length > 0 && afrom[0].getAddress().equals(identity.email) : iid == identity.id) { + if (iid < 0 + ? from != null && from.length > 0 && ((InternetAddress) from[0]).getAddress().equals(identity.email) + : iid == identity.id) { spFrom.setSelection(pos); break; } @@ -449,11 +437,11 @@ public class FragmentCompose extends FragmentEx { Handler handler = new Handler(); - etCc.setText(TextUtils.join(", " , MessageHelper.decodeAddresses(cc))); - etBcc.setText(TextUtils.join(", " , MessageHelper.decodeAddresses(bcc))); + etCc.setText(cc == null ? null : TextUtils.join(", " , cc)); + etBcc.setText(bcc == null ? null : TextUtils.join(", " , bcc)); if (action == null) { - etTo.setText(TextUtils.join(", " , MessageHelper.decodeAddresses(to))); + etTo.setText(to == null ? null : TextUtils.join(", " , to)); etSubject.setText(subject); if (body != null) etBody.setText(Html.fromHtml(HtmlHelper.sanitize(getContext(), body, false))); @@ -464,10 +452,10 @@ public class FragmentCompose extends FragmentEx { } }); } else if ("reply".equals(action) || "reply_all".equals(action)) { - etTo.setText(TextUtils.join(", " , MessageHelper.decodeAddresses(to))); + etTo.setText(to == null ? null : TextUtils.join(", " , to)); String text = String.format("

%s %s:

%s" , Html.escapeHtml(new Date().toString()), - Html.escapeHtml(TextUtils.join(", " , MessageHelper.decodeAddresses(to))), + Html.escapeHtml(to == null ? "" : TextUtils.join(", " , to)), HtmlHelper.sanitize(getContext(), body, true)); etSubject.setText(getContext().getString(R.string.title_subject_reply, subject)); etBody.setText(Html.fromHtml(text)); @@ -480,7 +468,7 @@ public class FragmentCompose extends FragmentEx { } else if ("forward".equals(action)) { String text = String.format("

%s %s:

%s" , Html.escapeHtml(new Date().toString()), - Html.escapeHtml(TextUtils.join(", " , MessageHelper.decodeAddresses(to))), + Html.escapeHtml(to == null ? "" : TextUtils.join(", " , to)), HtmlHelper.sanitize(getContext(), body, true)); etSubject.setText(getContext().getString(R.string.title_subject_forward, subject)); etBody.setText(Html.fromHtml(text)); @@ -539,7 +527,7 @@ public class FragmentCompose extends FragmentEx { String body = args.getString("body"); String subject = args.getString("subject"); - Address afrom = (ident == null ? null : new InternetAddress(ident.email, ident.name)); + Address afrom[] = (ident == null ? null : new Address[]{new InternetAddress(ident.email, ident.name)}); Address ato[] = (TextUtils.isEmpty(to) ? null : InternetAddress.parse(to)); Address acc[] = (TextUtils.isEmpty(cc) ? null : InternetAddress.parse(cc)); Address abcc[] = (TextUtils.isEmpty(bcc) ? null : InternetAddress.parse(bcc)); @@ -553,10 +541,10 @@ public class FragmentCompose extends FragmentEx { draft.identity = (ident == null ? null : ident.id); draft.replying = (rid < 0 ? null : rid); draft.thread = thread; - draft.from = MessageHelper.encodeAddresses(new Address[]{afrom}); - draft.to = MessageHelper.encodeAddresses(ato); - draft.cc = MessageHelper.encodeAddresses(acc); - draft.bcc = MessageHelper.encodeAddresses(abcc); + draft.from = afrom; + draft.to = ato; + draft.cc = acc; + draft.bcc = abcc; draft.subject = subject; draft.body = "
" + body.replaceAll("\\r?\\n" , "
") + "
"; draft.received = new Date().getTime(); diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index e6c7ef23e7..e3dec37259 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -23,10 +23,6 @@ import android.text.TextUtils; import android.util.Base64; import android.util.Log; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -85,20 +81,17 @@ public class MessageHelper { static MimeMessageEx from(EntityMessage message, Session isession) throws MessagingException { MimeMessageEx imessage = new MimeMessageEx(isession, message.id); - if (message.from != null) { - Address[] from = MessageHelper.decodeAddresses(message.from); - if (from.length > 0) - imessage.setFrom(from[0]); - } + if (message.from != null && message.from.length > 0) + imessage.setFrom(message.from[0]); - if (message.to != null) - imessage.setRecipients(Message.RecipientType.TO, MessageHelper.decodeAddresses(message.to)); + if (message.to != null && message.to.length > 0) + imessage.setRecipients(Message.RecipientType.TO, message.to); - if (message.cc != null) - imessage.setRecipients(Message.RecipientType.CC, MessageHelper.decodeAddresses(message.cc)); + if (message.cc != null && message.cc.length > 0) + imessage.setRecipients(Message.RecipientType.CC, message.cc); - if (message.bcc != null) - imessage.setRecipients(Message.RecipientType.BCC, MessageHelper.decodeAddresses(message.bcc)); + if (message.bcc != null && message.bcc.length > 0) + imessage.setRecipients(Message.RecipientType.BCC, message.bcc); if (message.subject != null) imessage.setSubject(message.subject); @@ -153,86 +146,42 @@ public class MessageHelper { return (TextUtils.isEmpty(msgid) ? Long.toString(uid) : msgid); } - String getFrom() throws MessagingException, JSONException { - return encodeAddresses(imessage.getFrom()); + Address[] getFrom() throws MessagingException { + return imessage.getFrom(); } - String getTo() throws MessagingException, JSONException { - return encodeAddresses(imessage.getRecipients(Message.RecipientType.TO)); + Address[] getTo() throws MessagingException { + return imessage.getRecipients(Message.RecipientType.TO); } - String getCc() throws MessagingException, JSONException { - return encodeAddresses(imessage.getRecipients(Message.RecipientType.CC)); + Address[] getCc() throws MessagingException { + return imessage.getRecipients(Message.RecipientType.CC); } - String getBcc() throws MessagingException, JSONException { - return encodeAddresses(imessage.getRecipients(Message.RecipientType.BCC)); + Address[] getBcc() throws MessagingException { + return imessage.getRecipients(Message.RecipientType.BCC); } - String getReply() throws MessagingException, JSONException { - return encodeAddresses(imessage.getReplyTo()); + Address[] getReply() throws MessagingException { + return imessage.getReplyTo(); } - static String encodeAddresses(Address[] addresses) throws JSONException { + static String getFormattedAddresses(Address[] addresses) { if (addresses == null) return null; - JSONArray jaddresses = new JSONArray(); - if (addresses != null) - for (Address address : addresses) - if (address instanceof InternetAddress) { - String a = ((InternetAddress) address).getAddress(); - String p = ((InternetAddress) address).getPersonal(); - JSONObject jaddress = new JSONObject(); - if (a != null) - jaddress.put("address" , a); - if (p != null) - jaddress.put("personal" , p); - jaddresses.put(jaddress); - } - return jaddresses.toString(); - } - static InternetAddress[] decodeAddresses(String json) { - if (json == null) - return new InternetAddress[0]; - List
result = new ArrayList<>(); - try { - JSONArray jaddresses = new JSONArray(json); - for (int i = 0; i < jaddresses.length(); i++) { - JSONObject jaddress = (JSONObject) jaddresses.get(i); - if (jaddress.has("personal")) - result.add(new InternetAddress( - jaddress.getString("address"), - jaddress.getString("personal"))); + List formatted = new ArrayList<>(); + for (Address address : addresses) + if (address instanceof InternetAddress) { + InternetAddress a = (InternetAddress) address; + String personal = a.getPersonal(); + if (TextUtils.isEmpty(personal)) + formatted.add(address.toString()); else - result.add(new InternetAddress( - jaddress.getString("address"))); - } - } catch (Throwable ex) { - Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - } - return result.toArray(new InternetAddress[0]); - } - - static String getFormattedAddresses(String json) { - if (json == null) - return null; - try { - List addresses = new ArrayList<>(); - for (Address address : decodeAddresses(json)) - if (address instanceof InternetAddress) { - InternetAddress a = (InternetAddress) address; - String personal = a.getPersonal(); - if (TextUtils.isEmpty(personal)) - addresses.add(address.toString()); - else - addresses.add(personal); - } else - addresses.add(address.toString()); - return TextUtils.join(", " , addresses); - } catch (Throwable ex) { - return ex.getMessage(); - } + formatted.add(personal); + } else + formatted.add(address.toString()); + return TextUtils.join(", " , formatted); } String getHtml() throws MessagingException {