fix: fix frequent RemoteManager crash

This commit is contained in:
Steve Johnson 2023-11-02 12:01:36 +08:00
parent 546378b9c4
commit 6caa238298
3 changed files with 33 additions and 25 deletions

View File

@ -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 {

View File

@ -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")
}
}

View File

@ -9,7 +9,8 @@ import java.io.File
import java.util.zip.ZipFile
object ApplicationObserver {
private val activities: MutableSet<Activity> = mutableSetOf()
private val _createdActivities: MutableSet<Activity> = mutableSetOf()
private val _visibleActivities: MutableSet<Activity> = mutableSetOf()
private var visibleChanged: (Boolean) -> Unit = {}
@ -23,25 +24,31 @@ object ApplicationObserver {
}
val createdActivities: Set<Activity>
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)
appVisible = activities.isNotEmpty()
_createdActivities.remove(activity)
_visibleActivities.remove(activity)
appVisible = _visibleActivities.isNotEmpty()
}
override fun onActivityStarted(activity: Activity) {
_visibleActivities.add(activity)
appVisible = true
}
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) {}