@@ -36,6 +36,7 @@ import androidx.compose.runtime.DisposableEffect
36
36
import androidx.compose.runtime.getValue
37
37
import androidx.compose.runtime.mutableStateOf
38
38
import androidx.compose.runtime.remember
39
+ import androidx.compose.runtime.rememberUpdatedState
39
40
import androidx.compose.runtime.setValue
40
41
import androidx.compose.ui.Modifier
41
42
import androidx.compose.ui.graphics.toAndroidRectF
@@ -346,3 +347,60 @@ fun PiPBuilderAddRemoteActions(
346
347
Log .i(PIP_TAG , " API does not support PiP" )
347
348
}
348
349
}
350
+
351
+ @Composable
352
+ fun PipListenerPreAPI12 (shouldEnterPipMode : Boolean ) {
353
+ // [START android_compose_pip_pre12_listener]
354
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O &&
355
+ Build .VERSION .SDK_INT < Build .VERSION_CODES .S
356
+ ) {
357
+ val context = LocalContext .current
358
+ DisposableEffect (context) {
359
+ val onUserLeaveBehavior: () -> Unit = {
360
+ context.findActivity()
361
+ .enterPictureInPictureMode(PictureInPictureParams .Builder ().build())
362
+ }
363
+ context.findActivity().addOnUserLeaveHintListener(
364
+ onUserLeaveBehavior
365
+ )
366
+ onDispose {
367
+ context.findActivity().removeOnUserLeaveHintListener(
368
+ onUserLeaveBehavior
369
+ )
370
+ }
371
+ }
372
+ } else {
373
+ Log .i(" PiP info" , " API does not support PiP" )
374
+ }
375
+ // [END android_compose_pip_pre12_listener]
376
+ }
377
+
378
+ @Composable
379
+ fun EnterPiPPre12 (shouldEnterPipMode : Boolean ) {
380
+ // [START android_compose_pip_pre12_should_enter_pip]
381
+ val currentShouldEnterPipMode by rememberUpdatedState(newValue = shouldEnterPipMode)
382
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O &&
383
+ Build .VERSION .SDK_INT < Build .VERSION_CODES .S
384
+ ) {
385
+ val context = LocalContext .current
386
+ DisposableEffect (context) {
387
+ val onUserLeaveBehavior: () -> Unit = {
388
+ if (currentShouldEnterPipMode) {
389
+ context.findActivity()
390
+ .enterPictureInPictureMode(PictureInPictureParams .Builder ().build())
391
+ }
392
+ }
393
+ context.findActivity().addOnUserLeaveHintListener(
394
+ onUserLeaveBehavior
395
+ )
396
+ onDispose {
397
+ context.findActivity().removeOnUserLeaveHintListener(
398
+ onUserLeaveBehavior
399
+ )
400
+ }
401
+ }
402
+ } else {
403
+ Log .i(" PiP info" , " API does not support PiP" )
404
+ }
405
+ // [END android_compose_pip_pre12_should_enter_pip]
406
+ }
0 commit comments