Changed RemotePlayService so that it works for multiple routes at once.

This commit is contained in:
Felix Ableitner 2014-02-08 20:50:26 +01:00
parent c02d9124ae
commit c0146efb72

View file

@ -29,6 +29,7 @@ package com.github.nutomic.controldlna.upnp;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.teleal.cling.android.AndroidUpnpService; import org.teleal.cling.android.AndroidUpnpService;
@ -95,12 +96,6 @@ public class RemotePlayService extends Service implements RegistryListener {
private ConcurrentHashMap<String, Device<?, ?, ?>> mDevices = private ConcurrentHashMap<String, Device<?, ?, ?>> mDevices =
new ConcurrentHashMap<String, Device<?, ?, ?>>(); new ConcurrentHashMap<String, Device<?, ?, ?>>();
private Device<?, ?, ?> mCurrentRenderer;
private int mPlaybackState;
private boolean mManuallyStopped;
protected AndroidUpnpService mUpnpService; protected AndroidUpnpService mUpnpService;
private ServiceConnection mUpnpServiceConnection = new ServiceConnection() { private ServiceConnection mUpnpServiceConnection = new ServiceConnection() {
@ -126,11 +121,19 @@ public class RemotePlayService extends Service implements RegistryListener {
}; };
/** /**
* Receives events from current renderer. * All active binders. The Hashmap value is unused.
*/ */
private SubscriptionCallback mSubscriptionCallback; WeakHashMap<Binder, Boolean> mBinders = new WeakHashMap<Binder, Boolean>();
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 @Override
public void startSearch(Messenger listener) public void startSearch(Messenger listener)
@ -141,6 +144,11 @@ public class RemotePlayService extends Service implements RegistryListener {
@Override @Override
public void selectRenderer(String id) throws RemoteException { public void selectRenderer(String id) throws RemoteException {
mCurrentRenderer = mDevices.get(id); mCurrentRenderer = mDevices.get(id);
for (Binder b : mBinders.keySet()) {
if (b != this && mCurrentRenderer.equals(b.mCurrentRenderer))
b.unselectRenderer("");
}
mSubscriptionCallback = new SubscriptionCallback( mSubscriptionCallback = new SubscriptionCallback(
mCurrentRenderer.findService( mCurrentRenderer.findService(
new ServiceType("schemas-upnp-org", "AVTransport")), 600) { new ServiceType("schemas-upnp-org", "AVTransport")), 600) {
@ -225,6 +233,7 @@ public class RemotePlayService extends Service implements RegistryListener {
public void unselectRenderer(String sessionId) throws RemoteException { public void unselectRenderer(String sessionId) throws RemoteException {
if (mDevices.get(sessionId) != null) if (mDevices.get(sessionId) != null)
stop(sessionId); stop(sessionId);
if (mSubscriptionCallback != null)
mSubscriptionCallback.end(); mSubscriptionCallback.end();
mCurrentRenderer = null; mCurrentRenderer = null;
} }
@ -424,7 +433,9 @@ public class RemotePlayService extends Service implements RegistryListener {
@Override @Override
public IBinder onBind(Intent itnent) { 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() if (mUpnpService.getControlPoint().getRegistry()
.getDevice(new UDN(d.getKey()), false) == null) { .getDevice(new UDN(d.getKey()), false) == null) {
deviceRemoved(d.getValue()); deviceRemoved(d.getValue());
if (d.getValue().equals(mCurrentRenderer)) for (Binder b : mBinders.keySet()) {
mCurrentRenderer = null; if (b.mCurrentRenderer.equals(d.getValue())) {
b.mSubscriptionCallback.end();
b.mCurrentRenderer = null;
}
}
} }
} }
} }