From 7f292c4158e1854ca6dc72895c829e538cbc6c7b Mon Sep 17 00:00:00 2001
From: Runt <qingingrunt2010@qq.com>
Date: Wed, 04 May 2022 15:21:57 +0000
Subject: [PATCH] 发送歌词功能

---
 app/src/main/java/com/auto/lyric/MainActivity.java                  |    5 -
 app/src/main/java/com/auto/lyric/service/FloatingWindowService.java |   66 +++++++++++----------
 app/src/main/java/com/auto/lyric/service/AutoInputService.java      |   58 ++++++++++++-------
 3 files changed, 73 insertions(+), 56 deletions(-)

diff --git a/app/src/main/java/com/auto/lyric/MainActivity.java b/app/src/main/java/com/auto/lyric/MainActivity.java
index 121b6f9..e0ac153 100644
--- a/app/src/main/java/com/auto/lyric/MainActivity.java
+++ b/app/src/main/java/com/auto/lyric/MainActivity.java
@@ -26,9 +26,6 @@
 
     @Override
     public void initViews() {
-
-        Intent inputService  = new Intent(getApplicationContext(), AutoInputService.class);
-
         Intent floatService  = new Intent(getApplicationContext(), FloatingWindowService.class);
         binding.button.setOnClickListener(v -> {
 
@@ -75,7 +72,9 @@
                     startActivity(intent);
                 }else{//开启监听服务
                     startService(floatService);
+                    startService(new Intent(this,AutoInputService.class));
                 }
+                //startService(floatService);
             }
         });
     }
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..e5bb979 100644
--- a/app/src/main/java/com/auto/lyric/service/AutoInputService.java
+++ b/app/src/main/java/com/auto/lyric/service/AutoInputService.java
@@ -43,15 +43,19 @@
                     }
                     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 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 +81,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")){
+                    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();
-                    }
+                    clickEdit();
                 }
 
                 if(event.getText().contains("更多选项") ){
@@ -128,6 +123,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());
diff --git a/app/src/main/java/com/auto/lyric/service/FloatingWindowService.java b/app/src/main/java/com/auto/lyric/service/FloatingWindowService.java
index 92d3ce3..122af61 100644
--- a/app/src/main/java/com/auto/lyric/service/FloatingWindowService.java
+++ b/app/src/main/java/com/auto/lyric/service/FloatingWindowService.java
@@ -43,12 +43,12 @@
     private WindowManager.LayoutParams params;
     FloatViewBinding binding;
     final String TAG = "FloatingWindowService";
-    final int THREAD_STOP = 0, THREAD_START = 1 , KEYBOARD_SEND = 100,UPDATE_TIME = 200;
+    final int THREAD_STOP = 0, KEYBOARD_SEND = 100,UPDATE_TIME = 200;
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
     SimpleDateFormat msFormat = new SimpleDateFormat("mm:ss.SSS");
-    boolean pause;
-    int progress;
-
+    boolean pause;//是否暂停
+    int progress;//时间进度
+    LyricObject lyricObject;//当前发送过的歌词
     Handler handler;
 
     Timer timer;
@@ -60,6 +60,13 @@
             binding.lyric.setOffsetY(220 - index * (binding.lyric.getSIZEWORD() + 44));
             binding.lyric.invalidate();
             handler.sendEmptyMessage(UPDATE_TIME);
+            if(lyricObject == null || lyricObject != LyricServer.getLrc_map().get(index)){
+                lyricObject = LyricServer.getLrc_map().get(index);
+                Message message = new Message();
+                message.what = KEYBOARD_SEND;
+                message.obj = lyricObject.lrc;
+                handler.sendMessage(message);
+            }
             if(index == LyricServer.getLrc_map().size()-1){
                 handler.sendEmptyMessage(THREAD_STOP);
             }
@@ -90,6 +97,8 @@
             stopService(new Intent(this,this.getClass()));
             stopService(new Intent(this,AutoInputService.class));
         });
