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

The following examples show how to use androidx.core.view.accessibility.AccessibilityNodeInfoCompat#setContentDescription() . 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: RadialTimePickerView.java    From DateTimePicker with Apache License 2.0 6 votes vote down vote up
@Override
protected void onPopulateNodeForVirtualView(int virtualViewId, AccessibilityNodeInfoCompat node) {
    node.setClassName(getClass().getName());
    node.addAction(AccessibilityNodeInfoCompat.AccessibilityActionCompat.ACTION_CLICK);

    final int type = getTypeFromId(virtualViewId);
    final int value = getValueFromId(virtualViewId);
    final CharSequence description = getVirtualViewDescription(type, value);
    node.setContentDescription(description);

    getBoundsForVirtualView(virtualViewId, mTempRect);
    node.setBoundsInParent(mTempRect);

    final boolean selected = isVirtualViewSelected(type, value);
    node.setSelected(selected);

    final int nextId = getVirtualViewIdAfter(type, value);
    if (nextId != INVALID_ID) {
        node.setTraversalBefore(RadialTimePickerView.this, nextId);
    }
}
 
Example 3
Source File: Chip.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onPopulateNodeForVirtualView(
    int virtualViewId, @NonNull AccessibilityNodeInfoCompat node) {
  if (virtualViewId == CLOSE_ICON_VIRTUAL_ID) {
    CharSequence closeIconContentDescription = getCloseIconContentDescription();
    if (closeIconContentDescription != null) {
      node.setContentDescription(closeIconContentDescription);
    } else {
      CharSequence chipText = getText();
      node.setContentDescription(
          getContext()
              .getString(
                  R.string.mtrl_chip_close_icon_content_description,
                  !TextUtils.isEmpty(chipText) ? chipText : "")
              .trim());
    }
    node.setBoundsInParent(getCloseIconTouchBoundsInt());
    node.addAction(AccessibilityActionCompat.ACTION_CLICK);
    node.setEnabled(isEnabled());
  } else {
    node.setContentDescription("");
    node.setBoundsInParent(EMPTY_BOUNDS);
  }
}
 
Example 4
Source File: Chip.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onPopulateNodeForHost(@NonNull AccessibilityNodeInfoCompat node) {
  node.setCheckable(isCheckable());
  node.setClickable(isClickable());
  if (isCheckable() || isClickable()) {
    node.setClassName(
        isCheckable()
            ? COMPOUND_BUTTON_ACCESSIBILITY_CLASS_NAME
            : BUTTON_ACCESSIBILITY_CLASS_NAME);
  } else {
    node.setClassName(GENERIC_VIEW_ACCESSIBILITY_CLASS_NAME);
  }
  CharSequence chipText = getText();
  if (VERSION.SDK_INT >= VERSION_CODES.M) {
    node.setText(chipText);
  } else {
    // Before M, TalkBack doesn't get the text from setText, so we have to set the content
    // description instead.
    node.setContentDescription(chipText);
  }
}
 
Example 5
Source File: MonthView.java    From MaterialDateTimePicker with Apache License 2.0 6 votes vote down vote up
@Override
protected void onPopulateNodeForVirtualView(int virtualViewId,
                                            @NonNull AccessibilityNodeInfoCompat node) {
    getItemBounds(virtualViewId, mTempRect);

    node.setContentDescription(getItemDescription(virtualViewId));
    node.setBoundsInParent(mTempRect);
    node.addAction(AccessibilityNodeInfo.ACTION_CLICK);

    // Flag non-selectable dates as disabled
    node.setEnabled(!mController.isOutOfRange(mYear, mMonth, virtualViewId));

    if (virtualViewId == mSelectedDay) {
        node.setSelected(true);
    }

}
 
Example 6
Source File: MonthView.java    From PersianDateRangePicker with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPopulateNodeForVirtualView(int virtualViewId,
                                            AccessibilityNodeInfoCompat node) {
  getItemBounds(virtualViewId, mTempRect);

  node.setContentDescription(getItemDescription(virtualViewId));
  node.setBoundsInParent(mTempRect);
  node.addAction(AccessibilityNodeInfo.ACTION_CLICK);

  if (virtualViewId == mSelectedDay) {
    node.setSelected(true);
  }

}
 
