The Picture-in-Picture (PiP) Jetpack Library offers a streamlined and robust solution for Android app developers to implement PiP functionality, particularly for media playback, video communication, and navigation apps. By providing a unified API, the library helps eliminate boilerplate code, common in-app bugs, and improve the overall quality of the PiP user experience.
The PiP Jetpack library facilitates the existing PiP APIs by addressing several key challenges and inconsistencies across the Android ecosystem:
- OS fragmentation: The library automatically handles differences in PiP API
calls across various Android versions, such as using
enterPictureInPictureModebefore Android 12 andisAutoEnterEnabledafter, so developers don't need to manage version differences. - Incorrect PiP parameters: It provides a unified solution for correctly
setting PiP parameters, for example
setSourceRectHint, to create smooth and high-quality animations during media playback. - Unified PiP state callbacks: It consolidates
onPictureInPictureModeChangedandonPictureInPictureUiStateChangedinto a single, unified callback interface (PictureInPictureDelegate.OnPictureInPictureEventListener) for simplified state and UI management. - Boilerplate code reduction: The library reduces the amount of repetitive,
boilerplate code by offering predefined sets of
RemoteActionsfor common use cases, such as playback controls and video call actions. - Future-proofing: Further PiP features are delivered through the Jetpack library, allowing adopters to access additional functionality with minimal to no effort.
Adopt Jetpack
In order to adopt the Jetpack Library, replace your existing custom PiP implementation with the Jetpack Library APIs. The complexity and cost of adoption will vary based on the app's current implementation.
The following sections describe some of the typical use cases of PiP and the necessary implementation steps:
Navigation
The app informs the library of the navigation's active or inactive state and sets the aspect ratio. The Jetpack library handles the rest.
Key differences:
- No need to differentiate auto-enter and legacy-enter on app side.
- Consolidated callback interfaces.
- New
PictureInPictureParamsbuilder for back compatibility.
Video Call
The app informs the library of the call's active or inactive state and sets the aspect ratio.
Key differences:
- No need to differentiate auto-enter and legacy-enter on app side.
- Consolidated callback interfaces.
- New
PictureInPictureParamsbuilder for back compatibility. - Standardized action icons for video call.
Video Playback
The Jetpack library offers player delegates that you can integrate to manage
PiP enablement or disablement and accurately set the source rectangle hint.
You can also opt into a predefined set of RemoteAction objects, similar to
those used in video call scenarios.
Key features:
- Handles back compatibility, no OS version check is required.
- Playback state synchronization and auto-entry control.
- Continuous geometry tracking using
SourceRectHint.