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