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

The following examples show how to use androidx.core.view.accessibility.AccessibilityNodeInfoCompat#setClassName() . 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: 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 2
Source File: NestedScrollView.java    From AndroidAnimationExercise with Apache License 2.0 6 votes vote down vote up
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
    super.onInitializeAccessibilityNodeInfo(host, info);
    final NestedScrollView nsvHost = (NestedScrollView) host;
    info.setClassName(ScrollView.class.getName());
    if (nsvHost.isEnabled()) {
        final int scrollRange = nsvHost.getScrollRange();
        if (scrollRange > 0) {
            info.setScrollable(true);
            if (nsvHost.getScrollY() > 0) {
                info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD);
            }
            if (nsvHost.getScrollY() < scrollRange) {
                info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD);
            }
        }
    }
}
 
Example 3
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 4
Source File: SliderPager.java    From Android-Image-Slider with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
    super.onInitializeAccessibilityNodeInfo(host, info);
    info.setClassName(SliderPager.class.getName());
    info.setScrollable(canScroll());
    if (canScrollHorizontally(1)) {
        info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD);
    }
    if (canScrollHorizontally(-1)) {
        info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD);
    }
}
 
Example 5
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 6
Source File: VerticalViewPager.java    From DKVideoPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
    super.onInitializeAccessibilityNodeInfo(host, info);
    info.setClassName(ViewPager.class.getName());
    info.setScrollable(canScroll());
    if (internalCanScrollVertically(1)) {
        info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD);
    }
    if (internalCanScrollVertically(-1)) {
        info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD);
    }
}
 
Example 7
Source File: NumberPicker.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
private AccessibilityNodeInfo createAccessibilityNodeInfoForVirtualButton(int virtualViewId,
                                                                          String text, int left, int top, int right, int bottom) {
    AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
    info.setClassName(Button.class.getName());
    info.setPackageName(getContext().getPackageName());
    info.setSource(NumberPicker.this, virtualViewId);
    info.setParent(NumberPicker.this);
    info.setText(text);
    info.setClickable(true);
    info.setLongClickable(true);
    info.setEnabled(NumberPicker.this.isEnabled());
    Rect boundsInParent = mTempRect;
    boundsInParent.set(left, top, right, bottom);
    info.setVisibleToUser(isVisibleToUser(boundsInParent));
    info.setBoundsInParent(boundsInParent);
    Rect boundsInScreen = boundsInParent;
    int[] locationOnScreen = mTempArray;
    getLocationOnScreen(locationOnScreen);
    boundsInScreen.offset(locationOnScreen[0], locationOnScreen[1]);
    info.setBoundsInScreen(boundsInScreen);

    if (mAccessibilityFocusedView != virtualViewId) {
        info.addAction(AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS);
    }
    if (mAccessibilityFocusedView == virtualViewId) {
        info.addAction(AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
    }
    if (NumberPicker.this.isEnabled()) {
        info.addAction(AccessibilityNodeInfoCompat.ACTION_CLICK);
    }

    return info.unwrap();
}
 
Example 8
Source File: CustomViewPager.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
    super.onInitializeAccessibilityNodeInfo(host, info);
    info.setClassName(CustomViewPager.class.getName());
    info.setScrollable(canScroll());
    if (canScrollHorizontally(1)) {
        info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD);
    }
    if (canScrollHorizontally(-1)) {
        info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD);
    }
}
 
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: DropdownMenuEndIconDelegate.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onInitializeAccessibilityNodeInfo(
    View host, @NonNull AccessibilityNodeInfoCompat info) {
  super.onInitializeAccessibilityNodeInfo(host, info);
  // The non-editable exposed dropdown menu behaves like a Spinner.
  if (textInputLayout.getEditText().getKeyListener() == null) {
    info.setClassName(Spinner.class.getName());
  }
  if (info.isShowingHintText()) {
    // Set hint text to null so TalkBack doesn't announce the label twice when there is no
    // item selected.
    info.setHintText(null);
  }
}
 
Example 11
Source File: ExploreByTouchObjectHelper.java    From talkback with Apache License 2.0 5 votes vote down vote up
@Override
protected final void onPopulateNodeForVirtualView(
    int virtualViewId, AccessibilityNodeInfoCompat node) {
  final T item = getItemForVirtualViewId(virtualViewId);
  if (item == null) {
    return;
  }

  populateNodeForItem(item, node);
  node.setClassName(item.getClass().getName());
}
 
