Java Code Examples for android.view.accessibility.AccessibilityNodeInfo#getParent()

The following examples show how to use android.view.accessibility.AccessibilityNodeInfo#getParent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UiObject2.java    From za-Farmer with MIT License 6 votes vote down vote up
/** Returns the visible bounds of {@code node} in screen coordinates. */
private Rect getVisibleBounds(AccessibilityNodeInfo node) {
    // Get the object bounds in screen coordinates
    Rect ret = new Rect();
    node.getBoundsInScreen(ret);

    // Trim any portion of the bounds that are not on the screen
    Rect screen = new Rect(0, 0, mDevice.getDisplayWidth(), mDevice.getDisplayHeight());
    ret.intersect(screen);

    // Find the visible bounds of our first scrollable ancestor
    AccessibilityNodeInfo ancestor = null;
    for (ancestor = node.getParent(); ancestor != null; ancestor = ancestor.getParent()) {
        // If this ancestor is scrollable
        if (ancestor.isScrollable()) {
            // Trim any portion of the bounds that are hidden by the non-visible portion of our
            // ancestor
            Rect ancestorRect = getVisibleBounds(ancestor);
            ret.intersect(ancestorRect);
            break;
        }
    }

    return ret;
}
 
Example 2
Source File: AccessibilityUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 长按指定的节点
 * @param nodeInfo    {@link AccessibilityNodeInfo}
 * @param clickParent 如果当前节点不可点击, 是否往上追溯点击父节点, 直到点击成功或没有父节点
 * @param clickAll    判断是否点击全部节点
 * @return {@code true} success, {@code false} fail
 */
public static boolean performLongClick(final AccessibilityNodeInfo nodeInfo, final boolean clickParent, final boolean clickAll) {
    if (nodeInfo == null) return false;
    if (clickParent) {
        if (nodeInfo.isClickable()) {
            return nodeInfo.performAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
        } else {
            AccessibilityNodeInfo parent = nodeInfo.getParent();
            while (parent != null) {
                if (performLongClick(parent)) {
                    if (!clickAll) {
                        return true;
                    }
                }
                parent = parent.getParent();
            }
            return true;
        }
    } else {
        return performLongClick(nodeInfo);
    }
}
 
Example 3
Source File: AnimatedEditText.java    From stynico with MIT License 6 votes vote down vote up
/**
    * 搜索包含红包的UI节点, 点击所有
    *
    * @param root 根UI节点
    * @return 成功点击的红包数
    */
   Integer getFromNode(AccessibilityNodeInfo root)
   {
List<AccessibilityNodeInfo> mNodes =
    root.findAccessibilityNodeInfosByText(getResources().getString(R.string.chat_pattern));

for (AccessibilityNodeInfo node : mNodes)
{
    // Log.d("node", node.toString());
    AccessibilityNodeInfo parent = node.getParent();
    if (parent == null)
    {
	//Log.d("node.parent", "null"); // 有时候没有父节点, 蜜汁bug
    }
    else
    {
	//Log.d("click", "GET" + Integer.valueOf(node.hashCode()).toString());
	parent.performAction(AccessibilityNodeInfo.ACTION_CLICK); // TextView不能点, 点的是ListView, 详情查看clickable
	cnt_get += 1;
	lastNode = node.hashCode();
    }
}

return mNodes.size(); // 即搜索结果数目
   }
 
Example 4
Source File: AccessibilityNodeInfoUtils.java    From oversec with GNU General Public License v3.0 6 votes vote down vote up
private static AccessibilityNodeInfo refreshFromParent(
        AccessibilityNodeInfo node) {
    AccessibilityNodeInfo parent = node.getParent();
    if (parent != null) {
        try {
            int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; ++i) {
                AccessibilityNodeInfo child = parent.getChild(i);
                if (node.equals(child)) {
                    return child;
                }
                recycleNodes(child);
            }
        } finally {
            parent.recycle();
        }
    }
    return null;
}
 
