Adds set text action for voice access (flutter/engine#24734)
This commit is contained in:
@@ -38,6 +38,7 @@ class SemanticsAction {
|
||||
static const int _kDismissIndex = 1 << 18;
|
||||
static const int _kMoveCursorForwardByWordIndex = 1 << 19;
|
||||
static const int _kMoveCursorBackwardByWordIndex = 1 << 20;
|
||||
static const int _kSetText = 1 << 21;
|
||||
// READ THIS: if you add an action here, you MUST update the
|
||||
// numSemanticsActions value in testing/dart/semantics_test.dart, or tests
|
||||
// will fail.
|
||||
@@ -115,6 +116,14 @@ class SemanticsAction {
|
||||
/// movement should extend (or start) a selection.
|
||||
static const SemanticsAction moveCursorBackwardByCharacter = SemanticsAction._(_kMoveCursorBackwardByCharacterIndex);
|
||||
|
||||
/// Replaces the current text in the text field.
|
||||
///
|
||||
/// This is for example used by the text editing in voice access.
|
||||
///
|
||||
/// The action includes a string argument, which is the new text to
|
||||
/// replace.
|
||||
static const SemanticsAction setText = SemanticsAction._(_kSetText);
|
||||
|
||||
/// Set the text selection to the given range.
|
||||
///
|
||||
/// The provided argument is a Map<String, int> which includes the keys `base`
|
||||
@@ -218,6 +227,7 @@ class SemanticsAction {
|
||||
_kDismissIndex: dismiss,
|
||||
_kMoveCursorForwardByWordIndex: moveCursorForwardByWord,
|
||||
_kMoveCursorBackwardByWordIndex: moveCursorBackwardByWord,
|
||||
_kSetText: setText,
|
||||
};
|
||||
|
||||
@override
|
||||
@@ -265,6 +275,8 @@ class SemanticsAction {
|
||||
return 'SemanticsAction.moveCursorForwardByWord';
|
||||
case _kMoveCursorBackwardByWordIndex:
|
||||
return 'SemanticsAction.moveCursorBackwardByWord';
|
||||
case _kSetText:
|
||||
return 'SemanticsAction.setText';
|
||||
}
|
||||
assert(false, 'Unhandled index: $index');
|
||||
return '';
|
||||
|
||||
@@ -39,6 +39,7 @@ enum class SemanticsAction : int32_t {
|
||||
kDismiss = 1 << 18,
|
||||
kMoveCursorForwardByWordIndex = 1 << 19,
|
||||
kMoveCursorBackwardByWordIndex = 1 << 20,
|
||||
kSetText = 1 << 21,
|
||||
};
|
||||
|
||||
const int kScrollableSemanticsActions =
|
||||
|
||||
@@ -29,6 +29,7 @@ class SemanticsAction {
|
||||
static const int _kDismissIndex = 1 << 18;
|
||||
static const int _kMoveCursorForwardByWordIndex = 1 << 19;
|
||||
static const int _kMoveCursorBackwardByWordIndex = 1 << 20;
|
||||
static const int _kSetText = 1 << 21;
|
||||
final int index;
|
||||
static const SemanticsAction tap = SemanticsAction._(_kTapIndex);
|
||||
static const SemanticsAction longPress = SemanticsAction._(_kLongPressIndex);
|
||||
@@ -57,6 +58,7 @@ class SemanticsAction {
|
||||
SemanticsAction._(_kMoveCursorForwardByWordIndex);
|
||||
static const SemanticsAction moveCursorBackwardByWord =
|
||||
SemanticsAction._(_kMoveCursorBackwardByWordIndex);
|
||||
static const SemanticsAction setText = SemanticsAction._(_kSetText);
|
||||
static const Map<int, SemanticsAction> values = <int, SemanticsAction>{
|
||||
_kTapIndex: tap,
|
||||
_kLongPressIndex: longPress,
|
||||
|
||||
@@ -1056,6 +1056,16 @@ public class AccessibilityBridge extends AccessibilityNodeProvider {
|
||||
accessibilityChannel.dispatchSemanticsAction(virtualViewId, Action.DISMISS);
|
||||
return true;
|
||||
}
|
||||
case AccessibilityNodeInfo.ACTION_SET_TEXT:
|
||||
{
|
||||
// Set text APIs aren't available until API 21. We can't handle the case here so
|
||||
// return false instead. It's extremely unlikely that this case would ever be
|
||||
// triggered in the first place in API < 21.
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
||||
return false;
|
||||
}
|
||||
return performSetText(virtualViewId, arguments);
|
||||
}
|
||||
default:
|
||||
// might be a custom accessibility accessibilityAction.
|
||||
final int flutterId = accessibilityAction - FIRST_RESOURCE_ID;
|
||||
@@ -1115,6 +1125,22 @@ public class AccessibilityBridge extends AccessibilityNodeProvider {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the responsibilities of {@link #performAction(int, int, Bundle)} for the specific
|
||||
* scenario of cursor movement.
|
||||
*/
|
||||
@TargetApi(21)
|
||||
@RequiresApi(21)
|
||||
private boolean performSetText(int virtualViewId, @NonNull Bundle arguments) {
|
||||
String newText = "";
|
||||
if (arguments != null
|
||||
&& arguments.containsKey(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE)) {
|
||||
newText = arguments.getString(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE);
|
||||
}
|
||||
accessibilityChannel.dispatchSemanticsAction(virtualViewId, Action.SET_TEXT, newText);
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO(ianh): implement findAccessibilityNodeInfosByText()
|
||||
|
||||
/**
|
||||
@@ -1755,7 +1781,8 @@ public class AccessibilityBridge extends AccessibilityNodeProvider {
|
||||
CUSTOM_ACTION(1 << 17),
|
||||
DISMISS(1 << 18),
|
||||
MOVE_CURSOR_FORWARD_BY_WORD(1 << 19),
|
||||
MOVE_CURSOR_BACKWARD_BY_WORD(1 << 20);
|
||||
MOVE_CURSOR_BACKWARD_BY_WORD(1 << 20),
|
||||
SET_TEXT(1 << 21);
|
||||
|
||||
public final int value;
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import android.annotation.TargetApi;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Bundle;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewParent;
|
||||
@@ -332,6 +333,45 @@ public class AccessibilityBridgeTest {
|
||||
assertEquals(sentences.get(0).toString(), "new_node2");
|
||||
}
|
||||
|
||||
@TargetApi(21)
|
||||
@Test
|
||||
public void itCanPerformSetText() {
|
||||
AccessibilityChannel mockChannel = mock(AccessibilityChannel.class);
|
||||
AccessibilityViewEmbedder mockViewEmbedder = mock(AccessibilityViewEmbedder.class);
|
||||
AccessibilityManager mockManager = mock(AccessibilityManager.class);
|
||||
View mockRootView = mock(View.class);
|
||||
Context context = mock(Context.class);
|
||||
when(mockRootView.getContext()).thenReturn(context);
|
||||
when(context.getPackageName()).thenReturn("test");
|
||||
AccessibilityBridge accessibilityBridge =
|
||||
setUpBridge(
|
||||
/*rootAccessibilityView=*/ mockRootView,
|
||||
/*accessibilityChannel=*/ mockChannel,
|
||||
/*accessibilityManager=*/ mockManager,
|
||||
/*contentResolver=*/ null,
|
||||
/*accessibilityViewEmbedder=*/ mockViewEmbedder,
|
||||
/*platformViewsAccessibilityDelegate=*/ null);
|
||||
|
||||
ViewParent mockParent = mock(ViewParent.class);
|
||||
when(mockRootView.getParent()).thenReturn(mockParent);
|
||||
when(mockManager.isEnabled()).thenReturn(true);
|
||||
|
||||
TestSemanticsNode root = new TestSemanticsNode();
|
||||
root.id = 0;
|
||||
TestSemanticsNode node1 = new TestSemanticsNode();
|
||||
node1.id = 1;
|
||||
node1.addFlag(AccessibilityBridge.Flag.IS_TEXT_FIELD);
|
||||
root.children.add(node1);
|
||||
TestSemanticsUpdate testSemanticsUpdate = root.toUpdate();
|
||||
accessibilityBridge.updateSemantics(testSemanticsUpdate.buffer, testSemanticsUpdate.strings);
|
||||
Bundle bundle = new Bundle();
|
||||
String expectedText = "some string";
|
||||
bundle.putString(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, expectedText);
|
||||
accessibilityBridge.performAction(1, AccessibilityNodeInfo.ACTION_SET_TEXT, bundle);
|
||||
verify(mockChannel)
|
||||
.dispatchSemanticsAction(1, AccessibilityBridge.Action.SET_TEXT, expectedText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void itAnnouncesWhiteSpaceWhenNoNamesRoute() {
|
||||
AccessibilityViewEmbedder mockViewEmbedder = mock(AccessibilityViewEmbedder.class);
|
||||
|
||||
@@ -20,7 +20,7 @@ void main() {
|
||||
});
|
||||
|
||||
// This must match the number of actions in lib/ui/semantics.dart
|
||||
const int numSemanticsActions = 21;
|
||||
const int numSemanticsActions = 22;
|
||||
test('SemanticsAction.values refers to all actions.', () async {
|
||||
expect(SemanticsAction.values.length, equals(numSemanticsActions));
|
||||
for (int index = 0; index < numSemanticsActions; ++index) {
|
||||
|
||||
Reference in New Issue
Block a user