Example 7
Source File: ComponentAccessibilityDelegate.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPopulateNodeForVirtualView(int virtualViewId, AccessibilityNodeInfoCompat node) {
  final MountItem mountItem = getAccessibleMountItem(mView);
  if (mountItem == null) {
    Log.e(TAG, "No accessible mount item found for view: " + mView);

    // ExploreByTouchHelper insists that we set something.
    node.setContentDescription("");
    node.setBoundsInParent(getDefaultBounds());
    return;
  }

  final Drawable drawable = (Drawable) mountItem.getContent();
  final Rect bounds = drawable.getBounds();

  final Component component = getLayoutOutput(mountItem).getComponent();
  final ComponentLifecycle lifecycle = component;

  node.setClassName(lifecycle.getClass().getName());

  if (virtualViewId >= lifecycle.getExtraAccessibilityNodesCount()) {
    Log.e(TAG, "Received unrecognized virtual view id: " + virtualViewId);

    // ExploreByTouchHelper insists that we set something.
    node.setContentDescription("");
    node.setBoundsInParent(getDefaultBounds());
    return;
  }

  lifecycle.onPopulateExtraAccessibilityNode(node, virtualViewId, bounds.left, bounds.top);
}
 
Example 8
Source File: SimpleMonthView.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPopulateNodeForVirtualView(int virtualViewId, AccessibilityNodeInfoCompat node) {
    final boolean hasBounds = getBoundsForDay(virtualViewId, mTempRect);

    if (!hasBounds) {
        // The day is invalid, kill the node.
        mTempRect.setEmpty();
        node.setContentDescription("");
        node.setBoundsInParent(mTempRect);
        node.setVisibleToUser(false);
        return;
    }

    node.setText(getDayText(virtualViewId));
    node.setContentDescription(getDayDescription(virtualViewId));
    node.setBoundsInParent(mTempRect);

    final boolean isDayEnabled = isDayEnabled(virtualViewId);
    if (isDayEnabled) {
        //node.addAction(AccessibilityAction.ACTION_CLICK);
        node.addAction(AccessibilityNodeInfoCompat.AccessibilityActionCompat.ACTION_CLICK);
    }

    node.setEnabled(isDayEnabled);

    if (virtualViewId == mActivatedDay) {
        // TODO: This should use activated once that's supported.
        node.setChecked(true);
    }
}
 
Example 9
Source File: BaseSlider.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPopulateNodeForVirtualView(
    int virtualViewId, AccessibilityNodeInfoCompat info) {

  info.addAction(AccessibilityNodeInfoCompat.AccessibilityActionCompat.ACTION_SET_PROGRESS);

  List<Float> values = slider.getValues();
  final float value = values.get(virtualViewId);
  float valueFrom = slider.getValueFrom();
  float valueTo = slider.getValueTo();

  if (slider.isEnabled()) {
    if (value > valueFrom) {
      info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD);
    }
    if (value < valueTo) {
      info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD);
    }
  }

  info.setRangeInfo(RangeInfoCompat.obtain(RANGE_TYPE_FLOAT, valueFrom, valueTo, value));
  info.setClassName(SeekBar.class.getName());
  StringBuilder contentDescription = new StringBuilder();
  // Add the content description of the slider.
  if (slider.getContentDescription() != null) {
    contentDescription.append(slider.getContentDescription()).append(",");
  }
  // Add the range to the content description.
  if (values.size() > 1) {
    contentDescription.append(startOrEndDescription(virtualViewId));
    contentDescription.append(slider.formatValue(value));
  }
  info.setContentDescription(contentDescription.toString());

  slider.updateBoundsForVirturalViewId(virtualViewId, virtualViewBounds);
  info.setBoundsInParent(virtualViewBounds);
}
 
Example 10
Source File: RadialMenuView.java    From talkback with Apache License 2.0 5 votes vote down vote up
@Override
protected void populateNodeForItem(RadialMenuItem item, AccessibilityNodeInfoCompat node) {
  node.setContentDescription(item.getTitle());
  node.setVisibleToUser(item.isVisible());
  node.setCheckable(item.isCheckable());
  node.setChecked(item.isChecked());
  node.setEnabled(item.isEnabled());
  node.setClickable(true);

  getLocalVisibleRect(tempRect);
  node.setBoundsInParent(tempRect);

  getGlobalVisibleRect(tempRect);
  node.setBoundsInScreen(tempRect);
}
 
