From 09885f7dd37361b93e2a9eb45561a112c6982cd0 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Wed, 3 May 2023 08:38:18 +0530 Subject: [PATCH 1/2] Allow existing stream notifications to be replaced/updated. --- .../libretube/workers/NotificationWorker.kt | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt index a8eb6c6e1a..8a62af5b1c 100644 --- a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt +++ b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt @@ -5,7 +5,6 @@ import android.app.PendingIntent.FLAG_UPDATE_CURRENT import android.content.Context import android.content.Intent import android.graphics.Bitmap -import android.os.Build import android.util.Log import androidx.core.app.NotificationCompat import androidx.core.app.PendingIntentCompat @@ -16,7 +15,6 @@ import androidx.work.WorkerParameters import com.github.libretube.R import com.github.libretube.api.SubscriptionHelper import com.github.libretube.api.obj.StreamItem -import com.github.libretube.constants.DOWNLOAD_PROGRESS_NOTIFICATION_ID import com.github.libretube.constants.IntentData import com.github.libretube.constants.PUSH_CHANNEL_ID import com.github.libretube.constants.PreferenceKeys @@ -35,16 +33,8 @@ import kotlinx.coroutines.withContext */ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : CoroutineWorker(appContext, parameters) { - private val notificationManager = appContext.getSystemService()!! - // the id where notification channels start - private var notificationId = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - notificationManager.activeNotifications.size + DOWNLOAD_PROGRESS_NOTIFICATION_ID - } else { - DOWNLOAD_PROGRESS_NOTIFICATION_ID - } - override suspend fun doWork(): Result { if (!checkTime()) Result.success() // check whether there are new streams and notify if there are some @@ -117,13 +107,13 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : } // group the new streams by the uploader - val channelGroups = filteredVideos.groupBy { it.uploaderUrl } + val channelGroups = filteredVideos.groupBy { it.uploaderUrl!!.toID() } Log.d(TAG(), "Create notifications for new videos") // create a notification for each new stream channelGroups.forEach { (uploaderUrl, streams) -> - createNotificationsForChannel(uploaderUrl!!, streams) + createNotificationsForChannel(uploaderUrl, streams) } // save the latest streams that got notified about PreferenceHelper.setLatestVideoId(videoFeed.first().url!!.toID()) @@ -145,13 +135,12 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : showStreamNotification(group, it, false) } - val summaryId = ++notificationId + val notificationId = group.hashCode() val intent = Intent(applicationContext, MainActivity::class.java) .setFlags(INTENT_FLAGS) - .putExtra(IntentData.channelId, group.toID()) - + .putExtra(IntentData.channelId, group) val pendingIntent = PendingIntentCompat - .getActivity(applicationContext, summaryId, intent, FLAG_UPDATE_CURRENT, false) + .getActivity(applicationContext, notificationId, intent, FLAG_UPDATE_CURRENT, false) // Create summary notification containing new streams for Android versions below 7.0. val newStreams = applicationContext.resources @@ -170,7 +159,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY) .build() - notificationManager.notify(summaryId, summaryNotification) + notificationManager.notify(notificationId, summaryNotification) } } @@ -179,12 +168,13 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : stream: StreamItem, isSingleNotification: Boolean ) { + val videoId = stream.url!!.toID() val intent = Intent(applicationContext, MainActivity::class.java) .setFlags(INTENT_FLAGS) - .putExtra(IntentData.videoId, stream.url!!.toID()) - val code = ++notificationId + .putExtra(IntentData.videoId, videoId) + val notificationId = videoId.hashCode() val pendingIntent = PendingIntentCompat - .getActivity(applicationContext, code, intent, FLAG_UPDATE_CURRENT, false) + .getActivity(applicationContext, notificationId, intent, FLAG_UPDATE_CURRENT, false) val notificationBuilder = createNotificationBuilder(group) .setContentTitle(stream.title) @@ -208,7 +198,7 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : ) } - notificationManager.notify(code, notificationBuilder.build()) + notificationManager.notify(notificationId, notificationBuilder.build()) } private fun createNotificationBuilder(group: String): NotificationCompat.Builder { From 4b7ba2ee7625d08811bbf365a28bad41b21fbb45 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Thu, 4 May 2023 06:02:26 +0530 Subject: [PATCH 2/2] Avoid creating intermediate lists when filtering streams. --- .../libretube/workers/NotificationWorker.kt | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt index 8a62af5b1c..0af1961bcb 100644 --- a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt +++ b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt @@ -92,28 +92,24 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : return true } - // filter the new videos until the last seen video in the feed - val newStreams = videoFeed.takeWhile { it.url!!.toID() != lastSeenStreamId } - - // return if the previous video didn't get found - if (newStreams.isEmpty()) return true - - // hide for notifications unsubscribed channels val channelsToIgnore = PreferenceHelper.getIgnorableNotificationChannels() - val filteredVideos = newStreams.filter { - channelsToIgnore.none { channelId -> - channelId == it.uploaderUrl?.toID() - } - } - // group the new streams by the uploader - val channelGroups = filteredVideos.groupBy { it.uploaderUrl!!.toID() } + val channelGroups = videoFeed.asSequence() + // filter the new videos until the last seen video in the feed + .takeWhile { it.url!!.toID() != lastSeenStreamId } + // hide for notifications unsubscribed channels + .filter { it.uploaderUrl!!.toID() !in channelsToIgnore } + // group the new streams by the uploader + .groupBy { it.uploaderUrl!!.toID() } + + // return if the previous video didn't get found or all the channels have notifications disabled + if (channelGroups.isEmpty()) return true Log.d(TAG(), "Create notifications for new videos") // create a notification for each new stream - channelGroups.forEach { (uploaderUrl, streams) -> - createNotificationsForChannel(uploaderUrl, streams) + channelGroups.forEach { (channelId, streams) -> + createNotificationsForChannel(channelId, streams) } // save the latest streams that got notified about PreferenceHelper.setLatestVideoId(videoFeed.first().url!!.toID())