Warn when a low keepalive is used

This commit is contained in:
sim 2023-03-24 09:00:43 +01:00
parent ef14f709f2
commit 5bdad9ca4c
3 changed files with 49 additions and 16 deletions

View File

@ -15,6 +15,7 @@ import org.unifiedpush.distributor.nextpush.services.FailureHandler
import org.unifiedpush.distributor.nextpush.services.RestartNetworkCallback import org.unifiedpush.distributor.nextpush.services.RestartNetworkCallback
import org.unifiedpush.distributor.nextpush.services.RestartWorker import org.unifiedpush.distributor.nextpush.services.RestartWorker
import org.unifiedpush.distributor.nextpush.services.StartService import org.unifiedpush.distributor.nextpush.services.StartService
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showLowKeepaliveNotification
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showStartErrorNotification import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showStartErrorNotification
import org.unifiedpush.distributor.nextpush.utils.TAG import org.unifiedpush.distributor.nextpush.utils.TAG
import java.lang.Exception import java.lang.Exception
@ -47,6 +48,9 @@ class SSEListener(val context: Context) : EventSourceListener() {
val message = Gson().fromJson(data, SSEResponse::class.java) val message = Gson().fromJson(data, SSEResponse::class.java)
keepalive = message.keepalive keepalive = message.keepalive
Log.d(TAG, "New keepalive: $keepalive") Log.d(TAG, "New keepalive: $keepalive")
if (keepalive < 25) {
showLowKeepaliveNotification(context, keepalive)
}
} }
"message" -> { "message" -> {
val message = Gson().fromJson(data, SSEResponse::class.java) val message = Gson().fromJson(data, SSEResponse::class.java)

View File

@ -16,7 +16,6 @@ import org.unifiedpush.distributor.nextpush.activities.MainActivity
const val NOTIFICATION_ID_FOREGROUND = 51115 const val NOTIFICATION_ID_FOREGROUND = 51115
const val NOTIFICATION_ID_WARNING = 51215 const val NOTIFICATION_ID_WARNING = 51215
const val NOTIFICATION_ID_START_ERROR = 51315
private data class ChannelData( private data class ChannelData(
val id: String, val id: String,
@ -34,6 +33,8 @@ private data class NotificationData(
) )
object NotificationUtils { object NotificationUtils {
private val Context.WARNING_CHANNEL_ID
get() = "${this.getString(R.string.app_name)}.Warning"
private var warningShown = false private var warningShown = false
private fun createNotificationChannel(context: Context, channelData: ChannelData) { private fun createNotificationChannel(context: Context, channelData: ChannelData) {
@ -139,7 +140,7 @@ object NotificationUtils {
if (warningShown) { if (warningShown) {
return return
} }
val notificationChannelId = "${context.getString(R.string.app_name)}.Warning" val notificationChannelId = context.WARNING_CHANNEL_ID
createNotificationChannel( createNotificationChannel(
context, context,
@ -169,16 +170,23 @@ object NotificationUtils {
warningShown = true warningShown = true
} }
fun deleteWarningNotification(context: Context) {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(NOTIFICATION_ID_WARNING)
warningShown = false
}
fun showStartErrorNotification(context: Context) { fun showStartErrorNotification(context: Context) {
val notificationChannelId = "${context.getString(R.string.app_name)}.StartError" val notificationChannelId = context.WARNING_CHANNEL_ID
createNotificationChannel( createNotificationChannel(
context, context,
ChannelData( ChannelData(
notificationChannelId, notificationChannelId,
"Start error", "Warning",
NotificationManager.IMPORTANCE_HIGH, NotificationManager.IMPORTANCE_HIGH,
context.getString(R.string.start_error_notif_description) context.getString(R.string.warning_notif_description)
) )
) )
@ -186,7 +194,7 @@ object NotificationUtils {
context, context,
NotificationData( NotificationData(
context.getString(R.string.start_error_notif_content), context.getString(R.string.start_error_notif_content),
context.getString(R.string.start_error_notif_ticker), context.getString(R.string.warning_notif_ticker),
Notification.PRIORITY_HIGH, Notification.PRIORITY_HIGH,
false, false,
notificationChannelId notificationChannelId
@ -195,13 +203,35 @@ object NotificationUtils {
true true
) )
show(context, NOTIFICATION_ID_START_ERROR, notification) show(context, NOTIFICATION_ID_WARNING, notification)
} }
fun deleteWarningNotification(context: Context) { fun showLowKeepaliveNotification(context: Context, keepalive: Int) {
val notificationManager = val notificationChannelId = context.WARNING_CHANNEL_ID
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(NOTIFICATION_ID_WARNING) createNotificationChannel(
warningShown = false context,
ChannelData(
notificationChannelId,
"Warning",
NotificationManager.IMPORTANCE_HIGH,
context.getString(R.string.warning_notif_description)
)
)
val notification = createNotification(
context,
NotificationData(
context.getString(R.string.low_keepalive_notif_content).format(keepalive),
context.getString(R.string.warning_notif_ticker),
Notification.PRIORITY_HIGH,
false,
notificationChannelId
),
null,
true
)
show(context, NOTIFICATION_ID_WARNING, notification)
} }
} }

View File

@ -15,12 +15,11 @@
<string name="main_account_title">Account</string> <string name="main_account_title">Account</string>
<string name="main_account_desc">You are connected as: %s</string> <string name="main_account_desc">You are connected as: %s</string>
<string name="action_restart">Restart Service</string> <string name="action_restart">Restart Service</string>
<string name="start_error_notif_content">The service could not be started correctly. Check the configuration of your server.</string>
<string name="start_error_notif_description">The service can not start correctly</string>
<string name="start_error_notif_ticker">Error</string>
<string name="warning_notif_content">NextPush is disconnected</string> <string name="warning_notif_content">NextPush is disconnected</string>
<string name="warning_notif_description">Warn when NextPush is disconnected</string> <string name="warning_notif_description">Warn when NextPush is disconnected or an issue occured.</string>
<string name="warning_notif_ticker">Warning</string> <string name="warning_notif_ticker">Warning</string>
<string name="start_error_notif_content">The service could not be started correctly. Check the configuration of your server.</string>
<string name="low_keepalive_notif_content">The server app is configured with a low keepalive: %ss. It will drain your battery. We recommend using a higher keepalive.</string>
<string name="foreground_notif_ticker">Foreground</string> <string name="foreground_notif_ticker">Foreground</string>
<string name="message_missing_nextcloud_app">Nextcloud Files is not installed on your device.\nPlease install it</string> <string name="message_missing_nextcloud_app">Nextcloud Files is not installed on your device.\nPlease install it</string>
<string name="uri_market_nextcloud_app">market://details?id=com.nextcloud.client</string> <string name="uri_market_nextcloud_app">market://details?id=com.nextcloud.client</string>