Example usage for android.view.accessibility AccessibilityEvent getSource

List of usage examples for android.view.accessibility AccessibilityEvent getSource

Introduction

In this page you can find the example usage for android.view.accessibility AccessibilityEvent getSource.

Prototype

public AccessibilityNodeInfo getSource() 

Source Link

Document

Gets the AccessibilityNodeInfo of the event source.

Usage

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

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUSED) {
        final AccessibilityNodeInfo node = event.getSource();
        if (node == null) {
            LogUtils.log(this, Log.WARN, "TYPE_VIEW_ACCESSIBILITY_FOCUSED event without a source.");
            return;
        }/*from   w ww.  j  a va2s.  co m*/

        // When a new view gets focus, clear the state of the granularity
        // manager if this event came from a different node than the locked
        // node but from the same window.
        final AccessibilityNodeInfoCompat nodeCompat = new AccessibilityNodeInfoCompat(node);
        mGranularityManager.onNodeFocused(nodeCompat);
        nodeCompat.recycle();
    }
}

From source file:com.linroid.pushapp.service.ApkAutoInstallService.java

/**
 * ??/*w ww .  j av  a  2  s . c  om*/
 *
 * @param event
 * @return
 */
private boolean openAfterInstalled(AccessibilityEvent event) {
    AccessibilityNodeInfo eventInfo;
    if (event != null && event.getSource() != null) {
        eventInfo = event.getSource();
    } else {
        eventInfo = getRootInActiveWindow();
    }
    boolean success = false;
    if (eventInfo != null) {
        success = performEventAction(eventInfo, getString(R.string.btn_accessibility_run), false)
                || performEventAction(eventInfo, getString(R.string.btn_accessibility_open), false);
        eventInfo.recycle();
    }
    return success;
}

From source file:com.linroid.pushapp.service.ApkAutoInstallService.java

/**
 * ?AccessibilityNodeInfo/*  w  w w .  j  ava 2 s.  c  om*/
 *
 * @param event
 * @param text
 * @return
 */
private boolean hasAccessibilityNodeInfoByText(AccessibilityEvent event, String text) {
    List<AccessibilityNodeInfo> nodes = null;
    if (event != null && event.getSource() != null) {
        nodes = event.getSource().findAccessibilityNodeInfosByText(text);
    } else {
        AccessibilityNodeInfo info = getRootInActiveWindow();
        if (info != null) {
            nodes = info.findAccessibilityNodeInfosByText(text);
        }
    }
    return !(nodes == null || nodes.size() <= 0);
}

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

/**
 * Helper method for {@link #shouldDropEvent} that filters out selected events that occur
 * in close proximity to focused events.
 *
 * A selected event should be kept if://  w ww.j a va  2 s .c  om
 * - The most recent focused event occurred over {@link #DELAY_SELECTED_AFTER_FOCUS} ms ago.
 * - The most recent focused event occurred on a different branch of the accessibility node
 *   tree, i.e., not in an ancestor or descendant of the selected event.
 *
 * @param event The view-selected event to consider retaining.
 * @return Whether to retain the event.
 */
private boolean shouldKeepViewSelectedEvent(final AccessibilityEvent event) {
    if (mLastFocusedEvent == null) {
        return true;
    }

    if (event.getEventTime() - mLastFocusedEvent.getEventTime() > DELAY_SELECTED_AFTER_FOCUS) {
        return true;
    }

    // AccessibilityEvent.getSource will obtain() an AccessibilityNodeInfo, so it is our
    // responsibility to recycle() it.
    AccessibilityNodeInfo selectedSource = event.getSource();
    AccessibilityNodeInfo focusedSource = mLastFocusedEvent.getSource();

    try {
        // Note: AccessibilityNodeInfoCompat constructor will silently succeed when wrapping
        // a null object.
        if (selectedSource != null && focusedSource != null) {
            AccessibilityNodeInfoCompat selectedSourceCompat = new AccessibilityNodeInfoCompat(selectedSource);
            AccessibilityNodeInfoCompat focusedSourceCompat = new AccessibilityNodeInfoCompat(focusedSource);

            if (AccessibilityNodeInfoUtils.areInSameBranch(selectedSourceCompat, focusedSourceCompat)) {
                return false;
            }
        }

        // In different branch (or we could not check branches of accessibility node tree).
        return true;
    } finally {
        if (selectedSource != null) {
            selectedSource.recycle();
        }
        if (focusedSource != null) {
            focusedSource.recycle();
        }
    }
}

