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

The following examples show how to use androidx.core.view.accessibility.AccessibilityNodeInfoCompat#getContentDescription() . 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: TextSpec.java    From litho with Apache License 2.0 6 votes vote down vote up
@OnPopulateAccessibilityNode
static void onPopulateAccessibilityNode(
    View host,
    AccessibilityNodeInfoCompat node,
    @Prop(resType = ResType.STRING) CharSequence text,
    @Prop(optional = true, resType = ResType.BOOL) boolean isSingleLine) {
  if (ViewCompat.getImportantForAccessibility(host)
      == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
    ViewCompat.setImportantForAccessibility(host, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
  }
  CharSequence contentDescription = node.getContentDescription();
  node.setText(contentDescription != null ? contentDescription : text);
  node.setContentDescription(contentDescription != null ? contentDescription : text);

  node.addAction(AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
  node.addAction(AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
  node.setMovementGranularities(
      AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_CHARACTER
          | AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_WORD
          | AccessibilityNodeInfoCompat.MOVEMENT_GRANULARITY_PARAGRAPH);

  if (!isSingleLine) {
    node.setMultiLine(true);
  }
}
 
Example 2
Source File: AccessibilityNodeInfoUtils.java    From talkback with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the text of a <code>node</code> by returning the content description (if available) or by
 * returning the text.
 *
 * @param node The node.
 * @return The node text.
 */
public static @Nullable CharSequence getNodeText(@Nullable AccessibilityNodeInfoCompat node) {
  if (node == null) {
    return null;
  }

  // Prefer content description over text.
  // TODO: Why are we checking the trimmed length?
  final CharSequence contentDescription = node.getContentDescription();
  if (!TextUtils.isEmpty(contentDescription)
      && (TextUtils.getTrimmedLength(contentDescription) > 0)) {
    return contentDescription;
  }

  final CharSequence text = node.getText();
  if (!TextUtils.isEmpty(text) && (TextUtils.getTrimmedLength(text) > 0)) {
    return text;
  }

  return null;
}
 
Example 3
Source File: SpannableUtils.java    From talkback with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves SpannableString containing the target span in the accessibility node. The content
 * description and text of the node is checked in order.
 *
 * @param node The AccessibilityNodeInfoCompat where the text comes from.
 * @param spanClass Class of target span.
 * @return SpannableString with at least 1 target span. null if no target span found in the node.
 */
public static <T> @Nullable SpannableString getStringWithTargetSpan(
    AccessibilityNodeInfoCompat node, Class<T> spanClass) {

  CharSequence text = node.getContentDescription();
  if (isEmptyOrNotSpannableStringType(text)) {
    text = AccessibilityNodeInfoUtils.getText(node);
    if (isEmptyOrNotSpannableStringType(text)) {
      return null;
    }
  }

  SpannableString spannable = SpannableString.valueOf(text);
  T[] spans = spannable.getSpans(0, spannable.length(), spanClass);
  if (spans == null || spans.length == 0) {
    return null;
  }

  return spannable;
}
 
Example 4
Source File: FeedbackUtils.java    From talkback with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the developer-provided text inside a node that will be used to generate spoken feedback.
 * If the node does not have any text, we attempt to get node text of any non-focusable children.
 * If there are no focusable children with text, an empty string will be returned.
 *
 * <p>Note: This method should never be called with nodes returned from
 * AccessibilityNodeInfo#obtain. These nodes do not retain children information, so this method
 * may return the incorrect text. Instead, use SwitchAccessNodeCompat#getNodeText.
 *
 * @param nodeCompat the {@link AccessibilityNodeInfoCompat} of the node from which the
 *     developer-provided text should be retrieved
 * @return the developer-provided text (content description or text) of the given node. If there
 *     is neither content description nor text inside the node or its children, then return an
 *     empty string
 */
public static String getNodeText(AccessibilityNodeInfoCompat nodeCompat) {
  CharSequence speakableText = nodeCompat.getContentDescription();

  if (StringUtils.isEmpty(speakableText)) {
    speakableText = nodeCompat.getText();
  }

  // If speakable text is empty, see if there are any non-focusable children nodes. If so, use
  // their text for the speakable text of this node. We filter out any focusable children nodes
  // to prevent duplicated speakable text from both a parent and child node.
  if (StringUtils.isEmpty(speakableText)) {
    StringBuilder builder = new StringBuilder();

    int numChildren = nodeCompat.getChildCount();
    for (int i = 0; i < numChildren; i++) {
      AccessibilityNodeInfoCompat child = nodeCompat.getChild(i);
      if ((child != null)
          && AccessibilityNodeInfoUtils.hasMinimumPixelsVisibleOnScreen(child)
          && !AccessibilityNodeInfoUtils.shouldFocusNode(child)) {
        CharSequence childText = getNodeText(child);

        if (!StringUtils.isEmpty(childText)) {
          if (builder.length() != 0) {
            builder.append(" ");
          }
          builder.append(childText);
        }
      }

      if (child != null) {
        child.recycle();
      }
    }

    speakableText = builder.toString();
  }
  if (StringUtils.isEmpty(speakableText)) {
    speakableText = "";
  }

  return speakableText.toString();
}
 
Example 5
Source File: GranularityTraversal.java    From talkback with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the text for granularity traversal. For edit texts, it first checks for text and then the
 * hint text and then the content description.
 *
 * <p><strong>Note:</strong> For edit texts with no text and just hint text, getText() returns the
 * hint text.
 *
 * @param node on which granularity traversal has to be performed.
 * @return the content description or the text/hint text in case of edit fields, since navigation
 *     on these is not handled by the framework.
 */
public static CharSequence getIterableTextForAccessibility(AccessibilityNodeInfoCompat node) {
  if ((Role.getRole(node) == Role.ROLE_EDIT_TEXT) && !TextUtils.isEmpty(node.getText())) {
    return node.getText();
  }

  return node.getContentDescription();
}