Refactor `AutoJsUtil` and `MainActivity` to improve script execution and synchronization
Enhanced script execution flow with dedicated `broadcastLock` and `taskLock` for better synchronization. Improved logging, broadcast receiver management, and error handling in `AutoJsUtil`. Removed unused methods and redundant synchronization. Added counter for script runs and optimized lifecycle management for stability.
This commit is contained in:
parent
c8650a2fac
commit
2c9ca8ec44
|
@ -232,14 +232,17 @@ public class MainActivity extends AppCompatActivity {
|
|||
initializeExecutorService();
|
||||
executorService.submit(() -> {
|
||||
try {
|
||||
AutoJsUtil.flag = true;
|
||||
AutoJsUtil.registerScriptResultReceiver(this);
|
||||
synchronized (broadcastLock) {
|
||||
AutoJsUtil.flag = true; // 广播状态更新
|
||||
}
|
||||
|
||||
for (int i = 0; i < number; i++) {
|
||||
synchronized (lock) {
|
||||
// 等待 flag 设置为 false 时暂停
|
||||
synchronized (taskLock) {
|
||||
while (!AutoJsUtil.flag) {
|
||||
lock.wait(); // 当前线程进入等待状态
|
||||
taskLock.wait(30000);
|
||||
}
|
||||
// 执行实际逻辑
|
||||
Log.d("MainActivity", "任务执行第:" + i);
|
||||
executeSingleLogic(i);
|
||||
}
|
||||
}
|
||||
|
@ -251,7 +254,8 @@ public class MainActivity extends AppCompatActivity {
|
|||
});
|
||||
}
|
||||
|
||||
public final static Object lock = new Object();
|
||||
public static final Object broadcastLock = new Object(); // 广播锁
|
||||
public static final Object taskLock = new Object(); // 任务逻辑锁
|
||||
|
||||
public void executeSingleLogic(int i) {
|
||||
Log.i("MainActivity", "executeSingleLogic: Start execution for index " + i);
|
||||
|
@ -280,7 +284,6 @@ public class MainActivity extends AppCompatActivity {
|
|||
Log.i("MainActivity", "executeSingleLogic: Finished execution for index " + i + " in " + (endTime - startTime) + " ms");
|
||||
}
|
||||
|
||||
|
||||
private void startProxyVpn(Context context) {
|
||||
if (!isNetworkAvailable(context)) {
|
||||
Toast.makeText(context, "Network is not available", Toast.LENGTH_SHORT).show();
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.example.studyapp.autoJS;
|
||||
|
||||
import static androidx.core.content.ContextCompat.startActivity;
|
||||
import static com.example.studyapp.MainActivity.broadcastLock;
|
||||
import static com.example.studyapp.MainActivity.taskLock;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.ActivityNotFoundException;
|
||||
|
@ -35,8 +37,10 @@ public class AutoJsUtil {
|
|||
public static BroadcastReceiver scriptResultReceiver;
|
||||
public static volatile boolean flag;
|
||||
|
||||
private static int count;
|
||||
public static void runAutojsScript(Context context,String url) {
|
||||
// 检查脚本文件
|
||||
Log.i("AutoJsUtil", "-------脚本运行开始:--------"+ count++ );
|
||||
File scriptFile = new File(Environment.getExternalStorageDirectory(), "script/main.js");
|
||||
if (!scriptFile.exists()) {
|
||||
runOnUiThread(() -> Toast.makeText(context, "脚本文件未找到: " + scriptFile.getAbsolutePath(), Toast.LENGTH_SHORT).show());
|
||||
|
@ -57,8 +61,8 @@ public class AutoJsUtil {
|
|||
intent.putExtra("url", url);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
try {
|
||||
registerScriptResultReceiver(context);
|
||||
context.startActivity(intent);
|
||||
flag = false;
|
||||
Log.i("AutoJsUtil", "脚本运行中:" + scriptFile.getAbsolutePath());
|
||||
} catch (Exception e) {
|
||||
Log.e("AutoJsUtil", "运行脚本失败", e);
|
||||
|
@ -68,34 +72,38 @@ public class AutoJsUtil {
|
|||
}
|
||||
|
||||
public static void registerScriptResultReceiver(Context context) {
|
||||
// 使用锁进行控制
|
||||
synchronized (MainActivity.lock) {
|
||||
|
||||
if (scriptResultReceiver == null) {
|
||||
// 创建广播接收器
|
||||
scriptResultReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String scriptResult = intent.getStringExtra("result");
|
||||
Log.d("MainActivity", "----脚本运行结束通知一次------; 当前线程:" + Thread.currentThread().getName());
|
||||
String scriptResult = intent.getStringExtra(SCRIPT_RESULT_KEY);
|
||||
if (scriptResult != null && !scriptResult.isEmpty()) {
|
||||
Log.d("MainActivity", "Script result received");
|
||||
synchronized (MainActivity.lock){
|
||||
flag = true;
|
||||
MainActivity.lock.notifyAll();
|
||||
synchronized (broadcastLock) {
|
||||
AutoJsUtil.flag = true;
|
||||
}
|
||||
synchronized (taskLock) {
|
||||
taskLock.notifyAll(); // 唤醒任务线程
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
// 注册广播接收器
|
||||
try {
|
||||
IntentFilter filter = new IntentFilter(AUTOJS_SCRIPT_FINISHED_ACTION);
|
||||
Context appContext = context.getApplicationContext();
|
||||
ContextCompat.registerReceiver(appContext, scriptResultReceiver, filter, ContextCompat.RECEIVER_EXPORTED);
|
||||
flag = false;
|
||||
Log.d("MainActivity", "广播接收器成功注册");
|
||||
} catch (Exception e) {
|
||||
Log.e("MainActivity", "Failed to register receiver", e);
|
||||
scriptResultReceiver = null; // 确保状态一致
|
||||
}
|
||||
} else {
|
||||
Log.w("MainActivity", "广播接收器已注册,无需重复注册");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isActivityAvailable(Context context, String packageName, String className) {
|
||||
|
@ -125,14 +133,6 @@ public class AutoJsUtil {
|
|||
private static final Object lock = new Object();
|
||||
|
||||
|
||||
public static void unregisterScriptResultReceiver(Context context) {
|
||||
synchronized (lock) {
|
||||
if (scriptResultReceiver != null) {
|
||||
context.getApplicationContext().unregisterReceiver(scriptResultReceiver);
|
||||
scriptResultReceiver = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void stopAutojsScript(Context context) {
|
||||
// 停止运行脚本的 Intent
|
||||
|
|
|
@ -242,7 +242,6 @@ public class ChangeDeviceInfoUtil {
|
|||
String date1 = afDeviceObject.optString(".date1", "");
|
||||
String date2 = afDeviceObject.optString(".date2", "");
|
||||
String bootId = afDeviceObject.optString("BootId", "");
|
||||
|
||||
// 设置机型, 直接设置属性
|
||||
ShellUtils.execRootCmd("setprop ro.product.brand " + ro_product_brand);
|
||||
ShellUtils.execRootCmd("setprop ro.product.model "+ ro_product_model );
|
||||
|
|
Loading…
Reference in New Issue