From ef9a607dbeb01cb64abe4742f9dd885660eb8095 Mon Sep 17 00:00:00 2001 From: Michatec Date: Sun, 16 Aug 2026 20:31:45 +0200 Subject: [PATCH] feat(cast): add volume control to custom cast controller dialog - Integrated a SeekBar for volume adjustment in the cast connection dialog. - Implemented a Cast listener to synchronize volume state between the remote device and the UI. - Added volume icons and updated the dialog layout to include the control bar. - Managed listener registration and UI updates within the dialog lifecycle. --- .../ui/CustomCastControllerDialogFragment.kt | 41 ++++++++++++++++++- .../main/res/drawable/ic_volume_down_24dp.xml | 10 +++++ .../main/res/drawable/ic_volume_up_24dp.xml | 10 +++++ .../main/res/layout/dialog_cast_connected.xml | 32 ++++++++++++++- 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/drawable/ic_volume_down_24dp.xml create mode 100644 app/src/main/res/drawable/ic_volume_up_24dp.xml diff --git a/app/src/main/java/com/michatec/radio/ui/CustomCastControllerDialogFragment.kt b/app/src/main/java/com/michatec/radio/ui/CustomCastControllerDialogFragment.kt index 8b84eec..68d308c 100644 --- a/app/src/main/java/com/michatec/radio/ui/CustomCastControllerDialogFragment.kt +++ b/app/src/main/java/com/michatec/radio/ui/CustomCastControllerDialogFragment.kt @@ -3,8 +3,11 @@ package com.michatec.radio.ui import android.app.Dialog import android.os.Bundle import android.view.LayoutInflater +import android.widget.SeekBar import androidx.mediarouter.app.MediaRouteControllerDialogFragment +import com.google.android.gms.cast.Cast import com.google.android.gms.cast.framework.CastContext +import com.google.android.gms.cast.framework.CastSession import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.michatec.radio.R import com.michatec.radio.databinding.DialogCastConnectedBinding @@ -17,6 +20,13 @@ class CustomCastControllerDialogFragment : MediaRouteControllerDialogFragment() private var _binding: DialogCastConnectedBinding? = null private val binding get() = _binding!! + private var castSession: CastSession? = null + + private val castListener = object : Cast.Listener() { + override fun onVolumeChanged() { + updateVolumeSeekBar() + } + } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val context = requireContext() @@ -24,11 +34,23 @@ class CustomCastControllerDialogFragment : MediaRouteControllerDialogFragment() _binding = DialogCastConnectedBinding.inflate(LayoutInflater.from(context)) val castContext = CastContext.getSharedInstance(context) - val castSession = castContext.sessionManager.currentCastSession + castSession = castContext.sessionManager.currentCastSession val deviceName = castSession?.castDevice?.friendlyName ?: context.getString(R.string.media_route_menu_title) binding.textViewDeviceName.text = deviceName + updateVolumeSeekBar() + binding.seekBarVolume.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { + if (fromUser) { + castSession?.volume = progress / 100.0 + } + } + + override fun onStartTrackingTouch(seekBar: SeekBar?) {} + override fun onStopTrackingTouch(seekBar: SeekBar?) {} + }) + val dialog = MaterialAlertDialogBuilder(context) .setView(binding.root) .create() @@ -45,6 +67,23 @@ class CustomCastControllerDialogFragment : MediaRouteControllerDialogFragment() return dialog } + override fun onStart() { + super.onStart() + castSession?.addCastListener(castListener) + updateVolumeSeekBar() + } + + override fun onStop() { + super.onStop() + castSession?.removeCastListener(castListener) + } + + private fun updateVolumeSeekBar() { + val volume = castSession?.volume ?: 0.0 + binding.seekBarVolume.progress = (volume * 100).toInt() + binding.seekBarVolume.isEnabled = castSession?.isMute == false + } + override fun onDestroyView() { super.onDestroyView() _binding = null diff --git a/app/src/main/res/drawable/ic_volume_down_24dp.xml b/app/src/main/res/drawable/ic_volume_down_24dp.xml new file mode 100644 index 0000000..958fff2 --- /dev/null +++ b/app/src/main/res/drawable/ic_volume_down_24dp.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/ic_volume_up_24dp.xml b/app/src/main/res/drawable/ic_volume_up_24dp.xml new file mode 100644 index 0000000..a1f277c --- /dev/null +++ b/app/src/main/res/drawable/ic_volume_up_24dp.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/dialog_cast_connected.xml b/app/src/main/res/layout/dialog_cast_connected.xml index 4327861..870472f 100644 --- a/app/src/main/res/layout/dialog_cast_connected.xml +++ b/app/src/main/res/layout/dialog_cast_connected.xml @@ -29,12 +29,42 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" - android:layout_marginBottom="24dp" + android:layout_marginBottom="16dp" android:gravity="center" android:textAppearance="?attr/textAppearanceBodyLarge" android:textColor="?attr/colorOnSurfaceVariant" android:textStyle="bold" /> + + + + + + + + + +