From d7774c2ac6220467808ba6e755354e41dabddf26 Mon Sep 17 00:00:00 2001 From: yjj38 Date: Thu, 10 Jul 2025 17:13:42 +0800 Subject: [PATCH] =?UTF-8?q?feat(app):=20=E4=BC=98=E5=8C=96=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E5=88=9D=E5=A7=8B=E5=8C=96=E5=92=8C=E6=9D=83=E9=99=90?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构了 MainActivity 中的 onCreate 方法,优化了应用初始化流程 - 添加了请求存储权限的独立方法 requestStoragePermission - 实现了只在工作不存在时调度周期性工作的逻辑 schedulePeriodicWorkIfNotExists - 优化了按钮点击事件的处理,提高了代码可读性和健壮性 - 新增了部署配置文件 deployment.xml,用于腾讯云服务器配置 --- .idea/deployment.xml | 14 ++ .idea/misc.xml | 4 + .../com/example/retention/MainActivity.java | 153 ++++++++++-------- 3 files changed, 106 insertions(+), 65 deletions(-) create mode 100644 .idea/deployment.xml diff --git a/.idea/deployment.xml b/.idea/deployment.xml new file mode 100644 index 0000000..40710bf --- /dev/null +++ b/.idea/deployment.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 8562ed5..f2f4035 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,9 @@ + + + + diff --git a/app/src/main/java/com/example/retention/MainActivity.java b/app/src/main/java/com/example/retention/MainActivity.java index 89abb91..6189420 100644 --- a/app/src/main/java/com/example/retention/MainActivity.java +++ b/app/src/main/java/com/example/retention/MainActivity.java @@ -25,6 +25,7 @@ import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.appcompat.app.AppCompatActivity; +import androidx.work.ExistingPeriodicWorkPolicy; import androidx.work.PeriodicWorkRequest; import androidx.work.WorkManager; @@ -38,6 +39,7 @@ import com.example.retention.proxy.ClashUtil; import com.example.retention.service.MyAccessibilityService; import com.example.retention.task.TaskUtil; import com.example.retention.utils.LogFileUtil; +import com.example.retention.utils.ShellUtils; import com.example.retention.worker.CheckAccessibilityWorker; import com.example.retention.worker.LoadDeviceWorker; @@ -92,87 +94,108 @@ 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) { - ActivityCompat.requestPermissions( - this, - new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, - REQUEST_CODE_STORAGE_PERMISSION - ); + 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); + } } - } 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 (!isNetworkAvailable(this)) { + showToast("网络不可用"); + logError("Network not available, closing app."); + finish(); + return; } - } - if (!isNetworkAvailable(this)) { - showToast("网络不可用"); - logError("Network not available, closing app."); - finish(); - return; - } + logInfo("onCreate: Setting up work manager"); + schedulePeriodicWorkIfNotExists(); - logInfo("onCreate: Setting up work manager"); - PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(CheckAccessibilityWorker.class, 15, TimeUnit.MINUTES) - .build(); - WorkManager.getInstance(this).enqueue(workRequest); + logInfo("onCreate: Setting up UI components"); - logInfo("onCreate: Setting up UI components"); - - setupButton(R.id.run_script_button, v -> AutoJsUtil.runAutojsScript(this)); - 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.modifyDeviceInfoButton, v -> ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this, armClient)); - setupButton(R.id.resetDeviceInfoButton, v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(getPackageName(), this)); - - Button executeButton = findViewById(R.id.execute_button); - Button stopExecuteButton = findViewById(R.id.stop_execute_button); - - if (executeButton != null) { - executeButton.setOnClickListener(v -> { - executeButton.setEnabled(false); - startLoadWork(); + 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")); - if (stopExecuteButton != null) { - stopExecuteButton.setOnClickListener(v -> { - WorkManager.getInstance(this).cancelAllWorkByTag(WORK_TAG); - executeButton.setEnabled(true); + armClient = new ArmCloudApiClient(); + + 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.modifyDeviceInfoButton, v -> ChangeDeviceInfoUtil.changeDeviceInfo(currentPackage, this, armClient)); + setupButton(R.id.resetDeviceInfoButton, v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(currentPackage, this)); + + setupButton(R.id.execute_button, v -> { + ((Button) v).setEnabled(false); + startLoadWork(); }); - } else { - showToast("Stop button not found"); - } + + 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 + ); + } + }); } + private void setupButton(int resId, View.OnClickListener listener) { - Button button = findViewById(resId); - if (button != null) { - button.setOnClickListener(listener); - } else { - String msg = getResources().getResourceEntryName(resId) + " not found"; - logWarn(msg); - showToast(msg); - } + Button button = findViewById(resId); + if (button != null) { + button.setOnClickListener(listener); + } else { + logInfo("Button not found: " + resId); + } } private static String WORK_TAG = "LOAD_WORK";