Runt
2022-06-04 a046d5a804d6842796a718fba4e98c3259340a60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.auto.lyric;
 
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
 
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.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 dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    UserInfo userInfo;
    @Override
    public void initViews() {
        Intent floatService  = new Intent(getApplicationContext(), FloatingWindowService.class);
        String key = getStringProjectPrefrence("activeKey");
        userInfo = new Gson().fromJson(key,UserInfo.class);
        try {
            if(TextUtils.isEmpty(key) || TextUtils.isEmpty(userInfo.keyDateValid)||
                    new Date().getTime() > dateFormat.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.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);
        });
    }
 
}