From 9a39f581a8b42d392bd98909b70f3923226d228c Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Mon, 14 May 2018 21:54:06 +0200 Subject: apply styling helper to conversation overview --- .../messenger/services/NotificationService.java | 4 +- .../messenger/ui/adapter/ConversationAdapter.java | 4 +- .../messenger/ui/adapter/MessageAdapter.java | 2 +- .../pixart/messenger/utils/CharSequenceUtils.java | 86 ++++++++++++++++++++++ .../de/pixart/messenger/utils/StylingHelper.java | 41 ++++++++++- .../java/de/pixart/messenger/utils/UIHelper.java | 27 +++++-- 6 files changed, 151 insertions(+), 13 deletions(-) create mode 100644 src/main/java/de/pixart/messenger/utils/CharSequenceUtils.java (limited to 'src/main/java/de/pixart/messenger') diff --git a/src/main/java/de/pixart/messenger/services/NotificationService.java b/src/main/java/de/pixart/messenger/services/NotificationService.java index fe167b904..62461c0c0 100644 --- a/src/main/java/de/pixart/messenger/services/NotificationService.java +++ b/src/main/java/de/pixart/messenger/services/NotificationService.java @@ -552,10 +552,10 @@ public class NotificationService { } /** message preview for Android Auto **/ for (Message message : messages) { - Pair preview = UIHelper.getMessagePreview(mXmppConnectionService, message); + Pair preview = UIHelper.getMessagePreview(mXmppConnectionService, message); // only show user written text if (!preview.second) { - uBuilder.addMessage(preview.first); + uBuilder.addMessage(preview.first.toString()); uBuilder.setLatestTimestamp(message.getTimeSent()); } } diff --git a/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java index c35e13328..abb84a51a 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/ConversationAdapter.java @@ -172,9 +172,9 @@ public class ConversationAdapter extends RecyclerView.Adapter preview = UIHelper.getMessagePreview(activity, message); + final Pair preview = UIHelper.getMessagePreview(activity, message, viewHolder.lastMessage.getCurrentTextColor()); if (showPreviewText) { - viewHolder.lastMessage.setText(EmojiWrapper.transform(preview.first)); + viewHolder.lastMessage.setText(EmojiWrapper.transform(UIHelper.shorten(preview.first))); } else { viewHolder.lastMessageIcon.setContentDescription(preview.first); } diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java index 9666ee1d0..24fd2213d 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -394,7 +394,7 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie } } - private void displayInfoMessage(ViewHolder viewHolder, String text, boolean darkBackground) { + private void displayInfoMessage(ViewHolder viewHolder, CharSequence text, boolean darkBackground) { viewHolder.download_button.setVisibility(View.GONE); viewHolder.audioPlayer.setVisibility(View.GONE); viewHolder.image.setVisibility(View.GONE); diff --git a/src/main/java/de/pixart/messenger/utils/CharSequenceUtils.java b/src/main/java/de/pixart/messenger/utils/CharSequenceUtils.java new file mode 100644 index 000000000..7a9d2f1fb --- /dev/null +++ b/src/main/java/de/pixart/messenger/utils/CharSequenceUtils.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2018, Daniel Gultsch All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package de.pixart.messenger.utils; + +import android.text.Spannable; + +import java.util.ArrayList; +import java.util.List; + +public class CharSequenceUtils { + + private static int getStartIndex(CharSequence input) { + int length = input.length(); + int index = 0; + while (Character.isWhitespace(input.charAt(index))) { + ++index; + if (index >= length) { + break; + } + } + return index; + } + + private static int getEndIndex(CharSequence input) { + int index = input.length() - 1; + while (Character.isWhitespace(input.charAt(index))) { + --index; + if (index < 0) { + break; + } + } + return index; + } + + public static CharSequence trim(CharSequence input) { + int begin = getStartIndex(input); + int end = getEndIndex(input); + if (begin > end) { + return ""; + } else { + return StylingHelper.subSequence(input, begin, end + 1); + } + } + + public static List split(Spannable charSequence, char c) { + List out = new ArrayList<>(); + int begin = 0; + for (int i = 0; i < charSequence.length(); ++i) { + if (charSequence.charAt(i) == c) { + out.add(StylingHelper.subSequence(charSequence, begin, i)); + begin = ++i; + } + } + if (begin < charSequence.length()) { + out.add(StylingHelper.subSequence(charSequence, begin, charSequence.length())); + } + return out; + } +} \ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/utils/StylingHelper.java b/src/main/java/de/pixart/messenger/utils/StylingHelper.java index 9db7b2b47..4412d935b 100644 --- a/src/main/java/de/pixart/messenger/utils/StylingHelper.java +++ b/src/main/java/de/pixart/messenger/utils/StylingHelper.java @@ -36,6 +36,7 @@ import android.support.annotation.ColorInt; import android.support.v4.content.ContextCompat; import android.text.Editable; import android.text.ParcelableSpan; +import android.text.Spannable; import android.text.SpannableString; import android.text.Spanned; import android.text.TextWatcher; @@ -132,6 +133,44 @@ public class StylingHelper { } + static CharSequence subSequence(CharSequence charSequence, int start, int end) { + if (start == 0 && charSequence.length() + 1 == end) { + return charSequence; + } + if (charSequence instanceof Spannable) { + Spannable spannable = (Spannable) charSequence; + Spannable sub = (Spannable) spannable.subSequence(start, end); + for (Class clazz : SPAN_CLASSES) { + ParcelableSpan[] spannables = spannable.getSpans(start, end, clazz); + for (ParcelableSpan parcelableSpan : spannables) { + int beginSpan = spannable.getSpanStart(parcelableSpan); + int endSpan = spannable.getSpanEnd(parcelableSpan); + if (beginSpan >= start && endSpan <= end) { + continue; + } + sub.setSpan(clone(parcelableSpan), Math.max(beginSpan - start, 0), Math.min(sub.length() - 1, endSpan), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } + } + return sub; + } else { + return charSequence.subSequence(start, end); + } + } + + private static ParcelableSpan clone(ParcelableSpan span) { + if (span instanceof ForegroundColorSpan) { + return new ForegroundColorSpan(((ForegroundColorSpan) span).getForegroundColor()); + } else if (span instanceof TypefaceSpan) { + return new TypefaceSpan(((TypefaceSpan) span).getFamily()); + } else if (span instanceof StyleSpan) { + return new StyleSpan(((StyleSpan) span).getStyle()); + } else if (span instanceof StrikethroughSpan) { + return new StrikethroughSpan(); + } else { + throw new AssertionError("Unknown Span"); + } + } + public static boolean isDarkText(TextView textView) { int argb = textView.getCurrentTextColor(); return Color.red(argb) + Color.green(argb) + Color.blue(argb) == 0; @@ -163,7 +202,7 @@ public class StylingHelper { private static @ColorInt int transformColor(@ColorInt int c) { - return Color.argb(Math.round(Color.alpha(c) * 0.6f), Color.red(c), Color.green(c), Color.blue(c)); + return Color.argb(Math.round(Color.alpha(c) * 0.45f), Color.red(c), Color.green(c), Color.blue(c)); } private static int indexOfIgnoreCase(final String haystack, final String needle, final int start) { diff --git a/src/main/java/de/pixart/messenger/utils/UIHelper.java b/src/main/java/de/pixart/messenger/utils/UIHelper.java index e167e8c84..e03f84189 100644 --- a/src/main/java/de/pixart/messenger/utils/UIHelper.java +++ b/src/main/java/de/pixart/messenger/utils/UIHelper.java @@ -1,6 +1,8 @@ package de.pixart.messenger.utils; import android.content.Context; +import android.support.annotation.ColorInt; +import android.text.SpannableStringBuilder; import android.text.format.DateFormat; import android.text.format.DateUtils; import android.util.Pair; @@ -239,7 +241,11 @@ public class UIHelper { } } - public static Pair getMessagePreview(final Context context, final Message message) { + public static Pair getMessagePreview(final Context context, final Message message) { + return getMessagePreview(context, message, 0); + } + + public static Pair getMessagePreview(final Context context, final Message message, @ColorInt int textColor) { final Transferable d = message.getTransferable(); if (d != null) { switch (d.getStatus()) { @@ -289,14 +295,17 @@ public class UIHelper { return new Pair<>(context.getString(R.string.x_file_offered_for_download, getFileDescriptionString(context, message)), true); } else { - String[] lines = body.split("\n"); - StringBuilder builder = new StringBuilder(); - for(String l : lines) { + SpannableStringBuilder styledBody = new SpannableStringBuilder(body); + if (textColor != 0) { + StylingHelper.format(styledBody, 0, styledBody.length() - 1, textColor); + } + SpannableStringBuilder builder = new SpannableStringBuilder(); + for (CharSequence l : CharSequenceUtils.split(styledBody, '\n')) { if (l.length() > 0) { char first = l.charAt(0); if ((first != '>' || !isPositionFollowedByQuoteableCharacter(l,0)) && first != '\u00bb') { - String line = l.trim(); - if (line.isEmpty()) { + CharSequence line = CharSequenceUtils.trim(l); + if (line.length() == 0) { continue; } char last = line.charAt(line.length()-1); @@ -313,11 +322,15 @@ public class UIHelper { if (builder.length() == 0) { builder.append(body.trim()); } - return new Pair<>(builder.length() > 256 ? builder.substring(0,256) : builder.toString(), false); + return new Pair<>(builder, false); } } } + public static CharSequence shorten(CharSequence input) { + return input.length() > 256 ? StylingHelper.subSequence(input, 0, 256) : input; + } + public static boolean isPositionFollowedByQuoteableCharacter(CharSequence body, int pos) { return !isPositionFollowedByNumber(body, pos) && !isPositionFollowedByEmoticon(body, pos) -- cgit v1.2.3