From 7b9daa6f5e8f622c055231a8baef3214df99846c Mon Sep 17 00:00:00 2001
From: Runt <qingingrunt2010@qq.com>
Date: Wed, 11 May 2022 13:02:09 +0000
Subject: [PATCH] 发送歌词功能 优化

---
 app/src/main/java/com/auto/lyric/service/AutoInputService.java |   90 +++++++++++++++++++++++++++------------------
 1 files changed, 54 insertions(+), 36 deletions(-)

diff --git a/app/src/main/java/com/auto/lyric/service/AutoInputService.java b/app/src/main/java/com/auto/lyric/service/AutoInputService.java
index 73c3ea9..c68f2c6 100644
--- a/app/src/main/java/com/auto/lyric/service/AutoInputService.java
+++ b/app/src/main/java/com/auto/lyric/service/AutoInputService.java
@@ -4,6 +4,7 @@
 import android.accessibilityservice.AccessibilityServiceInfo;
 import android.annotation.SuppressLint;
 import android.content.BroadcastReceiver;
+import android.content.ClipboardManager;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -28,30 +29,35 @@
         @Override
         public void onReceive(Context context, Intent intent) {
             int action = intent.getIntExtra("action", 0);
+            ClipboardManager cmb = (ClipboardManager) context .getSystemService(Context.CLIPBOARD_SERVICE);
             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);
+                    Log.e(TAG, "ACTION_SEND "+cmb.getText()+" nodeInfos: " + sendNode);
                     if(sendNode != null){
                         Log.e(TAG, "ACTION_SEND nodeInfos: " + sendNode.getClassName());
                         sendNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                     }else{
-                        printView(getRootInActiveWindow()," ");
+                        //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();
+                    try{
+                        List<AccessibilityNodeInfo> list = getRootInActiveWindow().findAccessibilityNodeInfosByText("说点什么...");
+                        Log.e(TAG, "ACTION_PASTE "+cmb.getText()+" 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();
+                        }
+                    }catch (Exception e){
+                        e.printStackTrace();
                     }
 
                     break;
@@ -77,23 +83,14 @@
         switch (eventType) {
             case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED://界面变化事件
                 Log.e(TAG, "TYPE_WINDOW_CONTENT_CHANGED " );
+                if(event.getClassName().toString().equals("androidx.viewpager.widget.ViewPager") && flag){
+                    clickEdit();
+                }
                 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(source != null && event.getClassName().toString().equals("com.ss.android.ugc.aweme.live.LivePlayActivity") && flag){
+                    clickEdit();
                 }
 
                 if(event.getText().contains("更多选项") ){
@@ -128,6 +125,25 @@
 
     }
 
+    /**
+     * 点击弹出 edit弹框
+     */
+    private void clickEdit(){
+        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();
+        }
+    }
+
     @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());
@@ -159,20 +175,22 @@
 
     @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;
+        try{
+            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;
             }
-        }else if(node != null && node.getContentDescription() != null && node.getContentDescription().equals(desc)){
-            return node;
-        }
+        }catch (Exception e){}
         return null;
     }
     private void printWindow(AccessibilityWindowInfo window, String space){

--
Gitblit v1.9.1