添加取消定时任务的方法

This commit is contained in:
Administrator 2025-06-26 18:18:37 +08:00
parent 203fccea87
commit d8dd6e740a
4 changed files with 413 additions and 384 deletions

View File

@ -29,6 +29,7 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.work.PeriodicWorkRequest; import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager; import androidx.work.WorkManager;
import com.example.studyapp.autoJS.AutoJsUtil; import com.example.studyapp.autoJS.AutoJsUtil;
import com.example.studyapp.device.ChangeDeviceInfoUtil; import com.example.studyapp.device.ChangeDeviceInfoUtil;
@ -52,53 +53,53 @@ import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private static WeakReference<MainActivity> instance; private static WeakReference<MainActivity> 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 项数据仅为部分示例数据 // 假设我们从配置文件中提取出了以下 name 项数据仅为部分示例数据
private final String[] proxyNames = { private final String[] proxyNames = {
"mr", "sr", "bq", "ml", "ht", "ga", "mk", "by", "pr", "hr", "hu", "mr", "sr", "bq", "ml", "ht", "ga", "mk", "by", "pr", "hr", "hu",
"in", "gt", "at", "kh", "bn", "mg", "kr", "ca", "gh", "ma", "md", "in", "gt", "at", "kh", "bn", "mg", "kr", "ca", "gh", "ma", "md",
"je", "pa", "ba", "mm", "ir", "gy", "mt", "ae", "es", "ng", "ls", "je", "pa", "ba", "mm", "ir", "gy", "mt", "ae", "es", "ng", "ls",
"ag", "pk", "bd", "kn", "mw", "ve", "hk", "cv", "hn", "tm", "us", "ag", "pk", "bd", "kn", "mw", "ve", "hk", "cv", "hn", "tm", "us",
"cz", "ly", "gb", "kz", "it", "bh", "sn", "fi", "co", "sx", "bm", "cz", "ly", "gb", "kz", "it", "bh", "sn", "fi", "co", "sx", "bm",
"fj", "cw", "st", "bw", "fr", "bb", "tg", "ci", "gd", "ne", "bj", "fj", "cw", "st", "bw", "fr", "bb", "tg", "ci", "gd", "ne", "bj",
"nz", "rs", "do", "cl", "lb", "nl", "re", "aw", "ug", "sv", "ar", "nz", "rs", "do", "cl", "lb", "nl", "re", "aw", "ug", "sv", "ar",
"jo", "bg", "jp", "rw", "py", "mn", "ec", "uz", "ro", "cu", "gu", "jo", "bg", "jp", "rw", "py", "mn", "ec", "uz", "ro", "cu", "gu",
"xk", "sy", "so", "zm", "tz", "ni", "sc", "my", "gf", "na", "zw", "xk", "sy", "so", "zm", "tz", "ni", "sc", "my", "gf", "na", "zw",
"la", "et", "ao", "ua", "om", "np", "mx", "mz", "dm", "ye", "gi", "la", "et", "ao", "ua", "om", "np", "mx", "mz", "dm", "ye", "gi",
"cr", "cm", "ph", "am", "th", "ch", "br", "sd", "ie", "bo", "bs", "cr", "cm", "ph", "am", "th", "ch", "br", "sd", "ie", "bo", "bs",
"tc", "vg", "pe", "sa", "dk", "tn", "ee", "jm", "lc", "pt", "qa", "tc", "vg", "pe", "sa", "dk", "tn", "ee", "jm", "lc", "pt", "qa",
"ge", "ps" "ge", "ps"
}; };
public static volatile String scriptResult; public static volatile String scriptResult;
// 初始化 ExecutorService // 初始化 ExecutorService
private void initializeExecutorService() { private void initializeExecutorService() {
if (executorService == null || executorService.isShutdown()) { if (executorService == null || executorService.isShutdown()) {
executorService = new ThreadPoolExecutor( executorService = new ThreadPoolExecutor(
1, // 核心线程数 1, // 核心线程数
1, // 最大线程数 1, // 最大线程数
0L, TimeUnit.MILLISECONDS, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(50), // 阻塞队列 new LinkedBlockingQueue<>(50), // 阻塞队列
new ThreadPoolExecutor.AbortPolicy() // 拒绝策略 new ThreadPoolExecutor.AbortPolicy() // 拒绝策略
); );
}
} }
}
/** /**
* 获取 Android 设备的 ANDROID_ID * 获取 Android 设备的 ANDROID_ID
* *
* @param context 应用上下文 * @param context 应用上下文
* @return 设备的 ANDROID_ID若无法获取则返回 null * @return 设备的 ANDROID_ID若无法获取则返回 null
*/ */
private String getAndroidId(Context context) { private String getAndroidId(Context context) {
// if (context == null) { // if (context == null) {
// LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "getAndroidId: Context cannot be null",null); // LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "getAndroidId: Context cannot be null",null);
// throw new IllegalArgumentException("Context cannot be 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); // LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "getAndroidId: Failed to get ANDROID_ID",e);
// return null; // return null;
// } // }
return "FyZqWrStUvOpKlMn"; 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();
} }
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); private static final int REQUEST_CODE_PERMISSIONS = 100;
Button runScriptButton = findViewById(R.id.run_script_button);
if (runScriptButton != null) { @Override
// runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this)); protected void onCreate(@Nullable Bundle savedInstanceState) {
runScriptButton.setOnClickListener(v -> { 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(); initializeExecutorService();
executorService.submit(() -> { executorService.submit(() -> {
File files1Dir = new File(getExternalFilesDir(null).getAbsolutePath(),"FyZqWrStUvOpKlMn_wsj.reader_sp.zip"); try {
File destFile = new File(Environment.getExternalStorageDirectory(), "apkpath"); AutoJsUtil.registerScriptResultReceiver(this);
Log.d("TAG", "onCreate: "+destFile.getAbsolutePath()); AutoJsUtil.flag = true;
try { ChangeDeviceInfoUtil.getDeviceInfo(taskId, androidId);
// ZipUtils.unzip(files1Dir.getAbsolutePath(), destFile.getAbsolutePath()); executeSingleLogic();
if (destFile.exists()) { while (isRunning) {
ChangeDeviceInfoUtil.installApk(destFile.getAbsolutePath()); 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); public static final LinkedBlockingQueue<String> scriptResultQueue = new LinkedBlockingQueue<>();
if (connectButton != null) { private volatile boolean isRunning = true; // 主线程运行状态
connectButton.setOnClickListener(v -> { public static final Object taskLock = new Object(); // 任务逻辑锁
String chmodResult = ShellUtils.execRootCmdAndGetResult("pm uninstall com.rovio.baba");
}); public void executeSingleLogic() {
// connectButton.setOnClickListener(v -> startProxyVpn(this)); LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Proxy not active, starting VPN", null);
} else { startProxyVpn(this);
Toast.makeText(this, "Connect button not found", Toast.LENGTH_SHORT).show(); 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); private void startProxyVpn(Context context) {
if (switchVpnButton != null) { if (!isNetworkAvailable(context)) {
switchVpnButton.setOnClickListener(v -> ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170")); Toast.makeText(context, "Network is not available", Toast.LENGTH_SHORT).show();
} else { LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Network is not available.", null);
Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show(); return;
}
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);
} }
});
} else {
Toast.makeText(this, "Stop button not found", Toast.LENGTH_SHORT).show();
}
}
private void startLoadWork(){ if (!(context instanceof Activity)) {
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(LoadDeviceWorker.class, 15, TimeUnit.MINUTES) Toast.makeText(context, "Context must be an Activity", Toast.LENGTH_SHORT).show();
.setInitialDelay(0, TimeUnit.SECONDS).build(); LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Context is not an Activity.", null);
WorkManager.getInstance(this).enqueue(workRequest); return;
}
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);
}
} }
} 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<String> scriptResultQueue = new LinkedBlockingQueue<>(); try {
private volatile boolean isRunning = true; // 主线程运行状态 ClashUtil.startProxy(context); // 在主线程中调用
public static final Object taskLock = new Object(); // 任务逻辑锁 ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170");
} catch (Exception e) {
public void executeSingleLogic() { LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Failed to start VPN", e);
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Proxy not active, starting VPN",null); Toast.makeText(context, "Failed to start VPN: " + (e.getMessage() != null ? e.getMessage() : "Unknown error"), Toast.LENGTH_SHORT).show();
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;
} }
}
if (allGranted) {
// 所有权限已授予
startMyForegroundService();
} else {
Toast.makeText(this, "未授予必要权限,请检查设置", Toast.LENGTH_SHORT).show();
}
} }
}
private void startMyForegroundService() { @Override
Intent serviceIntent = new Intent(this, MyAccessibilityService.class); public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
ContextCompat.startForegroundService(this, serviceIntent); 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;
}
}
if (allGranted) {
@Override // 所有权限已授予
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { startMyForegroundService();
super.onActivityResult(requestCode, resultCode, data); } else {
Toast.makeText(this, "未授予必要权限,请检查设置", Toast.LENGTH_SHORT).show();
switch (requestCode) { }
case ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE: }
handleStoragePermissionResult(resultCode);
break;
default:
break;
} }
}
private void handleStoragePermissionResult(int resultCode) { private void startMyForegroundService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && Environment.isExternalStorageManager()) { Intent serviceIntent = new Intent(this, MyAccessibilityService.class);
Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show(); ContextCompat.startForegroundService(this, serviceIntent);
} 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 @Override
protected void onDestroy() { protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onDestroy: Cleaning up resources",null); super.onActivityResult(requestCode, resultCode, data);
super.onDestroy();
instance.clear(); switch (requestCode) {
if (AutoJsUtil.scriptResultReceiver != null) { case ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE:
unregisterReceiver(AutoJsUtil.scriptResultReceiver); handleStoragePermissionResult(resultCode);
AutoJsUtil.scriptResultReceiver = null; break;
default:
break;
}
} }
if (executorService != null) {
executorService.shutdown();
}
isRunning = false;
}
public static MainActivity getInstance() { private void handleStoragePermissionResult(int resultCode) {
return instance.get(); // 返回实例 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(); // 返回实例
}
} }

