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-22 18:13:34 +08:00
|
|
|
import android.app.AlertDialog;
|
2025-05-25 23:22:52 +08:00
|
|
|
import android.net.Uri;
|
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.provider.Settings;
|
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
|
|
|
|
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-06-07 09:52:33 +08:00
|
|
|
import androidx.work.PeriodicWorkRequest;
|
|
|
|
import androidx.work.WorkManager;
|
2025-05-29 16:49:38 +08:00
|
|
|
import com.example.studyapp.autoJS.AutoJsUtil;
|
2025-06-09 16:17:53 +08:00
|
|
|
import com.example.studyapp.device.ChangeDeviceInfoUtil;
|
2025-05-22 18:13:34 +08:00
|
|
|
|
2025-06-09 16:17:53 +08:00
|
|
|
import com.example.studyapp.utils.ClashUtil;
|
2025-06-07 09:52:33 +08:00
|
|
|
import com.example.studyapp.worker.CheckAccessibilityWorker;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2025-05-22 18:13:34 +08:00
|
|
|
|
2025-05-20 16:39:05 +08:00
|
|
|
public class MainActivity extends AppCompatActivity {
|
2025-05-27 19:25:48 +08:00
|
|
|
|
2025-06-09 16:17:53 +08:00
|
|
|
private static final int REQUEST_CODE_STORAGE_PERMISSION = 1;
|
|
|
|
|
|
|
|
private static final int ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE = 1001;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
|
|
|
System.setProperty("java.library.path", this.getApplicationInfo().nativeLibraryDir);
|
|
|
|
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 及更高版本检查全文件管理权限
|
|
|
|
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);
|
|
|
|
}
|
2025-05-20 16:39:05 +08:00
|
|
|
}
|
|
|
|
|
2025-06-09 16:17:53 +08:00
|
|
|
if (!isNetworkAvailable(this)) {
|
|
|
|
Toast.makeText(this, "Network is not available", Toast.LENGTH_SHORT).show();
|
|
|
|
finish();
|
|
|
|
}
|
2025-05-25 23:22:52 +08:00
|
|
|
|
2025-06-09 16:17:53 +08:00
|
|
|
PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(CheckAccessibilityWorker.class, 15, TimeUnit.MINUTES)
|
|
|
|
.build();
|
|
|
|
WorkManager.getInstance(this).enqueue(workRequest);
|
2025-05-25 23:22:52 +08:00
|
|
|
|
2025-06-09 16:17:53 +08:00
|
|
|
// 初始化按钮
|
|
|
|
Button runScriptButton = findViewById(R.id.run_script_button);
|
|
|
|
if (runScriptButton != null) {
|
|
|
|
runScriptButton.setOnClickListener(v -> AutoJsUtil.runAutojsScript(this));
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this, "Button not found", Toast.LENGTH_SHORT).show();
|
2025-05-25 23:22:52 +08:00
|
|
|
}
|
|
|
|
|
2025-06-09 16:17:53 +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();
|
2025-05-25 23:22:52 +08:00
|
|
|
}
|
|
|
|
|
2025-06-09 16:17:53 +08:00
|
|
|
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();
|
2025-05-22 18:13:34 +08:00
|
|
|
}
|
|
|
|
|
2025-06-09 16:17:53 +08:00
|
|
|
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();
|
2025-05-25 23:22:52 +08:00
|
|
|
}
|
|
|
|
|
2025-06-09 16:17:53 +08:00
|
|
|
Button modifyDeviceInfoButton = findViewById(R.id.modifyDeviceInfoButton);
|
|
|
|
if (modifyDeviceInfoButton != null) {
|
2025-06-09 16:50:33 +08:00
|
|
|
modifyDeviceInfoButton.setOnClickListener(v -> ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(),this));
|
2025-06-09 16:17:53 +08:00
|
|
|
} else {
|
|
|
|
Toast.makeText(this, "modifyDeviceInfo button not found", Toast.LENGTH_SHORT).show();
|
2025-05-29 16:49:38 +08:00
|
|
|
}
|
|
|
|
|
2025-06-09 16:17:53 +08:00
|
|
|
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();
|
2025-05-29 16:49:38 +08:00
|
|
|
}
|
|
|
|
|
2025-06-09 16:17:53 +08:00
|
|
|
// try {
|
|
|
|
// if (!ClashUtil.checkProxy(this)) {
|
|
|
|
// startProxyVpn(this);
|
|
|
|
// }else {
|
|
|
|
// ClashUtil.switchProxyGroup("GLOBAL","us", "127.0.0.1:6170");
|
|
|
|
// };
|
|
|
|
// ChangeDeviceInfoUtil.changeDeviceInfo(getPackageName(), this);
|
|
|
|
// AutoJsUtil.runAutojsScript(this);
|
|
|
|
// } catch (InterruptedException e) {
|
|
|
|
// throw new RuntimeException(e);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startProxyVpn(Context context) {
|
|
|
|
if (!isNetworkAvailable(context)) {
|
|
|
|
Toast.makeText(context, "Network is not available", Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
2025-05-30 11:33:35 +08:00
|
|
|
}
|
|
|
|
|
2025-06-09 16:17:53 +08:00
|
|
|
if (!(context instanceof Activity)) {
|
|
|
|
Toast.makeText(context, "Context must be an Activity", Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
2025-05-29 16:49:38 +08:00
|
|
|
}
|
2025-06-09 16:17:53 +08:00
|
|
|
Activity activity = (Activity) context;
|
|
|
|
|
|
|
|
try {
|
|
|
|
ClashUtil.startProxy(context); // 在主线程中调用
|
|
|
|
} 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();
|
2025-05-20 16:39:05 +08:00
|
|
|
}
|
2025-06-09 16:17:53 +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) {
|
|
|
|
Toast.makeText(this, "Storage Permissions granted", Toast.LENGTH_SHORT).show();
|
|
|
|
} else {
|
|
|
|
// 提示权限被拒绝,同时允许用户重新授予权限
|
|
|
|
showPermissionExplanationDialog();
|
|
|
|
}
|
2025-05-25 23:22:52 +08:00
|
|
|
}
|
2025-06-09 16:17:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
|
|
|
switch (requestCode) {
|
|
|
|
case ALLOW_ALL_FILES_ACCESS_PERMISSION_CODE:
|
|
|
|
handleStoragePermissionResult(resultCode);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2025-05-22 18:13:34 +08:00
|
|
|
}
|
2025-06-09 16:17:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
if (AutoJsUtil.scriptResultReceiver != null) {
|
|
|
|
unregisterReceiver(AutoJsUtil.scriptResultReceiver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isNetworkAvailable(Context context) {
|
|
|
|
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
if (connectivityManager != null) {
|
|
|
|
Network network = connectivityManager.getActiveNetwork();
|
|
|
|
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
|
|
|
}
|
2025-06-09 16:17:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
2025-06-07 09:52:33 +08:00
|
|
|
}
|