Java Code Examples for androidx.core.view.accessibility.AccessibilityNodeInfoCompat#getTextSelectionEnd()

The following examples show how to use androidx.core.view.accessibility.AccessibilityNodeInfoCompat#getTextSelectionEnd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextEditingUtils.java    From talkback with Apache License 2.0 6 votes vote down vote up
/**
 * Moves the cursor forward or backward in the text by one sentence in the given {@link
 * AccessibilityNodeInfoCompat}. The movement direction depends on the given actionId.
 *
 * @param nodeCompat The {@link AccessibilityNodeInfoCompat} in which to move the cursor
 * @param direction A {@link MovementDirection} indicating the movement direction
 * @return {@code true} if this movement is successful
 */
public static boolean moveCursorBySentenceGranularity(
    AccessibilityNodeInfoCompat nodeCompat, MovementDirection direction) {
  CharSequence text = nodeCompat.getText();
  int currentCursorPosition = nodeCompat.getTextSelectionEnd();
  int newCursorPosition = 0;
  switch (direction) {
    case DIRECTION_PREVIOUS:
      newCursorPosition = getEndOfLastSentenceBeforeIndex(text, currentCursorPosition);
      break;
    case DIRECTION_NEXT:
      newCursorPosition = getEndOfNextSentenceAfterIndex(text, currentCursorPosition);
      break;
  }
  return selectText(nodeCompat, newCursorPosition, newCursorPosition);
}
 
Example 2
Source File: TextEditingUtils.java    From talkback with Apache License 2.0 4 votes vote down vote up
/**
 * Deletes text in the given {@link AccessibilityNodeInfoCompat} with the granularity indicated in
 * the given {@link Bundle}.
 *
 * @param nodeCompat The {@link AccessibilityNodeInfo} containing the text to delete
 * @param arguments The {@link Bundle} containing the granularity arguments for deletion
 * @return {@code true} if the deletion is successful
 */
public static boolean deleteTextWithGranularity(
    AccessibilityNodeInfoCompat nodeCompat, Bundle arguments) {
  if (arguments == Bundle.EMPTY) {
    return false;
  }

  nodeCompat.refresh();
  CharSequence text = nodeCompat.getText();

  // Find the bounds of the section of text to delete.
  int deleteSectionEnd = nodeCompat.getTextSelectionEnd();
  int deleteSectionStart;
  int deleteGranularity =
      arguments.getInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT);
  if (deleteGranularity == ACTION_GRANULARITY_SENTENCE) {
    deleteSectionStart = getEndOfLastSentenceBeforeIndex(text, deleteSectionEnd);
  } else if (deleteGranularity == ACTION_GRANULARITY_HIGHLIGHT) {
    deleteSectionStart = nodeCompat.getTextSelectionStart();
  } else {
    if (!PerformActionUtils.performAction(
        nodeCompat,
        AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
        arguments,
        null)) {
      return false;
    }
    nodeCompat.refresh();
    deleteSectionStart = nodeCompat.getTextSelectionEnd();
  }
  int deleteSectionLowerIndex = Math.min(deleteSectionStart, deleteSectionEnd);
  int deleteSectionUpperIndex = Math.max(deleteSectionStart, deleteSectionEnd);

  // Set text to be the entire existing text minus the section to delete.
  String oldText = (text == null) ? "" : text.toString();
  String newText =
      oldText.substring(0, deleteSectionLowerIndex) + oldText.substring(deleteSectionUpperIndex);
  arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, newText);
  if (!PerformActionUtils.performAction(
      nodeCompat, AccessibilityNodeInfo.ACTION_SET_TEXT, arguments, null)) {
    return false;
  }
  nodeCompat.refresh();

  // Place the cursor back where it was before the deletion.
  int endOfText = (text == null) ? 0 : text.length();
  int newCursorPosition = Math.min(deleteSectionLowerIndex, endOfText);
  return selectText(nodeCompat, newCursorPosition, newCursorPosition);
}
 
Example 3
Source File: TextEditActor.java    From talkback with Apache License 2.0 4 votes vote down vote up
/** Inserts text in edit-text. Modifies edit history. */
public boolean insert(
    AccessibilityNodeInfoCompat node, CharSequence textToInsert, EventId eventId) {

  if (node == null || Role.getRole(node) != Role.ROLE_EDIT_TEXT) {
    return false;
  }

  // Find current selected text or cursor position.
  int selectionStart = node.getTextSelectionStart();
  if (selectionStart < 0) {
    selectionStart = 0;
  }
  int selectionEnd = node.getTextSelectionEnd();
  if (selectionEnd < 0) {
    selectionEnd = selectionStart;
  }
  if (selectionEnd < selectionStart) {
    // Swap start and end to make sure they are in order.
    int newStart = selectionEnd;
    selectionEnd = selectionStart;
    selectionStart = newStart;
  }
  CharSequence currentText = node.getText();
  LogUtils.v(
      "RuleEditText",
      "insert() currentText=\"%s\"",
      (currentText == null ? "null" : currentText));
  if (currentText == null) {
    currentText = "";
  }

  // Set updated text.
  CharSequence textUpdated =
      TextUtils.concat(
          currentText.subSequence(0, selectionStart),
          textToInsert,
          currentText.subSequence(selectionEnd, currentText.length()));
  Bundle arguments = new Bundle();
  arguments.putCharSequence(ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, textUpdated);
  boolean result = PerformActionUtils.performAction(node, ACTION_SET_TEXT, arguments, eventId);
  if (!result) {
    return false;
  }

  // Move cursor to end of inserted text.
  return moveCursor(node, selectionStart + textToInsert.length(), eventId);
}
 
Example 4
Source File: TextEditingUtils.java    From talkback with Apache License 2.0 2 votes vote down vote up
/**
 * Checks the input {@link AccessibilityNodeInfoCompat} to see if it contains selected text.
 *
 * @param nodeCompat The {@link AccessibilityNodeInfoCompat} to check
 * @return {@code true} if the input contains text and some part of the text is selected
 */
public static boolean isTextSelected(AccessibilityNodeInfoCompat nodeCompat) {
  return nodeCompat.getText() != null
      && nodeCompat.getTextSelectionEnd() != nodeCompat.getTextSelectionStart();
}