-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding pip snippets to latest branch
- Loading branch information
1 parent
ef49816
commit 64701fc
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...s/src/main/java/com/example/compose/snippets/pictureinpicture/PictureInPictureSnippets.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.example.compose.snippets.pictureinpicture | ||
|
||
import android.app.PictureInPictureParams | ||
import android.content.Context | ||
import android.content.ContextWrapper | ||
import android.os.Build | ||
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 | ||
) | ||
} | ||
} | ||
} | ||
// [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 | ||
) | ||
} | ||
} | ||
} | ||
// [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") | ||
} | ||
|