|
34 | 34 | import android.text.TextUtils;
|
35 | 35 | import android.text.style.URLSpan;
|
36 | 36 | import android.text.util.Linkify;
|
| 37 | +import android.text.util.Linkify.MatchFilter; |
37 | 38 | import android.util.AttributeSet;
|
38 | 39 | import android.util.DisplayMetrics;
|
39 | 40 | import android.util.Log;
|
| 41 | +import android.util.Patterns; |
40 | 42 | import android.view.GestureDetector;
|
41 | 43 | import android.view.KeyEvent;
|
42 | 44 | import android.view.MotionEvent;
|
@@ -227,6 +229,33 @@ public void run() {
|
227 | 229 | */
|
228 | 230 | private Hashtable<Integer,URLSpan[]> mLinkLayer = new Hashtable<Integer,URLSpan[]>();
|
229 | 231 |
|
| 232 | + /** |
| 233 | + * Accept links that start with http[s]: |
| 234 | + */ |
| 235 | + private static class HttpMatchFilter implements MatchFilter { |
| 236 | + public boolean acceptMatch(CharSequence s, int start, int end) { |
| 237 | + return startsWith(s, start, end, "http:") || |
| 238 | + startsWith(s, start, end, "https:"); |
| 239 | + } |
| 240 | + |
| 241 | + private boolean startsWith(CharSequence s, int start, int end, |
| 242 | + String prefix) { |
| 243 | + int prefixLen = prefix.length(); |
| 244 | + int fragmentLen = end - start; |
| 245 | + if (prefixLen > fragmentLen) { |
| 246 | + return false; |
| 247 | + } |
| 248 | + for (int i = 0; i < prefixLen; i++) { |
| 249 | + if (s.charAt(start + i) != prefix.charAt(i)) { |
| 250 | + return false; |
| 251 | + } |
| 252 | + } |
| 253 | + return true; |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + private static MatchFilter sHttpMatchFilter = new HttpMatchFilter(); |
| 258 | + |
230 | 259 | /**
|
231 | 260 | * Convert any URLs in the current row into a URLSpan,
|
232 | 261 | * and store that result in a hash table of URLSpan entries.
|
@@ -265,7 +294,8 @@ private int createLinks(int row)
|
265 | 294 | ++lineCount;
|
266 | 295 | }
|
267 | 296 |
|
268 |
| - Linkify.addLinks(textToLinkify, Linkify.WEB_URLS); |
| 297 | + Linkify.addLinks(textToLinkify, Patterns.WEB_URL, |
| 298 | + null, sHttpMatchFilter, null); |
269 | 299 | URLSpan [] urls = textToLinkify.getSpans(0, textToLinkify.length(), URLSpan.class);
|
270 | 300 | if(urls.length > 0)
|
271 | 301 | {
|
|
0 commit comments