Example 5
Source File: WeChatNearly.java    From pc-android-controller-android with Apache License 2.0 6 votes vote down vote up
private static void startHello(AccessibilityNodeInfo nodeInfo) {

        if (nodeInfo == null) {
            L.d("rootWindow为空");
            return;
        }
        final List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("打招呼");
        if (list == null || list.isEmpty()) {
            return;
        }
        if (list.size() > 0) {
            final AccessibilityNodeInfo info = list.get(list.size() - 1);
            final AccessibilityNodeInfo parent = info.getParent();
            if (parent == null) {
                L.e("parent is null");
                return;
            }

            info.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);

        }
    }
 
Example 6
Source File: AccessibilityNodeInfoUtils.java    From oversec with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the root node of the tree containing {@code node}.
 */
public static AccessibilityNodeInfo getRoot(AccessibilityNodeInfo node) {
    if (node == null) {
        return null;
    }

    AccessibilityNodeInfo current = null;
    AccessibilityNodeInfo parent = AccessibilityNodeInfo.obtain(node);

    do {
        current = parent;
        parent = current.getParent();
    } while (parent != null);

    return current;
}
 
Example 7
Source File: TreeDebugNavigationMode.java    From brailleback with Apache License 2.0 5 votes vote down vote up
private AccessibilityNodeInfo getPreviousSibling(
    AccessibilityNodeInfo from) {
    AccessibilityNodeInfo ret = null;
    AccessibilityNodeInfo parent = from.getParent();
    if (parent == null) {
        return null;
    }
    AccessibilityNodeInfo prev = null;
    AccessibilityNodeInfo cur = null;
    try {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; ++i) {
            cur = parent.getChild(i);
            if (cur == null) {
                return null;
            }
            if (cur.equals(from)) {
                ret = prev;
                prev = null;
                return ret;
            }
            if (prev != null) {
                prev.recycle();
            }
            prev = cur;
            cur = null;
        }
    } finally {
        parent.recycle();
        if (prev != null) {
            prev.recycle();
        }
        if (cur != null) {
            cur.recycle();
        }
    }
    return ret;
}
 
Example 8
Source File: UiObject2.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
/** Returns the visible bounds of {@code node} in screen coordinates. */
private Rect getVisibleBounds(AccessibilityNodeInfo node) {
	// Get the object bounds in screen coordinates
	Rect ret = new Rect();
	node.getBoundsInScreen(ret);

	// Trim any portion of the bounds that are not on the screen
	Rect screen = new Rect(0, 0, mDevice.getDisplayWidth(),
			mDevice.getDisplayHeight());
	ret.intersect(screen);

	// Find the visible bounds of our first scrollable ancestor
	AccessibilityNodeInfo ancestor = null;
	for (ancestor = node.getParent(); ancestor != null; ancestor = ancestor
			.getParent()) {
		// If this ancestor is scrollable
		if (ancestor.isScrollable()) {
			// Trim any portion of the bounds that are hidden by the
			// non-visible portion of our
			// ancestor
			Rect ancestorRect = getVisibleBounds(ancestor);
			ret.intersect(ancestorRect);
			break;
		}
	}

	return ret;
}
 
Example 9
Source File: LaunchApp.java    From PUMA with Apache License 2.0 5 votes vote down vote up
private List<AccessibilityNodeInfo> getClickables(AccessibilityNodeInfo root) {
	if (root == null) {
		Util.err("FATAL: getClickables() source is NULL");
	}

	List<AccessibilityNodeInfo> ret = new ArrayList<AccessibilityNodeInfo>();
	List<AccessibilityNodeInfo> leaves = get_leaf_nodes(root);

	for (int i = 0; i < leaves.size(); i++) {
		AccessibilityNodeInfo leaf = leaves.get(i);
		AccessibilityNodeInfo node = leaf;
		boolean matched = matchNode(node);

		while (!matched) {
			node = node.getParent();

			if (node != null) {
				// Util.log("UP " + node.getClassName() + ", " + node.getText());
				matched = matchNode(node);
			} else {
				break;
			}
		}

		if (matched && !ret.contains(node)) {
			// Util.log("FOUND " + node.getClassName() + ", " + node.getText());
			ret.add(node);
		}
	}

	return ret;
}
 
