aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils/ui/TextViewUtil.java
blob: 6182d922c6a8a85a225a8df3ad0be323f961e3d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package de.thedevstack.conversationsplus.utils.ui;

import android.support.annotation.ColorRes;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.text.util.Linkify;
import android.util.Patterns;
import android.view.View;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.thedevstack.conversationsplus.ConversationsPlusColors;

/**
 *
 */
public final class TextViewUtil extends ViewUtil {
    public static void linkifyXmpp(TextView tv) {
        int patternMatchCount = 0;
        int oldAutoLinkMask = tv.getAutoLinkMask();
        CharSequence body = tv.getText();

        // first check if we have a match on XMPP_PATTERN so we do not have to check for EMAIL_ADDRESSES
        patternMatchCount += countMatches(XMPP_PATTERN, body);
        if ((Linkify.EMAIL_ADDRESSES & oldAutoLinkMask) != 0 && patternMatchCount > 0) {
            oldAutoLinkMask -= Linkify.EMAIL_ADDRESSES;
        }

        // count matches for all patterns
        if ((Linkify.WEB_URLS & oldAutoLinkMask) != 0) {
            patternMatchCount += countMatches(Patterns.WEB_URL, body);
        }
        if ((Linkify.EMAIL_ADDRESSES & oldAutoLinkMask) != 0) {
            patternMatchCount += countMatches(Patterns.EMAIL_ADDRESS, body);
        }
        if ((Linkify.PHONE_NUMBERS & oldAutoLinkMask) != 0) {
            patternMatchCount += countMatches(Patterns.PHONE, body);
        }

        tv.setTextIsSelectable(patternMatchCount <= 1);
        tv.setAutoLinkMask(0);
        Linkify.addLinks(tv, XMPP_PATTERN, "xmpp");
        tv.setAutoLinkMask(oldAutoLinkMask);
    }

    public static void setTextWithoutAutoLink(TextView tv, CharSequence text) {
        int oldAutoLinkMask = tv.getAutoLinkMask();
        tv.setAutoLinkMask(0);
        tv.setText(text);
        tv.setAutoLinkMask(oldAutoLinkMask);
    }

    public static TextView showAndSetText(TextView tv, CharSequence text) {
        visible(tv);
        return setText(tv, text);
    }

    public static TextView showAndSetText(TextView tv, int textResId) {
        visible(tv);
        return setText(tv, textResId);
    }

    public static TextView setText(TextView tv, CharSequence text) {
        if (null != tv) {
            tv.setText(text);
        }

        return tv;
    }

    public static TextView setText(TextView tv, int textResId) {
        if (null != tv) {
            tv.setText(textResId);
        }

        return tv;
    }

    public static TextView setText(View parentView, int textViewId, CharSequence text) {
        return setText((TextView) parentView.findViewById(textViewId), text);
    }

    public static TextView setText(View parentView, int textViewId, int textResId) {
        return setText((TextView) parentView.findViewById(textViewId), textResId);
    }

    public static void enable(TextView tv) {
        setColorEnabledAndTextResId(tv, null, true, null);
    }

    public static void enable(TextView tv, String text) {
        setColorEnabledAndText(tv, null, true, text);
    }

    public static void enable(TextView tv, Integer color) {
        setColorEnabledAndTextResId(tv, color, true, null);
    }

    public static void enable(TextView tv, Integer color, @StringRes Integer resid) {
        setColorEnabledAndTextResId(tv, color, true, resid);
    }

    public static void disable(TextView tv) {
        setColorEnabledAndTextResId(tv, null, false, null);
    }

    public static void disable(TextView tv, String text) {
        setColorEnabledAndText(tv, null, false, text);
    }

    public static void disable(TextView tv, Integer color) {
        setColorEnabledAndTextResId(tv, color, false, null);
    }

    public static void disable(TextView tv, Integer color, @StringRes Integer resid) {
        setColorEnabledAndTextResId(tv, color, false, resid);
    }

    public static void setColor(TextView tv, Integer color) {
        setColorByIdEnabledAndTextResId(tv, color, null, null);
    }

    private static void setColorByIdEnabledAndTextResId(TextView tv, @ColorRes Integer colorResId, Boolean enabled, @StringRes Integer resid) {
        Integer color = null;
        if (null != colorResId) {
            color = ConversationsPlusColors.byId(colorResId);
        }
        setColorEnabledAndTextResId(tv, color, enabled, resid);
    }

    public static void setColorEnabledAndTextResId(TextView tv, Integer color, Boolean enabled, @StringRes Integer resid) {
        if (null != color) {
            tv.setTextColor(color);
        }

        if (enabled != null) {
            tv.setEnabled(enabled);
        }

        if (resid != null) {
            setText(tv, resid);
        }
    }

    public static void setColorEnabledAndText(TextView tv, Integer color, Boolean enabled, String text) {
        if (null != color) {
            tv.setTextColor(color);
        }

        if (enabled != null) {
            tv.setEnabled(enabled);
        }

        if (text != null) {
            setText(tv, text);
        }
    }

    /**
     * Counts the number of occurrences of the pattern in body.
     *
     * @param pattern the pattern to match
     * @param body    the body to find the pattern
     * @return the number of occurrences
     */
    private static int countMatches(Pattern pattern, CharSequence body) {
        Matcher matcher = pattern.matcher(body);
        int count = 0;
        while (matcher.find()) {
            count++;
        }
        return count;
    }

    private static final Pattern XMPP_PATTERN = Pattern
            .compile("xmpp\\:(?:(?:["
                    + Patterns.GOOD_IRI_CHAR
                    + "\\;\\/\\?\\@\\&\\=\\#\\~\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])"
                    + "|(?:\\%[a-fA-F0-9]{2}))+");

    private TextViewUtil() {
        // avoid instantiation - helper class
    }
}