diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java index 1b11c9a3af..03a6b0e00e 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java @@ -6193,6 +6193,7 @@ protected void onPause() { if (ApplicationLoader.applicationLoaderInstance != null) { doNotPause = ApplicationLoader.applicationLoaderInstance.onPause(); } + ProxyUtil.unregisterNetworkCallback(); ConnectionsManager.getInstance(currentAccount).setAppPaused(!doNotPause, false); if (PhotoViewer.hasInstance() && PhotoViewer.getInstance().isVisible()) { PhotoViewer.getInstance().onPause(); 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 3626fb4c86..e725ca9fcf 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/ProxyUtil.kt +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/utils/ProxyUtil.kt @@ -49,6 +49,8 @@ import java.io.File object ProxyUtil { + private var networkCallback: ConnectivityManager.NetworkCallback? = null + private var networkCallbackStatus = false @JvmStatic fun isVPNEnabled(): Boolean { @@ -66,8 +68,11 @@ object ProxyUtil { @JvmStatic fun registerNetworkCallback() { + if (networkCallbackStatus) { + return + } val connectivityManager = ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager - val networkCallback: ConnectivityManager.NetworkCallback = + networkCallback = object : ConnectivityManager.NetworkCallback() { override fun onAvailable(network: Network) { val networkCapabilities = @@ -92,12 +97,25 @@ object ProxyUtil { } } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - connectivityManager.registerDefaultNetworkCallback(networkCallback) - } else { - val request: NetworkRequest = NetworkRequest.Builder() - .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build() - connectivityManager.registerNetworkCallback(request, networkCallback) + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + connectivityManager.registerDefaultNetworkCallback(networkCallback!!) + } else { + val request: NetworkRequest = NetworkRequest.Builder() + .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build() + connectivityManager.registerNetworkCallback(request, networkCallback!!) + } + networkCallbackStatus = true + } catch (_: Exception) {} + } + + @JvmStatic + fun unregisterNetworkCallback() { + if (networkCallbackStatus && networkCallback != null) { + val connectivityManager = ApplicationLoader.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + try { + connectivityManager.unregisterNetworkCallback(networkCallback!!) + } catch (_: Exception) {} } }