| | |
| | | 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; |
| | |
| | | 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;//是否暂停 |
| | |
| | | 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); |
| | | } |
| | | } |
| | | }; |
| | |
| | | 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 |
| | |
| | | if(binding.btnStart.getText().equals("开启")){ |
| | | start(); |
| | | }else{ |
| | | pause(); |
| | | binding.btnStart.setText("开启"); |
| | | binding.btnPause.setEnabled(false); |
| | | stop(); |
| | | } |
| | | }); |
| | | initHandler(); |
| | | } |
| | | |
| | | @Nullable |
| | |
| | | } |
| | | |
| | | 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(); |
| | |
| | | }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)); |
| | | } |
| | | } |
| | | }; |
| | |
| | | @Override |
| | | public int onStartCommand(Intent intent, int flags, int startId) { |
| | | Log.e(TAG,"onStartCommand flags:"+flags+" startId:"+startId+ " intent:"+intent); |
| | | manager.addView(binding.getRoot(),params); |
| | | 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 |
| | |
| | | public void onDestroy() { |
| | | super.onDestroy(); |
| | | Log.e(TAG,"onDestroy "); |
| | | timer.cancel(); |
| | | try { |
| | | timer.cancel(); |
| | | countDownTimer.cancel(); |
| | | }catch (Exception e){} |
| | | AutoInputService.flag = false; |
| | | manager.removeView(binding.getRoot()); |
| | | } |
| | |
| | | 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); |
| | |
| | | } |
| | | |
| | | private void pause(){ |
| | | timer.cancel(); |
| | | try { |
| | | timer.cancel(); |
| | | countDownTimer.cancel(); |
| | | }catch (Exception e){} |
| | | pause = true; |
| | | AutoInputService.flag = false; |
| | | binding.btnBack.setEnabled(false); |
| | |
| | | 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); |
| | | // 创建一个剪贴数据集,包含一个普通文本数据条目(需要复制的数据),其他的还有 |