| | |
| | | import android.provider.Settings; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | import android.view.ViewGroup; |
| | | import android.widget.EditText; |
| | | import android.widget.LinearLayout; |
| | | import android.widget.Toast; |
| | | |
| | | import androidx.appcompat.app.AlertDialog; |
| | | |
| | | import com.auto.lyric.base.activities.BaseActivity; |
| | | import com.auto.lyric.data.ActiveResult; |
| | | import com.auto.lyric.data.LyricServer; |
| | | import com.auto.lyric.data.TextBean; |
| | | import com.auto.lyric.data.UserInfo; |
| | | import com.auto.lyric.databinding.ActivityMainBinding; |
| | | import com.auto.lyric.retrofit.observable.LoadingHttpObserver; |
| | | import com.auto.lyric.service.AutoInputService; |
| | | import com.auto.lyric.service.FloatingWindowService; |
| | | import com.auto.lyric.util.DeviceUtil; |
| | | import com.auto.lyric.vm.MainViewModel; |
| | | import com.google.gson.Gson; |
| | | |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.regex.Pattern; |
| | | |
| | | public class MainActivity extends BaseActivity<ActivityMainBinding, MainViewModel> { |
| | | |
| | | Intent service = new Intent(getApplicationContext(), AutoInputService.class); |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); |
| | | |
| | | @Override |
| | | public void initViews() { |
| | | Intent floatService = new Intent(getApplicationContext(), FloatingWindowService.class); |
| | | String key = getStringProjectPrefrence("activeKey"); |
| | | UserInfo result = new Gson().fromJson(key,UserInfo.class); |
| | | try { |
| | | if(TextUtils.isEmpty(key) || TextUtils.isEmpty(result.keyDateValid)|| |
| | | new Date().getTime() > dateFormat.parse(result.keyDateValid).getTime()){ |
| | | activeDialog(); |
| | | }else{ |
| | | |
| | | } |
| | | viewModel.checkActive(result.userId,result.activeKey,new LoadingHttpObserver<ActiveResult>(this) { |
| | | |
| | | @Override |
| | | protected void onResume() { |
| | | super.onResume(); |
| | | @Override |
| | | public void onError(ActiveResult error) { |
| | | super.onError(error); |
| | | } |
| | | |
| | | //申请权限 |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| | | //开启悬浮窗 |
| | | if(!Settings.canDrawOverlays(getApplicationContext())) { |
| | | //启动Activity让用户授权 |
| | | Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); |
| | | intent.setData(Uri.parse("package:" + getPackageName())); |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
| | | startActivity(intent); |
| | | }else if(!isAccessibilitySettingsOn()){//辅助权限 |
| | | Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
| | | startActivity(intent); |
| | | }else{//开启监听服务 |
| | | startService(service); |
| | | @Override |
| | | public void onComplete(ActiveResult result) { |
| | | } |
| | | }); |
| | | } |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | binding.button.setOnClickListener(v -> { |
| | | |
| | | if(binding.edit.getText().toString().trim().length() == 0){ |
| | | Toast.makeText(this,"请输入文本", Toast.LENGTH_SHORT).show(); |
| | | return; |
| | | } |
| | | ArrayList<TextBean> arrayList = new ArrayList<>();//解析的文本数据 |
| | | SimpleDateFormat msFormat = new SimpleDateFormat("mm:ss.SSS"); |
| | | String[] strings = binding.edit.getText().toString().split("\n"); |
| | | for(String text : strings){ |
| | | int index = text.indexOf("]"); |
| | | if(index < 8 ){ |
| | | continue; |
| | | } |
| | | String regex = "(?<=\\])"; |
| | | Pattern compile = Pattern.compile(regex); |
| | | String[] split = compile.split(text,2); |
| | | long wait = 0; |
| | | try { |
| | | wait = 28800000 + msFormat.parse(split[0].replace("[","").replace("]","")).getTime(); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | arrayList.add(new TextBean(wait,split[1])); |
| | | } |
| | | if(arrayList.size() == 0){ |
| | | return; |
| | | } |
| | | |
| | | LyricServer.read(Arrays.asList(strings)); |
| | | //申请权限 |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| | | //开启悬浮窗 |
| | | if(!Settings.canDrawOverlays(getApplicationContext())) { |
| | | //启动Activity让用户授权 |
| | | Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); |
| | | intent.setData(Uri.parse("package:" + getPackageName())); |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
| | | startActivity(intent); |
| | | }else if(!isAccessibilitySettingsOn()){//辅助权限 |
| | | Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); |
| | | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK ); |
| | | startActivity(intent); |
| | | }else{//开启监听服务 |
| | | startService(floatService); |
| | | startService(new Intent(this,AutoInputService.class)); |
| | | } |
| | | //startService(floatService); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | public boolean isAccessibilitySettingsOn() { |
| | |
| | | } |
| | | |
| | | |
| | | private void activeDialog(){ |
| | | AlertDialog.Builder builder = new AlertDialog.Builder(this); |
| | | builder.setTitle("软件尚未激活或已过期"); |
| | | EditText editText = new EditText(this); |
| | | editText.setHint("请输入激活码"); |
| | | ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| | | params.setMargins(DeviceUtil.convertDpToPixel(30,this),DeviceUtil.convertDpToPixel(20,this),DeviceUtil.convertDpToPixel(30,this),0); |
| | | editText.setLayoutParams(params); |
| | | LinearLayout lin = new LinearLayout(this); |
| | | lin.addView(editText); |
| | | builder.setView(lin); |
| | | builder.setCancelable(false); |
| | | AlertDialog dialg = builder.create(); |
| | | dialg.setButton(AlertDialog.BUTTON_NEGATIVE,"激活", (dialog, which) -> { |
| | | viewModel.register(editText.getText().toString().trim(),new LoadingHttpObserver<ActiveResult>(this) { |
| | | |
| | | @Override |
| | | public void onError(ActiveResult error) { |
| | | super.onError(error); |
| | | dialg.show(); |
| | | } |
| | | |
| | | @Override |
| | | public void onComplete(ActiveResult result) { |
| | | putStringProjectPrefrence("activeKey",new Gson().toJson(result.userInfo)); |
| | | } |
| | | }); |
| | | }); |
| | | dialg.setButton(AlertDialog.BUTTON_POSITIVE,"退出程序",(dialog,which) ->{ |
| | | System.exit(0); |
| | | }); |
| | | dialg.show(); |
| | | } |
| | | |
| | | } |