Runt
2022-05-04 7f292c4158e1854ca6dc72895c829e538cbc6c7b
app/src/main/java/com/auto/lyric/service/FloatingWindowService.java
@@ -22,6 +22,7 @@
import androidx.annotation.Nullable;
import com.auto.lyric.R;
import com.auto.lyric.data.LyricObject;
import com.auto.lyric.data.LyricServer;
import com.auto.lyric.databinding.FloatViewBinding;
import com.auto.lyric.util.DeviceUtil;
@@ -42,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;
@@ -59,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);
            }
@@ -89,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
@@ -112,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();
                }
            }
        });
@@ -138,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();
@@ -165,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();
@@ -195,6 +186,40 @@
        Log.e(TAG,"onStartCommand flags:"+flags+" startId:"+startId+ " intent:"+intent);
        manager.addView(binding.getRoot(),params);
        binding.lyric.setTextSize();
        binding.lyric.setOnTouchListener(new View.OnTouchListener() {
            float touchY; //当触摸歌词View时,保存为当前触点的Y轴坐标
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float tt=event.getY();
                switch(event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        touchY=tt-touchY;
                        binding.lyric.setOffsetY(binding.lyric.getOffsetY()+touchY);
                        Log.e("LyricView","offsety:"+binding.lyric.getOffsetY());
                        binding.lyric.invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        if(binding.btnStart.getText().equals("开启") || pause) {
                            int index = binding.lyric.getIndexFromOffsetY(binding.lyric.getOffsetY());
                            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:" + lyricObject);
                                progress = lyricObject.begintime;
                                binding.timer.setText(msFormat.format(progress));
                            }
                            binding.lyric.invalidate();
                        }
                        break;
                }
                touchY=tt;
                return true;
            }
        });
        return super.onStartCommand(intent, flags, startId);
    }
@@ -203,6 +228,7 @@
        super.onDestroy();
        Log.e(TAG,"onDestroy ");
        timer.cancel();
        AutoInputService.flag = false;
        manager.removeView(binding.getRoot());
    }
@@ -210,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);
@@ -218,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("继续");
    }
    //复制