diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2008-12-17 18:05:43 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2008-12-17 18:05:43 -0800 |
commit | f013e1afd1e68af5e3b868c26a653bbfb39538f8 (patch) | |
tree | 7ad6c8fd9c7b55f4b4017171dec1cb760bbd26bf /core/java/android/text/util | |
parent | e70cfafe580c6f2994c4827cd8a534aabf3eb05c (diff) | |
download | frameworks_base-f013e1afd1e68af5e3b868c26a653bbfb39538f8.zip frameworks_base-f013e1afd1e68af5e3b868c26a653bbfb39538f8.tar.gz frameworks_base-f013e1afd1e68af5e3b868c26a653bbfb39538f8.tar.bz2 |
Code drop from //branches/cupcake/...@124589
Diffstat (limited to 'core/java/android/text/util')
-rw-r--r-- | core/java/android/text/util/Linkify.java | 38 | ||||
-rw-r--r-- | core/java/android/text/util/Regex.java | 20 |
2 files changed, 39 insertions, 19 deletions
diff --git a/core/java/android/text/util/Linkify.java b/core/java/android/text/util/Linkify.java index 79ecfbd..d61e888 100644 --- a/core/java/android/text/util/Linkify.java +++ b/core/java/android/text/util/Linkify.java @@ -69,7 +69,7 @@ public class Linkify { public static final int PHONE_NUMBERS = 0x04; /** - * Bit field indicating that phone numbers should be matched in methods that + * Bit field indicating that street addresses should be matched in methods that * take an options mask */ public static final int MAP_ADDRESSES = 0x08; @@ -78,8 +78,7 @@ public class Linkify { * Bit mask indicating that all available patterns should be matched in * methods that take an options mask */ - public static final int ALL = WEB_URLS | EMAIL_ADDRESSES | PHONE_NUMBERS - | MAP_ADDRESSES; + public static final int ALL = WEB_URLS | EMAIL_ADDRESSES | PHONE_NUMBERS | MAP_ADDRESSES; /** * Don't treat anything with fewer than this many digits as a @@ -109,8 +108,7 @@ public class Linkify { * Filters out URL matches that don't have enough digits to be a * phone number. */ - public static final MatchFilter sPhoneNumberMatchFilter = - new MatchFilter() { + public static final MatchFilter sPhoneNumberMatchFilter = new MatchFilter() { public final boolean acceptMatch(CharSequence s, int start, int end) { int digitCount = 0; @@ -133,8 +131,7 @@ public class Linkify { * '+1 (919) 555-1212' * becomes '+19195551212' */ - public static final TransformFilter sPhoneNumberTransformFilter = - new TransformFilter() { + public static final TransformFilter sPhoneNumberTransformFilter = new TransformFilter() { public final String transformUrl(final Matcher match, String url) { return Regex.digitsAndPlusOnly(match); } @@ -300,8 +297,7 @@ public class Linkify { * prepended to the url of links that do not have * a scheme specified in the link text */ - public static final void addLinks(TextView text, Pattern pattern, - String scheme) { + public static final void addLinks(TextView text, Pattern pattern, String scheme) { addLinks(text, pattern, scheme, null, null); } @@ -341,8 +337,7 @@ public class Linkify { * prepended to the url of links that do not have * a scheme specified in the link text */ - public static final boolean addLinks(Spannable text, Pattern pattern, - String scheme) { + public static final boolean addLinks(Spannable text, Pattern pattern, String scheme) { return addLinks(text, pattern, scheme, null, null); } @@ -388,8 +383,7 @@ public class Linkify { return hasMatches; } - private static final void applyLink(String url, int start, int end, - Spannable text) { + private static final void applyLink(String url, int start, int end, Spannable text) { URLSpan span = new URLSpan(url); text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); @@ -402,13 +396,22 @@ public class Linkify { } boolean hasPrefix = false; + for (int i = 0; i < prefixes.length; i++) { if (url.regionMatches(true, 0, prefixes[i], 0, prefixes[i].length())) { hasPrefix = true; + + // Fix capitalization if necessary + if (!url.regionMatches(false, 0, prefixes[i], 0, + prefixes[i].length())) { + url = prefixes[i] + url.substring(prefixes[i].length()); + } + break; } } + if (!hasPrefix) { url = prefixes[0] + url; } @@ -438,30 +441,35 @@ public class Linkify { } } - private static final void gatherMapLinks(ArrayList<LinkSpec> links, - Spannable s) { + private static final void gatherMapLinks(ArrayList<LinkSpec> links, Spannable s) { String string = s.toString(); String address; int base = 0; + while ((address = WebView.findAddress(string)) != null) { int start = string.indexOf(address); + if (start < 0) { break; } + LinkSpec spec = new LinkSpec(); int length = address.length(); int end = start + length; + spec.start = base + start; spec.end = base + end; string = string.substring(end); base += end; String encodedAddress = null; + try { encodedAddress = URLEncoder.encode(address,"UTF-8"); } catch (UnsupportedEncodingException e) { continue; } + spec.url = "geo:0,0?q=" + encodedAddress; links.add(spec); } diff --git a/core/java/android/text/util/Regex.java b/core/java/android/text/util/Regex.java index 55ad140..4c128ad 100644 --- a/core/java/android/text/util/Regex.java +++ b/core/java/android/text/util/Regex.java @@ -65,7 +65,7 @@ public class Regex { */ public static final Pattern WEB_URL_PATTERN = Pattern.compile( - "((?:(http|https):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)" + "((?:(http|https|Http|Https):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)" + "\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2}))+(?:\\:(?:[a-zA-Z0-9\\$\\-\\_" + "\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2}))+)?\\@)?)?" + "((?:(?:[a-zA-Z0-9][a-zA-Z0-9\\-]*\\.)+" // named host @@ -103,7 +103,9 @@ public class Regex { + "(?:\\:\\d{1,5})?)" // plus option port number + "(\\/(?:(?:[a-zA-Z0-9\\;\\/\\?\\:\\@\\&\\=\\#\\~" // plus option query params + "\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?" - + "\\b"); // and finally, a word boundary this is to stop foo.sure from matching as foo.su + + "(?:\\b|$)"); // and finally, a word boundary or end of + // input. This is to stop foo.sure from + // matching as foo.su public static final Pattern IP_ADDRESS_PATTERN = Pattern.compile( @@ -134,10 +136,20 @@ public class Regex { * might be phone numbers in arbitrary text, not for validating whether * something is in fact a phone number. It will miss many things that * are legitimate phone numbers. + * + * <p> The pattern matches the following: + * <ul> + * <li>Optionally, a + sign followed immediately by one or more digits. Spaces, dots, or dashes + * may follow. + * <li>Optionally, sets of digits in parentheses, separated by spaces, dots, or dashes. + * <li>A string starting and ending with a digit, containing digits, spaces, dots, and/or dashes. + * </ul> */ public static final Pattern PHONE_PATTERN - = Pattern.compile( - "(?:\\+[0-9]+)|(?:[0-9()][0-9()\\- \\.][0-9()\\- \\.]+[0-9])"); + = Pattern.compile( // sdd = space, dot, or dash + "(\\+[0-9]+[\\- \\.]*)?" // +<digits><sdd>* + + "(\\([0-9]+\\)[\\- \\.]*)?" // (<digits>)<sdd>* + + "([0-9][0-9\\- \\.][0-9\\- \\.]+[0-9])"); // <digit><digit|sdd>+<digit> /** * Convenience method to take all of the non-null matching groups in a |