Example 10
Source File: TreeDebugNavigationMode.java    From brailleback with Apache License 2.0 5 votes vote down vote up
private AccessibilityNodeInfo getNextSibling(
    AccessibilityNodeInfo from) {
    AccessibilityNodeInfo parent = from.getParent();
    if (parent == null) {
        return null;
    }
    AccessibilityNodeInfo cur = null;
    try {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount - 1; ++i) {
            cur = parent.getChild(i);
            if (cur == null) {
                return null;
            }
            if (cur.equals(from)) {
                return parent.getChild(i + 1);
            }
            if (cur != null) {
                cur.recycle();
                cur = null;
            }
        }
    } finally {
        parent.recycle();
        if (cur != null) {
            cur.recycle();
        }
    }
    return null;
}
 
Example 11
Source File: UiObject.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Walks up the layout hierarchy to find a scrollable parent. A scrollable parent
 * indicates that this node might be in a container where it is partially
 * visible due to scrolling. In this case, its clickable center might not be visible and
 * the click coordinates should be adjusted.
 *
 * @param node
 * @return The accessibility node info.
 */
private AccessibilityNodeInfo getScrollableParent(AccessibilityNodeInfo node) {
    AccessibilityNodeInfo parent = node;
    while(parent != null) {
        parent = parent.getParent();
        if (parent != null && parent.isScrollable()) {
            return parent;
        }
    }
    return null;
}
 
Example 12
Source File: dex_smali.java    From stynico with MIT License 5 votes vote down vote up
/**
    * 获取一个可以点击的节点
    */
   private AccessibilityNodeInfo getClickableNode(AccessibilityNodeInfo node)
   {
AccessibilityNodeInfo parent=node;
while (parent != null)
{
    if (parent.isClickable())
    {
	break;
    }
    parent = parent.getParent();
}
return parent;
   }
 
Example 13
Source File: dili.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 获取一个可以点击的节点
 */
private AccessibilityNodeInfo getClickableNode(AccessibilityNodeInfo node) {
    AccessibilityNodeInfo parent = node;
    while (parent != null) {
        if (parent.isClickable()) {
            break;
        }
        parent = parent.getParent();
    }
    return parent;
}
 
Example 14
Source File: peService.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 描述:处理口令红包
 * 作者:卜俊文
 * 邮箱:[email protected]
 * 日期:2017/11/1 下午4:58
 *
 * @param passwordList
 */
public void progressPassword(List<AccessibilityNodeInfo> passwordList) {
    if (passwordList != null && passwordList.size() > 0) {
        for (AccessibilityNodeInfo accessibilityNodeInfo : passwordList) {
            if (accessibilityNodeInfo != null && !TextUtils.isEmpty(accessibilityNodeInfo.getText()) && QQConstant.QQ_CLICK_PASSWORD_DIALOG.equals(accessibilityNodeInfo.getText().toString())) {
                //如果口令红包存在,就在输入框中进行输入,然后发送
                AccessibilityNodeInfo parent = accessibilityNodeInfo.getParent();
                if (parent != null) {
                    CharSequence contentDescription = parent.getContentDescription();
                    if (!TextUtils.isEmpty(contentDescription)) {
                        //1. 获取口令
                        String key = (String) contentDescription;
                        if (key.contains(",") && key.contains("口令:")) {
                            key = key.substring(key.indexOf("口令:") + 3, key.lastIndexOf(","));
                        }
                        // Log.e("口令", key);

                        //2. 填写口令到编辑框上然后进行发送
                        replyMessage(key);

                        //返回,关闭红包页面
                        performBackClick(1200);
                    }
                }
            }
        }
    }
}
 
Example 15
Source File: BaseAccessibilityService.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 模拟点击事件
 *
 * @param nodeInfo nodeInfo
 */
public void performViewClick(AccessibilityNodeInfo nodeInfo) {
    if (nodeInfo == null) {
        return;
    }
    while (nodeInfo != null) {
        if (nodeInfo.isClickable()) {
            nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            break;
        }
        nodeInfo = nodeInfo.getParent();
    }
}
 
Example 16
Source File: RedPacketService.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 遍历查找红包
 */
