2025-05-20 16:39:05 +08:00
|
|
|
package com.example.studyapp;
|
|
|
|
|
2025-05-25 23:22:52 +08:00
|
|
|
import android.app.Activity;
|
2025-05-30 11:33:35 +08:00
|
|
|
import android.app.ActivityManager;
|
2025-05-22 18:13:34 +08:00
|
|
|
import android.app.AlertDialog;
|
2025-05-29 16:49:38 +08:00
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.ServiceConnection;
|
2025-05-25 23:22:52 +08:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.net.VpnService;
|
2025-05-22 18:13:34 +08:00
|
|
|
import android.content.Context;
|
2025-05-20 16:39:05 +08:00
|
|
|
import android.content.Intent;
|
2025-05-25 23:22:52 +08:00
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.Network;
|
|
|
|
import android.net.NetworkCapabilities;
|
|
|
|
import android.os.Build;
|
2025-05-20 16:39:05 +08:00
|
|
|
import android.os.Bundle;
|
2025-05-25 23:22:52 +08:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.provider.Settings;
|
|
|
|
import android.util.Log;
|
2025-05-20 16:39:05 +08:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.Toast;
|
|
|
|
import android.Manifest;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.os.Environment;
|
2025-05-22 18:13:34 +08:00
|
|
|
|
|
|
|
import androidx.activity.result.ActivityResultLauncher;
|
2025-05-25 23:22:52 +08:00
|
|
|
import androidx.annotation.Nullable;
|
2025-05-20 16:39:05 +08:00
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
2025-05-29 16:49:38 +08:00
|
|
|
import com.example.studyapp.autoJS.AutoJsUtil;
|
2025-05-29 22:05:49 +08:00
|
|
|
import com.example.studyapp.device.ChangeDeviceInfo;
|
2025-05-25 23:22:52 +08:00
|
|
|
import com.example.studyapp.proxy.CustomVpnService;
|
2025-05-29 16:49:38 +08:00
|
|
|
import com.example.studyapp.utils.ReflectionHelper;
|
2025-05-22 18:13:34 +08:00
|
|
|
|
2025-05-29 16:49:38 +08:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
2025-05-22 18:13:34 +08:00
|
|
|
|
2025-05-20 16:39:05 +08:00
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
private static final int REQUEST_CODE_STORAGE_PERMISSION = 1;
|
2025-05-25 23:22:52 +08:00
|
|
|
private static final int VPN_REQUEST_CODE = 100; // Adding the missing constant
|
2025-05-27 19:25:48 +08:00
|
|
|
|
|
|
|
private static final int ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE = 1001;
|
|
|
|
|
2025-05-20 16:39:05 +08:00
|
|
|
@Override
|
2025-05-27 19:25:48 +08:00
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
2025-05-20 16:39:05 +08:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
2025-05-27 19:25:48 +08:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
|
|
|
|
// 针对 Android 10 或更低版本检查普通存储权限
|
|
|
|
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 {
|
|
|
|
// 针对 Android 11 及更高版本检查全文件管理权限
|
2025-05-29 16:49:38 +08:00
|
|
|
if (!Environment.isExternalStorageManager()) {
|
2025-05-27 19:25:48 +08:00
|
|
|
// 请求权限
|
|
|
|
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);
|
|
|
|
}
|
2025-05-20 16:39:05 +08:00
|
|
|
}
|
|
|
|
|
2025-05-29 16:49:38 +08:00
|
|
|
if (!isNetworkAvailable(this)) {
|
|
|
|
Toast.makeText(this, "Network is not available", Toast.LENGTH_SHORT).show();
|
|
|
|
finish();
|
|
|
|
}
|
2025-05-27 19:25:48 +08:00
|
|
|
// 初始化按钮
|
2025-05-20 16:39:05 +08:00
|
|
|
Button runScriptButton = findViewById(R.id.run_script_button);
|
|
|
|
if (runScriptButton != null) {
|
2025-05-29 16:49:38 +08:00
|
|
|
runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this));
|
2025-05-20 16:39:05 +08:00
|
|
|
} else {
|
|
|
|
Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
2025-05-29 16:49:38 +08:00
|
|
|
|
|
|
|
Button connectButton = findViewById(R.id.connectVpnButton);
|
|
|
|
if (connectButton != null) {
|
|
|
|
connectButton.setOnClickListener(v -> startProxyVpn(this));
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this, "Connect button not found", Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
Button disconnectButton = findViewById(R.id.disconnectVpnButton);
|
|
|
|
if (disconnectButton != null) {
|
|
|
|
disconnectButton.setOnClickListener(v -> stopProxy(this));
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this, "Disconnect button not found", Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
|
2025-05-29 22:05:49 +08:00
|
|
|
Button modifyDeviceInfoButton = findViewById(R.id.modifyDeviceInfoButton);
|
|
|
|
if (modifyDeviceInfoButton != null) {
|
|
|
|
modifyDeviceInfoButton.setOnClickListener(v -> ChangeDeviceInfo.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 -> ChangeDeviceInfo.resetChangedDeviceInfo(getPackageName(),this));
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this, "resetDeviceInfo button not found", Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
|
2025-05-20 16:39:05 +08:00
|
|
|
}
|
|
|
|
|
2025-05-23 16:19:37 +08:00
|
|
|
private void startProxyVpn(Context context) {
|
2025-05-25 23:22:52 +08:00
|
|
|
if (!isNetworkAvailable(context)) {
|
|
|
|
Toast.makeText(context, "Network is not available", Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(context instanceof Activity)) {
|
|
|
|
Toast.makeText(context, "Context must be an Activity", Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Activity activity = (Activity) context;
|
|
|
|
|
|
|
|
try {
|
|
|
|
startProxyServer(activity); // 在主线程中调用
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
Toast.makeText(context, "Failed to start VPN: VPN Service illegal state", Toast.LENGTH_SHORT).show();
|
|
|
|
} catch (Exception e) {
|
|
|
|
Toast.makeText(context, "Failed to start VPN: " + (e.getMessage() != null ? e.getMessage() : "Unknown error"), Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startProxyServer(Activity activity) {
|
|
|
|
// 请求 VPN 权限
|
|
|
|
Intent vpnPrepareIntent = VpnService.prepare(activity);
|
|
|
|
if (vpnPrepareIntent != null) {
|
|
|
|
// 如果尚未授予权限,请求权限,等待结果回调
|
|
|
|
startActivityForResult(vpnPrepareIntent, VPN_REQUEST_CODE);
|
|
|
|
} else {
|
|
|
|
// 如果已经授予权限,直接调用 onActivityResult 模拟结果处理
|
|
|
|
onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showToastOnUiThread(Context context, String message) {
|
|
|
|
new Handler(Looper.getMainLooper()).post(() ->
|
|
|
|
Toast.makeText(context, message, Toast.LENGTH_SHORT).show());
|
2025-05-22 18:13:34 +08:00
|
|
|
}
|
|
|
|
|
2025-05-20 16:39:05 +08:00
|
|
|
@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) {
|
2025-05-29 16:49:38 +08:00
|
|
|
Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show();
|
2025-05-20 16:39:05 +08:00
|
|
|
} else {
|
2025-05-29 16:49:38 +08:00
|
|
|
// 提示权限被拒绝,同时允许用户重新授予权限
|
2025-05-25 23:22:52 +08:00
|
|
|
showPermissionExplanationDialog();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-29 16:49:38 +08:00
|
|
|
|
2025-05-25 23:22:52 +08:00
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
2025-05-29 16:49:38 +08:00
|
|
|
switch (requestCode) {
|
|
|
|
case ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE:
|
|
|
|
handleStoragePermissionResult(resultCode);
|
|
|
|
break;
|
|
|
|
case VPN_REQUEST_CODE:
|
|
|
|
handleVpnPermissionResult(resultCode);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleStoragePermissionResult(int resultCode) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && Environment.isExternalStorageManager()) {
|
|
|
|
Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show();
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this, "请授予所有文件管理权限", Toast.LENGTH_SHORT).show();
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-30 11:33:35 +08:00
|
|
|
private boolean isServiceRunning(Context context, Class<?> serviceClass) {
|
|
|
|
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|
|
|
if (manager != null) {
|
|
|
|
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
|
|
|
|
if (serviceClass.getName().equals(service.service.getClassName())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-05-29 16:49:38 +08:00
|
|
|
private void stopProxy(Context context) {
|
|
|
|
if (context == null) {
|
|
|
|
Log.e("stopProxy", "上下文为空,无法停止服务");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-05-30 11:33:35 +08:00
|
|
|
if (!isServiceRunning(context, CustomVpnService.class)) {
|
|
|
|
Log.w("stopProxy", "服务未运行,无法停止");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
new Thread(() -> {
|
|
|
|
boolean isServiceStopped = true;
|
|
|
|
try {
|
|
|
|
// 通过反射获取服务实例
|
|
|
|
Object instance = ReflectionHelper.getInstance("com.example.studyapp.proxy.CustomVpnService", "instance");
|
|
|
|
if (instance != null) {
|
|
|
|
// 获取并调用 stopService 方法
|
|
|
|
Method stopServiceMethod = instance.getClass().getDeclaredMethod("stopService", Intent.class);
|
|
|
|
stopServiceMethod.invoke(instance, intent);
|
|
|
|
Log.d("stopProxy", "服务已成功停止");
|
|
|
|
} else {
|
|
|
|
isServiceStopped = false;
|
|
|
|
Log.w("stopProxy", "实例为空,服务可能未启动");
|
|
|
|
}
|
|
|
|
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
|
|
|
|
isServiceStopped = false;
|
|
|
|
Log.e("stopProxy", "无法停止服务: " + e.getMessage(), e);
|
|
|
|
} catch (Exception e) {
|
2025-05-29 16:49:38 +08:00
|
|
|
isServiceStopped = false;
|
2025-05-30 11:33:35 +08:00
|
|
|
Log.e("stopProxy", "停止服务时发生未知错误: " + e.getMessage(), e);
|
2025-05-27 19:25:48 +08:00
|
|
|
}
|
|
|
|
|
2025-05-30 11:33:35 +08:00
|
|
|
// 在主线程中更新用户提示
|
|
|
|
String message = isServiceStopped ? "VPN 服务已停止" : "停止 VPN 服务失败";
|
|
|
|
new Handler(Looper.getMainLooper()).post(() ->
|
|
|
|
Toast.makeText(context, message, Toast.LENGTH_SHORT).show());
|
|
|
|
}).start();
|
2025-05-29 16:49:38 +08:00
|
|
|
}
|
|
|
|
|
2025-05-30 11:33:35 +08:00
|
|
|
private Intent intent;
|
2025-05-29 16:49:38 +08:00
|
|
|
private void handleVpnPermissionResult(int resultCode) {
|
|
|
|
if (resultCode == RESULT_OK) {
|
|
|
|
|
2025-05-30 11:33:35 +08:00
|
|
|
intent = new Intent(this, CustomVpnService.class);
|
2025-05-29 16:49:38 +08:00
|
|
|
|
2025-05-25 23:22:52 +08:00
|
|
|
try {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
2025-05-29 16:49:38 +08:00
|
|
|
startForegroundService(intent);
|
2025-05-25 23:22:52 +08:00
|
|
|
} else {
|
2025-05-29 16:49:38 +08:00
|
|
|
startService(intent);
|
2025-05-25 23:22:52 +08:00
|
|
|
}
|
|
|
|
} catch (IllegalStateException e) {
|
2025-05-29 16:49:38 +08:00
|
|
|
Log.e("handleVpnPermissionResult", "Failed to start VPN service", e);
|
2025-05-25 23:22:52 +08:00
|
|
|
showToastOnUiThread(this, "Failed to start VPN service");
|
2025-05-20 16:39:05 +08:00
|
|
|
}
|
2025-05-29 16:49:38 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// 其他结果代码处理逻辑
|
|
|
|
showToastOnUiThread(this, "VPN permission denied or failed.");
|
|
|
|
Log.e("handleVpnPermissionResult", "VPN permission denied or failed with resultCode: " + resultCode);
|
2025-05-20 16:39:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-25 23:22:52 +08:00
|
|
|
private void showPermissionExplanationDialog() {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2025-05-22 18:13:34 +08:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
super.onDestroy();
|
2025-05-29 16:49:38 +08:00
|
|
|
if (AutoJsUtil.scriptResultReceiver != null) {
|
|
|
|
unregisterReceiver(AutoJsUtil.scriptResultReceiver);
|
2025-05-22 18:13:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-25 23:22:52 +08:00
|
|
|
private boolean isNetworkAvailable(Context context) {
|
|
|
|
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
if (connectivityManager != null) {
|
|
|
|
Network network = connectivityManager.getActiveNetwork();
|
2025-05-29 16:49:38 +08:00
|
|
|
if (network != null) {
|
|
|
|
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
|
|
|
|
return capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
|
|
|
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
|
|
|
|
}
|
2025-05-25 23:22:52 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2025-05-20 16:39:05 +08:00
|
|
|
}
|