| | |
| | | import android.os.Build; |
| | | 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, THREAD_START = 1 , KEYBOARD_SEND = 100,UPDATE_TIME = 200; |
| | | 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; |
| | | boolean pause;//是否暂停 |
| | | int progress;//时间进度 |
| | | LyricObject lyricObject;//当前发送过的歌词 |
| | | MyHandler handler ; |
| | | |
| | | Handler handler; |
| | | |
| | | Timer timer; |
| | | 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.sendEmptyMessage(UPDATE_TIME); |
| | | handler.sendMessage(new Message()); |
| | | 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; |
| | | 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)); |
| | | }); |
| | | binding.floating.setOnTouchListener(new View.OnTouchListener() { |
| | |
| | | @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(); |
| | | } |
| | | } |
| | | }); |
| | | 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("开启")){ |
| | | progress = 0 ; |
| | | start(); |
| | | startService(new Intent(this,AutoInputService.class)); |
| | | }else{ |
| | | 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)); |
| | | stop(); |
| | | } |
| | | }); |
| | | initHandler(); |
| | | handler = new MyHandler(Looper.getMainLooper(),this); |
| | | } |
| | | |
| | | @Nullable |
| | |
| | | return null; |
| | | } |
| | | |
| | | private void initHandler(){ |
| | | handler = new Handler(this.getMainLooper()){ |
| | | @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; |
| | | binding.btnStart.performClick(); |
| | | |
| | | }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(); |
| | | }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(); |
| | | 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 |
| | |
| | | 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 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(); |
| | |
| | | public void onDestroy() { |
| | | super.onDestroy(); |
| | | Log.e(TAG,"onDestroy "); |
| | | timer.cancel(); |
| | | try { |
| | | timer.cancel(); |
| | | }catch (Exception e){} |
| | | AutoInputService.flag = false; |
| | | pause = true; |
| | | manager.removeView(binding.getRoot()); |
| | | } |
| | | |
| | |
| | | * 开始 |
| | | */ |
| | | private void start(){ |
| | | timer = new Timer(); |
| | | task = new ServiceTask(); |
| | | timer.schedule(task,0,10); |
| | | AutoInputService.flag = true; |
| | | pause = false; |
| | | try { |
| | | timer.cancel(); |
| | | }catch (Exception e){} |
| | | timer.schedule(new ServiceTask(),0,10); |
| | | binding.btnStart.setText("停止"); |
| | | binding.btnPause.setText("暂停"); |
| | | binding.btnBack.setEnabled(true); |
| | | binding.btnFast.setEnabled(true); |
| | | binding.btnPause.setEnabled(true); |
| | | Intent intent = new Intent(); |
| | | intent.setAction(AutoInputService.class.getName()); |
| | | intent.putExtra("action",AutoInputService.ACTION_START); |
| | | sendBroadcast(intent); |
| | | } |
| | | |
| | | private void pause(){ |
| | | try { |
| | | timer.cancel(); |
| | | }catch (Exception e){} |
| | | pause = true; |
| | | AutoInputService.flag = false; |
| | | binding.btnBack.setEnabled(false); |
| | | binding.btnFast.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); |
| | | // 创建一个剪贴数据集,包含一个普通文本数据条目(需要复制的数据),其他的还有 |
| | |
| | | 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)); |
| | | } |
| | | } |
| | | |
| | | } |