-
Notifications
You must be signed in to change notification settings - Fork 362
Add snippets for Jetpack PiP and notifications #900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bedc9f2
17da2b4
b7955ef
31de567
8316289
0b158f9
7cb57c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright 2026 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.notifications | ||
|
|
||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.app.PendingIntent; | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.core.app.NotificationCompat | ||
| import androidx.core.app.PendingIntentCompat | ||
|
|
||
| @Composable | ||
| fun NotificationSnippets(context: Context) { | ||
| // [START android_notification_authenticated_action] | ||
| val intent = Intent( | ||
| context, null | ||
| /** Replace with a valid target activity */ | ||
| ) | ||
| val moreSecureNotification = NotificationCompat.Action.Builder( | ||
| 0, "Reply", | ||
| PendingIntentCompat.getActivity(context, 0, intent, PendingIntent.FLAG_NO_CREATE, false) | ||
| ) | ||
| // This notification always requests authentication when invoked | ||
| // from a lock screen. | ||
| .setAuthenticationRequired(true) | ||
| .build() | ||
| // [END android_notification_authenticated_action] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * Copyright 2026 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.content.res.Configuration | ||
| import android.os.Bundle | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.activity.compose.setContent | ||
| import androidx.core.content.ContextCompat | ||
| import androidx.core.pip.BasicPictureInPicture | ||
| import androidx.core.pip.PictureInPictureDelegate | ||
|
|
||
| // [START android_jpip_basic_impl] | ||
| class NavOrVideoCallJpipActivity : ComponentActivity(), PictureInPictureDelegate.OnPictureInPictureEventListener { | ||
| private lateinit var pictureInPictureImpl: BasicPictureInPicture | ||
|
alabiaga marked this conversation as resolved.
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| pictureInPictureImpl = BasicPictureInPicture(this) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need to clean up pictureInPictureImpl in this file too?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i did clean it up in both files
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm can you tell me which line number it is in this file? maybe I'm not seeing it..
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i instantiate pictureInPictureImpl in line 32 vs inline on line 29 as suggested by the assistant |
||
| // BasicPictureInPicture is ideal for Navigation and Video call use cases. | ||
| pictureInPictureImpl.addOnPictureInPictureEventListener( | ||
| ContextCompat.getMainExecutor(this), | ||
| this | ||
| ) | ||
| setContent { | ||
| } | ||
| } | ||
| override fun onPictureInPictureEvent( | ||
| event: PictureInPictureDelegate.Event, | ||
| config: Configuration? | ||
| ) { | ||
| when (event) { | ||
| PictureInPictureDelegate.Event.ENTERED -> { /* Toggle to PiP layout */ } | ||
| PictureInPictureDelegate.Event.EXITED -> { /* Toggle to Full-screen layout */ } | ||
| PictureInPictureDelegate.Event.STASHED -> { /* Optional: PiP is stashed */ } | ||
| PictureInPictureDelegate.Event.UNSTASHED -> { /* Optional: PiP is unstashed */ } | ||
| } | ||
| } | ||
| } | ||
| // [END android_jpip_basic_impl] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -404,3 +404,4 @@ fun EnterPiPPre12(shouldEnterPipMode: Boolean) { | |
| } | ||
| // [END android_compose_pip_pre12_should_enter_pip] | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Copyright 2026 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.content.res.Configuration | ||
| import android.os.Bundle | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.activity.compose.setContent | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.DisposableEffect | ||
| import androidx.core.content.ContextCompat | ||
| import androidx.core.pip.PictureInPictureDelegate | ||
| import androidx.core.pip.VideoPlaybackPictureInPicture | ||
|
|
||
| // [START android_jpip_video_playback_impl] | ||
| class VideoPlaybackJpipActivity : ComponentActivity(), PictureInPictureDelegate.OnPictureInPictureEventListener { | ||
| private lateinit var pictureInPictureImpl: VideoPlaybackPictureInPicture | ||
|
alabiaga marked this conversation as resolved.
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| pictureInPictureImpl = VideoPlaybackPictureInPicture(this) | ||
| pictureInPictureImpl.addOnPictureInPictureEventListener( | ||
| ContextCompat.getMainExecutor(this), | ||
| this | ||
| ) | ||
| setContent { | ||
| ContentScreen(pictureInPictureImpl) | ||
| } | ||
| } | ||
| override fun onPictureInPictureEvent( | ||
| event: PictureInPictureDelegate.Event, | ||
| config: Configuration? | ||
| ) { | ||
| when (event) { | ||
| PictureInPictureDelegate.Event.ENTER_ANIMATION_START -> { /* Hide overlays */ } | ||
| PictureInPictureDelegate.Event.ENTER_ANIMATION_END -> { /* Animation finished */ } | ||
| PictureInPictureDelegate.Event.ENTERED -> { /* Switch to PiP layout */ } | ||
| PictureInPictureDelegate.Event.STASHED -> { /* PiP stashed */ } | ||
| PictureInPictureDelegate.Event.UNSTASHED -> { /* PiP unstashed */ } | ||
| PictureInPictureDelegate.Event.EXITED -> { /* Return to full-screen */ } | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| fun ContentScreen(pipController: VideoPlaybackPictureInPicture) { | ||
| DisposableEffect(pipController) { | ||
| onDispose { | ||
| pipController.close() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // [END android_jpip_video_playback_impl] | ||
Uh oh!
There was an error while loading. Please reload this page.