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.linroid.pushapp.service.ApkAutoInstallService.java

/**
 * ?AccessibilityNodeInfo //from  w w  w . jav  a  2  s  . com
 *
 * @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.linroid.pushapp.service.ApkAutoInstallService.java

/**
 * ??/* w  w w.  ja v  a2s .  c  om*/
 *
 * @param event
 */
private void onApplicationUninstalled(AccessibilityEvent event) {
    Timber.d("??");

    // ???
    handler.removeCallbacks(handleUninstallTimeout);

    AccessibilityNodeInfo validInfo = getValidAccessibilityNodeInfo(event, sUninstallList);
    String label = null;
    if (validInfo != null && processApplicationUninstalled(event) && sUninstallList != null
            && validInfo.getText() != null) {
        label = validInfo.getText().toString();
        removePackFromListByAppName(sUninstallList, label);
        validInfo.recycle();
    }

    // ???Apk
    processPrepareInstall(label);
}

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

/**
 * ?//from w w w.jav a2  s .co  m
 *
 * @param event
 */
private void onApplicationInstalled(AccessibilityEvent event) {
    Timber.d("?");

    // ??
    handler.removeCallbacks(handleInstallTimeout);

    AccessibilityNodeInfo validInfo = getValidAccessibilityNodeInfo(event, sInstallList);
    if (validInfo != null) {
        if (autoOpen.getValue()) {
            boolean openSuccess = openAfterInstalled(event);
            Timber.d("?%s", openSuccess);
        }
        String label = validInfo.getText().toString();
        removePackFromListByAppName(sInstallList, label, true);
        validInfo.recycle();
    }
}

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

/**
 * ?//  w ww .j ava 2s  .c o  m
 *
 * @param event
 */
private void onApplicationUninstall(AccessibilityEvent event) {
    Timber.d("?");

    // ??????
    handler.postDelayed(handleUninstallTimeout, UNINSTALL_TIMEOUT);

    if (isValidPackageEvent(event, sUninstallList)) {
        AccessibilityNodeInfo nodeInfo = getAccessibilityNodeInfoByText(event,
                getString(R.string.btn_accessibility_uninstall));
        if (nodeInfo != null) {
            performClick(nodeInfo);
            return;
        }
        nodeInfo = getAccessibilityNodeInfoByText(event, getString(R.string.btn_accessibility_ok));
        if (nodeInfo != null) {
            performClick(nodeInfo);
            nodeInfo.recycle();
        }
    }
}

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

/**
 * ??//from  w  w w.  ja  v a2s  .  com
 *
 * @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

/**
 * ?//from   ww w  . java  2s. c  o m
 *
 * @param info
 * @param text
 * @param isGlobalAction ?
 * @return ??
 */
private boolean performEventAction(AccessibilityNodeInfo info, String text, boolean isGlobalAction) {
    if (info == null) {
        return false;
    }
    List<AccessibilityNodeInfo> nodes = info.findAccessibilityNodeInfosByText(text);
    if (nodes != null && nodes.size() > 0) {
        for (AccessibilityNodeInfo nodeInfo : nodes) {
            String nodeText = nodeInfo.getText() == null ? null : nodeInfo.getText().toString();
            if (text.equalsIgnoreCase(nodeText)) {
                if (isGlobalAction) {
                    //
                    performGlobalAction(GLOBAL_ACTION_BACK);
                } else {
                    performClick(nodeInfo);
                }
                return true;
            }
            nodeInfo.recycle();
        }
    }
    return false;
}

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;
    }/* w ww . j  av a2 s . c o 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.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://from w  w w.  j  a  v a 2s .c  o m
 * - 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.linroid.pushapp.service.ApkAutoInstallService.java

/**
 * /*  www.ja  v  a  2  s  . com*/
 *
 * @param event
 */
private void onInstallFail(AccessibilityEvent event) {
    Timber.e("");
    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);

    //  sInstallList  prepare ???
    if (sInstallList != null && sInstallList.size() > 0) {
        AccessibilityNodeInfo validInfo = getValidAccessibilityNodeInfo(event, sInstallList);
        if (validInfo != null && processApplicationUninstalled(event) && validInfo.getText() != null) {
            String label = validInfo.getText().toString();
            removePackFromListByAppName(sInstallList, label);
            for (int i = 0; i < sInstallList.size(); i++) {
                int key = sInstallList.keyAt(i);
                Pack pack = sInstallList.get(key);
                if (pack.getAppName().equals(label)) {
                    addPrepareInstallApplication(pack);
                    sInstallList.remove(key);
                    startActivity(IntentUtil.uninstallApp(pack.getPath()));
                    break;
                }
            }
            validInfo.recycle();
        }
    }

}