private static AccessibilityNodeInfoCompat createNodeInfoFromView()

in core/src/main/java/com/facebook/testing/screenshot/layouthierarchy/AccessibilityUtil.java [168:201]


  private static AccessibilityNodeInfoCompat createNodeInfoFromView(
      @Nullable View view, int retryCount) {
    if (view == null) {
      return null;
    }

    final AccessibilityNodeInfoCompat nodeInfo = AccessibilityNodeInfoCompat.obtain();

    // For some unknown reason, Android seems to occasionally throw a NPE from
    // onInitializeAccessibilityNodeInfo.
    try {
      ViewCompat.onInitializeAccessibilityNodeInfo(view, nodeInfo);
    } catch (NullPointerException e) {
      if (nodeInfo != null) {
        nodeInfo.recycle();
      }
      return null;
    } catch (RuntimeException e) {
      if (nodeInfo != null) {
        nodeInfo.recycle();
      }
      // For some unknown reason, Android seems to occasionally throw a IndexOutOfBoundsException
      // and also random RuntimeExceptions because the handler seems not to be initialized
      // from onInitializeAccessibilityNodeInfoInternal in ViewGroup.  This seems to be
      // nondeterministic, so lets retry if this happens.
      if (retryCount > 0) {
        return createNodeInfoFromView(view, retryCount - 1);
      }

      return null;
    }

    return nodeInfo;
  }