Runt
2022-03-29 9aeecdbe43d4c4601710b6c7b301f41f55c07746
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.auto.lyric.service;
 
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityWindowInfo;
import android.widget.Toast;
 
import java.util.List;
 
/**
 * Created by Runt (qingingrunt2010@qq.com) on 2022/3/20.
 */
public class AutoInputService extends AccessibilityService {
    final String TAG = "AutoInputService";
    public static final int ACTION_SEND = 1001,//发送
            ACTION_PASTE = 1002;//粘贴
 
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int action = intent.getIntExtra("action", 0);
            Log.e(TAG, "onReceive action: " + action);
            switch (action){
                case ACTION_SEND:
                    //List<AccessibilityNodeInfo> nodeInfos = getRootInActiveWindow().findAccessibilityNodeInfosByViewId("com.ss.android.ugc.aweme:id/llp");
                    AccessibilityNodeInfo sendNode = getViewByDesc(getRootInActiveWindow(),"发送");
                    Log.e(TAG, "ACTION_SEND nodeInfos: " + sendNode);
                    if(sendNode != null){
                        Log.e(TAG, "ACTION_SEND nodeInfos: " + sendNode.getClassName());
                        sendNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    }else{
                        printView(getRootInActiveWindow()," ");
                        Toast.makeText(getBaseContext(),"未找到控件",Toast.LENGTH_SHORT).show();
                    }
                    break;
                case ACTION_PASTE:
                    List<AccessibilityNodeInfo> list = getRootInActiveWindow().findAccessibilityNodeInfosByText("说点什么...");
                    Log.e(TAG, "ACTION_PASTE nodeInfos: " + list.size());
                    printView(getRootInActiveWindow()," ");
                    AccessibilityNodeInfo editNode = getView(getRootInActiveWindow(),"android.widget.EditText");
                    if(editNode != null){
                        Log.e(TAG, "ACTION_PASTE nodeInfos: " + editNode.getClassName());
                        editNode.performAction(AccessibilityNodeInfo.ACTION_PASTE);
                    }else{
                        Toast.makeText(getBaseContext(),"未找到控件",Toast.LENGTH_SHORT).show();
                    }
 
                    break;
            }
        }
    };
 
    static  boolean flag = false , isFront;
 
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        int eventType = event.getEventType();
        Log.d(TAG," text:" + event.getText()+" class:"+event.getClassName() +" action:" + event.getAction() +" windowId:"+ event.getWindowId());
        Log.d(TAG,"event:"+event );
        Log.d(TAG,"ParcelableData:"+event.getParcelableData()  );
        AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
        AccessibilityNodeInfo source = event.getSource();//当前界面的可访问节点信息
        if(source != null){
            //printView(source,"source == ");
        }
        Log.d(TAG,"source:"+(source == null? null : source.getClassName())  );
        Log.d(TAG,"nodeInfo:"+ (nodeInfo == null? null : nodeInfo.getClassName()) );
        switch (eventType) {
            case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED://界面变化事件
                Log.e(TAG, "TYPE_WINDOW_CONTENT_CHANGED " );
                break;
            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED://界面变化事件
                Log.e(TAG, "TYPE_WINDOW_STATE_CHANGED " );
                if(source != null && event.getClassName().toString().equals("com.ss.android.ugc.aweme.live.LivePlayActivity")){
                    List<AccessibilityNodeInfo> list = getRootInActiveWindow().findAccessibilityNodeInfosByText("说点什么...");
                    Log.e(TAG,"获取到控件 "+list.size());
                    if (list != null && list.size() > 0 && flag) {
                        //模拟第三方点击事件
                        //找到你的节点以后 就直接点击他就行了
                        //AccessibilityNodeInfo node = list.get(0);
                        list.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                            Log.e(TAG,"执行点击 "+list.get(0).getPaneTitle());
                        }
                    }else if(flag){
                        Toast.makeText(getBaseContext(),"未找到控件",Toast.LENGTH_SHORT).show();
                    }
                }
 
                if(event.getText().contains("更多选项") ){
                    //printView(nodeInfo," ");
                    //performGlobalAction(GLOBAL_ACTION_RECENTS); // 任务管理器
                    //performGlobalAction(GLOBAL_ACTION_BACK); // 返回安装页面
                }
                break;
            case AccessibilityEvent.TYPE_VIEW_FOCUSED://焦点变更
                Log.e(TAG, "TYPE_VIEW_FOCUSED ResourceName:" +( source == null ? null : source.getParent()));
                if(event.getClassName().equals("android.widget.EditText") && source != null){
                    //source.performAction(AccessibilityNodeInfo.ACTION_LONG_CLICK);
                    //source.performAction(AccessibilityNodeInfo.ACTION_PASTE);//粘贴
                }
                break;
            case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED:
                Log.e(TAG, "TYPE_VIEW_LONG_CLICKED ResourceName:" +( source == null ? null : source.getParent()));
                break;
            case AccessibilityEvent.TYPE_VIEW_CLICKED:
                Log.e(TAG, "TYPE_VIEW_CLICKED " );
                if(nodeInfo != null) {
                    Log.e(TAG, "TYPE_VIEW_CLICKED source:" + nodeInfo.getText() + " " + nodeInfo.getClass());
                }
                /**/
                //界面点击
                break;
            case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
                //界面文字改动
                Log.e(TAG, "TYPE_VIEW_TEXT_CHANGED " );
                break;
        }
 
    }
 
    @SuppressLint("NewApi")
    private void printView(AccessibilityNodeInfo node, String space){
        Log.e(TAG,space +node+" id:"+node.getViewIdResourceName() +" class:"+node.getClassName()+" text:"+node.getText()+" "+node.getPaneTitle());
        if(node.getChildCount() > 0){
            for(int i = 0 ; i < node.getChildCount() ; i ++){
                if(node.getChild(i) != null) {
                    printView(node.getChild(i), space + " \t");
                }
            }
        }
    }
    @SuppressLint("NewApi")
    private AccessibilityNodeInfo getView(AccessibilityNodeInfo node, String className){
        Log.e(TAG,className +" id:"+node.getViewIdResourceName() +" class:"+node.getClassName()+" text:"+node.getText()+" "+node.getPaneTitle());
        if(node.getChildCount() > 0){
            for(int i = 0 ; i < node.getChildCount() ; i ++){
                if(node.getChild(i) != null) {
                    AccessibilityNodeInfo nodeInfo = getView(node.getChild(i), className);
                    if(nodeInfo != null){
                        return nodeInfo;
                    }
                }
            }
        }else if(node.getClassName().equals(className)){
            return node;
        }
        return null;
    }
 
    @SuppressLint("NewApi")
    private AccessibilityNodeInfo getViewByDesc(AccessibilityNodeInfo node, String desc){
        Log.e(TAG,desc +" getViewByDesc id:"+node.getViewIdResourceName() +" class:"+node.getClassName()+" text:"+node.getText()+" "+node.getContentDescription());
        Log.e(TAG,"getViewByDesc  node:"+node);
        if(node.getChildCount() > 0){
            for(int i = 0 ; i < node.getChildCount() ; i ++){
                if(node.getChild(i) != null) {
                    AccessibilityNodeInfo nodeInfo = getViewByDesc(node.getChild(i), desc);
                    if(nodeInfo != null){
                        return nodeInfo;
                    }
                }
            }
        }else if(node != null && node.getContentDescription() != null && node.getContentDescription().equals(desc)){
            return node;
        }
        return null;
    }
    private void printWindow(AccessibilityWindowInfo window, String space){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Log.e(TAG,space +" id:"+window.getId() +" class:"+window.getClass().getName()+" text:"+window.getTitle()+" "+window.getRoot());
            printView(window.getRoot()," window node ");
        }
        if(window.getChildCount() > 0){
            for(int i = 0 ; i < window.getChildCount() ; i ++){
                if(window.getChild(i) != null) {
                    printWindow(window.getChild(i),space + " \t");
                }
            }
        }
    }
 
    @Override
    public void onInterrupt() {
        unregisterReceiver(receiver);
    }
    @Override
    protected void onServiceConnected() {
        AccessibilityServiceInfo serviceInfo = new AccessibilityServiceInfo();
        serviceInfo.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
        serviceInfo.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
        serviceInfo.packageNames = new String[]{/*"com.tencent.mm",*/"com.ss.android.ugc.aweme"/*,"com.runt.autotext"*/};
        serviceInfo.notificationTimeout=100;
        serviceInfo.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
 
        setServiceInfo(serviceInfo);
        IntentFilter filter = new IntentFilter();
        filter.addAction(AutoInputService.class.getName());
        registerReceiver(receiver,filter);
    }
}