refactor(MainActivity): 优化国家切换逻辑并统一代码

- 将 switchCountry 方法改为返回当前国家代码
- 在切换代理组时直接使用 switchCountry 方法获取最新国家代码
- 统一国家代码获取方式,提高代码可维护性
This commit is contained in:
yjj38 2025-07-07 18:53:31 +08:00
parent b003864b97
commit db423f125c
2 changed files with 4 additions and 5 deletions

View File

@ -187,11 +187,12 @@ public class MainActivity extends AppCompatActivity {
} }
// 添加切换国家的方法 // 添加切换国家的方法
private void switchCountry() { private String switchCountry() {
currentCountry = currentCountry.equals(CountryCode.US) ? currentCountry = currentCountry.equals(CountryCode.US) ?
CountryCode.RU : CountryCode.US; CountryCode.RU : CountryCode.US;
LogFileUtil.logAndWrite(Log.INFO, TAG, LogFileUtil.logAndWrite(Log.INFO, TAG,
"Switched country to: " + currentCountry, null); "Switched country to: " + currentCountry, null);
return currentCountry;
} }
@ -204,8 +205,7 @@ public class MainActivity extends AppCompatActivity {
setupButton(R.id.connectVpnButton, v -> startProxyVpn(this)); setupButton(R.id.connectVpnButton, v -> startProxyVpn(this));
setupButton(R.id.disconnectVpnButton, v -> ClashUtil.stopProxy(this)); setupButton(R.id.disconnectVpnButton, v -> ClashUtil.stopProxy(this));
setupButton(R.id.switchVpnButton, v -> { setupButton(R.id.switchVpnButton, v -> {
switchCountry(); ClashUtil.switchProxyGroup("GLOBAL", switchCountry(), "http://127.0.0.1:6170");
ClashUtil.switchProxyGroup("GLOBAL", currentCountry, "http://127.0.0.1:6170");
}); });
setupButton(R.id.resetDeviceInfoButton, setupButton(R.id.resetDeviceInfoButton,
v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(getPackageName(), this)); v -> ChangeDeviceInfoUtil.resetChangedDeviceInfo(getPackageName(), this));
@ -361,8 +361,7 @@ public class MainActivity extends AppCompatActivity {
} }
try { try {
ClashUtil.startProxy(context); ClashUtil.startProxy(context);
// 使用相同的国家代码 ClashUtil.switchProxyGroup("GLOBAL", switchCountry(), "http://127.0.0.1:6170");
ClashUtil.switchProxyGroup("GLOBAL", currentCountry, "http://127.0.0.1:6170");
} catch (Exception e) { } catch (Exception e) {
LogFileUtil.logAndWrite(Log.ERROR, TAG, "startProxyVpn: Failed to start VPN", e); LogFileUtil.logAndWrite(Log.ERROR, TAG, "startProxyVpn: Failed to start VPN", e);
Toast.makeText(context, "Failed to start VPN: " + Toast.makeText(context, "Failed to start VPN: " +