From 6caa2382986aefebc47ed5a028bc634e4e4358af Mon Sep 17 00:00:00 2001 From: Steve Johnson Date: Thu, 2 Nov 2023 12:01:36 +0800 Subject: [PATCH] fix: fix frequent RemoteManager crash --- .../com/github/kr328/clash/remote/Remote.kt | 28 ++++++++++--------- .../com/github/kr328/clash/remote/Service.kt | 3 +- .../github/kr328/clash/util/Application.kt | 25 +++++++++++------ 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/github/kr328/clash/remote/Remote.kt b/app/src/main/java/com/github/kr328/clash/remote/Remote.kt index a80bc2ae9..d8b249d1b 100644 --- a/app/src/main/java/com/github/kr328/clash/remote/Remote.kt +++ b/app/src/main/java/com/github/kr328/clash/remote/Remote.kt @@ -5,6 +5,7 @@ import android.content.Intent import com.github.kr328.clash.ApkBrokenActivity import com.github.kr328.clash.AppCrashedActivity import com.github.kr328.clash.common.Global +import com.github.kr328.clash.common.log.Log import com.github.kr328.clash.common.util.intent import com.github.kr328.clash.store.AppStore import com.github.kr328.clash.util.ApplicationObserver @@ -29,14 +30,25 @@ object Remote { fun launch() { ApplicationObserver.attach(Global.application) - ApplicationObserver.onVisibleChanged { visible.trySend(it) } + ApplicationObserver.onVisibleChanged { + if(it) { + Log.d("App becomes visible") + service.bind() + broadcasts.register() + } + else { + Log.d("App becomes invisible") + service.unbind() + broadcasts.unregister() + } + } Global.launch(Dispatchers.IO) { - run() + verifyApp() } } - private suspend fun run() { + private suspend fun verifyApp() { val context = Global.application val store = AppStore(context) val updatedAt = getLastUpdated(context) @@ -53,16 +65,6 @@ object Remote { store.updatedAt = updatedAt } } - - while (true) { - if (visible.receive()) { - service.bind() - broadcasts.register() - } else { - service.unbind() - broadcasts.unregister() - } - } } private fun getLastUpdated(context: Context): Long { diff --git a/app/src/main/java/com/github/kr328/clash/remote/Service.kt b/app/src/main/java/com/github/kr328/clash/remote/Service.kt index 4d4d5ab39..3d901bfa3 100644 --- a/app/src/main/java/com/github/kr328/clash/remote/Service.kt +++ b/app/src/main/java/com/github/kr328/clash/remote/Service.kt @@ -33,8 +33,7 @@ class Service(private val context: Application, val crashed: () -> Unit) { } lastCrashed = System.currentTimeMillis() - - Log.w("RemoteManager crashed") + Log.w("RemoteService killed or crashed") } } diff --git a/app/src/main/java/com/github/kr328/clash/util/Application.kt b/app/src/main/java/com/github/kr328/clash/util/Application.kt index 3c2e86e78..1fb633678 100644 --- a/app/src/main/java/com/github/kr328/clash/util/Application.kt +++ b/app/src/main/java/com/github/kr328/clash/util/Application.kt @@ -9,7 +9,8 @@ import java.io.File import java.util.zip.ZipFile object ApplicationObserver { - private val activities: MutableSet = mutableSetOf() + private val _createdActivities: MutableSet = mutableSetOf() + private val _visibleActivities: MutableSet = mutableSetOf() private var visibleChanged: (Boolean) -> Unit = {} @@ -23,25 +24,31 @@ object ApplicationObserver { } val createdActivities: Set - get() = activities + get() = _createdActivities private val activityObserver = object : Application.ActivityLifecycleCallbacks { @Synchronized override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { - activities.add(activity) - - appVisible = true + _createdActivities.add(activity) } @Synchronized override fun onActivityDestroyed(activity: Activity) { - activities.remove(activity) + _createdActivities.remove(activity) + _visibleActivities.remove(activity) + appVisible = _visibleActivities.isNotEmpty() + } + + override fun onActivityStarted(activity: Activity) { + _visibleActivities.add(activity) + appVisible = true + } - appVisible = activities.isNotEmpty() + override fun onActivityStopped(activity: Activity) { + _visibleActivities.remove(activity) + appVisible = _visibleActivities.isNotEmpty() } - override fun onActivityStarted(activity: Activity) {} - override fun onActivityStopped(activity: Activity) {} override fun onActivityPaused(activity: Activity) {} override fun onActivityResumed(activity: Activity) {} override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}