From 84db43d571e1473e3bb6520c8739f3f4b1446040 Mon Sep 17 00:00:00 2001 From: yjj38 Date: Wed, 9 Jul 2025 21:15:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(proxy):=20=E6=96=B0=E5=A2=9E=E5=9B=BD?= =?UTF-8?q?=E5=AE=B6=E4=BB=A3=E7=A0=81=E5=88=87=E6=8D=A2=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=B9=B6=E5=BC=95=E5=85=A5OkHttp=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 `CountryCode` 类,用于管理和切换国家代码(目前支持US和RU)。 - 在 `LoadDeviceWorker` 和 `MainActivity` 中引入 `CountryCode` 以实现国家切换。 - `MainActivity` 中的 `startProxyVpn` 方法现在使用 `CountryCode.switchCountry()` 来获取当前国家代码。 - `ClashUtil` 中引入 `okhttp3.logging.HttpLoggingInterceptor`。 - 在 `app/build.gradle` 中添加 `okhttp` 和 `logging-interceptor` 依赖。 - 修改了 `MainActivity` 中 `startProxyVpn` 失败时的日志记录标签。 --- app/build.gradle | 5 +++- .../com/example/retention/MainActivity.java | 3 +- .../example/retention/config/CountryCode.java | 30 +++++++++++++++++++ .../example/retention/proxy/ClashUtil.java | 1 + .../retention/worker/LoadDeviceWorker.java | 1 + 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/example/retention/config/CountryCode.java diff --git a/app/build.gradle b/app/build.gradle index 47c886b..67c7e2f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -96,11 +96,14 @@ dependencies { // 添加 Mockito 核心依赖 testImplementation 'org.mockito:mockito-core:5.4.0' - + // 如果需要在 Android Instrumented Tests 中使用 Mockito androidTestImplementation 'org.mockito:mockito-android:5.4.0' testImplementation 'junit:junit:4.13.2' testImplementation 'org.mockito:mockito-inline:4.8.0' + implementation 'com.squareup.okhttp3:okhttp:4.12.0' + implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0' + } diff --git a/app/src/main/java/com/example/retention/MainActivity.java b/app/src/main/java/com/example/retention/MainActivity.java index 50d124a..cd5346d 100644 --- a/app/src/main/java/com/example/retention/MainActivity.java +++ b/app/src/main/java/com/example/retention/MainActivity.java @@ -30,6 +30,7 @@ import androidx.work.WorkManager; import com.example.retention.R; import com.example.retention.autoJS.AutoJsUtil; +import com.example.retention.config.CountryCode; import com.example.retention.device.ArmCloudApiClient; import com.example.retention.device.ChangeDeviceInfoUtil; @@ -211,7 +212,7 @@ public class MainActivity extends AppCompatActivity { ClashUtil.switchProxyWithPort(CountryCode.switchCountry()); // ClashUtil.switchProxyGroup("PROXY", "my-socks5-proxy", "http://127.0.0.1:6170"); } catch (Exception e) { - LogFileUtil.logAndWrite(Log.ERROR, TAG, "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(); diff --git a/app/src/main/java/com/example/retention/config/CountryCode.java b/app/src/main/java/com/example/retention/config/CountryCode.java new file mode 100644 index 0000000..31d8afe --- /dev/null +++ b/app/src/main/java/com/example/retention/config/CountryCode.java @@ -0,0 +1,30 @@ +package com.example.retention.config; + +import android.util.Log; +import com.example.retention.utils.LogFileUtil; + +/** + * @Time: 2025-08-09 21:08 + * @Creator: 初屿贤 + * @File: ewfw + * @Project: study.App + * @Description: + */ +public class CountryCode { + public static final int DEVICE_TYPE = 2; + static final String US = "US"; + static final String RU = "RU"; + // 默认使用美国 + static final String DEFAULT = US; + + // 当前使用的国家代码 + public static String currentCountry = DEFAULT; + + public static String switchCountry() { + currentCountry = currentCountry.equals(US) ? + RU : US; + LogFileUtil.logAndWrite(Log.INFO, "TAG", + "Switched country to: " + currentCountry, null); + return currentCountry; + } +} diff --git a/app/src/main/java/com/example/retention/proxy/ClashUtil.java b/app/src/main/java/com/example/retention/proxy/ClashUtil.java index 099bdd5..690e3ed 100644 --- a/app/src/main/java/com/example/retention/proxy/ClashUtil.java +++ b/app/src/main/java/com/example/retention/proxy/ClashUtil.java @@ -22,6 +22,7 @@ import okhttp3.RequestBody; import okhttp3.Response; import org.json.JSONException; import org.json.JSONObject; +import okhttp3.logging.HttpLoggingInterceptor; /** * @Time: 2025/6/9 11:13 diff --git a/app/src/main/java/com/example/retention/worker/LoadDeviceWorker.java b/app/src/main/java/com/example/retention/worker/LoadDeviceWorker.java index 105f807..f239eb0 100644 --- a/app/src/main/java/com/example/retention/worker/LoadDeviceWorker.java +++ b/app/src/main/java/com/example/retention/worker/LoadDeviceWorker.java @@ -13,6 +13,7 @@ import androidx.work.WorkerParameters; import com.example.retention.MainActivity; import com.example.retention.autoJS.AutoJsUtil; +import com.example.retention.config.CountryCode; import com.example.retention.device.ChangeDeviceInfoUtil; import com.example.retention.proxy.ClashUtil; import com.example.retention.utils.LogFileUtil;