From ecc1844e3af7c103cdabf68a771403d31b0aa51d Mon Sep 17 00:00:00 2001
From: zaednasr <75589932+zaednasr@users.noreply.github.com>
Date: Sat, 19 Oct 2024 01:22:01 +0200
Subject: [PATCH] foreground info fixes
---
app/src/main/AndroidManifest.xml | 6 ++++++
.../deniscerri/ytdl/ui/downloads/HistoryFragment.kt | 2 +-
.../deniscerri/ytdl/work/CleanUpLeftoverDownloads.kt | 9 +++++++--
.../java/com/deniscerri/ytdl/work/DownloadWorker.kt | 9 +++++++--
.../com/deniscerri/ytdl/work/MoveCacheFilesWorker.kt | 9 +++++++--
.../com/deniscerri/ytdl/work/ObserveSourceWorker.kt | 9 +++++++--
.../deniscerri/ytdl/work/TerminalDownloadWorker.kt | 11 ++++++++---
.../ytdl/work/UpdateMultipleDownloadsFormatsWorker.kt | 10 ++++++++--
8 files changed, 51 insertions(+), 14 deletions(-)
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 93060c51..19d11490 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -23,6 +23,8 @@
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/java/com/deniscerri/ytdl/ui/downloads/HistoryFragment.kt b/app/src/main/java/com/deniscerri/ytdl/ui/downloads/HistoryFragment.kt
index 6a1d013f..533e62d0 100644
--- a/app/src/main/java/com/deniscerri/ytdl/ui/downloads/HistoryFragment.kt
+++ b/app/src/main/java/com/deniscerri/ytdl/ui/downloads/HistoryFragment.kt
@@ -511,7 +511,7 @@ class HistoryFragment : Fragment(), HistoryAdapter.OnItemClickListener{
websiteList = mutableListOf()
for (item in list){
if (item.website == "null" || item.website.isEmpty()) continue
- if (!websiteList.contains(item.website)) websiteList.add(item.website)
+ if (!websiteList.any { it.contentEquals(item.website, true) }) websiteList.add(item.website)
}
}
diff --git a/app/src/main/java/com/deniscerri/ytdl/work/CleanUpLeftoverDownloads.kt b/app/src/main/java/com/deniscerri/ytdl/work/CleanUpLeftoverDownloads.kt
index 709ce891..5834a096 100644
--- a/app/src/main/java/com/deniscerri/ytdl/work/CleanUpLeftoverDownloads.kt
+++ b/app/src/main/java/com/deniscerri/ytdl/work/CleanUpLeftoverDownloads.kt
@@ -1,6 +1,8 @@
package com.deniscerri.ytdl.work
import android.content.Context
+import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
+import android.os.Build
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
@@ -23,8 +25,11 @@ class CleanUpLeftoverDownloads(
val id = System.currentTimeMillis().toInt()
val notification = notificationUtil.createDeletingLeftoverDownloadsNotification()
- val foregroundInfo = ForegroundInfo(id, notification)
- setForegroundAsync(foregroundInfo)
+ if (Build.VERSION.SDK_INT > 33) {
+ setForegroundAsync(ForegroundInfo(id, notification, FOREGROUND_SERVICE_TYPE_SPECIAL_USE))
+ }else{
+ setForegroundAsync(ForegroundInfo(id, notification))
+ }
val dbManager = DBManager.getInstance(context)
val downloadRepo = DownloadRepository(dbManager.downloadDao)
diff --git a/app/src/main/java/com/deniscerri/ytdl/work/DownloadWorker.kt b/app/src/main/java/com/deniscerri/ytdl/work/DownloadWorker.kt
index 32569951..171f1863 100644
--- a/app/src/main/java/com/deniscerri/ytdl/work/DownloadWorker.kt
+++ b/app/src/main/java/com/deniscerri/ytdl/work/DownloadWorker.kt
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
+import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
@@ -105,8 +106,12 @@ class DownloadWorker(
)
val workNotif = notificationUtil.createDefaultWorkerNotification()
- val foregroundInfo = ForegroundInfo(1000000000, workNotif)
- setForegroundAsync(foregroundInfo)
+ val notificationID = 1000000000
+ if (Build.VERSION.SDK_INT > 33) {
+ setForegroundAsync(ForegroundInfo(notificationID, workNotif, FOREGROUND_SERVICE_TYPE_SPECIAL_USE))
+ }else{
+ setForegroundAsync(ForegroundInfo(notificationID, workNotif))
+ }
queuedItems.collectLatest { items ->
if (this@DownloadWorker.isStopped) return@collectLatest
diff --git a/app/src/main/java/com/deniscerri/ytdl/work/MoveCacheFilesWorker.kt b/app/src/main/java/com/deniscerri/ytdl/work/MoveCacheFilesWorker.kt
index afa94097..c14add11 100644
--- a/app/src/main/java/com/deniscerri/ytdl/work/MoveCacheFilesWorker.kt
+++ b/app/src/main/java/com/deniscerri/ytdl/work/MoveCacheFilesWorker.kt
@@ -3,6 +3,7 @@ package com.deniscerri.ytdl.work
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
+import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
import android.os.Build
import android.os.Environment
import android.os.Handler
@@ -39,8 +40,12 @@ class MoveCacheFilesWorker(
val intent = Intent(context, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
val notification = notificationUtil.createMoveCacheFilesNotification(pendingIntent, NotificationUtil.DOWNLOAD_MISC_CHANNEL_ID)
- val foregroundInfo = ForegroundInfo(id, notification)
- setForegroundAsync(foregroundInfo)
+
+ if (Build.VERSION.SDK_INT > 33) {
+ setForegroundAsync(ForegroundInfo(id, notification, FOREGROUND_SERVICE_TYPE_SPECIAL_USE))
+ }else{
+ setForegroundAsync(ForegroundInfo(id, notification))
+ }
var progress = 0
allContent.forEach {
diff --git a/app/src/main/java/com/deniscerri/ytdl/work/ObserveSourceWorker.kt b/app/src/main/java/com/deniscerri/ytdl/work/ObserveSourceWorker.kt
index e22836d1..aaf6c918 100644
--- a/app/src/main/java/com/deniscerri/ytdl/work/ObserveSourceWorker.kt
+++ b/app/src/main/java/com/deniscerri/ytdl/work/ObserveSourceWorker.kt
@@ -1,6 +1,8 @@
package com.deniscerri.ytdl.work
import android.content.Context
+import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
+import android.os.Build
import android.util.Log
import androidx.preference.PreferenceManager
import androidx.work.Constraints
@@ -52,8 +54,11 @@ class ObserveSourceWorker(
val workerID = System.currentTimeMillis().toInt()
val notification = notificationUtil.createObserveSourcesNotification(item.name)
- val foregroundInfo = ForegroundInfo(workerID, notification)
- setForegroundAsync(foregroundInfo)
+ if (Build.VERSION.SDK_INT > 33) {
+ setForegroundAsync(ForegroundInfo(workerID, notification, FOREGROUND_SERVICE_TYPE_SPECIAL_USE))
+ }else{
+ setForegroundAsync(ForegroundInfo(workerID, notification))
+ }
val list = kotlin.runCatching {
ytdlpUtil.getFromYTDL(item.url)
diff --git a/app/src/main/java/com/deniscerri/ytdl/work/TerminalDownloadWorker.kt b/app/src/main/java/com/deniscerri/ytdl/work/TerminalDownloadWorker.kt
index 615b88a9..5f5ffc37 100644
--- a/app/src/main/java/com/deniscerri/ytdl/work/TerminalDownloadWorker.kt
+++ b/app/src/main/java/com/deniscerri/ytdl/work/TerminalDownloadWorker.kt
@@ -3,6 +3,8 @@ package com.deniscerri.ytdl.work
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
+import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
+import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Log
@@ -50,9 +52,12 @@ class TerminalDownloadWorker(
val intent = Intent(context, TerminalActivity::class.java)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
val notification = notificationUtil.createDownloadServiceNotification(pendingIntent, command.take(65), NotificationUtil.DOWNLOAD_TERMINAL_RUNNING_NOTIFICATION_ID)
- val foregroundInfo = ForegroundInfo(itemId, notification)
- setForegroundAsync(foregroundInfo)
-
+ if (Build.VERSION.SDK_INT > 33) {
+ setForegroundAsync(ForegroundInfo(itemId, notification, FOREGROUND_SERVICE_TYPE_SPECIAL_USE))
+ }else{
+ setForegroundAsync(ForegroundInfo(itemId, notification))
+ }
+
val request = YoutubeDLRequest(emptyList())
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
diff --git a/app/src/main/java/com/deniscerri/ytdl/work/UpdateMultipleDownloadsFormatsWorker.kt b/app/src/main/java/com/deniscerri/ytdl/work/UpdateMultipleDownloadsFormatsWorker.kt
index 6e127ad0..0111a848 100644
--- a/app/src/main/java/com/deniscerri/ytdl/work/UpdateMultipleDownloadsFormatsWorker.kt
+++ b/app/src/main/java/com/deniscerri/ytdl/work/UpdateMultipleDownloadsFormatsWorker.kt
@@ -1,6 +1,8 @@
package com.deniscerri.ytdl.work
import android.content.Context
+import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
+import android.os.Build
import androidx.work.ForegroundInfo
import androidx.work.Worker
import androidx.work.WorkerParameters
@@ -28,8 +30,12 @@ class UpdateMultipleDownloadsFormatsWorker(
if (workID == 0) return Result.failure()
val notification = notificationUtil.createFormatsUpdateNotification()
- val foregroundInfo = ForegroundInfo(workID, notification)
- setForegroundAsync(foregroundInfo)
+
+ if (Build.VERSION.SDK_INT > 33) {
+ setForegroundAsync(ForegroundInfo(workID, notification, FOREGROUND_SERVICE_TYPE_SPECIAL_USE))
+ }else{
+ setForegroundAsync(ForegroundInfo(workID, notification))
+ }
var count = 0