From 8c50b7f6f11a564b2878c6439cbacc3ddbdcec15 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 5 May 2020 22:39:58 +0200 Subject: [PATCH] Optionally show recipients in message headers --- .../eu/faircode/email/AdapterMessage.java | 161 ++++++++++++------ .../java/eu/faircode/email/ContactInfo.java | 4 + .../eu/faircode/email/FragmentOptions.java | 2 +- .../email/FragmentOptionsDisplay.java | 12 +- .../res/layout/fragment_options_display.xml | 17 +- app/src/main/res/values/strings.xml | 2 + 6 files changed, 136 insertions(+), 62 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index f16b4060b5..c117d874f9 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -226,6 +226,7 @@ public class AdapterMessage extends RecyclerView.Adapter 0 ? View.VISIBLE : View.GONE); ivEncrypted.setVisibility(message.encrypted > 0 ? View.VISIBLE : View.GONE); - setFrom(message, addresses); + if (show_recipients && recipients != null && recipients.length > 0) + tvFrom.setText(context.getString(R.string.title_from_to, + MessageHelper.formatAddresses(senders, name_email, false), + MessageHelper.formatAddresses(recipients, name_email, false))); + else + tvFrom.setText(MessageHelper.formatAddresses(senders, name_email, false)); tvFrom.setPaintFlags(tvFrom.getPaintFlags() & ~Paint.UNDERLINE_TEXT_FLAG); tvSize.setText(message.totalSize == null ? null : Helper.humanReadableByteCount(message.totalSize, true)); tvSize.setVisibility( @@ -1062,7 +1069,7 @@ public class AdapterMessage extends RecyclerView.Adapter() { @Override protected ContactInfo[] onExecute(Context context, Bundle args) { long account = args.getLong("account"); - Address[] addresses = (Address[]) args.getSerializable("addresses"); + Address[] senders = (Address[]) args.getSerializable("senders"); + Address[] recipients = (Address[]) args.getSerializable("recipients"); + + Map map = new HashMap<>(); + if (senders != null) + for (Address a : senders) { + String email = ((InternetAddress) a).getAddress(); + if (!TextUtils.isEmpty(email)) + map.put(email, a); + } + if (recipients != null) + for (Address a : recipients) { + String email = ((InternetAddress) a).getAddress(); + if (!TextUtils.isEmpty(email)) + map.put(email, a); + } - return ContactInfo.get(context, account, addresses); + return ContactInfo.get(context, account, map.values().toArray(new Address[0])); } @Override @@ -1090,7 +1113,7 @@ public class AdapterMessage extends RecyclerView.Adapter map = new HashMap<>(); + for (ContactInfo c : info) + map.put(c.getEmailAddress(), c); - Uri lookupUri = info[0].getLookupUri(); - ibAvatar.setTag(lookupUri); - ibAvatar.setEnabled(lookupUri != null); + ContactInfo main = null; + if (senders != null && senders.length > 0) { + String email = ((InternetAddress) senders[0]).getAddress(); + if (!TextUtils.isEmpty(email)) + main = map.get(email); + } - if (addresses == null) - return; + if (main == null) { + ibAvatar.setImageDrawable(null); + ibAvatar.setTag(null); + } else { + ibAvatar.setImageBitmap(main.getPhotoBitmap()); - boolean known = false; - boolean updated = false; - Address[] modified = Arrays.copyOf(addresses, addresses.length); - for (int i = 0; i < info.length; i++) { - if (info[i].isKnown()) - known = true; - String displayName = info[i].getDisplayName(); - if (!TextUtils.isEmpty(displayName)) { - String email = ((InternetAddress) modified[i]).getAddress(); - String personal = ((InternetAddress) modified[i]).getPersonal(); - if (TextUtils.isEmpty(personal) || - (prefer_contact && !personal.equals(displayName))) - try { - modified[i] = new InternetAddress(email, displayName, StandardCharsets.UTF_8.name()); - updated = true; - } catch (UnsupportedEncodingException ex) { - Log.w(ex); + Uri lookupUri = main.getLookupUri(); + ibAvatar.setTag(lookupUri); + ibAvatar.setEnabled(lookupUri != null); + } + ibAvatar.setVisibility(main == null ? View.GONE : View.VISIBLE); + + Address[] _senders = fillIn(senders, map); + Address[] _recipients = fillIn(recipients, map); + if (_senders != null || _recipients != null) + if (show_recipients && _recipients != null && _recipients.length > 0) + tvFrom.setText(context.getString(R.string.title_from_to, + MessageHelper.formatAddresses(_senders, name_email, false), + MessageHelper.formatAddresses(_recipients, name_email, false))); + else + tvFrom.setText(MessageHelper.formatAddresses(senders, name_email, false)); + + if (distinguish_contacts) { + boolean known = false; + if (senders != null) + for (Address sender : senders) { + String email = ((InternetAddress) sender).getAddress(); + if (!TextUtils.isEmpty(email) && + map.containsKey(email) && map.get(email).isKnown()) { + known = true; + break; } - } + } + if (known) + tvFrom.setPaintFlags(tvFrom.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); } - if (updated) - setFrom(message, modified); - - if (distinguish_contacts && known) - tvFrom.setPaintFlags(tvFrom.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); } - private void setFrom(TupleMessageEx message, Address[] addresses) { - int recipients = 0; - if (viewType == ViewType.THREAD) { - recipients = (message.to == null ? 0 : message.to.length) + - (message.cc == null ? 0 : message.cc.length) + (message.bcc == null ? 0 : message.bcc.length); - if (message.to != null && message.to.length > 0) - recipients--; + private Address[] fillIn(Address[] addresses, Map map) { + if (addresses == null) + return null; + + boolean updated = false; + List
modified = new ArrayList<>(); + for (Address a : addresses) { + String email = ((InternetAddress) a).getAddress(); + if (TextUtils.isEmpty(email) || !map.containsKey(email)) + modified.add(a); + else { + String displayName = map.get(email).getDisplayName(); + if (TextUtils.isEmpty(displayName)) + modified.add(a); + else { + String personal = ((InternetAddress) a).getPersonal(); + if (TextUtils.isEmpty(personal) || + (prefer_contact && !personal.equals(displayName))) + try { + modified.add(new InternetAddress(email, displayName, StandardCharsets.UTF_8.name())); + updated = true; + } catch (UnsupportedEncodingException ex) { + Log.w(ex); + modified.add(a); + } + else + modified.add(a); + } + } } - if (recipients == 0) - tvFrom.setText(MessageHelper.formatAddresses(addresses, name_email, false)); - else - tvFrom.setText(context.getString(R.string.title_name_plus, - MessageHelper.formatAddresses(addresses, name_email, false), recipients)); + return (updated ? modified.toArray(new Address[0]) : null); } private void bindExpandWarning(TupleMessageEx message, boolean expanded) { @@ -4638,6 +4686,7 @@ public class AdapterMessage extends RecyclerView.Adapter + + %1$s (%2$s) %1$s +%2$d + %1$s > %2$s %1$d minutes Exit @@ -329,6 +330,7 @@ Show names and email addresses Prefer a contact name over a sent name Underline the sender when the sender is known as local \'to\' contact + Show recipients in message header Font size sender Font size subject Show subject above sender