diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt index 9007ffc..feec285 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt @@ -31,6 +31,7 @@ import org.unifiedpush.distributor.nextpush.account.ssoAccount import org.unifiedpush.distributor.nextpush.api.ApiUtils import org.unifiedpush.distributor.nextpush.distributor.sendUnregistered import org.unifiedpush.distributor.nextpush.distributor.getDb +import org.unifiedpush.distributor.nextpush.services.startListener import java.lang.String.format private const val TAG = "NextPush-MainActivity" @@ -100,7 +101,7 @@ class MainActivity : AppCompatActivity() { format(getString(R.string.main_account_desc), ssoAccount.name) showLogout = true invalidateOptionsMenu() - startListener() + startListener(this) } private fun showStart() { @@ -144,7 +145,7 @@ class MainActivity : AppCompatActivity() { Log.d(TAG, "Restarting the Listener") val serviceIntent = Intent(this, StartService::class.java) this.stopService(serviceIntent) - startListener() + startListener(this) } private fun logout() { @@ -168,16 +169,6 @@ class MainActivity : AppCompatActivity() { alert.show() } - private fun startListener(){ - Log.d(TAG, "Starting the Listener") - val serviceIntent = Intent(this, StartService::class.java) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - this.startForegroundService(serviceIntent) - }else{ - this.startService(serviceIntent) - } - } - private fun setListView(){ listView = findViewById(R.id.applications_list) val db = getDb(this) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/NotificationUtils.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/NotificationUtils.kt new file mode 100644 index 0000000..3113ff2 --- /dev/null +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/NotificationUtils.kt @@ -0,0 +1,98 @@ +package org.unifiedpush.distributor.nextpush.services + +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.content.Context +import android.os.Build +import androidx.core.app.NotificationManagerCompat +import org.unifiedpush.distributor.nextpush.R +import android.app.PendingIntent + +import android.content.Intent +import org.unifiedpush.distributor.nextpush.activities.MainActivity + + +const val NOTIF_ID_FOREGROUND = 51115 +const val NOTIF_ID_WARNING = 51215 + +fun createForegroundNotification(context: Context): Notification { + val appName = context.getString(R.string.app_name) + val notificationChannelId = "$appName.Listener" + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager; + val channel = NotificationChannel( + notificationChannelId, + appName, + NotificationManager.IMPORTANCE_LOW + ).let { + it.description = context.getString(R.string.listening_notif_description) + it + } + notificationManager.createNotificationChannel(channel) + } + + val builder: Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder( + context, + notificationChannelId + ) else Notification.Builder(context) + + return builder + .setContentTitle(context.getString(R.string.app_name)) + .setContentText(context.getString(R.string.listening_notif_description)) + .setSmallIcon(R.drawable.ic_launcher_notification) + .setTicker(context.getString(R.string.listening_notif_ticker)) + .setPriority(Notification.PRIORITY_LOW) // for under android 26 compatibility + .build() +} + +fun createWarningNotification(context: Context) { + val appName = context.getString(R.string.app_name) + val notificationChannelId = "$appName.Warning" + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val notificationManager = + context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager; + val channel = NotificationChannel( + notificationChannelId, + appName, + NotificationManager.IMPORTANCE_HIGH + ).let { + it.description = context.getString(R.string.warning_notif_description) + it + } + notificationManager.createNotificationChannel(channel) + } + + val notificationIntent: Intent = Intent(context, MainActivity::class.java) + + notificationIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP + + val intent = PendingIntent.getActivity( + context, 0, + notificationIntent, 0 + ) + + val builder: Notification.Builder = ( + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder( + context, + notificationChannelId + ) + else Notification.Builder(context) + ).setContentTitle(context.getString(R.string.app_name)) + .setContentText(context.getString(R.string.warning_notif_description)) + .setSmallIcon(R.drawable.ic_launcher_notification) + .setTicker(context.getString(R.string.warning_notif_ticker)) + .setPriority(Notification.PRIORITY_HIGH) // for under android 26 compatibility + .setContentIntent(intent) + + with(NotificationManagerCompat.from(context)) { + notify(NOTIF_ID_WARNING, builder.build()) + } +} + +fun deleteWarningNotification(context: Context) { + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager; + notificationManager.cancel(NOTIF_ID_WARNING) +} \ No newline at end of file diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt index f85939f..ae762e9 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/SSEListener.kt @@ -14,11 +14,8 @@ import org.unifiedpush.distributor.nextpush.distributor.sendMessage private const val TAG = "SSEListener" class SSEListener (val context: Context) : EventSourceListener() { - private var pingtime = 0.toLong() - private val networkConnected = false - override fun onOpen(eventSource: EventSource, response: Response) { - pingtime = System.currentTimeMillis() + deleteWarningNotification(context) try { Log.d(TAG, "onOpen: " + response.code) } catch (e: Exception) { @@ -28,7 +25,6 @@ class SSEListener (val context: Context) : EventSourceListener() { override fun onEvent(eventSource: EventSource, id: String?, eventType: String?, data: String) { Log.d(TAG, "New SSE message event=$eventType message=$data") - pingtime = System.currentTimeMillis() if (eventType == "warning") { Log.d(TAG, "Warning event received.") // Notification warning @@ -49,18 +45,17 @@ class SSEListener (val context: Context) : EventSourceListener() { override fun onClosed(eventSource: EventSource) { Log.d(TAG, "onClosed: $eventSource") - if (!networkConnected) return + startListener(context) } override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) { + createWarningNotification(context) t?.let { Log.d(TAG, "An error occurred: $t") return } response?.let { Log.d(TAG, "onFailure: ${it.code}") - if (!networkConnected) return - return } } } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt index 3b5319b..764ccfb 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/StartService.kt @@ -1,8 +1,5 @@ package org.unifiedpush.distributor.nextpush.services -import android.app.Notification -import android.app.NotificationChannel -import android.app.NotificationManager import android.app.Service import android.content.Context import android.content.Intent @@ -16,12 +13,21 @@ import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException import com.nextcloud.android.sso.helper.SingleAccountHelper import com.nextcloud.android.sso.ui.UiExceptionManager -import org.unifiedpush.distributor.nextpush.R import org.unifiedpush.distributor.nextpush.api.ApiUtils import org.unifiedpush.distributor.nextpush.account.ssoAccount private const val TAG = "StartService" +fun startListener(context: Context){ + Log.d(TAG, "Starting the Listener") + val serviceIntent = Intent(context, StartService::class.java) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(serviceIntent) + }else{ + context.startService(serviceIntent) + } +} + class StartService: Service(){ private var isServiceStarted = false @@ -35,8 +41,8 @@ class StartService: Service(){ override fun onCreate(){ super.onCreate() Log.i(TAG,"Starting") - val notification = createNotification() - startForeground(51115, notification) + val notification = createForegroundNotification(this) + startForeground(NOTIF_ID_FOREGROUND, notification) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @@ -50,37 +56,6 @@ class StartService: Service(){ super.onDestroy() } - private fun createNotification(): Notification { - val appName = getString(R.string.app_name) - val notificationChannelId = "$appName.Listener" - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager; - val channel = NotificationChannel( - notificationChannelId, - appName, - NotificationManager.IMPORTANCE_LOW - ).let { - it.description = getString(R.string.listening_notif_description) - it - } - notificationManager.createNotificationChannel(channel) - } - - val builder: Notification.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) Notification.Builder( - this, - notificationChannelId - ) else Notification.Builder(this) - - return builder - .setContentTitle(getString(R.string.app_name)) - .setContentText(getString(R.string.listening_notif_description)) - .setSmallIcon(R.drawable.ic_launcher_notification) - .setTicker("Listening") - .setPriority(Notification.PRIORITY_LOW) // for under android 26 compatibility - .build() - } - private fun startService() { if (isServiceStarted) return isServiceStarted = true diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 13de9a8..8606922 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,7 +5,7 @@ Registered applications - Listening incoming notifications + Listening for incoming notifications Login You are not connected to Nextcloud yet Logout @@ -16,4 +16,7 @@ Account You are connected as: %s Restart Service + NextPush is disconnected + Warning + Listening