Example usage for android.support.v4.view.accessibility AccessibilityNodeInfoCompat getChildCount

List of usage examples for android.support.v4.view.accessibility AccessibilityNodeInfoCompat getChildCount

Introduction

In this page you can find the example usage for android.support.v4.view.accessibility AccessibilityNodeInfoCompat getChildCount.

Prototype

public int getChildCount() 

Source Link

Document

Gets the number of children.

Usage

From source file:Main.java

private static AccessibilityNodeInfoCompat refreshFromChild(AccessibilityNodeInfoCompat node) {
    if (node.getChildCount() > 0) {
        AccessibilityNodeInfoCompat firstChild = node.getChild(0);
        if (firstChild != null) {
            AccessibilityNodeInfoCompat parent = firstChild.getParent();
            firstChild.recycle();//from w w  w .j a va 2 s. c o  m
            if (node.equals(parent)) {
                return parent;
            } else {
                recycleNodes(parent);
            }
        }
    }
    return null;
}

From source file:Main.java

private static AccessibilityNodeInfoCompat refreshFromParent(AccessibilityNodeInfoCompat node) {
    AccessibilityNodeInfoCompat parent = node.getParent();
    if (parent != null) {
        try {/* w ww .  j ava2 s.  co  m*/
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; ++i) {
                AccessibilityNodeInfoCompat child = parent.getChild(i);
                if (node.equals(child)) {
                    return child;
                }
                recycleNodes(child);
            }
        } finally {
            parent.recycle();
        }
    }
    return null;
}

From source file:com.googlecode.eyesfree.brailleback.FocusFinder.java

private static AccessibilityNodeInfoCompat findFirstFocusableDescendantInternal(
        AccessibilityNodeInfoCompat root, Context context, HashSet<AccessibilityNodeInfoCompat> seenNodes) {
    for (int i = 0, end = root.getChildCount(); i < end; ++i) {
        AccessibilityNodeInfoCompat child = root.getChild(i);
        if (child == null) {
            continue;
        }/*from   w  w w . ja v  a  2 s .  co  m*/
        if (AccessibilityNodeInfoUtils.shouldFocusNode(context, child)) {
            return child;
        }
        if (!seenNodes.add(child)) {
            LogUtils.log(FocusFinder.class, Log.ERROR, "Cycle in node tree");
            child.recycle();
            return null;
        }
        AccessibilityNodeInfoCompat n = findFirstFocusableDescendantInternal(child, context, seenNodes);
        if (n != null) {
            return n;
        }
    }
    return null;
}

From source file:com.googlecode.eyesfree.brailleback.FocusFinder.java

public static AccessibilityNodeInfoCompat findFirstFocusableDescendant(AccessibilityNodeInfoCompat root,
        Context context) {/*from  w  ww . j  a v a2  s.c o m*/
    // null guard and shortcut for leaf nodes.
    if (root == null || root.getChildCount() <= 0) {
        return null;
    }
    HashSet<AccessibilityNodeInfoCompat> seenNodes = new HashSet<AccessibilityNodeInfoCompat>();
    seenNodes.add(root);
    try {
        return findFirstFocusableDescendantInternal(root, context, seenNodes);
    } finally {
        seenNodes.remove(root); // Not owned by us.
        AccessibilityNodeInfoUtils.recycleNodes(seenNodes);
    }
}

From source file:com.googlecode.eyesfree.brailleback.FocusFinder.java

public static AccessibilityNodeInfoCompat findLastFocusableDescendant(AccessibilityNodeInfoCompat root,
        Context context) {/* w  ww  .java 2 s.  c  om*/
    // null guard and shortcut for leaf nodes.
    if (root == null || root.getChildCount() <= 0) {
        return null;
    }
    HashSet<AccessibilityNodeInfoCompat> seenNodes = new HashSet<AccessibilityNodeInfoCompat>();
    seenNodes.add(root);
    try {
        return findLastFocusableDescendantInternal(root, context, seenNodes);
    } finally {
        seenNodes.remove(root); // Not owned by us.
        AccessibilityNodeInfoUtils.recycleNodes(seenNodes);
    }
}

From source file:com.googlecode.eyesfree.brailleback.FocusFinder.java

private static AccessibilityNodeInfoCompat findLastFocusableDescendantInternal(AccessibilityNodeInfoCompat root,
        Context context, HashSet<AccessibilityNodeInfoCompat> seenNodes) {
    for (int end = root.getChildCount(), i = end - 1; i >= 0; --i) {
        AccessibilityNodeInfoCompat child = root.getChild(i);
        if (child == null) {
            continue;
        }//from  ww  w . j a  v a  2  s  .c  om

        AccessibilityNodeInfoCompat n = findLastFocusableDescendantInternal(child, context, seenNodes);
        if (n != null) {
            return n;
        }

        if (AccessibilityNodeInfoUtils.shouldFocusNode(context, child)) {
            return child;
        }
        if (!seenNodes.add(child)) {
            LogUtils.log(FocusFinder.class, Log.ERROR, "Cycle in node tree");
            child.recycle();
            return null;
        }
    }
    return null;
}

From source file:com.android.talkback.eventprocessor.ProcessorScrollPosition.java

private static CharSequence getSelectedPageTitle(AccessibilityNodeInfo node) {
    // We need to refresh() after the scroll to get an accurate page title but we can only
    // do that on API 18+.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
        return null;
    }//from   w  w w  . ja  v a 2 s .  co m

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

    AccessibilityNodeInfoCompat nodeCompat = new AccessibilityNodeInfoCompat(node);
    nodeCompat.refresh();

    int numChildren = nodeCompat.getChildCount(); // Not the number of pages!
    CharSequence title = null;
    for (int i = 0; i < numChildren; ++i) {
        AccessibilityNodeInfoCompat child = nodeCompat.getChild(i);
        if (child != null) {
            try {
                if (child.isVisibleToUser()) {
                    if (title == null) {
                        // Try to roughly match RulePagerPage, which uses getNodeText
                        // (but completely matching all the time is not critical).
                        title = AccessibilityNodeInfoUtils.getNodeText(child);
                    } else {
                        // Multiple visible children, abort.
                        return null;
                    }
                }
            } finally {
                child.recycle();
            }
        }
    }

    return title;
}

