1616
1717package com.example.android.pip
1818
19- import android.app.PendingIntent
20- import android.app.PictureInPictureParams
21- import android.app.PictureInPictureUiState
22- import android.app.RemoteAction
23- import android.content.BroadcastReceiver
24- import android.content.Context
25- import android.content.Intent
26- import android.content.IntentFilter
27- import android.content.res.Configuration
28- import android.graphics.drawable.Icon
29- import android.os.Build
3019import android.os.Bundle
31- import android.util.Rational
32- import android.view.View
3320import androidx.activity.ComponentActivity
34- import androidx.activity.trackPipAnimationHintView
3521import androidx.activity.viewModels
36- import androidx.annotation.DrawableRes
3722import androidx.annotation.RequiresApi
38- import androidx.annotation.StringRes
39- import androidx.core.app.ActivityCompat
40- import androidx.core.content.ContextCompat
41- import androidx.lifecycle.Lifecycle
42- import androidx.lifecycle.lifecycleScope
43- import androidx.lifecycle.repeatOnLifecycle
4423import com.example.android.pip.databinding.PipActivityBinding
45- import kotlinx.coroutines.launch
46-
47- /* * Intent action for stopwatch controls from Picture-in-Picture mode. */
48- private const val ACTION_STOPWATCH_CONTROL = " stopwatch_control"
49-
50- /* * Intent extra for stopwatch controls from Picture-in-Picture mode. */
51- private const val EXTRA_CONTROL_TYPE = " control_type"
52- private const val CONTROL_TYPE_CLEAR = 1
53- private const val CONTROL_TYPE_START_OR_PAUSE = 2
54-
55- private const val REQUEST_CLEAR = 3
56- private const val REQUEST_START_OR_PAUSE = 4
5724
5825/* *
5926 * Demonstrates usage of Picture-in-Picture mode on phones and tablets.
@@ -64,23 +31,6 @@ class PiPSampleActivity : ComponentActivity() {
6431 private val viewModel: PiPViewModel by viewModels()
6532 private lateinit var binding: PipActivityBinding
6633
67- /* *
68- * A [BroadcastReceiver] for handling action items on the picture-in-picture mode.
69- */
70- private val broadcastReceiver = object : BroadcastReceiver () {
71-
72- // Called when an item is clicked.
73- override fun onReceive (context : Context ? , intent : Intent ? ) {
74- if (intent == null || intent.action != ACTION_STOPWATCH_CONTROL ) {
75- return
76- }
77- when (intent.getIntExtra(EXTRA_CONTROL_TYPE , 0 )) {
78- CONTROL_TYPE_START_OR_PAUSE -> viewModel.startOrPause()
79- CONTROL_TYPE_CLEAR -> viewModel.clear()
80- }
81- }
82- }
83-
8434 override fun onCreate (savedInstanceState : Bundle ? ) {
8535 super .onCreate(savedInstanceState)
8636 binding = PipActivityBinding .inflate(layoutInflater)
@@ -89,132 +39,13 @@ class PiPSampleActivity : ComponentActivity() {
8939 binding.clear.setOnClickListener { viewModel.clear() }
9040 binding.startOrPause.setOnClickListener { viewModel.startOrPause() }
9141 binding.pip.setOnClickListener {
92- // enterPictureInPictureMode(updatePictureInPictureParams(viewModel.started.value == true))
9342 }
9443 // Observe data from the viewModel.
9544 viewModel.time.observe(this ) { time -> binding.time.text = time }
9645 viewModel.started.observe(this ) { started ->
9746 binding.startOrPause.setImageResource(
9847 if (started) R .drawable.ic_pause_24dp else R .drawable.ic_play_arrow_24dp,
9948 )
100- // updatePictureInPictureParams(started)
101- }
102-
103- // Use trackPipAnimationHint view to make a smooth enter/exit pip transition.
104- // See https://android.devsite.corp.google.com/develop/ui/views/picture-in-picture#smoother-transition
105- /* lifecycleScope.launch {
106- repeatOnLifecycle(Lifecycle.State.STARTED) {
107- trackPipAnimationHintView(binding.stopwatchBackground)
108- }
109- }*/
110-
111- // Handle events from the action icons on the picture-in-picture mode.
112- /* ActivityCompat.registerReceiver(
113- this,
114- broadcastReceiver,
115- IntentFilter(ACTION_STOPWATCH_CONTROL),
116- ContextCompat.RECEIVER_NOT_EXPORTED
117- )*/
118- }
119-
120- // This is called when the activity gets into or out of the picture-in-picture mode.
121- /* override fun onPictureInPictureModeChanged(
122- isInPictureInPictureMode: Boolean,
123- newConfig: Configuration,
124- ) {
125- super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig)
126- // Toggle visibility of in-app buttons. They cannot be interacted in the picture-in-picture
127- // mode, and their features are provided as the action icons.
128- toggleControls(if (isInPictureInPictureMode) View.GONE else View.VISIBLE)
129- }*/
130-
131- private fun toggleControls (view : Int ) {
132- binding.clear.visibility = view
133- binding.startOrPause.visibility = view
134- }
135-
136- /* @RequiresApi(35)
137- override fun onPictureInPictureUiStateChanged(pipState: PictureInPictureUiState) {
138- super.onPictureInPictureUiStateChanged(pipState)
139- if (pipState.isTransitioningToPip) {
140- toggleControls(View.GONE)
141- }
142- }*/
143-
144- /* *
145- * Updates the parameters of the picture-in-picture mode for this activity based on the current
146- * [started] state of the stopwatch.
147- */
148- /*
149- private fun updatePictureInPictureParams(started: Boolean): PictureInPictureParams {
150- val params = PictureInPictureParams.Builder()
151- // Set action items for the picture-in-picture mode. These are the only custom controls
152- // available during the picture-in-picture mode.
153- .setActions(
154- listOf(
155- // "Clear" action.
156- createRemoteAction(
157- R.drawable.ic_refresh_24dp,
158- R.string.clear,
159- REQUEST_CLEAR,
160- CONTROL_TYPE_CLEAR,
161- ),
162- if (started) {
163- // "Pause" action when the stopwatch is already started.
164- createRemoteAction(
165- R.drawable.ic_pause_24dp,
166- R.string.pause,
167- REQUEST_START_OR_PAUSE,
168- CONTROL_TYPE_START_OR_PAUSE,
169- )
170- } else {
171- // "Start" action when the stopwatch is not started.
172- createRemoteAction(
173- R.drawable.ic_play_arrow_24dp,
174- R.string.start,
175- REQUEST_START_OR_PAUSE,
176- CONTROL_TYPE_START_OR_PAUSE,
177- )
178- },
179- ),
180- )
181- // Set the aspect ratio of the picture-in-picture mode.
182- .setAspectRatio(Rational(16, 9))
183- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
184- // Turn the screen into the picture-in-picture mode if it's hidden by the "Home" button.
185- params.setAutoEnterEnabled(true)
186- // Disables the seamless resize. The seamless resize works great for videos where the
187- // content can be arbitrarily scaled, but you can disable this for non-video content so
188- // that the picture-in-picture mode is resized with a cross fade animation.
189- .setSeamlessResizeEnabled(false)
190- }
191- return params.build().also {
192- setPictureInPictureParams(it)
19349 }
19450 }
195- */
196-
197- /* *
198- * Creates a [RemoteAction]. It is used as an action icon on the overlay of the
199- * picture-in-picture mode.
200- */
201- private fun createRemoteAction (
202- @DrawableRes iconResId : Int ,
203- @StringRes titleResId : Int ,
204- requestCode : Int ,
205- controlType : Int ,
206- ): RemoteAction {
207- return RemoteAction (
208- Icon .createWithResource(this , iconResId),
209- getString(titleResId),
210- getString(titleResId),
211- PendingIntent .getBroadcast(
212- this ,
213- requestCode,
214- Intent (ACTION_STOPWATCH_CONTROL )
215- .putExtra(EXTRA_CONTROL_TYPE , controlType),
216- PendingIntent .FLAG_IMMUTABLE ,
217- ),
218- )
219- }
22051}
0 commit comments