下面列出了android.view.accessibility.AccessibilityNodeInfo#getContentDescription ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean checkCriteria(AccessibilityNodeInfo node) {
String text = "";
String desc = "";
if (node.getText() != null) {
text = node.getText().toString();
}
if (node.getContentDescription() != null) {
desc = node.getContentDescription().toString();
}
if (this.checkValue != null) {
return text.equals(this.checkValue) || desc.equals(this.checkValue);
} else if (this.checkPattern != null) {
return this.checkPattern.matcher(text).matches() || this.checkPattern.matcher(desc).matches();
}
return false;
}
private boolean watchList(AccessibilityEvent event) {
// Not a message
if ((event.getEventType() != AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && event.getEventType() != AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) || event.getSource() == null)
return false;
List<AccessibilityNodeInfo> nodes = event.getSource().findAccessibilityNodeInfosByText(WECHAT_NOTIFICATION_TIP);
if (!nodes.isEmpty()) {
CharSequence contentDescription;
AccessibilityNodeInfo nodeToClick;
synchronized (this) {
nodeToClick = nodes.get(0);
if (nodeToClick == null) return false;
contentDescription = nodeToClick.getContentDescription();
if (contentDescription != null)
if (!lastContentDescription.equals(contentDescription)) {
nodeToClick.performAction(AccessibilityNodeInfo.ACTION_CLICK);
lastContentDescription = contentDescription.toString();
return true;
}
}
}
return false;
}
@NonNull
private String[] getSenderContentDescriptionFromNode(AccessibilityNodeInfo node) {
int count = node.getChildCount();
String[] result = {"unknownSender", "unknownTime"};
for (int i = 0; i < count; i++) {
AccessibilityNodeInfo thisNode = node.getChild(i);
if ("android.widget.ImageView".equals(thisNode.getClassName())) {
CharSequence contentDescription = thisNode.getContentDescription();
if (contentDescription != null) result[0] = contentDescription.toString();
} else if ("android.widget.TextView".equals(thisNode.getClassName())) {
CharSequence thisNodeText = thisNode.getText();
if (thisNodeText != null) result[1] = thisNodeText.toString();
}
}
return result;
}
/**
* 描述:处理口令红包
* 作者:卜俊文
* 邮箱:[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);
}
}
}
}
}
}
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"
;
}
/**
* Gets the text of a <code>node</code> by returning the content description
* (if available) or by returning the text.
*
* @param node The node.
* @return The node text.
*/
public static CharSequence getNodeText(AccessibilityNodeInfo node) {
if (node == null) {
return null;
}
CharSequence text = node.getText();
if (!TextUtils.isEmpty(text)
&& (TextUtils.getTrimmedLength(text) > 0)) {
return text;
}
//Use contentDescription only if there are no child nodes -
if (node.getChildCount() == 0) {
final CharSequence contentDescription = node.getContentDescription();
if (!TextUtils.isEmpty(contentDescription)
&& (TextUtils.getTrimmedLength(contentDescription) > 0)) {
return contentDescription;
}
}
return null;
// // Prefer content description over text.
// // TODO: Why are we checking the trimmed length?
// final CharSequence contentDescription = node.getContentDescription();
// if (!TextUtils.isEmpty(contentDescription)
// && (TextUtils.getTrimmedLength(contentDescription) > 0)) {
// return contentDescription;
// }
//
// CharSequence text = node.getText();
// if (!TextUtils.isEmpty(text)
// && (TextUtils.getTrimmedLength(text) > 0)) {
// return text;
// }
//
// return null;
}
private boolean findEditText(AccessibilityNodeInfo rootNode, String content)
{
int count = rootNode.getChildCount();
// android.util.Log.d("maptrix", "root class=" + rootNode.getClassName() + "," + rootNode.getText() + "," + count);
for (int i = 0; i < count; i++)
{
AccessibilityNodeInfo nodeInfo = rootNode.getChild(i);
if (nodeInfo == null)
{
// android.util.Log.d("maptrix", "nodeinfo = null");
continue;
}
// android.util.Log.d("maptrix", "class=" + nodeInfo.getClassName());
// android.util.Log.e("maptrix", "ds=" + nodeInfo.getContentDescription());
if (nodeInfo.getContentDescription() != null)
{
int nindex = nodeInfo.getContentDescription().toString().indexOf(name);
int cindex = nodeInfo.getContentDescription().toString().indexOf(scontent);
// android.util.Log.e("maptrix", "nindex=" + nindex + " cindex=" + cindex);
if (nindex != -1)
{
itemNodeinfo = nodeInfo;
// android.util.Log.i("maptrix", "find node info");
}
}
if ("android.widget.EditText".equals(nodeInfo.getClassName()))
{
// android.util.Log.i("maptrix", "==================");
Bundle arguments = new Bundle();
arguments.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD);
arguments.putBoolean(AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN,
true);
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
arguments);
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
ClipData clip = ClipData.newPlainText("label", content);
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClip(clip);
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_PASTE);
return true;
}
if (findEditText(nodeInfo, content))
{
return true;
}
}
return false;
}
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);
}