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) {
67 format(editable, false);
68 }
69
70 public static void format(final Editable editable, final boolean replaceStyleAnnotation) {
71 for (ImStyleParser.Style style : ImStyleParser.parse(editable)) {
72 editable.setSpan(createSpanForStyle(style), style.getStart(), style.getEnd(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
73 if (replaceStyleAnnotation) {
74 editable.replace(style.getStart(), style.getStart() + 1, "\u200B");
75 editable.replace(style.getEnd(), style.getEnd() + 1, "\u200B");
76 }
77 }
78 }
79
80 public static void format(final Editable editable, @ColorInt int color) {
81 for(ImStyleParser.Style style : ImStyleParser.parse(editable)) {
82 editable.setSpan(createSpanForStyle(style), style.getStart() + 1, style.getEnd() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
83 editable.setSpan(new ForegroundColorSpan(color),style.getStart(),style.getStart()+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
84 editable.setSpan(new ForegroundColorSpan(color),style.getEnd(),style.getEnd()+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
85 }
86 }
87
88 private static ParcelableSpan createSpanForStyle(ImStyleParser.Style style) {
89 switch (style.getCharacter()) {
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 return new TypefaceSpan("monospace");
98 default:
99 throw new AssertionError("Unknown Style");
100 }
101 }
102
103 public static class MessageEditorStyler implements TextWatcher {
104
105 private final EditText mEditText;
106
107 public MessageEditorStyler(EditText editText) {
108 this.mEditText = editText;
109 }
110
111 @Override
112 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
113
114 }
115
116 @Override
117 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
118
119 }
120
121 @Override
122 public void afterTextChanged(Editable editable) {
123 clear(editable);
124 final int color = mEditText.getCurrentTextColor();
125 final int syntaxColor = Color.argb(
126 Math.round(Color.alpha(color) * 0.5f),
127 Color.red(color),
128 Color.green(color),
129 Color.blue(color)
130 );
131 format(editable,syntaxColor);
132 }
133 }
134}