Skip to content

Commit

Permalink
Rename bg manager more generic and run ktlint
Browse files Browse the repository at this point in the history
  • Loading branch information
123mpozzi committed Oct 23, 2024
1 parent 96eafe9 commit fca2dda
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bitmovin.player.reactnative


import android.content.ComponentName
import android.content.Context
import android.content.Intent
Expand All @@ -15,16 +14,14 @@ import com.facebook.react.bridge.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

// TODO: rename BackgroundPlaybackConnectionManager as both are form of bg playback
// TODO: mediaSessionPlayback<whatever> to mediaSession<whatever>
class MediaSessionConnectionManager(val context: ReactApplicationContext) {
class BackgroundPlaybackManager(val context: ReactApplicationContext) {
private var isServiceStarted = false
private lateinit var playerId: NativeId

private val _serviceBinder = MutableStateFlow<PlayerServiceBinder?>(null)
val serviceBinder = _serviceBinder.asStateFlow()

inner class BackgroundPlaybackServiceConnection: ServiceConnection {
inner class BackgroundPlaybackServiceConnection : ServiceConnection {
override fun onServiceConnected(className: ComponentName, service: IBinder) {
val binder = service as PlayerServiceBinder
_serviceBinder.value = binder
Expand All @@ -37,10 +34,13 @@ class MediaSessionConnectionManager(val context: ReactApplicationContext) {
}

fun setupBackgroundPlayback(playerId: NativeId, playerModule: PlayerModule) {
this@MediaSessionConnectionManager.playerId = playerId
val serviceClass = if (playerModule.isMediaSessionPlaybackEnabled)
MediaSessionPlaybackService::class.java else
this@BackgroundPlaybackManager.playerId = playerId
val serviceClass = if (playerModule.isMediaSessionPlaybackEnabled) {
MediaSessionPlaybackService::class.java
} else {
BackgroundPlaybackService::class.java
}
// val serviceClass = MediaSessionPlaybackService::class.java
val intent = Intent(context, serviceClass)
intent.action = Intent.ACTION_MEDIA_BUTTON
val connection: ServiceConnection = BackgroundPlaybackServiceConnection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.bitmovin.player.reactnative.converter.toJson
import com.bitmovin.player.reactnative.converter.toLockScreenControlConfig
import com.bitmovin.player.reactnative.converter.toPlayerConfig
import com.bitmovin.player.reactnative.extensions.mapToReactArray
import com.bitmovin.player.reactnative.extensions.playerModule
import com.facebook.react.bridge.*
import com.facebook.react.module.annotations.ReactModule
import java.security.InvalidParameterException
Expand All @@ -27,7 +26,7 @@ class PlayerModule(context: ReactApplicationContext) : BitmovinBaseModule(contex
*/
private val players: Registry<Player> = mutableMapOf()

var backgroundPlaybackConnectionManager: MediaSessionConnectionManager? = null
var backgroundPlaybackConnectionManager: BackgroundPlaybackManager? = null
var isMediaSessionPlaybackEnabled: Boolean = false
var isBackgroundPlaybackEnabled: Boolean = false

Expand Down Expand Up @@ -82,9 +81,11 @@ class PlayerModule(context: ReactApplicationContext) : BitmovinBaseModule(contex
val defaultMetadata = analyticsConfigJson?.getMap("defaultMetadata")?.toAnalyticsDefaultMetadata()
isMediaSessionPlaybackEnabled = playerConfigJson?.getMap("lockScreenControlConfig")
?.toLockScreenControlConfig()?.isEnabled ?: false
isBackgroundPlaybackEnabled = if (isMediaSessionPlaybackEnabled)
isMediaSessionPlaybackEnabled else
isBackgroundPlaybackEnabled = if (isMediaSessionPlaybackEnabled) {
isMediaSessionPlaybackEnabled
} else {
playerConfigJson?.getMap("playbackConfig")?.getBoolean("isBackgroundPlaybackEnabled") ?: false
}

val networkConfig = networkNativeId?.let { networkModule.getConfig(it) }
if (networkConfig != null) {
Expand All @@ -103,7 +104,7 @@ class PlayerModule(context: ReactApplicationContext) : BitmovinBaseModule(contex
}

if (isBackgroundPlaybackEnabled) {
backgroundPlaybackConnectionManager = MediaSessionConnectionManager(context)
backgroundPlaybackConnectionManager = BackgroundPlaybackManager(context)
promise.unit.resolveOnUiThread {
backgroundPlaybackConnectionManager?.setupBackgroundPlayback(nativeId, this@PlayerModule)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ fun String.toMediaType(): MediaType? = when (this) {
}

data class LockScreenControlConfig(
var isEnabled: Boolean = false
var isEnabled: Boolean = false,
)

fun ReadableMap.toLockScreenControlConfig(): LockScreenControlConfig = LockScreenControlConfig().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MediaSessionPlaybackService : MediaSessionService() {
}

fun connectSession() = mediaSession?.let { addSession(it) }
fun disconnectSession() = mediaSession?.let{
fun disconnectSession() = mediaSession?.let {
removeSession(it)
it.release()
}
Expand Down

0 comments on commit fca2dda

Please sign in to comment.