Skip to content

Commit

Permalink
fix: fix frequent RemoteManager crash
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejohnson7 committed Nov 2, 2023
1 parent 546378b commit 6caa238
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
28 changes: 15 additions & 13 deletions app/src/main/java/com/github/kr328/clash/remote/Remote.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/github/kr328/clash/remote/Service.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
25 changes: 16 additions & 9 deletions app/src/main/java/com/github/kr328/clash/util/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -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)
_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) {}
Expand Down

0 comments on commit 6caa238

Please sign in to comment.