From 8d15bcae8df0776e12e476260d890ac8cb5a0b7e Mon Sep 17 00:00:00 2001 From: Jonas Kalderstam Date: Thu, 19 Dec 2024 19:53:30 +0100 Subject: [PATCH] fixed crash on older versions of Android Signed-off-by: Jonas Kalderstam --- .../feeder/model/workmanager/BaseWorker.kt | 38 +++++++++++++++++++ .../model/workmanager/BlockListWorker.kt | 7 ++++ .../feeder/model/workmanager/FeedSyncer.kt | 7 ++++ .../SyncServiceGetUpdatesWorker.kt | 7 ++++ .../workmanager/SyncServiceSendReadWorker.kt | 7 ++++ 5 files changed, 66 insertions(+) diff --git a/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/BaseWorker.kt b/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/BaseWorker.kt index e63330326f..f088b523fd 100644 --- a/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/BaseWorker.kt +++ b/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/BaseWorker.kt @@ -4,12 +4,17 @@ import android.annotation.TargetApi import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context +import android.content.pm.ServiceInfo import android.os.Build import androidx.annotation.RequiresApi +import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat +import androidx.work.ForegroundInfo import com.nononsenseapps.feeder.R +private const val SYNC_NOTIFICATION_ID = 42623 private const val SYNC_CHANNEL_ID = "feederSyncNotifications" +private const val SYNC_NOTIFICATION_GROUP = "com.nononsenseapps.feeder.SYNC" /** * This is safe to call multiple times @@ -29,3 +34,36 @@ private fun createNotificationChannel( notificationManager.createNotificationChannel(channel) } + +/** + * Necessary for older Android versions. + */ +fun createForegroundInfo( + context: Context, + notificationManager: NotificationManagerCompat, +): ForegroundInfo { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + createNotificationChannel(context, notificationManager) + } + + val syncingText = context.getString(R.string.syncing) + + val notification = + NotificationCompat.Builder(context.applicationContext, SYNC_CHANNEL_ID) + .setContentTitle(syncingText) + .setTicker(syncingText) + .setGroup(SYNC_NOTIFICATION_GROUP) + .setSmallIcon(R.drawable.ic_stat_sync) + .setOngoing(true) + .build() + + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + ForegroundInfo( + SYNC_NOTIFICATION_ID, + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC, + ) + } else { + ForegroundInfo(SYNC_NOTIFICATION_ID, notification) + } +} diff --git a/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/BlockListWorker.kt b/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/BlockListWorker.kt index 6b9d87ce92..dbbcd931d2 100644 --- a/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/BlockListWorker.kt +++ b/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/BlockListWorker.kt @@ -1,7 +1,9 @@ package com.nononsenseapps.feeder.model.workmanager import android.content.Context +import androidx.core.app.NotificationManagerCompat import androidx.work.CoroutineWorker +import androidx.work.ForegroundInfo import androidx.work.WorkerParameters import com.nononsenseapps.feeder.db.room.BlocklistDao import com.nononsenseapps.feeder.db.room.ID_UNSET @@ -18,8 +20,13 @@ class BlockListWorker(val context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams), DIAware { override val di: DI by closestDI(context) + private val notificationManager: NotificationManagerCompat by instance() private val blocklistDao: BlocklistDao by instance() + override suspend fun getForegroundInfo(): ForegroundInfo { + return createForegroundInfo(context, notificationManager) + } + override suspend fun doWork(): Result { logDebug(LOG_TAG, "Doing work...") diff --git a/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/FeedSyncer.kt b/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/FeedSyncer.kt index 3170259115..9de880ff24 100644 --- a/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/FeedSyncer.kt +++ b/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/FeedSyncer.kt @@ -2,9 +2,11 @@ package com.nononsenseapps.feeder.model.workmanager import android.content.Context import android.util.Log +import androidx.core.app.NotificationManagerCompat import androidx.work.Constraints import androidx.work.CoroutineWorker import androidx.work.ExistingWorkPolicy +import androidx.work.ForegroundInfo import androidx.work.NetworkType import androidx.work.OneTimeWorkRequestBuilder import androidx.work.WorkManager @@ -39,8 +41,13 @@ class FeedSyncer(val context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams), DIAware { override val di: DI by closestDI(context) + private val notificationManager: NotificationManagerCompat by instance() private val rssLocalSync: RssLocalSync by instance() + override suspend fun getForegroundInfo(): ForegroundInfo { + return createForegroundInfo(context, notificationManager) + } + override suspend fun doWork(): Result { var success: Boolean diff --git a/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/SyncServiceGetUpdatesWorker.kt b/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/SyncServiceGetUpdatesWorker.kt index 61c81be2b8..6ba4d32866 100644 --- a/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/SyncServiceGetUpdatesWorker.kt +++ b/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/SyncServiceGetUpdatesWorker.kt @@ -2,9 +2,11 @@ package com.nononsenseapps.feeder.model.workmanager import android.content.Context import android.util.Log +import androidx.core.app.NotificationManagerCompat import androidx.work.Constraints import androidx.work.CoroutineWorker import androidx.work.ExistingWorkPolicy +import androidx.work.ForegroundInfo import androidx.work.NetworkType import androidx.work.OneTimeWorkRequestBuilder import androidx.work.WorkManager @@ -22,9 +24,14 @@ class SyncServiceGetUpdatesWorker(val context: Context, workerParams: WorkerPara CoroutineWorker(context, workerParams), DIAware { override val di: DI by closestDI(context) + private val notificationManager: NotificationManagerCompat by instance() private val syncClient: SyncRestClient by instance() private val repository: Repository by instance() + override suspend fun getForegroundInfo(): ForegroundInfo { + return createForegroundInfo(context, notificationManager) + } + override suspend fun doWork(): Result { return try { Log.d(LOG_TAG, "Doing work") diff --git a/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/SyncServiceSendReadWorker.kt b/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/SyncServiceSendReadWorker.kt index 77ea2b239c..07bb127372 100644 --- a/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/SyncServiceSendReadWorker.kt +++ b/app/src/main/java/com/nononsenseapps/feeder/model/workmanager/SyncServiceSendReadWorker.kt @@ -2,7 +2,9 @@ package com.nononsenseapps.feeder.model.workmanager import android.content.Context import android.util.Log +import androidx.core.app.NotificationManagerCompat import androidx.work.CoroutineWorker +import androidx.work.ForegroundInfo import androidx.work.WorkerParameters import com.nononsenseapps.feeder.sync.SyncRestClient import org.kodein.di.DI @@ -14,8 +16,13 @@ class SyncServiceSendReadWorker(val context: Context, workerParams: WorkerParame CoroutineWorker(context, workerParams), DIAware { override val di: DI by closestDI(context) + private val notificationManager: NotificationManagerCompat by instance() private val syncClient: SyncRestClient by di.instance() + override suspend fun getForegroundInfo(): ForegroundInfo { + return createForegroundInfo(context, notificationManager) + } + override suspend fun doWork(): Result { return try { Log.d(LOG_TAG, "Doing work")