Skip to content

Commit

Permalink
fixed crash on older versions of Android
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Kalderstam <[email protected]>
  • Loading branch information
spacecowboy committed Dec 19, 2024
1 parent d59fe0f commit 8d15bca
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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...")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down

0 comments on commit 8d15bca

Please sign in to comment.