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 editable.setSpan(createSpanForStyle(style), style.getStart() + 1, style.getEnd(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
71 makeKeywordOpaque(editable, style.getStart(), style.getStart() + 1, textColor);
72 makeKeywordOpaque(editable, style.getEnd(), style.getEnd() + 1, textColor);
73 }
74 }
75
76 private static ParcelableSpan createSpanForStyle(ImStyleParser.Style style) {
77 switch (style.getCharacter()) {
78 case '*':
79 return new StyleSpan(Typeface.BOLD);
80 case '_':
81 return new StyleSpan(Typeface.ITALIC);
82 case '~':
83 return new StrikethroughSpan();
84 case '`':
85 return new TypefaceSpan("monospace");
86 default:
87 throw new AssertionError("Unknown Style");
88 }
89 }
90
91 private static void makeKeywordOpaque(final Editable editable, int start, int end, @ColorInt int fallbackTextColor) {
92 QuoteSpan[] quoteSpans = editable.getSpans(start, end, QuoteSpan.class);
93 @ColorInt int textColor = quoteSpans.length > 0 ? quoteSpans[0].getColor() : fallbackTextColor;
94 @ColorInt int keywordColor = transformColor(textColor);
95 editable.setSpan(new ForegroundColorSpan(keywordColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
96 }
97
98 private static
99 @ColorInt
100 int transformColor(@ColorInt int c) {
101 return Color.argb(Math.round(Color.alpha(c) * 0.6f), Color.red(c), Color.green(c), Color.blue(c));
102 }
103
104 public static class MessageEditorStyler implements TextWatcher {
105
106 private final EditText mEditText;
107
108 public MessageEditorStyler(EditText editText) {
109 this.mEditText = editText;
110 }
111
112 @Override
113 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
114
115 }
116
117 @Override
118 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
119
120 }
121
122 @Override
123 public void afterTextChanged(Editable editable) {
124 clear(editable);
125 format(editable, mEditText.getCurrentTextColor());
126 }
127 }
128}