Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit 31d107b

Browse files
committed
add debug broadcasting of when service switches between foreground/background
1 parent d2f6093 commit 31d107b

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

topl-service/src/main/java/io/matthewnelson/topl_service/notification/ServiceNotification.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,26 +491,31 @@ class ServiceNotification internal constructor(
491491
private set
492492

493493
@Synchronized
494-
internal fun startForeground(torService: BaseService): ServiceNotification {
495-
if (!inForeground) {
494+
internal fun startForeground(torService: BaseService): Boolean {
495+
return if (!inForeground) {
496496
notificationBuilder?.let {
497497
torService.startForeground(notificationID, it.build())
498498
inForeground = true
499+
return true
499500
}
501+
false
502+
} else {
503+
false
500504
}
501-
return serviceNotification
502505
}
503506

504507
@Synchronized
505-
internal fun stopForeground(torService: BaseService): ServiceNotification {
506-
if (inForeground) {
508+
internal fun stopForeground(torService: BaseService): Boolean {
509+
return if (inForeground) {
507510
torService.stopForeground(!showNotification)
508511
inForeground = false
509512
notificationBuilder?.let {
510513
notify(torService, it)
511514
}
515+
true
516+
} else {
517+
false
512518
}
513-
return serviceNotification
514519
}
515520

516521

topl-service/src/main/java/io/matthewnelson/topl_service/service/BaseService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,10 @@ internal abstract class BaseService: Service() {
315315
fun removeNotificationActions() {
316316
serviceNotification.removeActions(this)
317317
}
318-
fun startForegroundService(): ServiceNotification {
318+
open fun startForegroundService(): Boolean {
319319
return serviceNotification.startForeground(this)
320320
}
321-
fun stopForegroundService(): ServiceNotification {
321+
open fun stopForegroundService(): Boolean {
322322
return serviceNotification.stopForeground(this)
323323
}
324324
fun updateNotificationContentText(string: String) {

topl-service/src/main/java/io/matthewnelson/topl_service/service/TorService.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ internal class TorService: BaseService() {
161161

162162
// See BaseService
163163

164+
override fun startForegroundService(): Boolean {
165+
val wasStarted = super.startForegroundService()
166+
if (wasStarted)
167+
broadcastLogger.debug("Service sent to Foreground")
168+
return wasStarted
169+
}
170+
override fun stopForegroundService(): Boolean {
171+
val wasStopped = super.stopForegroundService()
172+
if (wasStopped)
173+
broadcastLogger.debug("Service sent to Background")
174+
return wasStopped
175+
}
176+
164177

165178
/////////////////
166179
/// TOPL-Core ///

0 commit comments

Comments
 (0)