| 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.IntDef; |
| 20 |
import android.annotation.IntRange; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.StringDef; |
| 24 |
import android.annotation.WorkerThread; |
| 25 |
import android.os.LocaleList; |
| 26 |
import android.os.Looper; |
| 27 |
import android.os.Parcel; |
| 28 |
import android.os.Parcelable; |
| 29 |
import android.text.Spannable; |
| 30 |
import android.text.SpannableString; |
| 31 |
import android.text.style.URLSpan; |
| 32 |
import android.text.util.Linkify; |
| 33 |
import android.text.util.Linkify.LinkifyMask; |
| 34 |
import android.util.ArrayMap; |
| 35 |
|
| 36 |
import com.android.internal.annotations.GuardedBy; |
| 37 |
import com.android.internal.util.IndentingPrintWriter; |
| 38 |
import com.android.internal.util.Preconditions; |
| 39 |
|
| 40 |
import java.lang.annotation.Retention; |
| 41 |
import java.lang.annotation.RetentionPolicy; |
| 42 |
import java.text.BreakIterator; |
| 43 |
import java.util.ArrayList; |
| 44 |
import java.util.Collection; |
| 45 |
import java.util.Collections; |
| 46 |
import java.util.HashSet; |
| 47 |
import java.util.List; |
| 48 |
import java.util.Map; |
| 49 |
import java.util.Objects; |
| 50 |
import java.util.Set; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public interface TextClassifier { |
| 62 |
|
| 63 |
|
| 64 |
String LOG_TAG = "androidtc"; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@Retention(RetentionPolicy.SOURCE) |
| 69 |
@IntDef(value = {LOCAL, SYSTEM, DEFAULT_SYSTEM}) |
| 70 |
@interface TextClassifierType {} |
| 71 |
|
| 72 |
int LOCAL = 0; |
| 73 |
|
| 74 |
int SYSTEM = 1; |
| 75 |
|
| 76 |
int DEFAULT_SYSTEM = 2; |
| 77 |
|
| 78 |
|
| 79 |
static String typeToString(@TextClassifierType int type) { |
| 80 |
switch (type) { |
| 81 |
case LOCAL: |
| 82 |
return "Local"; |
| 83 |
case SYSTEM: |
| 84 |
return "System"; |
| 85 |
case DEFAULT_SYSTEM: |
| 86 |
return "Default system"; |
| 87 |
} |
| 88 |
return "Unknown"; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
String TYPE_UNKNOWN = ""; |
| 93 |
|
| 94 |
String TYPE_OTHER = "other"; |
| 95 |
|
| 96 |
String TYPE_EMAIL = "email"; |
| 97 |
|
| 98 |
String TYPE_PHONE = "phone"; |
| 99 |
|
| 100 |
String TYPE_ADDRESS = "address"; |
| 101 |
|
| 102 |
String TYPE_URL = "url"; |
| 103 |
|
| 104 |
|
| 105 |
String TYPE_DATE = "date"; |
| 106 |
|
| 107 |
|
| 108 |
String TYPE_DATE_TIME = "datetime"; |
| 109 |
|
| 110 |
String TYPE_FLIGHT_NUMBER = "flight"; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
String TYPE_DICTIONARY = "dictionary"; |
| 116 |
|
| 117 |
|
| 118 |
@Retention(RetentionPolicy.SOURCE) |
| 119 |
@StringDef(prefix = { "TYPE_" }, value = { |
| 120 |
TYPE_UNKNOWN, |
| 121 |
TYPE_OTHER, |
| 122 |
TYPE_EMAIL, |
| 123 |
TYPE_PHONE, |
| 124 |
TYPE_ADDRESS, |
| 125 |
TYPE_URL, |
| 126 |
TYPE_DATE, |
| 127 |
TYPE_DATE_TIME, |
| 128 |
TYPE_FLIGHT_NUMBER, |
| 129 |
TYPE_DICTIONARY |
| 130 |
}) |
| 131 |
@interface EntityType {} |
| 132 |
|
| 133 |
|
| 134 |
String HINT_TEXT_IS_EDITABLE = "android.text_is_editable"; |
| 135 |
|
| 136 |
String HINT_TEXT_IS_NOT_EDITABLE = "android.text_is_not_editable"; |
| 137 |
|
| 138 |
|
| 139 |
@Retention(RetentionPolicy.SOURCE) |
| 140 |
@StringDef(prefix = { "HINT_" }, value = {HINT_TEXT_IS_EDITABLE, HINT_TEXT_IS_NOT_EDITABLE}) |
| 141 |
@interface Hints {} |
| 142 |
|
| 143 |
|
| 144 |
@Retention(RetentionPolicy.SOURCE) |
| 145 |
@StringDef({WIDGET_TYPE_TEXTVIEW, WIDGET_TYPE_EDITTEXT, WIDGET_TYPE_UNSELECTABLE_TEXTVIEW, |
| 146 |
WIDGET_TYPE_WEBVIEW, WIDGET_TYPE_EDIT_WEBVIEW, WIDGET_TYPE_CUSTOM_TEXTVIEW, |
| 147 |
WIDGET_TYPE_CUSTOM_EDITTEXT, WIDGET_TYPE_CUSTOM_UNSELECTABLE_TEXTVIEW, |
| 148 |
WIDGET_TYPE_NOTIFICATION, WIDGET_TYPE_CLIPBOARD, WIDGET_TYPE_UNKNOWN }) |
| 149 |
@interface WidgetType {} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
String WIDGET_TYPE_TEXTVIEW = "textview"; |
| 154 |
|
| 155 |
|
| 156 |
String WIDGET_TYPE_EDITTEXT = "edittext"; |
| 157 |
|
| 158 |
|
| 159 |
String WIDGET_TYPE_UNSELECTABLE_TEXTVIEW = "nosel-textview"; |
| 160 |
|
| 161 |
|
| 162 |
String WIDGET_TYPE_WEBVIEW = "webview"; |
| 163 |
|
| 164 |
|
| 165 |
String WIDGET_TYPE_EDIT_WEBVIEW = "edit-webview"; |
| 166 |
|
| 167 |
String WIDGET_TYPE_CUSTOM_TEXTVIEW = "customview"; |
| 168 |
|
| 169 |
String WIDGET_TYPE_CUSTOM_EDITTEXT = "customedit"; |
| 170 |
|
| 171 |
|
| 172 |
String WIDGET_TYPE_CUSTOM_UNSELECTABLE_TEXTVIEW = "nosel-customview"; |
| 173 |
|
| 174 |
String WIDGET_TYPE_NOTIFICATION = "notification"; |
| 175 |
|
| 176 |
String WIDGET_TYPE_CLIPBOARD = "clipboard"; |
| 177 |
|
| 178 |
String WIDGET_TYPE_UNKNOWN = "unknown"; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
TextClassifier NO_OP = new TextClassifier() { |
| 185 |
@Override |
| 186 |
public String toString() { |
| 187 |
return "TextClassifier.NO_OP"; |
| 188 |
} |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
String EXTRA_FROM_TEXT_CLASSIFIER = "android.view.textclassifier.extra.FROM_TEXT_CLASSIFIER"; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
@WorkerThread |
| 212 |
@NonNull |
| 213 |
default TextSelection suggestSelection(@NonNull TextSelection.Request request) { |
| 214 |
Objects.requireNonNull(request); |
| 215 |
Utils.checkMainThread(); |
| 216 |
return new TextSelection.Builder(request.getStartIndex(), request.getEndIndex()).build(); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
@WorkerThread |
| 246 |
@NonNull |
| 247 |
default TextSelection suggestSelection( |
| 248 |
@NonNull CharSequence text, |
| 249 |
@IntRange(from = 0) int selectionStartIndex, |
| 250 |
@IntRange(from = 0) int selectionEndIndex, |
| 251 |
@Nullable LocaleList defaultLocales) { |
| 252 |
final TextSelection.Request request = new TextSelection.Request.Builder( |
| 253 |
text, selectionStartIndex, selectionEndIndex) |
| 254 |
.setDefaultLocales(defaultLocales) |
| 255 |
.build(); |
| 256 |
return suggestSelection(request); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
@WorkerThread |
| 271 |
@NonNull |
| 272 |
default TextClassification classifyText(@NonNull TextClassification.Request request) { |
| 273 |
Objects.requireNonNull(request); |
| 274 |
Utils.checkMainThread(); |
| 275 |
return TextClassification.EMPTY; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
@WorkerThread |
| 305 |
@NonNull |
| 306 |
default TextClassification classifyText( |
| 307 |
@NonNull CharSequence text, |
| 308 |
@IntRange(from = 0) int startIndex, |
| 309 |
@IntRange(from = 0) int endIndex, |
| 310 |
@Nullable LocaleList defaultLocales) { |
| 311 |
final TextClassification.Request request = new TextClassification.Request.Builder( |
| 312 |
text, startIndex, endIndex) |
| 313 |
.setDefaultLocales(defaultLocales) |
| 314 |
.build(); |
| 315 |
return classifyText(request); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
@WorkerThread |
| 332 |
@NonNull |
| 333 |
default TextLinks generateLinks(@NonNull TextLinks.Request request) { |
| 334 |
Objects.requireNonNull(request); |
| 335 |
Utils.checkMainThread(); |
| 336 |
return new TextLinks.Builder(request.getText().toString()).build(); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
@WorkerThread |
| 348 |
default int getMaxGenerateLinksTextLength() { |
| 349 |
return Integer.MAX_VALUE; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
@WorkerThread |
| 365 |
@NonNull |
| 366 |
default TextLanguage detectLanguage(@NonNull TextLanguage.Request request) { |
| 367 |
Objects.requireNonNull(request); |
| 368 |
Utils.checkMainThread(); |
| 369 |
return TextLanguage.EMPTY; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
@WorkerThread |
| 376 |
@NonNull |
| 377 |
default ConversationActions suggestConversationActions( |
| 378 |
@NonNull ConversationActions.Request request) { |
| 379 |
Objects.requireNonNull(request); |
| 380 |
Utils.checkMainThread(); |
| 381 |
return new ConversationActions(Collections.emptyList(), null); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
default void onSelectionEvent(@NonNull SelectionEvent event) { |
| 393 |
|
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
default void onTextClassifierEvent(@NonNull TextClassifierEvent event) {} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
default void destroy() {} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
default boolean isDestroyed() { |
| 426 |
return false; |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
default void dump(@NonNull IndentingPrintWriter printWriter) {} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
final class EntityConfig implements Parcelable { |
| 438 |
private final List<String> mIncludedTypes; |
| 439 |
private final List<String> mExcludedTypes; |
| 440 |
private final List<String> mHints; |
| 441 |
private final boolean mIncludeTypesFromTextClassifier; |
| 442 |
|
| 443 |
private EntityConfig( |
| 444 |
List<String> includedEntityTypes, |
| 445 |
List<String> excludedEntityTypes, |
| 446 |
List<String> hints, |
| 447 |
boolean includeTypesFromTextClassifier) { |
| 448 |
mIncludedTypes = Objects.requireNonNull(includedEntityTypes); |
| 449 |
mExcludedTypes = Objects.requireNonNull(excludedEntityTypes); |
| 450 |
mHints = Objects.requireNonNull(hints); |
| 451 |
mIncludeTypesFromTextClassifier = includeTypesFromTextClassifier; |
| 452 |
} |
| 453 |
|
| 454 |
private EntityConfig(Parcel in) { |
| 455 |
mIncludedTypes = new ArrayList<>(); |
| 456 |
in.readStringList(mIncludedTypes); |
| 457 |
mExcludedTypes = new ArrayList<>(); |
| 458 |
in.readStringList(mExcludedTypes); |
| 459 |
List<String> tmpHints = new ArrayList<>(); |
| 460 |
in.readStringList(tmpHints); |
| 461 |
mHints = Collections.unmodifiableList(tmpHints); |
| 462 |
mIncludeTypesFromTextClassifier = in.readByte() != 0; |
| 463 |
} |
| 464 |
|
| 465 |
@Override |
| 466 |
public void writeToParcel(Parcel parcel, int flags) { |
| 467 |
parcel.writeStringList(mIncludedTypes); |
| 468 |
parcel.writeStringList(mExcludedTypes); |
| 469 |
parcel.writeStringList(mHints); |
| 470 |
parcel.writeByte((byte) (mIncludeTypesFromTextClassifier ? 1 : 0)); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
@Deprecated |
| 481 |
public static EntityConfig createWithHints(@Nullable Collection<String> hints) { |
| 482 |
return new EntityConfig.Builder() |
| 483 |
.includeTypesFromTextClassifier(true) |
| 484 |
.setHints(hints) |
| 485 |
.build(); |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
@Deprecated |
| 501 |
public static EntityConfig create(@Nullable Collection<String> hints, |
| 502 |
@Nullable Collection<String> includedEntityTypes, |
| 503 |
@Nullable Collection<String> excludedEntityTypes) { |
| 504 |
return new EntityConfig.Builder() |
| 505 |
.setIncludedTypes(includedEntityTypes) |
| 506 |
.setExcludedTypes(excludedEntityTypes) |
| 507 |
.setHints(hints) |
| 508 |
.includeTypesFromTextClassifier(true) |
| 509 |
.build(); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
@Deprecated |
| 520 |
public static EntityConfig createWithExplicitEntityList( |
| 521 |
@Nullable Collection<String> entityTypes) { |
| 522 |
return new EntityConfig.Builder() |
| 523 |
.setIncludedTypes(entityTypes) |
| 524 |
.includeTypesFromTextClassifier(false) |
| 525 |
.build(); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
public Collection<String> resolveEntityListModifications( |
| 537 |
@NonNull Collection<String> entityTypes) { |
| 538 |
final Set<String> finalSet = new HashSet<>(); |
| 539 |
if (mIncludeTypesFromTextClassifier) { |
| 540 |
finalSet.addAll(entityTypes); |
| 541 |
} |
| 542 |
finalSet.addAll(mIncludedTypes); |
| 543 |
finalSet.removeAll(mExcludedTypes); |
| 544 |
return finalSet; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
public Collection<String> getHints() { |
| 553 |
return mHints; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
public boolean shouldIncludeTypesFromTextClassifier() { |
| 566 |
return mIncludeTypesFromTextClassifier; |
| 567 |
} |
| 568 |
|
| 569 |
@Override |
| 570 |
public int describeContents() { |
| 571 |
return 0; |
| 572 |
} |
| 573 |
|
| 574 |
public static final @android.annotation.NonNull Parcelable.Creator<EntityConfig> CREATOR = |
| 575 |
new Parcelable.Creator<EntityConfig>() { |
| 576 |
@Override |
| 577 |
public EntityConfig createFromParcel(Parcel in) { |
| 578 |
return new EntityConfig(in); |
| 579 |
} |
| 580 |
|
| 581 |
@Override |
| 582 |
public EntityConfig[] newArray(int size) { |
| 583 |
return new EntityConfig[size]; |
| 584 |
} |
| 585 |
}; |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
public static final class Builder { |
| 591 |
@Nullable |
| 592 |
private Collection<String> mIncludedTypes; |
| 593 |
@Nullable |
| 594 |
private Collection<String> mExcludedTypes; |
| 595 |
@Nullable |
| 596 |
private Collection<String> mHints; |
| 597 |
private boolean mIncludeTypesFromTextClassifier = true; |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
@NonNull |
| 603 |
public Builder setIncludedTypes(@Nullable Collection<String> includedTypes) { |
| 604 |
mIncludedTypes = includedTypes; |
| 605 |
return this; |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
@NonNull |
| 612 |
public Builder setExcludedTypes(@Nullable Collection<String> excludedTypes) { |
| 613 |
mExcludedTypes = excludedTypes; |
| 614 |
return this; |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
@NonNull |
| 622 |
public Builder includeTypesFromTextClassifier(boolean includeTypesFromTextClassifier) { |
| 623 |
mIncludeTypesFromTextClassifier = includeTypesFromTextClassifier; |
| 624 |
return this; |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
@NonNull |
| 634 |
public Builder setHints(@Nullable Collection<String> hints) { |
| 635 |
mHints = hints; |
| 636 |
return this; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
@NonNull |
| 644 |
public EntityConfig build() { |
| 645 |
return new EntityConfig( |
| 646 |
mIncludedTypes == null |
| 647 |
? Collections.emptyList() |
| 648 |
: new ArrayList<>(mIncludedTypes), |
| 649 |
mExcludedTypes == null |
| 650 |
? Collections.emptyList() |
| 651 |
: new ArrayList<>(mExcludedTypes), |
| 652 |
mHints == null |
| 653 |
? Collections.emptyList() |
| 654 |
: Collections.unmodifiableList(new ArrayList<>(mHints)), |
| 655 |
mIncludeTypesFromTextClassifier); |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
final class Utils { |
| 671 |
|
| 672 |
@GuardedBy("WORD_ITERATOR") |
| 673 |
private static final BreakIterator WORD_ITERATOR = BreakIterator.getWordInstance(); |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
static void checkArgument(@NonNull CharSequence text, int startIndex, int endIndex) { |
| 681 |
Preconditions.checkArgument(text != null); |
| 682 |
Preconditions.checkArgument(startIndex >= 0); |
| 683 |
Preconditions.checkArgument(endIndex <= text.length()); |
| 684 |
Preconditions.checkArgument(endIndex > startIndex); |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
static boolean checkTextLength(CharSequence text, int maxLength) { |
| 689 |
int textLength = text.length(); |
| 690 |
return textLength >= 0 && textLength <= maxLength; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
public static String getSubString( |
| 705 |
String text, int start, int end, int minimumLength) { |
| 706 |
Preconditions.checkArgument(start >= 0); |
| 707 |
Preconditions.checkArgument(end <= text.length()); |
| 708 |
Preconditions.checkArgument(start <= end); |
| 709 |
|
| 710 |
if (text.length() < minimumLength) { |
| 711 |
return text; |
| 712 |
} |
| 713 |
|
| 714 |
final int length = end - start; |
| 715 |
if (length >= minimumLength) { |
| 716 |
return text.substring(start, end); |
| 717 |
} |
| 718 |
|
| 719 |
final int offset = (minimumLength - length) / 2; |
| 720 |
int iterStart = Math.max(0, Math.min(start - offset, text.length() - minimumLength)); |
| 721 |
int iterEnd = Math.min(text.length(), iterStart + minimumLength); |
| 722 |
|
| 723 |
synchronized (WORD_ITERATOR) { |
| 724 |
WORD_ITERATOR.setText(text); |
| 725 |
iterStart = WORD_ITERATOR.isBoundary(iterStart) |
| 726 |
? iterStart : Math.max(0, WORD_ITERATOR.preceding(iterStart)); |
| 727 |
iterEnd = WORD_ITERATOR.isBoundary(iterEnd) |
| 728 |
? iterEnd : Math.max(iterEnd, WORD_ITERATOR.following(iterEnd)); |
| 729 |
WORD_ITERATOR.setText(""); |
| 730 |
return text.substring(iterStart, iterEnd); |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
public static TextLinks generateLegacyLinks(@NonNull TextLinks.Request request) { |
| 738 |
final String string = request.getText().toString(); |
| 739 |
final TextLinks.Builder links = new TextLinks.Builder(string); |
| 740 |
|
| 741 |
final Collection<String> entities = request.getEntityConfig() |
| 742 |
.resolveEntityListModifications(Collections.emptyList()); |
| 743 |
if (entities.contains(TextClassifier.TYPE_URL)) { |
| 744 |
addLinks(links, string, TextClassifier.TYPE_URL); |
| 745 |
} |
| 746 |
if (entities.contains(TextClassifier.TYPE_PHONE)) { |
| 747 |
addLinks(links, string, TextClassifier.TYPE_PHONE); |
| 748 |
} |
| 749 |
if (entities.contains(TextClassifier.TYPE_EMAIL)) { |
| 750 |
addLinks(links, string, TextClassifier.TYPE_EMAIL); |
| 751 |
} |
| 752 |
|
| 753 |
return links.build(); |
| 754 |
} |
| 755 |
|
| 756 |
private static void addLinks( |
| 757 |
TextLinks.Builder links, String string, @EntityType String entityType) { |
| 758 |
final Spannable spannable = new SpannableString(string); |
| 759 |
if (Linkify.addLinks(spannable, linkMask(entityType))) { |
| 760 |
final URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class); |
| 761 |
for (URLSpan urlSpan : spans) { |
| 762 |
links.addLink( |
| 763 |
spannable.getSpanStart(urlSpan), |
| 764 |
spannable.getSpanEnd(urlSpan), |
| 765 |
entityScores(entityType), |
| 766 |
urlSpan); |
| 767 |
} |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
@LinkifyMask |
| 772 |
private static int linkMask(@EntityType String entityType) { |
| 773 |
switch (entityType) { |
| 774 |
case TextClassifier.TYPE_URL: |
| 775 |
return Linkify.WEB_URLS; |
| 776 |
case TextClassifier.TYPE_PHONE: |
| 777 |
return Linkify.PHONE_NUMBERS; |
| 778 |
case TextClassifier.TYPE_EMAIL: |
| 779 |
return Linkify.EMAIL_ADDRESSES; |
| 780 |
default: |
| 781 |
|
| 782 |
return 0; |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
private static Map<String, Float> entityScores(@EntityType String entityType) { |
| 787 |
final Map<String, Float> scores = new ArrayMap<>(); |
| 788 |
scores.put(entityType, 1f); |
| 789 |
return scores; |
| 790 |
} |
| 791 |
|
| 792 |
static void checkMainThread() { |
| 793 |
if (Looper.myLooper() == Looper.getMainLooper()) { |
| 794 |
Log.w(LOG_TAG, "TextClassifier called on main thread"); |
| 795 |
} |
| 796 |
} |
| 797 |
} |
| 798 |
} |