|
1 | 1 | package com.coderpig.wechathelper;
|
2 | 2 |
|
3 | 3 | import android.accessibilityservice.AccessibilityService;
|
| 4 | +import android.app.Notification; |
| 5 | +import android.app.PendingIntent; |
| 6 | +import android.os.Handler; |
| 7 | +import android.util.Log; |
4 | 8 | import android.view.accessibility.AccessibilityEvent;
|
| 9 | +import android.view.accessibility.AccessibilityNodeInfo; |
| 10 | + |
| 11 | +import java.util.List; |
5 | 12 |
|
6 | 13 | /**
|
7 | 14 | * 描述:
|
8 | 15 | *
|
9 | 16 | * @author CoderPig on 2018/04/04 13:46.
|
10 | 17 | */
|
11 | 18 |
|
12 |
| -public class HelperService extends AccessibilityService{ |
| 19 | +public class HelperService extends AccessibilityService { |
| 20 | + |
| 21 | + private static final String TAG = "HelperService"; |
| 22 | + private Handler handler = new Handler(); |
| 23 | + private String userName = "呵呵呵"; |
| 24 | + |
13 | 25 | @Override
|
14 | 26 | public void onAccessibilityEvent(AccessibilityEvent event) {
|
| 27 | + int eventType = event.getEventType(); |
| 28 | + CharSequence classNameChr = event.getClassName(); |
| 29 | + String className = classNameChr.toString(); |
| 30 | + Log.d(TAG, event.toString()); |
| 31 | + switch (eventType) { |
| 32 | + case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED: |
| 33 | + if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) { |
| 34 | + Notification notification = (Notification) event.getParcelableData(); |
| 35 | + String content = notification.tickerText.toString(); |
| 36 | + if (content.contains("请求添加你为朋友")) { |
| 37 | + PendingIntent pendingIntent = notification.contentIntent; |
| 38 | + try { |
| 39 | + pendingIntent.send(); |
| 40 | + } catch (PendingIntent.CanceledException e) { |
| 41 | + e.printStackTrace(); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + break; |
| 46 | + case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: |
| 47 | + Log.d(TAG, "TYPE_WINDOW_STATE_CHANGED"); |
| 48 | + switch (className) { |
| 49 | + case "com.tencent.mm.plugin.subapp.ui.friend.FMessageConversationUI": |
| 50 | + addFriend(); |
| 51 | + break; |
| 52 | + case "com.tencent.mm.plugin.profile.ui.SayHiWithSnsPermissionUI": |
| 53 | + verifyFriend(); |
| 54 | + break; |
| 55 | + case "com.tencent.mm.plugin.profile.ui.ContactInfoUI": |
| 56 | + performBackClick(); |
| 57 | + break; |
| 58 | + case "com.tencent.mm.ui.LauncherUI": |
| 59 | + if (!userName.equals("123")) { |
| 60 | + openGroup(); |
| 61 | + } |
| 62 | + break; |
| 63 | + case "com.tencent.mm.ui.contact.ChatroomContactUI": |
| 64 | + inviteGroup(); |
| 65 | + break; |
| 66 | + } |
| 67 | + break; |
| 68 | + case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: |
| 69 | + |
| 70 | + } |
| 71 | + } |
15 | 72 |
|
| 73 | + private void addFriend() { |
| 74 | + AccessibilityNodeInfo nodeInfo = getRootInActiveWindow(); |
| 75 | + if (nodeInfo != null) { |
| 76 | + List<AccessibilityNodeInfo> list = nodeInfo |
| 77 | + .findAccessibilityNodeInfosByText("接受"); |
| 78 | + if (list != null && list.size() > 0) { |
| 79 | + for (AccessibilityNodeInfo n : list) { |
| 80 | + n.performAction(AccessibilityNodeInfo.ACTION_CLICK); |
| 81 | + } |
| 82 | + } else { |
| 83 | + performBackClick(); |
| 84 | + } |
| 85 | + } |
16 | 86 | }
|
17 | 87 |
|
| 88 | + private void verifyFriend() { |
| 89 | + AccessibilityNodeInfo nodeInfo = getRootInActiveWindow(); |
| 90 | + //获得用户名 |
| 91 | + if (nodeInfo != null) { |
| 92 | + userName = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/d0n").get(0).getText().toString(); |
| 93 | + AccessibilityNodeInfo finishNode = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/hd").get(0); |
| 94 | + finishNode.performAction(AccessibilityNodeInfo.ACTION_CLICK); |
| 95 | + handler.postDelayed(new Runnable() { |
| 96 | + @Override |
| 97 | + public void run() { |
| 98 | + AccessibilityNodeInfo nodeInfo = getRootInActiveWindow(); |
| 99 | + if (nodeInfo != null) { |
| 100 | + recycle(nodeInfo); |
| 101 | + } |
| 102 | + } |
| 103 | + }, 500L); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private void openGroup() { |
| 108 | + AccessibilityNodeInfo nodeInfo = getRootInActiveWindow(); |
| 109 | + if (nodeInfo != null) { |
| 110 | + List<AccessibilityNodeInfo> nodes = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/ca5"); |
| 111 | + for (AccessibilityNodeInfo info : nodes) { |
| 112 | + if (info.getText().toString().equals("通讯录")) { |
| 113 | + info.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK); |
| 114 | + handler.postDelayed(new Runnable() { |
| 115 | + @Override |
| 116 | + public void run() { |
| 117 | + AccessibilityNodeInfo nodeInfo = getRootInActiveWindow(); |
| 118 | + if (nodeInfo != null) { |
| 119 | + List<AccessibilityNodeInfo> nodes = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/j5"); |
| 120 | + for (AccessibilityNodeInfo info : nodes) { |
| 121 | + if (info.getText().toString().equals("群聊")) { |
| 122 | + info.getParent().getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK); |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + }, 500L); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private void inviteGroup() { |
| 135 | + AccessibilityNodeInfo nodeInfo = getRootInActiveWindow(); |
| 136 | + if (nodeInfo != null) { |
| 137 | + List<AccessibilityNodeInfo> nodes = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/a9v"); |
| 138 | + for (AccessibilityNodeInfo info : nodes) { |
| 139 | + if(info.getText().toString().equals("小猪的Python学习交流群")) { |
| 140 | + Log.d(TAG, "inviteGroup: Test" + info.getParent().getParent().getClassName().toString()); |
| 141 | + info.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK); |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private void performBackClick() { |
| 149 | + handler.postDelayed(new Runnable() { |
| 150 | + @Override |
| 151 | + public void run() { |
| 152 | + performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK); |
| 153 | + } |
| 154 | + }, 300L); |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + //遍历控件的方法 |
| 159 | + public void recycle(AccessibilityNodeInfo info) { |
| 160 | + if (info.getChildCount() == 0) { |
| 161 | + Log.i(TAG, "child widget----------------------------" + info.getClassName().toString()); |
| 162 | + Log.i(TAG, "showDialog:" + info.canOpenPopup()); |
| 163 | + Log.i(TAG, "Text:" + info.getText()); |
| 164 | + Log.i(TAG, "windowId:" + info.getWindowId()); |
| 165 | + Log.i(TAG, "resId:" + info.getViewIdResourceName()); |
| 166 | + } else { |
| 167 | + for (int i = 0; i < info.getChildCount(); i++) { |
| 168 | + if (info.getChild(i) != null) { |
| 169 | + recycle(info.getChild(i)); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + |
18 | 176 | @Override
|
19 | 177 | public void onInterrupt() {
|
20 | 178 |
|
21 | 179 | }
|
| 180 | + |
| 181 | + |
22 | 182 | }
|
0 commit comments