refactor(device): 重构设备信息修改和ARM云API客户端逻辑
- `ArmCloudApiClient`:移除构造函数中的 `baseUrl`、`accessKey` 和 `secretKey` 参数,将这些值硬编码到类中。 - `ChangeDeviceInfoUtil`: - `changeDeviceInfo` 方法:移除 `padCodes` 参数,改为在方法内部通过 `armClient.getDeviceCodes` 获取。 - `LoadDeviceWorker`: - `executeSingleLogic` 方法:在调用 `ChangeDeviceInfoUtil.changeDeviceInfo` 时传递 `MainActivity.armClient`。 - `MainActivity`: - 新增 `armClient` 静态成员变量,并在 `onCreate` 中初始化 `ArmCloudApiClient` 实例。 - 修改 `modifyDeviceInfoButton` 的点击事件,在调用 `ChangeDeviceInfoUtil.changeDeviceInfo` 时传递 `armClient`。 - `executeSingleLogic` 方法:在调用 `ChangeDeviceInfoUtil.changeDeviceInfo` 时传递 `armClient`。 - 移除了 `getAndroidId(Context context)` 方法中未使用的代码。 - 简化了 `onCreate` 方法中的按钮初始化逻辑,引入了 `setupButton` 辅助方法。 - 引入了 `logInfo`, `logError`, `logWarn`, `showToast` 等辅助方法以简化日志记录和UI提示。 - 移除了未使用的 `instance` 静态成员变量和 `getInstance()` 方法。 - 移除了 `executeLogic` 方法和相关的 `isRunning`、`taskLock` 成员变量,相关逻辑已移至 `LoadDeviceWorker`。 - 调整了 `onDestroy` 方法的逻辑。
This commit is contained in:
parent
f0339e7251
commit
c1cdbfeffc
|
@ -13,6 +13,7 @@ import android.os.Bundle;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
|
@ -29,6 +30,7 @@ import androidx.work.WorkManager;
|
||||||
|
|
||||||
import com.example.retention.R;
|
import com.example.retention.R;
|
||||||
import com.example.retention.autoJS.AutoJsUtil;
|
import com.example.retention.autoJS.AutoJsUtil;
|
||||||
|
import com.example.retention.device.ArmCloudApiClient;
|
||||||
import com.example.retention.device.ChangeDeviceInfoUtil;
|
import com.example.retention.device.ChangeDeviceInfoUtil;
|
||||||
|
|
||||||
import com.example.retention.proxy.ClashUtil;
|
import com.example.retention.proxy.ClashUtil;
|
||||||
|
@ -47,361 +49,280 @@ import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private static WeakReference<MainActivity> instance;
|
private static final int REQUEST_CODE_STORAGE_PERMISSION = 1;
|
||||||
|
|
||||||
private static final int REQUEST_CODE_STORAGE_PERMISSION = 1;
|
private static final int ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE = 1001;
|
||||||
|
|
||||||
private static final int ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE = 1001;
|
public static ExecutorService executorService;
|
||||||
|
|
||||||
public static ExecutorService executorService;
|
public static ArmCloudApiClient armClient;
|
||||||
|
|
||||||
// 假设我们从配置文件中提取出了以下 name 项数据(仅为部分示例数据)
|
// 假设我们从配置文件中提取出了以下 name 项数据(仅为部分示例数据)
|
||||||
private final String[] proxyNames = {
|
private final String[] proxyNames = {
|
||||||
"mr", "sr", "bq", "ml", "ht", "ga", "mk", "by", "pr", "hr", "hu",
|
"mr", "sr", "bq", "ml", "ht", "ga", "mk", "by", "pr", "hr", "hu",
|
||||||
"in", "gt", "at", "kh", "bn", "mg", "kr", "ca", "gh", "ma", "md",
|
"in", "gt", "at", "kh", "bn", "mg", "kr", "ca", "gh", "ma", "md",
|
||||||
"je", "pa", "ba", "mm", "ir", "gy", "mt", "ae", "es", "ng", "ls",
|
"je", "pa", "ba", "mm", "ir", "gy", "mt", "ae", "es", "ng", "ls",
|
||||||
"ag", "pk", "bd", "kn", "mw", "ve", "hk", "cv", "hn", "tm", "us",
|
"ag", "pk", "bd", "kn", "mw", "ve", "hk", "cv", "hn", "tm", "us",
|
||||||
"cz", "ly", "gb", "kz", "it", "bh", "sn", "fi", "co", "sx", "bm",
|
"cz", "ly", "gb", "kz", "it", "bh", "sn", "fi", "co", "sx", "bm",
|
||||||
"fj", "cw", "st", "bw", "fr", "bb", "tg", "ci", "gd", "ne", "bj",
|
"fj", "cw", "st", "bw", "fr", "bb", "tg", "ci", "gd", "ne", "bj",
|
||||||
"nz", "rs", "do", "cl", "lb", "nl", "re", "aw", "ug", "sv", "ar",
|
"nz", "rs", "do", "cl", "lb", "nl", "re", "aw", "ug", "sv", "ar",
|
||||||
"jo", "bg", "jp", "rw", "py", "mn", "ec", "uz", "ro", "cu", "gu",
|
"jo", "bg", "jp", "rw", "py", "mn", "ec", "uz", "ro", "cu", "gu",
|
||||||
"xk", "sy", "so", "zm", "tz", "ni", "sc", "my", "gf", "na", "zw",
|
"xk", "sy", "so", "zm", "tz", "ni", "sc", "my", "gf", "na", "zw",
|
||||||
"la", "et", "ao", "ua", "om", "np", "mx", "mz", "dm", "ye", "gi",
|
"la", "et", "ao", "ua", "om", "np", "mx", "mz", "dm", "ye", "gi",
|
||||||
"cr", "cm", "ph", "am", "th", "ch", "br", "sd", "ie", "bo", "bs",
|
"cr", "cm", "ph", "am", "th", "ch", "br", "sd", "ie", "bo", "bs",
|
||||||
"tc", "vg", "pe", "sa", "dk", "tn", "ee", "jm", "lc", "pt", "qa",
|
"tc", "vg", "pe", "sa", "dk", "tn", "ee", "jm", "lc", "pt", "qa",
|
||||||
"ge", "ps"
|
"ge", "ps"
|
||||||
};
|
};
|
||||||
|
|
||||||
public static volatile String scriptResult;
|
// 初始化 ExecutorService
|
||||||
|
private void initializeExecutorService() {
|
||||||
|
if (executorService == null || executorService.isShutdown()) {
|
||||||
|
executorService = new ThreadPoolExecutor(
|
||||||
|
1, // 核心线程数
|
||||||
|
1, // 最大线程数
|
||||||
|
0L, TimeUnit.MILLISECONDS,
|
||||||
|
new LinkedBlockingQueue<>(50), // 阻塞队列
|
||||||
|
new ThreadPoolExecutor.AbortPolicy() // 拒绝策略
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化 ExecutorService
|
private String getAndroidId() {
|
||||||
private void initializeExecutorService() {
|
return "FyZqWrStUvOpKlMn";
|
||||||
if (executorService == null || executorService.isShutdown()) {
|
}
|
||||||
executorService = new ThreadPoolExecutor(
|
|
||||||
1, // 核心线程数
|
|
||||||
1, // 最大线程数
|
private static final int REQUEST_CODE_PERMISSIONS = 100;
|
||||||
0L, TimeUnit.MILLISECONDS,
|
|
||||||
new LinkedBlockingQueue<>(50), // 阻塞队列
|
@Override
|
||||||
new ThreadPoolExecutor.AbortPolicy() // 拒绝策略
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
);
|
super.onCreate(savedInstanceState);
|
||||||
}
|
LogFileUtil.initialize(this);
|
||||||
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
|
logInfo("onCreate: Initializing application");
|
||||||
|
|
||||||
|
initializeExecutorService();
|
||||||
|
|
||||||
|
System.setProperty("java.library.path", getApplicationInfo().nativeLibraryDir);
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
|
||||||
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||||
|
!= PackageManager.PERMISSION_GRANTED) {
|
||||||
|
ActivityCompat.requestPermissions(
|
||||||
|
this,
|
||||||
|
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
|
||||||
|
REQUEST_CODE_STORAGE_PERMISSION
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!Environment.isExternalStorageManager()) {
|
||||||
|
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
|
||||||
|
intent.setData(Uri.parse("package:" + getPackageName()));
|
||||||
|
startActivityForResult(intent, ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
if (!isNetworkAvailable(this)) {
|
||||||
* 获取 Android 设备的 ANDROID_ID
|
showToast("网络不可用");
|
||||||
*
|
logError("Network not available, closing app.");
|
||||||
* @param context 应用上下文
|
finish();
|
||||||
* @return 设备的 ANDROID_ID,若无法获取,则返回 null
|
return;
|
||||||
*/
|
|
||||||
private String getAndroidId(Context context) {
|
|
||||||
// if (context == null) {
|
|
||||||
// LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "getAndroidId: Context cannot be null",null);
|
|
||||||
// throw new IllegalArgumentException("Context cannot be null");
|
|
||||||
// }
|
|
||||||
// try {
|
|
||||||
// return Settings.Secure.getString(
|
|
||||||
// context.getContentResolver(),
|
|
||||||
// Settings.Secure.ANDROID_ID
|
|
||||||
// );
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "getAndroidId: Failed to get ANDROID_ID",e);
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
return "FyZqWrStUvOpKlMn";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logInfo("onCreate: Setting up work manager");
|
||||||
|
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(CheckAccessibilityWorker.class, 15, TimeUnit.MINUTES)
|
||||||
|
.build();
|
||||||
|
WorkManager.getInstance(this).enqueue(workRequest);
|
||||||
|
|
||||||
private static final int REQUEST_CODE_PERMISSIONS = 100;
|
logInfo("onCreate: Setting up UI components");
|
||||||
|
|
||||||
@Override
|
setupButton(R.id.run_script_button, v -> AutoJsUtil.runAutojsScript(this));
|
||||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
setupButton(R.id.connectVpnButton, v -> startProxyVpn(this));
|
||||||
super.onCreate(savedInstanceState);
|
setupButton(R.id.disconnectVpnButton, v -> ClashUtil.stopProxy(this));
|
||||||
LogFileUtil.initialize(this);
|
setupButton(R.id.switchVpnButton, v -> ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170"));
|
||||||
setContentView(R.layout.activity_main);
|
|
||||||
instance = new WeakReference<>(this);
|
|
||||||
|
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Initializing application", null);
|
armClient = new ArmCloudApiClient();
|
||||||
initializeExecutorService();
|
|
||||||
System.setProperty("java.library.path", this.getApplicationInfo().nativeLibraryDir);
|
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
|
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
|
|
||||||
!= PackageManager.PERMISSION_GRANTED) {
|
|
||||||
ActivityCompat.requestPermissions(
|
|
||||||
this,
|
|
||||||
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
|
|
||||||
REQUEST_CODE_STORAGE_PERMISSION
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!Environment.isExternalStorageManager()) {
|
|
||||||
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
|
|
||||||
intent.setData(Uri.parse("package:" + getPackageName()));
|
|
||||||
startActivityForResult(intent, ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
|
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up work manager", null);
|
setupButton(R.id.modifyDeviceInfoButton, v -> ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this, armClient));
|
||||||
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(CheckAccessibilityWorker.class, 15, TimeUnit.MINUTES)
|
setupButton(R.id.resetDeviceInfoButton, v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(getPackageName(), this));
|
||||||
.build();
|
|
||||||
WorkManager.getInstance(this).enqueue(workRequest);
|
|
||||||
|
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onCreate: Setting up UI components", null);
|
Button executeButton = findViewById(R.id.execute_button);
|
||||||
Button runScriptButton = findViewById(R.id.run_script_button);
|
Button stopExecuteButton = findViewById(R.id.stop_execute_button);
|
||||||
if (runScriptButton != null) {
|
|
||||||
runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this));
|
|
||||||
} else {
|
|
||||||
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 (executeButton != null) {
|
||||||
if (connectButton != null) {
|
executeButton.setOnClickListener(v -> {
|
||||||
connectButton.setOnClickListener(v -> startProxyVpn(this));
|
executeButton.setEnabled(false);
|
||||||
} else {
|
startLoadWork();
|
||||||
Toast.makeText(this, "Connect button not found", Toast.LENGTH_SHORT).show();
|
});
|
||||||
}
|
|
||||||
|
|
||||||
Button disconnectButton = findViewById(R.id.disconnectVpnButton);
|
|
||||||
if (disconnectButton != null) {
|
|
||||||
disconnectButton.setOnClickListener(v -> ClashUtil.stopProxy(this));
|
|
||||||
} else {
|
|
||||||
Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
|
|
||||||
Button switchVpnButton = findViewById(R.id.switchVpnButton);
|
|
||||||
if (switchVpnButton != null) {
|
|
||||||
switchVpnButton.setOnClickListener(v -> ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170"));
|
|
||||||
} else {
|
|
||||||
Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
|
|
||||||
Button modifyDeviceInfoButton = findViewById(R.id.modifyDeviceInfoButton);
|
|
||||||
if (modifyDeviceInfoButton != null) {
|
|
||||||
modifyDeviceInfoButton.setOnClickListener(v -> ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this));
|
|
||||||
} else {
|
|
||||||
Toast.makeText(this, "modifyDeviceInfo button not found", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
|
|
||||||
Button resetDeviceInfoButton = findViewById(R.id.resetDeviceInfoButton);
|
|
||||||
if (resetDeviceInfoButton != null) {
|
|
||||||
resetDeviceInfoButton.setOnClickListener(v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(getPackageName(), this));
|
|
||||||
} else {
|
|
||||||
Toast.makeText(this, "resetDeviceInfo button not found", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化 ChangeDeviceInfoUtil
|
|
||||||
String androidId = getAndroidId(this);
|
|
||||||
String taskId = UUID.randomUUID().toString();
|
|
||||||
// ChangeDeviceInfoUtil.initialize("US", 2, this, androidId);
|
|
||||||
// 获取输入框和按钮
|
|
||||||
Button executeButton = findViewById(R.id.execute_button);
|
|
||||||
Button stopExecuteButton = findViewById(R.id.stop_execute_button);
|
|
||||||
|
|
||||||
// 设置按钮的点击事件
|
|
||||||
if (executeButton != null) {
|
|
||||||
executeButton.setOnClickListener(v -> {
|
|
||||||
executeButton.setEnabled(false);
|
|
||||||
// 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);
|
|
||||||
// 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 static String WORK_TAG = "LOAD_WORK";
|
if (stopExecuteButton != null) {
|
||||||
|
stopExecuteButton.setOnClickListener(v -> {
|
||||||
|
WorkManager.getInstance(this).cancelAllWorkByTag(WORK_TAG);
|
||||||
|
executeButton.setEnabled(true);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
showToast("Stop button not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void startLoadWork() {
|
|
||||||
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.
|
private void setupButton(int resId, View.OnClickListener listener) {
|
||||||
Builder(LoadDeviceWorker.class, 30, TimeUnit.MINUTES)
|
Button button = findViewById(resId);
|
||||||
.setInitialDelay(0, TimeUnit.SECONDS)
|
if (button != null) {
|
||||||
.addTag(WORK_TAG)
|
button.setOnClickListener(listener);
|
||||||
.build();
|
} else {
|
||||||
WorkManager.getInstance(this).enqueue(workRequest);
|
String msg = getResources().getResourceEntryName(resId) + " not found";
|
||||||
|
logWarn(msg);
|
||||||
|
showToast(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final LinkedBlockingQueue<String> scriptResultQueue = new LinkedBlockingQueue<>();
|
||||||
|
|
||||||
|
|
||||||
|
public void executeSingleLogic() {
|
||||||
|
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Proxy not active, starting VPN", null);
|
||||||
|
startProxyVpn(this);
|
||||||
|
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Changing device info", null);
|
||||||
|
ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this,armClient);
|
||||||
|
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script", null);
|
||||||
|
AutoJsUtil.runAutojsScript(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void executeLogic(String androidId, String taskId) {
|
if (!(context instanceof Activity)) {
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Start execution", null);
|
Toast.makeText(context, "Context must be an Activity", Toast.LENGTH_SHORT).show();
|
||||||
|
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Context is not an Activity.", null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!isNetworkAvailable(this)) {
|
try {
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Network is not available!", null);
|
ClashUtil.startProxy(context); // 在主线程中调用
|
||||||
Toast.makeText(this, "网络不可用,请检查网络连接", Toast.LENGTH_SHORT).show();
|
ClashUtil.switchProxyGroup("GLOBAL", "us", "http://127.0.0.1:6170");
|
||||||
return;
|
} catch (Exception 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||||
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||||
|
if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) {
|
||||||
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show();
|
||||||
|
} else {
|
||||||
|
// 提示权限被拒绝,同时允许用户重新授予权限
|
||||||
|
showPermissionExplanationDialog();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (requestCode == REQUEST_CODE_PERMISSIONS) {
|
||||||
|
boolean allGranted = true;
|
||||||
|
for (int result : grantResults) {
|
||||||
|
if (result != PackageManager.PERMISSION_GRANTED) {
|
||||||
|
allGranted = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeLogic: Submitting job to executor", null);
|
if (allGranted) {
|
||||||
initializeExecutorService();
|
// 所有权限已授予
|
||||||
executorService.submit(() -> {
|
startMyForegroundService();
|
||||||
try {
|
} else {
|
||||||
AutoJsUtil.registerScriptResultReceiver(this);
|
Toast.makeText(this, "未授予必要权限,请检查设置", Toast.LENGTH_SHORT).show();
|
||||||
AutoJsUtil.flag = true;
|
}
|
||||||
ChangeDeviceInfoUtil.getDeviceInfo(taskId, androidId);
|
|
||||||
executeSingleLogic();
|
|
||||||
while (isRunning) {
|
|
||||||
if (!isRunning) break;
|
|
||||||
|
|
||||||
// 从队列中获取最新的 scriptResult
|
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script", null);
|
|
||||||
String currentScriptResult = scriptResultQueue.take();
|
|
||||||
ChangeDeviceInfoUtil.getDeviceInfo(taskId, androidId);
|
|
||||||
ChangeDeviceInfoUtil.processPackageInfo(TaskUtil.getPackageInfo(androidId), this);
|
|
||||||
executeSingleLogic();
|
|
||||||
TaskUtil.execSaveTask(this, androidId, taskId, currentScriptResult);
|
|
||||||
LogFileUtil.logAndWrite(android.util.Log.DEBUG, "MainActivity", "----发送result------;" + currentScriptResult, null);
|
|
||||||
if (currentScriptResult != null && !TextUtils.isEmpty(currentScriptResult)) {
|
|
||||||
infoUpload(this, androidId, currentScriptResult);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Thread interrupted while waiting", e);
|
|
||||||
} catch (Exception e) {
|
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "executeLogic: Unexpected task error.", e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static final LinkedBlockingQueue<String> scriptResultQueue = new LinkedBlockingQueue<>();
|
private void startMyForegroundService() {
|
||||||
private volatile boolean isRunning = true; // 主线程运行状态
|
Intent serviceIntent = new Intent(this, MyAccessibilityService.class);
|
||||||
public static final Object taskLock = new Object(); // 任务逻辑锁
|
ContextCompat.startForegroundService(this, serviceIntent);
|
||||||
|
}
|
||||||
|
|
||||||
public void executeSingleLogic() {
|
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Proxy not active, starting VPN", null);
|
@Override
|
||||||
startProxyVpn(this);
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Changing device info", null);
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this);
|
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script", null);
|
switch (requestCode) {
|
||||||
AutoJsUtil.runAutojsScript(this);
|
case ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE:
|
||||||
|
handleStoragePermissionResult(resultCode);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleStoragePermissionResult(int resultCode) {
|
||||||
private void startProxyVpn(Context context) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && Environment.isExternalStorageManager()) {
|
||||||
if (!isNetworkAvailable(context)) {
|
Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show();
|
||||||
Toast.makeText(context, "Network is not available", Toast.LENGTH_SHORT).show();
|
} else {
|
||||||
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", "startProxyVpn: Network is not available.", null);
|
Toast.makeText(this, "请授予所有文件管理权限", Toast.LENGTH_SHORT).show();
|
||||||
return;
|
finish();
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
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);
|
|
||||||
Toast.makeText(context, "Failed to start VPN: " + (e.getMessage() != null ? e.getMessage() : "Unknown error"), Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
private void showPermissionExplanationDialog() {
|
||||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
builder.setTitle("Permission Required")
|
||||||
if (requestCode == REQUEST_CODE_STORAGE_PERMISSION) {
|
.setMessage("Storage Permission is required for the app to function. Please enable it in Settings.")
|
||||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
.setPositiveButton("Go to Settings", (dialog, which) -> {
|
||||||
Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show();
|
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||||||
} else {
|
Uri uri = Uri.fromParts("package", getPackageName(), null);
|
||||||
// 提示权限被拒绝,同时允许用户重新授予权限
|
intent.setData(uri);
|
||||||
showPermissionExplanationDialog();
|
startActivity(intent);
|
||||||
}
|
})
|
||||||
}
|
.setNegativeButton("Cancel", (dialog, which) -> finish())
|
||||||
if (requestCode == REQUEST_CODE_PERMISSIONS) {
|
.show();
|
||||||
boolean allGranted = true;
|
}
|
||||||
for (int result : grantResults) {
|
|
||||||
if (result != PackageManager.PERMISSION_GRANTED) {
|
|
||||||
allGranted = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (allGranted) {
|
@Override
|
||||||
// 所有权限已授予
|
protected void onDestroy() {
|
||||||
startMyForegroundService();
|
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onDestroy: Cleaning up resources", null);
|
||||||
} else {
|
super.onDestroy();
|
||||||
Toast.makeText(this, "未授予必要权限,请检查设置", Toast.LENGTH_SHORT).show();
|
if (AutoJsUtil.scriptResultReceiver != null) {
|
||||||
}
|
unregisterReceiver(AutoJsUtil.scriptResultReceiver);
|
||||||
}
|
AutoJsUtil.scriptResultReceiver = null;
|
||||||
}
|
}
|
||||||
|
if (executorService != null) {
|
||||||
private void startMyForegroundService() {
|
executorService.shutdown();
|
||||||
Intent serviceIntent = new Intent(this, MyAccessibilityService.class);
|
|
||||||
ContextCompat.startForegroundService(this, serviceIntent);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
private void logInfo(String message) {
|
||||||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", message, null);
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
}
|
||||||
|
|
||||||
switch (requestCode) {
|
private void logError(String message) {
|
||||||
case ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE:
|
LogFileUtil.logAndWrite(Log.ERROR, "MainActivity", message, null);
|
||||||
handleStoragePermissionResult(resultCode);
|
}
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleStoragePermissionResult(int resultCode) {
|
private void logWarn(String message) {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && Environment.isExternalStorageManager()) {
|
LogFileUtil.logAndWrite(Log.WARN, "MainActivity", message, null);
|
||||||
Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show();
|
}
|
||||||
} else {
|
|
||||||
Toast.makeText(this, "请授予所有文件管理权限", Toast.LENGTH_SHORT).show();
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showPermissionExplanationDialog() {
|
private void showToast(String message) {
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
|
||||||
builder.setTitle("Permission Required")
|
}
|
||||||
.setMessage("Storage Permission is required for the app to function. Please enable it in Settings.")
|
|
||||||
.setPositiveButton("Go to Settings", (dialog, which) -> {
|
|
||||||
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
|
||||||
Uri uri = Uri.fromParts("package", getPackageName(), null);
|
|
||||||
intent.setData(uri);
|
|
||||||
startActivity(intent);
|
|
||||||
})
|
|
||||||
.setNegativeButton("Cancel", (dialog, which) -> finish())
|
|
||||||
.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onDestroy() {
|
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "onDestroy: Cleaning up resources", null);
|
|
||||||
super.onDestroy();
|
|
||||||
instance.clear();
|
|
||||||
if (AutoJsUtil.scriptResultReceiver != null) {
|
|
||||||
unregisterReceiver(AutoJsUtil.scriptResultReceiver);
|
|
||||||
AutoJsUtil.scriptResultReceiver = null;
|
|
||||||
}
|
|
||||||
if (executorService != null) {
|
|
||||||
executorService.shutdown();
|
|
||||||
}
|
|
||||||
isRunning = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static MainActivity getInstance() {
|
|
||||||
return instance.get(); // 返回实例
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,16 +34,14 @@ public class ArmCloudApiClient {
|
||||||
|
|
||||||
private final OkHttpClient client;
|
private final OkHttpClient client;
|
||||||
private final String baseUrl = "https://openapi-hk.armcloud.net";
|
private final String baseUrl = "https://openapi-hk.armcloud.net";
|
||||||
private final String accessKey;
|
private final String accessKey = "gz8f1u0t63byzdu6ozbx8r5qs3e5lipt";
|
||||||
private final String secretKey;
|
private final String secretKey = "3yc8c8bg1dym0zaiwjh867al";
|
||||||
|
|
||||||
public ArmCloudApiClient(String baseUrl, String accessKey, String secretKey) {
|
public ArmCloudApiClient() {
|
||||||
this.client = new Builder()
|
this.client = new Builder()
|
||||||
.connectTimeout(10, TimeUnit.SECONDS)
|
.connectTimeout(10, TimeUnit.SECONDS)
|
||||||
.readTimeout(10, TimeUnit.SECONDS)
|
.readTimeout(10, TimeUnit.SECONDS)
|
||||||
.build();
|
.build();
|
||||||
this.accessKey = accessKey;
|
|
||||||
this.secretKey = secretKey;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String ALGORITHM = "HmacSHA256";
|
private static final String ALGORITHM = "HmacSHA256";
|
||||||
|
|
|
@ -285,7 +285,7 @@ public class ChangeDeviceInfoUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void changeDeviceInfo(String current_pkg_name, Context context, ArmCloudApiClient client, String[] padCodes) {
|
public static void changeDeviceInfo(String current_pkg_name, Context context, ArmCloudApiClient client) {
|
||||||
|
|
||||||
final String B_PREFIX = current_pkg_name + ".";
|
final String B_PREFIX = current_pkg_name + ".";
|
||||||
final String U_PREFIX = current_pkg_name + "_";
|
final String U_PREFIX = current_pkg_name + "_";
|
||||||
|
@ -438,6 +438,7 @@ public class ChangeDeviceInfoUtil {
|
||||||
|
|
||||||
// 调用接口更新实例属性
|
// 调用接口更新实例属性
|
||||||
try {
|
try {
|
||||||
|
String[] padCodes = client.getDeviceCodes(1, 1, null, null, null, null, null, null, null, null);
|
||||||
String response = client.updateInstanceProperties(
|
String response = client.updateInstanceProperties(
|
||||||
padCodes,
|
padCodes,
|
||||||
null, // modemPersistProps
|
null, // modemPersistProps
|
||||||
|
|
|
@ -11,6 +11,7 @@ import androidx.annotation.NonNull;
|
||||||
import androidx.work.CoroutineWorker;
|
import androidx.work.CoroutineWorker;
|
||||||
import androidx.work.WorkerParameters;
|
import androidx.work.WorkerParameters;
|
||||||
|
|
||||||
|
import com.example.retention.MainActivity;
|
||||||
import com.example.retention.autoJS.AutoJsUtil;
|
import com.example.retention.autoJS.AutoJsUtil;
|
||||||
import com.example.retention.device.ChangeDeviceInfoUtil;
|
import com.example.retention.device.ChangeDeviceInfoUtil;
|
||||||
import com.example.retention.proxy.ClashUtil;
|
import com.example.retention.proxy.ClashUtil;
|
||||||
|
@ -54,7 +55,7 @@ public class LoadDeviceWorker extends CoroutineWorker {
|
||||||
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(context);
|
startProxyVpn(context);
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Changing device info",null);
|
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Changing device info",null);
|
||||||
ChangeDeviceInfoUtil.changeDeviceInfo(context.getPackageName(), context);
|
ChangeDeviceInfoUtil.changeDeviceInfo(context.getPackageName(), context, MainActivity.armClient);
|
||||||
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script",null);
|
LogFileUtil.logAndWrite(Log.INFO, "MainActivity", "executeSingleLogic: Running AutoJs script",null);
|
||||||
Utils.writePackageName(ChangeDeviceInfoUtil.packageName);
|
Utils.writePackageName(ChangeDeviceInfoUtil.packageName);
|
||||||
AutoJsUtil.runAutojsScript(context);
|
AutoJsUtil.runAutojsScript(context);
|
||||||
|
|
Loading…
Reference in New Issue