package com.auto.lyric;
|
|
import android.Manifest;
|
import android.content.Intent;
|
import android.net.Uri;
|
import android.os.Build;
|
import android.os.Environment;
|
import android.provider.Settings;
|
import android.text.TextUtils;
|
import android.util.Log;
|
import android.widget.Toast;
|
|
import androidx.activity.result.ActivityResultLauncher;
|
import androidx.activity.result.contract.ActivityResultContracts;
|
|
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.ui.collect.CollectActivity;
|
import com.auto.lyric.vm.MainViewModel;
|
import com.google.gson.Gson;
|
|
import java.io.File;
|
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> {
|
SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
UserInfo userInfo;
|
@Override
|
public void initViews() {
|
//选择文件
|
ActivityResultLauncher fileLauncher = registerForActivityResult(new ActivityResultContracts.OpenDocument(), result -> {
|
Log.e(TAG,"result:"+result+" "+(result == null?"":result.getPath()));
|
if(result != null && result.getPath().indexOf(".lrc") == result.getPath().length()-4) {
|
Log.e(TAG,"getRootDirectory:"+" "+Environment.getExternalStorageDirectory());
|
String path = Environment.getExternalStorageDirectory()+"/"+result.getPath().split(":")[1];
|
|
viewModel.uploadFile(userInfo.userId,userInfo.activeKey,new File(path));
|
}else{
|
showDialog("文件格式错误", "请选择标准的歌词文件", (dialog, which) -> {
|
dialog.dismiss();
|
});
|
}
|
});
|
//单个权限申请
|
ActivityResultLauncher singlePermission = registerForActivityResult(new ActivityResultContracts.RequestPermission(), result -> {
|
if(result){
|
fileLauncher.launch(new String[]{"*/*"});
|
}else{
|
showDialog("权限申请", "没有文件读取权限", (dialog, which) -> {
|
dialog.dismiss();
|
});
|
}
|
});
|
Intent floatService = new Intent(getApplicationContext(), FloatingWindowService.class);
|
String deviceInfoStr = getStringProjectPrefrence("deviceInfo");
|
if(TextUtils.isEmpty(deviceInfoStr)){
|
viewModel.registerDevice();
|
}
|
long date = getLongProjectPrefrence("launchDate");
|
if(date == 0 || !dateFormat.format(new Date()).equals(dateFormat.format(new Date(date))) ){
|
viewModel.launchApp();
|
}
|
String key = getStringProjectPrefrence("activeKey");
|
/*userInfo = new Gson().fromJson(key,UserInfo.class);
|
try {
|
if(TextUtils.isEmpty(key) || TextUtils.isEmpty(userInfo.keyDateValid)||
|
new Date().getTime() > dateTimeFormat.parse(userInfo.keyDateValid).getTime()){
|
activeDialog();
|
}else{
|
|
viewModel.checkActive(userInfo.userId,userInfo.activeKey,new LoadingHttpObserver<ActiveResult>(this) {
|
|
@Override
|
public void onError(ActiveResult error) {
|
super.onError(error);
|
if(error.result.equals("108")){
|
activeDialog();
|
}
|
}
|
|
@Override
|
public void onComplete(ActiveResult result) {
|
userInfo = result.userInfo;
|
}
|
});
|
}
|
} 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);
|
}
|
});
|
//收藏
|
binding.collect.setOnClickListener(v -> addCollectDailog(userInfo.userId,userInfo.activeKey,binding.edit.getText().toString()));
|
//清空
|
binding.clear.setOnClickListener(v -> binding.edit.setText(""));
|
//保存本地
|
binding.save.setOnClickListener(v -> {});
|
binding.txtLocal.setOnClickListener(v -> {
|
singlePermission.launch(Manifest.permission.READ_EXTERNAL_STORAGE);//读取文件权限
|
});
|
//收藏夹
|
binding.txtFavorite.setOnClickListener(v -> startActivity(new Intent(mContext, CollectActivity.class)));
|
}
|
|
public boolean isAccessibilitySettingsOn() {
|
int accessibilityEnabled = 0;
|
final String service = getPackageName() + "/" + AutoInputService.class.getCanonicalName();
|
try {
|
accessibilityEnabled = Settings.Secure.getInt(
|
this.getApplicationContext().getContentResolver(),
|
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
|
Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled);
|
} catch (Settings.SettingNotFoundException e) {
|
Log.e(TAG, "Error finding setting, default accessibility to not found: "
|
+ e.getMessage());
|
}
|
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
|
|
if (accessibilityEnabled == 1) {
|
Log.v(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------");
|
String settingValue = Settings.Secure.getString(
|
getApplicationContext().getContentResolver(),
|
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
|
if (settingValue != null) {
|
mStringColonSplitter.setString(settingValue);
|
while (mStringColonSplitter.hasNext()) {
|
String accessibilityService = mStringColonSplitter.next();
|
|
Log.v(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
|
if (accessibilityService.equalsIgnoreCase(service)) {
|
Log.v(TAG, "We've found the correct setting - accessibility is switched on!");
|
return true;
|
}
|
}
|
}
|
} else {
|
Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
|
}
|
|
return false;
|
}
|
|
private void addCollectDailog(String userId,String activeKey,String lrc ){
|
|
showInputDialog("收藏歌词","填写歌词名称","收藏","取消",(dialog1,editText, which) -> {
|
viewModel.addToCollect(userId, activeKey, editText.getText().toString(), lrc, new LoadingHttpObserver<ActiveResult>(this) {
|
@Override
|
public void onComplete(ActiveResult error) {
|
|
}
|
});
|
},(dialog1,editText, which) -> {});
|
}
|
|
private void activeDialog(){
|
showInputDialog("软件尚未激活或已过期", "请输入激活码", "激活", "退出程序", (dialog1,editText, which) -> {
|
viewModel.register(editText.getText().toString().trim(),new LoadingHttpObserver<ActiveResult>(this) {
|
@Override
|
public void onComplete(ActiveResult result) {
|
dialog1.dismiss();
|
userInfo = result.userInfo;
|
putStringProjectPrefrence("activeKey",new Gson().toJson(result.userInfo));
|
}
|
});
|
}, (dialog1,editText, which) -> {
|
System.exit(0);
|
});
|
}
|
|
}
|