From 971cd73fb3cf5fd37c0b5f243a9a81c125dd378a Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 25 Jul 2019 01:37:47 -0700 Subject: [PATCH] Dismiss notification on error --- .../magisk/model/download/NotificationService.kt | 14 +++++++------- .../magisk/model/download/RemoteFileService.kt | 1 + .../com/topjohnwu/magisk/view/Notifications.java | 9 ++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/model/download/NotificationService.kt b/app/src/main/java/com/topjohnwu/magisk/model/download/NotificationService.kt index d1a1138da..034f08897 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/download/NotificationService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/download/NotificationService.kt @@ -46,9 +46,6 @@ abstract class NotificationService : Service() { ) : Int { val currentNotification = remove(id)?.run(editBody) - updateForeground() - - cancel(id) var newId = -1 currentNotification?.let { newId = nextInt(Int.MAX_VALUE) @@ -56,7 +53,6 @@ abstract class NotificationService : Service() { } if (!hasNotifications) { - stopForeground(true) stopSelf() } return newId @@ -72,12 +68,16 @@ abstract class NotificationService : Service() { manager.cancel(id) } - private fun remove(id: Int) = notifications.remove(id) - .also { updateForeground() } + protected fun remove(id: Int) = notifications.remove(id).also { + cancel(id) + updateForeground() + } private fun updateForeground() { - if (notifications.isNotEmpty()) + if (hasNotifications) startForeground(notifications.keys.first(), notifications.values.first().build()) + else + stopForeground(true) } // -- diff --git a/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt b/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt index 73a2f79ad..5e6e0e7a5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/download/RemoteFileService.kt @@ -49,6 +49,7 @@ abstract class RemoteFileService : NotificationService() { .onErrorResumeNext(download(subject)) .doOnSubscribe { update(subject.hashCode()) { it.setContentTitle(subject.title) } } .observeOn(AndroidSchedulers.mainThread()) + .doOnError { remove(subject.hashCode()) } .doOnSuccess { val id = finish(it, subject) runCatching { onFinished(it, subject, id) }.onFailure { Timber.e(it) } diff --git a/app/src/main/java/com/topjohnwu/magisk/view/Notifications.java b/app/src/main/java/com/topjohnwu/magisk/view/Notifications.java index d642bdd7b..1faa7fe2a 100644 --- a/app/src/main/java/com/topjohnwu/magisk/view/Notifications.java +++ b/app/src/main/java/com/topjohnwu/magisk/view/Notifications.java @@ -7,6 +7,10 @@ import android.content.Context; import android.content.Intent; import android.os.Build; +import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; +import androidx.core.app.TaskStackBuilder; + import com.topjohnwu.magisk.App; import com.topjohnwu.magisk.ClassMap; import com.topjohnwu.magisk.Const; @@ -16,17 +20,12 @@ import com.topjohnwu.magisk.model.receiver.GeneralReceiver; import com.topjohnwu.magisk.ui.SplashActivity; import com.topjohnwu.magisk.utils.Utils; -import androidx.core.app.NotificationCompat; -import androidx.core.app.NotificationManagerCompat; -import androidx.core.app.TaskStackBuilder; - public class Notifications { public static NotificationManagerCompat mgr = NotificationManagerCompat.from(App.self); public static void setup(Context c) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationManager mgr = c.getSystemService(NotificationManager.class); mgr.deleteNotificationChannel("magisk_notification"); NotificationChannel channel = new NotificationChannel(Const.ID.UPDATE_NOTIFICATION_CHANNEL,