Skip to content

Commit

Permalink
Combine notification and ForegroundInfo creation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Nov 30, 2024
1 parent 0bf953a commit dbf1960
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.schabi.newpipe.local.subscription.workers

import android.app.Notification
import android.content.Context
import android.content.pm.ServiceInfo
import android.net.Uri
Expand Down Expand Up @@ -30,8 +29,7 @@ class SubscriptionExportWorker(
) : CoroutineWorker(appContext, params) {
// This is needed for API levels < 31 (Android S).
override suspend fun getForegroundInfo(): ForegroundInfo {
val notification = createNotification(applicationContext.getString(R.string.export_ongoing))
return createForegroundInfo(notification)
return createForegroundInfo(applicationContext.getString(R.string.export_ongoing))
}

override suspend fun doWork(): Result {
Expand All @@ -44,9 +42,8 @@ class SubscriptionExportWorker(
.map { SubscriptionItem(it.serviceId, it.url, it.name) }

val qty = subscriptions.size
val title =
applicationContext.resources.getQuantityString(R.plurals.export_subscriptions, qty, qty)
setForeground(createForegroundInfo(createNotification(title)))
val title = applicationContext.resources.getQuantityString(R.plurals.export_subscriptions, qty, qty)
setForeground(createForegroundInfo(title))

withContext(Dispatchers.IO) {
applicationContext.contentResolver.openOutputStream(uri)?.use {
Expand Down Expand Up @@ -80,18 +77,17 @@ class SubscriptionExportWorker(
}
}

private fun createNotification(title: String): Notification =
NotificationCompat
.Builder(applicationContext, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_newpipe_triangle_white)
.setOngoing(true)
.setProgress(-1, -1, true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
.setContentTitle(title)
.build()

private fun createForegroundInfo(notification: Notification): ForegroundInfo {
private fun createForegroundInfo(title: String): ForegroundInfo {
val notification =
NotificationCompat
.Builder(applicationContext, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_newpipe_triangle_white)
.setOngoing(true)
.setProgress(-1, -1, true)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
.setContentTitle(title)
.build()
val serviceType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC else 0
return ForegroundInfo(NOTIFICATION_ID, notification, serviceType)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.schabi.newpipe.local.subscription.workers

import android.app.Notification
import android.content.Context
import android.content.pm.ServiceInfo
import android.os.Build
Expand Down Expand Up @@ -33,8 +32,7 @@ class SubscriptionImportWorker(
) : CoroutineWorker(appContext, params) {
// This is needed for API levels < 31 (Android S).
override suspend fun getForegroundInfo(): ForegroundInfo {
val title = applicationContext.getString(R.string.import_ongoing)
return createForegroundInfo(createNotification(title, null, 0, 0))
return createForegroundInfo(applicationContext.getString(R.string.import_ongoing), null, 0, 0)
}

override suspend fun doWork(): Result {
Expand Down Expand Up @@ -78,16 +76,15 @@ class SubscriptionImportWorker(
ExtractorHelper.getChannelTab(it.serviceId, channelInfo.tabs[0], true).await()

val currentIndex = mutex.withLock { index++ }
val notification = createNotification(title, channelInfo.name, currentIndex, qty)
setForeground(createForegroundInfo(notification))
setForeground(createForegroundInfo(title, channelInfo.name, currentIndex, qty))

Pair(channelInfo, listOf(channelTab))
}
}.awaitAll()
}

title = applicationContext.resources.getQuantityString(R.plurals.import_subscriptions, qty, qty)
setForeground(createForegroundInfo(createNotification(title, null, 0, 0)))
setForeground(createForegroundInfo(title, null, 0, 0))
index = 0

val subscriptionManager = SubscriptionManager(applicationContext)
Expand All @@ -96,7 +93,7 @@ class SubscriptionImportWorker(
subscriptionManager.upsertAll(chunk)
}
index += chunk.size
setForeground(createForegroundInfo(createNotification(title, null, index, qty)))
setForeground(createForegroundInfo(title, null, index, qty))
}

withContext(Dispatchers.Main) {
Expand All @@ -108,38 +105,38 @@ class SubscriptionImportWorker(
return Result.success()
}

private fun createNotification(
private fun createForegroundInfo(
title: String,
text: String?,
currentProgress: Int,
maxProgress: Int,
): Notification =
NotificationCompat
.Builder(applicationContext, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_newpipe_triangle_white)
.setOngoing(true)
.setProgress(maxProgress, currentProgress, currentProgress == 0)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
.setContentTitle(title)
.setContentText(text)
.addAction(
R.drawable.ic_close,
applicationContext.getString(R.string.cancel),
WorkManager.getInstance(applicationContext).createCancelPendingIntent(id),
).apply {
if (currentProgress > 0 && maxProgress > 0) {
val progressText = "$currentProgress/$maxProgress"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setSubText(progressText)
} else {
setContentInfo(progressText)
): ForegroundInfo {
val notification =
NotificationCompat
.Builder(applicationContext, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_newpipe_triangle_white)
.setOngoing(true)
.setProgress(maxProgress, currentProgress, currentProgress == 0)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
.setContentTitle(title)
.setContentText(text)
.addAction(
R.drawable.ic_close,
applicationContext.getString(R.string.cancel),
WorkManager.getInstance(applicationContext).createCancelPendingIntent(id),
).apply {
if (currentProgress > 0 && maxProgress > 0) {
val progressText = "$currentProgress/$maxProgress"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setSubText(progressText)
} else {
setContentInfo(progressText)
}
}
}
}.build()

private fun createForegroundInfo(notification: Notification): ForegroundInfo {
}.build()
val serviceType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC else 0

return ForegroundInfo(NOTIFICATION_ID, notification, serviceType)
}

Expand Down

0 comments on commit dbf1960

Please sign in to comment.