From c949cc6e01f09089d905b20f3c3fea4cc8bdc7c1 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 24 Mar 2020 09:46:29 +0100 Subject: [PATCH] Added reply with selected text --- .../eu/faircode/email/AdapterMessage.java | 22 ++++- .../eu/faircode/email/FragmentCompose.java | 92 +++++++++++-------- .../eu/faircode/email/FragmentMessages.java | 20 ++-- 3 files changed, 90 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 150b2b2f55..ac373d2dd3 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -3297,7 +3297,7 @@ public class AdapterMessage extends RecyclerView.Adapter end) { + int tmp = start; + start = end; + end = tmp; + } + + return tvBody.getText().subSequence(start, end).toString(); + } + private View.AccessibilityDelegate accessibilityDelegateHeader = new View.AccessibilityDelegate() { @Override public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 665c0fabba..8daf9286da 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -971,6 +971,7 @@ public class FragmentCompose extends FragmentBase { args.putString("subject", a.getString("subject")); args.putString("body", a.getString("body")); args.putString("text", a.getString("text")); + args.putString("selected", a.getString("selected")); args.putParcelableArrayList("attachments", a.getParcelableArrayList("attachments")); draftLoader.execute(this, args, "compose:new"); } else { @@ -3101,13 +3102,14 @@ public class FragmentCompose extends FragmentBase { } } + String s = args.getString("selected"); if (ref.content && !"editasnew".equals(action) && - !"list".equals(action) && + !("list".equals(action) && TextUtils.isEmpty(s)) && !"receipt".equals(action)) { // Reply/forward - Element div = document.createElement("div"); - div.attr("fairemail", "reference"); + Element reply = document.createElement("div"); + reply.attr("fairemail", "reference"); // Build reply header Element p = document.createElement("p"); @@ -3152,53 +3154,69 @@ public class FragmentCompose extends FragmentBase { } else p.text(DF.format(new Date(ref.received)) + " " + MessageHelper.formatAddresses(ref.from) + ":"); - div.appendChild(p); - - // Get referenced message body - Document d = JsoupEx.parse(ref.getFile(context)); - - // Remove signature separators - boolean remove_signatures = prefs.getBoolean("remove_signatures", false); - if (remove_signatures) - d.body().filter(new NodeFilter() { - private boolean remove = false; - - @Override - public FilterResult head(Node node, int depth) { - if (node instanceof TextNode) { - TextNode tnode = (TextNode) node; - String text = tnode.getWholeText() - .replaceAll("[\r\n]+$", "") - .replaceAll("^[\r\n]+", ""); - if ("-- ".equals(text)) { - if (tnode.getWholeText().endsWith("\n")) - remove = true; - else { - Node next = node.nextSibling(); - if (next != null && "br".equals(next.nodeName())) + reply.appendChild(p); + + Document d; + if (TextUtils.isEmpty(s)) { + // Get referenced message body + d = JsoupEx.parse(ref.getFile(context)); + + // Remove signature separators + boolean remove_signatures = prefs.getBoolean("remove_signatures", false); + if (remove_signatures) + d.body().filter(new NodeFilter() { + private boolean remove = false; + + @Override + public FilterResult head(Node node, int depth) { + if (node instanceof TextNode) { + TextNode tnode = (TextNode) node; + String text = tnode.getWholeText() + .replaceAll("[\r\n]+$", "") + .replaceAll("^[\r\n]+", ""); + if ("-- ".equals(text)) { + if (tnode.getWholeText().endsWith("\n")) remove = true; + else { + Node next = node.nextSibling(); + if (next != null && "br".equals(next.nodeName())) + remove = true; + } } } + + return (remove ? FilterResult.REMOVE : FilterResult.CONTINUE); } - return (remove ? FilterResult.REMOVE : FilterResult.CONTINUE); - } + @Override + public FilterResult tail(Node node, int depth) { + return FilterResult.CONTINUE; + } + }); + } else { + // Selected text + d = Document.createShell(""); - @Override - public FilterResult tail(Node node, int depth) { - return FilterResult.CONTINUE; - } - }); + Element div = d.createElement("div"); + for (String line : s.split("\\r?\\n")) { + Element span = document.createElement("span"); + span.text(line); + div.appendChild(span); + div.appendElement("br"); + } + d.body().appendChild(div); + } // Quote referenced message body Element e = d.body(); boolean quote_reply = prefs.getBoolean("quote_reply", true); - boolean quote = (quote_reply && ("reply".equals(action) || "reply_all".equals(action))); + boolean quote = (quote_reply && + ("reply".equals(action) || "reply_all".equals(action) || "list".equals(action))); e.tagName(quote ? "blockquote" : "p"); - div.appendChild(e); + reply.appendChild(e); - document.body().appendChild(div); + document.body().appendChild(reply); addSignature(context, document, data.draft, selected); } diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 0183be0220..d6e3b7a134 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -1996,13 +1996,16 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. long id = values.get("expanded").get(0); int pos = adapter.getPositionForKey(id); TupleMessageEx message = adapter.getItemAtPosition(pos); + AdapterMessage.ViewHolder holder = + (AdapterMessage.ViewHolder) rvMessage.findViewHolderForAdapterPosition(pos); + String selected = (holder == null ? null : holder.getSelectedText()); if (message == null) return; - onReply(message, fabReply); + onReply(message, selected, fabReply); } } - void onReply(final TupleMessageEx message, final View anchor) { + void onReply(final TupleMessageEx message, final String selected, final View anchor) { Bundle args = new Bundle(); args.putLong("id", message.id); @@ -2054,13 +2057,13 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. public boolean onMenuItemClick(MenuItem target) { switch (target.getItemId()) { case R.id.menu_reply_to_sender: - onMenuReply(message, "reply"); + onMenuReply(message, "reply", selected); return true; case R.id.menu_reply_to_all: - onMenuReply(message, "reply_all"); + onMenuReply(message, "reply_all", selected); return true; case R.id.menu_reply_list: - onMenuReply(message, "list"); + onMenuReply(message, "list", selected); return true; case R.id.menu_reply_receipt: onMenuReply(message, "receipt"); @@ -2093,9 +2096,14 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. } private void onMenuReply(TupleMessageEx message, String action) { + onMenuReply(message, action, null); + } + + private void onMenuReply(TupleMessageEx message, String action, String selected) { Intent reply = new Intent(getContext(), ActivityCompose.class) .putExtra("action", action) - .putExtra("reference", message.id); + .putExtra("reference", message.id) + .putExtra("selected", selected); startActivity(reply); }