Example usage for android.view.accessibility AccessibilityNodeInfo recycle

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

Introduction

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

Prototype

public void recycle() 

Source Link

Document

Return an instance back to be reused.

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  ww.j  a  v  a  2 s  .co  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.utils.FocusFinder.java

public static AccessibilityNodeInfoCompat getFocusedNode(AccessibilityService service, boolean fallbackOnRoot) {
    AccessibilityNodeInfo root = service.getRootInActiveWindow();
    AccessibilityNodeInfo focused = null;

    try {//from w w  w.  j  av  a2 s  .c  o m
        AccessibilityNodeInfo ret = null;
        if (root != null) {
            focused = root.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY);
            if (focused != null && focused.isVisibleToUser()) {
                ret = focused;
                focused = null;
            } else if (fallbackOnRoot) {
                ret = root;
                root = null;
            }
        } else {
            LogUtils.log(service, Log.ERROR, "No current window root");
        }

        if (ret != null) {
            return new AccessibilityNodeInfoCompat(ret);
        }
    } finally {
        if (root != null) {
            root.recycle();
        }

        if (focused != null) {
            focused.recycle();
        }
    }

    return null;
}

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

public static AccessibilityNodeInfoCompat getFocusedNode(AccessibilityService service, boolean fallbackOnRoot) {
    AccessibilityNodeInfo root = service.getRootInActiveWindow();
    AccessibilityNodeInfo focused = null;
    try {/*w w w .  j a va2  s . c o  m*/
        AccessibilityNodeInfo ret = null;
        if (root != null) {
            focused = root.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY);
            if (focused != null && focused.isVisibleToUser()) {
                ret = focused;
                focused = null;
            } else if (fallbackOnRoot) {
                ret = root;
                root = null;
            }
        } else {
            LogUtils.log(service, Log.ERROR, "No current window root");
        }
        if (ret != null) {
            return new AccessibilityNodeInfoCompat(ret);
        }
    } finally {
        if (root != null) {
            root.recycle();
        }
        if (focused != null) {
            focused.recycle();
        }
    }
    return null;
}

From source file:com.android.screenspeak.formatter.FallbackFormatter.java

@Override
public boolean format(AccessibilityEvent event, ScreenSpeakService context, Utterance utterance) {
    final AccessibilityNodeInfo source = event.getSource();

    // Drop events that have source nodes.
    if (source != null) {
        source.recycle();
        return false;
    }/*from   w  w w  .j a  v a  2  s. c o m*/

    // Add earcons and patterns since the event doesn't have a source node
    switch (event.getEventType()) {
    case AccessibilityEvent.TYPE_VIEW_FOCUSED:
        utterance.addHaptic(R.array.view_focused_or_selected_pattern);
        utterance.addAuditory(R.raw.focus_actionable);
        break;
    case AccessibilityEvent.TYPE_VIEW_SELECTED:
        utterance.addHaptic(R.array.view_focused_or_selected_pattern);
        utterance.addAuditory(R.raw.focus_actionable);
        break;
    case AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER:
        utterance.addHaptic(R.array.view_hovered_pattern);
        utterance.addAuditory(R.raw.focus);
        break;
    }

    final CharSequence text = AccessibilityEventUtils.getEventTextOrDescription(event);
    if (!TextUtils.isEmpty(text)) {
        utterance.addSpoken(text);
    }

    return true;
}

From source file:com.android.talkback.formatter.FallbackFormatter.java

@Override
public boolean format(AccessibilityEvent event, TalkBackService context, Utterance utterance) {
    final AccessibilityNodeInfo source = event.getSource();

    // Drop events that have source nodes.
    if (source != null) {
        source.recycle();
        return false;
    }/*from  ww w  .  j  a v a  2 s  .co m*/

    boolean hasEarcons = true;

    // Add earcons and patterns since the event doesn't have a source node
    switch (event.getEventType()) {
    case AccessibilityEvent.TYPE_VIEW_FOCUSED:
        utterance.addHaptic(R.array.view_focused_or_selected_pattern);
        utterance.addAuditory(R.raw.focus_actionable);
        break;
    case AccessibilityEvent.TYPE_VIEW_SELECTED:
        utterance.addHaptic(R.array.view_focused_or_selected_pattern);
        utterance.addAuditory(R.raw.focus_actionable);
        break;
    case AccessibilityEventCompat.TYPE_VIEW_HOVER_ENTER:
        utterance.addHaptic(R.array.view_hovered_pattern);
        utterance.addAuditory(R.raw.focus);
        break;
    default:
        hasEarcons = false;
        break;
    }

    final CharSequence text = AccessibilityEventUtils.getEventTextOrDescription(event);
    if (!TextUtils.isEmpty(text)) {
        utterance.addSpoken(text);
    }

    return hasEarcons || !utterance.getSpoken().isEmpty();
}

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

