mirror of
https://invent.kde.org/network/kdeconnect-android.git
synced 2025-12-12 20:35:58 +01:00
MprisMediaSession: Do not hold onto SystemVolumeProvider forever
This commit is contained in:
@@ -34,7 +34,6 @@ import org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationReceiver
|
||||
import org.kde.kdeconnect.Plugins.SystemVolumePlugin.SystemVolumePlugin
|
||||
import org.kde.kdeconnect.Plugins.SystemVolumePlugin.SystemVolumeProvider
|
||||
import org.kde.kdeconnect.Plugins.SystemVolumePlugin.SystemVolumeProvider.Companion.currentProvider
|
||||
import org.kde.kdeconnect.Plugins.SystemVolumePlugin.SystemVolumeProvider.Companion.fromPlugin
|
||||
import org.kde.kdeconnect.Plugins.SystemVolumePlugin.SystemVolumeProvider.ProviderStateListener
|
||||
import org.kde.kdeconnect_tp.R
|
||||
|
||||
@@ -133,6 +132,10 @@ class MprisMediaSession : OnSharedPreferenceChangeListener, NotificationReceiver
|
||||
plugin.removePlayerListUpdatedHandler("media_notification")
|
||||
updateMediaNotification()
|
||||
|
||||
val systemVolumeProvider = SystemVolumeProvider.getInstance()
|
||||
systemVolumeProvider.setPlugin(null)
|
||||
systemVolumeProvider.removeStateListener( this)
|
||||
|
||||
if (mprisDevices.isEmpty()) {
|
||||
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
prefs.unregisterOnSharedPreferenceChangeListener(this)
|
||||
@@ -210,8 +213,9 @@ class MprisMediaSession : OnSharedPreferenceChangeListener, NotificationReceiver
|
||||
private fun updateRemoteDeviceVolumeControl() {
|
||||
val plugin = KdeConnect.getInstance().getDevicePlugin(notificationDeviceId, SystemVolumePlugin::class.java)
|
||||
?: return
|
||||
val systemVolumeProvider = fromPlugin(plugin)
|
||||
systemVolumeProvider.addStateListener(this)
|
||||
val systemVolumeProvider = SystemVolumeProvider.getInstance()
|
||||
systemVolumeProvider.setPlugin(plugin)
|
||||
systemVolumeProvider.addStateListener( this)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,7 +16,7 @@ import org.kde.kdeconnect.Plugins.SystemVolumePlugin.SystemVolumePlugin.SinkList
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.floor
|
||||
|
||||
class SystemVolumeProvider private constructor(plugin: SystemVolumePlugin) :
|
||||
class SystemVolumeProvider :
|
||||
VolumeProviderCompat(VOLUME_CONTROL_ABSOLUTE, DEFAULT_MAX_VOLUME, 0),
|
||||
SinkListener,
|
||||
UpdateListener {
|
||||
@@ -31,11 +31,8 @@ class SystemVolumeProvider private constructor(plugin: SystemVolumePlugin) :
|
||||
private set
|
||||
|
||||
@JvmStatic
|
||||
fun fromPlugin(systemVolumePlugin: SystemVolumePlugin): SystemVolumeProvider {
|
||||
val currentProvider = currentProvider ?: SystemVolumeProvider(systemVolumePlugin)
|
||||
|
||||
currentProvider.update(systemVolumePlugin)
|
||||
|
||||
fun getInstance(): SystemVolumeProvider {
|
||||
val currentProvider = currentProvider ?: SystemVolumeProvider()
|
||||
return currentProvider
|
||||
}
|
||||
|
||||
@@ -49,28 +46,27 @@ class SystemVolumeProvider private constructor(plugin: SystemVolumePlugin) :
|
||||
}
|
||||
}
|
||||
|
||||
private val stateListeners: MutableList<ProviderStateListener>
|
||||
private val stateListeners: MutableList<ProviderStateListener> = mutableListOf()
|
||||
|
||||
private var defaultSink: Sink? = null
|
||||
|
||||
private var systemVolumePlugin: SystemVolumePlugin
|
||||
private var systemVolumePlugin: SystemVolumePlugin? = null
|
||||
|
||||
init {
|
||||
systemVolumePlugin = plugin
|
||||
stateListeners = mutableListOf()
|
||||
}
|
||||
|
||||
private fun update(plugin: SystemVolumePlugin) {
|
||||
fun setPlugin(plugin: SystemVolumePlugin?) {
|
||||
if (plugin === systemVolumePlugin) return
|
||||
|
||||
propagateState(false)
|
||||
defaultSink = null
|
||||
stopListeningForSinks()
|
||||
systemVolumePlugin = plugin
|
||||
startListeningForSinks()
|
||||
if (plugin != null) {
|
||||
startListeningForSinks()
|
||||
}
|
||||
}
|
||||
|
||||
override fun sinksChanged() {
|
||||
val systemVolumePlugin = systemVolumePlugin ?: return
|
||||
|
||||
for (sink in systemVolumePlugin.sinks) {
|
||||
sink.addListener(this)
|
||||
}
|
||||
@@ -109,6 +105,8 @@ class SystemVolumeProvider private constructor(plugin: SystemVolumePlugin) :
|
||||
}
|
||||
|
||||
private fun updateLocalAndRemoteVolume(sink: Sink?, volume: Int) {
|
||||
val systemVolumePlugin = systemVolumePlugin ?: return
|
||||
|
||||
val shouldUpdateRemote = updateLocalVolume(volume)
|
||||
if (!shouldUpdateRemote || sink == null) return
|
||||
val remoteVolume = scaleFromLocal(volume, sink.maxVolume)
|
||||
@@ -156,6 +154,7 @@ class SystemVolumeProvider private constructor(plugin: SystemVolumePlugin) :
|
||||
}
|
||||
|
||||
fun startListeningForSinks() {
|
||||
val systemVolumePlugin = systemVolumePlugin ?: return
|
||||
systemVolumePlugin.addSinkListener(this)
|
||||
systemVolumePlugin.requestSinkList()
|
||||
}
|
||||
@@ -167,6 +166,7 @@ class SystemVolumeProvider private constructor(plugin: SystemVolumePlugin) :
|
||||
}
|
||||
|
||||
private fun stopListeningForSinks() {
|
||||
val systemVolumePlugin = systemVolumePlugin ?: return
|
||||
for (sink in systemVolumePlugin.sinks) {
|
||||
sink.removeListener(this)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user