From a2223ad74ce9de0e8e6427d53307f6fab0ae868f Mon Sep 17 00:00:00 2001 From: Rowdy Mitchell Chotkan Date: Tue, 27 Feb 2024 15:51:57 +0100 Subject: [PATCH] Update existing notification instead of replace --- .../ipv8/android/service/IPv8Service.kt | 61 +++++++++++++------ 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/ipv8-android/src/main/java/nl/tudelft/ipv8/android/service/IPv8Service.kt b/ipv8-android/src/main/java/nl/tudelft/ipv8/android/service/IPv8Service.kt index 6466e165..18069d0e 100644 --- a/ipv8-android/src/main/java/nl/tudelft/ipv8/android/service/IPv8Service.kt +++ b/ipv8-android/src/main/java/nl/tudelft/ipv8/android/service/IPv8Service.kt @@ -24,7 +24,7 @@ import nl.tudelft.ipv8.android.R import kotlin.system.exitProcess open class IPv8Service : Service(), LifecycleObserver { - private val scope = CoroutineScope(Dispatchers.Default) + protected val scope = CoroutineScope(Dispatchers.Default) private var isForeground = false @@ -60,13 +60,13 @@ open class IPv8Service : Service(), LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_STOP) fun onBackground() { isForeground = false - showForegroundNotification() + updateNotification() } @OnLifecycleEvent(Lifecycle.Event.ON_START) fun onForeground() { isForeground = true - showForegroundNotification() + updateNotification() } private fun createNotificationChannel() { @@ -89,25 +89,12 @@ open class IPv8Service : Service(), LifecycleObserver { } private fun showForegroundNotification() { - val cancelBroadcastIntent = Intent(this, StopIPv8Receiver::class.java) - val flags = - when { - SDK_INT >= Build.VERSION_CODES.M -> FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE - else -> FLAG_UPDATE_CURRENT - } - val cancelPendingIntent = - PendingIntent.getBroadcast( - applicationContext, - 0, - cancelBroadcastIntent, - flags, - ) - val builder = createNotification() - // Allow cancellation when the app is running in background + // Allow cancellation when the app is running in background. + // Should technically not be triggered. if (!isForeground) { - builder.addAction(NotificationCompat.Action(0, "Stop", cancelPendingIntent)) + addStopAction(builder) } if (SDK_INT > Build.VERSION_CODES.TIRAMISU) { @@ -133,6 +120,42 @@ open class IPv8Service : Service(), LifecycleObserver { .setContentText("Running") } + /** + * Updates the notification to show the current state of the IPv8 service. + */ + protected fun updateNotification() { + val builder = createNotification() + + // Allow cancellation when the app is running in background. + if (!isForeground) { + addStopAction(builder) + } + + val mNotificationManager = + getSystemService() + mNotificationManager?.notify(ONGOING_NOTIFICATION_ID, builder.build()) + } + + /** + * Adds a stop action to the notification. + */ + private fun addStopAction(builder: NotificationCompat.Builder) { + val cancelBroadcastIntent = Intent(this, StopIPv8Receiver::class.java) + val flags = + when { + SDK_INT >= Build.VERSION_CODES.M -> FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE + else -> FLAG_UPDATE_CURRENT + } + val cancelPendingIntent = + PendingIntent.getBroadcast( + applicationContext, + 0, + cancelBroadcastIntent, + flags, + ) + builder.addAction(NotificationCompat.Action(0, "Stop", cancelPendingIntent)) + } + /** * Returns a running IPv8 instance. */