Merge "Use TextClassicationSession to call smart selection APIs." into rvc-dev am: 5630dc86ed am: ff16872e06
Change-Id: I48d3cc272f7584c146f5bbfc3d839abf0f94ed54
This commit is contained in:
@@ -90,7 +90,7 @@ public final class SelectionActionModeHelper {
|
|||||||
mTextView = mEditor.getTextView();
|
mTextView = mEditor.getTextView();
|
||||||
mTextClassificationHelper = new TextClassificationHelper(
|
mTextClassificationHelper = new TextClassificationHelper(
|
||||||
mTextView.getContext(),
|
mTextView.getContext(),
|
||||||
mTextView::getTextClassifier,
|
mTextView::getTextClassificationSession,
|
||||||
getText(mTextView),
|
getText(mTextView),
|
||||||
0, 1, mTextView.getTextLocales());
|
0, 1, mTextView.getTextLocales());
|
||||||
mSelectionTracker = new SelectionTracker(mTextView);
|
mSelectionTracker = new SelectionTracker(mTextView);
|
||||||
@@ -500,7 +500,7 @@ public final class SelectionActionModeHelper {
|
|||||||
selectionEnd = sortedSelectionIndices[1];
|
selectionEnd = sortedSelectionIndices[1];
|
||||||
}
|
}
|
||||||
mTextClassificationHelper.init(
|
mTextClassificationHelper.init(
|
||||||
mTextView::getTextClassifier,
|
mTextView::getTextClassificationSession,
|
||||||
getText(mTextView),
|
getText(mTextView),
|
||||||
selectionStart, selectionEnd,
|
selectionStart, selectionEnd,
|
||||||
mTextView.getTextLocales());
|
mTextView.getTextLocales());
|
||||||
|
|||||||
@@ -12626,7 +12626,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
return getTextClassifier() == TextClassifier.NO_OP;
|
return getTextClassifier() == TextClassifier.NO_OP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts an ActionMode for the specified TextLinkSpan.
|
* Starts an ActionMode for the specified TextLinkSpan.
|
||||||
*
|
*
|
||||||
@@ -12670,7 +12669,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
|||||||
.setDefaultLocales(getTextLocales())
|
.setDefaultLocales(getTextLocales())
|
||||||
.build();
|
.build();
|
||||||
final Supplier<TextClassification> supplier = () ->
|
final Supplier<TextClassification> supplier = () ->
|
||||||
getTextClassifier().classifyText(request);
|
getTextClassificationSession().classifyText(request);
|
||||||
final Consumer<TextClassification> consumer = classification -> {
|
final Consumer<TextClassification> consumer = classification -> {
|
||||||
if (classification != null) {
|
if (classification != null) {
|
||||||
if (!classification.getActions().isEmpty()) {
|
if (!classification.getActions().isEmpty()) {
|
||||||
|
|||||||
@@ -120,8 +120,10 @@ public class TextViewActivityTest {
|
|||||||
public void setUp() {
|
public void setUp() {
|
||||||
mActivity = mActivityRule.getActivity();
|
mActivity = mActivityRule.getActivity();
|
||||||
mInstrumentation = InstrumentationRegistry.getInstrumentation();
|
mInstrumentation = InstrumentationRegistry.getInstrumentation();
|
||||||
mActivity.getSystemService(TextClassificationManager.class)
|
TextClassificationManager tcm = mActivity.getSystemService(
|
||||||
.setTextClassifier(TextClassifier.NO_OP);
|
TextClassificationManager.class);
|
||||||
|
tcm.setTextClassifier(TextClassifier.NO_OP);
|
||||||
|
tcm.setTextClassificationSessionFactory(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -1173,6 +1175,53 @@ public class TextViewActivityTest {
|
|||||||
assertEquals(TextClassifier.TYPE_PHONE, lastEvent.getEntityType());
|
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
|
@Test
|
||||||
public void testPastePlainText_menuAction() {
|
public void testPastePlainText_menuAction() {
|
||||||
initializeClipboardWithText(TextStyle.STYLED);
|
initializeClipboardWithText(TextStyle.STYLED);
|
||||||
@@ -1227,6 +1276,7 @@ public class TextViewActivityTest {
|
|||||||
|
|
||||||
private final class TestableTextClassifier implements TextClassifier {
|
private final class TestableTextClassifier implements TextClassifier {
|
||||||
final List<SelectionEvent> mSelectionEvents = new ArrayList<>();
|
final List<SelectionEvent> mSelectionEvents = new ArrayList<>();
|
||||||
|
final List<TextSelection.Request> mTextSelectionRequests = new ArrayList<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSelectionEvent(SelectionEvent event) {
|
public void onSelectionEvent(SelectionEvent event) {
|
||||||
@@ -1235,6 +1285,7 @@ public class TextViewActivityTest {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TextSelection suggestSelection(TextSelection.Request request) {
|
public TextSelection suggestSelection(TextSelection.Request request) {
|
||||||
|
mTextSelectionRequests.add(request);
|
||||||
return new TextSelection.Builder(request.getStartIndex(), request.getEndIndex())
|
return new TextSelection.Builder(request.getStartIndex(), request.getEndIndex())
|
||||||
.setEntityType(TextClassifier.TYPE_PHONE, 1)
|
.setEntityType(TextClassifier.TYPE_PHONE, 1)
|
||||||
.build();
|
.build();
|
||||||
@@ -1243,5 +1294,9 @@ public class TextViewActivityTest {
|
|||||||
List<SelectionEvent> getSelectionEvents() {
|
List<SelectionEvent> getSelectionEvents() {
|
||||||
return mSelectionEvents;
|
return mSelectionEvents;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<TextSelection.Request> getTextSelectionRequests() {
|
||||||
|
return mTextSelectionRequests;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user