添加取消定时任务的方法
This commit is contained in:
parent
203fccea87
commit
d8dd6e740a
|
@ -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;
|
||||
|
||||
|
@ -125,7 +126,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
setContentView(R.layout.activity_main);
|
||||
instance = new WeakReference<>(this);
|
||||
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Initializing application",null);
|
||||
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) {
|
||||
|
@ -146,47 +147,27 @@ public class MainActivity extends AppCompatActivity {
|
|||
}
|
||||
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);
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "Network not available, closing app.", null);
|
||||
finish();
|
||||
}
|
||||
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up work manager",null);
|
||||
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);
|
||||
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 -> {
|
||||
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());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this));
|
||||
} else {
|
||||
LogFileUtil.logAndWrite(Log.WARN, "MainActivity", "Run Script Button not found",null);
|
||||
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));
|
||||
connectButton.setOnClickListener(v -> startProxyVpn(this));
|
||||
} else {
|
||||
Toast.makeText(this, "Connect button not found", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
@ -231,41 +212,48 @@ public class MainActivity extends AppCompatActivity {
|
|||
if (executeButton != null) {
|
||||
executeButton.setOnClickListener(v -> {
|
||||
executeButton.setEnabled(false);
|
||||
Toast.makeText(this, "任务正在执行", Toast.LENGTH_SHORT).show();
|
||||
// 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);
|
||||
// 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 void startLoadWork(){
|
||||
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(LoadDeviceWorker.class, 15, TimeUnit.MINUTES)
|
||||
.setInitialDelay(0, TimeUnit.SECONDS).build();
|
||||
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);
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Start execution", null);
|
||||
|
||||
if (!isNetworkAvailable(this)) {
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Network is not available!",null);
|
||||
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);
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Submitting job to executor", null);
|
||||
initializeExecutorService();
|
||||
executorService.submit(() -> {
|
||||
try {
|
||||
|
@ -277,7 +265,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
if (!isRunning) break;
|
||||
|
||||
// 从队列中获取最新的 scriptResult
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script",null);
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script", null);
|
||||
String currentScriptResult = scriptResultQueue.take();
|
||||
ChangeDeviceInfoUtil.getDeviceInfo(taskId, androidId);
|
||||
ChangeDeviceInfoUtil.processPackageInfo(TaskUtil.getPackageInfo(androidId), this);
|
||||
|
@ -302,11 +290,11 @@ public class MainActivity extends AppCompatActivity {
|
|||
public static final Object taskLock = new Object(); // 任务逻辑锁
|
||||
|
||||
public void executeSingleLogic() {
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Proxy not active, starting VPN",null);
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Proxy not active, starting VPN", null);
|
||||
startProxyVpn(this);
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Changing device info",null);
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Changing device info", null);
|
||||
ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this);
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script",null);
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script", null);
|
||||
AutoJsUtil.runAutojsScript(this);
|
||||
}
|
||||
|
||||
|
@ -314,13 +302,13 @@ public class MainActivity extends AppCompatActivity {
|
|||
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);
|
||||
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);
|
||||
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Context is not an Activity.", null);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -328,7 +316,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
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);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -406,7 +394,7 @@ public class MainActivity extends AppCompatActivity {
|
|||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onDestroy: Cleaning up resources",null);
|
||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onDestroy: Cleaning up resources", null);
|
||||
super.onDestroy();
|
||||
instance.clear();
|
||||
if (AutoJsUtil.scriptResultReceiver != null) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<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){
|
||||
File file = new File(Environment.getExternalStorageDirectory(),
|
||||
"script/packagesname.txt");
|
||||
|
|
|
@ -9,74 +9,89 @@
|
|||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:divider="@android:color/darker_gray"
|
||||
android:showDividers="middle"
|
||||
android:dividerPadding="8dp">
|
||||
android:dividerPadding="8dp"
|
||||
android:orientation="vertical"
|
||||
android:showDividers="middle">
|
||||
|
||||
<!-- VPN 分组标题 -->
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="VPN 操作"
|
||||
android:padding="8dp"
|
||||
android:gravity="center"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
|
||||
android:padding="8dp"
|
||||
android:text="VPN 操作"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
|
||||
android:visibility="gone" />
|
||||
|
||||
<!-- VPN 按钮 -->
|
||||
<Button
|
||||
android:id="@+id/run_script_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="运行 脚本" />
|
||||
android:text="运行 脚本"
|
||||
android:visibility="gone" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/connectVpnButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="开启 VPN" />
|
||||
android:text="开启 VPN"
|
||||
android:visibility="gone" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/disconnectVpnButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="断开 VPN" />
|
||||
android:text="断开 VPN"
|
||||
android:visibility="gone" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/switchVpnButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="切换 VPN" />
|
||||
android:text="切换 VPN"
|
||||
android:visibility="gone" />
|
||||
|
||||
<!-- 设备信息分组标题 -->
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="设备信息操作"
|
||||
android:padding="8dp"
|
||||
android:gravity="center"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
|
||||
android:padding="8dp"
|
||||
android:text="设备信息操作"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
|
||||
android:visibility="gone" />
|
||||
|
||||
<!-- 设备信息按钮 -->
|
||||
<Button
|
||||
android:id="@+id/modifyDeviceInfoButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="修改设备信息" />
|
||||
android:text="修改设备信息"
|
||||
android:visibility="gone" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/resetDeviceInfoButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="重置设备信息" />
|
||||
android:text="重置设备信息"
|
||||
android:visibility="gone" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Bigo操作"
|
||||
android:padding="8dp"
|
||||
android:gravity="center"
|
||||
android:padding="8dp"
|
||||
android:text="Bigo操作"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/execute_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="一键执行" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/stop_execute_button"
|
||||
android:layout_width="wrap_content"
|
||||
|
|
Loading…
Reference in New Issue