Skip to content

Commit 5cb7dd2

Browse files
Adding pip snippets to latest branch (#196)
* Adding pip snippets to latest branch * Apply Spotless * Adding pip snippets to latest --------- Co-authored-by: MagicalMeghan <[email protected]>
1 parent ef49816 commit 5cb7dd2

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compose.snippets.pictureinpicture
18+
19+
import android.app.PictureInPictureParams
20+
import android.content.Context
21+
import android.content.ContextWrapper
22+
import android.os.Build
23+
import android.util.Log
24+
import androidx.activity.ComponentActivity
25+
import androidx.compose.runtime.Composable
26+
import androidx.compose.runtime.DisposableEffect
27+
import androidx.compose.runtime.getValue
28+
import androidx.compose.runtime.rememberUpdatedState
29+
import androidx.compose.ui.platform.LocalContext
30+
31+
@Composable
32+
fun PipListenerPreAPI12(shouldEnterPipMode: Boolean) {
33+
// [START android_compose_pip_pre12_listener]
34+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
35+
Build.VERSION.SDK_INT < Build.VERSION_CODES.S
36+
) {
37+
val context = LocalContext.current
38+
DisposableEffect(context) {
39+
val onUserLeaveBehavior: () -> Unit = {
40+
context.findActivity()
41+
.enterPictureInPictureMode(PictureInPictureParams.Builder().build())
42+
}
43+
context.findActivity().addOnUserLeaveHintListener(
44+
onUserLeaveBehavior
45+
)
46+
onDispose {
47+
context.findActivity().removeOnUserLeaveHintListener(
48+
onUserLeaveBehavior
49+
)
50+
}
51+
}
52+
} else {
53+
Log.i("PiP info", "API does not support PiP")
54+
}
55+
// [END android_compose_pip_pre12_listener]
56+
}
57+
58+
@Composable
59+
fun EnterPiPPre12(shouldEnterPipMode: Boolean) {
60+
// [START android_compose_pip_pre12_should_enter_pip]
61+
val currentShouldEnterPipMode by rememberUpdatedState(newValue = shouldEnterPipMode)
62+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
63+
Build.VERSION.SDK_INT < Build.VERSION_CODES.S
64+
) {
65+
val context = LocalContext.current
66+
DisposableEffect(context) {
67+
val onUserLeaveBehavior: () -> Unit = {
68+
if (currentShouldEnterPipMode) {
69+
context.findActivity()
70+
.enterPictureInPictureMode(PictureInPictureParams.Builder().build())
71+
}
72+
}
73+
context.findActivity().addOnUserLeaveHintListener(
74+
onUserLeaveBehavior
75+
)
76+
onDispose {
77+
context.findActivity().removeOnUserLeaveHintListener(
78+
onUserLeaveBehavior
79+
)
80+
}
81+
}
82+
} else {
83+
Log.i("PiP info", "API does not support PiP")
84+
}
85+
// [END android_compose_pip_pre12_should_enter_pip]
86+
}
87+
88+
internal fun Context.findActivity(): ComponentActivity {
89+
var context = this
90+
while (context is ContextWrapper) {
91+
if (context is ComponentActivity) return context
92+
context = context.baseContext
93+
}
94+
throw IllegalStateException("Picture in picture should be called in the context of an Activity")
95+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[versions]
22
accompanist = "0.32.0"
33
androidGradlePlugin = "8.2.2"
4-
androidx-activity-compose = "1.9.0-alpha02"
4+
androidx-activity-compose = "1.9.0-alpha01"
55
androidx-appcompat = "1.6.1"
66
androidx-compose-bom = "2024.01.00"
77
androidx-constraintlayout = "2.1.4"

0 commit comments

Comments
 (0)