Skip to content

Commit c9d155a

Browse files
Isira-SeneviratneProfpatsch
authored andcommitted
Combine notification and ForegroundInfo creation methods
1 parent 4e31cce commit c9d155a

2 files changed

Lines changed: 44 additions & 51 deletions

File tree

app/src/main/java/org/schabi/newpipe/local/subscription/workers/SubscriptionExportWorker.kt

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.schabi.newpipe.local.subscription.workers
22

3-
import android.app.Notification
43
import android.content.Context
54
import android.content.pm.ServiceInfo
65
import android.net.Uri
@@ -30,8 +29,7 @@ class SubscriptionExportWorker(
3029
) : CoroutineWorker(appContext, params) {
3130
// This is needed for API levels < 31 (Android S).
3231
override suspend fun getForegroundInfo(): ForegroundInfo {
33-
val notification = createNotification(applicationContext.getString(R.string.export_ongoing))
34-
return createForegroundInfo(notification)
32+
return createForegroundInfo(applicationContext.getString(R.string.export_ongoing))
3533
}
3634

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

4644
val qty = subscriptions.size
47-
val title =
48-
applicationContext.resources.getQuantityString(R.plurals.export_subscriptions, qty, qty)
49-
setForeground(createForegroundInfo(createNotification(title)))
45+
val title = applicationContext.resources.getQuantityString(R.plurals.export_subscriptions, qty, qty)
46+
setForeground(createForegroundInfo(title))
5047

5148
withContext(Dispatchers.IO) {
5249
applicationContext.contentResolver.openOutputStream(uri)?.use {
@@ -80,18 +77,17 @@ class SubscriptionExportWorker(
8077
}
8178
}
8279

83-
private fun createNotification(title: String): Notification =
84-
NotificationCompat
85-
.Builder(applicationContext, NOTIFICATION_CHANNEL_ID)
86-
.setSmallIcon(R.drawable.ic_newpipe_triangle_white)
87-
.setOngoing(true)
88-
.setProgress(-1, -1, true)
89-
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
90-
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
91-
.setContentTitle(title)
92-
.build()
93-
94-
private fun createForegroundInfo(notification: Notification): ForegroundInfo {
80+
private fun createForegroundInfo(title: String): ForegroundInfo {
81+
val notification =
82+
NotificationCompat
83+
.Builder(applicationContext, NOTIFICATION_CHANNEL_ID)
84+
.setSmallIcon(R.drawable.ic_newpipe_triangle_white)
85+
.setOngoing(true)
86+
.setProgress(-1, -1, true)
87+
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
88+
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
89+
.setContentTitle(title)
90+
.build()
9591
val serviceType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC else 0
9692
return ForegroundInfo(NOTIFICATION_ID, notification, serviceType)
9793
}

app/src/main/java/org/schabi/newpipe/local/subscription/workers/SubscriptionImportWorker.kt

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.schabi.newpipe.local.subscription.workers
22

3-
import android.app.Notification
43
import android.content.Context
54
import android.content.pm.ServiceInfo
65
import android.os.Build
@@ -33,8 +32,7 @@ class SubscriptionImportWorker(
3332
) : CoroutineWorker(appContext, params) {
3433
// This is needed for API levels < 31 (Android S).
3534
override suspend fun getForegroundInfo(): ForegroundInfo {
36-
val title = applicationContext.getString(R.string.import_ongoing)
37-
return createForegroundInfo(createNotification(title, null, 0, 0))
35+
return createForegroundInfo(applicationContext.getString(R.string.import_ongoing), null, 0, 0)
3836
}
3937

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

8078
val currentIndex = mutex.withLock { index++ }
81-
val notification = createNotification(title, channelInfo.name, currentIndex, qty)
82-
setForeground(createForegroundInfo(notification))
79+
setForeground(createForegroundInfo(title, channelInfo.name, currentIndex, qty))
8380

8481
Pair(channelInfo, listOf(channelTab))
8582
}
8683
}.awaitAll()
8784
}
8885

8986
title = applicationContext.resources.getQuantityString(R.plurals.import_subscriptions, qty, qty)
90-
setForeground(createForegroundInfo(createNotification(title, null, 0, 0)))
87+
setForeground(createForegroundInfo(title, null, 0, 0))
9188
index = 0
9289

9390
val subscriptionManager = SubscriptionManager(applicationContext)
@@ -96,7 +93,7 @@ class SubscriptionImportWorker(
9693
subscriptionManager.upsertAll(chunk)
9794
}
9895
index += chunk.size
99-
setForeground(createForegroundInfo(createNotification(title, null, index, qty)))
96+
setForeground(createForegroundInfo(title, null, index, qty))
10097
}
10198

10299
withContext(Dispatchers.Main) {
@@ -108,38 +105,38 @@ class SubscriptionImportWorker(
108105
return Result.success()
109106
}
110107

111-
private fun createNotification(
108+
private fun createForegroundInfo(
112109
title: String,
113110
text: String?,
114111
currentProgress: Int,
115112
maxProgress: Int,
116-
): Notification =
117-
NotificationCompat
118-
.Builder(applicationContext, NOTIFICATION_CHANNEL_ID)
119-
.setSmallIcon(R.drawable.ic_newpipe_triangle_white)
120-
.setOngoing(true)
121-
.setProgress(maxProgress, currentProgress, currentProgress == 0)
122-
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
123-
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
124-
.setContentTitle(title)
125-
.setContentText(text)
126-
.addAction(
127-
R.drawable.ic_close,
128-
applicationContext.getString(R.string.cancel),
129-
WorkManager.getInstance(applicationContext).createCancelPendingIntent(id),
130-
).apply {
131-
if (currentProgress > 0 && maxProgress > 0) {
132-
val progressText = "$currentProgress/$maxProgress"
133-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
134-
setSubText(progressText)
135-
} else {
136-
setContentInfo(progressText)
113+
): ForegroundInfo {
114+
val notification =
115+
NotificationCompat
116+
.Builder(applicationContext, NOTIFICATION_CHANNEL_ID)
117+
.setSmallIcon(R.drawable.ic_newpipe_triangle_white)
118+
.setOngoing(true)
119+
.setProgress(maxProgress, currentProgress, currentProgress == 0)
120+
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
121+
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
122+
.setContentTitle(title)
123+
.setContentText(text)
124+
.addAction(
125+
R.drawable.ic_close,
126+
applicationContext.getString(R.string.cancel),
127+
WorkManager.getInstance(applicationContext).createCancelPendingIntent(id),
128+
).apply {
129+
if (currentProgress > 0 && maxProgress > 0) {
130+
val progressText = "$currentProgress/$maxProgress"
131+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
132+
setSubText(progressText)
133+
} else {
134+
setContentInfo(progressText)
135+
}
137136
}
138-
}
139-
}.build()
140-
141-
private fun createForegroundInfo(notification: Notification): ForegroundInfo {
137+
}.build()
142138
val serviceType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC else 0
139+
143140
return ForegroundInfo(NOTIFICATION_ID, notification, serviceType)
144141
}
145142

0 commit comments

Comments
 (0)