예제 #1
0
    @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();
    }
예제 #2
0
    @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);
          }
        }
      }
    }