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