Example usage for android.view.accessibility AccessibilityNodeInfo getLabeledBy

List of usage examples for android.view.accessibility AccessibilityNodeInfo getLabeledBy

Introduction

In this page you can find the example usage for android.view.accessibility AccessibilityNodeInfo getLabeledBy.

Prototype

public AccessibilityNodeInfo getLabeledBy() 

Source Link

Document

Gets the node info which serves as the label of the view represented by this info for accessibility purposes.

Usage

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}.
 */// w w  w .  j a v a2  s .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;
}

From source file:com.android.screenspeak.speechrules.NodeSpeechRuleProcessor.java

/**
 * If the supplied node has a label, replaces the builder text with a
 * version formatted with the label./* w w  w  .j  a  v  a2 s .c  o m*/
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void formatTextWithLabel(AccessibilityNodeInfoCompat node, SpannableStringBuilder builder) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
        return;

    // TODO(KM): add getLabeledBy to support lib
    AccessibilityNodeInfo info = (AccessibilityNodeInfo) node.getInfo();
    if (info == null)
        return;
    AccessibilityNodeInfo labeledBy = info.getLabeledBy();
    if (labeledBy == null)
        return;
    AccessibilityNodeInfoCompat labelNode = new AccessibilityNodeInfoCompat(labeledBy);

    final SpannableStringBuilder labelDescription = new SpannableStringBuilder();
    Set<AccessibilityNodeInfoCompat> visitedNodes = new HashSet<>();
    appendDescriptionForTree(labelNode, labelDescription, null, null, visitedNodes);
    AccessibilityNodeInfoUtils.recycleNodes(visitedNodes);
    if (TextUtils.isEmpty(labelDescription)) {
        return;
    }

    final String labeled = mContext.getString(R.string.template_labeled_item, builder, labelDescription);
    Spannable spannableLabeledText = StringBuilderUtils.createSpannableFromTextWithTemplate(labeled, builder);

    // Replace the text of the builder.
    builder.clear();
    builder.append(spannableLabeledText);
}

From source file:com.android.talkback.speechrules.NodeSpeechRuleProcessor.java

/**
 * If the supplied node has a label, replaces the builder text with a
 * version formatted with the label.//from www.jav  a2s. c  o  m
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void formatTextWithLabel(AccessibilityNodeInfoCompat node, SpannableStringBuilder builder) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
        return;

    // TODO: add getLabeledBy to support lib
    AccessibilityNodeInfo info = (AccessibilityNodeInfo) node.getInfo();
    if (info == null)
        return;
    AccessibilityNodeInfo labeledBy = info.getLabeledBy();
    if (labeledBy == null)
        return;
    AccessibilityNodeInfoCompat labelNode = new AccessibilityNodeInfoCompat(labeledBy);

    final SpannableStringBuilder labelDescription = new SpannableStringBuilder();
    Set<AccessibilityNodeInfoCompat> visitedNodes = new HashSet<>();
    appendDescriptionForTree(labelNode, labelDescription, null, null, visitedNodes);
    AccessibilityNodeInfoUtils.recycleNodes(visitedNodes);
    if (TextUtils.isEmpty(labelDescription)) {
        return;
    }

    final String labeled = mContext.getString(R.string.template_labeled_item, builder, labelDescription);
    Spannable spannableLabeledText = StringBuilderUtils.createSpannableFromTextWithTemplate(labeled, builder);

    // Replace the text of the builder.
    builder.clear();
    builder.append(spannableLabeledText);
}