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