Merge "TextClassifier: Introduce LocaleList in the API."
This commit is contained in:
committed by
Android (Google) Code Review
commit
eb47ae261a
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user