StylingHelper.java

  1/*
  2 * Copyright (c) 2017, Daniel Gultsch All rights reserved.
  3 *
  4 * Redistribution and use in source and binary forms, with or without modification,
  5 * are permitted provided that the following conditions are met:
  6 *
  7 * 1. Redistributions of source code must retain the above copyright notice, this
  8 * list of conditions and the following disclaimer.
  9 *
 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
 11 * this list of conditions and the following disclaimer in the documentation and/or
 12 * other materials provided with the distribution.
 13 *
 14 * 3. Neither the name of the copyright holder nor the names of its contributors
 15 * may be used to endorse or promote products derived from this software without
 16 * specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 */
 29
 30package eu.siacs.conversations.utils;
 31
 32import android.content.Context;
 33import android.graphics.Color;
 34import android.graphics.Typeface;
 35import android.support.annotation.ColorInt;
 36import android.support.v4.content.ContextCompat;
 37import android.text.Editable;
 38import android.text.ParcelableSpan;
 39import android.text.SpannableString;
 40import android.text.Spanned;
 41import android.text.TextWatcher;
 42import android.text.style.BackgroundColorSpan;
 43import android.text.style.ForegroundColorSpan;
 44import android.text.style.StrikethroughSpan;
 45import android.text.style.StyleSpan;
 46import android.text.style.TypefaceSpan;
 47import android.widget.EditText;
 48import android.widget.TextView;
 49
 50import java.util.Arrays;
 51import java.util.List;
 52
 53import eu.siacs.conversations.R;
 54import eu.siacs.conversations.entities.Message;
 55import eu.siacs.conversations.ui.text.QuoteSpan;
 56
 57public class StylingHelper {
 58
 59	private static List<? extends Class<? extends ParcelableSpan>> SPAN_CLASSES = Arrays.asList(
 60			StyleSpan.class,
 61			StrikethroughSpan.class,
 62			TypefaceSpan.class,
 63			ForegroundColorSpan.class
 64	);
 65
 66	public static void clear(final Editable editable) {
 67		final int end = editable.length() - 1;
 68		for (Class<? extends ParcelableSpan> clazz : SPAN_CLASSES) {
 69			for (ParcelableSpan span : editable.getSpans(0, end, clazz)) {
 70				editable.removeSpan(span);
 71			}
 72		}
 73	}
 74
 75	public static void format(final Editable editable, int start, int end, @ColorInt int textColor) {
 76		for (ImStyleParser.Style style : ImStyleParser.parse(editable, start, end)) {
 77			final int keywordLength = style.getKeyword().length();
 78			editable.setSpan(createSpanForStyle(style), style.getStart() + keywordLength, style.getEnd() - keywordLength + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 79			makeKeywordOpaque(editable, style.getStart(), style.getStart() + keywordLength, textColor);
 80			makeKeywordOpaque(editable, style.getEnd() - keywordLength + 1, style.getEnd() + 1, textColor);
 81		}
 82	}
 83
 84	public static void format(final Editable editable, @ColorInt int textColor) {
 85		int end = 0;
 86		Message.MergeSeparator[] spans = editable.getSpans(0, editable.length() - 1, Message.MergeSeparator.class);
 87		for (Message.MergeSeparator span : spans) {
 88			format(editable, end, editable.getSpanStart(span), textColor);
 89			end = editable.getSpanEnd(span);
 90		}
 91		format(editable, end, editable.length() - 1, textColor);
 92	}
 93
 94	public static void highlight(final Context context, final Editable editable, List<String> needles, boolean dark) {
 95		for(String needle : needles) {
 96			if (!FtsUtils.isKeyword(needle)) {
 97				highlight(context, editable, needle, dark);
 98			}
 99		}
100	}
101
102	private static void highlight(final Context context, final Editable editable, String needle, boolean dark) {
103		final int length = needle.length();
104		String string = editable.toString();
105		int start = indexOfIgnoreCase(string, needle, 0);
106		while (start != -1) {
107			int end = start + length;
108			editable.setSpan(new BackgroundColorSpan(ContextCompat.getColor(context, dark ? R.color.blue_a100 : R.color.blue_a200)), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
109			editable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, dark ? R.color.black87 : R.color.white)), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
110			start = indexOfIgnoreCase(string, needle, start + length);
111		}
112
113	}
114
115	public static boolean isDarkText(TextView textView) {
116		int argb = textView.getCurrentTextColor();
117		return Color.red(argb) + Color.green(argb) + Color.blue(argb) == 0;
118	}
119
120	private static ParcelableSpan createSpanForStyle(ImStyleParser.Style style) {
121		switch (style.getKeyword()) {
122			case "*":
123				return new StyleSpan(Typeface.BOLD);
124			case "_":
125				return new StyleSpan(Typeface.ITALIC);
126			case "~":
127				return new StrikethroughSpan();
128			case "`":
129			case "```":
130				return new TypefaceSpan("monospace");
131			default:
132				throw new AssertionError("Unknown Style");
133		}
134	}
135
136	private static void makeKeywordOpaque(final Editable editable, int start, int end, @ColorInt int fallbackTextColor) {
137		QuoteSpan[] quoteSpans = editable.getSpans(start, end, QuoteSpan.class);
138		@ColorInt int textColor = quoteSpans.length > 0 ? quoteSpans[0].getColor() : fallbackTextColor;
139		@ColorInt int keywordColor = transformColor(textColor);
140		editable.setSpan(new ForegroundColorSpan(keywordColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
141	}
142
143	private static
144	@ColorInt
145	int transformColor(@ColorInt int c) {
146		return Color.argb(Math.round(Color.alpha(c) * 0.6f), Color.red(c), Color.green(c), Color.blue(c));
147	}
148
149	private static int indexOfIgnoreCase(final String haystack, final String needle, final int start) {
150		if (haystack == null || needle == null) {
151			return -1;
152		}
153		final int endLimit = haystack.length() - needle.length() + 1;
154		if (start > endLimit) {
155			return -1;
156		}
157		if (needle.length() == 0) {
158			return start;
159		}
160		for (int i = start; i < endLimit; i++) {
161			if (haystack.regionMatches(true, i, needle, 0, needle.length())) {
162				return i;
163			}
164		}
165		return -1;
166	}
167
168	public static class MessageEditorStyler implements TextWatcher {
169
170		private final EditText mEditText;
171
172		public MessageEditorStyler(EditText editText) {
173			this.mEditText = editText;
174		}
175
176		@Override
177		public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
178
179		}
180
181		@Override
182		public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
183
184		}
185
186		@Override
187		public void afterTextChanged(Editable editable) {
188			clear(editable);
189			format(editable, mEditText.getCurrentTextColor());
190		}
191	}
192}