Example 12
Source File: ComponentAccessibilityDelegate.java    From litho with Apache License 2.0 4 votes vote down vote up
@Override
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat node) {
  final MountItem mountItem = getAccessibleMountItem(mView);

  if (mNodeInfo != null && mNodeInfo.getOnInitializeAccessibilityNodeInfoHandler() != null) {
    EventDispatcherUtils.dispatchOnInitializeAccessibilityNodeInfoEvent(
        mNodeInfo.getOnInitializeAccessibilityNodeInfoHandler(), host, node, mSuperDelegate);
  } else if (mountItem != null) {
    super.onInitializeAccessibilityNodeInfo(host, node);
    // Coalesce the accessible mount item's information with the
    // the root host view's as they are meant to behave as a single
    // node in the accessibility framework.
    final Component component = getLayoutOutput(mountItem).getComponent();
    component.onPopulateAccessibilityNode(host, node);
  } else {
    super.onInitializeAccessibilityNodeInfo(host, node);
  }

  // If an accessibilityRole has been set, set the className here.  It's important that this
  // happens *after* any calls to super, since the super call will set a className of its own and
  // override this one.
  if (mNodeInfo != null && mNodeInfo.getAccessibilityRole() != null) {
    node.setClassName(mNodeInfo.getAccessibilityRole());
  }

  if (mNodeInfo != null && mNodeInfo.getAccessibilityRoleDescription() != null) {
    node.setRoleDescription(mNodeInfo.getAccessibilityRoleDescription());

    // if no role was explicitly specified, set a role of "NONE".  This allows the role
    // description to still be announced without changing any other behavior.
    if (mNodeInfo.getAccessibilityRole() == null) {
      node.setClassName(AccessibilityRole.NONE);
    }
  }

  if (mNodeInfo != null
      && mNodeInfo.getAccessibilityHeadingState() != NodeInfo.ACCESSIBILITY_HEADING_UNSET) {
    node.setHeading(
        mNodeInfo.getAccessibilityHeadingState() == NodeInfo.ACCESSIBILITY_HEADING_SET_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);
  }
}
 
Example 14
Source File: NumberPicker.java    From DateTimePicker with Apache License 2.0 4 votes vote down vote up
private AccessibilityNodeInfo createAccessibilityNodeInfoForNumberPicker(int left, int top,
                                                                         int right, int bottom) {
    AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
    info.setClassName(NumberPicker.class.getName());
    info.setPackageName(getContext().getPackageName());
    info.setSource(NumberPicker.this);

    if (hasVirtualDecrementButton()) {
        info.addChild(NumberPicker.this, VIRTUAL_VIEW_ID_DECREMENT);
    }
    info.addChild(NumberPicker.this, VIRTUAL_VIEW_ID_INPUT);
    if (hasVirtualIncrementButton()) {
        info.addChild(NumberPicker.this, VIRTUAL_VIEW_ID_INCREMENT);
    }

    //info.setParent((View) getParentForAccessibility());
    info.setParent((View) ViewCompat.getParentForAccessibility(NumberPicker.this));
    info.setEnabled(NumberPicker.this.isEnabled());
    info.setScrollable(true);

    // FIXME final float applicationScale = getContext().getResources().getCompatibilityInfo().applicationScale;
    final float applicationScale = 1f;

    Rect boundsInParent = mTempRect;
    boundsInParent.set(left, top, right, bottom);
    //boundsInParent.scale(applicationScale);
    scaleRect(boundsInParent, applicationScale);
    info.setBoundsInParent(boundsInParent);

    info.setVisibleToUser(isVisibleToUser());

    Rect boundsInScreen = boundsInParent;
    int[] locationOnScreen = mTempArray;
    getLocationOnScreen(locationOnScreen);
    boundsInScreen.offset(locationOnScreen[0], locationOnScreen[1]);
    // boundsInScreen.scale(applicationScale);
    scaleRect(boundsInScreen, applicationScale);
    info.setBoundsInScreen(boundsInScreen);

    if (mAccessibilityFocusedView != View.NO_ID) {
        info.addAction(AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS);
    }
    if (mAccessibilityFocusedView == View.NO_ID) {
        info.addAction(AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
    }
    if (NumberPicker.this.isEnabled()) {
        if (getWrapSelectorWheel() || getValue() < getMaxValue()) {
            info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_FORWARD);
        }
        if (getWrapSelectorWheel() || getValue() > getMinValue()) {
            info.addAction(AccessibilityNodeInfoCompat.ACTION_SCROLL_BACKWARD);
        }
    }

    return info.unwrap();
}
 
Example 15
Source File: FrescoVitoImage2Spec.java    From fresco with MIT License 4 votes vote down vote up
@OnPopulateAccessibilityNode
static void onPopulateAccessibilityNode(View host, AccessibilityNodeInfoCompat node) {
  node.setClassName(AccessibilityRole.IMAGE);
}