private void findRedPacket(AccessibilityNodeInfo rootNode) {
    if (rootNode != null) {
        //从最后一行开始找起
        for (int i = rootNode.getChildCount() - 1; i >= 0; i--) {
            AccessibilityNodeInfo node = rootNode.getChild(i);
            //如果node为空则跳过该节点
            if (node == null) {
                continue;
            }
            CharSequence text = node.getText();
            if (text != null && text.toString().equals("领取红包")) {
                AccessibilityNodeInfo parent = node.getParent();
                //while循环,遍历"领取红包"的各个父布局,直至找到可点击的为止
                while (parent != null) {
                    if (parent.isClickable()) {
                        //模拟点击
                        Log.e("spire发现了列表页的红包 执行点击", text.toString());
                        parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        //isOpenRP用于判断该红包是否点击过
                        isOpenRP = true;

                        break;
                    }
                    parent = parent.getParent();
                }
            }
            //判断是否已经打开过那个最新的红包了,是的话就跳出for循环,不是的话继续遍历
            if (isOpenRP) {
                break;
            } else {
                findRedPacket(node);
            }

        }
    }
}
 
Example 17
Source File: AnimatedEditText.java    From stynico with MIT License 5 votes vote down vote up
/**
    * 搜索包含红包的UI节点, 点击末几个
    *
    * @param root      根UI节点
    * @param size      点击最后size个
    * @param ignoreDup 是否无视重复检测
    * @return 成功点击的红包数
    */
   Integer getFromLastNode(AccessibilityNodeInfo root, Integer size, boolean ignoreDup)
   {
List<AccessibilityNodeInfo> mNodes =
    root.findAccessibilityNodeInfosByText(getResources().getString(R.string.chat_pattern));

size = Math.min(size, mNodes.size()); // 先设成功点击数为预计点击的红包数目
for (Integer i = mNodes.size() - size; i < mNodes.size(); i++)
{
    AccessibilityNodeInfo node = mNodes.get(i);
    //  Log.d("node", node.toString());
    AccessibilityNodeInfo parent = node.getParent();
    if (parent == null)
    {
	//Log.d("node.parent", "null"); // 有时候没有父节点, 蜜汁bug
    }
    else
    {
	if (ignoreDup || (lastNode != node.hashCode()))
	{ // 非重复红包, 点击
	    //    Log.d("click", "GET" + Integer.valueOf(node.hashCode()).toString());
	    parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
	    cnt_get += 1;
	    lastNode = node.hashCode();
	}
	else
	{
	    //   Log.d("node duplicate", Integer.valueOf(node.hashCode()).toString());
	    size -= 1; // 重复红包, 减少成功计数
	}
    }
}

return size;
   }
 
Example 18
Source File: AccessUtil.java    From pc-android-controller-android with Apache License 2.0 4 votes vote down vote up
/**
     * 点击匹配的nodeInfo
     *
     * @param str text关键字
     */
    public static boolean openNext(AccessibilityNodeInfo nodeInfo, String str) {
        if (nodeInfo == null) {
            L.d("rootWindow为空");
            return false;
        }
        final List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText(str);
        if (list == null || list.isEmpty()) {
            return false;
        }
        if (list != null && list.size() > 0) {
            final AccessibilityNodeInfo info = list.get(list.size() - 1);
            if (info == null) {
                return false;
            }
            final AccessibilityNodeInfo parent = info.getParent();
            if(parent == null) {
                return false;
            }
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    Looper.prepare();
//                    try {
//                        sleep(2000);
////                        info.performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                        parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }).start();
            info.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);

            return true;

        } else {
//            Toast.makeText(this, "找不到有效的节点", Toast.LENGTH_SHORT).show();
        }

        return false;

    }
 
