diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt index 1fb962e..8b4d214 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt @@ -23,6 +23,9 @@ import java.util.Calendar class SSEListener(val context: Context) : EventSourceListener() { + private var pinged = false + private var started = false + override fun onOpen(eventSource: EventSource, response: Response) { FailureHandler.newEventSource(context, eventSource) StartService.wakeLock?.let { @@ -43,7 +46,14 @@ class SSEListener(val context: Context) : EventSourceListener() { lastEventDate = Calendar.getInstance() when (type) { - "start" -> context.hasStartedOnce = true + "start" -> { + started = true + context.hasStartedOnce = true + } + "ping" -> { + pinged = true + FailureHandler.newPing() + } "keepalive" -> { val message = Gson().fromJson(data, SSEResponse::class.java) keepalive = message.keepalive @@ -76,7 +86,7 @@ class SSEListener(val context: Context) : EventSourceListener() { Log.d(TAG, "onClosed: $eventSource") eventSource.cancel() if (!shouldRestart()) return - FailureHandler.newFail(context, eventSource) + FailureHandler.newFail(context, eventSource, started, pinged) RestartWorker.run(context, delay = 0) } @@ -95,7 +105,7 @@ class SSEListener(val context: Context) : EventSourceListener() { FailureHandler.once(eventSource) return } - FailureHandler.newFail(context, eventSource) + FailureHandler.newFail(context, eventSource, started, pinged) val delay = when (FailureHandler.nFails) { 1 -> 2 // 2sec 2 -> 5 // 5sec diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt index 17a6e7d..ae285f4 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt @@ -4,6 +4,7 @@ import android.content.Context import android.util.Log import okhttp3.sse.EventSource import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.deleteWarningNotification +import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showNoPingNotification import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.showWarningNotification import org.unifiedpush.distributor.nextpush.utils.TAG @@ -12,6 +13,8 @@ object FailureHandler { var nFails = 0 private set + private var nFailsBeforePing = 0 + // This is the last eventSource opened private var eventSource: EventSource? = null @@ -22,7 +25,11 @@ object FailureHandler { deleteWarningNotification(context) } - fun newFail(context: Context, eventSource: EventSource?) { + fun newPing() { + nFailsBeforePing = 0 + } + + fun newFail(context: Context, eventSource: EventSource?, started: Boolean, pinged: Boolean) { Log.d(TAG, "newFail/Eventsource: $eventSource") // ignore fails from a possible old eventSource // if we are already reconnected @@ -32,6 +39,12 @@ object FailureHandler { if (nFails == 2) { showWarningNotification(context) } + if (started && !pinged) { + nFailsBeforePing++ + if (nFailsBeforePing == 3) { + showNoPingNotification(context) + } + } this.eventSource = null } } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/NotificationUtils.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/NotificationUtils.kt index 7f4b665..06d716f 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/NotificationUtils.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/NotificationUtils.kt @@ -33,8 +33,6 @@ private data class NotificationData( ) object NotificationUtils { - private val Context.WARNING_CHANNEL_ID - get() = "${this.getString(R.string.app_name)}.Warning" private var warningShown = false private fun createNotificationChannel(context: Context, channelData: ChannelData) { @@ -136,11 +134,8 @@ object NotificationUtils { ) } - fun showWarningNotification(context: Context) { - if (warningShown) { - return - } - val notificationChannelId = context.WARNING_CHANNEL_ID + private fun createWarningNotificationChannel(context: Context): String { + val notificationChannelId = "${context.getString(R.string.app_name)}.Warning" createNotificationChannel( context, @@ -151,6 +146,14 @@ object NotificationUtils { context.getString(R.string.warning_notif_description) ) ) + return notificationChannelId + } + + fun showWarningNotification(context: Context) { + if (warningShown) { + return + } + val notificationChannelId = createWarningNotificationChannel(context) val intent = createIntentToMain(context) @@ -178,17 +181,7 @@ object NotificationUtils { } fun showStartErrorNotification(context: Context) { - val notificationChannelId = context.WARNING_CHANNEL_ID - - createNotificationChannel( - context, - ChannelData( - notificationChannelId, - "Warning", - NotificationManager.IMPORTANCE_HIGH, - context.getString(R.string.warning_notif_description) - ) - ) + val notificationChannelId = createWarningNotificationChannel(context) val notification = createNotification( context, @@ -207,17 +200,7 @@ object NotificationUtils { } fun showLowKeepaliveNotification(context: Context, keepalive: Int) { - val notificationChannelId = context.WARNING_CHANNEL_ID - - createNotificationChannel( - context, - ChannelData( - notificationChannelId, - "Warning", - NotificationManager.IMPORTANCE_HIGH, - context.getString(R.string.warning_notif_description) - ) - ) + val notificationChannelId = createWarningNotificationChannel(context) val notification = createNotification( context, @@ -234,4 +217,23 @@ object NotificationUtils { show(context, NOTIFICATION_ID_WARNING, notification) } + + fun showNoPingNotification(context: Context) { + val notificationChannelId = createWarningNotificationChannel(context) + + val notification = createNotification( + context, + NotificationData( + context.getString(R.string.no_ping_notif_content), + context.getString(R.string.warning_notif_ticker), + Notification.PRIORITY_HIGH, + false, + notificationChannelId + ), + null, + true + ) + + show(context, NOTIFICATION_ID_WARNING, notification) + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ba43f06..8131a6b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -20,6 +20,7 @@ Warning The service could not be started correctly. Check the configuration of your server. The server app is configured with a low keepalive: %ss. It will drain your battery. We recommend using a higher keepalive. + NextPush was disconnected 3 times before receiving the ping. You probably have a problem with your server configuration. Your reverse proxy timeout is probably too low. Foreground Nextcloud Files is not installed on your device.\nPlease install it market://details?id=com.nextcloud.client