Skip to content

Commit 59a9060

Browse files
committed
Auto-linkification only match URLs that start with https?:
Fixes #319
1 parent f71f3ea commit 59a9060

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

libraries/emulatorview/src/jackpal/androidterm/emulatorview/EmulatorView.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@
3434
import android.text.TextUtils;
3535
import android.text.style.URLSpan;
3636
import android.text.util.Linkify;
37+
import android.text.util.Linkify.MatchFilter;
3738
import android.util.AttributeSet;
3839
import android.util.DisplayMetrics;
3940
import android.util.Log;
41+
import android.util.Patterns;
4042
import android.view.GestureDetector;
4143
import android.view.KeyEvent;
4244
import android.view.MotionEvent;
@@ -227,6 +229,33 @@ public void run() {
227229
*/
228230
private Hashtable<Integer,URLSpan[]> mLinkLayer = new Hashtable<Integer,URLSpan[]>();
229231

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+
230259
/**
231260
* Convert any URLs in the current row into a URLSpan,
232261
* and store that result in a hash table of URLSpan entries.
@@ -265,7 +294,8 @@ private int createLinks(int row)
265294
++lineCount;
266295
}
267296

268-
Linkify.addLinks(textToLinkify, Linkify.WEB_URLS);
297+
Linkify.addLinks(textToLinkify, Patterns.WEB_URL,
298+
null, sHttpMatchFilter, null);
269299
URLSpan [] urls = textToLinkify.getSpans(0, textToLinkify.length(), URLSpan.class);
270300
if(urls.length > 0)
271301
{

0 commit comments

Comments
 (0)