1f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki/*
2f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki * Copyright (C) 2017 The Android Open Source Project
3f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki *
4f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki * Licensed under the Apache License, Version 2.0 (the "License");
5f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki * you may not use this file except in compliance with the License.
6f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki * You may obtain a copy of the License at
7f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki *
8f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki *      http://www.apache.org/licenses/LICENSE-2.0
9f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki *
10f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki * Unless required by applicable law or agreed to in writing, software
11f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki * distributed under the License is distributed on an "AS IS" BASIS,
12f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki * See the License for the specific language governing permissions and
14f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki * limitations under the License.
15f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki */
16f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki
17f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Tokipackage android.view.textclassifier;
18f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki
19c7073a4b050238457074cc5c290d77bd95bde928Abodunrinwa Tokiimport android.annotation.IntDef;
20f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Tokiimport android.annotation.IntRange;
21f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Tokiimport android.annotation.NonNull;
224cfda0beefc7f21f1818c689ca54dd11480c68f2Abodunrinwa Tokiimport android.annotation.Nullable;
23f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Tokiimport android.annotation.StringDef;
24e0b57893c68a685d287664df28467694a39e4432Abodunrinwa Tokiimport android.annotation.WorkerThread;
254cfda0beefc7f21f1818c689ca54dd11480c68f2Abodunrinwa Tokiimport android.os.LocaleList;
26d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Tokiimport android.os.Looper;
270d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althausimport android.os.Parcel;
280d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althausimport android.os.Parcelable;
296563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Tokiimport android.text.Spannable;
306563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Tokiimport android.text.SpannableString;
316563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Tokiimport android.text.style.URLSpan;
326563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Tokiimport android.text.util.Linkify;
336563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Tokiimport android.text.util.Linkify.LinkifyMask;
346563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Tokiimport android.util.ArrayMap;
35db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledleyimport android.util.ArraySet;
36f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki
374d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Tokiimport com.android.internal.util.Preconditions;
384d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki
39f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Tokiimport java.lang.annotation.Retention;
40f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Tokiimport java.lang.annotation.RetentionPolicy;
41db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledleyimport java.util.ArrayList;
42db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledleyimport java.util.Collection;
43db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledleyimport java.util.Collections;
44ab669a01a7cbb77107e4335f4940f4e39da0e2d7Richard Ledleyimport java.util.HashSet;
456563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Tokiimport java.util.Map;
46ab669a01a7cbb77107e4335f4940f4e39da0e2d7Richard Ledleyimport java.util.Set;
47f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki
48f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki/**
49f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki * Interface for providing text classification related features.
50f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki *
51d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki * <p><strong>NOTE: </strong>Unless otherwise stated, methods of this interface are blocking
52d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki * operations. Call on a worker thread.
53f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki */
54f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Tokipublic interface TextClassifier {
55f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki
56692b196cc12f6852b0bb9009c882a69b67dda4d8Abodunrinwa Toki    /** @hide */
57008f387e8344f9f98149856cd737086c14752f4dAbodunrinwa Toki    String DEFAULT_LOG_TAG = "androidtc";
58692b196cc12f6852b0bb9009c882a69b67dda4d8Abodunrinwa Toki
59c7073a4b050238457074cc5c290d77bd95bde928Abodunrinwa Toki
60c7073a4b050238457074cc5c290d77bd95bde928Abodunrinwa Toki    /** @hide */
61c7073a4b050238457074cc5c290d77bd95bde928Abodunrinwa Toki    @Retention(RetentionPolicy.SOURCE)
62c7073a4b050238457074cc5c290d77bd95bde928Abodunrinwa Toki    @IntDef(value = {LOCAL, SYSTEM})
63c7073a4b050238457074cc5c290d77bd95bde928Abodunrinwa Toki    @interface TextClassifierType {}  // TODO: Expose as system APIs.
64c7073a4b050238457074cc5c290d77bd95bde928Abodunrinwa Toki    /** Specifies a TextClassifier that runs locally in the app's process. @hide */
65c7073a4b050238457074cc5c290d77bd95bde928Abodunrinwa Toki    int LOCAL = 0;
66c7073a4b050238457074cc5c290d77bd95bde928Abodunrinwa Toki    /** Specifies a TextClassifier that runs in the system process and serves all apps. @hide */
67c7073a4b050238457074cc5c290d77bd95bde928Abodunrinwa Toki    int SYSTEM = 1;
68c7073a4b050238457074cc5c290d77bd95bde928Abodunrinwa Toki
69705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus    /** The TextClassifier failed to run. */
704d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki    String TYPE_UNKNOWN = "";
71705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus    /** The classifier ran, but didn't recognize a known entity. */
72f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki    String TYPE_OTHER = "other";
73705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus    /** E-mail address (e.g. "noreply@android.com"). */
74f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki    String TYPE_EMAIL = "email";
75705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus    /** Phone number (e.g. "555-123 456"). */
76f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki    String TYPE_PHONE = "phone";
77705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus    /** Physical address. */
78f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki    String TYPE_ADDRESS = "address";
79705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus    /** Web URL. */
809b4c82a83cc3c1aafac2325d7a601ba3e090b90bAbodunrinwa Toki    String TYPE_URL = "url";
81705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus    /** Time reference that is no more specific than a date. May be absolute such as "01/01/2000" or
82705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus     * relative like "tomorrow". **/
83705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus    String TYPE_DATE = "date";
84705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus    /** Time reference that includes a specific time. May be absolute such as "01/01/2000 5:30pm" or
85705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus     * relative like "tomorrow at 5:30pm". **/
86705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus    String TYPE_DATE_TIME = "datetime";
87705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus    /** Flight number in IATA format. */
88705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus    String TYPE_FLIGHT_NUMBER = "flight";
89f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki
9045cb3e6c1fce0792fe0222e4ae8e16eda49084adAbodunrinwa Toki    /** @hide */
91f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki    @Retention(RetentionPolicy.SOURCE)
925db9a911354271abdc13a2a645d0de7d2619010eJeff Sharkey    @StringDef(prefix = { "TYPE_" }, value = {
935db9a911354271abdc13a2a645d0de7d2619010eJeff Sharkey            TYPE_UNKNOWN,
945db9a911354271abdc13a2a645d0de7d2619010eJeff Sharkey            TYPE_OTHER,
955db9a911354271abdc13a2a645d0de7d2619010eJeff Sharkey            TYPE_EMAIL,
965db9a911354271abdc13a2a645d0de7d2619010eJeff Sharkey            TYPE_PHONE,
975db9a911354271abdc13a2a645d0de7d2619010eJeff Sharkey            TYPE_ADDRESS,
985db9a911354271abdc13a2a645d0de7d2619010eJeff Sharkey            TYPE_URL,
99705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus            TYPE_DATE,
100705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus            TYPE_DATE_TIME,
101705b9e9a1b435c7b8ab3b47d300ced487ed2262eJan Althaus            TYPE_FLIGHT_NUMBER,
102f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki    })
103f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki    @interface EntityType {}
104f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki
1051fc998b0fda051188665e599c891da4a5750581dRichard Ledley    /** Designates that the text in question is editable. **/
1061fc998b0fda051188665e599c891da4a5750581dRichard Ledley    String HINT_TEXT_IS_EDITABLE = "android.text_is_editable";
1071fc998b0fda051188665e599c891da4a5750581dRichard Ledley    /** Designates that the text in question is not editable. **/
1081fc998b0fda051188665e599c891da4a5750581dRichard Ledley    String HINT_TEXT_IS_NOT_EDITABLE = "android.text_is_not_editable";
109db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley
110db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley    /** @hide */
111db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley    @Retention(RetentionPolicy.SOURCE)
1121fc998b0fda051188665e599c891da4a5750581dRichard Ledley    @StringDef(prefix = { "HINT_" }, value = {HINT_TEXT_IS_EDITABLE, HINT_TEXT_IS_NOT_EDITABLE})
1131fc998b0fda051188665e599c891da4a5750581dRichard Ledley    @interface Hints {}
114db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley
11588be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /** @hide */
11688be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    @Retention(RetentionPolicy.SOURCE)
1174dd4314744022566af2c53cd5bb79d19faae21a0Tony Mak    @StringDef({WIDGET_TYPE_TEXTVIEW, WIDGET_TYPE_EDITTEXT, WIDGET_TYPE_UNSELECTABLE_TEXTVIEW,
1184dd4314744022566af2c53cd5bb79d19faae21a0Tony Mak            WIDGET_TYPE_WEBVIEW, WIDGET_TYPE_EDIT_WEBVIEW, WIDGET_TYPE_CUSTOM_TEXTVIEW,
1194dd4314744022566af2c53cd5bb79d19faae21a0Tony Mak            WIDGET_TYPE_CUSTOM_EDITTEXT, WIDGET_TYPE_CUSTOM_UNSELECTABLE_TEXTVIEW,
1204dd4314744022566af2c53cd5bb79d19faae21a0Tony Mak            WIDGET_TYPE_UNKNOWN})
12188be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    @interface WidgetType {}
12288be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki
12388be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /** The widget involved in the text classification session is a standard
12488be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * {@link android.widget.TextView}. */
12588be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    String WIDGET_TYPE_TEXTVIEW = "textview";
12688be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /** The widget involved in the text classification session is a standard
12788be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * {@link android.widget.EditText}. */
12888be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    String WIDGET_TYPE_EDITTEXT = "edittext";
12988be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /** The widget involved in the text classification session is a standard non-selectable
13088be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * {@link android.widget.TextView}. */
13188be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    String WIDGET_TYPE_UNSELECTABLE_TEXTVIEW = "nosel-textview";
13288be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /** The widget involved in the text classification session is a standard
13388be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * {@link android.webkit.WebView}. */
13488be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    String WIDGET_TYPE_WEBVIEW = "webview";
13588be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /** The widget involved in the text classification session is a standard editable
13688be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * {@link android.webkit.WebView}. */
13788be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    String WIDGET_TYPE_EDIT_WEBVIEW = "edit-webview";
13888be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /** The widget involved in the text classification session is a custom text widget. */
13988be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    String WIDGET_TYPE_CUSTOM_TEXTVIEW = "customview";
14088be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /** The widget involved in the text classification session is a custom editable text widget. */
14188be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    String WIDGET_TYPE_CUSTOM_EDITTEXT = "customedit";
14288be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /** The widget involved in the text classification session is a custom non-selectable text
14388be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * widget. */
14488be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    String WIDGET_TYPE_CUSTOM_UNSELECTABLE_TEXTVIEW = "nosel-customview";
14588be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /** The widget involved in the text classification session is of an unknown/unspecified type. */
14688be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    String WIDGET_TYPE_UNKNOWN = "unknown";
14788be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki
148f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki    /**
149f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki     * No-op TextClassifier.
150f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki     * This may be used to turn off TextClassifier features.
151f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki     */
1522b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki    TextClassifier NO_OP = new TextClassifier() {};
153f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki
154f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki    /**
15533ff200cbf5595f931ec38422cc0d4351990b762Abodunrinwa Toki     * Returns suggested text selection start and end indices, recognized entity types, and their
15633ff200cbf5595f931ec38422cc0d4351990b762Abodunrinwa Toki     * associated confidence scores. The entity types are ordered from highest to lowest scoring.
157f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki     *
158d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki     * <p><strong>NOTE: </strong>Call on a worker thread.
159d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki     *
160080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * <p><strong>NOTE: </strong>If a TextClassifier has been destroyed, calls to this method should
16188be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * throw an {@link IllegalStateException}. See {@link #isDestroyed()}.
16288be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     *
163080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * @param request the text selection request
164f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki     */
165e0b57893c68a685d287664df28467694a39e4432Abodunrinwa Toki    @WorkerThread
166f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki    @NonNull
167080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki    default TextSelection suggestSelection(@NonNull TextSelection.Request request) {
168080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        Preconditions.checkNotNull(request);
169080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        Utils.checkMainThread();
170080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        return new TextSelection.Builder(request.getStartIndex(), request.getEndIndex()).build();
1712b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki    }
1722b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki
1732b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki    /**
1744d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * Returns suggested text selection start and end indices, recognized entity types, and their
1754d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * associated confidence scores. The entity types are ordered from highest to lowest scoring.
1764d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *
177d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki     * <p><strong>NOTE: </strong>Call on a worker thread.
178d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki     *
179080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * <p><strong>NOTE: </strong>If a TextClassifier has been destroyed, calls to this method should
18088be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * throw an {@link IllegalStateException}. See {@link #isDestroyed()}.
18188be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     *
182080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * <p><b>NOTE:</b> Do not implement. The default implementation of this method calls
183080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * {@link #suggestSelection(TextSelection.Request)}. If that method calls this method,
184080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * a stack overflow error will happen.
185080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     *
1864d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * @param text text providing context for the selected text (which is specified
1874d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *      by the sub sequence starting at selectionStartIndex and ending at selectionEndIndex)
1884d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * @param selectionStartIndex start index of the selected part of text
1894d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * @param selectionEndIndex end index of the selected part of text
190080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * @param defaultLocales ordered list of locale preferences that may be used to
191080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     *      disambiguate the provided text. If no locale preferences exist, set this to null
192080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     *      or an empty locale list.
1934d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *
1944d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * @throws IllegalArgumentException if text is null; selectionStartIndex is negative;
1954d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *      selectionEndIndex is greater than text.length() or not greater than selectionStartIndex
1964d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *
197080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * @see #suggestSelection(TextSelection.Request)
1984d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     */
1992b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki    @WorkerThread
2002b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki    @NonNull
2012b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki    default TextSelection suggestSelection(
202f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki            @NonNull CharSequence text,
203f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki            @IntRange(from = 0) int selectionStartIndex,
2044cfda0beefc7f21f1818c689ca54dd11480c68f2Abodunrinwa Toki            @IntRange(from = 0) int selectionEndIndex,
2052b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki            @Nullable LocaleList defaultLocales) {
206080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        final TextSelection.Request request = new TextSelection.Request.Builder(
207080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki                text, selectionStartIndex, selectionEndIndex)
208080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki                .setDefaultLocales(defaultLocales)
209080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki                .build();
210080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        return suggestSelection(request);
2112b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki    }
212f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki
213ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki    // TODO: Remove once apps can build against the latest sdk.
214ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki    /** @hide */
215ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki    default TextSelection suggestSelection(
216ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki            @NonNull CharSequence text,
217ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki            @IntRange(from = 0) int selectionStartIndex,
218ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki            @IntRange(from = 0) int selectionEndIndex,
219ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki            @Nullable TextSelection.Options options) {
2205f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley        if (options == null) {
2215f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley            return suggestSelection(new TextSelection.Request.Builder(
2225f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley                    text, selectionStartIndex, selectionEndIndex).build());
2235f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley        } else if (options.getRequest() != null) {
2245f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley            return suggestSelection(options.getRequest());
2255f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley        } else {
2265f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley            return suggestSelection(
2275f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley                    new TextSelection.Request.Builder(text, selectionStartIndex, selectionEndIndex)
2285f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley                            .setDefaultLocales(options.getDefaultLocales())
2295f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley                            .build());
2305f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley        }
231ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki    }
232ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki
233f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki    /**
234e0b57893c68a685d287664df28467694a39e4432Abodunrinwa Toki     * Classifies the specified text and returns a {@link TextClassification} object that can be
235e0b57893c68a685d287664df28467694a39e4432Abodunrinwa Toki     * used to generate a widget for handling the classified text.
236f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki     *
237d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki     * <p><strong>NOTE: </strong>Call on a worker thread.
238d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki     *
23988be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * <strong>NOTE: </strong>If a TextClassifier has been destroyed, calls to this method should
24088be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * throw an {@link IllegalStateException}. See {@link #isDestroyed()}.
24188be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     *
242080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * @param request the text classification request
243f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki     */
244e0b57893c68a685d287664df28467694a39e4432Abodunrinwa Toki    @WorkerThread
245f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki    @NonNull
246080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki    default TextClassification classifyText(@NonNull TextClassification.Request request) {
247080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        Preconditions.checkNotNull(request);
248080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        Utils.checkMainThread();
2492b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki        return TextClassification.EMPTY;
2502b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki    }
2512b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki
2522b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki    /**
2534d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * Classifies the specified text and returns a {@link TextClassification} object that can be
2544d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * used to generate a widget for handling the classified text.
2554d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *
256d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki     * <p><strong>NOTE: </strong>Call on a worker thread.
257d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki     *
2584d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * <p><b>NOTE:</b> Do not implement. The default implementation of this method calls
259080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * {@link #classifyText(TextClassification.Request)}. If that method calls this method,
260080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * a stack overflow error will happen.
2614d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *
26288be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * <strong>NOTE: </strong>If a TextClassifier has been destroyed, calls to this method should
26388be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * throw an {@link IllegalStateException}. See {@link #isDestroyed()}.
26488be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     *
2654d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * @param text text providing context for the text to classify (which is specified
2664d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *      by the sub sequence starting at startIndex and ending at endIndex)
2674d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * @param startIndex start index of the text to classify
2684d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * @param endIndex end index of the text to classify
269080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * @param defaultLocales ordered list of locale preferences that may be used to
270080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     *      disambiguate the provided text. If no locale preferences exist, set this to null
271080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     *      or an empty locale list.
2724d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *
2734d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * @throws IllegalArgumentException if text is null; startIndex is negative;
2744d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *      endIndex is greater than text.length() or not greater than startIndex
2754d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *
276080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * @see #classifyText(TextClassification.Request)
2774d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     */
2782b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki    @WorkerThread
2792b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki    @NonNull
2802b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki    default TextClassification classifyText(
2814cfda0beefc7f21f1818c689ca54dd11480c68f2Abodunrinwa Toki            @NonNull CharSequence text,
2824cfda0beefc7f21f1818c689ca54dd11480c68f2Abodunrinwa Toki            @IntRange(from = 0) int startIndex,
2834cfda0beefc7f21f1818c689ca54dd11480c68f2Abodunrinwa Toki            @IntRange(from = 0) int endIndex,
2842b6020fc80dc239114ecc7ecd501d382d4883641Abodunrinwa Toki            @Nullable LocaleList defaultLocales) {
285080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        final TextClassification.Request request = new TextClassification.Request.Builder(
286080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki                text, startIndex, endIndex)
287080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki                .setDefaultLocales(defaultLocales)
288080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki                .build();
289080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        return classifyText(request);
29068d945234667d11a65a924a7cdc1f5753a41a80fRichard Ledley    }
29168d945234667d11a65a924a7cdc1f5753a41a80fRichard Ledley
292ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki    // TODO: Remove once apps can build against the latest sdk.
293ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki    /** @hide */
294ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki    default TextClassification classifyText(
295ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki            @NonNull CharSequence text,
296ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki            @IntRange(from = 0) int startIndex,
297ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki            @IntRange(from = 0) int endIndex,
298ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki            @Nullable TextClassification.Options options) {
2995f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley        if (options == null) {
3005f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley            return classifyText(
3015f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley                    new TextClassification.Request.Builder(text, startIndex, endIndex).build());
3025f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley        } else if (options.getRequest() != null) {
3035f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley            return classifyText(options.getRequest());
3045f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley        } else {
3055f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley            return classifyText(new TextClassification.Request.Builder(text, startIndex, endIndex)
3065f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley                    .setDefaultLocales(options.getDefaultLocales())
3075f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley                    .setReferenceTime(options.getReferenceTime())
3085f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley                    .build());
3095f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley        }
310ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki    }
311ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki
31268d945234667d11a65a924a7cdc1f5753a41a80fRichard Ledley    /**
313d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki     * Generates and returns a {@link TextLinks} that may be applied to the text to annotate it with
314d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki     * links information.
315d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki     *
316d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki     * <p><strong>NOTE: </strong>Call on a worker thread.
3174d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *
31888be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * <strong>NOTE: </strong>If a TextClassifier has been destroyed, calls to this method should
31988be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * throw an {@link IllegalStateException}. See {@link #isDestroyed()}.
32088be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     *
321080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * @param request the text links request
3224d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *
323108aad3c3036e61850cd39f55687626674f47d30Jan Althaus     * @see #getMaxGenerateLinksTextLength()
3244d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     */
3254d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki    @WorkerThread
32688be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    @NonNull
327080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki    default TextLinks generateLinks(@NonNull TextLinks.Request request) {
328080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        Preconditions.checkNotNull(request);
329080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        Utils.checkMainThread();
330080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        return new TextLinks.Builder(request.getText().toString()).build();
3314d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki    }
3324d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki
333ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki    // TODO: Remove once apps can build against the latest sdk.
334ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki    /** @hide */
335ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki    default TextLinks generateLinks(
336ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki            @NonNull CharSequence text, @Nullable TextLinks.Options options) {
3375f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley        if (options == null) {
3385f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley            return generateLinks(new TextLinks.Request.Builder(text).build());
3395f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley        } else if (options.getRequest() != null) {
3405f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley            return generateLinks(options.getRequest());
3415f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley        } else {
3425f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley            return generateLinks(new TextLinks.Request.Builder(text)
3435f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley                    .setDefaultLocales(options.getDefaultLocales())
3445f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley                    .setEntityConfig(options.getEntityConfig())
3455f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley                    .build());
3465f77b6a35a6de6b3c7439ef8432e5992c31dfb2bRichard Ledley        }
347ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki    }
348ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki
3494d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki    /**
350108aad3c3036e61850cd39f55687626674f47d30Jan Althaus     * Returns the maximal length of text that can be processed by generateLinks.
351108aad3c3036e61850cd39f55687626674f47d30Jan Althaus     *
35288be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * <strong>NOTE: </strong>If a TextClassifier has been destroyed, calls to this method should
35388be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * throw an {@link IllegalStateException}. See {@link #isDestroyed()}.
35488be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     *
355080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki     * @see #generateLinks(TextLinks.Request)
356108aad3c3036e61850cd39f55687626674f47d30Jan Althaus     */
357ad52f4b97c897689d0b4dbfe344229a9970136ebAbodunrinwa Toki    @WorkerThread
358108aad3c3036e61850cd39f55687626674f47d30Jan Althaus    default int getMaxGenerateLinksTextLength() {
359108aad3c3036e61850cd39f55687626674f47d30Jan Althaus        return Integer.MAX_VALUE;
360108aad3c3036e61850cd39f55687626674f47d30Jan Althaus    }
361108aad3c3036e61850cd39f55687626674f47d30Jan Althaus
362108aad3c3036e61850cd39f55687626674f47d30Jan Althaus    /**
36388be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * Reports a selection event.
36488be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     *
36588be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * <strong>NOTE: </strong>If a TextClassifier has been destroyed, calls to this method should
36688be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * throw an {@link IllegalStateException}. See {@link #isDestroyed()}.
36788be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     */
36888be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    default void onSelectionEvent(@NonNull SelectionEvent event) {}
36988be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki
37088be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /**
37188be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * Destroys this TextClassifier.
37288be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     *
37388be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * <strong>NOTE: </strong>If a TextClassifier has been destroyed, calls to its methods should
37488be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * throw an {@link IllegalStateException}. See {@link #isDestroyed()}.
37588be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     *
37688be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * <p>Subsequent calls to this method are no-ops.
37788be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     */
37888be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    default void destroy() {}
37988be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki
38088be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /**
38188be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * Returns whether or not this TextClassifier has been destroyed.
38288be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     *
38388be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * <strong>NOTE: </strong>If a TextClassifier has been destroyed, caller should not interact
38488be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * with the classifier and an attempt to do so would throw an {@link IllegalStateException}.
38588be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * However, this method should never throw an {@link IllegalStateException}.
38688be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     *
38788be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     * @see #destroy()
38888be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki     */
38988be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    default boolean isDestroyed() {
39088be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki        return false;
39188be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    }
39288be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki
39388be5a6cee59868eaee6f7b52fd8b2e6f6f28429Abodunrinwa Toki    /**
394db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley     * Configuration object for specifying what entities to identify.
395db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley     *
396db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley     * Configs are initially based on a predefined preset, and can be modified from there.
397db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley     */
3980d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus    final class EntityConfig implements Parcelable {
3991fc998b0fda051188665e599c891da4a5750581dRichard Ledley        private final Collection<String> mHints;
400db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley        private final Collection<String> mExcludedEntityTypes;
401db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley        private final Collection<String> mIncludedEntityTypes;
4021fc998b0fda051188665e599c891da4a5750581dRichard Ledley        private final boolean mUseHints;
4031fc998b0fda051188665e599c891da4a5750581dRichard Ledley
4041fc998b0fda051188665e599c891da4a5750581dRichard Ledley        private EntityConfig(boolean useHints, Collection<String> hints,
4051fc998b0fda051188665e599c891da4a5750581dRichard Ledley                Collection<String> includedEntityTypes, Collection<String> excludedEntityTypes) {
4061fc998b0fda051188665e599c891da4a5750581dRichard Ledley            mHints = hints == null
4071fc998b0fda051188665e599c891da4a5750581dRichard Ledley                    ? Collections.EMPTY_LIST
4081fc998b0fda051188665e599c891da4a5750581dRichard Ledley                    : Collections.unmodifiableCollection(new ArraySet<>(hints));
4091fc998b0fda051188665e599c891da4a5750581dRichard Ledley            mExcludedEntityTypes = excludedEntityTypes == null
4101fc998b0fda051188665e599c891da4a5750581dRichard Ledley                    ? Collections.EMPTY_LIST : new ArraySet<>(excludedEntityTypes);
4111fc998b0fda051188665e599c891da4a5750581dRichard Ledley            mIncludedEntityTypes = includedEntityTypes == null
4121fc998b0fda051188665e599c891da4a5750581dRichard Ledley                    ? Collections.EMPTY_LIST : new ArraySet<>(includedEntityTypes);
4131fc998b0fda051188665e599c891da4a5750581dRichard Ledley            mUseHints = useHints;
4141fc998b0fda051188665e599c891da4a5750581dRichard Ledley        }
415db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley
4161fc998b0fda051188665e599c891da4a5750581dRichard Ledley        /**
4171fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * Creates an EntityConfig.
4181fc998b0fda051188665e599c891da4a5750581dRichard Ledley         *
4191fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * @param hints Hints for the TextClassifier to determine what types of entities to find.
4201fc998b0fda051188665e599c891da4a5750581dRichard Ledley         */
421080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        public static EntityConfig createWithHints(@Nullable Collection<String> hints) {
4221fc998b0fda051188665e599c891da4a5750581dRichard Ledley            return new EntityConfig(/* useHints */ true, hints,
4231fc998b0fda051188665e599c891da4a5750581dRichard Ledley                    /* includedEntityTypes */null, /* excludedEntityTypes */ null);
424db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley        }
425db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley
426ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki        // TODO: Remove once apps can build against the latest sdk.
427ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki        /** @hide */
428ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki        public static EntityConfig create(@Nullable Collection<String> hints) {
429ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki            return createWithHints(hints);
430ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki        }
431ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki
432db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley        /**
4331fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * Creates an EntityConfig.
4341fc998b0fda051188665e599c891da4a5750581dRichard Ledley         *
4351fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * @param hints Hints for the TextClassifier to determine what types of entities to find
4361fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * @param includedEntityTypes Entity types, e.g. {@link #TYPE_EMAIL}, to explicitly include
4371fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * @param excludedEntityTypes Entity types, e.g. {@link #TYPE_PHONE}, to explicitly exclude
4381fc998b0fda051188665e599c891da4a5750581dRichard Ledley         *
439db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley         *
440db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley         * Note that if an entity has been excluded, the exclusion will take precedence.
441db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley         */
4421fc998b0fda051188665e599c891da4a5750581dRichard Ledley        public static EntityConfig create(@Nullable Collection<String> hints,
4431fc998b0fda051188665e599c891da4a5750581dRichard Ledley                @Nullable Collection<String> includedEntityTypes,
4441fc998b0fda051188665e599c891da4a5750581dRichard Ledley                @Nullable Collection<String> excludedEntityTypes) {
4451fc998b0fda051188665e599c891da4a5750581dRichard Ledley            return new EntityConfig(/* useHints */ true, hints,
4461fc998b0fda051188665e599c891da4a5750581dRichard Ledley                    includedEntityTypes, excludedEntityTypes);
447db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley        }
448db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley
449db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley        /**
4501fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * Creates an EntityConfig with an explicit entity list.
4511fc998b0fda051188665e599c891da4a5750581dRichard Ledley         *
4521fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * @param entityTypes Complete set of entities, e.g. {@link #TYPE_URL} to find.
4531fc998b0fda051188665e599c891da4a5750581dRichard Ledley         *
454db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley         */
455080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        public static EntityConfig createWithExplicitEntityList(
456080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki                @Nullable Collection<String> entityTypes) {
4571fc998b0fda051188665e599c891da4a5750581dRichard Ledley            return new EntityConfig(/* useHints */ false, /* hints */ null,
4581fc998b0fda051188665e599c891da4a5750581dRichard Ledley                    /* includedEntityTypes */ entityTypes, /* excludedEntityTypes */ null);
459db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley        }
460db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley
461ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki        // TODO: Remove once apps can build against the latest sdk.
462ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki        /** @hide */
463ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki        public static EntityConfig createWithEntityList(@Nullable Collection<String> entityTypes) {
464ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki            return createWithExplicitEntityList(entityTypes);
465ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki        }
466ae82e7ad280e55dca22014c6abc857372229f89cAbodunrinwa Toki
467db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley        /**
4681fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * Returns a list of the final set of entities to find.
4691fc998b0fda051188665e599c891da4a5750581dRichard Ledley         *
4701fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * @param entities Entities we think should be found before factoring in includes/excludes
4711fc998b0fda051188665e599c891da4a5750581dRichard Ledley         *
4721fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * This method is intended for use by TextClassifier implementations.
473db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley         */
474ab669a01a7cbb77107e4335f4940f4e39da0e2d7Richard Ledley        public Collection<String> resolveEntityListModifications(
475ab669a01a7cbb77107e4335f4940f4e39da0e2d7Richard Ledley                @NonNull Collection<String> entities) {
476ab669a01a7cbb77107e4335f4940f4e39da0e2d7Richard Ledley            final Set<String> finalSet = new HashSet();
4771fc998b0fda051188665e599c891da4a5750581dRichard Ledley            if (mUseHints) {
478ab669a01a7cbb77107e4335f4940f4e39da0e2d7Richard Ledley                finalSet.addAll(entities);
479db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley            }
480ab669a01a7cbb77107e4335f4940f4e39da0e2d7Richard Ledley            finalSet.addAll(mIncludedEntityTypes);
481ab669a01a7cbb77107e4335f4940f4e39da0e2d7Richard Ledley            finalSet.removeAll(mExcludedEntityTypes);
482ab669a01a7cbb77107e4335f4940f4e39da0e2d7Richard Ledley            return finalSet;
4831fc998b0fda051188665e599c891da4a5750581dRichard Ledley        }
4841fc998b0fda051188665e599c891da4a5750581dRichard Ledley
4851fc998b0fda051188665e599c891da4a5750581dRichard Ledley        /**
4861fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * Retrieves the list of hints.
4871fc998b0fda051188665e599c891da4a5750581dRichard Ledley         *
4881fc998b0fda051188665e599c891da4a5750581dRichard Ledley         * @return An unmodifiable collection of the hints.
4891fc998b0fda051188665e599c891da4a5750581dRichard Ledley         */
4901fc998b0fda051188665e599c891da4a5750581dRichard Ledley        public Collection<String> getHints() {
4911fc998b0fda051188665e599c891da4a5750581dRichard Ledley            return mHints;
492db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley        }
4930d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus
4940d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus        @Override
4950d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus        public int describeContents() {
4960d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus            return 0;
4970d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus        }
4980d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus
4990d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus        @Override
5000d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus        public void writeToParcel(Parcel dest, int flags) {
5011fc998b0fda051188665e599c891da4a5750581dRichard Ledley            dest.writeStringList(new ArrayList<>(mHints));
5020d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus            dest.writeStringList(new ArrayList<>(mExcludedEntityTypes));
5030d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus            dest.writeStringList(new ArrayList<>(mIncludedEntityTypes));
5041fc998b0fda051188665e599c891da4a5750581dRichard Ledley            dest.writeInt(mUseHints ? 1 : 0);
5050d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus        }
5060d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus
5070d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus        public static final Parcelable.Creator<EntityConfig> CREATOR =
5080d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus                new Parcelable.Creator<EntityConfig>() {
5090d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus                    @Override
5100d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus                    public EntityConfig createFromParcel(Parcel in) {
5110d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus                        return new EntityConfig(in);
5120d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus                    }
5130d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus
5140d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus                    @Override
5150d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus                    public EntityConfig[] newArray(int size) {
5160d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus                        return new EntityConfig[size];
5170d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus                    }
5180d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus                };
5190d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus
5200d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus        private EntityConfig(Parcel in) {
5211fc998b0fda051188665e599c891da4a5750581dRichard Ledley            mHints = new ArraySet<>(in.createStringArrayList());
5220d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus            mExcludedEntityTypes = new ArraySet<>(in.createStringArrayList());
5230d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus            mIncludedEntityTypes = new ArraySet<>(in.createStringArrayList());
5241fc998b0fda051188665e599c891da4a5750581dRichard Ledley            mUseHints = in.readInt() == 1;
5250d9fbb9bd15fb1c6eae171d316a2de65aaffeb48Jan Althaus        }
526db18a578f1aa2e39f88a53eab962ed6470ca2fe1Richard Ledley    }
5274d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki
5284d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki    /**
5294d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * Utility functions for TextClassifier methods.
5304d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *
5314d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * <ul>
5324d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *  <li>Provides validation of input parameters to TextClassifier methods
5334d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * </ul>
5344d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     *
5354d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * Intended to be used only in this package.
5364d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     * @hide
5374d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki     */
5384d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki    final class Utils {
5394d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki
5404d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki        /**
5414d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki         * @throws IllegalArgumentException if text is null; startIndex is negative;
5424d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki         *      endIndex is greater than text.length() or is not greater than startIndex;
5434d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki         *      options is null
5444d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki         */
545080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        static void checkArgument(@NonNull CharSequence text, int startIndex, int endIndex) {
5464d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki            Preconditions.checkArgument(text != null);
5474d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki            Preconditions.checkArgument(startIndex >= 0);
5484d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki            Preconditions.checkArgument(endIndex <= text.length());
5494d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki            Preconditions.checkArgument(endIndex > startIndex);
5504d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki        }
5514d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki
552080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        static void checkTextLength(CharSequence text, int maxLength) {
553108aad3c3036e61850cd39f55687626674f47d30Jan Althaus            Preconditions.checkArgumentInRange(text.length(), 0, maxLength, "text.length()");
554108aad3c3036e61850cd39f55687626674f47d30Jan Althaus        }
555108aad3c3036e61850cd39f55687626674f47d30Jan Althaus
5566563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki        /**
5576563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki         * Generates links using legacy {@link Linkify}.
5586563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki         */
559080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        public static TextLinks generateLegacyLinks(@NonNull TextLinks.Request request) {
560080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki            final String string = request.getText().toString();
5616563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            final TextLinks.Builder links = new TextLinks.Builder(string);
5626563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki
563ab669a01a7cbb77107e4335f4940f4e39da0e2d7Richard Ledley            final Collection<String> entities = request.getEntityConfig()
5646563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                    .resolveEntityListModifications(Collections.emptyList());
5656563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            if (entities.contains(TextClassifier.TYPE_URL)) {
5666563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                addLinks(links, string, TextClassifier.TYPE_URL);
5676563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            }
5686563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            if (entities.contains(TextClassifier.TYPE_PHONE)) {
5696563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                addLinks(links, string, TextClassifier.TYPE_PHONE);
5706563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            }
5716563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            if (entities.contains(TextClassifier.TYPE_EMAIL)) {
5726563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                addLinks(links, string, TextClassifier.TYPE_EMAIL);
5736563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            }
5746563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            // NOTE: Do not support MAP_ADDRESSES. Legacy version does not work well.
5756563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            return links.build();
5766563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki        }
5776563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki
5786563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki        private static void addLinks(
5796563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                TextLinks.Builder links, String string, @EntityType String entityType) {
5806563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            final Spannable spannable = new SpannableString(string);
5816563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            if (Linkify.addLinks(spannable, linkMask(entityType))) {
5826563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                final URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
5836563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                for (URLSpan urlSpan : spans) {
5846563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                    links.addLink(
5856563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                            spannable.getSpanStart(urlSpan),
5866563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                            spannable.getSpanEnd(urlSpan),
5876563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                            entityScores(entityType),
5886563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                            urlSpan);
5896563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                }
5906563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            }
5916563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki        }
5926563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki
5936563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki        @LinkifyMask
5946563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki        private static int linkMask(@EntityType String entityType) {
5956563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            switch (entityType) {
5966563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                case TextClassifier.TYPE_URL:
5976563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                    return Linkify.WEB_URLS;
5986563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                case TextClassifier.TYPE_PHONE:
5996563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                    return Linkify.PHONE_NUMBERS;
6006563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                case TextClassifier.TYPE_EMAIL:
6016563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                    return Linkify.EMAIL_ADDRESSES;
6026563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                default:
6036563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                    // NOTE: Do not support MAP_ADDRESSES. Legacy version does not work well.
6046563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki                    return 0;
6056563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            }
6066563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki        }
6076563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki
6086563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki        private static Map<String, Float> entityScores(@EntityType String entityType) {
6096563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            final Map<String, Float> scores = new ArrayMap<>();
6106563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            scores.put(entityType, 1f);
6116563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki            return scores;
6126563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki        }
6136563833cf3c79e8cd211e32357422ae899674437Abodunrinwa Toki
614080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki        static void checkMainThread() {
615080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki            if (Looper.myLooper() == Looper.getMainLooper()) {
616080c8542b68cf17a0441862c404cb49ce0e86cfeAbodunrinwa Toki                Log.w(DEFAULT_LOG_TAG, "TextClassifier called on main thread");
617d32906c202db3b84151c310ecd89a07bb41208f7Abodunrinwa Toki            }
6184d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki        }
6194d232d6243af42e2784664f6fab040e853f05223Abodunrinwa Toki    }
620f001fefff3745c29e2ff6903d69ad921df1ea277Abodunrinwa Toki}
621