Commit c49e20c5 authored by Shimi Zhang's avatar Shimi Zhang Committed by Commit Bot

[Smart Selection] Use system locale for Smart Selection

This is to match TextView's behavior, see b/122818072.

TextView passed its locale to TextSelection/TextClassification, this
locale is from an inner mTextPaint object, TextPaint is a subclass
of android.graphics.Paint.

The default locale in android.graphics.Paint is set to
LocaleList.getAdjustedDefault(). [1]

Note that there is still a gap between WebView and TextView since
TextView has API to set locale, but WebView doesn't.

[1] https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/graphics/java/android/graphics/Paint.java#491

Bug: b/122818072, 710505
Change-Id: Iaaf07656e7c852f30ac0b5ae76108583fc5105da
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1636311Reviewed-by: default avatarChangwan Ryu <changwan@chromium.org>
Reviewed-by: default avatarPedro Amaral <amaralp@chromium.org>
Commit-Queue: Shimi Zhang <ctzsm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#664866}
parent daea77af
......@@ -141,11 +141,11 @@ public class SmartSelectionClient implements SelectionClient {
switch (callbackData) {
case RequestType.SUGGEST_AND_CLASSIFY:
mProvider.sendSuggestAndClassifyRequest(text, start, end, null);
mProvider.sendSuggestAndClassifyRequest(text, start, end);
break;
case RequestType.CLASSIFY:
mProvider.sendClassifyRequest(text, start, end, null);
mProvider.sendClassifyRequest(text, start, end);
break;
default:
......
......@@ -22,7 +22,6 @@ import org.chromium.ui.base.WindowAndroid;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Locale;
/**
* Controls Smart Text selection. Talks to the Android TextClassificationManager API.
......@@ -58,13 +57,12 @@ public class SmartSelectionProvider {
};
}
public void sendSuggestAndClassifyRequest(
CharSequence text, int start, int end, Locale[] locales) {
sendSmartSelectionRequest(RequestType.SUGGEST_AND_CLASSIFY, text, start, end, locales);
public void sendSuggestAndClassifyRequest(CharSequence text, int start, int end) {
sendSmartSelectionRequest(RequestType.SUGGEST_AND_CLASSIFY, text, start, end);
}
public void sendClassifyRequest(CharSequence text, int start, int end, Locale[] locales) {
sendSmartSelectionRequest(RequestType.CLASSIFY, text, start, end, locales);
public void sendClassifyRequest(CharSequence text, int start, int end) {
sendSmartSelectionRequest(RequestType.CLASSIFY, text, start, end);
}
public void cancelAllRequests() {
......@@ -98,7 +96,7 @@ public class SmartSelectionProvider {
@TargetApi(Build.VERSION_CODES.O)
private void sendSmartSelectionRequest(
@RequestType int requestType, CharSequence text, int start, int end, Locale[] locales) {
@RequestType int requestType, CharSequence text, int start, int end) {
TextClassifier classifier = getTextClassifier();
if (classifier == null || classifier == TextClassifier.NO_OP) {
mHandler.post(mFailureResponseRunnable);
......@@ -110,8 +108,7 @@ public class SmartSelectionProvider {
mClassificationTask = null;
}
mClassificationTask =
new ClassificationTask(classifier, requestType, text, start, end, locales);
mClassificationTask = new ClassificationTask(classifier, requestType, text, start, end);
mClassificationTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
......@@ -122,16 +119,14 @@ public class SmartSelectionProvider {
private final CharSequence mText;
private final int mOriginalStart;
private final int mOriginalEnd;
private final Locale[] mLocales;
ClassificationTask(TextClassifier classifier, @RequestType int requestType,
CharSequence text, int start, int end, Locale[] locales) {
CharSequence text, int start, int end) {
mTextClassifier = classifier;
mRequestType = requestType;
mText = text;
mOriginalStart = start;
mOriginalEnd = end;
mLocales = locales;
}
@Override
......@@ -143,23 +138,17 @@ public class SmartSelectionProvider {
if (mRequestType == RequestType.SUGGEST_AND_CLASSIFY) {
textSelection = mTextClassifier.suggestSelection(
mText, start, end, makeLocaleList(mLocales));
mText, start, end, LocaleList.getAdjustedDefault());
start = Math.max(0, textSelection.getSelectionStartIndex());
end = Math.min(mText.length(), textSelection.getSelectionEndIndex());
if (isCancelled()) return new SelectionClient.Result();
}
TextClassification tc =
mTextClassifier.classifyText(mText, start, end, makeLocaleList(mLocales));
TextClassification tc = mTextClassifier.classifyText(
mText, start, end, LocaleList.getAdjustedDefault());
return makeResult(start, end, tc, textSelection);
}
@SuppressLint("NewApi")
private LocaleList makeLocaleList(Locale[] locales) {
if (locales == null || locales.length == 0) return null;
return new LocaleList(locales);
}
private SelectionClient.Result makeResult(
int start, int end, TextClassification tc, TextSelection ts) {
SelectionClient.Result result = new SelectionClient.Result();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment