From b303179efbfa7d586de4b977756a98bebb9d8e0b Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sun, 25 Feb 2024 20:31:58 +0800 Subject: [PATCH] fix: load proxy settings when app start --- .../java/org/telegram/messenger/SharedConfig.java | 11 +++-------- .../java/org/telegram/tgnet/ConnectionsManager.java | 5 +---- .../org/telegram/ui/Adapters/DrawerLayoutAdapter.java | 6 +++--- .../src/main/java/org/telegram/ui/LaunchActivity.java | 2 +- .../main/java/tw/nekomimi/nekogram/utils/ProxyUtil.kt | 2 +- 5 files changed, 9 insertions(+), 17 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java b/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java index 287047cdd..4ecb4b121 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java @@ -1428,24 +1428,18 @@ public class SharedConfig { LocaleController.resetImperialSystemType(); } - public static boolean proxyEnabled; - public static void setProxyEnable(boolean enable) { if (enable && currentProxy == null) { enable = false; } - proxyEnabled = enable; - SharedPreferences preferences = MessagesController.getGlobalMainSettings(); - preferences.edit().putBoolean("proxy_enabled", enable).apply(); ProxyInfo finalInfo = currentProxy; - + boolean finalEnable = enable; UIUtil.runOnIoDispatcher(() -> { - - if (proxyEnabled) { + if (finalEnable) { ConnectionsManager.setProxySettings(true, finalInfo.address, finalInfo.port, finalInfo.username, finalInfo.password, finalInfo.secret); } else { ConnectionsManager.setProxySettings(false, "", 0, "", "", ""); @@ -1602,6 +1596,7 @@ public class SharedConfig { } public static boolean isProxyEnabled() { + loadProxyList(); return MessagesController.getGlobalMainSettings().getBoolean("proxy_enabled", false) && currentProxy != null; } diff --git a/TMessagesProj/src/main/java/org/telegram/tgnet/ConnectionsManager.java b/TMessagesProj/src/main/java/org/telegram/tgnet/ConnectionsManager.java index 3378abec9..8577fc530 100644 --- a/TMessagesProj/src/main/java/org/telegram/tgnet/ConnectionsManager.java +++ b/TMessagesProj/src/main/java/org/telegram/tgnet/ConnectionsManager.java @@ -495,10 +495,7 @@ SharedPreferences mainPreferences; native_init(currentAccount, version, layer, apiId, deviceModel, systemVersion, appVersion, langCode, systemLangCode, configPath, logPath, regId, cFingerprint, installer, packageId, timezoneOffset, userId, userPremium, enablePushConnection, ApplicationLoader.isNetworkOnline(), ApplicationLoader.getCurrentNetworkType(), SharedConfig.measureDevicePerformanceClass()); Utilities.stageQueue.postRunnable(() -> { - - SharedConfig.loadProxyList(); - - if (SharedConfig.proxyEnabled && SharedConfig.currentProxy != null) { + if (SharedConfig.isProxyEnabled()) { native_setProxySettings(currentAccount, SharedConfig.currentProxy.address, SharedConfig.currentProxy.port, SharedConfig.currentProxy.username, SharedConfig.currentProxy.password, SharedConfig.currentProxy.secret); } checkConnection(); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java b/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java index 2f71f6e6e..abc2d9376 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java @@ -403,9 +403,9 @@ public class DrawerLayoutAdapter extends RecyclerListView.SelectionAdapter imple items.add(new Item(11, LocaleController.getString("SavedMessages", R.string.SavedMessages), savedIcon)); items.add(new Item(8, LocaleController.getString("Settings", R.string.Settings), settingsIcon)); items.add(new Item(10, LocaleController.getString("Calls", R.string.Calls), callsIcon)); - if (NekoConfig.useProxyItem.Bool() && (!NekoConfig.hideProxyByDefault.Bool() || SharedConfig.proxyEnabled)) { - items.add(new CheckItem(13, LocaleController.getString("Proxy", R.string.Proxy), R.drawable.menu_policy, () -> SharedConfig.proxyEnabled, () -> { - SharedConfig.setProxyEnable(!SharedConfig.proxyEnabled); + if (NekoConfig.useProxyItem.Bool() && (!NekoConfig.hideProxyByDefault.Bool() || SharedConfig.isProxyEnabled())) { + items.add(new CheckItem(13, LocaleController.getString("Proxy", R.string.Proxy), R.drawable.menu_policy, SharedConfig::isProxyEnabled, () -> { + SharedConfig.setProxyEnable(!SharedConfig.isProxyEnabled()); return true; })); } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java index d98053676..3873116af 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java @@ -6227,7 +6227,7 @@ public class LaunchActivity extends BasePermissionsActivity implements INavigati } if (NaConfig.INSTANCE.getDisableProxyWhenVpnEnabled().Bool()) { - if (SharedConfig.proxyEnabled && ProxyUtil.isVPNEnabled()) { + if (SharedConfig.isProxyEnabled() && ProxyUtil.isVPNEnabled()) { SharedConfig.setProxyEnable(false); } else if (!ProxyUtil.isVPNEnabled()) { SharedConfig.setProxyEnable(true); diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/ProxyUtil.kt b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/ProxyUtil.kt index 360ac29e8..f2e095cdf 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/ProxyUtil.kt +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/ProxyUtil.kt @@ -80,7 +80,7 @@ object ProxyUtil { } } } - if ((SharedConfig.proxyEnabled && vpn) || (!SharedConfig.proxyEnabled && !vpn)) { + if ((SharedConfig.isProxyEnabled() && vpn) || (!SharedConfig.isProxyEnabled() && !vpn)) { SharedConfig.setProxyEnable(!vpn) UIUtil.runOnUIThread(Runnable { NotificationCenter.getGlobalInstance()