Remove init hack in DiscoveryService

This commit is contained in:
Cameron Gutman 2014-07-04 15:11:31 -07:00
parent 0f4e5a4585
commit 825233309d
2 changed files with 20 additions and 45 deletions

Binary file not shown.

View File

@ -1,6 +1,5 @@
package com.limelight.discovery; package com.limelight.discovery;
import java.io.IOException;
import java.util.List; import java.util.List;
import com.limelight.nvstream.mdns.MdnsComputer; import com.limelight.nvstream.mdns.MdnsComputer;
@ -47,59 +46,35 @@ public class DiscoveryService extends Service {
multicastLock = wifiMgr.createMulticastLock("Limelight mDNS"); multicastLock = wifiMgr.createMulticastLock("Limelight mDNS");
multicastLock.setReferenceCounted(false); multicastLock.setReferenceCounted(false);
// This pattern is nasty. We have to do it discoveryAgent = new MdnsDiscoveryAgent(new MdnsDiscoveryListener() {
// because we can't do network on the main thread
// even if it's non-blocking
Thread initThread = new Thread() {
@Override @Override
public void run() { public void notifyComputerAdded(MdnsComputer computer) {
try { if (boundListener != null) {
discoveryAgent = MdnsDiscoveryAgent.createDiscoveryAgent(new MdnsDiscoveryListener() { boundListener.notifyComputerAdded(computer);
@Override
public void notifyComputerAdded(MdnsComputer computer) {
if (boundListener != null) {
boundListener.notifyComputerAdded(computer);
}
}
@Override
public void notifyComputerRemoved(MdnsComputer computer) {
if (boundListener != null) {
boundListener.notifyComputerRemoved(computer);
}
}
@Override
public void notifyDiscoveryFailure(Exception e) {
if (boundListener != null) {
boundListener.notifyDiscoveryFailure(e);
}
}
});
} catch (IOException e) {
e.printStackTrace();
discoveryAgent = null;
} }
} }
};
initThread.start(); @Override
try { public void notifyComputerRemoved(MdnsComputer computer) {
initThread.join(); if (boundListener != null) {
} catch (InterruptedException e) { boundListener.notifyComputerRemoved(computer);
discoveryAgent = null; }
} }
@Override
public void notifyDiscoveryFailure(Exception e) {
if (boundListener != null) {
boundListener.notifyDiscoveryFailure(e);
}
}
});
} }
private DiscoveryBinder binder = new DiscoveryBinder(); private DiscoveryBinder binder = new DiscoveryBinder();
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
// We should only be bindable if discovery can happen return binder;
if (discoveryAgent != null) {
return binder;
}
return null;
} }
@Override @Override