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

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

Merge "TextClassifierImpl: Fix empty locale list issue." into oc-dev

parents 681ff5c7 a2df6e54
Loading
Loading
Loading
Loading
+10 −10
Original line number Diff line number Diff line
@@ -53,7 +53,6 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@@ -89,7 +88,7 @@ final class TextClassifierImpl implements TextClassifier {
    @Override
    public TextSelection suggestSelection(
            @NonNull CharSequence text, int selectionStartIndex, int selectionEndIndex,
            LocaleList defaultLocales) {
            @Nullable LocaleList defaultLocales) {
        validateInput(text, selectionStartIndex, selectionEndIndex);
        try {
            if (text.length() > 0) {
@@ -128,7 +127,8 @@ final class TextClassifierImpl implements TextClassifier {

    @Override
    public TextClassificationResult getTextClassificationResult(
            @NonNull CharSequence text, int startIndex, int endIndex, LocaleList defaultLocales) {
            @NonNull CharSequence text, int startIndex, int endIndex,
            @Nullable LocaleList defaultLocales) {
        validateInput(text, startIndex, endIndex);
        try {
            if (text.length() > 0) {
@@ -156,7 +156,8 @@ final class TextClassifierImpl implements TextClassifier {
    }

    @Override
    public LinksInfo getLinks(CharSequence text, int linkMask, LocaleList defaultLocales) {
    public LinksInfo getLinks(
            @NonNull CharSequence text, int linkMask, @Nullable LocaleList defaultLocales) {
        Preconditions.checkArgument(text != null);
        try {
            return LinksInfoFactory.create(
@@ -199,12 +200,11 @@ final class TextClassifierImpl implements TextClassifier {
    @GuardedBy("mSmartSelectionLock") // Do not call outside this lock.
    @Nullable
    private Locale findBestSupportedLocaleLocked(LocaleList localeList) {
        final List<Locale.LanguageRange> languageRangeList = Locale.LanguageRange.parse(
                new StringJoiner(",")
                        // Specified localeList takes priority over the system default
                        .add(localeList.toLanguageTags())
                        .add(LocaleList.getDefault().toLanguageTags())
                        .toString());
        // Specified localeList takes priority over the system default, so it is listed first.
        final String languages = localeList.isEmpty()
                ? LocaleList.getDefault().toLanguageTags()
                : localeList.toLanguageTags() + "," + LocaleList.getDefault().toLanguageTags();
        final List<Locale.LanguageRange> languageRangeList = Locale.LanguageRange.parse(languages);
        return Locale.lookup(languageRangeList, loadModelFilePathsLocked().keySet());
    }

+30 −5
Original line number Diff line number Diff line
@@ -24,11 +24,6 @@ import android.os.LocaleList;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.view.textclassifier.TextClassificationManager;
import android.view.textclassifier.TextClassificationResult;
import android.view.textclassifier.TextClassifier;
import android.view.textclassifier.TextLanguage;
import android.view.textclassifier.TextSelection;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
@@ -73,6 +68,23 @@ public class TextClassificationManagerTest {
                isTextSelection(smartStartIndex, smartEndIndex, TextClassifier.TYPE_EMAIL));
    }

    @Test
    public void testSmartSelection_nullLocaleList() {
        if (isTextClassifierDisabled()) return;

        String text = "Contact me at droid@android.com";
        String selected = "droid";
        String suggested = "droid@android.com";
        int startIndex = text.indexOf(selected);
        int endIndex = startIndex + selected.length();
        int smartStartIndex = text.indexOf(suggested);
        int smartEndIndex = smartStartIndex + suggested.length();
        LocaleList nullLocales = null;

        assertThat(mClassifier.suggestSelection(text, startIndex, endIndex, nullLocales),
                isTextSelection(smartStartIndex, smartEndIndex, TextClassifier.TYPE_EMAIL));
    }

    @Test
    public void testSmartSelection_url() {
        if (isTextClassifierDisabled()) return;
@@ -113,6 +125,19 @@ public class TextClassificationManagerTest {
                isTextClassificationResult(classifiedText, TextClassifier.TYPE_URL));
    }

    @Test
    public void testTextClassificationResult_nullLocaleList() {
        if (isTextClassifierDisabled()) return;

        String text = "Contact me at droid@android.com";
        String classifiedText = "droid@android.com";
        int startIndex = text.indexOf(classifiedText);
        int endIndex = startIndex + classifiedText.length();
        LocaleList nullLocales = null;
        assertThat(mClassifier.getTextClassificationResult(text, startIndex, endIndex, nullLocales),
                isTextClassificationResult(classifiedText, TextClassifier.TYPE_EMAIL));
    }

    @Test
    public void testLanguageDetection() {
        if (isTextClassifierDisabled()) return;