Merge branch 'feature-remove-merge-separator' of https://github.com/Mishiranu/Conversations into Mishiranu-feature-remove-merge-separator

This commit is contained in:
Daniel Gultsch 2016-10-20 18:18:25 +02:00
commit f2696b66ba
3 changed files with 41 additions and 40 deletions

View file

@ -2,6 +2,7 @@ package eu.siacs.conversations.entities;
import android.content.ContentValues; import android.content.ContentValues;
import android.database.Cursor; import android.database.Cursor;
import android.text.SpannableStringBuilder;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
@ -19,8 +20,6 @@ public class Message extends AbstractEntity {
public static final String TABLENAME = "messages"; public static final String TABLENAME = "messages";
public static final String MERGE_SEPARATOR = "\n\u200B\n";
public static final int STATUS_RECEIVED = 0; public static final int STATUS_RECEIVED = 0;
public static final int STATUS_UNSEND = 1; public static final int STATUS_UNSEND = 1;
public static final int STATUS_SEND = 2; public static final int STATUS_SEND = 2;
@ -491,22 +490,26 @@ public class Message extends AbstractEntity {
); );
} }
public String getMergedBody() { public static class MergeSeparator {}
StringBuilder body = new StringBuilder(this.body.trim());
public SpannableStringBuilder getMergedBody() {
SpannableStringBuilder body = new SpannableStringBuilder(this.body.trim());
Message current = this; Message current = this;
while (current.mergeable(current.next())) { while (current.mergeable(current.next())) {
current = current.next(); current = current.next();
if (current == null) { if (current == null) {
break; break;
} }
body.append(MERGE_SEPARATOR); body.append("\n\n");
body.setSpan(new MergeSeparator(), body.length() - 2, body.length(),
SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
body.append(current.getBody().trim()); body.append(current.getBody().trim());
} }
return body.toString(); return body;
} }
public boolean hasMeCommand() { public boolean hasMeCommand() {
return getMergedBody().startsWith(ME_COMMAND); return this.body.trim().startsWith(ME_COMMAND);
} }
public int getMergedStatus() { public int getMergedStatus() {

View file

@ -656,7 +656,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
} }
private void copyText(Message message) { private void copyText(Message message) {
if (activity.copyTextToClipboard(message.getMergedBody(), if (activity.copyTextToClipboard(message.getMergedBody().toString(),
R.string.message_text)) { R.string.message_text)) {
Toast.makeText(activity, R.string.message_copied_to_clipboard, Toast.makeText(activity, R.string.message_copied_to_clipboard,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();

View file

@ -304,32 +304,30 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
viewHolder.messageBody.setIncludeFontPadding(true); viewHolder.messageBody.setIncludeFontPadding(true);
if (message.getBody() != null) { if (message.getBody() != null) {
final String nick = UIHelper.getMessageDisplayName(message); final String nick = UIHelper.getMessageDisplayName(message);
String body; SpannableStringBuilder body = message.getMergedBody();
try { boolean hasMeCommand = message.hasMeCommand();
body = message.getMergedBody().replaceAll("^" + Message.ME_COMMAND, nick + " "); if (hasMeCommand) {
} catch (ArrayIndexOutOfBoundsException e) { body = body.replace(0, Message.ME_COMMAND.length(), nick + " ");
body = message.getMergedBody();
} }
if (body.length() > Config.MAX_DISPLAY_MESSAGE_CHARS) { if (body.length() > Config.MAX_DISPLAY_MESSAGE_CHARS) {
body = body.substring(0, Config.MAX_DISPLAY_MESSAGE_CHARS)+"\u2026"; body = new SpannableStringBuilder(body, 0, Config.MAX_DISPLAY_MESSAGE_CHARS);
body.append("\u2026");
} }
Spannable formattedBody = new SpannableString(body); Message.MergeSeparator[] mergeSeparators = body.getSpans(0, body.length(), Message.MergeSeparator.class);
int i = body.indexOf(Message.MERGE_SEPARATOR); for (Message.MergeSeparator mergeSeparator : mergeSeparators) {
while(i >= 0) { int start = body.getSpanStart(mergeSeparator);
final int end = i + Message.MERGE_SEPARATOR.length(); int end = body.getSpanEnd(mergeSeparator);
formattedBody.setSpan(new RelativeSizeSpan(0.3f),i,end,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); body.setSpan(new RelativeSizeSpan(0.3f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
i = body.indexOf(Message.MERGE_SEPARATOR,end);
} }
if (message.getType() != Message.TYPE_PRIVATE) { if (message.getType() != Message.TYPE_PRIVATE) {
if (message.hasMeCommand()) { if (hasMeCommand) {
formattedBody.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, nick.length(), body.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 0, nick.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
} else { } else {
String privateMarker; String privateMarker;
if (message.getStatus() <= Message.STATUS_RECEIVED) { if (message.getStatus() <= Message.STATUS_RECEIVED) {
privateMarker = activity privateMarker = activity.getString(R.string.private_message);
.getString(R.string.private_message);
} else { } else {
final String to; final String to;
if (message.getCounterpart() != null) { if (message.getCounterpart() != null) {
@ -339,23 +337,23 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
} }
privateMarker = activity.getString(R.string.private_message_to, to); privateMarker = activity.getString(R.string.private_message_to, to);
} }
formattedBody = new SpannableStringBuilder().append(privateMarker).append(' ').append(formattedBody); body.insert(0, privateMarker);
formattedBody.setSpan(new ForegroundColorSpan(getMessageTextColor(darkBackground,false)), 0, privateMarker int privateMarkerIndex = privateMarker.length();
.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); body.insert(privateMarkerIndex, " ");
formattedBody.setSpan(new StyleSpan(Typeface.BOLD), 0, body.setSpan(new ForegroundColorSpan(getMessageTextColor(darkBackground, false)),
privateMarker.length(), 0, privateMarkerIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); body.setSpan(new StyleSpan(Typeface.BOLD),
if (message.hasMeCommand()) { 0, privateMarkerIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
formattedBody.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), privateMarker.length() + 1, if (hasMeCommand) {
privateMarker.length() + 1 + nick.length(), body.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), privateMarkerIndex + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); privateMarkerIndex + 1 + nick.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
} }
Linkify.addLinks(formattedBody, Linkify.WEB_URLS); Linkify.addLinks(body, Linkify.WEB_URLS);
Linkify.addLinks(formattedBody, XMPP_PATTERN, "xmpp"); Linkify.addLinks(body, XMPP_PATTERN, "xmpp");
Linkify.addLinks(formattedBody, GeoHelper.GEO_URI, "geo"); Linkify.addLinks(body, GeoHelper.GEO_URI, "geo");
viewHolder.messageBody.setAutoLinkMask(0); viewHolder.messageBody.setAutoLinkMask(0);
viewHolder.messageBody.setText(formattedBody); viewHolder.messageBody.setText(body);
viewHolder.messageBody.setTextIsSelectable(true); viewHolder.messageBody.setTextIsSelectable(true);
viewHolder.messageBody.setMovementMethod(ClickableMovementMethod.getInstance()); viewHolder.messageBody.setMovementMethod(ClickableMovementMethod.getInstance());
listSelectionManager.onUpdate(viewHolder.messageBody, message); listSelectionManager.onUpdate(viewHolder.messageBody, message);