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 { ...@@ -141,11 +141,11 @@ public class SmartSelectionClient implements SelectionClient {
switch (callbackData) { switch (callbackData) {
case RequestType.SUGGEST_AND_CLASSIFY: case RequestType.SUGGEST_AND_CLASSIFY:
mProvider.sendSuggestAndClassifyRequest(text, start, end, null); mProvider.sendSuggestAndClassifyRequest(text, start, end);
break; break;
case RequestType.CLASSIFY: case RequestType.CLASSIFY:
mProvider.sendClassifyRequest(text, start, end, null); mProvider.sendClassifyRequest(text, start, end);
break; break;
default: default:
......
...@@ -22,7 +22,6 @@ import org.chromium.ui.base.WindowAndroid; ...@@ -22,7 +22,6 @@ import org.chromium.ui.base.WindowAndroid;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.util.Locale;
/** /**
* Controls Smart Text selection. Talks to the Android TextClassificationManager API. * Controls Smart Text selection. Talks to the Android TextClassificationManager API.
...@@ -58,13 +57,12 @@ public class SmartSelectionProvider { ...@@ -58,13 +57,12 @@ public class SmartSelectionProvider {
}; };
} }
public void sendSuggestAndClassifyRequest( public void sendSuggestAndClassifyRequest(CharSequence text, int start, int end) {
CharSequence text, int start, int end, Locale[] locales) { sendSmartSelectionRequest(RequestType.SUGGEST_AND_CLASSIFY, text, start, end);
sendSmartSelectionRequest(RequestType.SUGGEST_AND_CLASSIFY, text, start, end, locales);
} }
public void sendClassifyRequest(CharSequence text, int start, int end, Locale[] locales) { public void sendClassifyRequest(CharSequence text, int start, int end) {
sendSmartSelectionRequest(RequestType.CLASSIFY, text, start, end, locales); sendSmartSelectionRequest(RequestType.CLASSIFY, text, start, end);
} }
public void cancelAllRequests() { public void cancelAllRequests() {
...@@ -98,7 +96,7 @@ public class SmartSelectionProvider { ...@@ -98,7 +96,7 @@ public class SmartSelectionProvider {
@TargetApi(Build.VERSION_CODES.O) @TargetApi(Build.VERSION_CODES.O)
private void sendSmartSelectionRequest( 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(); TextClassifier classifier = getTextClassifier();
if (classifier == null || classifier == TextClassifier.NO_OP) { if (classifier == null || classifier == TextClassifier.NO_OP) {
mHandler.post(mFailureResponseRunnable); mHandler.post(mFailureResponseRunnable);
...@@ -110,8 +108,7 @@ public class SmartSelectionProvider { ...@@ -110,8 +108,7 @@ public class SmartSelectionProvider {
mClassificationTask = null; mClassificationTask = null;
} }
mClassificationTask = mClassificationTask = new ClassificationTask(classifier, requestType, text, start, end);
new ClassificationTask(classifier, requestType, text, start, end, locales);
mClassificationTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR); mClassificationTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
} }
...@@ -122,16 +119,14 @@ public class SmartSelectionProvider { ...@@ -122,16 +119,14 @@ public class SmartSelectionProvider {
private final CharSequence mText; private final CharSequence mText;
private final int mOriginalStart; private final int mOriginalStart;
private final int mOriginalEnd; private final int mOriginalEnd;
private final Locale[] mLocales;
ClassificationTask(TextClassifier classifier, @RequestType int requestType, ClassificationTask(TextClassifier classifier, @RequestType int requestType,
CharSequence text, int start, int end, Locale[] locales) { CharSequence text, int start, int end) {
mTextClassifier = classifier; mTextClassifier = classifier;
mRequestType = requestType; mRequestType = requestType;
mText = text; mText = text;
mOriginalStart = start; mOriginalStart = start;
mOriginalEnd = end; mOriginalEnd = end;
mLocales = locales;
} }
@Override @Override
...@@ -143,23 +138,17 @@ public class SmartSelectionProvider { ...@@ -143,23 +138,17 @@ public class SmartSelectionProvider {
if (mRequestType == RequestType.SUGGEST_AND_CLASSIFY) { if (mRequestType == RequestType.SUGGEST_AND_CLASSIFY) {
textSelection = mTextClassifier.suggestSelection( textSelection = mTextClassifier.suggestSelection(
mText, start, end, makeLocaleList(mLocales)); mText, start, end, LocaleList.getAdjustedDefault());
start = Math.max(0, textSelection.getSelectionStartIndex()); start = Math.max(0, textSelection.getSelectionStartIndex());
end = Math.min(mText.length(), textSelection.getSelectionEndIndex()); end = Math.min(mText.length(), textSelection.getSelectionEndIndex());
if (isCancelled()) return new SelectionClient.Result(); if (isCancelled()) return new SelectionClient.Result();
} }
TextClassification tc = TextClassification tc = mTextClassifier.classifyText(
mTextClassifier.classifyText(mText, start, end, makeLocaleList(mLocales)); mText, start, end, LocaleList.getAdjustedDefault());
return makeResult(start, end, tc, textSelection); 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( private SelectionClient.Result makeResult(
int start, int end, TextClassification tc, TextSelection ts) { int start, int end, TextClassification tc, TextSelection ts) {
SelectionClient.Result result = new SelectionClient.Result(); 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