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

The following examples show how to use androidx.core.view.accessibility.AccessibilityNodeInfoCompat#unwrap() . 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: 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 3
Source File: AccessibilityNodeInfoUtils.java    From talkback with Apache License 2.0 5 votes vote down vote up
/** Returns true if the node supports text location data. */
@TargetApi(Build.VERSION_CODES.O)
public static boolean supportsTextLocation(AccessibilityNodeInfoCompat node) {
  if (!BuildVersionUtils.isAtLeastO() || node == null) {
    return false;
  }
  AccessibilityNodeInfo info = node.unwrap();
  if (info == null) {
    return false;
  }
  List<String> extraData = info.getAvailableExtraData();
  return extraData != null
      && extraData.contains(AccessibilityNodeInfo.EXTRA_DATA_TEXT_CHARACTER_LOCATION_KEY);
}
 
Example 4
Source File: AccessibilityNodeInfoUtils.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the node to which the given node's window is anchored, if there is an anchor. Note: you
 * must recycle the node that is returned from this method.
 */
public static AccessibilityNodeInfoCompat getAnchor(@Nullable AccessibilityNodeInfoCompat node) {
  if (!BuildVersionUtils.isAtLeastN()) {
    return null;
  }

  if (node == null) {
    return null;
  }

  AccessibilityNodeInfo nativeNode = node.unwrap();
  if (nativeNode == null) {
    return null;
  }

  AccessibilityWindowInfo nativeWindow = getWindow(nativeNode);
  if (nativeWindow == null) {
    return null;
  }

  AccessibilityNodeInfo nativeAnchor = nativeWindow.getAnchor();
  if (nativeAnchor == null) {
    return null;
  }

  return AccessibilityNodeInfoUtils.toCompat(nativeAnchor);
}
 
Example 5
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 6
Source File: AccessibilityNodeInfoUtils.java    From talkback with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the location of specific range of node text. It returns null if the node doesn't support
 * text location data or the index is incorrect.
 *
 * @param node The node being queried.
 * @param fromCharIndex start index of the queried text range.
 * @param toCharIndex end index of the queried text range.
 */
@TargetApi(Build.VERSION_CODES.O)
@Nullable
public static List<Rect> getTextLocations(
    AccessibilityNodeInfoCompat node, int fromCharIndex, int toCharIndex) {
  if (node == null || !BuildVersionUtils.isAtLeastO()) {
    return null;
  }

  if (fromCharIndex < 0
      || !PrimitiveUtils.isInInterval(
          toCharIndex, fromCharIndex, node.getText().length(), true)) {
    return null;
  }
  AccessibilityNodeInfo info = node.unwrap();
  if (info == null) {
    return null;
  }
  Bundle args = new Bundle();
  args.putInt(
      AccessibilityNodeInfo.EXTRA_DATA_TEXT_CHARACTER_LOCATION_ARG_START_INDEX, fromCharIndex);
  args.putInt(
      AccessibilityNodeInfo.EXTRA_DATA_TEXT_CHARACTER_LOCATION_ARG_LENGTH,
      toCharIndex - fromCharIndex);
  if (!info.refreshWithExtraData(
      AccessibilityNodeInfo.EXTRA_DATA_TEXT_CHARACTER_LOCATION_KEY, args)) {
    return null;
  }

  Bundle extras = info.getExtras();
  Parcelable[] data =
      extras.getParcelableArray(AccessibilityNodeInfo.EXTRA_DATA_TEXT_CHARACTER_LOCATION_KEY);
  if (data == null) {
    return null;
  }
  List<Rect> result = new ArrayList<>(data.length);
  for (Parcelable item : data) {
    if (item == null) {
      continue;
    }
    RectF rectF = (RectF) item;
    result.add(
        new Rect((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));
  }
  return result;
}