From d8dd6e740a669f3ab064e4d43744a1721ef8dbdb Mon Sep 17 00:00:00 2001 From: Administrator Date: Thu, 26 Jun 2025 18:18:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=96=E6=B6=88=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E4=BB=BB=E5=8A=A1=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/studyapp/MainActivity.java | 632 +++++++++--------- .../studyapp/device/ChangeDeviceInfoUtil.java | 2 +- .../com/example/studyapp/utils/Utils.java | 26 + app/src/main/res/layout/activity_main.xml | 137 ++-- 4 files changed, 413 insertions(+), 384 deletions(-) diff --git a/app/src/main/java/com/example/studyapp/MainActivity.java b/app/src/main/java/com/example/studyapp/MainActivity.java index b2587a6..832400b 100644 --- a/app/src/main/java/com/example/studyapp/MainActivity.java +++ b/app/src/main/java/com/example/studyapp/MainActivity.java @@ -29,6 +29,7 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.work.PeriodicWorkRequest; import androidx.work.WorkManager; + import com.example.studyapp.autoJS.AutoJsUtil; import com.example.studyapp.device.ChangeDeviceInfoUtil; @@ -52,53 +53,53 @@ import java.util.concurrent.TimeUnit; public class MainActivity extends AppCompatActivity { - private static WeakReference instance; + private static WeakReference instance; - private static final int REQUEST_CODE_STORAGE_PERMISSION = 1; + private static final int REQUEST_CODE_STORAGE_PERMISSION = 1; - private static final int ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE = 1001; + private static final int ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE = 1001; - public static ExecutorService executorService; + public static ExecutorService executorService; - // 假设我们从配置文件中提取出了以下 name 项数据(仅为部分示例数据) - private final String[] proxyNames = { - "mr", "sr", "bq", "ml", "ht", "ga", "mk", "by", "pr", "hr", "hu", - "in", "gt", "at", "kh", "bn", "mg", "kr", "ca", "gh", "ma", "md", - "je", "pa", "ba", "mm", "ir", "gy", "mt", "ae", "es", "ng", "ls", - "ag", "pk", "bd", "kn", "mw", "ve", "hk", "cv", "hn", "tm", "us", - "cz", "ly", "gb", "kz", "it", "bh", "sn", "fi", "co", "sx", "bm", - "fj", "cw", "st", "bw", "fr", "bb", "tg", "ci", "gd", "ne", "bj", - "nz", "rs", "do", "cl", "lb", "nl", "re", "aw", "ug", "sv", "ar", - "jo", "bg", "jp", "rw", "py", "mn", "ec", "uz", "ro", "cu", "gu", - "xk", "sy", "so", "zm", "tz", "ni", "sc", "my", "gf", "na", "zw", - "la", "et", "ao", "ua", "om", "np", "mx", "mz", "dm", "ye", "gi", - "cr", "cm", "ph", "am", "th", "ch", "br", "sd", "ie", "bo", "bs", - "tc", "vg", "pe", "sa", "dk", "tn", "ee", "jm", "lc", "pt", "qa", - "ge", "ps" - }; + // 假设我们从配置文件中提取出了以下 name 项数据(仅为部分示例数据) + private final String[] proxyNames = { + "mr", "sr", "bq", "ml", "ht", "ga", "mk", "by", "pr", "hr", "hu", + "in", "gt", "at", "kh", "bn", "mg", "kr", "ca", "gh", "ma", "md", + "je", "pa", "ba", "mm", "ir", "gy", "mt", "ae", "es", "ng", "ls", + "ag", "pk", "bd", "kn", "mw", "ve", "hk", "cv", "hn", "tm", "us", + "cz", "ly", "gb", "kz", "it", "bh", "sn", "fi", "co", "sx", "bm", + "fj", "cw", "st", "bw", "fr", "bb", "tg", "ci", "gd", "ne", "bj", + "nz", "rs", "do", "cl", "lb", "nl", "re", "aw", "ug", "sv", "ar", + "jo", "bg", "jp", "rw", "py", "mn", "ec", "uz", "ro", "cu", "gu", + "xk", "sy", "so", "zm", "tz", "ni", "sc", "my", "gf", "na", "zw", + "la", "et", "ao", "ua", "om", "np", "mx", "mz", "dm", "ye", "gi", + "cr", "cm", "ph", "am", "th", "ch", "br", "sd", "ie", "bo", "bs", + "tc", "vg", "pe", "sa", "dk", "tn", "ee", "jm", "lc", "pt", "qa", + "ge", "ps" + }; - public static volatile String scriptResult; + public static volatile String scriptResult; - // 初始化 ExecutorService - private void initializeExecutorService() { - if (executorService == null || executorService.isShutdown()) { - executorService = new ThreadPoolExecutor( - 1, // 核心线程数 - 1, // 最大线程数 - 0L, TimeUnit.MILLISECONDS, - new LinkedBlockingQueue<>(50), // 阻塞队列 - new ThreadPoolExecutor.AbortPolicy() // 拒绝策略 - ); + // 初始化 ExecutorService + private void initializeExecutorService() { + if (executorService == null || executorService.isShutdown()) { + executorService = new ThreadPoolExecutor( + 1, // 核心线程数 + 1, // 最大线程数 + 0L, TimeUnit.MILLISECONDS, + new LinkedBlockingQueue<>(50), // 阻塞队列 + new ThreadPoolExecutor.AbortPolicy() // 拒绝策略 + ); + } } - } - /** - * 获取 Android 设备的 ANDROID_ID - * - * @param context 应用上下文 - * @return 设备的 ANDROID_ID,若无法获取,则返回 null - */ - private String getAndroidId(Context context) { + /** + * 获取 Android 设备的 ANDROID_ID + * + * @param context 应用上下文 + * @return 设备的 ANDROID_ID,若无法获取,则返回 null + */ + private String getAndroidId(Context context) { // if (context == null) { // LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "getAndroidId: Context cannot be null",null); // throw new IllegalArgumentException("Context cannot be null"); @@ -112,314 +113,301 @@ public class MainActivity extends AppCompatActivity { // LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "getAndroidId: Failed to get ANDROID_ID",e); // return null; // } - return "FyZqWrStUvOpKlMn"; - } - - - private static final int REQUEST_CODE_PERMISSIONS = 100; - - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - LogFileUtil.initialize(this); - setContentView(R.layout.activity_main); - instance = new WeakReference<>(this); - - LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Initializing application",null); - initializeExecutorService(); - System.setProperty("java.library.path", this.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 - ); - } - } 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)) { - Toast.makeText(this, "Network is not available", Toast.LENGTH_SHORT).show(); - LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "Network not available, closing app.",null); - finish(); + return "FyZqWrStUvOpKlMn"; } - LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up work manager",null); - PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(CheckAccessibilityWorker.class, 15, TimeUnit.MINUTES) - .build(); - WorkManager.getInstance(this).enqueue(workRequest); - LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up UI components",null); - Button runScriptButton = findViewById(R.id.run_script_button); - if (runScriptButton != null) { -// runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this)); - runScriptButton.setOnClickListener(v -> { + private static final int REQUEST_CODE_PERMISSIONS = 100; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + LogFileUtil.initialize(this); + setContentView(R.layout.activity_main); + instance = new WeakReference<>(this); + + LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Initializing application", null); + initializeExecutorService(); + System.setProperty("java.library.path", this.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 + ); + } + } 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)) { + Toast.makeText(this, "Network is not available", Toast.LENGTH_SHORT).show(); + LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "Network not available, closing app.", null); + finish(); + } + + LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up work manager", null); + PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(CheckAccessibilityWorker.class, 15, TimeUnit.MINUTES) + .build(); + WorkManager.getInstance(this).enqueue(workRequest); + + LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up UI components", null); + Button runScriptButton = findViewById(R.id.run_script_button); + if (runScriptButton != null) { + runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this)); + } else { + LogFileUtil.logAndWrite(Log.WARN, "MainActivity", "Run Script Button not found", null); + Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show(); + } + + Button connectButton = findViewById(R.id.connectVpnButton); + if (connectButton != null) { + connectButton.setOnClickListener(v -> startProxyVpn(this)); + } else { + Toast.makeText(this, "Connect button not found", Toast.LENGTH_SHORT).show(); + } + + Button disconnectButton = findViewById(R.id.disconnectVpnButton); + if (disconnectButton != null) { + disconnectButton.setOnClickListener(v -> ClashUtil.stopProxy(this)); + } else { + Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show(); + } + + Button switchVpnButton = findViewById(R.id.switchVpnButton); + if (switchVpnButton != null) { + switchVpnButton.setOnClickListener(v -> ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170")); + } else { + Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show(); + } + + Button modifyDeviceInfoButton = findViewById(R.id.modifyDeviceInfoButton); + if (modifyDeviceInfoButton != null) { + modifyDeviceInfoButton.setOnClickListener(v -> ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this)); + } else { + Toast.makeText(this, "modifyDeviceInfo button not found", Toast.LENGTH_SHORT).show(); + } + + Button resetDeviceInfoButton = findViewById(R.id.resetDeviceInfoButton); + if (resetDeviceInfoButton != null) { + resetDeviceInfoButton.setOnClickListener(v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(getPackageName(), this)); + } else { + Toast.makeText(this, "resetDeviceInfo button not found", Toast.LENGTH_SHORT).show(); + } + + // 初始化 ChangeDeviceInfoUtil + String androidId = getAndroidId(this); + String taskId = UUID.randomUUID().toString(); +// ChangeDeviceInfoUtil.initialize("US", 2, this, androidId); + // 获取输入框和按钮 + Button executeButton = findViewById(R.id.execute_button); + Button stopExecuteButton = findViewById(R.id.stop_execute_button); + + // 设置按钮的点击事件 + if (executeButton != null) { + executeButton.setOnClickListener(v -> { + executeButton.setEnabled(false); +// Toast.makeText(this, "任务正在执行", Toast.LENGTH_SHORT).show(); +// executeLogic(androidId,taskId); + startLoadWork(); + }); + } + if (stopExecuteButton != null) { + stopExecuteButton.setOnClickListener(v -> { +// if (executorService != null && !executorService.isShutdown()) { +// executorService.shutdownNow(); +// ClashUtil.stopProxy(this); +// AutoJsUtil.stopAutojsScript(this); +// executeButton.setEnabled(true); +// } + WorkManager.getInstance(this).cancelAllWorkByTag(WORK_TAG); + executeButton.setEnabled(true); + }); + } else { + Toast.makeText(this, "Stop button not found", Toast.LENGTH_SHORT).show(); + } + } + + private static String WORK_TAG = "LOAD_WORK"; + + private void startLoadWork() { + PeriodicWorkRequest workRequest = new PeriodicWorkRequest. + Builder(LoadDeviceWorker.class, 30, TimeUnit.MINUTES) + .setInitialDelay(0, TimeUnit.SECONDS) + .addTag(WORK_TAG) + .build(); + WorkManager.getInstance(this).enqueue(workRequest); + } + + private void executeLogic(String androidId, String taskId) { + LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Start execution", null); + + if (!isNetworkAvailable(this)) { + LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Network is not available!", null); + Toast.makeText(this, "网络不可用,请检查网络连接", Toast.LENGTH_SHORT).show(); + return; + } + + LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Submitting job to executor", null); initializeExecutorService(); executorService.submit(() -> { - File files1Dir = new File(getExternalFilesDir(null).getAbsolutePath(),"FyZqWrStUvOpKlMn_wsj.reader_sp.zip"); - File destFile = new File(Environment.getExternalStorageDirectory(), "apkpath"); - Log.d("TAG", "onCreate: "+destFile.getAbsolutePath()); - try { -// ZipUtils.unzip(files1Dir.getAbsolutePath(), destFile.getAbsolutePath()); - if (destFile.exists()) { - ChangeDeviceInfoUtil.installApk(destFile.getAbsolutePath()); + try { + AutoJsUtil.registerScriptResultReceiver(this); + AutoJsUtil.flag = true; + ChangeDeviceInfoUtil.getDeviceInfo(taskId, androidId); + executeSingleLogic(); + while (isRunning) { + if (!isRunning) break; + + // 从队列中获取最新的 scriptResult + LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script", null); + String currentScriptResult = scriptResultQueue.take(); + ChangeDeviceInfoUtil.getDeviceInfo(taskId, androidId); + ChangeDeviceInfoUtil.processPackageInfo(TaskUtil.getPackageInfo(androidId), this); + executeSingleLogic(); + TaskUtil.execSaveTask(this, androidId, taskId, currentScriptResult); + LogFileUtil.logAndWrite(android.util.Log.DEBUG, "MainActivity", "----发送result------;" + currentScriptResult, null); + if (currentScriptResult != null && !TextUtils.isEmpty(currentScriptResult)) { + infoUpload(this, androidId, currentScriptResult); + } + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Thread interrupted while waiting", e); + } catch (Exception e) { + LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Unexpected task error.", e); } - } catch (Exception e) { - e.printStackTrace(); - } }); - - }); - } else { - LogFileUtil.logAndWrite(Log.WARN, "MainActivity", "Run Script Button not found",null); - Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show(); } - Button connectButton = findViewById(R.id.connectVpnButton); - if (connectButton != null) { - connectButton.setOnClickListener(v -> { - String chmodResult = ShellUtils.execRootCmdAndGetResult("pm uninstall com.rovio.baba"); - }); -// connectButton.setOnClickListener(v -> startProxyVpn(this)); - } else { - Toast.makeText(this, "Connect button not found", Toast.LENGTH_SHORT).show(); + public static final LinkedBlockingQueue scriptResultQueue = new LinkedBlockingQueue<>(); + private volatile boolean isRunning = true; // 主线程运行状态 + public static final Object taskLock = new Object(); // 任务逻辑锁 + + public void executeSingleLogic() { + LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Proxy not active, starting VPN", null); + startProxyVpn(this); + LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Changing device info", null); + ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this); + LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script", null); + AutoJsUtil.runAutojsScript(this); } - Button disconnectButton = findViewById(R.id.disconnectVpnButton); - if (disconnectButton != null) { - disconnectButton.setOnClickListener(v -> ClashUtil.stopProxy(this)); - } else { - Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show(); - } - Button switchVpnButton = findViewById(R.id.switchVpnButton); - if (switchVpnButton != null) { - switchVpnButton.setOnClickListener(v -> ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170")); - } else { - Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show(); - } - - Button modifyDeviceInfoButton = findViewById(R.id.modifyDeviceInfoButton); - if (modifyDeviceInfoButton != null) { - modifyDeviceInfoButton.setOnClickListener(v -> ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this)); - } else { - Toast.makeText(this, "modifyDeviceInfo button not found", Toast.LENGTH_SHORT).show(); - } - - Button resetDeviceInfoButton = findViewById(R.id.resetDeviceInfoButton); - if (resetDeviceInfoButton != null) { - resetDeviceInfoButton.setOnClickListener(v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(getPackageName(), this)); - } else { - Toast.makeText(this, "resetDeviceInfo button not found", Toast.LENGTH_SHORT).show(); - } - - // 初始化 ChangeDeviceInfoUtil - String androidId = getAndroidId(this); - String taskId = UUID.randomUUID().toString(); -// ChangeDeviceInfoUtil.initialize("US", 2, this, androidId); - // 获取输入框和按钮 - Button executeButton = findViewById(R.id.execute_button); - Button stopExecuteButton = findViewById(R.id.stop_execute_button); - - // 设置按钮的点击事件 - if (executeButton != null) { - executeButton.setOnClickListener(v -> { - executeButton.setEnabled(false); - Toast.makeText(this, "任务正在执行", Toast.LENGTH_SHORT).show(); -// executeLogic(androidId,taskId); - startLoadWork(); - }); - } - if (stopExecuteButton != null) { - stopExecuteButton.setOnClickListener(v -> { - if (executorService != null && !executorService.isShutdown()) { - executorService.shutdownNow(); - ClashUtil.stopProxy(this); - AutoJsUtil.stopAutojsScript(this); - executeButton.setEnabled(true); + private void startProxyVpn(Context context) { + if (!isNetworkAvailable(context)) { + Toast.makeText(context, "Network is not available", Toast.LENGTH_SHORT).show(); + LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Network is not available.", null); + return; } - }); - } else { - Toast.makeText(this, "Stop button not found", Toast.LENGTH_SHORT).show(); - } - } - private void startLoadWork(){ - PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(LoadDeviceWorker.class, 15, TimeUnit.MINUTES) - .setInitialDelay(0, TimeUnit.SECONDS).build(); - WorkManager.getInstance(this).enqueue(workRequest); - } - - private void executeLogic(String androidId, String taskId) { - LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Start execution",null); - - if (!isNetworkAvailable(this)) { - LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Network is not available!",null); - Toast.makeText(this, "网络不可用,请检查网络连接", Toast.LENGTH_SHORT).show(); - return; - } - - LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Submitting job to executor",null); - initializeExecutorService(); - executorService.submit(() -> { - try { - AutoJsUtil.registerScriptResultReceiver(this); - AutoJsUtil.flag = true; - ChangeDeviceInfoUtil.getDeviceInfo(taskId, androidId); - executeSingleLogic(); - while (isRunning) { - if (!isRunning) break; - - // 从队列中获取最新的 scriptResult - LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script",null); - String currentScriptResult = scriptResultQueue.take(); - ChangeDeviceInfoUtil.getDeviceInfo(taskId, androidId); - ChangeDeviceInfoUtil.processPackageInfo(TaskUtil.getPackageInfo(androidId), this); - executeSingleLogic(); - TaskUtil.execSaveTask(this, androidId, taskId, currentScriptResult); - LogFileUtil.logAndWrite(android.util.Log.DEBUG, "MainActivity", "----发送result------;" + currentScriptResult, null); - if (currentScriptResult != null && !TextUtils.isEmpty(currentScriptResult)) { - infoUpload(this, androidId, currentScriptResult); - } + if (!(context instanceof Activity)) { + Toast.makeText(context, "Context must be an Activity", Toast.LENGTH_SHORT).show(); + LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Context is not an Activity.", null); + return; } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Thread interrupted while waiting", e); - } catch (Exception e) { - LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Unexpected task error.", e); - } - }); - } - public static final LinkedBlockingQueue scriptResultQueue = new LinkedBlockingQueue<>(); - private volatile boolean isRunning = true; // 主线程运行状态 - public static final Object taskLock = new Object(); // 任务逻辑锁 - - public void executeSingleLogic() { - LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Proxy not active, starting VPN",null); - startProxyVpn(this); - LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Changing device info",null); - ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this); - LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script",null); - AutoJsUtil.runAutojsScript(this); - } - - - private void startProxyVpn(Context context) { - if (!isNetworkAvailable(context)) { - Toast.makeText(context, "Network is not available", Toast.LENGTH_SHORT).show(); - LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Network is not available.",null); - return; - } - - if (!(context instanceof Activity)) { - Toast.makeText(context, "Context must be an Activity", Toast.LENGTH_SHORT).show(); - LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Context is not an Activity.",null); - return; - } - - try { - ClashUtil.startProxy(context); // 在主线程中调用 - ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170"); - } catch (Exception e) { - LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Failed to start VPN",e); - Toast.makeText(context, "Failed to start VPN: " + (e.getMessage() != null ? e.getMessage() : "Unknown error"), Toast.LENGTH_SHORT).show(); - } - } - - @Override - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { - super.onRequestPermissionsResult(requestCode, permissions, grantResults); - if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) { - if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show(); - } else { - // 提示权限被拒绝,同时允许用户重新授予权限 - showPermissionExplanationDialog(); - } - } - if (requestCode == REQUEST_CODE_PERMISSIONS) { - boolean allGranted = true; - for (int result : grantResults) { - if (result != PackageManager.PERMISSION_GRANTED) { - allGranted = false; - break; + try { + ClashUtil.startProxy(context); // 在主线程中调用 + ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170"); + } catch (Exception e) { + LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Failed to start VPN", e); + Toast.makeText(context, "Failed to start VPN: " + (e.getMessage() != null ? e.getMessage() : "Unknown error"), Toast.LENGTH_SHORT).show(); } - } - - if (allGranted) { - // 所有权限已授予 - startMyForegroundService(); - } else { - Toast.makeText(this, "未授予必要权限,请检查设置", Toast.LENGTH_SHORT).show(); - } } - } - private void startMyForegroundService() { - Intent serviceIntent = new Intent(this, MyAccessibilityService.class); - ContextCompat.startForegroundService(this, serviceIntent); - } + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) { + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show(); + } else { + // 提示权限被拒绝,同时允许用户重新授予权限 + showPermissionExplanationDialog(); + } + } + if (requestCode == REQUEST_CODE_PERMISSIONS) { + boolean allGranted = true; + for (int result : grantResults) { + if (result != PackageManager.PERMISSION_GRANTED) { + allGranted = false; + break; + } + } - - @Override - protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { - super.onActivityResult(requestCode, resultCode, data); - - switch (requestCode) { - case ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE: - handleStoragePermissionResult(resultCode); - break; - default: - break; + if (allGranted) { + // 所有权限已授予 + startMyForegroundService(); + } else { + Toast.makeText(this, "未授予必要权限,请检查设置", Toast.LENGTH_SHORT).show(); + } + } } - } - private void handleStoragePermissionResult(int resultCode) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && Environment.isExternalStorageManager()) { - Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show(); - } else { - Toast.makeText(this, "请授予所有文件管理权限", Toast.LENGTH_SHORT).show(); - finish(); + private void startMyForegroundService() { + Intent serviceIntent = new Intent(this, MyAccessibilityService.class); + ContextCompat.startForegroundService(this, serviceIntent); } - } - private void showPermissionExplanationDialog() { - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle("Permission Required") - .setMessage("Storage Permission is required for the app to function. Please enable it in Settings.") - .setPositiveButton("Go to Settings", (dialog, which) -> { - Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); - Uri uri = Uri.fromParts("package", getPackageName(), null); - intent.setData(uri); - startActivity(intent); - }) - .setNegativeButton("Cancel", (dialog, which) -> finish()) - .show(); - } - @Override - protected void onDestroy() { - LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onDestroy: Cleaning up resources",null); - super.onDestroy(); - instance.clear(); - if (AutoJsUtil.scriptResultReceiver != null) { - unregisterReceiver(AutoJsUtil.scriptResultReceiver); - AutoJsUtil.scriptResultReceiver = null; + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + switch (requestCode) { + case ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE: + handleStoragePermissionResult(resultCode); + break; + default: + break; + } } - if (executorService != null) { - executorService.shutdown(); - } - isRunning = false; - } - public static MainActivity getInstance() { - return instance.get(); // 返回实例 - } + private void handleStoragePermissionResult(int resultCode) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && Environment.isExternalStorageManager()) { + Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(this, "请授予所有文件管理权限", Toast.LENGTH_SHORT).show(); + finish(); + } + } + + private void showPermissionExplanationDialog() { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle("Permission Required") + .setMessage("Storage Permission is required for the app to function. Please enable it in Settings.") + .setPositiveButton("Go to Settings", (dialog, which) -> { + Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); + Uri uri = Uri.fromParts("package", getPackageName(), null); + intent.setData(uri); + startActivity(intent); + }) + .setNegativeButton("Cancel", (dialog, which) -> finish()) + .show(); + } + + @Override + protected void onDestroy() { + LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onDestroy: Cleaning up resources", null); + super.onDestroy(); + instance.clear(); + if (AutoJsUtil.scriptResultReceiver != null) { + unregisterReceiver(AutoJsUtil.scriptResultReceiver); + AutoJsUtil.scriptResultReceiver = null; + } + if (executorService != null) { + executorService.shutdown(); + } + isRunning = false; + } + + public static MainActivity getInstance() { + return instance.get(); // 返回实例 + } } diff --git a/app/src/main/java/com/example/studyapp/device/ChangeDeviceInfoUtil.java b/app/src/main/java/com/example/studyapp/device/ChangeDeviceInfoUtil.java index c767734..98ff3a5 100644 --- a/app/src/main/java/com/example/studyapp/device/ChangeDeviceInfoUtil.java +++ b/app/src/main/java/com/example/studyapp/device/ChangeDeviceInfoUtil.java @@ -214,7 +214,7 @@ public class ChangeDeviceInfoUtil { private static boolean processPackage(String packageName, String zipName, Context context) { try { File filesDir = new File(context.getExternalFilesDir(null).getAbsolutePath()); - File file = TaskUtil.downloadCodeFile("FyZqWrStUvOpKlMn_wsj.reader_sp.zip", filesDir); + File file = TaskUtil.downloadCodeFile(zipName, filesDir); if (file != null && file.exists()) { File destFile = new File(context.getCacheDir(), packageName); diff --git a/app/src/main/java/com/example/studyapp/utils/Utils.java b/app/src/main/java/com/example/studyapp/utils/Utils.java index 1c76ad5..ad6c7fe 100644 --- a/app/src/main/java/com/example/studyapp/utils/Utils.java +++ b/app/src/main/java/com/example/studyapp/utils/Utils.java @@ -1,6 +1,8 @@ package com.example.studyapp.utils; import android.content.Context; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkCapabilities; @@ -12,6 +14,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.List; public class Utils { @@ -28,6 +31,29 @@ public class Utils { return false; } + public static boolean isAppInstalled(Context context, String packageName) { + try { + PackageManager pm = context.getPackageManager(); + pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); + return true; + } catch (PackageManager.NameNotFoundException e) { + return false; + } + } + + public static boolean isPackageInstalled(Context context, String packageName) { + PackageManager pm = context.getPackageManager(); + List packages = pm.getInstalledApplications(0); + + for (ApplicationInfo packageInfo : packages) { + Log.d("TAG", "isPackageInstalled: "+packageInfo.packageName); + if (packageInfo.packageName.equals(packageName)) { + return true; + } + } + return false; + } + public static void writePackageName(String packageName){ File file = new File(Environment.getExternalStorageDirectory(), "script/packagesname.txt"); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f6adc8b..fb2dfd4 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,87 +1,102 @@ + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:padding="16dp" + tools:context=".MainActivity"> + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:divider="@android:color/darker_gray" + android:dividerPadding="8dp" + android:orientation="vertical" + android:showDividers="middle"> + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:gravity="center" + android:padding="8dp" + android:text="VPN 操作" + android:textAppearance="@style/TextAppearance.AppCompat.Medium" + android:visibility="gone" />