-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Picture in picture support for Android (#614)
* Picture in picture support for Android * tweak * renamed pip method * vale fix
- Loading branch information
Showing
15 changed files
with
299 additions
and
2 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
docusaurus/docs/Flutter/05-advanced/04-picture-in-picture.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
id: picture_in_picture | ||
sidebar_position: 4 | ||
title: Picture in Picture (PiP) | ||
--- | ||
|
||
Picture in picture (PIP) keeps the call running and visible while you navigate to other apps. | ||
|
||
:::info | ||
At the moment Picture in Picture is only supported on Android. | ||
::: | ||
|
||
### Enable Picture-in-Picture | ||
You can enable Picture in Picture by setting the `enablePictureInPicture` property to `true` in the `StreamCallContainer` or `StreamCallContent` widget. | ||
|
||
```dart | ||
StreamCallContainer( | ||
call: widget.call, | ||
enablePictureInPicture: true, | ||
) | ||
``` | ||
|
||
You can customize the widget rendered while app is in Picture-in-Picture mode by providing `callPictureInPictureBuilder` to `StreamCallContent`. | ||
|
||
```dart | ||
StreamCallContainer( | ||
call: widget.call, | ||
callContentBuilder: ( | ||
BuildContext context, | ||
Call call, | ||
CallState callState, | ||
) { | ||
return StreamCallContent( | ||
call: call, | ||
callState: callState, | ||
enablePictureInPicture: true, | ||
callPictureInPictureBuilder: (context, call, callState) => // YOUR CUSTOM WIDGET | ||
}) | ||
``` | ||
|
||
### Android Configuration | ||
To enable Picture in Picture on Android, you need to add the following configuration to your `AndroidManifest.xml` file. | ||
|
||
```xml | ||
<activity android:name="VideoActivity" | ||
android:supportsPictureInPicture="true" | ||
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation" | ||
.. | ||
/> | ||
``` | ||
|
||
Then you need to add this code to your `MainActivity` class. It will enter Picture in Picture mode when the user leaves the app but only if the call is active. | ||
|
||
```kotlin | ||
import io.flutter.embedding.android.FlutterActivity | ||
import io.getstream.video.flutter.stream_video_flutter.service.PictureInPictureHelper | ||
|
||
class MainActivity: FlutterActivity() { | ||
override fun onUserLeaveHint() { | ||
super.onUserLeaveHint() | ||
PictureInPictureHelper.enterPictureInPictureIfInCall(this) | ||
} | ||
} | ||
``` | ||
|
||
Done. Now after leaving the app, you'll see that the call will be still alive in the background like the one below: | ||
|
||
![Picture in Picture example](../assets/advanced_assets/pip_example.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
dogfooding/android/app/src/main/kotlin/io/getstream/video/flutter/dogfooding/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
package io.getstream.video.flutter.dogfooding | ||
|
||
import io.flutter.embedding.android.FlutterActivity | ||
import io.getstream.video.flutter.stream_video_flutter.service.PictureInPictureHelper | ||
|
||
class MainActivity: FlutterActivity() { | ||
override fun onUserLeaveHint() { | ||
super.onUserLeaveHint() | ||
PictureInPictureHelper.enterPictureInPictureIfInCall(this) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
.../kotlin/io/getstream/video/flutter/stream_video_flutter/service/PictureInPictureHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.getstream.video.flutter.stream_video_flutter.service | ||
|
||
import android.app.Activity | ||
import android.app.PictureInPictureParams | ||
import android.content.pm.ActivityInfo | ||
import android.content.pm.PackageManager | ||
import android.os.Build | ||
import android.util.Rational | ||
import io.getstream.video.flutter.stream_video_flutter.service.utils.getBoolean | ||
|
||
class PictureInPictureHelper { | ||
companion object { | ||
const val PIP_ENABLED_PREF_KEY = "pip_enabled" | ||
|
||
fun disablePictureInPicture(activity: Activity) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
val params = PictureInPictureParams.Builder() | ||
params.setAutoEnterEnabled(false) | ||
activity.setPictureInPictureParams(params.build()) | ||
} | ||
} | ||
|
||
fun enterPictureInPictureIfInCall(activity: Activity) { | ||
val pipEnabled = getBoolean(activity, PIP_ENABLED_PREF_KEY) | ||
if (!pipEnabled) return | ||
|
||
if (activity.packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
val currentOrientation = activity.resources.configuration.orientation | ||
|
||
val aspect = | ||
if (currentOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { | ||
Rational(9, 16) | ||
} else { | ||
Rational(16, 9) | ||
} | ||
|
||
val params = PictureInPictureParams.Builder() | ||
params.setAspectRatio(aspect).apply { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
setAutoEnterEnabled(true) | ||
} | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
setTitle("Video Player") | ||
setSeamlessResizeEnabled(true) | ||
} | ||
} | ||
|
||
activity.enterPictureInPictureMode(params.build()) | ||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
activity.enterPictureInPictureMode() | ||
} | ||
} | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...n/io/getstream/video/flutter/stream_video_flutter/service/utils/SharedPreferencesUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.getstream.video.flutter.stream_video_flutter.service.utils | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
|
||
private const val PREFERENCES_FILE_NAME = "stream_video_flutter" | ||
private var prefs: SharedPreferences? = null | ||
private var editor: SharedPreferences.Editor? = null | ||
|
||
private fun initInstance(context: Context) { | ||
prefs = context.getSharedPreferences(PREFERENCES_FILE_NAME, Context.MODE_PRIVATE) | ||
editor = prefs?.edit() | ||
} | ||
|
||
fun putBoolean(context: Context?, key: String, value: Boolean) { | ||
if (context == null) return | ||
initInstance(context) | ||
editor?.putBoolean(key, value) | ||
editor?.commit() | ||
} | ||
|
||
fun getBoolean(context: Context?, key: String, defaultValue: Boolean = false): Boolean { | ||
if (context == null) return defaultValue; | ||
initInstance(context) | ||
return prefs?.getBoolean(key, defaultValue) ?: defaultValue | ||
} | ||
|
||
fun remove(context: Context?, key: String) { | ||
if (context == null) return | ||
initInstance(context) | ||
editor?.remove(key) | ||
editor?.commit() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.