| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view.textclassifier; |
| 18 |
|
| 19 |
import android.annotation.FloatRange; |
| 20 |
import android.annotation.IntDef; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.content.Context; |
| 24 |
import android.os.Bundle; |
| 25 |
import android.os.LocaleList; |
| 26 |
import android.os.Parcel; |
| 27 |
import android.os.Parcelable; |
| 28 |
import android.text.Spannable; |
| 29 |
import android.text.method.MovementMethod; |
| 30 |
import android.text.style.ClickableSpan; |
| 31 |
import android.text.style.URLSpan; |
| 32 |
import android.view.View; |
| 33 |
import android.view.textclassifier.TextClassifier.EntityConfig; |
| 34 |
import android.view.textclassifier.TextClassifier.EntityType; |
| 35 |
import android.widget.TextView; |
| 36 |
|
| 37 |
import com.android.internal.annotations.VisibleForTesting; |
| 38 |
import com.android.internal.annotations.VisibleForTesting.Visibility; |
| 39 |
import com.android.internal.util.Preconditions; |
| 40 |
|
| 41 |
import java.lang.annotation.Retention; |
| 42 |
import java.lang.annotation.RetentionPolicy; |
| 43 |
import java.time.ZonedDateTime; |
| 44 |
import java.util.ArrayList; |
| 45 |
import java.util.Collection; |
| 46 |
import java.util.Collections; |
| 47 |
import java.util.List; |
| 48 |
import java.util.Locale; |
| 49 |
import java.util.Map; |
| 50 |
import java.util.Objects; |
| 51 |
import java.util.function.Function; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public final class TextLinks implements Parcelable { |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
@Retention(RetentionPolicy.SOURCE) |
| 64 |
@IntDef({STATUS_LINKS_APPLIED, STATUS_NO_LINKS_FOUND, STATUS_NO_LINKS_APPLIED, |
| 65 |
STATUS_DIFFERENT_TEXT, STATUS_UNSUPPORTED_CHARACTER}) |
| 66 |
public @interface Status {} |
| 67 |
|
| 68 |
|
| 69 |
public static final int STATUS_LINKS_APPLIED = 0; |
| 70 |
|
| 71 |
|
| 72 |
public static final int STATUS_NO_LINKS_FOUND = 1; |
| 73 |
|
| 74 |
|
| 75 |
public static final int STATUS_NO_LINKS_APPLIED = 2; |
| 76 |
|
| 77 |
|
| 78 |
public static final int STATUS_DIFFERENT_TEXT = 3; |
| 79 |
|
| 80 |
|
| 81 |
public static final int STATUS_UNSUPPORTED_CHARACTER = 4; |
| 82 |
|
| 83 |
|
| 84 |
@Retention(RetentionPolicy.SOURCE) |
| 85 |
@IntDef({APPLY_STRATEGY_IGNORE, APPLY_STRATEGY_REPLACE}) |
| 86 |
public @interface ApplyStrategy {} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public static final int APPLY_STRATEGY_IGNORE = 0; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public static final int APPLY_STRATEGY_REPLACE = 1; |
| 99 |
|
| 100 |
private final String mFullText; |
| 101 |
private final List<TextLink> mLinks; |
| 102 |
private final Bundle mExtras; |
| 103 |
|
| 104 |
private TextLinks(String fullText, ArrayList<TextLink> links, Bundle extras) { |
| 105 |
mFullText = fullText; |
| 106 |
mLinks = Collections.unmodifiableList(links); |
| 107 |
mExtras = extras; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
@NonNull |
| 114 |
public CharSequence getText() { |
| 115 |
return mFullText; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
@NonNull |
| 122 |
public Collection<TextLink> getLinks() { |
| 123 |
return mLinks; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
@NonNull |
| 132 |
public Bundle getExtras() { |
| 133 |
return mExtras; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
@Status |
| 153 |
public int apply( |
| 154 |
@NonNull Spannable text, |
| 155 |
@ApplyStrategy int applyStrategy, |
| 156 |
@Nullable Function<TextLink, TextLinkSpan> spanFactory) { |
| 157 |
Objects.requireNonNull(text); |
| 158 |
return new TextLinksParams.Builder() |
| 159 |
.setApplyStrategy(applyStrategy) |
| 160 |
.setSpanFactory(spanFactory) |
| 161 |
.build() |
| 162 |
.apply(text, this); |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public String toString() { |
| 167 |
return String.format(Locale.US, "TextLinks{fullText=%s, links=%s}", mFullText, mLinks); |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public int describeContents() { |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
public void writeToParcel(Parcel dest, int flags) { |
| 177 |
dest.writeString(mFullText); |
| 178 |
dest.writeTypedList(mLinks); |
| 179 |
dest.writeBundle(mExtras); |
| 180 |
} |
| 181 |
|
| 182 |
public static final @android.annotation.NonNull Parcelable.Creator<TextLinks> CREATOR = |
| 183 |
new Parcelable.Creator<TextLinks>() { |
| 184 |
@Override |
| 185 |
public TextLinks createFromParcel(Parcel in) { |
| 186 |
return new TextLinks(in); |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public TextLinks[] newArray(int size) { |
| 191 |
return new TextLinks[size]; |
| 192 |
} |
| 193 |
}; |
| 194 |
|
| 195 |
private TextLinks(Parcel in) { |
| 196 |
mFullText = in.readString(); |
| 197 |
mLinks = in.createTypedArrayList(TextLink.CREATOR); |
| 198 |
mExtras = in.readBundle(); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public static final class TextLink implements Parcelable { |
| 205 |
private final EntityConfidence mEntityScores; |
| 206 |
private final int mStart; |
| 207 |
private final int mEnd; |
| 208 |
private final Bundle mExtras; |
| 209 |
@Nullable private final URLSpan mUrlSpan; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
private TextLink(int start, int end, @NonNull EntityConfidence entityConfidence, |
| 224 |
@NonNull Bundle extras, @Nullable URLSpan urlSpan) { |
| 225 |
Objects.requireNonNull(entityConfidence); |
| 226 |
Preconditions.checkArgument(!entityConfidence.getEntities().isEmpty()); |
| 227 |
Preconditions.checkArgument(start <= end); |
| 228 |
Objects.requireNonNull(extras); |
| 229 |
mStart = start; |
| 230 |
mEnd = end; |
| 231 |
mEntityScores = entityConfidence; |
| 232 |
mUrlSpan = urlSpan; |
| 233 |
mExtras = extras; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public int getStart() { |
| 242 |
return mStart; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public int getEnd() { |
| 251 |
return mEnd; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public int getEntityCount() { |
| 260 |
return mEntityScores.getEntities().size(); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
@NonNull public @EntityType String getEntity(int index) { |
| 269 |
return mEntityScores.getEntities().get(index); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public @FloatRange(from = 0.0, to = 1.0) float getConfidenceScore( |
| 278 |
@EntityType String entityType) { |
| 279 |
return mEntityScores.getConfidenceScore(entityType); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
@NonNull |
| 286 |
public Bundle getExtras() { |
| 287 |
return mExtras; |
| 288 |
} |
| 289 |
|
| 290 |
@Override |
| 291 |
public String toString() { |
| 292 |
return String.format(Locale.US, |
| 293 |
"TextLink{start=%s, end=%s, entityScores=%s, urlSpan=%s}", |
| 294 |
mStart, mEnd, mEntityScores, mUrlSpan); |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public int describeContents() { |
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public void writeToParcel(Parcel dest, int flags) { |
| 304 |
mEntityScores.writeToParcel(dest, flags); |
| 305 |
dest.writeInt(mStart); |
| 306 |
dest.writeInt(mEnd); |
| 307 |
dest.writeBundle(mExtras); |
| 308 |
} |
| 309 |
|
| 310 |
private static TextLink readFromParcel(Parcel in) { |
| 311 |
final EntityConfidence entityConfidence = EntityConfidence.CREATOR.createFromParcel(in); |
| 312 |
final int start = in.readInt(); |
| 313 |
final int end = in.readInt(); |
| 314 |
final Bundle extras = in.readBundle(); |
| 315 |
return new TextLink(start, end, entityConfidence, extras, null ); |
| 316 |
} |
| 317 |
|
| 318 |
public static final @android.annotation.NonNull Parcelable.Creator<TextLink> CREATOR = |
| 319 |
new Parcelable.Creator<TextLink>() { |
| 320 |
@Override |
| 321 |
public TextLink createFromParcel(Parcel in) { |
| 322 |
return readFromParcel(in); |
| 323 |
} |
| 324 |
|
| 325 |
@Override |
| 326 |
public TextLink[] newArray(int size) { |
| 327 |
return new TextLink[size]; |
| 328 |
} |
| 329 |
}; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public static final class Request implements Parcelable { |
| 336 |
|
| 337 |
private final CharSequence mText; |
| 338 |
@Nullable private final LocaleList mDefaultLocales; |
| 339 |
@Nullable private final EntityConfig mEntityConfig; |
| 340 |
private final boolean mLegacyFallback; |
| 341 |
private final Bundle mExtras; |
| 342 |
@Nullable private final ZonedDateTime mReferenceTime; |
| 343 |
@Nullable private SystemTextClassifierMetadata mSystemTcMetadata; |
| 344 |
|
| 345 |
private Request( |
| 346 |
CharSequence text, |
| 347 |
LocaleList defaultLocales, |
| 348 |
EntityConfig entityConfig, |
| 349 |
boolean legacyFallback, |
| 350 |
ZonedDateTime referenceTime, |
| 351 |
Bundle extras) { |
| 352 |
mText = text; |
| 353 |
mDefaultLocales = defaultLocales; |
| 354 |
mEntityConfig = entityConfig; |
| 355 |
mLegacyFallback = legacyFallback; |
| 356 |
mReferenceTime = referenceTime; |
| 357 |
mExtras = extras; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
@NonNull |
| 364 |
public CharSequence getText() { |
| 365 |
return mText; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
@Nullable |
| 373 |
public LocaleList getDefaultLocales() { |
| 374 |
return mDefaultLocales; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
@Nullable |
| 383 |
public EntityConfig getEntityConfig() { |
| 384 |
return mEntityConfig; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public boolean isLegacyFallback() { |
| 394 |
return mLegacyFallback; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
@Nullable |
| 402 |
public ZonedDateTime getReferenceTime() { |
| 403 |
return mReferenceTime; |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
@Nullable |
| 411 |
public String getCallingPackageName() { |
| 412 |
return mSystemTcMetadata != null ? mSystemTcMetadata.getCallingPackageName() : null; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 421 |
public void setSystemTextClassifierMetadata( |
| 422 |
@Nullable SystemTextClassifierMetadata systemTcMetadata) { |
| 423 |
mSystemTcMetadata = systemTcMetadata; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
@Nullable |
| 432 |
public SystemTextClassifierMetadata getSystemTextClassifierMetadata() { |
| 433 |
return mSystemTcMetadata; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
@NonNull |
| 442 |
public Bundle getExtras() { |
| 443 |
return mExtras; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
public static final class Builder { |
| 450 |
|
| 451 |
private final CharSequence mText; |
| 452 |
|
| 453 |
@Nullable private LocaleList mDefaultLocales; |
| 454 |
@Nullable private EntityConfig mEntityConfig; |
| 455 |
private boolean mLegacyFallback = true; |
| 456 |
@Nullable private Bundle mExtras; |
| 457 |
@Nullable private ZonedDateTime mReferenceTime; |
| 458 |
|
| 459 |
public Builder(@NonNull CharSequence text) { |
| 460 |
mText = Objects.requireNonNull(text); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
@NonNull |
| 473 |
public Builder setDefaultLocales(@Nullable LocaleList defaultLocales) { |
| 474 |
mDefaultLocales = defaultLocales; |
| 475 |
return this; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
@NonNull |
| 487 |
public Builder setEntityConfig(@Nullable EntityConfig entityConfig) { |
| 488 |
mEntityConfig = entityConfig; |
| 489 |
return this; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
@NonNull |
| 502 |
public Builder setLegacyFallback(boolean legacyFallback) { |
| 503 |
mLegacyFallback = legacyFallback; |
| 504 |
return this; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
public Builder setExtras(@Nullable Bundle extras) { |
| 513 |
mExtras = extras; |
| 514 |
return this; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
@NonNull |
| 527 |
public Builder setReferenceTime(@Nullable ZonedDateTime referenceTime) { |
| 528 |
mReferenceTime = referenceTime; |
| 529 |
return this; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
@NonNull |
| 536 |
public Request build() { |
| 537 |
return new Request( |
| 538 |
mText, mDefaultLocales, mEntityConfig, |
| 539 |
mLegacyFallback, mReferenceTime, |
| 540 |
mExtras == null ? Bundle.EMPTY : mExtras); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
@Override |
| 545 |
public int describeContents() { |
| 546 |
return 0; |
| 547 |
} |
| 548 |
|
| 549 |
@Override |
| 550 |
public void writeToParcel(Parcel dest, int flags) { |
| 551 |
dest.writeString(mText.toString()); |
| 552 |
dest.writeParcelable(mDefaultLocales, flags); |
| 553 |
dest.writeParcelable(mEntityConfig, flags); |
| 554 |
dest.writeBundle(mExtras); |
| 555 |
dest.writeString(mReferenceTime == null ? null : mReferenceTime.toString()); |
| 556 |
dest.writeParcelable(mSystemTcMetadata, flags); |
| 557 |
} |
| 558 |
|
| 559 |
private static Request readFromParcel(Parcel in) { |
| 560 |
final String text = in.readString(); |
| 561 |
final LocaleList defaultLocales = in.readParcelable(null, android.os.LocaleList.class); |
| 562 |
final EntityConfig entityConfig = in.readParcelable(null, android.view.textclassifier.TextClassifier.EntityConfig.class); |
| 563 |
final Bundle extras = in.readBundle(); |
| 564 |
final String referenceTimeString = in.readString(); |
| 565 |
final ZonedDateTime referenceTime = referenceTimeString == null |
| 566 |
? null : ZonedDateTime.parse(referenceTimeString); |
| 567 |
final SystemTextClassifierMetadata systemTcMetadata = in.readParcelable(null, android.view.textclassifier.SystemTextClassifierMetadata.class); |
| 568 |
|
| 569 |
final Request request = new Request(text, defaultLocales, entityConfig, |
| 570 |
true, referenceTime, extras); |
| 571 |
request.setSystemTextClassifierMetadata(systemTcMetadata); |
| 572 |
return request; |
| 573 |
} |
| 574 |
|
| 575 |
public static final @android.annotation.NonNull Parcelable.Creator<Request> CREATOR = |
| 576 |
new Parcelable.Creator<Request>() { |
| 577 |
@Override |
| 578 |
public Request createFromParcel(Parcel in) { |
| 579 |
return readFromParcel(in); |
| 580 |
} |
| 581 |
|
| 582 |
@Override |
| 583 |
public Request[] newArray(int size) { |
| 584 |
return new Request[size]; |
| 585 |
} |
| 586 |
}; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
public static class TextLinkSpan extends ClickableSpan { |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
@Retention(RetentionPolicy.SOURCE) |
| 601 |
@IntDef({INVOCATION_METHOD_UNSPECIFIED, INVOCATION_METHOD_TOUCH, |
| 602 |
INVOCATION_METHOD_KEYBOARD}) |
| 603 |
public @interface InvocationMethod {} |
| 604 |
|
| 605 |
|
| 606 |
public static final int INVOCATION_METHOD_UNSPECIFIED = -1; |
| 607 |
|
| 608 |
public static final int INVOCATION_METHOD_TOUCH = 0; |
| 609 |
|
| 610 |
public static final int INVOCATION_METHOD_KEYBOARD = 1; |
| 611 |
|
| 612 |
private final TextLink mTextLink; |
| 613 |
|
| 614 |
public TextLinkSpan(@NonNull TextLink textLink) { |
| 615 |
mTextLink = textLink; |
| 616 |
} |
| 617 |
|
| 618 |
@Override |
| 619 |
public void onClick(View widget) { |
| 620 |
onClick(widget, INVOCATION_METHOD_UNSPECIFIED); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
public final void onClick(View widget, @InvocationMethod int invocationMethod) { |
| 625 |
if (widget instanceof TextView) { |
| 626 |
final TextView textView = (TextView) widget; |
| 627 |
final Context context = textView.getContext(); |
| 628 |
if (TextClassificationManager.getSettings(context).isSmartLinkifyEnabled()) { |
| 629 |
switch (invocationMethod) { |
| 630 |
case INVOCATION_METHOD_TOUCH: |
| 631 |
textView.requestActionMode(this); |
| 632 |
break; |
| 633 |
case INVOCATION_METHOD_KEYBOARD: |
| 634 |
case INVOCATION_METHOD_UNSPECIFIED: |
| 635 |
default: |
| 636 |
textView.handleClick(this); |
| 637 |
break; |
| 638 |
} |
| 639 |
} else { |
| 640 |
if (mTextLink.mUrlSpan != null) { |
| 641 |
mTextLink.mUrlSpan.onClick(textView); |
| 642 |
} else { |
| 643 |
textView.handleClick(this); |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
public final TextLink getTextLink() { |
| 650 |
return mTextLink; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
@VisibleForTesting(visibility = Visibility.PRIVATE) |
| 655 |
@Nullable |
| 656 |
public final String getUrl() { |
| 657 |
if (mTextLink.mUrlSpan != null) { |
| 658 |
return mTextLink.mUrlSpan.getURL(); |
| 659 |
} |
| 660 |
return null; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
public static final class Builder { |
| 668 |
private final String mFullText; |
| 669 |
private final ArrayList<TextLink> mLinks; |
| 670 |
private Bundle mExtras; |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
public Builder(@NonNull String fullText) { |
| 678 |
mFullText = Objects.requireNonNull(fullText); |
| 679 |
mLinks = new ArrayList<>(); |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
@NonNull |
| 692 |
public Builder addLink(int start, int end, @NonNull Map<String, Float> entityScores) { |
| 693 |
return addLink(start, end, entityScores, Bundle.EMPTY, null); |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
@NonNull |
| 703 |
public Builder addLink(int start, int end, @NonNull Map<String, Float> entityScores, |
| 704 |
@NonNull Bundle extras) { |
| 705 |
return addLink(start, end, entityScores, extras, null); |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
@NonNull |
| 715 |
Builder addLink(int start, int end, @NonNull Map<String, Float> entityScores, |
| 716 |
@Nullable URLSpan urlSpan) { |
| 717 |
return addLink(start, end, entityScores, Bundle.EMPTY, urlSpan); |
| 718 |
} |
| 719 |
|
| 720 |
private Builder addLink(int start, int end, @NonNull Map<String, Float> entityScores, |
| 721 |
@NonNull Bundle extras, @Nullable URLSpan urlSpan) { |
| 722 |
mLinks.add(new TextLink( |
| 723 |
start, end, new EntityConfidence(entityScores), extras, urlSpan)); |
| 724 |
return this; |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
@NonNull |
| 731 |
public Builder clearTextLinks() { |
| 732 |
mLinks.clear(); |
| 733 |
return this; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
@NonNull |
| 742 |
public Builder setExtras(@Nullable Bundle extras) { |
| 743 |
mExtras = extras; |
| 744 |
return this; |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
@NonNull |
| 753 |
public TextLinks build() { |
| 754 |
return new TextLinks(mFullText, mLinks, |
| 755 |
mExtras == null ? Bundle.EMPTY : mExtras); |
| 756 |
} |
| 757 |
} |
| 758 |
} |