优化异常处理

This commit is contained in:
Administrator 2025-06-28 11:27:26 +08:00
parent 87b70e2197
commit e650ec29c6
6 changed files with 608 additions and 578 deletions

Binary file not shown.

View File

@ -28,9 +28,11 @@ 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;
import com.example.studyapp.device.LoadDeviceCallback;
import com.example.studyapp.proxy.ClashUtil; import com.example.studyapp.proxy.ClashUtil;
import com.example.studyapp.service.MyAccessibilityService; import com.example.studyapp.service.MyAccessibilityService;
import com.example.studyapp.task.TaskUtil; import com.example.studyapp.task.TaskUtil;
@ -38,6 +40,8 @@ import com.example.studyapp.utils.LogFileUtil;
import com.example.studyapp.utils.ShellUtils; import com.example.studyapp.utils.ShellUtils;
import com.example.studyapp.worker.CheckAccessibilityWorker; import com.example.studyapp.worker.CheckAccessibilityWorker;
import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
@ -122,7 +126,7 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
instance = new WeakReference<>(this); instance = new WeakReference<>(this);
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Initializing application",null); LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Initializing application", null);
initializeExecutorService(); initializeExecutorService();
System.setProperty("java.library.path", this.getApplicationInfo().nativeLibraryDir); System.setProperty("java.library.path", this.getApplicationInfo().nativeLibraryDir);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
@ -143,16 +147,16 @@ public class MainActivity extends AppCompatActivity {
} }
if (!isNetworkAvailable(this)) { if (!isNetworkAvailable(this)) {
Toast.makeText(this, "Network is not available", Toast.LENGTH_SHORT).show(); 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(); 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) PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(CheckAccessibilityWorker.class, 15, TimeUnit.MINUTES)
.build(); .build();
WorkManager.getInstance(this).enqueue(workRequest); 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); Button runScriptButton = findViewById(R.id.run_script_button);
if (runScriptButton != null) { if (runScriptButton != null) {
runScriptButton.setOnClickListener(v -> { runScriptButton.setOnClickListener(v -> {
@ -168,7 +172,7 @@ public class MainActivity extends AppCompatActivity {
}); });
// runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this)); // runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this));
} else { } 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(); Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show();
} }
@ -193,12 +197,12 @@ public class MainActivity extends AppCompatActivity {
Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show();
} }
Button modifyDeviceInfoButton = findViewById(R.id.modifyDeviceInfoButton); // Button modifyDeviceInfoButton = findViewById(R.id.modifyDeviceInfoButton);
if (modifyDeviceInfoButton != null) { // if (modifyDeviceInfoButton != null) {
modifyDeviceInfoButton.setOnClickListener(v -> ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this)); // modifyDeviceInfoButton.setOnClickListener(v -> ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this));
} else { // } else {
Toast.makeText(this, "modifyDeviceInfo button not found", Toast.LENGTH_SHORT).show(); // Toast.makeText(this, "modifyDeviceInfo button not found", Toast.LENGTH_SHORT).show();
} // }
Button resetDeviceInfoButton = findViewById(R.id.resetDeviceInfoButton); Button resetDeviceInfoButton = findViewById(R.id.resetDeviceInfoButton);
if (resetDeviceInfoButton != null) { if (resetDeviceInfoButton != null) {
@ -220,7 +224,7 @@ public class MainActivity extends AppCompatActivity {
executeButton.setOnClickListener(v -> { executeButton.setOnClickListener(v -> {
executeButton.setEnabled(false); executeButton.setEnabled(false);
Toast.makeText(this, "任务正在执行", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "任务正在执行", Toast.LENGTH_SHORT).show();
executeLogic(androidId,taskId); executeLogic(androidId, taskId);
}); });
} }
if (stopExecuteButton != null) { if (stopExecuteButton != null) {
@ -238,48 +242,55 @@ public class MainActivity extends AppCompatActivity {
} }
private void executeLogic(String androidId, String taskId) { 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)) { 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(); Toast.makeText(this, "网络不可用,请检查网络连接", Toast.LENGTH_SHORT).show();
return; return;
} }
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Submitting job to executor",null); LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Submitting job to executor", null);
initializeExecutorService(); initializeExecutorService();
executorService.submit(() -> { executorService.submit(() -> {
try { try {
ChangeDeviceInfoUtil.getAddDeviceInfo("US", 2); ChangeDeviceInfoUtil.getAddDeviceInfo("US", 2, (bigoDevice, afDevice) -> {
executeSingleLogic(); startProxyVpn(MainActivity.this);
ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), MainActivity.this, bigoDevice, afDevice);
AutoJsUtil.runAutojsScript(this);
});
AutoJsUtil.registerScriptResultReceiver(this); AutoJsUtil.registerScriptResultReceiver(this);
AutoJsUtil.flag = true; AutoJsUtil.flag = true;
while (isRunning) { while (isRunning) {
if (!isRunning) break; if (!isRunning) break;
try {
// 从队列中获取最新的 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(); String currentScriptResult = scriptResultQueue.take();
ChangeDeviceInfoUtil.getAddDeviceInfo("US", 2); ChangeDeviceInfoUtil.getAddDeviceInfo("US", 2, (bigoDevice, afDevice) -> {
executeSingleLogic(); try {
LogFileUtil.logAndWrite(android.util.Log.DEBUG, "MainActivity", "----发送result------;" + currentScriptResult, null); startProxyVpn(MainActivity.this);
ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), MainActivity.this, bigoDevice, afDevice);
AutoJsUtil.runAutojsScript(this);
if (currentScriptResult != null && !TextUtils.isEmpty(currentScriptResult)) { if (currentScriptResult != null && !TextUtils.isEmpty(currentScriptResult)) {
TaskUtil.execSaveTask(this, androidId, taskId, currentScriptResult); TaskUtil.execSaveTask(this, androidId, taskId, currentScriptResult);
try {
infoUpload(this, androidId, currentScriptResult); infoUpload(this, androidId, currentScriptResult);
} catch (IOException e) {
e.printStackTrace();
} }
} catch (Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "changeDeviceInfo erro", e);
} }
Thread.sleep(5000); });
} LogFileUtil.logAndWrite(android.util.Log.DEBUG, "MainActivity", "----发送result------;" + currentScriptResult, null);
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Thread interrupted while waiting", e); LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "loop: Thread interrupted while waiting", e);
} catch(Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "loop erro", e);
}
}
} catch (Exception e) { } catch (Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Unexpected task error.", e); LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "start executeLogic: Unexpected task error.", e);
} }
}); });
} }
@ -288,26 +299,25 @@ public class MainActivity extends AppCompatActivity {
private volatile boolean isRunning = true; // 主线程运行状态 private volatile boolean isRunning = true; // 主线程运行状态
public static final Object taskLock = new Object(); // 任务逻辑锁 public static final Object taskLock = new Object(); // 任务逻辑锁
public void executeSingleLogic() { // 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); // 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); // ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this);
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script",null);
AutoJsUtil.runAutojsScript(this);
}
/// / LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script",null);
// AutoJsUtil.runAutojsScript(this);
// }
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();
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Network is not available.",null); LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Network is not available.", null);
return; return;
} }
if (!(context instanceof Activity)) { if (!(context instanceof Activity)) {
Toast.makeText(context, "Context must be an Activity", Toast.LENGTH_SHORT).show(); 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; return;
} }
@ -315,7 +325,7 @@ public class MainActivity extends AppCompatActivity {
ClashUtil.startProxy(context); // 在主线程中调用 ClashUtil.startProxy(context); // 在主线程中调用
ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170"); ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170");
} catch (Exception e) { } 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(); Toast.makeText(context, "Failed to start VPN: " + (e.getMessage() != null ? e.getMessage() : "Unknown error"), Toast.LENGTH_SHORT).show();
} }
} }
@ -393,7 +403,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onDestroy() { 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(); super.onDestroy();
instance.clear(); instance.clear();
if (AutoJsUtil.scriptResultReceiver != null) { if (AutoJsUtil.scriptResultReceiver != null) {

View File

@ -75,15 +75,16 @@ public class ChangeDeviceInfoUtil {
}); });
} }
public static void getAddDeviceInfo(String country, int tag){ public static void getAddDeviceInfo(String country, int tag, LoadDeviceCallback callback){
LogFileUtil.logAndWrite(android.util.Log.DEBUG, LOG_TAG, "Initializing device info...", null); LogFileUtil.logAndWrite(android.util.Log.DEBUG, LOG_TAG, "Initializing device info...", null);
executorService.submit(() -> { executorService.submit(() -> {
try { try {
LogFileUtil.logAndWrite(android.util.Log.DEBUG, LOG_TAG, "Starting network requests...", null); LogFileUtil.logAndWrite(android.util.Log.DEBUG, LOG_TAG, "Starting network requests...", null);
String bigoJson = fetchJsonSafely(buildBigoUrl(country, tag), "bigoJson"); String bigoJson = fetchJsonSafely(buildBigoUrl(country, tag), "bigoJson");
String afJson = fetchJsonSafely(buildAfUrl(country, tag), "afJson"); String afJson = fetchJsonSafely(buildAfUrl(country, tag), "afJson");
fallBackToNetworkData(bigoJson, afJson); JSONObject bigoDeviceObject = new JSONObject(bigoJson).optJSONObject("device");
JSONObject afDeviceObject = new JSONObject(afJson).optJSONObject("device");
callback.onLoadDeviceInfo(bigoDeviceObject, afDeviceObject);
} catch (IOException | JSONException e) { } catch (IOException | JSONException e) {
LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Error occurred during initialization", e); LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Error occurred during initialization", e);
} catch (Exception e) { } catch (Exception e) {
@ -126,26 +127,33 @@ public class ChangeDeviceInfoUtil {
private static String fetchJsonSafely(String url, String logKey) throws IOException { private static String fetchJsonSafely(String url, String logKey) throws IOException {
String json = null; String json = null;
int attemptCount = 0;
while (true) {
attemptCount++;
try { try {
json = HttpUtil.requestGet(url); json = HttpUtil.requestGet(url);
if (json != null && !json.isEmpty()) { if (json != null && !json.isEmpty()) {
LogFileUtil.logAndWrite(android.util.Log.DEBUG, LOG_TAG, "Received " + logKey + ": " + json, null); LogFileUtil.logAndWrite(android.util.Log.DEBUG, LOG_TAG,
"Received " + logKey + " after " + attemptCount + " attempt(s): " + json, null);
return json; return json;
} else { } else {
LogFileUtil.logAndWrite(android.util.Log.WARN, LOG_TAG, "Empty or null response for: " + logKey + ", retrying...", null); LogFileUtil.logAndWrite(android.util.Log.WARN, LOG_TAG,
"Empty or null response for: " + logKey + ", retrying in 3 seconds...", null);
} }
} catch (IOException e) { } catch (IOException e) {
LogFileUtil.logAndWrite(android.util.Log.WARN, LOG_TAG, "Error fetching " + logKey + ": " + e.getMessage() + ", retrying...", e); LogFileUtil.logAndWrite(android.util.Log.WARN, LOG_TAG,
"Error fetching " + logKey + " (attempt " + attemptCount + "): " + e.getMessage() + ", retrying in 3 seconds...", e);
} }
// Retry once if the initial attempt failed try {
json = HttpUtil.requestGet(url); Thread.sleep(3000);
if (json != null && !json.isEmpty()) { } catch (InterruptedException ie) {
LogFileUtil.logAndWrite(android.util.Log.DEBUG, LOG_TAG, "Retry success for " + logKey + ": " + json, null); Thread.currentThread().interrupt();
return json; LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG,
} else { "Retry interrupted for " + logKey, ie);
LogFileUtil.logAndWrite(android.util.Log.ERROR, LOG_TAG, "Retry failed for " + logKey + ", response is still null or empty.", null); throw new IOException("Retry interrupted for " + logKey, ie);
throw new IOException("Failed to fetch valid JSON for " + logKey); }
} }
} }
@ -260,8 +268,8 @@ public class ChangeDeviceInfoUtil {
} }
public static void changeDeviceInfo(String current_pkg_name, Context context) { public static void changeDeviceInfo(String current_pkg_name, Context context, JSONObject bigoDeviceObject, JSONObject afDeviceObject) {
try{
BigoInfo bigoDevice; BigoInfo bigoDevice;
if (bigoDeviceObject != null) { if (bigoDeviceObject != null) {
// BIGO // BIGO
@ -331,6 +339,7 @@ public class ChangeDeviceInfoUtil {
logAndWrite(android.util.Log.ERROR, "ChangeDeviceInfoUtil", "Error occurred while changing device info", e); logAndWrite(android.util.Log.ERROR, "ChangeDeviceInfoUtil", "Error occurred while changing device info", e);
throw new RuntimeException("Error occurred in changeDeviceInfo", e); throw new RuntimeException("Error occurred in changeDeviceInfo", e);
} }
bigoDeviceObject = null;
} }
DeviceInfo deviceInfo; DeviceInfo deviceInfo;
@ -490,6 +499,10 @@ public class ChangeDeviceInfoUtil {
logAndWrite(Log.ERROR, "ChangeDeviceInfoUtil", "Error occurred in changeDeviceInfo", e); logAndWrite(Log.ERROR, "ChangeDeviceInfoUtil", "Error occurred in changeDeviceInfo", e);
throw new RuntimeException("Error occurred in changeDeviceInfo", e); throw new RuntimeException("Error occurred in changeDeviceInfo", e);
} }
afDeviceObject = null;
}
} catch (Exception e) {
e.printStackTrace();
} }
} }

View File

@ -0,0 +1,7 @@
package com.example.studyapp.device;
import org.json.JSONObject;
public interface LoadDeviceCallback{
void onLoadDeviceInfo(JSONObject bigoDevice, JSONObject afDevice);
}