Skip to content

Commit c1fd233

Browse files
authored
Merge pull request #189 from alabiaga/main
Update PiP sample to use new callback for toggling of UI elements
2 parents d380a2f + 9e7aaea commit c1fd233

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

samples/user-interface/picture-in-picture/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ plugins {
2323
android {
2424
namespace = "com.example.android.pip"
2525
viewBinding.isEnabled = true
26+
compileSdk = 35
2627
}
2728

2829
dependencies {

samples/user-interface/picture-in-picture/src/main/java/com/example/android/pip/PiPMovieActivity.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.example.android.pip
1818

1919
import android.app.PictureInPictureParams
20+
import android.app.PictureInPictureUiState
2021
import android.content.res.Configuration
2122
import android.graphics.Rect
2223
import android.os.Build
@@ -195,6 +196,15 @@ class PiPMovieActivity : AppCompatActivity() {
195196
}
196197
}
197198

199+
@RequiresApi(35)
200+
override fun onPictureInPictureUiStateChanged(pipState: PictureInPictureUiState) {
201+
super.onPictureInPictureUiStateChanged(pipState)
202+
if (pipState.isTransitioningToPip) {
203+
binding.movie.hideControls()
204+
}
205+
}
206+
207+
198208
private fun updatePictureInPictureParams(): PictureInPictureParams {
199209
// Calculate the aspect ratio of the PiP screen.
200210
val aspectRatio = Rational(binding.movie.width, binding.movie.height)

samples/user-interface/picture-in-picture/src/main/java/com/example/android/pip/PiPSampleActivity.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package com.example.android.pip
1818

1919
import android.app.PendingIntent
2020
import android.app.PictureInPictureParams
21+
import android.app.PictureInPictureUiState
2122
import android.app.RemoteAction
2223
import android.content.BroadcastReceiver
2324
import android.content.Context
@@ -63,7 +64,7 @@ private const val REQUEST_START_OR_PAUSE = 4
6364
description = "Basic usage of Picture-in-Picture mode showcasing a stopwatch",
6465
documentation = "https://developer.android.com/develop/ui/views/picture-in-picture",
6566
)
66-
@RequiresApi(Build.VERSION_CODES.O)
67+
@RequiresApi(26)
6768
class PiPSampleActivity : AppCompatActivity() {
6869

6970
private val viewModel: PiPViewModel by viewModels()
@@ -128,14 +129,21 @@ class PiPSampleActivity : AppCompatActivity() {
128129
newConfig: Configuration,
129130
) {
130131
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
131-
if (isInPictureInPictureMode) {
132-
// Hide in-app buttons. They cannot be interacted in the picture-in-picture mode, and
133-
// their features are provided as the action icons.
134-
binding.clear.visibility = View.GONE
135-
binding.startOrPause.visibility = View.GONE
136-
} else {
137-
binding.clear.visibility = View.VISIBLE
138-
binding.startOrPause.visibility = View.VISIBLE
132+
// Toggle visibility of in-app buttons. They cannot be interacted in the picture-in-picture
133+
// mode, and their features are provided as the action icons.
134+
toggleControls(if (isInPictureInPictureMode) View.GONE else View.VISIBLE)
135+
}
136+
137+
private fun toggleControls(view: Int) {
138+
binding.clear.visibility = view
139+
binding.startOrPause.visibility = view
140+
}
141+
142+
@RequiresApi(35)
143+
override fun onPictureInPictureUiStateChanged(pipState: PictureInPictureUiState) {
144+
super.onPictureInPictureUiStateChanged(pipState)
145+
if (pipState.isTransitioningToPip) {
146+
toggleControls(View.GONE)
139147
}
140148
}
141149

0 commit comments

Comments
 (0)