Example 11
Source File: MonthView.java    From MaterialDateRangePicker with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPopulateNodeForVirtualView(int virtualViewId,
        AccessibilityNodeInfoCompat node) {
    getItemBounds(virtualViewId, mTempRect);

    node.setContentDescription(getItemDescription(virtualViewId));
    node.setBoundsInParent(mTempRect);
    node.addAction(AccessibilityNodeInfo.ACTION_CLICK);

    if (virtualViewId == mSelectedDay) {
        node.setSelected(true);
    }

}
 
Example 12
Source File: MonthView.java    From cathode with Apache License 2.0 5 votes vote down vote up
@Override protected void onPopulateNodeForVirtualView(int virtualViewId,
    AccessibilityNodeInfoCompat node) {
  getItemBounds(virtualViewId, mTempRect);

  node.setContentDescription(getItemDescription(virtualViewId));
  node.setBoundsInParent(mTempRect);
  node.addAction(AccessibilityNodeInfo.ACTION_CLICK);

  if (virtualViewId == mSelectedDay) {
    node.setSelected(true);
  }
}
 
Example 13
Source File: TextSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnPopulateExtraAccessibilityNode
static void onPopulateExtraAccessibilityNode(
    AccessibilityNodeInfoCompat node,
    int extraNodeIndex,
    int componentBoundsLeft,
    int componentBoundsTop,
    @Prop(resType = ResType.STRING) CharSequence text,
    @FromBoundsDefined Layout textLayout,
    @FromBoundsDefined ClickableSpan[] clickableSpans) {
  if (!(text instanceof Spanned)) {
    return;
  }

  final Spanned spanned = (Spanned) text;

  final ClickableSpan span = clickableSpans[extraNodeIndex];
  final int start = spanned.getSpanStart(span);
  final int end = spanned.getSpanEnd(span);
  final int startLine = textLayout.getLineForOffset(start);
  final int endLine = textLayout.getLineForOffset(end);

  // The bounds for multi-line strings should *only* include the first line.  This is because
  // Talkback triggers its click at the center point of these bounds, and if that center point
  // is outside the spannable, it will click on something else.  There is no harm in not outlining
  // the wrapped part of the string, as the text for the whole string will be read regardless of
  // the bounding box.
  final int selectionPathEnd =
      startLine == endLine ? end : textLayout.getLineVisibleEnd(startLine);

  textLayout.getSelectionPath(start, selectionPathEnd, sTempPath);
  sTempPath.computeBounds(sTempRectF, /* unused */ true);

  sTempRect.set(
      componentBoundsLeft + (int) sTempRectF.left,
      componentBoundsTop + (int) sTempRectF.top,
      componentBoundsLeft + (int) sTempRectF.right,
      componentBoundsTop + (int) sTempRectF.bottom);

  if (sTempRect.isEmpty()) {
    // Text is not actually visible.
    // Override bounds so it doesn't crash ExploreByTouchHelper.java
    sTempRect.set(0, 0, 1, 1);
    node.setBoundsInParent(sTempRect);
    node.setContentDescription(""); // make node non-focusable
    return;
  }

  node.setBoundsInParent(sTempRect);

  node.setClickable(true);
  node.setFocusable(true);
  node.setEnabled(true);
  node.setVisibleToUser(true);
  node.setText(spanned.subSequence(start, end));
  if (span instanceof AccessibleClickableSpan) {
    AccessibleClickableSpan accessibleClickableSpan = (AccessibleClickableSpan) span;
    String contentDescription = accessibleClickableSpan.getAccessibilityDescription();
    String role = accessibleClickableSpan.getAccessibilityRole();
    if (contentDescription != null) {
      node.setContentDescription(contentDescription);
    }
    if (role != null) {
      node.setClassName(role);
    } else {
      node.setClassName(AccessibilityRole.BUTTON);
    }
  } else {
    node.setClassName(AccessibilityRole.BUTTON);
  }
}