From d93a7af5346e2fca5119d468aacf9cb2e3f21727 Mon Sep 17 00:00:00 2001 From: kr328 Date: Sun, 21 Nov 2021 13:01:56 +0800 Subject: [PATCH] Fix: fix network observe --- .../clash/module/NetworkObserveModule.kt | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/NetworkObserveModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/NetworkObserveModule.kt index 094a5cc5..357b7abb 100644 --- a/service/src/main/java/com/github/kr328/clash/service/clash/module/NetworkObserveModule.kt +++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/NetworkObserveModule.kt @@ -41,26 +41,18 @@ class NetworkObserveModule(service: Service) : } } - private fun register(): Boolean { - return try { + private fun register(): Result { + return runCatching { connectivity.registerNetworkCallback(request, callback) - - true - } catch (e: Exception) { - Log.w("Observe network changed: $e", e) - - false + }.onFailure { + Log.w("Observe network change: $it", it) } } - private fun unregister(): Boolean { - try { + private fun unregister(): Result { + return runCatching { connectivity.unregisterNetworkCallback(callback) - } catch (e: Exception) { - // ignored } - - return false } override suspend fun run() { @@ -79,9 +71,9 @@ class NetworkObserveModule(service: Service) : screenToggle.onReceive { when (it.action) { Intent.ACTION_SCREEN_ON -> - register() + register().isFailure Intent.ACTION_SCREEN_OFF -> - unregister() + unregister().isFailure else -> false }