@@ -5,7 +5,8 @@ import android.app.PendingIntent
55import android.app.Service
66import android.content.Intent
77import android.content.pm.ServiceInfo
8- import android.os.Build
8+ import android.os.Build.VERSION
9+ import android.os.Build.VERSION_CODES
910import android.os.IBinder
1011import androidx.annotation.RequiresApi
1112import androidx.core.app.NotificationCompat
@@ -37,6 +38,8 @@ import to.bitkit.utils.Logger
3738import to.bitkit.utils.jsonLogOf
3839import javax.inject.Inject
3940
41+ typealias Hook = (() -> Unit )?
42+
4043@AndroidEntryPoint
4144class 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)
0 commit comments