From c0146efb722bc20a75bba1cfdfed59a6f17d5eb2 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sat, 8 Feb 2014 20:50:26 +0100 Subject: [PATCH] Changed RemotePlayService so that it works for multiple routes at once. --- .../controldlna/upnp/RemotePlayService.java | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/com/github/nutomic/controldlna/upnp/RemotePlayService.java b/src/com/github/nutomic/controldlna/upnp/RemotePlayService.java index 1824286..6e51eff 100644 --- a/src/com/github/nutomic/controldlna/upnp/RemotePlayService.java +++ b/src/com/github/nutomic/controldlna/upnp/RemotePlayService.java @@ -29,6 +29,7 @@ package com.github.nutomic.controldlna.upnp; import java.util.Map; import java.util.Map.Entry; +import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; import org.teleal.cling.android.AndroidUpnpService; @@ -94,12 +95,6 @@ public class RemotePlayService extends Service implements RegistryListener { private ConcurrentHashMap> mDevices = new ConcurrentHashMap>(); - - private Device mCurrentRenderer; - - private int mPlaybackState; - - private boolean mManuallyStopped; protected AndroidUpnpService mUpnpService; @@ -124,13 +119,21 @@ public class RemotePlayService extends Service implements RegistryListener { mUpnpService = null; } }; - + /** - * Receives events from current renderer. + * All active binders. The Hashmap value is unused. */ - private SubscriptionCallback mSubscriptionCallback; + WeakHashMap mBinders = new WeakHashMap(); - private final IRemotePlayService.Stub mBinder = new IRemotePlayService.Stub() { + private class Binder extends IRemotePlayService.Stub { + + private Device mCurrentRenderer; + + private int mPlaybackState; + + private boolean mManuallyStopped; + + private SubscriptionCallback mSubscriptionCallback; @Override public void startSearch(Messenger listener) @@ -141,6 +144,11 @@ public class RemotePlayService extends Service implements RegistryListener { @Override public void selectRenderer(String id) throws RemoteException { mCurrentRenderer = mDevices.get(id); + for (Binder b : mBinders.keySet()) { + if (b != this && mCurrentRenderer.equals(b.mCurrentRenderer)) + b.unselectRenderer(""); + } + mSubscriptionCallback = new SubscriptionCallback( mCurrentRenderer.findService( new ServiceType("schemas-upnp-org", "AVTransport")), 600) { @@ -225,7 +233,8 @@ public class RemotePlayService extends Service implements RegistryListener { public void unselectRenderer(String sessionId) throws RemoteException { if (mDevices.get(sessionId) != null) stop(sessionId); - mSubscriptionCallback.end(); + if (mSubscriptionCallback != null) + mSubscriptionCallback.end(); mCurrentRenderer = null; } @@ -424,7 +433,9 @@ public class RemotePlayService extends Service implements RegistryListener { @Override public IBinder onBind(Intent itnent) { - return mBinder; + Binder b = new Binder(); + mBinders.put(b, true); + return b; } /** @@ -497,8 +508,12 @@ public class RemotePlayService extends Service implements RegistryListener { if (mUpnpService.getControlPoint().getRegistry() .getDevice(new UDN(d.getKey()), false) == null) { deviceRemoved(d.getValue()); - if (d.getValue().equals(mCurrentRenderer)) - mCurrentRenderer = null; + for (Binder b : mBinders.keySet()) { + if (b.mCurrentRenderer.equals(d.getValue())) { + b.mSubscriptionCallback.end(); + b.mCurrentRenderer = null; + } + } } } }