refactor(app): 优化代码结构和可读性
- 格式化代码,调整缩进和空格 - 添加方法注释,解释主要功能 - 修改变量命名,提高可读性 - 删除冗余代码和不必要的注释
This commit is contained in:
parent
c3467add0e
commit
d2d95ada82
|
@ -94,112 +94,115 @@ public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
LogFileUtil.initialize(this);
|
LogFileUtil.initialize(this);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
logInfo("onCreate: Initializing application");
|
logInfo("onCreate: Initializing application");
|
||||||
|
|
||||||
initializeExecutorService();
|
initializeExecutorService();
|
||||||
|
|
||||||
System.setProperty("java.library.path", getApplicationInfo().nativeLibraryDir);
|
System.setProperty("java.library.path", getApplicationInfo().nativeLibraryDir);
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||||
!= PackageManager.PERMISSION_GRANTED) {
|
!= PackageManager.PERMISSION_GRANTED) {
|
||||||
requestStoragePermission();
|
requestStoragePermission();
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!Environment.isExternalStorageManager()) {
|
|
||||||
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
|
|
||||||
intent.setData(Uri.parse("package:" + getPackageName()));
|
|
||||||
startActivityForResult(intent, ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
if (!isNetworkAvailable(this)) {
|
if (!Environment.isExternalStorageManager()) {
|
||||||
showToast("网络不可用");
|
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
|
||||||
logError("Network not available, closing app.");
|
intent.setData(Uri.parse("package:" + getPackageName()));
|
||||||
finish();
|
startActivityForResult(intent, ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
logInfo("onCreate: Setting up work manager");
|
if (!isNetworkAvailable(this)) {
|
||||||
schedulePeriodicWorkIfNotExists();
|
showToast("网络不可用");
|
||||||
|
logError("Network not available, closing app.");
|
||||||
|
finish();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
logInfo("onCreate: Setting up UI components");
|
logInfo("onCreate: Setting up work manager");
|
||||||
|
schedulePeriodicWorkIfNotExists();
|
||||||
|
|
||||||
setupButton(R.id.run_script_button, v -> {
|
logInfo("onCreate: Setting up UI components");
|
||||||
try {
|
|
||||||
AutoJsUtil.runAutojsScript(this);
|
|
||||||
} catch (Exception e) {
|
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "runAutojsScript: " + e.getMessage(), e);
|
|
||||||
showToast("执行脚本失败");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
setupButton(R.id.connectVpnButton, v -> startProxyVpn(this));
|
|
||||||
setupButton(R.id.disconnectVpnButton, v -> ClashUtil.stopProxy(this));
|
|
||||||
setupButton(R.id.switchVpnButton, v -> ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170"));
|
|
||||||
|
|
||||||
armClient = new ArmCloudApiClient();
|
setupButton(R.id.run_script_button, v -> {
|
||||||
|
try {
|
||||||
|
AutoJsUtil.runAutojsScript(this);
|
||||||
|
} catch (Exception e) {
|
||||||
|
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "runAutojsScript: " + e.getMessage(), e);
|
||||||
|
showToast("执行脚本失败");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setupButton(R.id.connectVpnButton, v -> startProxyVpn(this));
|
||||||
|
setupButton(R.id.disconnectVpnButton, v -> ClashUtil.stopProxy(this));
|
||||||
|
setupButton(R.id.switchVpnButton, v -> ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170"));
|
||||||
|
|
||||||
String currentPackage = getPackageName();
|
armClient = new ArmCloudApiClient();
|
||||||
ShellUtils.execRootCmdAndGetResult("pm grant " + currentPackage + " android.permission.INTERACT_ACROSS_USERS");
|
|
||||||
ShellUtils.execRootCmdAndGetResult("pm grant " + currentPackage + " android.permission.WRITE_SECURE_SETTINGS");
|
|
||||||
|
|
||||||
setupButton(R.id.modifyDeviceInfoButton, v -> ChangeDeviceInfoUtil.changeDeviceInfo(currentPackage, this, armClient));
|
String currentPackage = getPackageName();
|
||||||
setupButton(R.id.resetDeviceInfoButton, v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(currentPackage, this));
|
ShellUtils.execRootCmdAndGetResult("pm grant " + currentPackage + " android.permission.INTERACT_ACROSS_USERS");
|
||||||
|
ShellUtils.execRootCmdAndGetResult("pm grant " + currentPackage + " android.permission.WRITE_SECURE_SETTINGS");
|
||||||
|
|
||||||
setupButton(R.id.execute_button, v -> {
|
setupButton(R.id.modifyDeviceInfoButton, v -> ChangeDeviceInfoUtil.changeDeviceInfo(currentPackage, this, armClient));
|
||||||
((Button) v).setEnabled(false);
|
setupButton(R.id.resetDeviceInfoButton, v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(currentPackage, this));
|
||||||
startLoadWork();
|
|
||||||
});
|
|
||||||
|
|
||||||
setupButton(R.id.stop_execute_button, v -> {
|
setupButton(R.id.execute_button, v -> {
|
||||||
WorkManager.getInstance(this).cancelAllWorkByTag(WORK_TAG);
|
((Button) v).setEnabled(false);
|
||||||
Button executeButton = findViewById(R.id.execute_button);
|
startLoadWork();
|
||||||
if (executeButton != null) {
|
});
|
||||||
executeButton.setEnabled(true);
|
|
||||||
}
|
setupButton(R.id.stop_execute_button, v -> {
|
||||||
});
|
WorkManager.getInstance(this).cancelAllWorkByTag(WORK_TAG);
|
||||||
|
Button executeButton = findViewById(R.id.execute_button);
|
||||||
|
if (executeButton != null) {
|
||||||
|
executeButton.setEnabled(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void requestStoragePermission() {
|
private void requestStoragePermission() {
|
||||||
ActivityCompat.requestPermissions(
|
ActivityCompat.requestPermissions(
|
||||||
this,
|
this,
|
||||||
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
|
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
|
||||||
REQUEST_CODE_STORAGE_PERMISSION
|
REQUEST_CODE_STORAGE_PERMISSION
|
||||||
);
|
);
|
||||||
}
|
|
||||||
private void schedulePeriodicWorkIfNotExists() {
|
|
||||||
WorkManager.getInstance(this).getWorkInfosForUniqueWorkLiveData("CheckAccessibilityWorker")
|
|
||||||
.observe(this, workInfos -> {
|
|
||||||
if (workInfos == null || workInfos.isEmpty()) {
|
|
||||||
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(CheckAccessibilityWorker.class, 15, TimeUnit.MINUTES)
|
|
||||||
.addTag("CheckAccessibilityWorker")
|
|
||||||
.build();
|
|
||||||
WorkManager.getInstance(this).enqueueUniquePeriodicWork(
|
|
||||||
"CheckAccessibilityWorker",
|
|
||||||
ExistingPeriodicWorkPolicy.REPLACE,
|
|
||||||
workRequest
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void schedulePeriodicWorkIfNotExists() {
|
||||||
|
WorkManager.getInstance(this).getWorkInfosForUniqueWorkLiveData("CheckAccessibilityWorker")
|
||||||
|
.observe(this, workInfos -> {
|
||||||
|
if (workInfos == null || workInfos.isEmpty()) {
|
||||||
|
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(CheckAccessibilityWorker.class, 15, TimeUnit.MINUTES)
|
||||||
|
.addTag("CheckAccessibilityWorker")
|
||||||
|
.build();
|
||||||
|
WorkManager.getInstance(this).enqueueUniquePeriodicWork(
|
||||||
|
"CheckAccessibilityWorker",
|
||||||
|
ExistingPeriodicWorkPolicy.REPLACE,
|
||||||
|
workRequest
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void setupButton(int resId, View.OnClickListener listener) {
|
private void setupButton(int resId, View.OnClickListener listener) {
|
||||||
Button button = findViewById(resId);
|
Button button = findViewById(resId);
|
||||||
if (button != null) {
|
if (button != null) {
|
||||||
button.setOnClickListener(listener);
|
button.setOnClickListener(listener);
|
||||||
} else {
|
} else {
|
||||||
logInfo("Button not found: " + resId);
|
logInfo("Button not found: " + resId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String WORK_TAG = "LOAD_WORK";
|
private static String WORK_TAG = "LOAD_WORK";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 这段代码的功能是启动一个周期性后台任务: 创建一个周期为30分钟的PeriodicWorkRequest,执行LoadDeviceWorker类的任务 设置初始延迟为0秒,添加任务标签WORK_TAG 使用WorkManager将任务加入队列,准备执行
|
||||||
|
*/
|
||||||
private void startLoadWork() {
|
private void startLoadWork() {
|
||||||
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.
|
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.
|
||||||
Builder(LoadDeviceWorker.class, 30, TimeUnit.MINUTES)
|
Builder(LoadDeviceWorker.class, 30, TimeUnit.MINUTES)
|
||||||
|
|
|
@ -36,26 +36,34 @@ public class LoadDeviceWorker extends CoroutineWorker {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 这段代码是LoadDeviceWorker类中的doWork方法,用于执行后台任务。其功能如下: 生成唯一的taskId,调用ChangeDeviceInfoUtil.getDeviceInfoSync获取设备信息。 获取packageName和zipName,并记录日志。
|
||||||
|
* 如果获取设备信息成功且packageName和zipName非空,则调用processPackageInfoWithDeviceInfo处理包信息。 若处理成功,执行executeSingleLogic方法。 否则,记录获取设备信息失败的日志。 最后返回任务执行结果为成功。
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public @Nullable Object doWork(@NotNull Continuation<? super Result> continuation) {
|
public @Nullable Object doWork(@NotNull Continuation<? super Result> continuation) {
|
||||||
String taskId = UUID.randomUUID().toString();
|
String taskId = UUID.randomUUID().toString();
|
||||||
boolean result = ChangeDeviceInfoUtil.getDeviceInfoSync(taskId, androidId);
|
boolean result = ChangeDeviceInfoUtil.getDeviceInfoSync(taskId, androidId);
|
||||||
String packageName = ChangeDeviceInfoUtil.packageName;
|
String packageName = ChangeDeviceInfoUtil.packageName;
|
||||||
String zipName = ChangeDeviceInfoUtil.zipName;
|
String zipName = ChangeDeviceInfoUtil.zipName;
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "TAG","doWork: " + result + " " + packageName + " " + zipName, null);
|
LogFileUtil.logAndWrite(Log.INFO, "TAG", "doWork: " + result + " " + packageName + " " + zipName, null);
|
||||||
if (result && !TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(zipName)) {
|
if (result && !TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(zipName)) {
|
||||||
boolean isSuccess = ChangeDeviceInfoUtil.processPackageInfoWithDeviceInfo(packageName, zipName, getApplicationContext(), androidId, taskId);
|
boolean isSuccess = ChangeDeviceInfoUtil.processPackageInfoWithDeviceInfo(packageName, zipName, getApplicationContext(), androidId, taskId);
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
executeSingleLogic(context, packageName);
|
executeSingleLogic(context, packageName);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "TAG", "doWork: get Device info false", null);
|
LogFileUtil.logAndWrite(Log.INFO, "TAG", "doWork: get Device info false", null);
|
||||||
}
|
}
|
||||||
return Result.success();
|
return Result.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 这段代码是LoadDeviceWorker类中的executeSingleLogic方法,用于执行特定逻辑。其功能如下: 检查传入的packageName是否为空或空字符串,如果是,则记录日志并退出方法。 记录代理未激活,并调用startProxyVpn方法启动VPN。
|
||||||
|
* 记录更改设备信息,并调用ChangeDeviceInfoUtil.changeDeviceInfo方法。 记录运行AutoJs脚本,并调用Utils.writePackageName和AutoJsUtil.runAutojsScript方法。
|
||||||
|
*/
|
||||||
public void executeSingleLogic(Context context, String packageName) {
|
public void executeSingleLogic(Context context, String packageName) {
|
||||||
if (packageName == null || packageName.isEmpty()){
|
if (packageName == null || packageName.isEmpty()) {
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Package name is empty", null);
|
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Package name is empty", null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue