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

Commit 108aad3c authored by Jan Althaus's avatar Jan Althaus
Browse files

Adding maximum input size checks

Bug: 67629726
Test: Added tests and ran core tests
Change-Id: Ib6ca53b068731fa0eabcabaed230d7f3ccde1288
parent db72e3b4
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -50240,6 +50240,7 @@ package android.view.textclassifier {
    method public default android.view.textclassifier.TextLinks generateLinks(java.lang.CharSequence);
    method public default java.util.Collection<java.lang.String> getEntitiesForPreset(int);
    method public default android.view.textclassifier.logging.Logger getLogger(android.view.textclassifier.logging.Logger.Config);
    method public default int getMaxGenerateLinksTextLength();
    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);
+3 −0
Original line number Diff line number Diff line
@@ -10478,6 +10478,9 @@ public final class Settings {
         * <pre>
         * smart_selection_dark_launch              (boolean)
         * smart_selection_enabled_for_edit_text    (boolean)
         * suggest_selection_max_range_length       (int)
         * classify_text_max_range_length           (int)
         * generate_links_max_text_length           (int)
         * </pre>
         *
         * <p>
+11 −3
Original line number Diff line number Diff line
@@ -644,7 +644,13 @@ public class Linkify {
            @Nullable Runnable modifyTextView) {
        Preconditions.checkNotNull(text);
        Preconditions.checkNotNull(classifier);
        final Supplier<TextLinks> supplier = () -> classifier.generateLinks(text, options);

        // The input text may exceed the maximum length the text classifier can handle. In such
        // cases, we process the text up to the maximum length.
        final CharSequence truncatedText = text.subSequence(
                0, Math.min(text.length(), classifier.getMaxGenerateLinksTextLength()));

        final Supplier<TextLinks> supplier = () -> classifier.generateLinks(truncatedText, options);
        final Consumer<TextLinks> consumer = links -> {
            if (links.getLinks().isEmpty()) {
                if (callback != null) {
@@ -653,7 +659,8 @@ public class Linkify {
                return;
            }

            final TextLinkSpan[] old = text.getSpans(0, text.length(), TextLinkSpan.class);
            // Remove spans only for the part of the text we generated links for.
            final TextLinkSpan[] old = text.getSpans(0, truncatedText.length(), TextLinkSpan.class);
            for (int i = old.length - 1; i >= 0; i--) {
                text.removeSpan(old[i]);
            }
@@ -662,7 +669,8 @@ public class Linkify {
                    ? null : options.getSpanFactory();
            final @TextLinks.ApplyStrategy int applyStrategy = (options == null)
                    ? TextLinks.APPLY_STRATEGY_IGNORE : options.getApplyStrategy();
            final @TextLinks.Status int result =  links.apply(text, applyStrategy, spanFactory);
            final @TextLinks.Status int result = links.apply(text, applyStrategy, spanFactory,
                    true /*allowPrefix*/);
            if (result == TextLinks.STATUS_LINKS_APPLIED) {
                if (modifyTextView != null) {
                    modifyTextView.run();
+9 −0
Original line number Diff line number Diff line
@@ -121,6 +121,15 @@ final class SystemTextClassifier implements TextClassifier {
        return mFallback.generateLinks(text, options);
    }

    /**
     * @inheritDoc
     */
    @Override
    public int getMaxGenerateLinksTextLength() {
        // TODO: retrieve this from the bound service.
        return mFallback.getMaxGenerateLinksTextLength();
    }

    private static final class TextSelectionCallback extends ITextSelectionCallback.Stub {

        final ResponseReceiver<TextSelection> mReceiver = new ResponseReceiver<>();
+25 −2
Original line number Diff line number Diff line
@@ -276,9 +276,11 @@ public interface TextClassifier {
     * @param text the text to generate annotations for
     * @param options configuration for link generation
     *
     * @throws IllegalArgumentException if text is null
     * @throws IllegalArgumentException if text is null or the text is too long for the
     *      TextClassifier implementation.
     *
     * @see #generateLinks(CharSequence)
     * @see #getMaxGenerateLinksTextLength()
     */
    @WorkerThread
    default TextLinks generateLinks(
@@ -299,15 +301,27 @@ public interface TextClassifier {
     *
     * @param text the text to generate annotations for
     *
     * @throws IllegalArgumentException if text is null
     * @throws IllegalArgumentException if text is null or the text is too long for the
     *      TextClassifier implementation.
     *
     * @see #generateLinks(CharSequence, TextLinks.Options)
     * @see #getMaxGenerateLinksTextLength()
     */
    @WorkerThread
    default TextLinks generateLinks(@NonNull CharSequence text) {
        return generateLinks(text, null);
    }

    /**
     * Returns the maximal length of text that can be processed by generateLinks.
     *
     * @see #generateLinks(CharSequence)
     * @see #generateLinks(CharSequence, TextLinks.Options)
     */
    default int getMaxGenerateLinksTextLength() {
        return Integer.MAX_VALUE;
    }

    /**
     * Returns a {@link Collection} of the entity types in the specified preset.
     *
@@ -461,6 +475,15 @@ public interface TextClassifier {
            checkMainThread(allowInMainThread);
        }

        /**
         * @throws IllegalArgumentException if text is null; the text is too long or options is null
         */
        public static void validate(@NonNull CharSequence text, int maxLength,
                boolean allowInMainThread) {
            validate(text, allowInMainThread);
            Preconditions.checkArgumentInRange(text.length(), 0, maxLength, "text.length()");
        }

        private static void checkMainThread(boolean allowInMainThread) {
            if (!allowInMainThread && Looper.myLooper() == Looper.getMainLooper()) {
                Slog.w(DEFAULT_LOG_TAG, "TextClassifier called on main thread");
Loading