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.SystemVolumePlugin
|
||||||
import org.kde.kdeconnect.Plugins.SystemVolumePlugin.SystemVolumeProvider
|
import org.kde.kdeconnect.Plugins.SystemVolumePlugin.SystemVolumeProvider
|
||||||
import org.kde.kdeconnect.Plugins.SystemVolumePlugin.SystemVolumeProvider.Companion.currentProvider
|
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.Plugins.SystemVolumePlugin.SystemVolumeProvider.ProviderStateListener
|
||||||
import org.kde.kdeconnect_tp.R
|
import org.kde.kdeconnect_tp.R
|
||||||
|
|
||||||
@@ -133,6 +132,10 @@ class MprisMediaSession : OnSharedPreferenceChangeListener, NotificationReceiver
|
|||||||
plugin.removePlayerListUpdatedHandler("media_notification")
|
plugin.removePlayerListUpdatedHandler("media_notification")
|
||||||
updateMediaNotification()
|
updateMediaNotification()
|
||||||
|
|
||||||
|
val systemVolumeProvider = SystemVolumeProvider.getInstance()
|
||||||
|
systemVolumeProvider.setPlugin(null)
|
||||||
|
systemVolumeProvider.removeStateListener( this)
|
||||||
|
|
||||||
if (mprisDevices.isEmpty()) {
|
if (mprisDevices.isEmpty()) {
|
||||||
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
prefs.unregisterOnSharedPreferenceChangeListener(this)
|
prefs.unregisterOnSharedPreferenceChangeListener(this)
|
||||||
@@ -210,8 +213,9 @@ class MprisMediaSession : OnSharedPreferenceChangeListener, NotificationReceiver
|
|||||||
private fun updateRemoteDeviceVolumeControl() {
|
private fun updateRemoteDeviceVolumeControl() {
|
||||||
val plugin = KdeConnect.getInstance().getDevicePlugin(notificationDeviceId, SystemVolumePlugin::class.java)
|
val plugin = KdeConnect.getInstance().getDevicePlugin(notificationDeviceId, SystemVolumePlugin::class.java)
|
||||||
?: return
|
?: return
|
||||||
val systemVolumeProvider = fromPlugin(plugin)
|
val systemVolumeProvider = SystemVolumeProvider.getInstance()
|
||||||
systemVolumeProvider.addStateListener(this)
|
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.ceil
|
||||||
import kotlin.math.floor
|
import kotlin.math.floor
|
||||||
|
|
||||||
class SystemVolumeProvider private constructor(plugin: SystemVolumePlugin) :
|
class SystemVolumeProvider :
|
||||||
VolumeProviderCompat(VOLUME_CONTROL_ABSOLUTE, DEFAULT_MAX_VOLUME, 0),
|
VolumeProviderCompat(VOLUME_CONTROL_ABSOLUTE, DEFAULT_MAX_VOLUME, 0),
|
||||||
SinkListener,
|
SinkListener,
|
||||||
UpdateListener {
|
UpdateListener {
|
||||||
@@ -31,11 +31,8 @@ class SystemVolumeProvider private constructor(plugin: SystemVolumePlugin) :
|
|||||||
private set
|
private set
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun fromPlugin(systemVolumePlugin: SystemVolumePlugin): SystemVolumeProvider {
|
fun getInstance(): SystemVolumeProvider {
|
||||||
val currentProvider = currentProvider ?: SystemVolumeProvider(systemVolumePlugin)
|
val currentProvider = currentProvider ?: SystemVolumeProvider()
|
||||||
|
|
||||||
currentProvider.update(systemVolumePlugin)
|
|
||||||
|
|
||||||
return currentProvider
|
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 defaultSink: Sink? = null
|
||||||
|
|
||||||
private var systemVolumePlugin: SystemVolumePlugin
|
private var systemVolumePlugin: SystemVolumePlugin? = null
|
||||||
|
|
||||||
init {
|
fun setPlugin(plugin: SystemVolumePlugin?) {
|
||||||
systemVolumePlugin = plugin
|
|
||||||
stateListeners = mutableListOf()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun update(plugin: SystemVolumePlugin) {
|
|
||||||
if (plugin === systemVolumePlugin) return
|
if (plugin === systemVolumePlugin) return
|
||||||
|
|
||||||
propagateState(false)
|
propagateState(false)
|
||||||
defaultSink = null
|
defaultSink = null
|
||||||
stopListeningForSinks()
|
stopListeningForSinks()
|
||||||
systemVolumePlugin = plugin
|
systemVolumePlugin = plugin
|
||||||
startListeningForSinks()
|
if (plugin != null) {
|
||||||
|
startListeningForSinks()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun sinksChanged() {
|
override fun sinksChanged() {
|
||||||
|
val systemVolumePlugin = systemVolumePlugin ?: return
|
||||||
|
|
||||||
for (sink in systemVolumePlugin.sinks) {
|
for (sink in systemVolumePlugin.sinks) {
|
||||||
sink.addListener(this)
|
sink.addListener(this)
|
||||||
}
|
}
|
||||||
@@ -109,6 +105,8 @@ class SystemVolumeProvider private constructor(plugin: SystemVolumePlugin) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun updateLocalAndRemoteVolume(sink: Sink?, volume: Int) {
|
private fun updateLocalAndRemoteVolume(sink: Sink?, volume: Int) {
|
||||||
|
val systemVolumePlugin = systemVolumePlugin ?: return
|
||||||
|
|
||||||
val shouldUpdateRemote = updateLocalVolume(volume)
|
val shouldUpdateRemote = updateLocalVolume(volume)
|
||||||
if (!shouldUpdateRemote || sink == null) return
|
if (!shouldUpdateRemote || sink == null) return
|
||||||
val remoteVolume = scaleFromLocal(volume, sink.maxVolume)
|
val remoteVolume = scaleFromLocal(volume, sink.maxVolume)
|
||||||
@@ -156,6 +154,7 @@ class SystemVolumeProvider private constructor(plugin: SystemVolumePlugin) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun startListeningForSinks() {
|
fun startListeningForSinks() {
|
||||||
|
val systemVolumePlugin = systemVolumePlugin ?: return
|
||||||
systemVolumePlugin.addSinkListener(this)
|
systemVolumePlugin.addSinkListener(this)
|
||||||
systemVolumePlugin.requestSinkList()
|
systemVolumePlugin.requestSinkList()
|
||||||
}
|
}
|
||||||
@@ -167,6 +166,7 @@ class SystemVolumeProvider private constructor(plugin: SystemVolumePlugin) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun stopListeningForSinks() {
|
private fun stopListeningForSinks() {
|
||||||
|
val systemVolumePlugin = systemVolumePlugin ?: return
|
||||||
for (sink in systemVolumePlugin.sinks) {
|
for (sink in systemVolumePlugin.sinks) {
|
||||||
sink.removeListener(this)
|
sink.removeListener(this)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user