Example usage for android.support.v4.view.accessibility AccessibilityRecordCompat getCurrentItemIndex

List of usage examples for android.support.v4.view.accessibility AccessibilityRecordCompat getCurrentItemIndex

Introduction

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

Prototype

public int getCurrentItemIndex() 

Source Link

Document

Gets the index of the source in the list of items the can be visited.

Usage

From source file:com.google.android.marvin.mytalkback.ProcessorFocusAndSingleTap.java

/**
 * Attempts to place focus on the {@code event}'s selected item.
 *//*from  ww w .  j  av  a 2 s  .com*/
private boolean setFocusFromViewSelected(AccessibilityEvent event, AccessibilityRecordCompat record) {
    AccessibilityNodeInfoCompat source = null;
    AccessibilityNodeInfoCompat child = null;

    try {
        source = record.getSource();
        if (source == null) {
            return false;
        }

        final int index = (record.getCurrentItemIndex() - record.getFromIndex());
        if ((index < 0) || (index >= source.getChildCount())) {
            return false;
        }

        child = source.getChild(index);
        if (child == null) {
            return false;
        }

        if (!AccessibilityNodeInfoUtils.isTopLevelScrollItem(mService, child)) {
            return false;
        }

        return tryFocusing(child);
    } finally {
        AccessibilityNodeInfoUtils.recycleNodes(source, child);
    }
}

From source file:com.android.screenspeak.eventprocessor.ProcessorFocusAndSingleTap.java

/**
 * @param record the AccessbilityRecord for the event
 * @param isViewFocusedEvent true if the event is TYPE_VIEW_FOCUSED, otherwise it is
 * TYPE_VIEW_SELECTED./*from   w w w.  ja v  a2 s  .  c o m*/
 */
private boolean setFocusOnView(AccessibilityRecordCompat record, boolean isViewFocusedEvent) {
    AccessibilityNodeInfoCompat source = null;
    AccessibilityNodeInfoCompat existing = null;
    AccessibilityNodeInfoCompat child = null;

    try {
        source = record.getSource();
        if (source == null) {
            return false;
        }

        if (record.getItemCount() > 0) {
            final int index = (record.getCurrentItemIndex() - record.getFromIndex());
            if (index >= 0 && index < source.getChildCount()) {
                child = source.getChild(index);
                if (child != null) {
                    if (AccessibilityNodeInfoUtils.isTopLevelScrollItem(child) && tryFocusing(child)) {
                        return true;
                    }
                }
            }
        }

        if (!isViewFocusedEvent) {
            return false;
        }

        // Logic below is only specific to TYPE_VIEW_FOCUSED event
        // Try focusing the source node.
        if (tryFocusing(source)) {
            return true;
        }

        // If we fail and the source node already contains focus, abort.
        existing = source.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY);
        if (existing != null) {
            return false;
        }

        // If we fail to focus a node, perhaps because it is a focusable
        // but non-speaking container, we should still attempt to place
        // focus on a speaking child within the container.
        child = AccessibilityNodeInfoUtils.searchFromBfs(source,
                AccessibilityNodeInfoUtils.FILTER_SHOULD_FOCUS);
        return child != null && tryFocusing(child);

    } finally {
        AccessibilityNodeInfoUtils.recycleNodes(source, existing, child);
    }
}