Merge pull request #2750 from Isira-Seneviratne/startForegroundService_helper

Use ContextCompat.startForegroundService().
This commit is contained in:
Bnyro 2023-01-18 09:40:02 +01:00 committed by GitHub
commit d002285b85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 26 deletions

View File

@ -1,15 +0,0 @@
package com.github.libretube.compat
import android.content.Context
import android.content.Intent
import android.os.Build
class ServiceCompat(private val context: Context) {
fun startForeground(intent: Intent) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
context.startForegroundService(intent)
} else {
context.startService(intent)
}
}
}

View File

@ -3,23 +3,21 @@ package com.github.libretube.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.github.libretube.compat.ServiceCompat
import androidx.core.content.ContextCompat
import com.github.libretube.services.DownloadService
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == null) return
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == null) return
val serviceIntent = Intent(context, DownloadService::class.java)
serviceIntent.action = intent.action
.setAction(intent.action)
val id = intent.getIntExtra("id", -1)
if (id == -1) return
serviceIntent.putExtra("id", id)
context?.let {
ServiceCompat(it).startForeground(serviceIntent)
}
ContextCompat.startForegroundService(context, serviceIntent)
}
companion object {

View File

@ -3,7 +3,7 @@ package com.github.libretube.util
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import com.github.libretube.compat.ServiceCompat
import androidx.core.content.ContextCompat
import com.github.libretube.constants.IntentData
import com.github.libretube.services.BackgroundMode
@ -33,7 +33,7 @@ object BackgroundHelper {
intent.putExtra(IntentData.keepQueue, keepQueue)
// start the background mode as foreground service
ServiceCompat(context).startForeground(intent)
ContextCompat.startForegroundService(context, intent)
}
/**

View File

@ -3,7 +3,7 @@ package com.github.libretube.util
import android.content.Context
import android.content.Intent
import android.os.Build
import com.github.libretube.compat.ServiceCompat
import androidx.core.content.ContextCompat
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.obj.DownloadItem
@ -66,7 +66,7 @@ object DownloadHelper {
intent.putExtra(IntentData.audioQuality, audioQuality)
intent.putExtra(IntentData.subtitleCode, subtitleCode)
ServiceCompat(context).startForeground(intent)
ContextCompat.startForegroundService(context, intent)
}
fun DownloadItem.getNotificationId(): Int {