优化异常处理

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;
@ -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) {
@ -251,35 +255,42 @@ public class MainActivity extends AppCompatActivity {
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) { } catch(Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Unexpected task error.", e); LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "loop erro", e);
}
}
} catch (Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "start executeLogic: Unexpected task error.", e);
} }
}); });
} }
@ -288,16 +299,15 @@ 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();

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);
}