Skip to content

Commit a21b6d5

Browse files
authored
Use startForegroundService() for the MediaService (#95)
1 parent 5234c25 commit a21b6d5

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

readium/navigator/src/main/java/org/readium/r2/navigator/media/MediaService.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import android.app.PendingIntent
1111
import android.content.Context
1212
import android.content.Intent
1313
import android.graphics.Bitmap
14+
import android.os.Build
1415
import android.os.Bundle
1516
import android.os.Process
1617
import android.os.ResultReceiver
@@ -303,7 +304,7 @@ open class MediaService : MediaBrowserServiceCompat(), CoroutineScope by MainSco
303304
val currentNavigator: StateFlow<MediaSessionNavigator?> get() = navigator
304305

305306
fun getNavigator(context: Context, publication: Publication, publicationId: PublicationId, initialLocator: Locator?): MediaSessionNavigator {
306-
context.startService(Intent(context, serviceClass))
307+
context.startForegroundServiceCompat(Intent(context, serviceClass))
307308

308309
currentNavigator.value
309310
?.takeIf { it.publicationId == publicationId }
@@ -329,6 +330,14 @@ open class MediaService : MediaBrowserServiceCompat(), CoroutineScope by MainSco
329330

330331
}
331332

333+
private fun Context.startForegroundServiceCompat(intent: Intent) {
334+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
335+
startForegroundService(intent)
336+
} else {
337+
startService(intent)
338+
}
339+
}
340+
332341
// FIXME: Move to r2-shared
333342
internal fun <T> createIfNull(property: KMutableProperty0<T?>, owner: Any, factory: () -> T): T =
334343
property.get() ?: synchronized(owner) {

readium/shared/src/main/java/org/readium/r2/shared/FuelPromiseExtension.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import nl.komponents.kovenant.Promise
1616
import nl.komponents.kovenant.deferred
1717
import nl.komponents.kovenant.task
1818

19+
@Deprecated("Dependency to Fuel and kovenant will eventually be removed from the Readium Toolkit")
1920
fun Request.promise(): Promise<Triple<Request, Response, ByteArray>, Exception> {
2021
val deferred = deferred<Triple<Request, Response, ByteArray>, Exception>()
2122
task { response() } success {

0 commit comments

Comments
 (0)