diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/pictureinpicture/PictureInPictureSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/pictureinpicture/PictureInPictureSnippets.kt new file mode 100644 index 000000000..79e8805f9 --- /dev/null +++ b/compose/snippets/src/main/java/com/example/compose/snippets/pictureinpicture/PictureInPictureSnippets.kt @@ -0,0 +1,95 @@ +/* + * Copyright 2024 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.content.Context +import android.content.ContextWrapper +import android.os.Build +import android.util.Log +import androidx.activity.ComponentActivity +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.rememberUpdatedState +import androidx.compose.ui.platform.LocalContext + +@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] +} + +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") +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1b32f8851..34a119488 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] accompanist = "0.32.0" androidGradlePlugin = "8.2.2" -androidx-activity-compose = "1.9.0-alpha02" +androidx-activity-compose = "1.9.0-alpha01" androidx-appcompat = "1.6.1" androidx-compose-bom = "2024.01.00" androidx-constraintlayout = "2.1.4"