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

The following examples show how to use androidx.core.view.accessibility.AccessibilityNodeInfoCompat#setBoundsInParent() . 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: NumberPicker.java    From DateTimePicker with Apache License 2.0 6 votes vote down vote up
private AccessibilityNodeInfo createAccessibiltyNodeInfoForInputText(
        int left, int top, int right, int bottom) {
    AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.wrap(mInputText.createAccessibilityNodeInfo());
    info.setSource(NumberPicker.this, VIRTUAL_VIEW_ID_INPUT);
    if (mAccessibilityFocusedView != VIRTUAL_VIEW_ID_INPUT) {
        info.addAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
    }
    if (mAccessibilityFocusedView == VIRTUAL_VIEW_ID_INPUT) {
        info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS);
    }
    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);
    return info.unwrap();
}
 
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: 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 5
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 6
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 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: 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);
  }
}
 
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();
}