Skip to content

Commit 983ea54

Browse files
committed
Add a safe executor which does not crash when an exception occurs (for Telephony)
Signed-off-by: Kyle Corry <[email protected]>
1 parent dc21e80 commit 983ea54

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.kylecorry.andromeda.core.coroutines
2+
3+
import android.util.Log
4+
import java.util.concurrent.Executor
5+
import java.util.concurrent.Executors
6+
7+
class SafeExecutor(
8+
private val delegate: Executor,
9+
private val onError: (Throwable) -> Unit = defaultThrowableHandler
10+
) : Executor {
11+
override fun execute(command: Runnable) {
12+
delegate.execute {
13+
try {
14+
command.run()
15+
} catch (e: Throwable) {
16+
onError(e)
17+
}
18+
}
19+
}
20+
21+
companion object {
22+
23+
private val defaultThrowableHandler: (Throwable) -> Unit = {
24+
Log.e("SafeExecutor", "Exception caught in executor: ${it.message}", it)
25+
}
26+
27+
fun newSingleThreadExecutor(onError: (Throwable) -> Unit = defaultThrowableHandler): SafeExecutor {
28+
return SafeExecutor(Executors.newSingleThreadExecutor(), onError)
29+
}
30+
}
31+
}

gradle/libs.versions.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
[versions]
22
androidLibraryVersion = "8.7.3"
33
androidXDocumentFileVersion = "1.0.1"
4-
androidXExifinterfaceVersion = "1.3.7"
4+
androidXExifinterfaceVersion = "1.4.0"
55
appCompatVersion = "1.7.0"
66
cameraCamera2Version = "1.4.1"
7-
constraintlayoutVersion = "2.2.0"
7+
constraintlayoutVersion = "2.2.1"
88
coreKtxVersion = "1.15.0"
99
flexboxVersion = "3.0.0"
1010
tensorflowLiteSupportVersion = "0.4.4"
1111
coroutinesVersion = "1.8.0"
12-
datastorePreferencesVersion = "1.1.1"
13-
desugarVersion = "2.1.4"
12+
datastorePreferencesVersion = "1.1.3"
13+
desugarVersion = "2.1.5"
1414
espressoCoreVersion = "3.6.1"
15-
gsonVersion = "2.10.1"
15+
gsonVersion = "2.12.1"
1616
junit = "1.2.1"
1717
junitJupiterApiVersion = "5.11.3"
1818
junitPlatformRunnerVersion = "1.11.3"
@@ -26,7 +26,7 @@ markwonVersion = "4.6.2"
2626
mockitoKotlinVersion = "5.1.0"
2727
preferenceKtxVersion = "1.2.1"
2828
printVersion = "1.0.0"
29-
recyclerviewVersion = "1.3.2"
29+
recyclerviewVersion = "1.4.0"
3030
solVersion = "10.2.0"
3131
workRuntimeKtxVersion = "2.10.0"
3232
zxingVersion = "3.5.2"

signal/src/main/java/com/kylecorry/andromeda/signal/CellSignalSensor.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ import android.content.Context
55
import android.os.Build
66
import android.os.Handler
77
import android.os.Looper
8-
import android.os.SystemClock
9-
import android.telephony.*
8+
import android.telephony.CellInfo
9+
import android.telephony.CellInfoCdma
10+
import android.telephony.CellInfoGsm
11+
import android.telephony.CellInfoLte
12+
import android.telephony.CellInfoNr
13+
import android.telephony.CellInfoTdscdma
14+
import android.telephony.CellInfoWcdma
15+
import android.telephony.TelephonyManager
1016
import androidx.core.content.getSystemService
1117
import androidx.core.math.MathUtils
18+
import com.kylecorry.andromeda.core.coroutines.SafeExecutor
1219
import com.kylecorry.andromeda.core.sensors.AbstractSensor
1320
import com.kylecorry.andromeda.core.sensors.Quality
1421
import com.kylecorry.andromeda.core.time.AndroidTime
@@ -17,7 +24,6 @@ import com.kylecorry.andromeda.core.tryOrNothing
1724
import com.kylecorry.andromeda.permissions.Permissions
1825
import java.time.Duration
1926
import java.time.Instant
20-
import java.util.concurrent.Executors
2127

2228
class CellSignalSensor(
2329
private val context: Context,
@@ -59,7 +65,13 @@ class CellSignalSensor(
5965
return@tryOrNothing
6066
}
6167
telephony?.requestCellInfoUpdate(
62-
Executors.newSingleThreadExecutor(),
68+
SafeExecutor.newSingleThreadExecutor {
69+
tryOrNothing {
70+
Handler(Looper.getMainLooper()).post {
71+
updateCellInfo(emptyList())
72+
}
73+
}
74+
},
6375
@SuppressLint("NewApi")
6476
object : TelephonyManager.CellInfoCallback() {
6577
override fun onCellInfo(cellInfo: MutableList<CellInfo>) {

0 commit comments

Comments
 (0)