| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.text.util; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.app.ActivityThread; |
| 23 |
import android.compat.annotation.UnsupportedAppUsage; |
| 24 |
import android.content.Context; |
| 25 |
import android.os.Build; |
| 26 |
import android.telephony.PhoneNumberUtils; |
| 27 |
import android.telephony.TelephonyManager; |
| 28 |
import android.text.Spannable; |
| 29 |
import android.text.SpannableString; |
| 30 |
import android.text.Spanned; |
| 31 |
import android.text.method.LinkMovementMethod; |
| 32 |
import android.text.method.MovementMethod; |
| 33 |
import android.text.style.URLSpan; |
| 34 |
import android.util.Log; |
| 35 |
import android.util.Patterns; |
| 36 |
import android.webkit.WebView; |
| 37 |
import android.widget.TextView; |
| 38 |
|
| 39 |
import com.android.i18n.phonenumbers.PhoneNumberMatch; |
| 40 |
import com.android.i18n.phonenumbers.PhoneNumberUtil; |
| 41 |
import com.android.i18n.phonenumbers.PhoneNumberUtil.Leniency; |
| 42 |
|
| 43 |
import libcore.util.EmptyArray; |
| 44 |
|
| 45 |
import java.io.UnsupportedEncodingException; |
| 46 |
import java.lang.annotation.Retention; |
| 47 |
import java.lang.annotation.RetentionPolicy; |
| 48 |
import java.net.URLEncoder; |
| 49 |
import java.util.ArrayList; |
| 50 |
import java.util.Collections; |
| 51 |
import java.util.Comparator; |
| 52 |
import java.util.Locale; |
| 53 |
import java.util.function.Function; |
| 54 |
import java.util.regex.Matcher; |
| 55 |
import java.util.regex.Pattern; |
| 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 |
public class Linkify { |
| 85 |
|
| 86 |
private static final String LOG_TAG = "Linkify"; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public static final int WEB_URLS = 0x01; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public static final int EMAIL_ADDRESSES = 0x02; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public static final int PHONE_NUMBERS = 0x04; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@Deprecated |
| 118 |
public static final int MAP_ADDRESSES = 0x08; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
@Deprecated |
| 133 |
public static final int ALL = WEB_URLS | EMAIL_ADDRESSES | PHONE_NUMBERS | MAP_ADDRESSES; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
private static final int PHONE_NUMBER_MINIMUM_DIGITS = 5; |
| 140 |
|
| 141 |
|
| 142 |
@IntDef(flag = true, value = { WEB_URLS, EMAIL_ADDRESSES, PHONE_NUMBERS, MAP_ADDRESSES, ALL }) |
| 143 |
@Retention(RetentionPolicy.SOURCE) |
| 144 |
public @interface LinkifyMask {} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public static final MatchFilter sUrlMatchFilter = new MatchFilter() { |
| 151 |
public final boolean acceptMatch(CharSequence s, int start, int end) { |
| 152 |
if (start == 0) { |
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
if (s.charAt(start - 1) == '@') { |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
return true; |
| 161 |
} |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public static final MatchFilter sPhoneNumberMatchFilter = new MatchFilter() { |
| 169 |
public final boolean acceptMatch(CharSequence s, int start, int end) { |
| 170 |
int digitCount = 0; |
| 171 |
|
| 172 |
for (int i = start; i < end; i++) { |
| 173 |
if (Character.isDigit(s.charAt(i))) { |
| 174 |
digitCount++; |
| 175 |
if (digitCount >= PHONE_NUMBER_MINIMUM_DIGITS) { |
| 176 |
return true; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
return false; |
| 181 |
} |
| 182 |
}; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public static final TransformFilter sPhoneNumberTransformFilter = new TransformFilter() { |
| 192 |
public final String transformUrl(final Matcher match, String url) { |
| 193 |
return Patterns.digitsAndPlusOnly(match); |
| 194 |
} |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public interface MatchFilter { |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
boolean acceptMatch(CharSequence s, int start, int end); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public interface TransformFilter { |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
String transformUrl(final Matcher match, String url); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public static final boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask) { |
| 261 |
return addLinks(text, mask, null, null); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public static final boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask, |
| 277 |
@Nullable Function<String, URLSpan> urlSpanFactory) { |
| 278 |
return addLinks(text, mask, null, urlSpanFactory); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
private static boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask, |
| 294 |
@Nullable Context context, @Nullable Function<String, URLSpan> urlSpanFactory) { |
| 295 |
if (text != null && containsUnsupportedCharacters(text.toString())) { |
| 296 |
android.util.EventLog.writeEvent(0x534e4554, "116321860", -1, ""); |
| 297 |
return false; |
| 298 |
} |
| 299 |
|
| 300 |
if (mask == 0) { |
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
final URLSpan[] old = text.getSpans(0, text.length(), URLSpan.class); |
| 305 |
|
| 306 |
for (int i = old.length - 1; i >= 0; i--) { |
| 307 |
text.removeSpan(old[i]); |
| 308 |
} |
| 309 |
|
| 310 |
final ArrayList<LinkSpec> links = new ArrayList<LinkSpec>(); |
| 311 |
|
| 312 |
if ((mask & WEB_URLS) != 0) { |
| 313 |
gatherLinks(links, text, Patterns.AUTOLINK_WEB_URL, |
| 314 |
new String[] { "http://", "https://", "rtsp://", "ftp://" }, |
| 315 |
sUrlMatchFilter, null); |
| 316 |
} |
| 317 |
|
| 318 |
if ((mask & EMAIL_ADDRESSES) != 0) { |
| 319 |
gatherLinks(links, text, Patterns.AUTOLINK_EMAIL_ADDRESS, |
| 320 |
new String[] { "mailto:" }, |
| 321 |
null, null); |
| 322 |
} |
| 323 |
|
| 324 |
if ((mask & PHONE_NUMBERS) != 0) { |
| 325 |
gatherTelLinks(links, text, context); |
| 326 |
} |
| 327 |
|
| 328 |
if ((mask & MAP_ADDRESSES) != 0) { |
| 329 |
gatherMapLinks(links, text); |
| 330 |
} |
| 331 |
|
| 332 |
pruneOverlaps(links); |
| 333 |
|
| 334 |
if (links.size() == 0) { |
| 335 |
return false; |
| 336 |
} |
| 337 |
|
| 338 |
for (LinkSpec link: links) { |
| 339 |
applyLink(link.url, link.start, link.end, text, urlSpanFactory); |
| 340 |
} |
| 341 |
|
| 342 |
return true; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
public static boolean containsUnsupportedCharacters(String text) { |
| 353 |
if (text.contains("\u202C")) { |
| 354 |
Log.e(LOG_TAG, "Unsupported character for applying links: u202C"); |
| 355 |
return true; |
| 356 |
} |
| 357 |
if (text.contains("\u202D")) { |
| 358 |
Log.e(LOG_TAG, "Unsupported character for applying links: u202D"); |
| 359 |
return true; |
| 360 |
} |
| 361 |
if (text.contains("\u202E")) { |
| 362 |
Log.e(LOG_TAG, "Unsupported character for applying links: u202E"); |
| 363 |
return true; |
| 364 |
} |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public static final boolean addLinks(@NonNull TextView text, @LinkifyMask int mask) { |
| 382 |
if (mask == 0) { |
| 383 |
return false; |
| 384 |
} |
| 385 |
|
| 386 |
final Context context = text.getContext(); |
| 387 |
final CharSequence t = text.getText(); |
| 388 |
if (t instanceof Spannable) { |
| 389 |
if (addLinks((Spannable) t, mask, context, null)) { |
| 390 |
addLinkMovementMethod(text); |
| 391 |
return true; |
| 392 |
} |
| 393 |
|
| 394 |
return false; |
| 395 |
} else { |
| 396 |
SpannableString s = SpannableString.valueOf(t); |
| 397 |
|
| 398 |
if (addLinks(s, mask, context, null)) { |
| 399 |
addLinkMovementMethod(text); |
| 400 |
text.setText(s); |
| 401 |
|
| 402 |
return true; |
| 403 |
} |
| 404 |
|
| 405 |
return false; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
private static final void addLinkMovementMethod(@NonNull TextView t) { |
| 410 |
MovementMethod m = t.getMovementMethod(); |
| 411 |
|
| 412 |
if ((m == null) || !(m instanceof LinkMovementMethod)) { |
| 413 |
if (t.getLinksClickable()) { |
| 414 |
t.setMovementMethod(LinkMovementMethod.getInstance()); |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
public static final void addLinks(@NonNull TextView text, @NonNull Pattern pattern, |
| 431 |
@Nullable String scheme) { |
| 432 |
addLinks(text, pattern, scheme, null, null, null); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
public static final void addLinks(@NonNull TextView text, @NonNull Pattern pattern, |
| 450 |
@Nullable String scheme, @Nullable MatchFilter matchFilter, |
| 451 |
@Nullable TransformFilter transformFilter) { |
| 452 |
addLinks(text, pattern, scheme, null, matchFilter, transformFilter); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
public static final void addLinks(@NonNull TextView text, @NonNull Pattern pattern, |
| 473 |
@Nullable String defaultScheme, @Nullable String[] schemes, |
| 474 |
@Nullable MatchFilter matchFilter, @Nullable TransformFilter transformFilter) { |
| 475 |
SpannableString spannable = SpannableString.valueOf(text.getText()); |
| 476 |
|
| 477 |
boolean linksAdded = addLinks(spannable, pattern, defaultScheme, schemes, matchFilter, |
| 478 |
transformFilter); |
| 479 |
if (linksAdded) { |
| 480 |
text.setText(spannable); |
| 481 |
addLinkMovementMethod(text); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
public static final boolean addLinks(@NonNull Spannable text, @NonNull Pattern pattern, |
| 496 |
@Nullable String scheme) { |
| 497 |
return addLinks(text, pattern, scheme, null, null, null); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
public static final boolean addLinks(@NonNull Spannable spannable, @NonNull Pattern pattern, |
| 517 |
@Nullable String scheme, @Nullable MatchFilter matchFilter, |
| 518 |
@Nullable TransformFilter transformFilter) { |
| 519 |
return addLinks(spannable, pattern, scheme, null, matchFilter, |
| 520 |
transformFilter); |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
public static final boolean addLinks(@NonNull Spannable spannable, @NonNull Pattern pattern, |
| 542 |
@Nullable String defaultScheme, @Nullable String[] schemes, |
| 543 |
@Nullable MatchFilter matchFilter, @Nullable TransformFilter transformFilter) { |
| 544 |
return addLinks(spannable, pattern, defaultScheme, schemes, matchFilter, transformFilter, |
| 545 |
null); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
public static final boolean addLinks(@NonNull Spannable spannable, @NonNull Pattern pattern, |
| 567 |
@Nullable String defaultScheme, @Nullable String[] schemes, |
| 568 |
@Nullable MatchFilter matchFilter, @Nullable TransformFilter transformFilter, |
| 569 |
@Nullable Function<String, URLSpan> urlSpanFactory) { |
| 570 |
if (spannable != null && containsUnsupportedCharacters(spannable.toString())) { |
| 571 |
android.util.EventLog.writeEvent(0x534e4554, "116321860", -1, ""); |
| 572 |
return false; |
| 573 |
} |
| 574 |
|
| 575 |
final String[] schemesCopy; |
| 576 |
if (defaultScheme == null) defaultScheme = ""; |
| 577 |
if (schemes == null || schemes.length < 1) { |
| 578 |
schemes = EmptyArray.STRING; |
| 579 |
} |
| 580 |
|
| 581 |
schemesCopy = new String[schemes.length + 1]; |
| 582 |
schemesCopy[0] = defaultScheme.toLowerCase(Locale.ROOT); |
| 583 |
for (int index = 0; index < schemes.length; index++) { |
| 584 |
String scheme = schemes[index]; |
| 585 |
schemesCopy[index + 1] = (scheme == null) ? "" : scheme.toLowerCase(Locale.ROOT); |
| 586 |
} |
| 587 |
|
| 588 |
boolean hasMatches = false; |
| 589 |
Matcher m = pattern.matcher(spannable); |
| 590 |
|
| 591 |
while (m.find()) { |
| 592 |
int start = m.start(); |
| 593 |
int end = m.end(); |
| 594 |
boolean allowed = true; |
| 595 |
|
| 596 |
if (matchFilter != null) { |
| 597 |
allowed = matchFilter.acceptMatch(spannable, start, end); |
| 598 |
} |
| 599 |
|
| 600 |
if (allowed) { |
| 601 |
String url = makeUrl(m.group(0), schemesCopy, m, transformFilter); |
| 602 |
|
| 603 |
applyLink(url, start, end, spannable, urlSpanFactory); |
| 604 |
hasMatches = true; |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
return hasMatches; |
| 609 |
} |
| 610 |
|
| 611 |
private static void applyLink(String url, int start, int end, Spannable text, |
| 612 |
@Nullable Function<String, URLSpan> urlSpanFactory) { |
| 613 |
if (urlSpanFactory == null) { |
| 614 |
urlSpanFactory = DEFAULT_SPAN_FACTORY; |
| 615 |
} |
| 616 |
final URLSpan span = urlSpanFactory.apply(url); |
| 617 |
text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 618 |
} |
| 619 |
|
| 620 |
private static final String makeUrl(@NonNull String url, @NonNull String[] prefixes, |
| 621 |
Matcher matcher, @Nullable TransformFilter filter) { |
| 622 |
if (filter != null) { |
| 623 |
url = filter.transformUrl(matcher, url); |
| 624 |
} |
| 625 |
|
| 626 |
boolean hasPrefix = false; |
| 627 |
|
| 628 |
for (int i = 0; i < prefixes.length; i++) { |
| 629 |
if (url.regionMatches(true, 0, prefixes[i], 0, prefixes[i].length())) { |
| 630 |
hasPrefix = true; |
| 631 |
|
| 632 |
|
| 633 |
if (!url.regionMatches(false, 0, prefixes[i], 0, prefixes[i].length())) { |
| 634 |
url = prefixes[i] + url.substring(prefixes[i].length()); |
| 635 |
} |
| 636 |
|
| 637 |
break; |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
if (!hasPrefix && prefixes.length > 0) { |
| 642 |
url = prefixes[0] + url; |
| 643 |
} |
| 644 |
|
| 645 |
return url; |
| 646 |
} |
| 647 |
|
| 648 |
private static final void gatherLinks(ArrayList<LinkSpec> links, |
| 649 |
Spannable s, Pattern pattern, String[] schemes, |
| 650 |
MatchFilter matchFilter, TransformFilter transformFilter) { |
| 651 |
Matcher m = pattern.matcher(s); |
| 652 |
|
| 653 |
while (m.find()) { |
| 654 |
int start = m.start(); |
| 655 |
int end = m.end(); |
| 656 |
|
| 657 |
if (matchFilter == null || matchFilter.acceptMatch(s, start, end)) { |
| 658 |
LinkSpec spec = new LinkSpec(); |
| 659 |
String url = makeUrl(m.group(0), schemes, m, transformFilter); |
| 660 |
|
| 661 |
spec.url = url; |
| 662 |
spec.start = start; |
| 663 |
spec.end = end; |
| 664 |
|
| 665 |
links.add(spec); |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 671 |
private static void gatherTelLinks(ArrayList<LinkSpec> links, Spannable s, |
| 672 |
@Nullable Context context) { |
| 673 |
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); |
| 674 |
final Context ctx = (context != null) ? context : ActivityThread.currentApplication(); |
| 675 |
final String regionCode = (ctx != null) ? ctx.getSystemService(TelephonyManager.class). |
| 676 |
getSimCountryIso().toUpperCase(Locale.US) : Locale.getDefault().getCountry(); |
| 677 |
Iterable<PhoneNumberMatch> matches = phoneUtil.findNumbers(s.toString(), |
| 678 |
regionCode, Leniency.POSSIBLE, Long.MAX_VALUE); |
| 679 |
for (PhoneNumberMatch match : matches) { |
| 680 |
LinkSpec spec = new LinkSpec(); |
| 681 |
spec.url = "tel:" + PhoneNumberUtils.normalizeNumber(match.rawString()); |
| 682 |
spec.start = match.start(); |
| 683 |
spec.end = match.end(); |
| 684 |
links.add(spec); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
private static final void gatherMapLinks(ArrayList<LinkSpec> links, Spannable s) { |
| 689 |
String string = s.toString(); |
| 690 |
String address; |
| 691 |
int base = 0; |
| 692 |
|
| 693 |
try { |
| 694 |
while ((address = WebView.findAddress(string)) != null) { |
| 695 |
int start = string.indexOf(address); |
| 696 |
|
| 697 |
if (start < 0) { |
| 698 |
break; |
| 699 |
} |
| 700 |
|
| 701 |
LinkSpec spec = new LinkSpec(); |
| 702 |
int length = address.length(); |
| 703 |
int end = start + length; |
| 704 |
|
| 705 |
spec.start = base + start; |
| 706 |
spec.end = base + end; |
| 707 |
string = string.substring(end); |
| 708 |
base += end; |
| 709 |
|
| 710 |
String encodedAddress = null; |
| 711 |
|
| 712 |
try { |
| 713 |
encodedAddress = URLEncoder.encode(address,"UTF-8"); |
| 714 |
} catch (UnsupportedEncodingException e) { |
| 715 |
continue; |
| 716 |
} |
| 717 |
|
| 718 |
spec.url = "geo:0,0?q=" + encodedAddress; |
| 719 |
links.add(spec); |
| 720 |
} |
| 721 |
} catch (UnsupportedOperationException e) { |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
return; |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
private static final void pruneOverlaps(ArrayList<LinkSpec> links) { |
| 730 |
Comparator<LinkSpec> c = new Comparator<LinkSpec>() { |
| 731 |
public final int compare(LinkSpec a, LinkSpec b) { |
| 732 |
if (a.start < b.start) { |
| 733 |
return -1; |
| 734 |
} |
| 735 |
|
| 736 |
if (a.start > b.start) { |
| 737 |
return 1; |
| 738 |
} |
| 739 |
|
| 740 |
if (a.end < b.end) { |
| 741 |
return 1; |
| 742 |
} |
| 743 |
|
| 744 |
if (a.end > b.end) { |
| 745 |
return -1; |
| 746 |
} |
| 747 |
|
| 748 |
return 0; |
| 749 |
} |
| 750 |
}; |
| 751 |
|
| 752 |
Collections.sort(links, c); |
| 753 |
|
| 754 |
int len = links.size(); |
| 755 |
int i = 0; |
| 756 |
|
| 757 |
while (i < len - 1) { |
| 758 |
LinkSpec a = links.get(i); |
| 759 |
LinkSpec b = links.get(i + 1); |
| 760 |
int remove = -1; |
| 761 |
|
| 762 |
if ((a.start <= b.start) && (a.end > b.start)) { |
| 763 |
if (b.end <= a.end) { |
| 764 |
remove = i + 1; |
| 765 |
} else if ((a.end - a.start) > (b.end - b.start)) { |
| 766 |
remove = i + 1; |
| 767 |
} else if ((a.end - a.start) < (b.end - b.start)) { |
| 768 |
remove = i; |
| 769 |
} |
| 770 |
|
| 771 |
if (remove != -1) { |
| 772 |
links.remove(remove); |
| 773 |
len--; |
| 774 |
continue; |
| 775 |
} |
| 776 |
|
| 777 |
} |
| 778 |
|
| 779 |
i++; |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
private static final Function<String, URLSpan> DEFAULT_SPAN_FACTORY = |
| 788 |
(String string) -> new URLSpan(string); |
| 789 |
} |
| 790 |
|
| 791 |
class LinkSpec { |
| 792 |
String url; |
| 793 |
int start; |
| 794 |
int end; |
| 795 |
} |