From source file:com.android.screenspeak.controller.CursorControllerApp.java

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUSED) {
        final AccessibilityNodeInfo node = event.getSource();
        if (node == null) {
            if (LogUtils.LOG_LEVEL <= Log.WARN) {
                Log.w(LOGTAG, "TYPE_VIEW_ACCESSIBILITY_FOCUSED event without a source.");
            }//from  w  ww  .  jav  a 2  s  . c  om
            return;
        }

        // When a new view gets focus, clear the state of the granularity
        // manager if this event came from a different node than the locked
        // node but from the same window.
        final AccessibilityNodeInfoCompat nodeCompat = new AccessibilityNodeInfoCompat(node);
        mGranularityManager.onNodeFocused(nodeCompat);
        if (mSwitchNodeWithGranularityDirection == TraversalStrategy.SEARCH_FOCUS_FORWARD) {
            mGranularityManager.navigate(AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
        } else if (mSwitchNodeWithGranularityDirection == TraversalStrategy.SEARCH_FOCUS_BACKWARD) {
            mGranularityManager.startFromLastNode();
            mGranularityManager.navigate(AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
        }
        mSwitchNodeWithGranularityDirection = 0;
        nodeCompat.recycle();
        mReachedEdge = false;
        mGranularityNavigationReachedEdge = false;
    }
}

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

/**
 * Resets cached scrollable state when touch exploration after window state
 * changes./*  ww w .j a  v a  2s.  c  om*/
 */
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    switch (event.getEventType()) {
    case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
        // Reset cached scrollable state.
        mLastNodeWasScrollable = false;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Store window title in the map.
            List<CharSequence> titles = event.getText();
            if (titles.size() > 0) {
                AccessibilityNodeInfo node = event.getSource();
                if (node != null) {
                    int windowType = getWindowType(node);
                    if (windowType == AccessibilityWindowInfo.TYPE_APPLICATION
                            || windowType == AccessibilityWindowInfo.TYPE_SYSTEM) {
                        mWindowTitlesMap.put(node.getWindowId(), titles.get(0));
                    }
                    node.recycle();
                }
            }
        }
        break;
    case AccessibilityEvent.TYPE_WINDOWS_CHANGED:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // Copy key set not to modify original map.
            HashSet<Integer> windowIdsToBeRemoved = new HashSet<Integer>();
            windowIdsToBeRemoved.addAll(mWindowTitlesMap.keySet());

            // Enumerate window ids to be removed.
            List<AccessibilityWindowInfo> windows = mService.getWindows();
            for (AccessibilityWindowInfo window : windows) {
                windowIdsToBeRemoved.remove(window.getId());
            }

            // Delete titles of non-existing window ids.
            for (Integer windowId : windowIdsToBeRemoved) {
                mWindowTitlesMap.remove(windowId);
            }
        }
        break;
    }
}

From source file:com.linroid.pushapp.service.ApkAutoInstallService.java

/**
 * ?AccessibilityNodeInfo //from   w ww.ja  v  a2s. c  o m
 *
 * @param event
 * @param text
 * @return
 */
private AccessibilityNodeInfo getAccessibilityNodeInfoByText(AccessibilityEvent event, String text) {
    List<AccessibilityNodeInfo> nodes = null;
    // try-catch? event.getSource  NullPointerException
    try {
        if (event != null && event.getSource() != null) {
            nodes = event.getSource().findAccessibilityNodeInfosByText(text);
        }
    } catch (Exception e) {

    }

    // ?else???
    if (nodes == null || nodes.size() == 0) {
        AccessibilityNodeInfo info = getRootInActiveWindow();
        if (info != null) {
            nodes = info.findAccessibilityNodeInfosByText(text);
        }
    }
    if (nodes != null && nodes.size() > 0) {
        for (AccessibilityNodeInfo nodeInfo : nodes) {
            String nodeText = nodeInfo.getText() == null ? BuildConfig.VERSION_NAME
                    : nodeInfo.getText().toString();
            //nodeInfo.getClassName().equals(className) &&
            if (nodeText.equalsIgnoreCase(text)) {
                return nodeInfo;
            }
            nodeInfo.recycle();
        }
    }
    return null;
}

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

public boolean isFromRefocusAction(AccessibilityEvent event) {
    long eventTime = event.getEventTime();
    int eventType = event.getEventType();
    if (eventType != AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
            && eventType != AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED) {
        return false;
    }/*from w w  w.j a v a 2 s . co m*/
    AccessibilityNodeInfo source = event.getSource();
    try {
        return mLastRefocusStartTime < eventTime
                && (mLastRefocusEndTime > eventTime || mLastRefocusEndTime < mLastRefocusStartTime)
                && mLastRefocusedNode != null && mLastRefocusedNode.getInfo().equals(source);
    } finally {
        if (source != null) {
            source.recycle();
        }
    }
}

