PlatformPictureInPicture.android.kt
composeApp/src/androidMain/kotlin/eu/friedcutlet/yokan/services/media/player/PlatformPictureInPicture.android.kt
1package eu.friedcutlet.yokan.services.media.player23import android.app.Activity4import android.app.PendingIntent5import android.app.PictureInPictureParams6import android.app.RemoteAction7import android.content.BroadcastReceiver8import android.content.Context9import android.content.Intent10import android.content.IntentFilter11import android.graphics.drawable.Icon12import android.os.Build13import android.util.Rational14import kotlinx.coroutines.flow.Flow15import kotlinx.coroutines.flow.MutableSharedFlow16import kotlinx.coroutines.flow.MutableStateFlow17import kotlinx.coroutines.flow.StateFlow18import kotlinx.coroutines.flow.asStateFlow19import kotlinx.coroutines.flow.update2021object AndroidPictureInPictureController {22 private const val ActionIntent = "eu.friedcutlet.yokan.PICTURE_IN_PICTURE_ACTION"23 private const val ActionExtra = "action"24 private var activity: Activity? = null25 private var animePlayerActive = false26 private var playbackEligible = false27 private var playbackAdmitted: () -> Boolean = { false }28 private var playing = false29 private var replaceWithPrevious = false30 private val _isActive = MutableStateFlow(false)31 val isActive = _isActive.asStateFlow()32 internal val actions = MutableSharedFlow<PictureInPictureAction>(extraBufferCapacity = 8)33 private val receiver = object : BroadcastReceiver() {34 override fun onReceive(context: Context?, intent: Intent?) {35 val action = PictureInPictureAction.entries.getOrNull(intent?.getIntExtra(ActionExtra, -1) ?: -1) ?: return36 actions.tryEmit(action)37 }38 }3940 fun attach(activity: Activity) {41 if (this.activity === activity) return42 this.activity?.let(::unregister)43 this.activity = activity44 animePlayerActive = false45 playbackEligible = false46 playbackAdmitted = { false }47 _isActive.update { false }48 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {49 activity.registerReceiver(receiver, IntentFilter(ActionIntent), Context.RECEIVER_NOT_EXPORTED)50 } else {51 activity.registerReceiver(receiver, IntentFilter(ActionIntent))52 }53 }54 fun detach(activity: Activity) {55 if (this.activity !== activity) return56 unregister(activity)57 this.activity = null58 animePlayerActive = false59 playbackEligible = false60 playbackAdmitted = { false }61 _isActive.update { false }62 }63 private fun unregister(activity: Activity) {64 runCatching { activity.unregisterReceiver(receiver) }65 }66 fun onUserLeaveHint(activity: Activity) {67 if (this.activity === activity) enterIfEligible()68 }6970 fun setState(active: Boolean, eligible: Boolean, admitted: () -> Boolean) {71 animePlayerActive = active72 playbackEligible = eligible73 playbackAdmitted = admitted74 }7576 fun setActions(playing: Boolean, replaceWithPrevious: Boolean) {77 if (this.playing == playing && this.replaceWithPrevious == replaceWithPrevious) return78 this.playing = playing79 this.replaceWithPrevious = replaceWithPrevious80 val host = activity ?: return81 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && host.isInPictureInPictureMode) {82 runCatching { host.setPictureInPictureParams(params(host)) }83 }84 }8586 fun onPictureInPictureModeChanged(activity: Activity, active: Boolean) {87 if (this.activity !== activity) return88 _isActive.update { active }89 }9091 fun enterIfEligible(): Boolean {92 val host = activity ?: return false93 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O ||94 !animePlayerActive || !playbackEligible || !playbackAdmitted() || host.isFinishing || host.isInPictureInPictureMode95 ) return false96 return host.enterPictureInPictureMode(params(host))97 }9899 private fun params(host: Activity): PictureInPictureParams {100 val first = if (replaceWithPrevious) {101 action(host, PictureInPictureAction.Previous, android.R.drawable.ic_media_previous, "Previous episode")102 } else {103 action(host, PictureInPictureAction.SkipForward, android.R.drawable.ic_media_ff, "+10 s")104 }105 val toggle = if (playing) {106 action(host, PictureInPictureAction.Pause, android.R.drawable.ic_media_pause, "Pause")107 } else {108 action(host, PictureInPictureAction.Play, android.R.drawable.ic_media_play, "Play")109 }110 val next = action(host, PictureInPictureAction.Next, android.R.drawable.ic_media_next, "Next episode")111 return PictureInPictureParams.Builder()112 .setAspectRatio(Rational(16, 9))113 .setActions(listOf(first, toggle, next))114 .build()115 }116117 private fun action(host: Activity, action: PictureInPictureAction, icon: Int, title: String): RemoteAction {118 val intent = Intent(ActionIntent).setPackage(host.packageName).putExtra(ActionExtra, action.ordinal)119 val pending = PendingIntent.getBroadcast(120 host, action.ordinal, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,121 )122 return RemoteAction(Icon.createWithResource(host, icon), title, title, pending)123 }124}125126internal actual val pictureInPicturePlatform: PictureInPicturePlatform = object : PictureInPicturePlatform {127 override val isSupported: Boolean get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O128 override val active: StateFlow<Boolean> = AndroidPictureInPictureController.isActive129 override val actions: Flow<PictureInPictureAction> = AndroidPictureInPictureController.actions130131 override fun request(controller: MediaPlayerController?): Boolean = AndroidPictureInPictureController.enterIfEligible()132 override fun setPlaybackState(active: Boolean, eligible: Boolean, admitted: () -> Boolean) =133 AndroidPictureInPictureController.setState(active, eligible, admitted)134 override fun updateActions(playing: Boolean, replaceWithPrevious: Boolean) =135 AndroidPictureInPictureController.setActions(playing, replaceWithPrevious)136}137