Example 19
Source File: QueryController.java    From za-Farmer with MIT License 4 votes vote down vote up
private AccessibilityNodeInfo findNodePatternRecursive(
        UiSelector subSelector, AccessibilityNodeInfo fromNode, int index,
        UiSelector originalPattern) {

    if (subSelector.isMatchFor(fromNode, index)) {
        if(subSelector.isLeaf()) {
            if(mPatternIndexer == 0) {
                if (DEBUG)
                    Log.d(LOG_TAG, formatLog(
                            String.format("%s", subSelector.dumpToString(false))));
                return fromNode;
            } else {
                if (DEBUG)
                    Log.d(LOG_TAG, formatLog(
                            String.format("%s", subSelector.dumpToString(false))));
                mPatternCounter++; //count the pattern matched
                mPatternIndexer--; //decrement until zero for the instance requested

                // At a leaf selector within a group and still not instance matched
                // then reset the  selector to continue search from current position
                // in the accessibility tree for the next pattern match up until the
                // pattern index hits 0.
                subSelector = originalPattern;
                // starting over with next pattern search so reset to parent level
                mLogIndent = mLogParentIndent;
            }
        } else {
            if (DEBUG)
                Log.d(LOG_TAG, formatLog(
                        String.format("%s", subSelector.dumpToString(false))));

            if(subSelector.hasChildSelector()) {
                mLogIndent++; // next selector
                subSelector = subSelector.getChildSelector();
                if(subSelector == null) {
                    Log.e(LOG_TAG, "Error: A child selector without content");
                    return null;
                }
            } else if(subSelector.hasParentSelector()) {
                mLogIndent++; // next selector
                subSelector = subSelector.getParentSelector();
                if(subSelector == null) {
                    Log.e(LOG_TAG, "Error: A parent selector without content");
                    return null;
                }
                fromNode = fromNode.getParent();
                if(fromNode == null)
                    return null;
            }
        }
    }

    int childCount = fromNode.getChildCount();
    boolean hasNullChild = false;
    for (int i = 0; i < childCount; i++) {
        AccessibilityNodeInfo childNode = fromNode.getChild(i);
        if (childNode == null) {
            Log.w(LOG_TAG, String.format(
                    "AccessibilityNodeInfo returned a null child (%d of %d)", i, childCount));
            if (!hasNullChild) {
                Log.w(LOG_TAG, String.format("parent = %s", fromNode.toString()));
            }
            hasNullChild = true;
            continue;
        }
        if (!childNode.isVisibleToUser()) {
            if (DEBUG)
                Log.d(LOG_TAG,
                    String.format("Skipping invisible child: %s", childNode.toString()));
            continue;
        }
        AccessibilityNodeInfo retNode = findNodePatternRecursive(
                subSelector, childNode, i, originalPattern);
        if (retNode != null) {
            return retNode;
        }
    }
    return null;
}
 
Example 20
Source File: QueryController.java    From za-Farmer with MIT License 4 votes vote down vote up
private AccessibilityNodeInfo findNodeRegularRecursive(UiSelector subSelector,
        AccessibilityNodeInfo fromNode, int index) {

    if (subSelector.isMatchFor(fromNode, index)) {
        if (DEBUG) {
            Log.d(LOG_TAG, formatLog(String.format("%s",
                    subSelector.dumpToString(false))));
        }
        if(subSelector.isLeaf()) {
            return fromNode;
        }
        if(subSelector.hasChildSelector()) {
            mLogIndent++; // next selector
            subSelector = subSelector.getChildSelector();
            if(subSelector == null) {
                Log.e(LOG_TAG, "Error: A child selector without content");
                return null; // there is an implementation fault
            }
        } else if(subSelector.hasParentSelector()) {
            mLogIndent++; // next selector
            subSelector = subSelector.getParentSelector();
            if(subSelector == null) {
                Log.e(LOG_TAG, "Error: A parent selector without content");
                return null; // there is an implementation fault
            }
            // the selector requested we start at this level from
            // the parent node from the one we just matched
            fromNode = fromNode.getParent();
            if(fromNode == null)
                return null;
        }
    }

    int childCount = fromNode.getChildCount();
    boolean hasNullChild = false;
    for (int i = 0; i < childCount; i++) {
        AccessibilityNodeInfo childNode = fromNode.getChild(i);
        if (childNode == null) {
            Log.w(LOG_TAG, String.format(
                    "AccessibilityNodeInfo returned a null child (%d of %d)", i, childCount));
            if (!hasNullChild) {
                Log.w(LOG_TAG, String.format("parent = %s", fromNode.toString()));
            }
            hasNullChild = true;
            continue;
        }
        if (!childNode.isVisibleToUser()) {
            if (VERBOSE)
                Log.v(LOG_TAG,
                        String.format("Skipping invisible child: %s", childNode.toString()));
            continue;
        }
        AccessibilityNodeInfo retNode = findNodeRegularRecursive(subSelector, childNode, i);
        if (retNode != null) {
            return retNode;
        }
    }
    return null;
}