Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8e181791 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "TextClassifier cleanup #1"

parents b9cb8b33 4d232d62
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -49092,15 +49092,19 @@ package android.view.textclassifier {
  public abstract interface TextClassifier {
    method public default android.view.textclassifier.TextClassification classifyText(java.lang.CharSequence, int, int, android.view.textclassifier.TextClassification.Options);
    method public default android.view.textclassifier.TextClassification classifyText(java.lang.CharSequence, int, int);
    method public default android.view.textclassifier.TextClassification classifyText(java.lang.CharSequence, int, int, android.os.LocaleList);
    method public default android.view.textclassifier.TextLinks generateLinks(java.lang.CharSequence, android.view.textclassifier.TextLinks.Options);
    method public default android.view.textclassifier.TextLinks generateLinks(java.lang.CharSequence);
    method public default android.view.textclassifier.TextSelection suggestSelection(java.lang.CharSequence, int, int, android.view.textclassifier.TextSelection.Options);
    method public default android.view.textclassifier.TextSelection suggestSelection(java.lang.CharSequence, int, int);
    method public default android.view.textclassifier.TextSelection suggestSelection(java.lang.CharSequence, int, int, android.os.LocaleList);
    field public static final android.view.textclassifier.TextClassifier NO_OP;
    field public static final java.lang.String TYPE_ADDRESS = "address";
    field public static final java.lang.String TYPE_EMAIL = "email";
    field public static final java.lang.String TYPE_OTHER = "other";
    field public static final java.lang.String TYPE_PHONE = "phone";
    field public static final java.lang.String TYPE_UNKNOWN = "";
    field public static final java.lang.String TYPE_URL = "url";
  }
@@ -49116,13 +49120,9 @@ package android.view.textclassifier {
  }
  public static final class TextLinks.Options {
    ctor public TextLinks.Options();
    method public android.os.LocaleList getDefaultLocales();
  }
  public static final class TextLinks.Options.Builder {
    ctor public TextLinks.Options.Builder();
    method public android.view.textclassifier.TextLinks.Options build();
    method public android.view.textclassifier.TextLinks.Options.Builder setLocaleList(android.os.LocaleList);
    method public android.view.textclassifier.TextLinks.Options setDefaultLocales(android.os.LocaleList);
  }
  public static final class TextLinks.TextLink {
+7 −6
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import com.android.internal.util.Preconditions;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Information for generating a widget to handle classified text.
@@ -42,7 +43,7 @@ import java.util.List;
 *
 * <pre>{@code
 *   // Called preferably outside the UiThread.
 *   TextClassification classification = textClassifier.classifyText(allText, 10, 25, null);
 *   TextClassification classification = textClassifier.classifyText(allText, 10, 25);
 *
 *   // Called on the UiThread.
 *   Button button = new Button(context);
@@ -55,7 +56,7 @@ import java.util.List;
 *
 * <pre>{@code
 *   // Called preferably outside the UiThread.
 *   final TextClassification classification = textClassifier.classifyText(allText, 10, 25, null);
 *   final TextClassification classification = textClassifier.classifyText(allText, 10, 25);
 *
 *   // Called on the UiThread.
 *   view.startActionMode(new ActionMode.Callback() {
@@ -281,8 +282,8 @@ public final class TextClassification {

    @Override
    public String toString() {
        return String.format("TextClassification {"
                        + "text=%s, entities=%s, labels=%s, intents=%s}",
        return String.format(Locale.US,
                "TextClassification {text=%s, entities=%s, labels=%s, intents=%s}",
                mText, mEntityConfidence, mLabels, mIntents);
    }

@@ -421,7 +422,7 @@ public final class TextClassification {
        }

        /**
         * Ensures that we have at we have storage for the default action.
         * Ensures that we have storage for the default action.
         */
        private void ensureDefaultActionAvailable() {
            if (mIntents.isEmpty()) mIntents.add(null);
@@ -441,7 +442,7 @@ public final class TextClassification {
    }

    /**
     * TextClassification optional input parameters.
     * Optional input parameters for generating TextClassification.
     */
    public static final class Options {

+139 −7
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ import android.annotation.StringDef;
import android.annotation.WorkerThread;
import android.os.LocaleList;

import com.android.internal.util.Preconditions;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@@ -37,8 +39,7 @@ public interface TextClassifier {
    /** @hide */
    String DEFAULT_LOG_TAG = "TextClassifierImpl";

    /** @hide */
    String TYPE_UNKNOWN = "";  // TODO: Make this public API.
    String TYPE_UNKNOWN = "";
    String TYPE_OTHER = "other";
    String TYPE_EMAIL = "email";
    String TYPE_PHONE = "phone";
@@ -70,6 +71,8 @@ public interface TextClassifier {
     *
     * @throws IllegalArgumentException if text is null; selectionStartIndex is negative;
     *      selectionEndIndex is greater than text.length() or not greater than selectionStartIndex
     *
     * @see #suggestSelection(CharSequence, int, int)
     */
    @WorkerThread
    @NonNull
@@ -78,13 +81,46 @@ public interface TextClassifier {
            @IntRange(from = 0) int selectionStartIndex,
            @IntRange(from = 0) int selectionEndIndex,
            @Nullable TextSelection.Options options) {
        Utils.validateInput(text, selectionStartIndex, selectionEndIndex);
        return new TextSelection.Builder(selectionStartIndex, selectionEndIndex).build();
    }

    /**
     * Returns suggested text selection start and end indices, recognized entity types, and their
     * associated confidence scores. The entity types are ordered from highest to lowest scoring.
     *
     * <p><b>NOTE:</b> Do not implement. The default implementation of this method calls
     * {@link #suggestSelection(CharSequence, int, int, TextSelection.Options)}. If that method
     * calls this method, a stack overflow error will happen.
     *
     * @param text text providing context for the selected text (which is specified
     *      by the sub sequence starting at selectionStartIndex and ending at selectionEndIndex)
     * @param selectionStartIndex start index of the selected part of text
     * @param selectionEndIndex end index of the selected part of text
     *
     * @throws IllegalArgumentException if text is null; selectionStartIndex is negative;
     *      selectionEndIndex is greater than text.length() or not greater than selectionStartIndex
     *
     * @see #suggestSelection(CharSequence, int, int, TextSelection.Options)
     */
    // TODO: Consider deprecating (b/68846316)
    @WorkerThread
    @NonNull
    default TextSelection suggestSelection(
            @NonNull CharSequence text,
            @IntRange(from = 0) int selectionStartIndex,
            @IntRange(from = 0) int selectionEndIndex) {
        return suggestSelection(text, selectionStartIndex, selectionEndIndex,
                (TextSelection.Options) null);
    }

    /**
     * See {@link #suggestSelection(CharSequence, int, int)} or
     * {@link #suggestSelection(CharSequence, int, int, TextSelection.Options)}.
     *
     * <p><b>NOTE:</b> Do not implement. The default implementation of this method calls
     * {@link #suggestSelection(CharSequence, int, int, TextSelection.Options)}. If that method
     * calls this method, a stack overflow error will happen.
     */
    @WorkerThread
    @NonNull
    default TextSelection suggestSelection(
@@ -92,7 +128,10 @@ public interface TextClassifier {
            @IntRange(from = 0) int selectionStartIndex,
            @IntRange(from = 0) int selectionEndIndex,
            @Nullable LocaleList defaultLocales) {
        return new TextSelection.Builder(selectionStartIndex, selectionEndIndex).build();
        final TextSelection.Options options = (defaultLocales != null)
                ? new TextSelection.Options().setDefaultLocales(defaultLocales)
                : null;
        return suggestSelection(text, selectionStartIndex, selectionEndIndex, options);
    }

    /**
@@ -107,6 +146,8 @@ public interface TextClassifier {
     *
     * @throws IllegalArgumentException if text is null; startIndex is negative;
     *      endIndex is greater than text.length() or not greater than startIndex
     *
     * @see #classifyText(CharSequence, int, int)
     */
    @WorkerThread
    @NonNull
@@ -115,13 +156,45 @@ public interface TextClassifier {
            @IntRange(from = 0) int startIndex,
            @IntRange(from = 0) int endIndex,
            @Nullable TextClassification.Options options) {
        Utils.validateInput(text, startIndex, endIndex);
        return TextClassification.EMPTY;
    }

    /**
     * Classifies the specified text and returns a {@link TextClassification} object that can be
     * used to generate a widget for handling the classified text.
     *
     * <p><b>NOTE:</b> Do not implement. The default implementation of this method calls
     * {@link #classifyText(CharSequence, int, int, TextClassification.Options)}. If that method
     * calls this method, a stack overflow error will happen.
     *
     * @param text text providing context for the text to classify (which is specified
     *      by the sub sequence starting at startIndex and ending at endIndex)
     * @param startIndex start index of the text to classify
     * @param endIndex end index of the text to classify
     *
     * @throws IllegalArgumentException if text is null; startIndex is negative;
     *      endIndex is greater than text.length() or not greater than startIndex
     *
     * @see #classifyText(CharSequence, int, int, TextClassification.Options)
     */
    // TODO: Consider deprecating (b/68846316)
    @WorkerThread
    @NonNull
    default TextClassification classifyText(
            @NonNull CharSequence text,
            @IntRange(from = 0) int startIndex,
            @IntRange(from = 0) int endIndex) {
        return classifyText(text, startIndex, endIndex, (TextClassification.Options) null);
    }

    /**
     * See {@link #classifyText(CharSequence, int, int, TextClassification.Options)} or
     * {@link #classifyText(CharSequence, int, int)}.
     *
     * <p><b>NOTE:</b> Do not implement. The default implementation of this method calls
     * {@link #classifyText(CharSequence, int, int, TextClassification.Options)}. If that method
     * calls this method, a stack overflow error will happen.
     */
    @WorkerThread
    @NonNull
    default TextClassification classifyText(
@@ -129,7 +202,10 @@ public interface TextClassifier {
            @IntRange(from = 0) int startIndex,
            @IntRange(from = 0) int endIndex,
            @Nullable LocaleList defaultLocales) {
        return TextClassification.EMPTY;
        final TextClassification.Options options = (defaultLocales != null)
                ? new TextClassification.Options().setDefaultLocales(defaultLocales)
                : null;
        return classifyText(text, startIndex, endIndex, options);
    }

    /**
@@ -137,16 +213,38 @@ public interface TextClassifier {
     * information.
     *
     * @param text the text to generate annotations for
     * @param options configuration for link generation. If null, defaults will be used.
     * @param options configuration for link generation
     *
     * @throws IllegalArgumentException if text is null
     *
     * @see #generateLinks(CharSequence)
     */
    @WorkerThread
    default TextLinks generateLinks(
            @NonNull CharSequence text, @Nullable TextLinks.Options options) {
        Utils.validateInput(text);
        return new TextLinks.Builder(text.toString()).build();
    }

    /**
     * Returns a {@link TextLinks} that may be applied to the text to annotate it with links
     * information.
     *
     * <p><b>NOTE:</b> Do not implement. The default implementation of this method calls
     * {@link #generateLinks(CharSequence, TextLinks.Options)}. If that method calls this method,
     * a stack overflow error will happen.
     *
     * @param text the text to generate annotations for
     *
     * @throws IllegalArgumentException if text is null
     *
     * @see #generateLinks(CharSequence, TextLinks.Options)
     */
    @WorkerThread
    default TextLinks generateLinks(@NonNull CharSequence text) {
        return generateLinks(text, null);
    }

    /**
     * Logs a TextClassifier event.
     *
@@ -164,4 +262,38 @@ public interface TextClassifier {
    default TextClassifierConstants getSettings() {
        return TextClassifierConstants.DEFAULT;
    }


    /**
     * Utility functions for TextClassifier methods.
     *
     * <ul>
     *  <li>Provides validation of input parameters to TextClassifier methods
     * </ul>
     *
     * Intended to be used only in this package.
     * @hide
     */
    final class Utils {

        /**
         * @throws IllegalArgumentException if text is null; startIndex is negative;
         *      endIndex is greater than text.length() or is not greater than startIndex;
         *      options is null
         */
        static void validateInput(
                @NonNull CharSequence text, int startIndex, int endIndex) {
            Preconditions.checkArgument(text != null);
            Preconditions.checkArgument(startIndex >= 0);
            Preconditions.checkArgument(endIndex <= text.length());
            Preconditions.checkArgument(endIndex > startIndex);
        }

        /**
         * @throws IllegalArgumentException if text is null or options is null
         */
        static void validateInput(@NonNull CharSequence text) {
            Preconditions.checkArgument(text != null);
        }
    }
}
+6 −33
Original line number Diff line number Diff line
@@ -90,8 +90,8 @@ final class TextClassifierImpl implements TextClassifier {
    @Override
    public TextSelection suggestSelection(
            @NonNull CharSequence text, int selectionStartIndex, int selectionEndIndex,
            @Nullable TextSelection.Options options) {
        validateInput(text, selectionStartIndex, selectionEndIndex);
            @NonNull TextSelection.Options options) {
        Utils.validateInput(text, selectionStartIndex, selectionEndIndex);
        try {
            if (text.length() > 0) {
                final LocaleList locales = (options == null) ? null : options.getDefaultLocales();
@@ -140,19 +140,11 @@ final class TextClassifierImpl implements TextClassifier {
                text, selectionStartIndex, selectionEndIndex, options);
    }

    @Override
    public TextSelection suggestSelection(
            @NonNull CharSequence text, int selectionStartIndex, int selectionEndIndex,
            @Nullable LocaleList defaultLocales) {
        return suggestSelection(text, selectionStartIndex, selectionEndIndex,
                new TextSelection.Options().setDefaultLocales(defaultLocales));
    }

    @Override
    public TextClassification classifyText(
            @NonNull CharSequence text, int startIndex, int endIndex,
            @Nullable TextClassification.Options options) {
        validateInput(text, startIndex, endIndex);
            @NonNull TextClassification.Options options) {
        Utils.validateInput(text, startIndex, endIndex);
        try {
            if (text.length() > 0) {
                final String string = text.toString();
@@ -175,18 +167,10 @@ final class TextClassifierImpl implements TextClassifier {
        return TextClassifier.NO_OP.classifyText(text, startIndex, endIndex, options);
    }

    @Override
    public TextClassification classifyText(
            @NonNull CharSequence text, int startIndex, int endIndex,
            @Nullable LocaleList defaultLocales) {
        return classifyText(text, startIndex, endIndex,
                new TextClassification.Options().setDefaultLocales(defaultLocales));
    }

    @Override
    public TextLinks generateLinks(
            @NonNull CharSequence text, @Nullable TextLinks.Options options) {
        Preconditions.checkNotNull(text);
            @NonNull CharSequence text, @NonNull TextLinks.Options options) {
        Utils.validateInput(text);
        final String textString = text.toString();
        final TextLinks.Builder builder = new TextLinks.Builder(textString);
        try {
@@ -485,17 +469,6 @@ final class TextClassifierImpl implements TextClassifier {
        }
    }

    /**
     * @throws IllegalArgumentException if text is null; startIndex is negative;
     *      endIndex is greater than text.length() or is not greater than startIndex
     */
    private static void validateInput(@NonNull CharSequence text, int startIndex, int endIndex) {
        Preconditions.checkArgument(text != null);
        Preconditions.checkArgument(startIndex >= 0);
        Preconditions.checkArgument(endIndex <= text.length());
        Preconditions.checkArgument(endIndex > startIndex);
    }

    /**
     * Creates intents based on the classification type.
     */
+20 −34
Original line number Diff line number Diff line
@@ -161,39 +161,28 @@ public final class TextLinks {
     * Optional input parameters for generating TextLinks.
     */
    public static final class Options {
        private final LocaleList mLocaleList;

        private Options(LocaleList localeList) {
            this.mLocaleList = localeList;
        }
        private LocaleList mDefaultLocales;

        /**
         * Builder to construct Options.
         * @param defaultLocales ordered list of locale preferences that may be used to disambiguate
         *      the provided text. If no locale preferences exist, set this to null or an empty
         *      locale list.
         */
        public static final class Builder {
            private LocaleList mLocaleList;

            /**
             * Sets the LocaleList to use.
             *
             * @return this Builder.
             */
            public Builder setLocaleList(@Nullable LocaleList localeList) {
                this.mLocaleList = localeList;
        public Options setDefaultLocales(@Nullable LocaleList defaultLocales) {
            mDefaultLocales = defaultLocales;
            return this;
        }

        /**
             * Builds the Options object.
         * @return ordered list of locale preferences that can be used to disambiguate
         *      the provided text.
         */
            public Options build() {
                return new Options(mLocaleList);
            }
        @Nullable
        public LocaleList getDefaultLocales() {
            return mDefaultLocales;
        }
        public @Nullable LocaleList getDefaultLocales() {
            return mLocaleList;
    }
    };

    /**
     * A function to create spans from TextLinks.
@@ -204,12 +193,9 @@ public final class TextLinks {
     * @hide
     */
    public static final Function<TextLink, ClickableSpan> DEFAULT_SPAN_FACTORY =
            new Function<TextLink, ClickableSpan>() {
        @Override
        public ClickableSpan apply(TextLink textLink) {
            textLink -> {
                // TODO: Implement.
                throw new UnsupportedOperationException("Not yet implemented");
        }
            };

    /**
Loading