diff --git a/api/current.txt b/api/current.txt index 796a13e34276a..a8b9c2982162f 100644 --- a/api/current.txt +++ b/api/current.txt @@ -47912,9 +47912,9 @@ package android.view.textclassifier { } public abstract interface TextClassifier { - method public abstract android.view.textclassifier.LinksInfo getLinks(java.lang.CharSequence, int); - method public abstract android.view.textclassifier.TextClassificationResult getTextClassificationResult(java.lang.CharSequence, int, int); - method public abstract android.view.textclassifier.TextSelection suggestSelection(java.lang.CharSequence, int, int); + method public abstract android.view.textclassifier.LinksInfo getLinks(java.lang.CharSequence, int, android.os.LocaleList); + method public abstract android.view.textclassifier.TextClassificationResult getTextClassificationResult(java.lang.CharSequence, int, int, android.os.LocaleList); + method public abstract 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"; diff --git a/api/removed.txt b/api/removed.txt index c5dbf8daf0a4a..148f3f1e3281a 100644 --- a/api/removed.txt +++ b/api/removed.txt @@ -369,6 +369,16 @@ package android.view { } +package android.view.textclassifier { + + public abstract interface TextClassifier { + method public abstract android.view.textclassifier.LinksInfo getLinks(java.lang.CharSequence, int); + method public abstract android.view.textclassifier.TextClassificationResult getTextClassificationResult(java.lang.CharSequence, int, int); + method public abstract android.view.textclassifier.TextSelection suggestSelection(java.lang.CharSequence, int, int); + } + +} + package android.webkit { public class WebViewClient { diff --git a/api/system-current.txt b/api/system-current.txt index 3499e6ab6c950..188eeae0a5183 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -51375,9 +51375,9 @@ package android.view.textclassifier { } public abstract interface TextClassifier { - method public abstract android.view.textclassifier.LinksInfo getLinks(java.lang.CharSequence, int); - method public abstract android.view.textclassifier.TextClassificationResult getTextClassificationResult(java.lang.CharSequence, int, int); - method public abstract android.view.textclassifier.TextSelection suggestSelection(java.lang.CharSequence, int, int); + method public abstract android.view.textclassifier.LinksInfo getLinks(java.lang.CharSequence, int, android.os.LocaleList); + method public abstract android.view.textclassifier.TextClassificationResult getTextClassificationResult(java.lang.CharSequence, int, int, android.os.LocaleList); + method public abstract 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"; diff --git a/api/system-removed.txt b/api/system-removed.txt index a3093e294d15a..bd535d2be5132 100644 --- a/api/system-removed.txt +++ b/api/system-removed.txt @@ -363,6 +363,16 @@ package android.view { } +package android.view.textclassifier { + + public abstract interface TextClassifier { + method public abstract android.view.textclassifier.LinksInfo getLinks(java.lang.CharSequence, int); + method public abstract android.view.textclassifier.TextClassificationResult getTextClassificationResult(java.lang.CharSequence, int, int); + method public abstract android.view.textclassifier.TextSelection suggestSelection(java.lang.CharSequence, int, int); + } + +} + package android.webkit { public class WebViewClient { diff --git a/api/test-current.txt b/api/test-current.txt index d780446f436b7..f25bf5d532199 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -48281,9 +48281,9 @@ package android.view.textclassifier { } public abstract interface TextClassifier { - method public abstract android.view.textclassifier.LinksInfo getLinks(java.lang.CharSequence, int); - method public abstract android.view.textclassifier.TextClassificationResult getTextClassificationResult(java.lang.CharSequence, int, int); - method public abstract android.view.textclassifier.TextSelection suggestSelection(java.lang.CharSequence, int, int); + method public abstract android.view.textclassifier.LinksInfo getLinks(java.lang.CharSequence, int, android.os.LocaleList); + method public abstract android.view.textclassifier.TextClassificationResult getTextClassificationResult(java.lang.CharSequence, int, int, android.os.LocaleList); + method public abstract 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"; diff --git a/api/test-removed.txt b/api/test-removed.txt index c5dbf8daf0a4a..148f3f1e3281a 100644 --- a/api/test-removed.txt +++ b/api/test-removed.txt @@ -369,6 +369,16 @@ package android.view { } +package android.view.textclassifier { + + public abstract interface TextClassifier { + method public abstract android.view.textclassifier.LinksInfo getLinks(java.lang.CharSequence, int); + method public abstract android.view.textclassifier.TextClassificationResult getTextClassificationResult(java.lang.CharSequence, int, int); + method public abstract android.view.textclassifier.TextSelection suggestSelection(java.lang.CharSequence, int, int); + } + +} + package android.webkit { public class WebViewClient { diff --git a/core/java/android/view/textclassifier/TextClassifier.java b/core/java/android/view/textclassifier/TextClassifier.java index 791543ed5f846..46f7a81e0550a 100644 --- a/core/java/android/view/textclassifier/TextClassifier.java +++ b/core/java/android/view/textclassifier/TextClassifier.java @@ -18,7 +18,9 @@ package android.view.textclassifier; import android.annotation.IntRange; import android.annotation.NonNull; +import android.annotation.Nullable; import android.annotation.StringDef; +import android.os.LocaleList; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -51,20 +53,43 @@ public interface TextClassifier { @Override public TextSelection suggestSelection( - CharSequence text, int selectionStartIndex, int selectionEndIndex) { + CharSequence text, + int selectionStartIndex, + int selectionEndIndex, + LocaleList defaultLocales) { return new TextSelection.Builder(selectionStartIndex, selectionEndIndex).build(); } @Override public TextClassificationResult getTextClassificationResult( - CharSequence text, int startIndex, int endIndex) { + CharSequence text, int startIndex, int endIndex, LocaleList defaultLocales) { return TextClassificationResult.EMPTY; } @Override - public LinksInfo getLinks(CharSequence text, int linkMask) { + public LinksInfo getLinks(CharSequence text, int linkMask, LocaleList defaultLocales) { return LinksInfo.NO_OP; } + + // TODO: Remove + @Override + public TextSelection suggestSelection( + CharSequence text, int selectionStartIndex, int selectionEndIndex) { + throw new UnsupportedOperationException("Removed"); + } + + // TODO: Remove + @Override + public TextClassificationResult getTextClassificationResult( + CharSequence text, int startIndex, int endIndex) { + throw new UnsupportedOperationException("Removed"); + } + + // TODO: Remove + @Override + public LinksInfo getLinks(CharSequence text, int linkMask) { + throw new UnsupportedOperationException("Removed"); + } }; /** @@ -75,15 +100,20 @@ public interface TextClassifier { * 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 + * @param defaultLocales ordered list of locale preferences that can be used to disambiguate + * the provided text. If no locale preferences exist, set this to null or an empty locale + * list in which case the classifier will decide whether to use no locale information, use + * a default locale, or use the system default. * * @throws IllegalArgumentException if text is null; selectionStartIndex is negative; - * selectionEndIndex is greater than text.length() or less than selectionStartIndex + * selectionEndIndex is greater than text.length() or not greater than selectionStartIndex */ @NonNull TextSelection suggestSelection( @NonNull CharSequence text, @IntRange(from = 0) int selectionStartIndex, - @IntRange(from = 0) int selectionEndIndex); + @IntRange(from = 0) int selectionEndIndex, + @Nullable LocaleList defaultLocales); /** * Returns a {@link TextClassificationResult} object that can be used to generate a widget for @@ -93,13 +123,20 @@ public interface TextClassifier { * 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 + * @param defaultLocales ordered list of locale preferences that can be used to disambiguate + * the provided text. If no locale preferences exist, set this to null or an empty locale + * list in which case the classifier will decide whether to use no locale information, use + * a default locale, or use the system default. * * @throws IllegalArgumentException if text is null; startIndex is negative; - * endIndex is greater than text.length() or less than startIndex + * endIndex is greater than text.length() or not greater than startIndex */ @NonNull TextClassificationResult getTextClassificationResult( - @NonNull CharSequence text, int startIndex, int endIndex); + @NonNull CharSequence text, + @IntRange(from = 0) int startIndex, + @IntRange(from = 0) int endIndex, + @Nullable LocaleList defaultLocales); /** * Returns a {@link LinksInfo} that may be applied to the text to annotate it with links @@ -108,8 +145,25 @@ public interface TextClassifier { * @param text the text to generate annotations for * @param linkMask See {@link android.text.util.Linkify} for a list of linkMasks that may be * specified. Subclasses of this interface may specify additional linkMasks + * @param defaultLocales ordered list of locale preferences that can be used to disambiguate + * the provided text. If no locale preferences exist, set this to null or an empty locale + * list in which case the classifier will decide whether to use no locale information, use + * a default locale, or use the system default. * * @throws IllegalArgumentException if text is null */ - LinksInfo getLinks(@NonNull CharSequence text, int linkMask); + LinksInfo getLinks( + @NonNull CharSequence text, int linkMask, @Nullable LocaleList defaultLocales); + + // TODO: Remove + /** @removed */ + TextSelection suggestSelection( + CharSequence text, int selectionStartIndex, int selectionEndIndex); + // TODO: Remove + /** @removed */ + TextClassificationResult getTextClassificationResult( + CharSequence text, int startIndex, int endIndex); + // TODO: Remove + /** @removed */ + LinksInfo getLinks(CharSequence text, int linkMask); } diff --git a/core/java/android/view/textclassifier/TextClassifierImpl.java b/core/java/android/view/textclassifier/TextClassifierImpl.java index 536d7e0880212..0486f9f8d09f2 100644 --- a/core/java/android/view/textclassifier/TextClassifierImpl.java +++ b/core/java/android/view/textclassifier/TextClassifierImpl.java @@ -26,6 +26,7 @@ import android.content.pm.ResolveInfo; import android.graphics.drawable.Drawable; import android.icu.text.BreakIterator; import android.net.Uri; +import android.os.LocaleList; import android.os.ParcelFileDescriptor; import android.provider.Browser; import android.text.Spannable; @@ -74,7 +75,8 @@ final class TextClassifierImpl implements TextClassifier { @Override public TextSelection suggestSelection( - @NonNull CharSequence text, int selectionStartIndex, int selectionEndIndex) { + @NonNull CharSequence text, int selectionStartIndex, int selectionEndIndex, + LocaleList defaultLocales) { validateInput(text, selectionStartIndex, selectionEndIndex); try { if (text.length() > 0) { @@ -101,12 +103,12 @@ final class TextClassifierImpl implements TextClassifier { } // Getting here means something went wrong, return a NO_OP result. return TextClassifier.NO_OP.suggestSelection( - text, selectionStartIndex, selectionEndIndex); + text, selectionStartIndex, selectionEndIndex, defaultLocales); } @Override public TextClassificationResult getTextClassificationResult( - @NonNull CharSequence text, int startIndex, int endIndex) { + @NonNull CharSequence text, int startIndex, int endIndex, LocaleList defaultLocales) { validateInput(text, startIndex, endIndex); try { if (text.length() > 0) { @@ -125,11 +127,12 @@ final class TextClassifierImpl implements TextClassifier { Log.e(LOG_TAG, "Error getting assist info.", t); } // Getting here means something went wrong, return a NO_OP result. - return TextClassifier.NO_OP.getTextClassificationResult(text, startIndex, endIndex); + return TextClassifier.NO_OP.getTextClassificationResult( + text, startIndex, endIndex, defaultLocales); } @Override - public LinksInfo getLinks(CharSequence text, int linkMask) { + public LinksInfo getLinks(CharSequence text, int linkMask, LocaleList defaultLocales) { Preconditions.checkArgument(text != null); try { return LinksInfoFactory.create( @@ -139,7 +142,27 @@ final class TextClassifierImpl implements TextClassifier { Log.e(LOG_TAG, "Error getting links info.", t); } // Getting here means something went wrong, return a NO_OP result. - return TextClassifier.NO_OP.getLinks(text, linkMask); + return TextClassifier.NO_OP.getLinks(text, linkMask, defaultLocales); + } + + // TODO: Remove + @Override + public TextSelection suggestSelection( + CharSequence text, int selectionStartIndex, int selectionEndIndex) { + throw new UnsupportedOperationException("Removed"); + } + + // TODO: Remove + @Override + public TextClassificationResult getTextClassificationResult( + CharSequence text, int startIndex, int endIndex) { + throw new UnsupportedOperationException("Removed"); + } + + // TODO: Remove + @Override + public LinksInfo getLinks(CharSequence text, int linkMask) { + throw new UnsupportedOperationException("Removed"); } private SmartSelection getSmartSelection() throws FileNotFoundException { @@ -195,7 +218,7 @@ final class TextClassifierImpl implements TextClassifier { /** * @throws IllegalArgumentException if text is null; startIndex is negative; - * endIndex is greater than text.length() or less than startIndex + * 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); diff --git a/core/java/android/widget/SelectionActionModeHelper.java b/core/java/android/widget/SelectionActionModeHelper.java index b75193536f322..a03238374f39e 100644 --- a/core/java/android/widget/SelectionActionModeHelper.java +++ b/core/java/android/widget/SelectionActionModeHelper.java @@ -21,6 +21,7 @@ import android.annotation.Nullable; import android.annotation.UiThread; import android.annotation.WorkerThread; import android.os.AsyncTask; +import android.os.LocaleList; import android.text.Selection; import android.text.Spannable; import android.text.TextUtils; @@ -60,7 +61,7 @@ final class SelectionActionModeHelper { mEditor = Preconditions.checkNotNull(editor); final TextView textView = mEditor.getTextView(); mTextClassificationHelper = new TextClassificationHelper( - textView.getTextClassifier(), textView.getText(), 0, 1); + textView.getTextClassifier(), textView.getText(), 0, 1, textView.getTextLocales()); } public void startActionModeAsync() { @@ -170,7 +171,8 @@ final class SelectionActionModeHelper { private void resetTextClassificationHelper() { final TextView textView = mEditor.getTextView(); mTextClassificationHelper.reset(textView.getTextClassifier(), textView.getText(), - textView.getSelectionStart(), textView.getSelectionEnd()); + textView.getSelectionStart(), textView.getSelectionEnd(), + textView.getTextLocales()); } /** @@ -297,6 +299,7 @@ final class SelectionActionModeHelper { private int mSelectionStart; /** End index relative to mText. */ private int mSelectionEnd; + private LocaleList mLocales; /** Trimmed text starting from mTrimStart in mText. */ private CharSequence mTrimmedText; @@ -308,18 +311,19 @@ final class SelectionActionModeHelper { private int mRelativeEnd; TextClassificationHelper(TextClassifier textClassifier, - CharSequence text, int selectionStart, int selectionEnd) { - reset(textClassifier, text, selectionStart, selectionEnd); + CharSequence text, int selectionStart, int selectionEnd, LocaleList locales) { + reset(textClassifier, text, selectionStart, selectionEnd, locales); } @UiThread public void reset(TextClassifier textClassifier, - CharSequence text, int selectionStart, int selectionEnd) { + CharSequence text, int selectionStart, int selectionEnd, LocaleList locales) { mTextClassifier = Preconditions.checkNotNull(textClassifier); mText = Preconditions.checkNotNull(text).toString(); Preconditions.checkArgument(selectionEnd > selectionStart); mSelectionStart = selectionStart; mSelectionEnd = selectionEnd; + mLocales = locales; } @WorkerThread @@ -329,14 +333,14 @@ final class SelectionActionModeHelper { mSelectionStart, mSelectionEnd, mTextClassifier.getTextClassificationResult( - mTrimmedText, mRelativeStart, mRelativeEnd)); + mTrimmedText, mRelativeStart, mRelativeEnd, mLocales)); } @WorkerThread public SelectionResult suggestSelection() { trimText(); final TextSelection sel = mTextClassifier.suggestSelection( - mTrimmedText, mRelativeStart, mRelativeEnd); + mTrimmedText, mRelativeStart, mRelativeEnd, mLocales); mSelectionStart = Math.max(0, sel.getSelectionStartIndex() + mTrimStart); mSelectionEnd = Math.min(mText.length(), sel.getSelectionEndIndex() + mTrimStart); return classifyText();