Skip to content

Commit 16d6f1f

Browse files
authored
Merge pull request #989 from synonymdev/ovi/fix-982-bitkit-core-0.1.67
fix: core signal abort crash
2 parents 4290a7c + d75f0fa commit 16d6f1f

3 files changed

Lines changed: 23 additions & 21 deletions

File tree

app/src/main/java/to/bitkit/androidServices/LightningNodeService.kt

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import android.app.PendingIntent
55
import android.app.Service
66
import android.content.Intent
77
import android.content.pm.ServiceInfo
8-
import android.os.Build
8+
import android.os.Build.VERSION
9+
import android.os.Build.VERSION_CODES
910
import android.os.IBinder
1011
import androidx.annotation.RequiresApi
1112
import androidx.core.app.NotificationCompat
@@ -37,6 +38,8 @@ import to.bitkit.utils.Logger
3738
import to.bitkit.utils.jsonLogOf
3839
import javax.inject.Inject
3940

41+
typealias Hook = (() -> Unit)?
42+
4043
@AndroidEntryPoint
4144
class LightningNodeService : Service() {
4245

@@ -63,10 +66,6 @@ class LightningNodeService : Service() {
6366

6467
private var hasStartedNode = false
6568

66-
override fun onCreate() {
67-
super.onCreate()
68-
}
69-
7069
private fun setupService() {
7170
if (hasStartedNode) return
7271
hasStartedNode = true
@@ -157,19 +156,14 @@ class LightningNodeService : Service() {
157156
val action = intent?.action
158157
Logger.debug("Received start command action '$action'", context = TAG)
159158
when (action) {
160-
ACTION_START_SERVICE -> {
161-
if (promoteToForeground(startId)) setupService()
162-
}
163159
ACTION_STOP_SERVICE_AND_APP -> {
164-
Logger.debug("Received stop service action", context = TAG)
165-
stopForegroundService(startId)
160+
stopForegroundService(startId) { Logger.debug("Received stop service action", context = TAG) }
166161
activityManager.appTasks.forEach { it.finishAndRemoveTask() }
167162
serviceScope.launch { lightningRepo.stop() }
168163
}
169-
else -> {
170-
Logger.warn("Stopped service for unsupported action '$action'", context = TAG)
171-
stopSelf(startId)
172-
}
164+
165+
ACTION_START_SERVICE -> if (promoteToForeground(startId)) setupService()
166+
else -> stop(startId) { Logger.warn("Stopped service for unsupported action '$action'", context = TAG) }
173167
}
174168
return START_NOT_STICKY
175169
}
@@ -180,22 +174,29 @@ class LightningNodeService : Service() {
180174
this,
181175
ID_NOTIFICATION_NODE,
182176
createNotification(),
183-
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC,
177+
foregroundServiceTypeDataSync(),
184178
)
185179
}.fold(
186180
onSuccess = { true },
187181
onFailure = {
188182
if (it !is RuntimeException) throw it
189-
190-
Logger.error("Failed to promote foreground service", it, context = TAG)
191-
stopSelf(startId)
183+
stop(startId) { Logger.error("Failed to promote foreground service", it, context = TAG) }
192184
false
193185
}
194186
)
195187
}
196188

197-
private fun stopForegroundService(startId: Int) {
189+
private fun foregroundServiceTypeDataSync(): Int {
190+
return if (VERSION.SDK_INT >= VERSION_CODES.Q) ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC else 0
191+
}
192+
193+
private fun stopForegroundService(startId: Int, hook: Hook = null) {
198194
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
195+
stop(startId, hook)
196+
}
197+
198+
private fun stop(startId: Int, hook: Hook = null) {
199+
hook?.invoke()
199200
stopSelf(startId)
200201
}
201202

@@ -206,7 +207,7 @@ class LightningNodeService : Service() {
206207
super.onDestroy()
207208
}
208209

209-
@RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
210+
@RequiresApi(VERSION_CODES.VANILLA_ICE_CREAM)
210211
override fun onTimeout(startId: Int, fgsType: Int) {
211212
Logger.warn("Reached foreground service timeout for type '$fgsType'", context = TAG)
212213
stopForegroundService(startId)

changelog.d/next/989.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Bitkit now handles unexpected native on-chain lookup failures without crashing.

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ activity-compose = { module = "androidx.activity:activity-compose", version = "1
2121
appcompat = { module = "androidx.appcompat:appcompat", version = "1.7.1" }
2222
barcode-scanning = { module = "com.google.mlkit:barcode-scanning", version = "17.3.0" }
2323
biometric = { module = "androidx.biometric:biometric", version = "1.4.0-alpha05" }
24-
bitkit-core = { module = "com.synonym:bitkit-core-android", version = "0.1.64" }
24+
bitkit-core = { module = "com.synonym:bitkit-core-android", version = "0.1.67" }
2525
paykit = { module = "com.synonym:paykit-android", version = "0.1.0-rc8" }
2626
bouncycastle-provider-jdk = { module = "org.bouncycastle:bcprov-jdk18on", version = "1.83" }
2727
camera-camera2 = { module = "androidx.camera:camera-camera2", version.ref = "camera" }

0 commit comments

Comments
 (0)