/** 收到聊天里的红包 */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private void handleChatListHongBao() { AccessibilityNodeInfo nodeInfo = getService().getRootInActiveWindow(); if (nodeInfo == null) { Log.w(TAG, "rootWindow为空"); return; } List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText("领取红包"); if (list != null && list.isEmpty()) { // 从消息列表查找红包 list = nodeInfo.findAccessibilityNodeInfosByText("[微信红包]"); if (list == null || list.isEmpty()) { return; } for (AccessibilityNodeInfo n : list) { if (BuildConfig.DEBUG) { Log.i(TAG, "-->微信红包:" + n); } n.performAction(AccessibilityNodeInfo.ACTION_CLICK); break; } } else if (list != null) { // 最新的红包领起 for (int i = list.size() - 1; i >= 0; i--) { AccessibilityNodeInfo parent = list.get(i).getParent(); if (BuildConfig.DEBUG) { Log.i(TAG, "-->领取红包:" + parent); } if (parent != null) { if (isFirstChecked) { parent.performAction(AccessibilityNodeInfo.ACTION_CLICK); isFirstChecked = false; } break; } } } }
/** 点击聊天里的红包后,显示的界面 */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void handleLuckyMoneyReceive() { AccessibilityNodeInfo nodeInfo = getService().getRootInActiveWindow(); if (nodeInfo == null) { Log.w(TAG, "rootWindow为空"); return; } AccessibilityNodeInfo targetNode = null; List<AccessibilityNodeInfo> list = null; int event = getConfig().getWechatAfterOpenHongBaoEvent(); if (event == Config.WX_AFTER_OPEN_HONGBAO) { // 拆红包 if (getWechatVersion() < USE_ID_MIN_VERSION) { list = nodeInfo.findAccessibilityNodeInfosByText("拆红包"); } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { if (getWechatVersion() < WECHAT_6_3_11_VERSION) { list = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/b2c"); } else { list = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/b43"); } } if (list == null || list.isEmpty()) { List<AccessibilityNodeInfo> l = nodeInfo.findAccessibilityNodeInfosByText("给你发了一个红包"); if (l != null && !l.isEmpty()) { AccessibilityNodeInfo p = l.get(0).getParent(); if (p != null) { for (int i = 0; i < p.getChildCount(); i++) { AccessibilityNodeInfo node = p.getChild(i); if ("android.widget.Button".equals(node.getClassName())) { targetNode = node; break; } } } } } } } else if (event == Config.WX_AFTER_OPEN_SEE) { // 看一看 if (getWechatVersion() < USE_ID_MIN_VERSION) { // 低版本才有 看大家手气的功能 list = nodeInfo.findAccessibilityNodeInfosByText("看看大家的手气"); } } if (list != null && !list.isEmpty()) { targetNode = list.get(0); } if (targetNode != null) { final AccessibilityNodeInfo n = targetNode; long sDelayTime = getConfig().getWechatOpenDelayTime(); if (sDelayTime != 0) { getHandler() .postDelayed( new Runnable() { @Override public void run() { n.performAction(AccessibilityNodeInfo.ACTION_CLICK); } }, sDelayTime); } else { n.performAction(AccessibilityNodeInfo.ACTION_CLICK); } } }
@LargeTest public void testPerformAccessibilityActionClearSelection() throws Exception { final long startTimeMillis = SystemClock.uptimeMillis(); try { // find a view and make sure it is not selected AccessibilityNodeInfo button = mUiTestAutomationBridge.findAccessibilityNodeInfoByViewIdInActiveWindow(R.id.button5); assertFalse(button.isSelected()); // select the view assertTrue(button.performAction(ACTION_SELECT)); // find the view again and make sure it is selected button = mUiTestAutomationBridge.findAccessibilityNodeInfoByViewIdInActiveWindow(R.id.button5); assertTrue(button.isSelected()); // unselect the view assertTrue(button.performAction(ACTION_CLEAR_SELECTION)); // find the view again and make sure it is not selected button = mUiTestAutomationBridge.findAccessibilityNodeInfoByViewIdInActiveWindow(R.id.button5); assertFalse(button.isSelected()); } finally { if (DEBUG) { final long elapsedTimeMillis = SystemClock.uptimeMillis() - startTimeMillis; Log.i(LOG_TAG, "testPerformAccessibilityActionClearSelection: " + elapsedTimeMillis + "ms"); } } }
@Override public void onAccessibilityEvent(AccessibilityService service, AccessibilityEvent event) { Log.d(TAG, "event: " + event); // Log.d(TAG, "event: " + toString(event)); AccessibilityNodeInfo node = event.getSource(); CharSequence packageName = event.getPackageName(); int type = event.getEventType(); int windowId = event.getWindowId(); if (null == node) { Log.w(TAG, "node is null, ^~^"); return; } if ("com.android.settings".equals(packageName)) { // http://stackoverflow.com/questions/3873659/android-how-can-i-get-the-current-foreground-activity-from-a-service if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { mLastComponentName = new ComponentName(event.getPackageName().toString(), event.getClassName().toString()); Log.i(TAG, "last activity: " + mLastComponentName); // from QueryController if (event.getText() != null && event.getText().size() > 0) { if (event.getText().get(0) != null) { String lastActivityName = event.getText().get(0).toString(); // Log.i(TAG, "last activity name: " + lastActivityName); } } } if (AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED == type) { AccessibilityNodeInfo rootNode = node; rootNode = service.getRootInActiveWindow(); List<AccessibilityNodeInfo> nodes = rootNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/dashboard"); // nodes = rootNode.findAccessibilityNodeInfosByText("设置"); boolean settingActivity = nodes != null && nodes.size() > 0; nodes = rootNode.findAccessibilityNodeInfosByText("开发者选项"); boolean devActivity = nodes != null && nodes.size() == 1; settingActivity = new ComponentName("com.android.settings", "com.android.settings.Settings") .equals(mLastComponentName); devActivity = new ComponentName("com.android.settings", "com.android.settings.SubSettings") .equals(mLastComponentName); Log.d(TAG, "settingActivity: " + settingActivity + " devActivity: " + devActivity); if (settingActivity) { Log.d(TAG, "scroll to bottom"); nodes = rootNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/dashboard"); nodes.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); nodes.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); nodes.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); nodes.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); nodes = rootNode.findAccessibilityNodeInfosByText("开发者选项"); if (null != nodes && nodes.size() > 0) { Log.d(TAG, "open developer's setting"); nodes.get(0).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK); } } else if (devActivity) { nodes = rootNode.findAccessibilityNodeInfosByText("开启"); AccessibilityNodeInfo open = AccessibilityNodeInfoFinder.find(rootNode, new UiSelector().text("开启")); Log.d(TAG, "open: " + open); open = open.getParent().getChild(1); if (!open.isChecked()) { // open.setChecked(true); open.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK); } else { Log.i(TAG, "adb is open yet."); } AccessibilityNodeInfo usb = AccessibilityNodeInfoFinder.find(rootNode, new UiSelector().text("USB调试")); if (null == usb) { AccessibilityNodeInfo listview = AccessibilityNodeInfoFinder.find( rootNode, new UiSelector().className("android.widget.ListView")); listview.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); } usb = AccessibilityNodeInfoFinder.find(rootNode, new UiSelector().text("USB调试")); Log.i(TAG, "usb: " + usb); Log.i(TAG, "p: " + usb.getParent()); Log.i(TAG, "pp: " + usb.getParent().getParent()); AccessibilityNodeInfo debug = usb.getParent().getParent().getChild(1).getChild(0); Log.i(TAG, "debug: " + debug); debug = AccessibilityNodeInfoFinder.find( rootNode, new UiSelector() .className("android.widget.Switch") .fromParent(new UiSelector().className("android.widget.LinearLayout"))); Log.i(TAG, "debug: " + debug); usb = AccessibilityNodeInfoFinder.find(rootNode, new UiSelector().text("调试")); int index = 0; for (int i = 0; i < usb.getParent().getChildCount(); i++) { if ("调试".equals(usb.getParent().getChild(i).getText())) { index = i; break; } } usb = usb.getParent().getChild(index + 1); Log.i(TAG, "debug: " + debug); // if (!debug.isChecked()) { // debug.performAction(AccessibilityNodeInfo.ACTION_CLICK); // } else { // Log.i(TAG, "adb debug is open yet."); // } // if (null != nodes && nodes.size() > 0) { // Log.d(TAG, nodes.size() + " node found."); // Log.d(TAG, "open developer's option"); // // nodes.get(0).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK); // } else { // return; // } } } } // node.recycle(); }
@Override public void onAccessibilityEvent(AccessibilityService service, AccessibilityEvent event) { Log.d(TAG, "event: " + event); // Log.d(TAG, "event: " + toString(event)); AccessibilityNodeInfo node = event.getSource(); CharSequence packageName = event.getPackageName(); int type = event.getEventType(); int windowId = event.getWindowId(); if (null == node) { Log.w(TAG, "node is null, ^~^"); return; } if ("com.android.settings".equals(packageName)) { // http://stackoverflow.com/questions/3873659/android-how-can-i-get-the-current-foreground-activity-from-a-service if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { mLastComponentName = new ComponentName(event.getPackageName().toString(), event.getClassName().toString()); Log.i(TAG, "last activity: " + mLastComponentName); // from QueryController if (event.getText() != null && event.getText().size() > 0) { if (event.getText().get(0) != null) { String lastActivityName = event.getText().get(0).toString(); // Log.i(TAG, "last activity name: " + lastActivityName); } } boolean appDetail = new ComponentName( "com.android.settings", "com.android.settings.applications.InstalledAppDetailsTop") .equals(mLastComponentName); boolean stopConfirm = new ComponentName("com.android.settings", "android.app.AlertDialog") .equals(mLastComponentName); Log.d(TAG, "appDetail: " + appDetail + " stopConfirm: " + stopConfirm); if (appDetail) { AccessibilityNodeInfo stop = AccessibilityNodeInfoFinder.find( service.getRootInActiveWindow(), new UiSelector().text("强行停止")); if (stop.isEnabled()) { stop.performAction(AccessibilityNodeInfo.ACTION_CLICK); } else { Log.i(TAG, "app has stop yet."); } } if (stopConfirm) { AccessibilityNodeInfo confirm = AccessibilityNodeInfoFinder.find( service.getRootInActiveWindow(), new UiSelector().text("确定")); confirm.performAction(AccessibilityNodeInfo.ACTION_CLICK); Intent self = new Intent(service, MainActivity.class); self.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); service.startActivity(self); } } } }