|
2 | 2 |
|
3 | 3 | import static com.fox2code.androidansi.AnsiParser.parseAsSpannable;
|
4 | 4 |
|
5 |
| -import android.annotation.SuppressLint; |
6 | 5 | import android.content.Context;
|
| 6 | +import android.content.res.Resources; |
7 | 7 | import android.content.res.TypedArray;
|
| 8 | +import android.text.InputFilter; |
8 | 9 | import android.util.AttributeSet;
|
| 10 | +import android.util.Log; |
9 | 11 | import android.widget.TextView;
|
10 | 12 |
|
11 | 13 | import androidx.annotation.NonNull;
|
| 14 | +import androidx.emoji2.viewsintegration.EmojiTextViewHelper; |
12 | 15 |
|
13 |
| -public class AnsiTextView extends TextView{ |
| 16 | +public class AnsiTextView extends TextView { |
| 17 | + public static final int FLAG_PARSE_DISABLE_COLORS = AnsiParser.FLAG_PARSE_DISABLE_COLORS; |
| 18 | + public static final int FLAG_PARSE_DISABLE_ATTRIBUTES = AnsiParser.FLAG_PARSE_DISABLE_ATTRIBUTES; |
| 19 | + public static final int FLAG_PARSE_DISABLE_EXTRAS_COLORS = AnsiParser.FLAG_PARSE_DISABLE_EXTRAS_COLORS; |
| 20 | + public static final int FLAG_PARSE_DISABLE_SUBSCRIPT = AnsiParser.FLAG_PARSE_DISABLE_SUBSCRIPT; |
14 | 21 |
|
15 |
| - public final int FLAG_PARSE_DISABLE_COLORS = AnsiParser.FLAG_PARSE_DISABLE_COLORS; |
16 |
| - public final int FLAG_PARSE_DISABLE_ATTRIBUTES = AnsiParser.FLAG_PARSE_DISABLE_ATTRIBUTES; |
17 |
| - public final int FLAG_PARSE_DISABLE_EXTRAS_COLORS = AnsiParser.FLAG_PARSE_DISABLE_EXTRAS_COLORS; |
18 |
| - public final int FLAG_PARSE_DISABLE_SUBSCRIPT = AnsiParser.FLAG_PARSE_DISABLE_SUBSCRIPT; |
| 22 | + private Object mEmojiTextViewHelper; // Hold reference without requiring hard references |
| 23 | + private int parseFlags = 0; |
19 | 24 |
|
20 | 25 | public AnsiTextView(Context context) {
|
21 | 26 | super(context);
|
| 27 | + preInit(); |
22 | 28 | }
|
23 | 29 |
|
24 | 30 | public AnsiTextView(Context context, AttributeSet attrs) {
|
25 | 31 | super(context, attrs);
|
26 |
| - readAttr(context,attrs); |
| 32 | + preInit(); |
| 33 | + readAttr(context, attrs); |
27 | 34 | }
|
28 | 35 |
|
29 | 36 | public AnsiTextView(Context context, AttributeSet attrs, int defStyle) {
|
30 | 37 | super(context, attrs, defStyle);
|
31 |
| - readAttr(context,attrs); |
| 38 | + preInit(); |
| 39 | + readAttr(context, attrs); |
32 | 40 | }
|
33 | 41 |
|
34 | 42 | public void setAnsiText(@NonNull String ansiText) {
|
35 |
| - super.setText(parseAsSpannable(ansiText)); |
| 43 | + super.setText(parseAsSpannable(ansiText, this.parseFlags)); |
36 | 44 | }
|
37 | 45 |
|
38 | 46 | public void setAnsiText(@NonNull String ansiText, int parseFlags) {
|
39 | 47 | super.setText(parseAsSpannable(ansiText, parseFlags));
|
40 | 48 | }
|
41 | 49 |
|
| 50 | + private void preInit() { |
| 51 | + try { // Using "new" for optional classes is ok with ART |
| 52 | + mEmojiTextViewHelper = new EmojiTextViewHelper(this, false); |
| 53 | + ((EmojiTextViewHelper) mEmojiTextViewHelper).updateTransformationMethod(); |
| 54 | + } catch (Exception ignored) { |
| 55 | + mEmojiTextViewHelper = null; |
| 56 | + } |
| 57 | + } |
| 58 | + |
42 | 59 | private void readAttr(Context context, AttributeSet attrs) {
|
43 |
| - TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AnsiTextView); |
44 |
| - @NonNull String text = a.getString(R.styleable.AnsiTextView_setAnsiText) ; |
45 |
| - if (text != null) { |
46 |
| - super.setText(parseAsSpannable(text)); |
| 60 | + TypedArray a = null; |
| 61 | + try { |
| 62 | + a = context.obtainStyledAttributes(attrs, R.styleable.AnsiTextView); |
| 63 | + @NonNull String text = a.getString(R.styleable.AnsiTextView_ansiText); |
| 64 | + int parseFlags = a.getInt(R.styleable.AnsiTextView_ansiText, -1); |
| 65 | + a.recycle(); a = null; |
| 66 | + if (parseFlags != -1) { |
| 67 | + this.parseFlags = parseFlags; |
| 68 | + } else { |
| 69 | + parseFlags = this.parseFlags; |
| 70 | + } |
| 71 | + if (text != null) { |
| 72 | + super.setText(parseAsSpannable(text, parseFlags)); |
| 73 | + } |
| 74 | + } catch (Resources.NotFoundException e) { |
| 75 | + Log.w("AnsiTextView", "Failed to resolve ansiText resource!", e); |
| 76 | + } finally { |
| 77 | + if (a != null) a.recycle(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void setFilters(InputFilter[] filters) { |
| 83 | + if (this.mEmojiTextViewHelper != null) { |
| 84 | + filters = ((EmojiTextViewHelper) this.mEmojiTextViewHelper).getFilters(filters); |
| 85 | + } |
| 86 | + super.setFilters(filters); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void setAllCaps(boolean allCaps) { |
| 91 | + super.setAllCaps(allCaps); |
| 92 | + if (this.mEmojiTextViewHelper != null) { |
| 93 | + ((EmojiTextViewHelper) this.mEmojiTextViewHelper).setAllCaps(allCaps); |
47 | 94 | }
|
48 |
| - a.recycle(); |
49 | 95 | }
|
50 | 96 | }
|
0 commit comments