mirror of
				https://github.com/TeamNewPipe/NewPipe
				synced 2025-10-31 07:13:00 +00:00 
			
		
		
		
	Combine notification and ForegroundInfo creation methods
This commit is contained in:
		 Isira Seneviratne
					Isira Seneviratne
				
			
				
					committed by
					
						 Profpatsch
						Profpatsch
					
				
			
			
				
	
			
			
			 Profpatsch
						Profpatsch
					
				
			
						parent
						
							4e31ccebf8
						
					
				
				
					commit
					c9d155a335
				
			| @@ -1,6 +1,5 @@ | |||||||
| package org.schabi.newpipe.local.subscription.workers | package org.schabi.newpipe.local.subscription.workers | ||||||
|  |  | ||||||
| import android.app.Notification |  | ||||||
| import android.content.Context | import android.content.Context | ||||||
| import android.content.pm.ServiceInfo | import android.content.pm.ServiceInfo | ||||||
| import android.net.Uri | import android.net.Uri | ||||||
| @@ -30,8 +29,7 @@ class SubscriptionExportWorker( | |||||||
| ) : CoroutineWorker(appContext, params) { | ) : CoroutineWorker(appContext, params) { | ||||||
|     // This is needed for API levels < 31 (Android S). |     // This is needed for API levels < 31 (Android S). | ||||||
|     override suspend fun getForegroundInfo(): ForegroundInfo { |     override suspend fun getForegroundInfo(): ForegroundInfo { | ||||||
|         val notification = createNotification(applicationContext.getString(R.string.export_ongoing)) |         return createForegroundInfo(applicationContext.getString(R.string.export_ongoing)) | ||||||
|         return createForegroundInfo(notification) |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     override suspend fun doWork(): Result { |     override suspend fun doWork(): Result { | ||||||
| @@ -44,9 +42,8 @@ class SubscriptionExportWorker( | |||||||
|                     .map { SubscriptionItem(it.serviceId, it.url, it.name) } |                     .map { SubscriptionItem(it.serviceId, it.url, it.name) } | ||||||
|  |  | ||||||
|             val qty = subscriptions.size |             val qty = subscriptions.size | ||||||
|             val title = |             val title = applicationContext.resources.getQuantityString(R.plurals.export_subscriptions, qty, qty) | ||||||
|                 applicationContext.resources.getQuantityString(R.plurals.export_subscriptions, qty, qty) |             setForeground(createForegroundInfo(title)) | ||||||
|             setForeground(createForegroundInfo(createNotification(title))) |  | ||||||
|  |  | ||||||
|             withContext(Dispatchers.IO) { |             withContext(Dispatchers.IO) { | ||||||
|                 applicationContext.contentResolver.openOutputStream(uri)?.use { |                 applicationContext.contentResolver.openOutputStream(uri)?.use { | ||||||
| @@ -80,7 +77,8 @@ class SubscriptionExportWorker( | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun createNotification(title: String): Notification = |     private fun createForegroundInfo(title: String): ForegroundInfo { | ||||||
|  |         val notification = | ||||||
|             NotificationCompat |             NotificationCompat | ||||||
|                 .Builder(applicationContext, NOTIFICATION_CHANNEL_ID) |                 .Builder(applicationContext, NOTIFICATION_CHANNEL_ID) | ||||||
|                 .setSmallIcon(R.drawable.ic_newpipe_triangle_white) |                 .setSmallIcon(R.drawable.ic_newpipe_triangle_white) | ||||||
| @@ -90,8 +88,6 @@ class SubscriptionExportWorker( | |||||||
|                 .setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE) |                 .setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE) | ||||||
|                 .setContentTitle(title) |                 .setContentTitle(title) | ||||||
|                 .build() |                 .build() | ||||||
|  |  | ||||||
|     private fun createForegroundInfo(notification: Notification): ForegroundInfo { |  | ||||||
|         val serviceType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC else 0 |         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) |         return ForegroundInfo(NOTIFICATION_ID, notification, serviceType) | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -1,6 +1,5 @@ | |||||||
| package org.schabi.newpipe.local.subscription.workers | package org.schabi.newpipe.local.subscription.workers | ||||||
|  |  | ||||||
| import android.app.Notification |  | ||||||
| import android.content.Context | import android.content.Context | ||||||
| import android.content.pm.ServiceInfo | import android.content.pm.ServiceInfo | ||||||
| import android.os.Build | import android.os.Build | ||||||
| @@ -33,8 +32,7 @@ class SubscriptionImportWorker( | |||||||
| ) : CoroutineWorker(appContext, params) { | ) : CoroutineWorker(appContext, params) { | ||||||
|     // This is needed for API levels < 31 (Android S). |     // This is needed for API levels < 31 (Android S). | ||||||
|     override suspend fun getForegroundInfo(): ForegroundInfo { |     override suspend fun getForegroundInfo(): ForegroundInfo { | ||||||
|         val title = applicationContext.getString(R.string.import_ongoing) |         return createForegroundInfo(applicationContext.getString(R.string.import_ongoing), null, 0, 0) | ||||||
|         return createForegroundInfo(createNotification(title, null, 0, 0)) |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     override suspend fun doWork(): Result { |     override suspend fun doWork(): Result { | ||||||
| @@ -78,8 +76,7 @@ class SubscriptionImportWorker( | |||||||
|                             ExtractorHelper.getChannelTab(it.serviceId, channelInfo.tabs[0], true).await() |                             ExtractorHelper.getChannelTab(it.serviceId, channelInfo.tabs[0], true).await() | ||||||
|  |  | ||||||
|                         val currentIndex = mutex.withLock { index++ } |                         val currentIndex = mutex.withLock { index++ } | ||||||
|                         val notification = createNotification(title, channelInfo.name, currentIndex, qty) |                         setForeground(createForegroundInfo(title, channelInfo.name, currentIndex, qty)) | ||||||
|                         setForeground(createForegroundInfo(notification)) |  | ||||||
|  |  | ||||||
|                         Pair(channelInfo, listOf(channelTab)) |                         Pair(channelInfo, listOf(channelTab)) | ||||||
|                     } |                     } | ||||||
| @@ -87,7 +84,7 @@ class SubscriptionImportWorker( | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         title = applicationContext.resources.getQuantityString(R.plurals.import_subscriptions, qty, qty) |         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 |         index = 0 | ||||||
|  |  | ||||||
|         val subscriptionManager = SubscriptionManager(applicationContext) |         val subscriptionManager = SubscriptionManager(applicationContext) | ||||||
| @@ -96,7 +93,7 @@ class SubscriptionImportWorker( | |||||||
|                 subscriptionManager.upsertAll(chunk) |                 subscriptionManager.upsertAll(chunk) | ||||||
|             } |             } | ||||||
|             index += chunk.size |             index += chunk.size | ||||||
|             setForeground(createForegroundInfo(createNotification(title, null, index, qty))) |             setForeground(createForegroundInfo(title, null, index, qty)) | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         withContext(Dispatchers.Main) { |         withContext(Dispatchers.Main) { | ||||||
| @@ -108,12 +105,13 @@ class SubscriptionImportWorker( | |||||||
|         return Result.success() |         return Result.success() | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun createNotification( |     private fun createForegroundInfo( | ||||||
|         title: String, |         title: String, | ||||||
|         text: String?, |         text: String?, | ||||||
|         currentProgress: Int, |         currentProgress: Int, | ||||||
|         maxProgress: Int, |         maxProgress: Int, | ||||||
|     ): Notification = |     ): ForegroundInfo { | ||||||
|  |         val notification = | ||||||
|             NotificationCompat |             NotificationCompat | ||||||
|                 .Builder(applicationContext, NOTIFICATION_CHANNEL_ID) |                 .Builder(applicationContext, NOTIFICATION_CHANNEL_ID) | ||||||
|                 .setSmallIcon(R.drawable.ic_newpipe_triangle_white) |                 .setSmallIcon(R.drawable.ic_newpipe_triangle_white) | ||||||
| @@ -137,9 +135,8 @@ class SubscriptionImportWorker( | |||||||
|                         } |                         } | ||||||
|                     } |                     } | ||||||
|                 }.build() |                 }.build() | ||||||
|  |  | ||||||
|     private fun createForegroundInfo(notification: Notification): ForegroundInfo { |  | ||||||
|         val serviceType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC else 0 |         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) |         return ForegroundInfo(NOTIFICATION_ID, notification, serviceType) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user