Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce onPictureInPictureModeChanged to FlutterPlayerView (PiP #2) #86

Merged
merged 10 commits into from
Dec 22, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.bitmovin.player.flutter
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.res.Configuration
import android.view.View
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
Expand Down Expand Up @@ -37,7 +38,7 @@ class FlutterPlayerView(
messenger,
)

private val playerView: PlayerView = PlayerView(context, player = null)
private val playerView = PlayerView(context, player = null)

private val activity =
context.getActivity()
Expand Down Expand Up @@ -79,6 +80,13 @@ class FlutterPlayerView(
activityLifecycle.addObserver(activityLifecycleObserver)
}

private fun onPictureInPictureModeChanged(
isInPictureInPictureMode: Boolean,
newConfig: Configuration,
) {
// TODO: Handle picture in picture mode changed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this TODO will be part of a future PR, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes 🙂

}

override fun onMethodCall(
call: MethodCall,
result: MethodChannel.Result,
Expand Down Expand Up @@ -111,6 +119,30 @@ class FlutterPlayerView(
sink = null
}

private fun PlayerView.setOnPictureInPictureModeChangedCallback(callback: (Boolean, Configuration) -> Unit) {
var isInPictureInPictureMode = activity.isInPictureInPictureMode
// Listening to PiP changes usually happens by overriding `onPictureInPictureModeChanged` in the activity.
// This is not doable in an SDK context since the library consumer controls the activity.
// It's also possible to set a `addOnPictureInPictureModeChangedListener` to the activity that
// implements `OnPictureInPictureModeChangedProvider`, unfortunately `FlutterActvity` doesn't implement it.
// To work around this limitation we listen to configuration changes and check if the PiP mode changed.
// Since Flutter's `PlatformView` isn't actually an Android `View`,
// we unfortunately cannot just override the `onConfigurationChanged` function.
// Instead, we add a blank `View` to the `PlayerView` and override its `onConfigurationChanged` function.
// The alternative would be registering a `ComponentCallbacks` object to the activity,
// which doesn't work for some Android versions (tested on Android 10 with FlutterFragmentActivity).
addView(
object : View(context) {
override fun onConfigurationChanged(newConfig: Configuration) {
if (isInPictureInPictureMode != activity.isInPictureInPictureMode) {
isInPictureInPictureMode = activity.isInPictureInPictureMode
callback(isInPictureInPictureMode, newConfig)
}
}
},
)
}

private fun Context.getActivity(): Activity? =
when (this) {
is Activity -> this
Expand Down