diff options
Diffstat (limited to 'src/main/java/eu/siacs/conversations/utils/StylingHelper.java')
-rw-r--r-- | src/main/java/eu/siacs/conversations/utils/StylingHelper.java | 51 |
1 files changed, 47 insertions, 4 deletions
diff --git a/src/main/java/eu/siacs/conversations/utils/StylingHelper.java b/src/main/java/eu/siacs/conversations/utils/StylingHelper.java index 454844488..84eae1e41 100644 --- a/src/main/java/eu/siacs/conversations/utils/StylingHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/StylingHelper.java @@ -29,22 +29,28 @@ package eu.siacs.conversations.utils; +import android.content.Context; import android.graphics.Color; import android.graphics.Typeface; import android.support.annotation.ColorInt; +import android.support.v4.content.ContextCompat; import android.text.Editable; import android.text.ParcelableSpan; +import android.text.SpannableString; import android.text.Spanned; import android.text.TextWatcher; +import android.text.style.BackgroundColorSpan; import android.text.style.ForegroundColorSpan; import android.text.style.StrikethroughSpan; import android.text.style.StyleSpan; import android.text.style.TypefaceSpan; import android.widget.EditText; +import android.widget.TextView; import java.util.Arrays; import java.util.List; +import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.ui.text.QuoteSpan; @@ -67,7 +73,7 @@ public class StylingHelper { } public static void format(final Editable editable, int start, int end, @ColorInt int textColor) { - for (ImStyleParser.Style style : ImStyleParser.parse(editable,start,end)) { + for (ImStyleParser.Style style : ImStyleParser.parse(editable, start, end)) { final int keywordLength = style.getKeyword().length(); editable.setSpan(createSpanForStyle(style), style.getStart() + keywordLength, style.getEnd() - keywordLength + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); makeKeywordOpaque(editable, style.getStart(), style.getStart() + keywordLength, textColor); @@ -78,11 +84,29 @@ public class StylingHelper { public static void format(final Editable editable, @ColorInt int textColor) { int end = 0; Message.MergeSeparator[] spans = editable.getSpans(0, editable.length() - 1, Message.MergeSeparator.class); - for(Message.MergeSeparator span : spans) { - format(editable,end,editable.getSpanStart(span),textColor); + for (Message.MergeSeparator span : spans) { + format(editable, end, editable.getSpanStart(span), textColor); end = editable.getSpanEnd(span); } - format(editable,end,editable.length() -1,textColor); + format(editable, end, editable.length() - 1, textColor); + } + + public static void highlight(final Context context, final Editable editable, String needle, boolean dark) { + final int length = needle.length(); + String string = editable.toString(); + int start = indexOfIgnoreCase(string, needle, 0); + while (start != -1) { + int end = start + length; + editable.setSpan(new BackgroundColorSpan(ContextCompat.getColor(context, dark ? R.color.deep_purple_a100 : R.color.deep_purple_a200)), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); + editable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, dark ? R.color.black87 : R.color.white)), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); + start = indexOfIgnoreCase(string, needle, start + length); + } + + } + + public static boolean isDarkText(TextView textView) { + int argb = textView.getCurrentTextColor(); + return Color.red(argb) + Color.green(argb) + Color.blue(argb) == 0; } private static ParcelableSpan createSpanForStyle(ImStyleParser.Style style) { @@ -114,6 +138,25 @@ public class StylingHelper { return Color.argb(Math.round(Color.alpha(c) * 0.6f), Color.red(c), Color.green(c), Color.blue(c)); } + private static int indexOfIgnoreCase(final String haystack, final String needle, final int start) { + if (haystack == null || needle == null) { + return -1; + } + final int endLimit = haystack.length() - needle.length() + 1; + if (start > endLimit) { + return -1; + } + if (needle.length() == 0) { + return start; + } + for (int i = start; i < endLimit; i++) { + if (haystack.regionMatches(true, i, needle, 0, needle.length())) { + return i; + } + } + return -1; + } + public static class MessageEditorStyler implements TextWatcher { private final EditText mEditText; |