refactor(app): 优化代码结构和可读性

- 格式化代码,调整缩进和空格
- 添加方法注释,解释主要功能
- 修改变量命名,提高可读性
- 删除冗余代码和不必要的注释
This commit is contained in:
yjj38 2025-07-11 15:29:27 +08:00
parent c3467add0e
commit d2d95ada82
2 changed files with 94 additions and 83 deletions

View File

@ -94,112 +94,115 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogFileUtil.initialize(this);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
LogFileUtil.initialize(this);
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 (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
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);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestStoragePermission();
}
if (!isNetworkAvailable(this)) {
showToast("网络不可用");
logError("Network not available, closing app.");
finish();
return;
} 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);
}
}
logInfo("onCreate: Setting up work manager");
schedulePeriodicWorkIfNotExists();
if (!isNetworkAvailable(this)) {
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 -> {
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"));
logInfo("onCreate: Setting up UI components");
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();
ShellUtils.execRootCmdAndGetResult("pm grant " + currentPackage + " android.permission.INTERACT_ACROSS_USERS");
ShellUtils.execRootCmdAndGetResult("pm grant " + currentPackage + " android.permission.WRITE_SECURE_SETTINGS");
armClient = new ArmCloudApiClient();
setupButton(R.id.modifyDeviceInfoButton, v -> ChangeDeviceInfoUtil.changeDeviceInfo(currentPackage, this, armClient));
setupButton(R.id.resetDeviceInfoButton, v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(currentPackage, this));
String currentPackage = getPackageName();
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 -> {
((Button) v).setEnabled(false);
startLoadWork();
});
setupButton(R.id.modifyDeviceInfoButton, v -> ChangeDeviceInfoUtil.changeDeviceInfo(currentPackage, this, armClient));
setupButton(R.id.resetDeviceInfoButton, v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(currentPackage, this));
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);
}
});
setupButton(R.id.execute_button, v -> {
((Button) v).setEnabled(false);
startLoadWork();
});
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() {
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
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
);
}
});
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
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 setupButton(int resId, View.OnClickListener listener) {
Button button = findViewById(resId);
if (button != null) {
button.setOnClickListener(listener);
} else {
logInfo("Button not found: " + resId);
}
Button button = findViewById(resId);
if (button != null) {
button.setOnClickListener(listener);
} else {
logInfo("Button not found: " + resId);
}
}
private static String WORK_TAG = "LOAD_WORK";
/**
* 这段代码的功能是启动一个周期性后台任务 创建一个周期为30分钟的PeriodicWorkRequest执行LoadDeviceWorker类的任务 设置初始延迟为0秒添加任务标签WORK_TAG 使用WorkManager将任务加入队列准备执行
*/
private void startLoadWork() {
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.
Builder(LoadDeviceWorker.class, 30, TimeUnit.MINUTES)

View File

@ -36,26 +36,34 @@ public class LoadDeviceWorker extends CoroutineWorker {
this.context = context;
}
/**
* 这段代码是LoadDeviceWorker类中的doWork方法用于执行后台任务其功能如下 生成唯一的taskId调用ChangeDeviceInfoUtil.getDeviceInfoSync获取设备信息 获取packageName和zipName并记录日志
* 如果获取设备信息成功且packageName和zipName非空则调用processPackageInfoWithDeviceInfo处理包信息 若处理成功执行executeSingleLogic方法 否则记录获取设备信息失败的日志 最后返回任务执行结果为成功
*/
@Override
public @Nullable Object doWork(@NotNull Continuation<? super Result> continuation) {
String taskId = UUID.randomUUID().toString();
boolean result = ChangeDeviceInfoUtil.getDeviceInfoSync(taskId, androidId);
String packageName = ChangeDeviceInfoUtil.packageName;
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)) {
boolean isSuccess = ChangeDeviceInfoUtil.processPackageInfoWithDeviceInfo(packageName, zipName, getApplicationContext(), androidId, taskId);
if (isSuccess) {
executeSingleLogic(context, packageName);
}
} 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();
}
/**
* 这段代码是LoadDeviceWorker类中的executeSingleLogic方法用于执行特定逻辑其功能如下 检查传入的packageName是否为空或空字符串如果是则记录日志并退出方法 记录代理未激活并调用startProxyVpn方法启动VPN
* 记录更改设备信息并调用ChangeDeviceInfoUtil.changeDeviceInfo方法 记录运行AutoJs脚本并调用Utils.writePackageName和AutoJsUtil.runAutojsScript方法
*/
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);
return;
}