Runt
2022-05-04 7f292c4158e1854ca6dc72895c829e538cbc6c7b
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("继续");
    }
    //复制