| | |
| | | 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.Looper; |
| | | import android.os.Message; |
| | | import android.util.Log; |
| | | import android.view.Gravity; |
| | |
| | | import android.view.MotionEvent; |
| | | import android.view.View; |
| | | import android.view.WindowManager; |
| | | import android.widget.Toast; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.annotation.Nullable; |
| | |
| | | import com.auto.lyric.databinding.FloatViewBinding; |
| | | import com.auto.lyric.util.DeviceUtil; |
| | | |
| | | import java.text.ParseException; |
| | | import java.lang.ref.WeakReference; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.Timer; |
| | | import java.util.TimerTask; |
| | | |
| | |
| | | private WindowManager.LayoutParams params; |
| | | FloatViewBinding binding; |
| | | final String TAG = "FloatingWindowService"; |
| | | final int THREAD_STOP = 0, KEYBOARD_SEND = 100; |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); |
| | | final int THREAD_STOP = 99, KEYBOARD_SEND = 100; |
| | | SimpleDateFormat msFormat = new SimpleDateFormat("mm:ss.SSS"); |
| | | boolean pause;//是否暂停 |
| | | int progress;//时间进度 |
| | | LyricObject lyricObject;//当前发送过的歌词 |
| | | Handler handler; |
| | | MyHandler handler ; |
| | | |
| | | //计时器 |
| | | Timer timer; |
| | | CountDownTimer countDownTimer; |
| | | ServiceTask task;//计时器执行任务 |
| | | Timer timer = new Timer(); |
| | | //计时器执行任务 |
| | | 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.sendMessage(new Message()); |
| | | if(lyricObject == null || lyricObject != LyricServer.getLrc_map().get(index)){ |
| | | lyricObject = LyricServer.getLrc_map().get(index); |
| | | Message message = new Message(); |
| | |
| | | binding.btnFast.setOnClickListener(v -> progress+=700); |
| | | binding.btnBack.setOnClickListener(v -> progress-=700); |
| | | binding.btnStart.setOnClickListener(v -> { |
| | | try { |
| | | if(new Date().getTime() > dateFormat.parse("2022-09-30 04:00:00").getTime()){ |
| | | Toast.makeText(getApplicationContext(),"软件使用时间已过期", Toast.LENGTH_SHORT).show(); |
| | | return; |
| | | } |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if(binding.btnStart.getText().equals("开启")){ |
| | | start(); |
| | | }else{ |
| | | stop(); |
| | | } |
| | | }); |
| | | handler = new MyHandler(Looper.getMainLooper(),this); |
| | | } |
| | | |
| | | @Nullable |
| | |
| | | return null; |
| | | } |
| | | |
| | | private void initHandler(){ |
| | | handler = new Handler(){ |
| | | @Override |
| | | public void handleMessage(@NonNull Message msg) { |
| | | super.handleMessage(msg); |
| | | if(msg.what == THREAD_STOP){//停止 |
| | | stop(); |
| | | }else if(msg.what == KEYBOARD_SEND){//发送文本 |
| | | copy(msg.obj.toString()); |
| | | Intent intent = new Intent(); |
| | | intent.setAction(AutoInputService.class.getName()); |
| | | intent.putExtra("action",AutoInputService.ACTION_PASTE); |
| | | sendBroadcast(intent); |
| | | intent.putExtra("action",AutoInputService.ACTION_SEND); |
| | | sendBroadcast(intent); |
| | | }else if(msg.what == -1){//显示在输入框中 |
| | | |
| | | //Toast.makeText(getBaseContext(),"上一句--"+msg.obj,Toast.LENGTH_SHORT).show(); |
| | | } |
| | | } |
| | | }; |
| | | } |
| | | |
| | | @Override |
| | | public int onStartCommand(Intent intent, int flags, int startId) { |
| | |
| | | manager.addView(binding.getRoot(), params); |
| | | } |
| | | binding.lyric.setTextSize(); |
| | | initHandler(); |
| | | stop(); |
| | | progress = LyricServer.getLrc_map().get(0).begintime; |
| | | binding.timer.setText(msFormat.format(progress)); |
| | |
| | | case MotionEvent.ACTION_MOVE: |
| | | touchY=tt-touchY; |
| | | binding.lyric.setOffsetY(binding.lyric.getOffsetY()+touchY); |
| | | Log.e("LyricView","offsety:"+binding.lyric.getOffsetY()); |
| | | //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); |
| | | //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); |
| | | //Log.e("LyricView", "object:" + lyricObject); |
| | | progress = lyricObject.begintime; |
| | | binding.timer.setText(msFormat.format(progress)); |
| | | } |
| | |
| | | Log.e(TAG,"onDestroy "); |
| | | try { |
| | | timer.cancel(); |
| | | countDownTimer.cancel(); |
| | | }catch (Exception e){} |
| | | AutoInputService.flag = false; |
| | | pause = true; |
| | | manager.removeView(binding.getRoot()); |
| | | } |
| | | |
| | |
| | | */ |
| | | private void start(){ |
| | | AutoInputService.flag = true; |
| | | pause = false; |
| | | 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); |
| | | timer.schedule(new ServiceTask(),0,10); |
| | | binding.btnStart.setText("停止"); |
| | | binding.btnPause.setText("暂停"); |
| | | binding.btnBack.setEnabled(true); |
| | | binding.btnFast.setEnabled(true); |
| | | binding.btnPause.setEnabled(true); |
| | | pause = false; |
| | | Intent intent = new Intent(); |
| | | intent.setAction(AutoInputService.class.getName()); |
| | | intent.putExtra("action",AutoInputService.ACTION_START); |
| | | sendBroadcast(intent); |
| | | } |
| | | |
| | | private void pause(){ |
| | | try { |
| | | timer.cancel(); |
| | | countDownTimer.cancel(); |
| | | }catch (Exception e){} |
| | | pause = true; |
| | | AutoInputService.flag = false; |
| | |
| | | clipboard.setPrimaryClip(clipData); |
| | | } |
| | | |
| | | class MyHandler extends Handler{ |
| | | |
| | | private final WeakReference<FloatingWindowService> weakReference; |
| | | |
| | | MyHandler(Looper looper,FloatingWindowService service) { |
| | | super(looper); |
| | | this.weakReference = new WeakReference<>(service); |
| | | } |
| | | |
| | | @Override |
| | | public void handleMessage(@NonNull Message msg) { |
| | | super.handleMessage(msg); |
| | | if(msg.what == THREAD_STOP){//停止 |
| | | stop(); |
| | | }else if(msg.what == KEYBOARD_SEND){//发送文本 |
| | | copy(msg.obj.toString()); |
| | | Intent intent = new Intent(); |
| | | intent.setAction(AutoInputService.class.getName()); |
| | | intent.putExtra("action",AutoInputService.ACTION_PASTE); |
| | | sendBroadcast(intent); |
| | | intent.putExtra("action",AutoInputService.ACTION_SEND); |
| | | sendBroadcast(intent); |
| | | }else if(msg.what == -1){//显示在输入框中 |
| | | |
| | | //Toast.makeText(getBaseContext(),"上一句--"+msg.obj,Toast.LENGTH_SHORT).show(); |
| | | } |
| | | int index = binding.lyric.getIndex(progress); |
| | | binding.lyric.setOffsetY(220 - index * (binding.lyric.getSIZEWORD() + 44)); |
| | | binding.lyric.invalidate(); |
| | | weakReference.get().binding.timer.setText(msFormat.format(progress)); |
| | | } |
| | | } |
| | | |
| | | } |