Show nb of registrations on foreground notif

This commit is contained in:
S1m 2024-01-16 19:53:38 +01:00
parent d2bb7d6df4
commit 8895a8e482
5 changed files with 28 additions and 7 deletions

View File

@ -38,7 +38,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
Timer().schedule(45_000L /* 45secs */) {
if (FailureHandler.newFail(context, eventSource)) {
StartService.stopService()
NoStartNotification(context).show()
NoStartNotification(context).showSingle()
}
}
} else {
@ -79,7 +79,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
AppCompanion.keepalive.set(it)
Log.d(TAG, "New keepalive: $it")
if (it < 25) {
LowKeepAliveNotification(context, it).show()
LowKeepAliveNotification(context, it).showSingle()
} else {
LowKeepAliveNotification(context, it).delete()
}

View File

@ -49,12 +49,12 @@ object FailureHandler {
Log.d(TAG, "EventSource is known or null")
ttlFails.getAndIncrement()
if (nFails.incrementAndGet() == 2) {
DisconnectedNotification(context).show()
DisconnectedNotification(context).showSingle()
}
if (AppCompanion.started.get() && !AppCompanion.pinged.get()) {
Log.d(TAG, "The service has started and it has never received a ping.")
if (nFailsBeforePing.incrementAndGet() == 5) {
NoPingNotification(context).show()
NoPingNotification(context).showSingle()
}
}
this.eventSource.getAndSet(null)?.cancel()
@ -89,7 +89,7 @@ object FailureHandler {
nFails.set(5)
ttlFails.getAndIncrement()
this.eventSource.getAndSet(null)?.cancel()
DisconnectedNotification(context).show()
DisconnectedNotification(context).showSingle()
}
fun clearFails() {

View File

@ -3,12 +3,13 @@ package org.unifiedpush.distributor.nextpush.services
import android.content.Context
import android.util.Log
import org.unifiedpush.distributor.nextpush.Database
import org.unifiedpush.distributor.nextpush.utils.ForegroundNotification
import org.unifiedpush.distributor.nextpush.utils.TAG
object RegistrationCountCache {
private var nRegistrations: Int? = null
private fun getCount(context: Context): Int {
fun getCount(context: Context): Int {
return nRegistrations ?: Database.getDb(context).listTokens().size.apply {
nRegistrations = this
}
@ -24,6 +25,7 @@ object RegistrationCountCache {
if (count in intArrayOf(0,1)) {
RestartWorker.run(context, delay = 0, force = true)
}
ForegroundNotification(context).update()
}
fun oneOrMore(context: Context): Boolean = synchronized(this) {

View File

@ -13,6 +13,7 @@ import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationManagerCompat
import org.unifiedpush.distributor.nextpush.R
import org.unifiedpush.distributor.nextpush.activities.MainActivity
import org.unifiedpush.distributor.nextpush.services.RegistrationCountCache
import java.util.concurrent.atomic.AtomicBoolean
const val NOTIFICATION_ID_FOREGROUND = 51115
@ -130,12 +131,23 @@ open class AppNotification(
createIntentToMain()
)
}
fun show() {
show(notificationId, create())
}
fun showSingle() {
if (!shown.getAndSet(true)) {
show(notificationId, create())
}
}
fun update() {
if (shown.get()) {
show(notificationId, create())
}
}
fun delete() {
if (shown.getAndSet(false)) {
deleteNotification(notificationId)
@ -212,7 +224,12 @@ class ForegroundNotification(context: Context) : AppNotification(
NOTIFICATION_ID_FOREGROUND,
NotificationData(
context.getString(R.string.app_name),
context.getString(R.string.foreground_notif_description),
if (RegistrationCountCache.oneOrMore(context)) {
context.getString(R.string.foreground_notif_content_with_reg)
.format(RegistrationCountCache.getCount(context))
} else {
context.getString(R.string.foreground_notif_content_no_reg)
},
context.getString(R.string.foreground_notif_ticker),
Notification.PRIORITY_LOW,
true

View File

@ -6,6 +6,8 @@
<string name="help"></string>
<string name="foreground_notif_description">Notification to run in the foreground</string>
<string name="foreground_notif_content_no_reg">Waiting for registration to connect</string>
<string name="foreground_notif_content_with_reg">Connected for %s registration(s)</string>
<string name="sso_connection_button">Login with the Nextcloud File application</string>
<string name="action_logout">Logout</string>
<string name="logout_alert_title">Logout</string>