View File

@ -214,7 +214,7 @@ public class ChangeDeviceInfoUtil {
private static boolean processPackage(String packageName, String zipName, Context context) { private static boolean processPackage(String packageName, String zipName, Context context) {
try { try {
File filesDir = new File(context.getExternalFilesDir(null).getAbsolutePath()); 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()) { if (file != null && file.exists()) {
File destFile = new File(context.getCacheDir(), packageName); File destFile = new File(context.getCacheDir(), packageName);

View File

@ -1,6 +1,8 @@
package com.example.studyapp.utils; package com.example.studyapp.utils;
import android.content.Context; import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.Network; import android.net.Network;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
@ -12,6 +14,7 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List;
public class Utils { public class Utils {
@ -28,6 +31,29 @@ public class Utils {
return false; 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<ApplicationInfo> 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){ public static void writePackageName(String packageName){
File file = new File(Environment.getExternalStorageDirectory(), File file = new File(Environment.getExternalStorageDirectory(),
"script/packagesname.txt"); "script/packagesname.txt");

View File

@ -1,87 +1,102 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:padding="16dp" android:padding="16dp"
tools:context=".MainActivity"> tools:context=".MainActivity">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:divider="@android:color/darker_gray"
android:divider="@android:color/darker_gray" android:dividerPadding="8dp"
android:showDividers="middle" android:orientation="vertical"
android:dividerPadding="8dp"> android:showDividers="middle">
<!-- VPN 分组标题 --> <!-- VPN 分组标题 -->
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="VPN 操作" android:gravity="center"
android:padding="8dp" android:padding="8dp"
android:gravity="center" android:text="VPN 操作"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:visibility="gone" />
<!-- VPN 按钮 --> <!-- VPN 按钮 -->
<Button <Button
android:id="@+id/run_script_button" android:id="@+id/run_script_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="运行 脚本" /> android:text="运行 脚本"
android:visibility="gone" />
<Button <Button
android:id="@+id/connectVpnButton" android:id="@+id/connectVpnButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="开启 VPN" /> android:text="开启 VPN"
android:visibility="gone" />
<Button <Button
android:id="@+id/disconnectVpnButton" android:id="@+id/disconnectVpnButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="断开 VPN" /> android:text="断开 VPN"
android:visibility="gone" />
<Button <Button
android:id="@+id/switchVpnButton" android:id="@+id/switchVpnButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="切换 VPN" /> android:text="切换 VPN"
android:visibility="gone" />
<!-- 设备信息分组标题 --> <!-- 设备信息分组标题 -->
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="设备信息操作" android:gravity="center"
android:padding="8dp" android:padding="8dp"
android:gravity="center" android:text="设备信息操作"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:visibility="gone" />
<!-- 设备信息按钮 --> <!-- 设备信息按钮 -->
<Button <Button
android:id="@+id/modifyDeviceInfoButton" android:id="@+id/modifyDeviceInfoButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="修改设备信息" /> android:text="修改设备信息"
android:visibility="gone" />
<Button <Button
android:id="@+id/resetDeviceInfoButton" android:id="@+id/resetDeviceInfoButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="重置设备信息" /> android:text="重置设备信息"
android:visibility="gone" />
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Bigo操作" android:gravity="center"
android:padding="8dp" android:padding="8dp"
android:gravity="center" android:text="Bigo操作"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" /> android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<Button <Button
android:id="@+id/execute_button" android:id="@+id/execute_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="一键执行" /> android:text="一键执行" />
<Button <Button
android:id="@+id/stop_execute_button" android:id="@+id/stop_execute_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="停止执行" /> android:text="停止执行" />
</LinearLayout> </LinearLayout>