11package org.schabi.newpipe.local.subscription.workers
22
3- import android.app.Notification
43import android.content.Context
54import android.content.pm.ServiceInfo
65import 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