diff --git a/app/src/main/java/com/example/retention/MainActivity.java b/app/src/main/java/com/example/retention/MainActivity.java index 6189420..242b97a 100644 --- a/app/src/main/java/com/example/retention/MainActivity.java +++ b/app/src/main/java/com/example/retention/MainActivity.java @@ -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) diff --git a/app/src/main/java/com/example/retention/worker/LoadDeviceWorker.java b/app/src/main/java/com/example/retention/worker/LoadDeviceWorker.java index cf835e9..f649161 100644 --- a/app/src/main/java/com/example/retention/worker/LoadDeviceWorker.java +++ b/app/src/main/java/com/example/retention/worker/LoadDeviceWorker.java @@ -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 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; }