下面列出了android.view.accessibility.AccessibilityNodeInfo#getViewIdResourceName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static List<AccessibilityNodeInfo> findNodeInfosById(AccessibilityNodeInfo nodeInfo, String id) {
if (TextUtils.isEmpty(id)) {
L.e("name is null" + id);
return nodeInfoList;
}
if (nodeInfo == null) {
L.e("nodeInfo is null " + nodeInfo);
return nodeInfoList;
}
// L.d("findNodeInfosByName "+nodeInfo.getText());
String viewIdResourceName = nodeInfo.getViewIdResourceName();
L.d("viewIdResourceName " + viewIdResourceName + " child Count " + nodeInfo.getChildCount());
if (id.equals(viewIdResourceName)) {
nodeInfoList.add(nodeInfo);
return nodeInfoList;
}
for (int i = 0; i < nodeInfo.getChildCount(); i++) {
findNodeInfosByName(nodeInfo.getChild(i), id);
}
return nodeInfoList;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static boolean isWechatRedEnvelopeOpenNode(AccessibilityNodeInfo info) {
if (info == null)
return false;
String residName = info.getViewIdResourceName();
if ("com.tencent.mm:id/amt".equals(residName)) {
if ("android.widget.Button".equals(info.getClassName())) {
return true;
/*AccessibilityNodeInfo infoChild22 = info.getChild(0);
XLog.e(TAG, "red main layout2 "+infoChild22.getChildCount());
if (infoChild22 != null && infoChild22.getChildCount() == 2 && "android.widget.RelativeLayout".equals(infoChild22.getClassName())) {
XLog.e(TAG, "red main layout3");
AccessibilityNodeInfo infoChild30 = infoChild22.getChild(0);
if (infoChild30 != null && "微信红包".equals(infoChild30.getText() == null ? "" : infoChild30.getText().toString())) {
XLog.e(TAG, "red main layout4");
return true;
}
}*/
}
}
return false;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static boolean isWechatRedEnvelopeNode(AccessibilityNodeInfo info) {
if (info == null)
return false;
String residName = info.getViewIdResourceName();
if ("com.tencent.mm:id/s_".equals(residName)) {
if ("android.widget.LinearLayout".equals(info.getClassName())) {// 是3,与层次图显示的不一致
return true;
/*AccessibilityNodeInfo infoChild22 = info.getChild(0);
XLog.e(TAG, "red main layout2 "+infoChild22.getChildCount());
if (infoChild22 != null && infoChild22.getChildCount() == 2 && "android.widget.RelativeLayout".equals(infoChild22.getClassName())) {
XLog.e(TAG, "red main layout3");
AccessibilityNodeInfo infoChild30 = infoChild22.getChild(0);
if (infoChild30 != null && "微信红包".equals(infoChild30.getText() == null ? "" : infoChild30.getText().toString())) {
XLog.e(TAG, "red main layout4");
return true;
}
}*/
}
}
return false;
}
private void initAccessibilityNodeInfo(AccessibilityNodeInfo info) {
this.className = StringUtil.nonNullString(info.getClassName());
this.packageName = StringUtil.nonNullString(info.getPackageName());
this.resourceId = info.getViewIdResourceName();
this.text = StringUtil.nonNullString(info.getText());
this.description = StringUtil.nonNullString(info.getContentDescription());
Rect rect = new Rect();
info.getBoundsInScreen(rect);
this.nodeBound = rect;
this.isScrollable = info.isScrollable();
this.visible = info.isVisibleToUser();
this.isClickable = info.isClickable();
this.isFocusable = info.isFocusable();
this.isEditable = info.isEditable();
}
public static SerializedAccessibilityNodeInfo serialize(AccessibilityNodeInfo node){
SerializedAccessibilityNodeInfo serializedNode = new SerializedAccessibilityNodeInfo();
Rect boundsInScreen = new Rect(), boundsInParent = new Rect();
if(node == null){
return null;
}
if(node.getClassName() != null)
serializedNode.className = node.getClassName().toString();
node.getBoundsInScreen(boundsInScreen);
node.getBoundsInParent(boundsInParent);
serializedNode.boundsInScreen = boundsInScreen.flattenToString();
serializedNode.boundsInParent = boundsInParent.flattenToString();
if(node.getContentDescription() != null)
serializedNode.contentDescription = node.getContentDescription().toString();
if(node.getText() != null){
serializedNode.text = node.getText().toString();
}
if(node.getViewIdResourceName() != null)
serializedNode.viewId = node.getViewIdResourceName();
int childCount = node.getChildCount();
for(int i = 0; i < childCount; i ++){
if(node.getChild(i) != null){
serializedNode.children.add(serialize(node.getChild(i)));
}
}
return serializedNode;
}
private static String print(AccessibilityNodeInfo nodeInfo) {
if (nodeInfo == null)
return "";
CharSequence text = nodeInfo.getText();
CharSequence description = nodeInfo.getContentDescription();
CharSequence className = nodeInfo.getClassName();
CharSequence packageName = nodeInfo.getPackageName();
boolean focusable = nodeInfo.isFocusable();
boolean clickable = nodeInfo.isClickable();
Rect rect = new Rect();
nodeInfo.getBoundsInScreen(rect);
String viewId = nodeInfo.getViewIdResourceName();
return "| "
+ "text: " + text + " \t"
+ "description: " + description + " \t"
+ String.format("%-40s", "ID: " + viewId) + " \t"
+ String.format("%-40s", "class: " + className) + " \t"
+ String.format("%-30s", "location: " + rect) + " \t"
// + "focusable: " + focusable + " \t"
// + "clickable: " + clickable + " \t"
// + String.format("%-30s", "package: " + packageName) + " \t"
;
}
public String getResourceId() {
String resourceId = "";
try {
/*
* Unfortunately UiObject does not implement a getResourceId method.
* There is currently no way to determine the resource-id of a given
* element represented by UiObject. Until this support is added to
* UiAutomater, we try to match the implementation pattern that is
* already used by UiObject for getting attributes using reflection.
* The returned string matches exactly what is displayed in the
* UiAutomater inspector.
*/
AccessibilityNodeInfo node = (AccessibilityNodeInfo) invoke(method(element.getClass(), "findAccessibilityNodeInfo", long.class),
element, Configurator.getInstance().getWaitForSelectorTimeout());
if (node == null) {
throw new UiObjectNotFoundException(element.getSelector().toString());
}
resourceId = node.getViewIdResourceName();
} catch (final Exception e) {
Logger.error("Exception: " + e + " (" + e.getMessage() + ")");
}
return resourceId;
}
public static String getViewIdResourceName(AccessibilityNodeInfo nodeInfo) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return nodeInfo.getViewIdResourceName();
}
return null;
}
protected void parser(AccessibilityNodeInfo group) {
if (group.getChildCount() == 0)
return;
int headIconPos = 0;
int messagePos = 0;
subName = "";
message = "";
isRecalledMsg = false;
for (int j = 0; j < group.getChildCount(); j++) {
AccessibilityNodeInfo child = group.getChild(j);
if (child == null) {
Log.d(TAG, "parser: child is null, continue");
continue;
}
String nodeId = child.getViewIdResourceName();
if (nodeId == null) {
Log.d(TAG, "parser: node ID is null, continue");
continue;
}
switch (nodeId) {
case IdHeadIcon:
//头像图标
headIconPos = j;
break;
case IdChatItem:
switch (child.getClassName() + "") {
case "android.widget.RelativeLayout":
if (child.getChildCount() != 0) {
if (child.getContentDescription() != null) {
redPegNode = child;
message = "红包";
//红包或者是分享
Log.d(TAG, "content_layout: 红包");
}
} else {
message = "[图片]";
Log.d(TAG, "content_layout: 图片");
}
break;
case "android.widget.LinearLayout": {
if (child.getChildCount() == 2) {
AccessibilityNodeInfo child1 = child.getChild(0);
AccessibilityNodeInfo child2 = child.getChild(1);
if (child1 != null && "android.widget.RelativeLayout".contentEquals(child1.getClassName())) {
if (child2 != null && "android.widget.TextView".contentEquals(child2.getClassName())) {
// message = "回复 " + child1.getText() + ": \n" + child2.getText();
message = child2.getText() + "";
}
}
}
Log.d(TAG, "content_layout: 回复消息");
}
break;
case "android.widget.TextView": {
message = child.getText() + "";
Log.v(TAG, "content_layout: 普通文本");
}
break;
}
messagePos = j;
break;
case IdNickName:
subName = child.getText() + "";
break;
case IdGrayBar:
message = child.getText() + "";
Log.w(TAG, "parser: message: " + message);
int indexOfRecall = message.indexOf(RECALL);
if (indexOfRecall >= 0) {
isRecalledMsg = true;
subName = message.substring(0, indexOfRecall);
message = RECALL;
if ("对方".equals(subName))
subName = title;
else if ("你".equals(subName))
subName = "我";
Log.v(TAG, "content_layout: 灰底文本");
}
Log.w(TAG, "parser: " + indexOfRecall + " " + RECALL + " " + message);
break;
}
}
if (messagePos < headIconPos)
// 消息在头像左边
subName = "我";
else if ("".equals(subName))
// 两人聊天时 没有subName
subName = title;
Log.v(TAG, "parser: " + title + " - " + subName + " : " + message);
}
/**
* 好友:
* 姓名 1 android.widget.TextView
* 消息 5-[]-last android.widget.TextView focusable
* 文本框 7-0 android.widget.EditText
* 发送按钮 7-1 android.widget.Button
* <p>
* 群:
* 群名 1
* 消息 4/5-
* <p>
* 群里的临时会话:
* 姓名 1-0
* 消息 6-[]-last
* 文本框 8-0
* 发送按钮 8-1
*/
protected boolean init(AccessibilityNodeInfo root) {
//16 是其他界面
//14 是没有聊过天
//12 是发送完消息
if (root.getChildCount() < 12) {
Log.v(TAG, "init: root.childCount: " + root.getChildCount());
return false;
}
// List<AccessibilityNodeInfo> inputList;
// List<AccessibilityNodeInfo> sendList;
// inputList = root.findAccessibilityNodeInfosByViewId(IdInput);
// sendList = root.findAccessibilityNodeInfosByViewId(IdSend);
// if (inputList.size() == 0) {
// Log.d(TAG, "init: input is null, return");
// return false;
// }
// if (sendList.size() == 0) {
// Log.d(TAG, "init: send button is null, return");
// return false;
// }
// inputNode = inputList.get(0);
// sendBtnNode = sendList.get(0);
List<AccessibilityNodeInfo> titleList;
titleList = root.findAccessibilityNodeInfosByViewId(IdTitle);
if (titleList.size() == 0) {
Log.d(TAG, "init: title is null, return");
return false;
}
titleNode = titleList.get(0);
if (titleNode.getText() == null) {
Log.d(TAG, "init: name is null,return");
return false;
}
title = titleNode.getText() + "";
isOtherMsg = false;
for (int i = 4; i < 7; i++) {
AccessibilityNodeInfo child = root.getChild(i);
String name = child.getViewIdResourceName();
if (name == null)
continue;
switch (name) {
case IdChatGroupView:
chatGroupViewNode = child;
break;
case IdOtherMsg:
otherMsgNode = child;
isOtherMsg = true;
break;
}
}
if (chatGroupViewNode == null) {
Log.i(TAG, "init: chatGroupViewNode is null, return");
return false;
}
return true;
}
Builder(int id, @Nullable ViewHierarchyElementAndroid parent, AccessibilityNodeInfo fromInfo) {
// Bookkeeping
this.id = id;
this.parentId = (parent != null) ? parent.getId() : null;
// API 18+ properties
this.resourceName = AT_18 ? fromInfo.getViewIdResourceName() : null;
this.editable = AT_18 ? fromInfo.isEditable() : null;
// API 16+ properties
this.visibleToUser = AT_16 ? fromInfo.isVisibleToUser() : null;
// API 21+ properties
if (AT_21) {
ImmutableList.Builder<ViewHierarchyActionAndroid> actionBuilder =
new ImmutableList.Builder<>();
actionBuilder.addAll(
Lists.transform(
fromInfo.getActionList(),
action -> ViewHierarchyActionAndroid.newBuilder(action).build()));
this.actionList = actionBuilder.build();
}
// API 24+ properties
this.drawingOrder = AT_24 ? fromInfo.getDrawingOrder() : null;
// API 29+ properties
this.hasTouchDelegate = AT_29 ? (fromInfo.getTouchDelegateInfo() != null) : null;
// Base properties
this.className = fromInfo.getClassName();
this.packageName = fromInfo.getPackageName();
this.accessibilityClassName = fromInfo.getClassName();
this.contentDescription = SpannableStringAndroid.valueOf(fromInfo.getContentDescription());
this.text = SpannableStringAndroid.valueOf(fromInfo.getText());
this.importantForAccessibility = true;
this.clickable = fromInfo.isClickable();
this.longClickable = fromInfo.isLongClickable();
this.focusable = fromInfo.isFocusable();
this.scrollable = fromInfo.isScrollable();
this.canScrollForward =
((fromInfo.getActions() & AccessibilityNodeInfo.ACTION_SCROLL_FORWARD) != 0);
this.canScrollBackward =
((fromInfo.getActions() & AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD) != 0);
this.checkable = fromInfo.isCheckable();
this.checked = fromInfo.isChecked();
this.touchDelegateBounds = new ArrayList<>(); // Populated after construction
android.graphics.Rect tempRect = new android.graphics.Rect();
fromInfo.getBoundsInScreen(tempRect);
this.boundsInScreen = new Rect(tempRect.left, tempRect.top, tempRect.right, tempRect.bottom);
this.nonclippedHeight = null;
this.nonclippedWidth = null;
this.textSize = null;
this.textColor = null;
this.backgroundDrawableColor = null;
this.typefaceStyle = null;
this.enabled = fromInfo.isEnabled();
}