From source file:com.android.talkback.controller.CursorControllerApp.java

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    int eventType = event.getEventType();
    if (eventType == AccessibilityEventCompat.TYPE_VIEW_ACCESSIBILITY_FOCUSED) {
        final AccessibilityNodeInfo node = event.getSource();
        if (node == null) {
            if (LogUtils.LOG_LEVEL <= Log.WARN) {
                Log.w(LOGTAG, "TYPE_VIEW_ACCESSIBILITY_FOCUSED event without a source.");
            }//  ww w  . jav  a2  s  .c om
            return;
        }

        // When a new view gets focus, clear the state of the granularity
        // manager if this event came from a different node than the locked
        // node but from the same window.
        final AccessibilityNodeInfoCompat nodeCompat = new AccessibilityNodeInfoCompat(node);
        mGranularityManager.onNodeFocused(nodeCompat);
        if (mSwitchNodeWithGranularityDirection == AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY) {
            mGranularityManager.navigate(AccessibilityNodeInfoCompat.ACTION_NEXT_AT_MOVEMENT_GRANULARITY);
        } else if (mSwitchNodeWithGranularityDirection == AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY) {
            mGranularityManager.startFromLastNode();
            mGranularityManager.navigate(AccessibilityNodeInfoCompat.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY);
        }
        mSwitchNodeWithGranularityDirection = 0;
        nodeCompat.recycle();
        mReachedEdge = false;
        mGranularityNavigationReachedEdge = false;
    } else if (eventType == AccessibilityEvent.TYPE_VIEW_FOCUSED) {
        final AccessibilityNodeInfo node = event.getSource();
        if (node != null) {
            final AccessibilityNodeInfoCompat nodeCompat = new AccessibilityNodeInfoCompat(node);

            // Note: we also need to check ROLE_EDIT_TEXT for JB MR1 and lower and for
            // Chrome/WebView 51 and lower. We should check isEditable() first because it's
            // more semantically appropriate for what we want.
            if (nodeCompat.isEditable() || Role.getRole(nodeCompat) == Role.ROLE_EDIT_TEXT) {
                AccessibilityNodeInfoUtils.recycleNodes(mLastEditable);
                mLastEditable = nodeCompat;
            } else {
                nodeCompat.recycle();
            }
        }
    } else if (mIsWindowNavigationAvailable && eventType == AccessibilityEvent.TYPE_WINDOWS_CHANGED) {
        // Remove last focused nodes of non-existing windows.
        Set<Integer> windowIdsToBeRemoved = new HashSet(mLastFocusedNodeMap.keySet());
        for (AccessibilityWindowInfo window : mService.getWindows()) {
            windowIdsToBeRemoved.remove(window.getId());
        }
        for (Integer windowIdToBeRemoved : windowIdsToBeRemoved) {
            AccessibilityNodeInfoCompat removedNode = mLastFocusedNodeMap.remove(windowIdToBeRemoved);
            if (removedNode != null) {
                removedNode.recycle();
            }
        }
    }
}

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

private boolean shouldIgnoreUpdateListEvent(AccessibilityEvent event) {
    // Don't speak during full-screen read.
    if (mFullScreenReadController.isActive()) {
        return true;
    }/*from   w  ww. java  2  s.c  om*/

    final int fromIndex = event.getFromIndex() + 1;
    final int itemCount = event.getItemCount();
    if (itemCount <= 0 || fromIndex <= 0) {
        return true;
    }

    EventId eventId;
    try {
        eventId = new EventId(event);
    } catch (Exception e) {
        return true;
    }

    final Integer cachedFromIndex = mCachedFromValues.get(eventId);
    final Integer cachedItemCount = mCachedItemCounts.get(eventId);

    if ((cachedFromIndex != null) && (cachedFromIndex == fromIndex) && (cachedItemCount != null)
            && (cachedItemCount == itemCount)) {
        // The from index hasn't changed, which means the event is coming
        // from a re-layout or resize and should not be spoken.
        return true;
    }

    // The behavior of put() for an existing key is unspecified, so we can't
    // recycle the old or new key nodes.
    mCachedFromValues.put(eventId, fromIndex);
    mCachedItemCounts.put(eventId, itemCount);

    // Allow the list indices to be cached, but don't actually speak after auto-scroll.
    if (mAutoScrollNode != null) {
        AccessibilityNodeInfo source = event.getSource();
        if (source != null) {
            try {
                if (source.equals(mAutoScrollNode.getInfo())) {
                    mAutoScrollNode.recycle();
                    mAutoScrollNode = null;
                    return true;
                }
            } finally {
                source.recycle();
            }
        }
    }

    return false;
}