From source file:Main.java

/**
 * Returns whether the supplied {@link View} and {@link AccessibilityNodeInfoCompat} would
 * produce spoken feedback if it were accessibility focused.  NOTE: not all speaking nodes are
 * focusable./*from ww  w.j  a va  2 s  . c o  m*/
 *
 * @param view The {@link View} to evaluate
 * @param node The {@link AccessibilityNodeInfoCompat} to evaluate
 * @return {@code true} if it meets the criterion for producing spoken feedback
 */
public static boolean isSpeakingNode(@Nullable AccessibilityNodeInfoCompat node, @Nullable View view) {
    if (node == null || view == null) {
        return false;
    }

    if (!node.isVisibleToUser()) {
        return false;
    }

    int important = ViewCompat.getImportantForAccessibility(view);
    if (important == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
            || (important == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO && node.getChildCount() <= 0)) {
        return false;
    }

    return node.isCheckable() || hasText(node) || hasNonActionableSpeakingDescendants(node, view);
}

From source file:com.facebook.stetho.inspector.elements.android.AccessibilityNodeInfoWrapper.java

@Nullable
public static String getFocusableReasons(AccessibilityNodeInfoCompat node, View view) {
    boolean hasText = AccessibilityUtil.hasText(node);
    boolean isCheckable = node.isCheckable();
    boolean hasNonActionableSpeakingDescendants = AccessibilityUtil.hasNonActionableSpeakingDescendants(node,
            view);//  w  w w . j  a  v a 2s.c  o m

    if (AccessibilityUtil.isActionableForAccessibility(node)) {
        if (node.getChildCount() <= 0) {
            return "View is actionable and has no children.";
        } else if (hasText) {
            return "View is actionable and has a description.";
        } else if (isCheckable) {
            return "View is actionable and checkable.";
        } else if (hasNonActionableSpeakingDescendants) {
            return "View is actionable and has non-actionable descendants with descriptions.";
        }
    }

    if (AccessibilityUtil.isTopLevelScrollItem(node, view)) {
        if (hasText) {
            return "View is a direct child of a scrollable container and has a description.";
        } else if (isCheckable) {
            return "View is a direct child of a scrollable container and is checkable.";
        } else if (hasNonActionableSpeakingDescendants) {
            return "View is a direct child of a scrollable container and has non-actionable "
                    + "descendants with descriptions.";
        }
    }

    if (hasText) {
        return "View has a description and is not actionable, but has no actionable ancestor.";
    }

    return null;
}

From source file:com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckUtils.java

/**
 * Retrieve text for a node, which may include text from children of the node. This text is an
 * approximation of, but not always identical to, what TalkBack would speak for the node. One
 * difference is that there are no separators between the speakable text from different nodes.
 *
 * @param info The info whose text should be returned.
 *
 * @return Speakable text derived from the info and its children. Returns an empty string if there
 *         is no such text, and {@code null} if {@code info == null}.
 *//*from  w  w w .ja  v a  2s  .  c o m*/
static CharSequence getSpeakableTextForInfo(AccessibilityNodeInfo info) {
    if (info == null) {
        return null;
    }

    /* getLabeledBy for API 17+ */
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {

        AccessibilityNodeInfo labeledBy = info.getLabeledBy();
        if (labeledBy != null) {
            /* There could be a chain of labeledBy. Make sure it isn't a loop */
            Set<AccessibilityNodeInfo> infosVisited = new HashSet<>();
            infosVisited.add(info);
            infosVisited.add(labeledBy);
            AccessibilityNodeInfo endOfLabeledByChain = labeledBy.getLabeledBy();
            while (endOfLabeledByChain != null) {
                if (infosVisited.contains(endOfLabeledByChain)) {
                    infosVisited.remove(info);
                    for (AccessibilityNodeInfo infoVisited : infosVisited) {
                        infoVisited.recycle();
                    }
                    return null;
                }
                infosVisited.add(endOfLabeledByChain);
                labeledBy = endOfLabeledByChain;
                endOfLabeledByChain = labeledBy.getLabeledBy();
            }
            CharSequence labelText = getSpeakableTextForInfo(labeledBy);
            infosVisited.remove(info);
            for (AccessibilityNodeInfo infoVisited : infosVisited) {
                infoVisited.recycle();
            }
            return labelText;
        }
    }

    AccessibilityNodeInfoCompat compat = new AccessibilityNodeInfoCompat(info);

    // TODO(caseburkhardt) Pull in TalkBack's actual logic
    CharSequence nodeText = AccessibilityNodeInfoUtils.getNodeText(compat);
    StringBuilder returnStringBuilder = new StringBuilder((nodeText == null) ? "" : nodeText);
    /* If this node has a contentDescription, it overrides anything in children */
    if (TextUtils.isEmpty(compat.getContentDescription())) {
        for (int i = 0; i < compat.getChildCount(); ++i) {
            AccessibilityNodeInfoCompat child = compat.getChild(i);
            if (AccessibilityNodeInfoUtils.isVisibleOrLegacy(child)
                    && !AccessibilityNodeInfoUtils.isActionableForAccessibility(child)) {
                returnStringBuilder.append(getSpeakableTextForInfo((AccessibilityNodeInfo) child.getInfo()));
            }
        }
    }
    return returnStringBuilder;
}