diff options
| author | Abodunrinwa Toki <toki@google.com> | 2018-03-28 20:58:47 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2018-03-28 20:58:47 +0000 |
| commit | 119d10deb9605502df829becc14d2a0d57a2a50a (patch) | |
| tree | e93d0ec6307b07314ea348d300fc392902761be5 /core/java/android/widget/TextView.java | |
| parent | 07150c5d75ad1c47057ad20cebf3e4f5f88c76fb (diff) | |
| parent | 88be5a6cee59868eaee6f7b52fd8b2e6f6f28429 (diff) | |
Merge "Implement Stateful TextClassifier APIs." into pi-dev
Diffstat (limited to 'core/java/android/widget/TextView.java')
| -rw-r--r-- | core/java/android/widget/TextView.java | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 39755aaa2c44..8bf497e0e90d 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -163,6 +163,7 @@ import android.view.inputmethod.ExtractedTextRequest; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodManager; import android.view.textclassifier.TextClassification; +import android.view.textclassifier.TextClassificationContext; import android.view.textclassifier.TextClassificationManager; import android.view.textclassifier.TextClassifier; import android.view.textclassifier.TextLinks; @@ -427,6 +428,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener private boolean mPreDrawListenerDetached; private TextClassifier mTextClassifier; + private TextClassifier mTextClassificationSession; // A flag to prevent repeated movements from escaping the enclosing text view. The idea here is // that if a user is holding down a movement key to traverse text, we shouldn't also traverse @@ -11527,18 +11529,63 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener @NonNull public TextClassifier getTextClassifier() { if (mTextClassifier == null) { - TextClassificationManager tcm = + final TextClassificationManager tcm = mContext.getSystemService(TextClassificationManager.class); if (tcm != null) { - mTextClassifier = tcm.getTextClassifier(); - } else { - mTextClassifier = TextClassifier.NO_OP; + return tcm.getTextClassifier(); } + return TextClassifier.NO_OP; } return mTextClassifier; } /** + * Returns a session-aware text classifier. + */ + @NonNull + TextClassifier getTextClassificationSession() { + if (mTextClassificationSession == null || mTextClassificationSession.isDestroyed()) { + final TextClassificationManager tcm = + mContext.getSystemService(TextClassificationManager.class); + if (tcm != null) { + final String widgetType; + if (isTextEditable()) { + widgetType = TextClassifier.WIDGET_TYPE_EDITTEXT; + } else if (isTextSelectable()) { + widgetType = TextClassifier.WIDGET_TYPE_TEXTVIEW; + } else { + widgetType = TextClassifier.WIDGET_TYPE_UNSELECTABLE_TEXTVIEW; + } + // TODO: Tagged this widgetType with a * so it we can monitor if it reports + // SelectionEvents exactly as the older Logger does. Remove once investigations + // are complete. + final TextClassificationContext textClassificationContext = + new TextClassificationContext.Builder( + mContext.getPackageName(), "*" + widgetType) + .build(); + if (mTextClassifier != null) { + mTextClassificationSession = tcm.createTextClassificationSession( + textClassificationContext, mTextClassifier); + } else { + mTextClassificationSession = tcm.createTextClassificationSession( + textClassificationContext); + } + } else { + mTextClassificationSession = TextClassifier.NO_OP; + } + } + return mTextClassificationSession; + } + + /** + * Returns true if this TextView uses a no-op TextClassifier. + */ + boolean usesNoOpTextClassifier() { + return getTextClassifier() == TextClassifier.NO_OP; + } + + + /** * Starts an ActionMode for the specified TextLinkSpan. * * @return Whether or not we're attempting to start the action mode. |