/**
 * Outputs the node tree from the current node using dfs preorder
 * traversal./*www .java 2  s  . c o  m*/
 */
private void printNodes() {
    HashSet<AccessibilityNodeInfo> seen = new HashSet<AccessibilityNodeInfo>();
    if (mCurrentNode == null) {
        LogUtils.log(this, Log.VERBOSE, "No current node");
        return;
    }
    LogUtils.log(this, Log.VERBOSE, "Printing nodes");
    printNodeTree(AccessibilityNodeInfo.obtain(mCurrentNode), "", seen);
    for (AccessibilityNodeInfo node : seen) {
        node.recycle();
    }
}

From source file:com.android.utils.WindowManager.java

/**
 * Gets the window whose anchor equals the given node.
 *//*  w w w . java  2 s . c om*/
public AccessibilityWindowInfo getAnchoredWindow(@Nullable AccessibilityNodeInfoCompat targetAnchor) {
    if (!BuildCompat.isAtLeastN() || targetAnchor == null) {
        return null;
    }

    int windowCount = mWindows.size();
    for (int i = 0; i < windowCount; ++i) {
        AccessibilityWindowInfo window = mWindows.get(i);
        if (window != null) {
            AccessibilityNodeInfo anchor = window.getAnchor();
            if (anchor != null) {
                try {
                    if (anchor.equals(targetAnchor.getInfo())) {
                        return window;
                    }
                } finally {
                    anchor.recycle();
                }
            }
        }
    }

    return null;
}

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

/**
 * Given an {@link AccessibilityEvent}, speaks a scroll position.
 *
 * @param event The source event.//from w  w  w  . j a v  a 2 s .  c o m
 */
private void handleScrollFeedback(AccessibilityEvent event) {
    final CharSequence text;
    AccessibilityNodeInfo source = event.getSource();
    if (Role.getRole(source) == Role.ROLE_PAGER) {
        text = getDescriptionForPageEvent(event, source);
    } else {
        text = getDescriptionForScrollEvent(event);
    }
    if (source != null) {
        source.recycle();
    }

    if (TextUtils.isEmpty(text)) {
        return;
    }

    // don't pronounce non-visible nodes
    AccessibilityNodeInfo node = event.getSource();
    if (node != null && !node.isVisibleToUser()) {
        return;
    }

    // Use QUEUE mode so that we don't interrupt more important messages.
    mSpeechController.speak(text, SpeechController.QUEUE_MODE_QUEUE, 0, mSpeechParams);
}

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

private int getWindowId(AccessibilityEvent event) {
    AccessibilityNodeInfo node = event.getSource();
    if (node == null) {
        return WINDOW_ID_NONE;
    }/*from   w  w  w  .  ja v  a 2  s .com*/

    int windowId = node.getWindowId();
    node.recycle();
    return windowId;
}

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

private CharSequence getWindowTitleForFeedback(int windowId) {
    CharSequence title = getWindowTitle(windowId);

    // Try to fall back to application label if window title is not available.
    if (title == null) {
        CharSequence packageName = mWindowToPackageName.get(windowId);

        // Try to get package name from accessibility window info if it's not in the map.
        if (packageName == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            for (AccessibilityWindowInfo window : mService.getWindows()) {
                if (window.getId() == windowId) {
                    AccessibilityNodeInfo rootNode = window.getRoot();
                    if (rootNode != null) {
                        packageName = rootNode.getPackageName();
                        rootNode.recycle();
                    }/*from   w  w  w.  j  av  a  2s  .com*/
                }
            }
        }

        if (packageName != null) {
            title = mService.getApplicationLabel(packageName);
        }
    }

    title = WindowManager.formatWindowTitleForFeedback(title, mService);

    if (isAlertDialog(windowId)) {
        title = mService.getString(R.string.template_alert_dialog_template, title);
    }

    return title;
}