mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-29 08:20:32 +05:30
Merge pull request #2750 from Isira-Seneviratne/startForegroundService_helper
Use ContextCompat.startForegroundService().
This commit is contained in:
commit
d002285b85
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,23 +3,21 @@ package com.github.libretube.receivers
|
|||||||
import android.content.BroadcastReceiver
|
import android.content.BroadcastReceiver
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import com.github.libretube.compat.ServiceCompat
|
import androidx.core.content.ContextCompat
|
||||||
import com.github.libretube.services.DownloadService
|
import com.github.libretube.services.DownloadService
|
||||||
|
|
||||||
class NotificationReceiver : BroadcastReceiver() {
|
class NotificationReceiver : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
if (intent?.action == null) return
|
if (intent.action == null) return
|
||||||
|
|
||||||
val serviceIntent = Intent(context, DownloadService::class.java)
|
val serviceIntent = Intent(context, DownloadService::class.java)
|
||||||
serviceIntent.action = intent.action
|
.setAction(intent.action)
|
||||||
|
|
||||||
val id = intent.getIntExtra("id", -1)
|
val id = intent.getIntExtra("id", -1)
|
||||||
if (id == -1) return
|
if (id == -1) return
|
||||||
serviceIntent.putExtra("id", id)
|
serviceIntent.putExtra("id", id)
|
||||||
|
|
||||||
context?.let {
|
ContextCompat.startForegroundService(context, serviceIntent)
|
||||||
ServiceCompat(it).startForeground(serviceIntent)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -3,7 +3,7 @@ package com.github.libretube.util
|
|||||||
import android.app.ActivityManager
|
import android.app.ActivityManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
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.constants.IntentData
|
||||||
import com.github.libretube.services.BackgroundMode
|
import com.github.libretube.services.BackgroundMode
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ object BackgroundHelper {
|
|||||||
intent.putExtra(IntentData.keepQueue, keepQueue)
|
intent.putExtra(IntentData.keepQueue, keepQueue)
|
||||||
|
|
||||||
// start the background mode as foreground service
|
// start the background mode as foreground service
|
||||||
ServiceCompat(context).startForeground(intent)
|
ContextCompat.startForegroundService(context, intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,7 +3,7 @@ package com.github.libretube.util
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
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.IntentData
|
||||||
import com.github.libretube.constants.PreferenceKeys
|
import com.github.libretube.constants.PreferenceKeys
|
||||||
import com.github.libretube.db.obj.DownloadItem
|
import com.github.libretube.db.obj.DownloadItem
|
||||||
@ -66,7 +66,7 @@ object DownloadHelper {
|
|||||||
intent.putExtra(IntentData.audioQuality, audioQuality)
|
intent.putExtra(IntentData.audioQuality, audioQuality)
|
||||||
intent.putExtra(IntentData.subtitleCode, subtitleCode)
|
intent.putExtra(IntentData.subtitleCode, subtitleCode)
|
||||||
|
|
||||||
ServiceCompat(context).startForeground(intent)
|
ContextCompat.startForegroundService(context, intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun DownloadItem.getNotificationId(): Int {
|
fun DownloadItem.getNotificationId(): Int {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user