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.graphics.Color;
 33import android.graphics.Typeface;
 34import android.support.annotation.ColorInt;
 35import android.text.Editable;
 36import android.text.ParcelableSpan;
 37import android.text.Spanned;
 38import android.text.TextWatcher;
 39import android.text.style.ForegroundColorSpan;
 40import android.text.style.StrikethroughSpan;
 41import android.text.style.StyleSpan;
 42import android.text.style.TypefaceSpan;
 43import android.widget.EditText;
 44
 45import java.util.Arrays;
 46import java.util.List;
 47
 48import eu.siacs.conversations.entities.Message;
 49import eu.siacs.conversations.ui.text.QuoteSpan;
 50
 51public class StylingHelper {
 52
 53	private static List<? extends Class<? extends ParcelableSpan>> SPAN_CLASSES = Arrays.asList(
 54			StyleSpan.class,
 55			StrikethroughSpan.class,
 56			TypefaceSpan.class,
 57			ForegroundColorSpan.class
 58	);
 59
 60	public static void clear(final Editable editable) {
 61		final int end = editable.length() - 1;
 62		for (Class<? extends ParcelableSpan> clazz : SPAN_CLASSES) {
 63			for (ParcelableSpan span : editable.getSpans(0, end, clazz)) {
 64				editable.removeSpan(span);
 65			}
 66		}
 67	}
 68
 69	public static void format(final Editable editable, int start, int end, @ColorInt int textColor) {
 70		for (ImStyleParser.Style style : ImStyleParser.parse(editable,start,end)) {
 71			final int keywordLength = style.getKeyword().length();
 72			editable.setSpan(createSpanForStyle(style), style.getStart() + keywordLength, style.getEnd() - keywordLength + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
 73			makeKeywordOpaque(editable, style.getStart(), style.getStart() + keywordLength, textColor);
 74			makeKeywordOpaque(editable, style.getEnd() - keywordLength + 1, style.getEnd() + 1, textColor);
 75		}
 76	}
 77
 78	public static void format(final Editable editable, @ColorInt int textColor) {
 79		int end = 0;
 80		Message.MergeSeparator[] spans = editable.getSpans(0, editable.length() - 1, Message.MergeSeparator.class);
 81		for(Message.MergeSeparator span : spans) {
 82			format(editable,end,editable.getSpanStart(span),textColor);
 83			end = editable.getSpanEnd(span);
 84		}
 85		format(editable,end,editable.length() -1,textColor);
 86	}
 87
 88	private static ParcelableSpan createSpanForStyle(ImStyleParser.Style style) {
 89		switch (style.getKeyword()) {
 90			case "*":
 91				return new StyleSpan(Typeface.BOLD);
 92			case "_":
 93				return new StyleSpan(Typeface.ITALIC);
 94			case "~":
 95				return new StrikethroughSpan();
 96			case "`":
 97			case "```":
 98				return new TypefaceSpan("monospace");
 99			default:
100				throw new AssertionError("Unknown Style");
101		}
102	}
103
104	private static void makeKeywordOpaque(final Editable editable, int start, int end, @ColorInt int fallbackTextColor) {
105		QuoteSpan[] quoteSpans = editable.getSpans(start, end, QuoteSpan.class);
106		@ColorInt int textColor = quoteSpans.length > 0 ? quoteSpans[0].getColor() : fallbackTextColor;
107		@ColorInt int keywordColor = transformColor(textColor);
108		editable.setSpan(new ForegroundColorSpan(keywordColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
109	}
110
111	private static
112	@ColorInt
113	int transformColor(@ColorInt int c) {
114		return Color.argb(Math.round(Color.alpha(c) * 0.6f), Color.red(c), Color.green(c), Color.blue(c));
115	}
116
117	public static class MessageEditorStyler implements TextWatcher {
118
119		private final EditText mEditText;
120
121		public MessageEditorStyler(EditText editText) {
122			this.mEditText = editText;
123		}
124
125		@Override
126		public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
127
128		}
129
130		@Override
131		public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
132
133		}
134
135		@Override
136		public void afterTextChanged(Editable editable) {
137			clear(editable);
138			format(editable, mEditText.getCurrentTextColor());
139		}
140	}
141}