@@ -9,9 +9,6 @@ import android.os.IBinder
99import androidx.annotation.RequiresApi
1010import androidx.core.app.NotificationCompat
1111import androidx.core.content.ContextCompat
12- import androidx.lifecycle.DefaultLifecycleObserver
13- import androidx.lifecycle.LifecycleOwner
14- import androidx.lifecycle.ProcessLifecycleOwner
1512import dagger.hilt.android.AndroidEntryPoint
1613import kotlinx.coroutines.CoroutineDispatcher
1714import kotlinx.coroutines.CoroutineScope
@@ -20,6 +17,7 @@ import kotlinx.coroutines.cancel
2017import kotlinx.coroutines.launch
2118import org.lightningdevkit.ldknode.Event
2219import to.bitkit.App
20+ import to.bitkit.AppLifecycleListener
2321import to.bitkit.R
2422import to.bitkit.data.CacheStore
2523import to.bitkit.di.UiDispatcher
@@ -57,13 +55,30 @@ class LightningNodeService : Service() {
5755 @Inject
5856 lateinit var cacheStore: CacheStore
5957
60- private var lifecycleObserver : AppLifecycleObserver ? = null
58+ private var lifecycleListener : AppLifecycleListener ? = null
6159
6260 override fun onCreate () {
6361 super .onCreate()
6462 startForeground(ID_NOTIFICATION_NODE , createNotification())
6563 setupService()
66- lifecycleObserver = AppLifecycleObserver ().also { ProcessLifecycleOwner .get().lifecycle.addObserver(it) }
64+ setupLifecycleListener()
65+ }
66+
67+ private fun setupLifecycleListener () {
68+ lifecycleListener = AppLifecycleListener { isInForeground ->
69+ Logger .debug(" App lifecycle changed: isInForeground=$isInForeground " , context = TAG )
70+ serviceScope.launch {
71+ if (isInForeground) {
72+ lightningRepo.disableBatterySavingMode()
73+ .onSuccess { Logger .debug(" Exited battery saving mode" , context = TAG ) }
74+ .onFailure { Logger .warn(" Error exiting battery saving mode" , it, context = TAG ) }
75+ } else {
76+ lightningRepo.enableBatterySavingMode()
77+ .onSuccess { Logger .debug(" Entered battery saving mode" , context = TAG ) }
78+ .onFailure { Logger .warn(" Error entering battery saving mode" , it, context = TAG ) }
79+ }
80+ }
81+ }.also { App .lifecycle?.addListener(it) }
6782 }
6883
6984 private fun setupService () {
@@ -96,7 +111,7 @@ class LightningNodeService : Service() {
96111 sheet : NewTransactionSheetDetails ,
97112 notification : NotificationDetails ,
98113 ) {
99- if (App .currentActivity?.value != null ) {
114+ if (App .lifecycle?.activity != null ) {
100115 Logger .debug(" Skipping payment notification: activity is active" , context = TAG )
101116 return
102117 }
@@ -134,7 +149,7 @@ class LightningNodeService : Service() {
134149 ACTION_STOP_SERVICE_AND_APP -> {
135150 Logger .debug(" ACTION_STOP_SERVICE_AND_APP detected" , context = TAG )
136151 // Close activities gracefully without force-stopping the app
137- App .currentActivity?.value ?.finishAffinity()
152+ App .lifecycle?.activity ?.finishAffinity()
138153 // Stop the service
139154 stopSelf()
140155 return START_NOT_STICKY
@@ -145,7 +160,7 @@ class LightningNodeService : Service() {
145160
146161 override fun onDestroy () {
147162 Logger .debug(" onDestroy" , context = TAG )
148- lifecycleObserver ?.let { ProcessLifecycleOwner .get(). lifecycle.removeObserver (it) }
163+ lifecycleListener ?.let { App . lifecycle?.removeListener (it) }
149164 serviceScope.launch {
150165 lightningRepo.stop()
151166 serviceScope.cancel()
@@ -165,24 +180,6 @@ class LightningNodeService : Service() {
165180
166181 override fun onBind (intent : Intent ? ): IBinder ? = null
167182
168- private inner class AppLifecycleObserver : DefaultLifecycleObserver {
169- override fun onStart (owner : LifecycleOwner ) {
170- serviceScope.launch {
171- lightningRepo.disableBatterySavingMode()
172- .onSuccess { Logger .debug(" Sync intervals exited battery saving mode" , context = TAG ) }
173- .onFailure { Logger .warn(" Error setting sync intervals out of battery saving" , it, context = TAG ) }
174- }
175- }
176-
177- override fun onStop (owner : LifecycleOwner ) {
178- serviceScope.launch {
179- lightningRepo.enableBatterySavingMode()
180- .onSuccess { Logger .debug(" Sync intervals entered battery saving mode" , context = TAG ) }
181- .onFailure { Logger .warn(" Error setting sync intervals set to battery saving" , it, context = TAG ) }
182- }
183- }
184- }
185-
186183 companion object {
187184 const val TAG = " LightningNodeService"
188185 const val CHANNEL_ID_NODE = " bitkit_notification_channel_node"
0 commit comments