String toString(AccessibilityEvent event) { String str = ""; str += "eventtype: " + AccessibilityEvent.eventTypeToString(event.getEventType()); str += "\nsource: " + event.getSource(); str += "\npackagename: " + event.getPackageName(); str += "\nclassname: " + event.getClassName(); return str; }
@Override public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) { try { if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean("usexposed", false)) { AccessibilityNodeInfo ani = accessibilityEvent.getSource(); String command = accessibilityNodeInfoRecursion(ani); if (command != null) { if ((lastCommand + timeOut) < accessibilityEvent.getEventTime() && CommandInterpreter.interpret(this, command, true)) { lastCommand = accessibilityEvent.getEventTime(); } Log.v("command", command); } } } catch (Exception e) { e.printStackTrace(); } }
@LargeTest public void testAccessibilityEventGetSource() throws Exception { final long startTimeMillis = SystemClock.uptimeMillis(); try { // find a view and make sure it is not focused final AccessibilityNodeInfo button = mUiTestAutomationBridge.findAccessibilityNodeInfoByViewIdInActiveWindow(R.id.button5); assertFalse(button.isFocused()); AccessibilityEvent event = mUiTestAutomationBridge.executeCommandAndWaitForAccessibilityEvent( new Runnable() { @Override public void run() { // focus the view assertTrue(button.performAction(ACTION_FOCUS)); } }, new Predicate<AccessibilityEvent>() { @Override public boolean apply(AccessibilityEvent event) { return (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && event.getPackageName().equals(getActivity().getPackageName()) && event.getText().get(0).equals(button.getText())); } }, TIMEOUT_PROPAGATE_ACCESSIBILITY_EVENT_MILLIS); // check the last event assertNotNull(event); // check that last event source AccessibilityNodeInfo source = event.getSource(); assertNotNull(source); // bounds Rect buttonBounds = new Rect(); button.getBoundsInParent(buttonBounds); Rect sourceBounds = new Rect(); source.getBoundsInParent(sourceBounds); assertEquals(buttonBounds.left, sourceBounds.left); assertEquals(buttonBounds.right, sourceBounds.right); assertEquals(buttonBounds.top, sourceBounds.top); assertEquals(buttonBounds.bottom, sourceBounds.bottom); // char sequence attributes assertEquals(button.getPackageName(), source.getPackageName()); assertEquals(button.getClassName(), source.getClassName()); assertEquals(button.getText(), source.getText()); assertSame(button.getContentDescription(), source.getContentDescription()); // boolean attributes assertSame(button.isFocusable(), source.isFocusable()); assertSame(button.isClickable(), source.isClickable()); assertSame(button.isEnabled(), source.isEnabled()); assertNotSame(button.isFocused(), source.isFocused()); assertSame(button.isLongClickable(), source.isLongClickable()); assertSame(button.isPassword(), source.isPassword()); assertSame(button.isSelected(), source.isSelected()); assertSame(button.isCheckable(), source.isCheckable()); assertSame(button.isChecked(), source.isChecked()); } finally { if (DEBUG) { final long elapsedTimeMillis = SystemClock.uptimeMillis() - startTimeMillis; Log.i(LOG_TAG, "testAccessibilityEventGetSource: " + 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); } } } }