Skip to content

Commit dd698c1

Browse files
authored
all: smoother anr watchdog handling (fixes #9027) (#8996)
1 parent 83a17ba commit dd698c1

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
applicationId "org.ole.planet.myplanet"
1010
minSdk = 26
1111
targetSdk = 36
12-
versionCode = 3712
13-
versionName = "0.37.12"
12+
versionCode = 3713
13+
versionName = "0.37.13"
1414
ndkVersion = '26.3.11579264'
1515
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
vectorDrawables.useSupportLibrary = true

app/src/main/java/org/ole/planet/myplanet/utilities/ANRWatchdog.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package org.ole.planet.myplanet.utilities
22

3+
import android.os.Handler
34
import android.os.Looper
45
import android.os.SystemClock
5-
import kotlinx.coroutines.Dispatchers
6-
import kotlinx.coroutines.launch
7-
import org.ole.planet.myplanet.MainApplication
86

97
class ANRWatchdog(private val timeout: Long = DEFAULT_ANR_TIMEOUT, private val listener: ANRListener? = null) {
108
companion object {
@@ -13,6 +11,9 @@ class ANRWatchdog(private val timeout: Long = DEFAULT_ANR_TIMEOUT, private val l
1311

1412
private var isWatching = false
1513
private var tick = 0L
14+
private val mainHandler = Handler(Looper.getMainLooper())
15+
private val tickUpdater = Runnable { updateTick() }
16+
1617

1718
private fun updateTick() {
1819
tick = SystemClock.elapsedRealtime()
@@ -29,7 +30,7 @@ class ANRWatchdog(private val timeout: Long = DEFAULT_ANR_TIMEOUT, private val l
2930

3031
isWatching = true
3132
tick = SystemClock.elapsedRealtime()
32-
MainApplication.applicationScope.launch(Dispatchers.Main) { updateTick() }
33+
mainHandler.post(tickUpdater)
3334

3435
Thread({
3536
val threadName = Thread.currentThread().name
@@ -38,7 +39,7 @@ class ANRWatchdog(private val timeout: Long = DEFAULT_ANR_TIMEOUT, private val l
3839
while (isWatching) {
3940
val lastTick = tick
4041
val currentTime = SystemClock.elapsedRealtime()
41-
MainApplication.applicationScope.launch(Dispatchers.Main) { updateTick() }
42+
mainHandler.post(tickUpdater)
4243

4344
try {
4445
Thread.sleep(timeout / 2)
@@ -68,12 +69,12 @@ class ANRWatchdog(private val timeout: Long = DEFAULT_ANR_TIMEOUT, private val l
6869
}
6970
}
7071
}
71-
7272
Thread.currentThread().name = threadName
7373
}, "ANRWatchdog").start()
7474
}
7575

7676
fun stop() {
7777
isWatching = false
78+
mainHandler.removeCallbacks(tickUpdater)
7879
}
7980
}

0 commit comments

Comments
 (0)