-
Notifications
You must be signed in to change notification settings - Fork 174
/
PictureInPictureSnippets.kt
406 lines (368 loc) · 13.7 KB
/
PictureInPictureSnippets.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.compose.snippets.pictureinpicture
import android.app.PictureInPictureParams
import android.app.RemoteAction
import android.content.BroadcastReceiver
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.util.Log
import android.util.Rational
import androidx.activity.ComponentActivity
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.toAndroidRectF
import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
import androidx.core.app.PictureInPictureModeChangedInfo
import androidx.core.content.ContextCompat
import androidx.core.graphics.toRect
import androidx.core.util.Consumer
import androidx.media3.common.Player
import androidx.media3.common.VideoSize
import androidx.media3.exoplayer.ExoPlayer
var shouldEnterPipMode by mutableStateOf(false)
private const val PIP_TAG = "PiP info"
// [START android_compose_pip_broadcast_receiver_constants]
// Constant for broadcast receiver
const val ACTION_BROADCAST_CONTROL = "broadcast_control"
// Intent extras for broadcast controls from Picture-in-Picture mode.
const val EXTRA_CONTROL_TYPE = "control_type"
const val EXTRA_CONTROL_PLAY = 1
const val EXTRA_CONTROL_PAUSE = 2
// [END android_compose_pip_broadcast_receiver_constants]
@Composable
fun PiPBuilderSetAutoEnterEnabled(
modifier: Modifier = Modifier
) {
val context = LocalContext.current
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// [START android_compose_pip_builder_auto_enter]
val pipModifier = modifier.onGloballyPositioned { layoutCoordinates ->
val builder = PictureInPictureParams.Builder()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
builder.setAutoEnterEnabled(true)
}
context.findActivity().setPictureInPictureParams(builder.build())
}
VideoPlayer(pipModifier)
// [END android_compose_pip_builder_auto_enter]
} else {
Log.i(PIP_TAG, "API does not support PiP")
}
}
// [START android_compose_pip_find_activity]
internal fun Context.findActivity(): ComponentActivity {
var context = this
while (context is ContextWrapper) {
if (context is ComponentActivity) return context
context = context.baseContext
}
throw IllegalStateException("Picture in picture should be called in the context of an Activity")
}
// [END android_compose_pip_find_activity]
@Composable
fun EnterPiPThroughButton() {
// [START android_compose_pip_button_click]
val context = LocalContext.current
Button(onClick = {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.findActivity().enterPictureInPictureMode(
PictureInPictureParams.Builder().build()
)
} else {
Log.i(PIP_TAG, "API does not support PiP")
}
}) {
Text(text = "Enter PiP mode!")
}
// [END android_compose_pip_button_click]
}
// [START android_compose_pip_is_in_pip_mode]
@Composable
fun rememberIsInPipMode(): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val activity = LocalContext.current.findActivity()
var pipMode by remember { mutableStateOf(activity.isInPictureInPictureMode) }
DisposableEffect(activity) {
val observer = Consumer<PictureInPictureModeChangedInfo> { info ->
pipMode = info.isInPictureInPictureMode
}
activity.addOnPictureInPictureModeChangedListener(
observer
)
onDispose { activity.removeOnPictureInPictureModeChangedListener(observer) }
}
return pipMode
} else {
return false
}
}
// [END android_compose_pip_is_in_pip_mode]
@Composable
fun VideoPlayer() {
}
@Composable
fun VideoPlayer(modifier: Modifier) {
}
@Composable
fun ToggleUIBasedOnPiP(
modifier: Modifier = Modifier,
) {
// [START android_compose_pip_ui_toggle]
val inPipMode = rememberIsInPipMode()
Column(modifier = modifier) {
// This text will only show up when the app is not in PiP mode
if (!inPipMode) {
Text(
text = "Picture in Picture",
)
}
VideoPlayer()
}
// [END android_compose_pip_ui_toggle]
}
fun initializePlayer(context: Context) {
val player = ExoPlayer.Builder(context.applicationContext)
.build().apply {}
// [START android_compose_pip_toggle_pip_on_if_video_is_playing]
player.addListener(object : Player.Listener {
override fun onIsPlayingChanged(isPlaying: Boolean) {
shouldEnterPipMode = isPlaying
}
})
// [END android_compose_pip_toggle_pip_on_if_video_is_playing]
}
// [START android_compose_pip_release_player]
fun releasePlayer() {
shouldEnterPipMode = false
}
// [END android_compose_pip_release_player]
@Composable
fun PiPBuilderSetAutoEnterEnabledUsingState(
shouldEnterPipMode: Boolean,
modifier: Modifier = Modifier,
) {
val context = LocalContext.current
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// [START android_compose_pip_post_12_should_enter_pip]
val pipModifier = modifier.onGloballyPositioned { layoutCoordinates ->
val builder = PictureInPictureParams.Builder()
// Add autoEnterEnabled for versions S and up
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
builder.setAutoEnterEnabled(shouldEnterPipMode)
}
context.findActivity().setPictureInPictureParams(builder.build())
}
VideoPlayer(pipModifier)
// [END android_compose_pip_post_12_should_enter_pip]
} else {
Log.i(PIP_TAG, "API does not support PiP")
}
}
@Composable
fun PiPBuilderSetSourceRect(
shouldEnterPipMode: Boolean,
modifier: Modifier = Modifier,
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// [START android_compose_pip_set_source_rect]
val context = LocalContext.current
val pipModifier = modifier.onGloballyPositioned { layoutCoordinates ->
val builder = PictureInPictureParams.Builder()
if (shouldEnterPipMode) {
val sourceRect = layoutCoordinates.boundsInWindow().toAndroidRectF().toRect()
builder.setSourceRectHint(sourceRect)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
builder.setAutoEnterEnabled(shouldEnterPipMode)
}
context.findActivity().setPictureInPictureParams(builder.build())
}
VideoPlayer(pipModifier)
// [END android_compose_pip_set_source_rect]
} else {
Log.i(PIP_TAG, "API does not support PiP")
}
}
@Composable
fun PiPBuilderSetAspectRatio(
player: Player?,
shouldEnterPipMode: Boolean,
modifier: Modifier = Modifier,
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// [START android_compose_pip_set_aspect_ratio]
val context = LocalContext.current
val pipModifier = modifier.onGloballyPositioned { layoutCoordinates ->
val builder = PictureInPictureParams.Builder()
if (shouldEnterPipMode && player != null && player.videoSize != VideoSize.UNKNOWN) {
val sourceRect = layoutCoordinates.boundsInWindow().toAndroidRectF().toRect()
builder.setSourceRectHint(sourceRect)
builder.setAspectRatio(
Rational(player.videoSize.width, player.videoSize.height)
)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
builder.setAutoEnterEnabled(shouldEnterPipMode)
}
context.findActivity().setPictureInPictureParams(builder.build())
}
VideoPlayer(pipModifier)
// [END android_compose_pip_set_aspect_ratio]
} else {
Log.i(PIP_TAG, "API does not support PiP")
}
}
// [START android_compose_pip_broadcast_receiver]
@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun PlayerBroadcastReceiver(player: Player?) {
val isInPipMode = rememberIsInPipMode()
if (!isInPipMode || player == null) {
// Broadcast receiver is only used if app is in PiP mode and player is non null
return
}
val context = LocalContext.current
DisposableEffect(player) {
val broadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if ((intent == null) || (intent.action != ACTION_BROADCAST_CONTROL)) {
return
}
when (intent.getIntExtra(EXTRA_CONTROL_TYPE, 0)) {
EXTRA_CONTROL_PAUSE -> player.pause()
EXTRA_CONTROL_PLAY -> player.play()
}
}
}
ContextCompat.registerReceiver(
context,
broadcastReceiver,
IntentFilter(ACTION_BROADCAST_CONTROL),
ContextCompat.RECEIVER_NOT_EXPORTED
)
onDispose {
context.unregisterReceiver(broadcastReceiver)
}
}
}
// [END android_compose_pip_broadcast_receiver]
@RequiresApi(Build.VERSION_CODES.O)
fun listOfRemoteActions(): List<RemoteAction> {
return listOf()
}
@Composable
fun PiPBuilderAddRemoteActions(
player: Player?,
shouldEnterPipMode: Boolean,
modifier: Modifier = Modifier,
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// [START android_compose_pip_add_remote_actions]
val context = LocalContext.current
val pipModifier = modifier.onGloballyPositioned { layoutCoordinates ->
val builder = PictureInPictureParams.Builder()
builder.setActions(
listOfRemoteActions()
)
if (shouldEnterPipMode && player != null && player.videoSize != VideoSize.UNKNOWN) {
val sourceRect = layoutCoordinates.boundsInWindow().toAndroidRectF().toRect()
builder.setSourceRectHint(sourceRect)
builder.setAspectRatio(
Rational(player.videoSize.width, player.videoSize.height)
)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
builder.setAutoEnterEnabled(shouldEnterPipMode)
}
context.findActivity().setPictureInPictureParams(builder.build())
}
VideoPlayer(modifier = pipModifier)
// [END android_compose_pip_add_remote_actions]
} else {
Log.i(PIP_TAG, "API does not support PiP")
}
}
@Composable
fun PipListenerPreAPI12(shouldEnterPipMode: Boolean) {
// [START android_compose_pip_pre12_listener]
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.S
) {
val context = LocalContext.current
DisposableEffect(context) {
val onUserLeaveBehavior: () -> Unit = {
context.findActivity()
.enterPictureInPictureMode(PictureInPictureParams.Builder().build())
}
context.findActivity().addOnUserLeaveHintListener(
onUserLeaveBehavior
)
onDispose {
context.findActivity().removeOnUserLeaveHintListener(
onUserLeaveBehavior
)
}
}
} else {
Log.i("PiP info", "API does not support PiP")
}
// [END android_compose_pip_pre12_listener]
}
@Composable
fun EnterPiPPre12(shouldEnterPipMode: Boolean) {
// [START android_compose_pip_pre12_should_enter_pip]
val currentShouldEnterPipMode by rememberUpdatedState(newValue = shouldEnterPipMode)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.S
) {
val context = LocalContext.current
DisposableEffect(context) {
val onUserLeaveBehavior: () -> Unit = {
if (currentShouldEnterPipMode) {
context.findActivity()
.enterPictureInPictureMode(PictureInPictureParams.Builder().build())
}
}
context.findActivity().addOnUserLeaveHintListener(
onUserLeaveBehavior
)
onDispose {
context.findActivity().removeOnUserLeaveHintListener(
onUserLeaveBehavior
)
}
}
} else {
Log.i("PiP info", "API does not support PiP")
}
// [END android_compose_pip_pre12_should_enter_pip]
}