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, String needle, boolean dark) {
 95		final int length = needle.length();
 96		String string = editable.toString();
 97		int start = indexOfIgnoreCase(string, needle, 0);
 98		while (start != -1) {
 99			int end = start + length;
100			editable.setSpan(new BackgroundColorSpan(ContextCompat.getColor(context, dark ? R.color.deep_purple_a100 : R.color.deep_purple_a200)), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
101			editable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, dark ? R.color.black87 : R.color.white)), start, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
102			start = indexOfIgnoreCase(string, needle, start + length);
103		}
104
105	}
106
107	public static boolean isDarkText(TextView textView) {
108		int argb = textView.getCurrentTextColor();
109		return Color.red(argb) + Color.green(argb) + Color.blue(argb) == 0;
110	}
111
112	private static ParcelableSpan createSpanForStyle(ImStyleParser.Style style) {
113		switch (style.getKeyword()) {
114			case "*":
115				return new StyleSpan(Typeface.BOLD);
116			case "_":
117				return new StyleSpan(Typeface.ITALIC);
118			case "~":
119				return new StrikethroughSpan();
120			case "`":
121			case "```":
122				return new TypefaceSpan("monospace");
123			default:
124				throw new AssertionError("Unknown Style");
125		}
126	}
127
128	private static void makeKeywordOpaque(final Editable editable, int start, int end, @ColorInt int fallbackTextColor) {
129		QuoteSpan[] quoteSpans = editable.getSpans(start, end, QuoteSpan.class);
130		@ColorInt int textColor = quoteSpans.length > 0 ? quoteSpans[0].getColor() : fallbackTextColor;
131		@ColorInt int keywordColor = transformColor(textColor);
132		editable.setSpan(new ForegroundColorSpan(keywordColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
133	}
134
135	private static
136	@ColorInt
137	int transformColor(@ColorInt int c) {
138		return Color.argb(Math.round(Color.alpha(c) * 0.6f), Color.red(c), Color.green(c), Color.blue(c));
139	}
140
141	private static int indexOfIgnoreCase(final String haystack, final String needle, final int start) {
142		if (haystack == null || needle == null) {
143			return -1;
144		}
145		final int endLimit = haystack.length() - needle.length() + 1;
146		if (start > endLimit) {
147			return -1;
148		}
149		if (needle.length() == 0) {
150			return start;
151		}
152		for (int i = start; i < endLimit; i++) {
153			if (haystack.regionMatches(true, i, needle, 0, needle.length())) {
154				return i;
155			}
156		}
157		return -1;
158	}
159
160	public static class MessageEditorStyler implements TextWatcher {
161
162		private final EditText mEditText;
163
164		public MessageEditorStyler(EditText editText) {
165			this.mEditText = editText;
166		}
167
168		@Override
169		public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
170
171		}
172
173		@Override
174		public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
175
176		}
177
178		@Override
179		public void afterTextChanged(Editable editable) {
180			clear(editable);
181			format(editable, mEditText.getCurrentTextColor());
182		}
183	}
184}