Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Notification Service #85

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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() {
Expand All @@ -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) {
Expand All @@ -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<NotificationManager>()
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.
*/
Expand Down
Loading