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

Commit adc19403 authored by Abodunrinwa Toki's avatar Abodunrinwa Toki
Browse files

Do not linkify text with RLO/LRO characters.

Also don't show smart actions for selections in text with unsupported
characters.

Bug: 116321860
Test: atest android.view.textclassifier.TextClassifierTest \
            android.text.util.cts.LinkifyTest \
	    android.text.util.LinkifyTest \
	    android.widget.TextViewActivityTest

Change-Id: Id271cab8aef6b9b13ef17f1a8654c7616f75cf13
parent 96a62ff0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -52164,6 +52164,7 @@ package android.view.textclassifier {
    field public static final int STATUS_LINKS_APPLIED = 0; // 0x0
    field public static final int STATUS_NO_LINKS_APPLIED = 2; // 0x2
    field public static final int STATUS_NO_LINKS_FOUND = 1; // 0x1
    field public static final int STATUS_UNSUPPORTED_CHARACTER = 4; // 0x4
  }
  public static final class TextLinks.Builder {
+37 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.text.style.URLSpan;
import android.util.Log;
import android.util.Patterns;
import android.webkit.WebView;
import android.widget.TextView;
@@ -72,6 +73,9 @@ import java.util.regex.Pattern;
 */

public class Linkify {

    private static final String LOG_TAG = "Linkify";

    /**
     *  Bit field indicating that web URLs should be matched in methods that
     *  take an options mask
@@ -310,6 +314,11 @@ public class Linkify {
     */
    private static boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask,
            @Nullable Context context, @Nullable UrlSpanFactory urlSpanFactory) {
        if (text != null && containsUnsupportedCharacters(text.toString())) {
            android.util.EventLog.writeEvent(0x534e4554, "116321860", -1, "");
            return false;
        }

        if (mask == 0) {
            return false;
        }
@@ -355,6 +364,29 @@ public class Linkify {
        return true;
    }

    /**
     * Returns true if the specified text contains at least one unsupported character for applying
     * links. Also logs the error.
     *
     * @param text the text to apply links to
     * @hide
     */
    public static boolean containsUnsupportedCharacters(String text) {
        if (text.contains("\u202C")) {
            Log.e(LOG_TAG, "Unsupported character for applying links: u202C");
            return true;
        }
        if (text.contains("\u202D")) {
            Log.e(LOG_TAG, "Unsupported character for applying links: u202D");
            return true;
        }
        if (text.contains("\u202E")) {
            Log.e(LOG_TAG, "Unsupported character for applying links: u202E");
            return true;
        }
        return false;
    }

    /**
     *  Scans the text of the provided TextView and turns all occurrences of
     *  the link types indicated in the mask into clickable links.  If matches
@@ -560,6 +592,11 @@ public class Linkify {
            @Nullable String defaultScheme, @Nullable String[] schemes,
            @Nullable MatchFilter matchFilter, @Nullable TransformFilter transformFilter,
            @Nullable UrlSpanFactory urlSpanFactory) {
        if (spannable != null && containsUnsupportedCharacters(spannable.toString())) {
            android.util.EventLog.writeEvent(0x534e4554, "116321860", -1, "");
            return false;
        }

        final String[] schemesCopy;
        if (defaultScheme == null) defaultScheme = "";
        if (schemes == null || schemes.length < 1) {
+1 −1
Original line number Diff line number Diff line
@@ -105,7 +105,7 @@ public final class TextClassification implements Parcelable {
    /**
     * @hide
     */
    static final TextClassification EMPTY = new TextClassification.Builder().build();
    public static final TextClassification EMPTY = new TextClassification.Builder().build();

    private static final String LOG_TAG = "TextClassification";
    // TODO(toki): investigate a way to derive this based on device properties.
+4 −1
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ public final class TextLinks implements Parcelable {
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({STATUS_LINKS_APPLIED, STATUS_NO_LINKS_FOUND, STATUS_NO_LINKS_APPLIED,
            STATUS_DIFFERENT_TEXT})
            STATUS_DIFFERENT_TEXT, STATUS_UNSUPPORTED_CHARACTER})
    public @interface Status {}

    /** Links were successfully applied to the text. */
@@ -74,6 +74,9 @@ public final class TextLinks implements Parcelable {
    /** The specified text does not match the text used to generate the links. */
    public static final int STATUS_DIFFERENT_TEXT = 3;

    /** The specified text contains unsupported characters. */
    public static final int STATUS_UNSUPPORTED_CHARACTER = 4;

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({APPLY_STRATEGY_IGNORE, APPLY_STRATEGY_REPLACE})
+7 −0
Original line number Diff line number Diff line
@@ -107,6 +107,13 @@ public final class TextLinksParams {
        Preconditions.checkNotNull(textLinks);

        final String textString = text.toString();

        if (Linkify.containsUnsupportedCharacters(textString)) {
            // Do not apply links to text containing unsupported characters.
            android.util.EventLog.writeEvent(0x534e4554, "116321860", -1, "");
            return TextLinks.STATUS_UNSUPPORTED_CHARACTER;
        }

        if (!textString.startsWith(textLinks.getText())) {
            return TextLinks.STATUS_DIFFERENT_TEXT;
        }
Loading