+        progress = LyricServer.getLrc_map().get(0).begintime;
+        binding.timer.setText(msFormat.format(progress));
         binding.floating.setOnTouchListener(new View.OnTouchListener() {
             int startY;
             @Override
@@ -113,17 +122,9 @@
             @Override
             public void onClick(View view) {
                 if(pause){
-                    pause = false;
-                    binding.btnPause.setText("暂停");
-                    binding.btnBack.setEnabled(true);
-                    binding.btnFast.setEnabled(true);
                     start();
                 }else{
-                    pause = true;
-                    binding.btnPause.setText("继续");
-                    binding.btnBack.setEnabled(false);
-                    binding.btnFast.setEnabled(false);
-                    timer.cancel();
+                    pause();
                 }
             }
         });
@@ -139,17 +140,11 @@
                 e.printStackTrace();
             }
             if(binding.btnStart.getText().equals("开启")){
-                progress = 0 ;
                 start();
-                startService(new Intent(this,AutoInputService.class));
             }else{
+                pause();
                 binding.btnStart.setText("开启");
-                binding.btnBack.setEnabled(false);
-                binding.btnFast.setEnabled(false);
                 binding.btnPause.setEnabled(false);
-                progress = 0 ;
-                timer.cancel();
-                stopService(new Intent(this,AutoInputService.class));
             }
         });
         initHandler();
@@ -166,13 +161,8 @@
             @Override
             public void handleMessage(@NonNull Message msg) {
                 super.handleMessage(msg);
-                if(msg.what == THREAD_START){//开始
-                    AutoInputService.flag = true;
-                    binding.btnPause.performClick();
-                }else if(msg.what == THREAD_STOP){//停止
-                    AutoInputService.flag = false;
+                if(msg.what == THREAD_STOP){//停止
                     binding.btnStart.performClick();
-
                 }else if(msg.what == KEYBOARD_SEND){//发送文本
                     copy(msg.obj.toString());
                     Intent intent = new Intent();
@@ -213,13 +203,13 @@
                     case MotionEvent.ACTION_UP:
                         if(binding.btnStart.getText().equals("开启") || pause) {
                             int index = binding.lyric.getIndexFromOffsetY(binding.lyric.getOffsetY());
-                            LyricObject object = LyricServer.getLrc_map().get(index);
-                            Log.e("LyricView", "index:"+index+" object:" + object);
-                            if(object != null) {
+                            LyricObject lyricObject = LyricServer.getLrc_map().get(index);
+                            Log.e("LyricView", "index:"+index+" object:" + lyricObject);
+                            if(lyricObject != null) {
                                 binding.lyric.setIndex(index);
                                 binding.lyric.setOffsetY(220 - index * (binding.lyric.getSIZEWORD() + 44));
-                                Log.e("LyricView", "object:" + object);
-                                progress = object.begintime;
+                                Log.e("LyricView", "object:" + lyricObject);
+                                progress = lyricObject.begintime;
                                 binding.timer.setText(msFormat.format(progress));
                             }
                             binding.lyric.invalidate();
@@ -238,6 +228,7 @@
         super.onDestroy();
         Log.e(TAG,"onDestroy ");
         timer.cancel();
+        AutoInputService.flag = false;
         manager.removeView(binding.getRoot());
     }
 
@@ -245,6 +236,10 @@
      * 开始
      */
     private void start(){
+        AutoInputService.flag = true;
+        try {
+            timer.cancel();
+        }catch (Exception e){}
         timer = new Timer();
         task = new ServiceTask();
         timer.schedule(task,0,10);
@@ -253,8 +248,17 @@
         binding.btnBack.setEnabled(true);
         binding.btnFast.setEnabled(true);
         binding.btnPause.setEnabled(true);
+        pause = false;
     }
 
+    private void pause(){
+        timer.cancel();
+        pause = true;
+        AutoInputService.flag = false;
+        binding.btnBack.setEnabled(false);
+        binding.btnFast.setEnabled(false);
+        binding.btnPause.setText("继续");
+    }
 
 
     //复制

--
Gitblit v1.9.1