Runt
2022-05-15 75360a9fa2fcead8b516467d982c5cdf82c63263
歌词解析 修复及优化
handler 阻塞问题优化
3 files modified
107 ■■■■■ changed files
app/src/main/java/com/auto/lyric/data/LyricServer.java 32 ●●●● patch | view | raw | blame | history
app/src/main/java/com/auto/lyric/service/AutoInputService.java 8 ●●●● patch | view | raw | blame | history
app/src/main/java/com/auto/lyric/service/FloatingWindowService.java 67 ●●●● patch | view | raw | blame | history
app/src/main/java/com/auto/lyric/data/LyricServer.java
@@ -6,7 +6,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import java.util.regex.Matcher;
@@ -69,18 +68,22 @@
        // System.out.println("++++++++++++>>"+data);
        /*text = text.replace("[","");//将前面的替换成后面的
        text = text.replace("]","@");
        String splitdata[] = text.split("@");//分隔*/
        String splitdata[] = text.split("@");//分隔
        String regex = "(?<=\\])";
        Pattern compile = Pattern.compile(regex);
        String[] splitdata = compile.split(text,2);//分隔
        if(text.endsWith("]") || splitdata.length == 1){
            String str = text;
            str = str.replace("[","");
            str = str.replace("]","");
            str = str.replace(":",".");
            str = str.replace(".","@");
            String timedata[] =str.split("@");
        String[] splitdata = compile.split(text,2);//分隔*/
        int index = text.indexOf("]");
        String[] splitdata;
        if(text.length()-1 == index){
            splitdata = new String[]{text};
        }else{
            splitdata = new String[]{text.substring(0,index),text.substring(index+1)};
        }
        String tmpstr = splitdata[0].replace("[","").replace("]","");
        if(splitdata.length == 1){
            tmpstr = tmpstr.replace(":",".");
            tmpstr = tmpstr.replace(".","@");
            String timedata[] =tmpstr.split("@");
            Matcher matcher = pattern.matcher(timedata[0]);
            if(timedata.length==3 && matcher.matches()){
                int m = Integer.parseInt(timedata[0]);  //分
@@ -94,9 +97,6 @@
            }
        } else{
            String lrcContenet = splitdata[1];
            String tmpstr = splitdata[0];
            tmpstr = tmpstr.replace("[","");
            tmpstr = tmpstr.replace("]","");
            tmpstr = tmpstr.replace(":",".");
            tmpstr = tmpstr.replace(".","@");
            String timedata[] =tmpstr.split("@");
@@ -119,7 +119,6 @@
     */
    private static void initLrc(TreeMap<Integer, LyricObject> lrc_read){
        lrc_map.clear();
        Iterator<Integer> iterator = lrc_read.keySet().iterator();
        LyricObject oldval  = null;
        int i =0;
        for(Integer key : lrc_read.keySet()){
@@ -127,8 +126,7 @@
            if (oldval == null) {
                oldval = val;
            } else {
                LyricObject item1= new LyricObject();
                item1  = oldval;
                LyricObject item1 = oldval;
                item1.timeline = val.begintime-oldval.begintime;
                lrc_map.put(new Integer(i), item1);
                i++;
app/src/main/java/com/auto/lyric/service/AutoInputService.java
@@ -129,6 +129,7 @@
     * 点击弹出 edit弹框
     */
    private void clickEdit(){
        try{
        List<AccessibilityNodeInfo> list = getRootInActiveWindow().findAccessibilityNodeInfosByText("说点什么...");
        Log.e(TAG,"获取到控件 "+list.size());
        if (list != null && list.size() > 0 && flag) {
@@ -142,6 +143,7 @@
        }else if(flag){
            //Toast.makeText(getBaseContext(),"未找到控件",Toast.LENGTH_SHORT).show();
        }
        }catch ( Exception e){}
    }
    @SuppressLint("NewApi")
@@ -176,8 +178,8 @@
    @SuppressLint("NewApi")
    private AccessibilityNodeInfo getViewByDesc(AccessibilityNodeInfo node, String desc){
        try{
            Log.e(TAG,desc +" getViewByDesc id:"+node.getViewIdResourceName() +" class:"+node.getClassName()+" text:"+node.getText()+" "+node.getContentDescription());
            Log.e(TAG,"getViewByDesc  node:"+node);
            //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) {
@@ -209,7 +211,9 @@
    @Override
    public void onInterrupt() {
        Log.e(TAG,"onInterrupt 服务停止");
        unregisterReceiver(receiver);
        Toast.makeText(getBaseContext(),"服务停止",Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onServiceConnected() {
app/src/main/java/com/auto/lyric/service/FloatingWindowService.java
@@ -7,6 +7,7 @@
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
@@ -43,7 +44,7 @@
    private WindowManager.LayoutParams params;
    FloatViewBinding binding;
    final String TAG = "FloatingWindowService";
    final int THREAD_STOP = 0, KEYBOARD_SEND = 100,UPDATE_TIME = 200;
    final int THREAD_STOP = 0, KEYBOARD_SEND = 100;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    SimpleDateFormat msFormat = new SimpleDateFormat("mm:ss.SSS");
    boolean pause;//是否暂停
@@ -51,24 +52,30 @@
    LyricObject lyricObject;//当前发送过的歌词
    Handler handler;
    //计时器
    Timer timer;
    ServiceTask task;
    CountDownTimer countDownTimer;
    ServiceTask task;//计时器执行任务
    class ServiceTask extends TimerTask{
        @Override
        public void run() {
            int index = binding.lyric.getIndex(progress+=10);
            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);
                boolean flag = handler.sendMessage(message);
                Log.e(TAG,"sendmessge "+message.obj +" 发送成功:"+flag);
            }
            //停止
            if(index == LyricServer.getLrc_map().size()-1){
                handler.sendEmptyMessage(THREAD_STOP);
                Message stop = new Message();
                stop.what = THREAD_STOP;
                boolean flag = handler.sendMessage(stop);
                Log.e(TAG,"sendEmptyMessage THREAD_STOP 发送成功:"+flag);
            }
        }
    };
@@ -94,11 +101,9 @@
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = DeviceUtil.convertDpToPixel(200,getBaseContext());
        binding.close.setOnClickListener(v ->{
            stopService(new Intent(this,this.getClass()));
            stopService(new Intent(this,FloatingWindowService.class));
            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
@@ -142,12 +147,9 @@
            if(binding.btnStart.getText().equals("开启")){
                start();
            }else{
                pause();
                binding.btnStart.setText("开启");
                binding.btnPause.setEnabled(false);
                stop();
            }
        });
        initHandler();
    }
    @Nullable
@@ -157,12 +159,12 @@
    }
    private void initHandler(){
        handler = new Handler(this.getMainLooper()){
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if(msg.what == THREAD_STOP){//停止
                    binding.btnStart.performClick();
                    stop();
                }else if(msg.what == KEYBOARD_SEND){//发送文本
                    copy(msg.obj.toString());
                    Intent intent = new Intent();
@@ -174,8 +176,6 @@
                }else if(msg.what == -1){//显示在输入框中
                    //Toast.makeText(getBaseContext(),"上一句--"+msg.obj,Toast.LENGTH_SHORT).show();
                }else if(msg.what == UPDATE_TIME){
                    binding.timer.setText(msFormat.format(progress));
                }
            }
        };
@@ -184,8 +184,17 @@
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG,"onStartCommand flags:"+flags+" startId:"+startId+ " intent:"+intent);
        if(binding.getRoot().getParent() == null) {
        manager.addView(binding.getRoot(),params);
        }
        binding.lyric.setTextSize();
        initHandler();
        stop();
        progress = LyricServer.getLrc_map().get(0).begintime;
        binding.timer.setText(msFormat.format(progress));
        int index = binding.lyric.getIndex(progress);
        binding.lyric.setOffsetY(220 - index * (binding.lyric.getSIZEWORD() + 44));
        binding.lyric.invalidate();
        binding.lyric.setOnTouchListener(new View.OnTouchListener() {
            float touchY; //当触摸歌词View时,保存为当前触点的Y轴坐标
            @Override
@@ -227,7 +236,10 @@
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG,"onDestroy ");
        try {
        timer.cancel();
            countDownTimer.cancel();
        }catch (Exception e){}
        AutoInputService.flag = false;
        manager.removeView(binding.getRoot());
    }
@@ -239,7 +251,20 @@
        AutoInputService.flag = true;
        try {
            timer.cancel();
            countDownTimer.cancel();
        }catch (Exception e){}
        countDownTimer = new CountDownTimer(LyricServer.getLrc_map().get(LyricServer.getLrc_map().keySet().size()-1).begintime,10) {
            @Override
            public void onTick(long millisUntilFinished) {
                binding.timer.setText(msFormat.format(progress));
            }
            @Override
            public void onFinish() {
            }
        };
        countDownTimer.start();
        timer = new Timer();
        task = new ServiceTask();
        timer.schedule(task,0,10);
@@ -252,7 +277,10 @@
    }
    private void pause(){
        try {
        timer.cancel();
            countDownTimer.cancel();
        }catch (Exception e){}
        pause = true;
        AutoInputService.flag = false;
        binding.btnBack.setEnabled(false);
@@ -260,9 +288,16 @@
        binding.btnPause.setText("继续");
    }
    private void stop(){
        pause();
        binding.btnStart.setText("开启");
        binding.btnPause.setEnabled(false);
    }
    //复制
    private void copy(String data) {
        Log.e(TAG,"copy "+data);
        // 获取系统剪贴板
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建一个剪贴数据集,包含一个普通文本数据条目(需要复制的数据),其他的还有