Skip to content

Commit 6ea91d1

Browse files
authored
Restore missing snippets (#334)
1 parent e7dfd8a commit 6ea91d1

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/pictureinpicture/PictureInPictureSnippets.kt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import androidx.compose.runtime.DisposableEffect
3636
import androidx.compose.runtime.getValue
3737
import androidx.compose.runtime.mutableStateOf
3838
import androidx.compose.runtime.remember
39+
import androidx.compose.runtime.rememberUpdatedState
3940
import androidx.compose.runtime.setValue
4041
import androidx.compose.ui.Modifier
4142
import androidx.compose.ui.graphics.toAndroidRectF
@@ -346,3 +347,60 @@ fun PiPBuilderAddRemoteActions(
346347
Log.i(PIP_TAG, "API does not support PiP")
347348
}
348349
}
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

Comments
 (0)