Merge "Use TextClassicationSession to call smart selection APIs." into rvc-dev am: 5630dc86ed am: ff16872e06 am: b4af13b8b7
Change-Id: Icd65d248d921a08921fc9208aa66bc510b21453d
This commit is contained in:
@@ -90,7 +90,7 @@ public final class SelectionActionModeHelper {
|
||||
mTextView = mEditor.getTextView();
|
||||
mTextClassificationHelper = new TextClassificationHelper(
|
||||
mTextView.getContext(),
|
||||
mTextView::getTextClassifier,
|
||||
mTextView::getTextClassificationSession,
|
||||
getText(mTextView),
|
||||
0, 1, mTextView.getTextLocales());
|
||||
mSelectionTracker = new SelectionTracker(mTextView);
|
||||
@@ -500,7 +500,7 @@ public final class SelectionActionModeHelper {
|
||||
selectionEnd = sortedSelectionIndices[1];
|
||||
}
|
||||
mTextClassificationHelper.init(
|
||||
mTextView::getTextClassifier,
|
||||
mTextView::getTextClassificationSession,
|
||||
getText(mTextView),
|
||||
selectionStart, selectionEnd,
|
||||
mTextView.getTextLocales());
|
||||
|
||||
@@ -12626,7 +12626,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
return getTextClassifier() == TextClassifier.NO_OP;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Starts an ActionMode for the specified TextLinkSpan.
|
||||
*
|
||||
@@ -12670,7 +12669,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
.setDefaultLocales(getTextLocales())
|
||||
.build();
|
||||
final Supplier<TextClassification> supplier = () ->
|
||||
getTextClassifier().classifyText(request);
|
||||
getTextClassificationSession().classifyText(request);
|
||||
final Consumer<TextClassification> consumer = classification -> {
|
||||
if (classification != null) {
|
||||
if (!classification.getActions().isEmpty()) {
|
||||
|
||||
@@ -120,8 +120,10 @@ public class TextViewActivityTest {
|
||||
public void setUp() {
|
||||
mActivity = mActivityRule.getActivity();
|
||||
mInstrumentation = InstrumentationRegistry.getInstrumentation();
|
||||
mActivity.getSystemService(TextClassificationManager.class)
|
||||
.setTextClassifier(TextClassifier.NO_OP);
|
||||
TextClassificationManager tcm = mActivity.getSystemService(
|
||||
TextClassificationManager.class);
|
||||
tcm.setTextClassifier(TextClassifier.NO_OP);
|
||||
tcm.setTextClassificationSessionFactory(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -1173,6 +1175,53 @@ public class TextViewActivityTest {
|
||||
assertEquals(TextClassifier.TYPE_PHONE, lastEvent.getEntityType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTextClassifierSession() throws Throwable {
|
||||
useSystemDefaultTextClassifier();
|
||||
TextClassificationManager tcm =
|
||||
mActivity.getSystemService(TextClassificationManager.class);
|
||||
List<TestableTextClassifier> testableTextClassifiers = new ArrayList<>();
|
||||
tcm.setTextClassificationSessionFactory(classificationContext -> {
|
||||
TestableTextClassifier textClassifier = new TestableTextClassifier();
|
||||
testableTextClassifiers.add(textClassifier);
|
||||
return new TextClassifier() {
|
||||
private boolean mIsDestroyed = false;
|
||||
|
||||
@Override
|
||||
public TextSelection suggestSelection(TextSelection.Request request) {
|
||||
return textClassifier.suggestSelection(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
mIsDestroyed = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDestroyed() {
|
||||
return mIsDestroyed;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
// Long press to trigger selection
|
||||
onView(withId(R.id.textview)).perform(replaceText("android.com"));
|
||||
onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(0));
|
||||
sleepForFloatingToolbarPopup();
|
||||
// Click "Copy" to dismiss the selection.
|
||||
clickFloatingToolbarItem(mActivity.getString(com.android.internal.R.string.copy));
|
||||
|
||||
// Long press to trigger another selection
|
||||
onView(withId(R.id.textview)).perform(replaceText("android@android.com"));
|
||||
onView(withId(R.id.textview)).perform(longPressOnTextAtIndex(0));
|
||||
sleepForFloatingToolbarPopup();
|
||||
|
||||
// suggestSelection should be called in two different TextClassifier sessions.
|
||||
assertEquals(2, testableTextClassifiers.size());
|
||||
assertEquals(1, testableTextClassifiers.get(0).getTextSelectionRequests().size());
|
||||
assertEquals(1, testableTextClassifiers.get(1).getTextSelectionRequests().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPastePlainText_menuAction() {
|
||||
initializeClipboardWithText(TextStyle.STYLED);
|
||||
@@ -1227,6 +1276,7 @@ public class TextViewActivityTest {
|
||||
|
||||
private final class TestableTextClassifier implements TextClassifier {
|
||||
final List<SelectionEvent> mSelectionEvents = new ArrayList<>();
|
||||
final List<TextSelection.Request> mTextSelectionRequests = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void onSelectionEvent(SelectionEvent event) {
|
||||
@@ -1235,6 +1285,7 @@ public class TextViewActivityTest {
|
||||
|
||||
@Override
|
||||
public TextSelection suggestSelection(TextSelection.Request request) {
|
||||
mTextSelectionRequests.add(request);
|
||||
return new TextSelection.Builder(request.getStartIndex(), request.getEndIndex())
|
||||
.setEntityType(TextClassifier.TYPE_PHONE, 1)
|
||||
.build();
|
||||
@@ -1243,5 +1294,9 @@ public class TextViewActivityTest {
|
||||
List<SelectionEvent> getSelectionEvents() {
|
||||
return mSelectionEvents;
|
||||
}
|
||||
|
||||
List<TextSelection.Request> getTextSelectionRequests() {
|
||||
return mTextSelectionRequests;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user