Discovery cleanup

This commit is contained in:
Adam Mills 2018-08-25 14:55:04 -04:00
parent 84299d4cfd
commit 3c3e391232
No known key found for this signature in database
GPG Key ID: 7733DCD6D0428689
4 changed files with 43 additions and 36 deletions

2
extern/GmsApi vendored

@ -1 +1 @@
Subproject commit e5dfb2e459f0196642c17fa04368874677f4e38b
Subproject commit 9b04f236b58bd43b5e9d735c6e676b5eb30502ed

View File

@ -27,7 +27,7 @@ dependencies {
implementation "com.takisoft.fix:preference-v7:$supportLibraryVersion.0"
implementation "de.hdodenhof:circleimageview:1.3.0"
implementation "com.squareup.wire:wire-runtime:1.6.1"
implementation "su.litvak.chromecast:api-v2:0.10.3-SNAPSHOT"
implementation "su.litvak.chromecast:api-v2:0.10.4-SNAPSHOT"
// Specified manually due to
// https://github.com/vitalidze/chromecast-java-api-v2/issues/91

View File

@ -71,18 +71,6 @@ public class CastContextImpl extends ICastContext.Stub {
.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
.addControlCategory(defaultCategory)
.build();
// TODO: Find a home for this once the rest of the implementation
// becomes more clear. Uncomment this to enable discovery of devices.
// Note that the scan currently isn't ever disabled as part of the
// lifecycle, so we don't want to ship with this.
/*
Bundle selectorBundle = this.mergedSelector.asBundle();
router.clearCallbacks();
router.registerMediaRouterCallbackImpl(selectorBundle, new MediaRouterCallbackImpl(this));
router.addCallback(selectorBundle, MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);
*/
}
@Override

View File

@ -53,6 +53,7 @@ public class CastMediaRouteProvider extends MediaRouteProvider {
private static final String TAG = CastMediaRouteProvider.class.getSimpleName();
private Map<String, CastDevice> castDevices = new HashMap<String, CastDevice>();
private Map<String, String> serviceCastIds = new HashMap<String, String>();
private NsdManager mNsdManager;
private NsdManager.DiscoveryListener mDiscoveryListener;
@ -180,7 +181,6 @@ public class CastMediaRouteProvider extends MediaRouteProvider {
@Override
public void onDiscoveryStarted(String regType) {
Log.d(TAG, "DiscoveryListener unimplemented Method: onDiscoveryStarted");
CastMediaRouteProvider.this.state = State.DISCOVERING;
}
@ -189,7 +189,10 @@ public class CastMediaRouteProvider extends MediaRouteProvider {
mNsdManager.resolveService(service, new NsdManager.ResolveListener() {
@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
Log.e(TAG, "DiscoveryListener unimplemented Method: Resolve failed" + errorCode);
if (errorCode == NsdManager.FAILURE_ALREADY_ACTIVE) {
return;
}
Log.e(TAG, "DiscoveryListener Resolve failed. Error code " + errorCode);
}
@Override
@ -197,16 +200,21 @@ public class CastMediaRouteProvider extends MediaRouteProvider {
String name = serviceInfo.getServiceName();
InetAddress host = serviceInfo.getHost();
int port = serviceInfo.getPort();
Map<String, byte[]> attributes = serviceInfo.getAttributes();
if (attributes == null) {
Log.e(TAG, "Error getting service attributes from DNS-SD response");
return;
}
try {
String id = new String(serviceInfo.getAttributes().get("id"), "UTF-8");
String deviceVersion = new String(serviceInfo.getAttributes().get("ve"), "UTF-8");
String friendlyName = new String(serviceInfo.getAttributes().get("fn"), "UTF-8");
String modelName = new String(serviceInfo.getAttributes().get("md"), "UTF-8");
String iconPath = new String(serviceInfo.getAttributes().get("ic"), "UTF-8");
int status = Integer.parseInt(new String(serviceInfo.getAttributes().get("st"), "UTF-8"));
String id = new String(attributes.get("id"), "UTF-8");
String deviceVersion = new String(attributes.get("ve"), "UTF-8");
String friendlyName = new String(attributes.get("fn"), "UTF-8");
String modelName = new String(attributes.get("md"), "UTF-8");
String iconPath = new String(attributes.get("ic"), "UTF-8");
int status = Integer.parseInt(new String(attributes.get("st"), "UTF-8"));
onChromeCastDiscovered(id, name, host, port, deviceVersion, friendlyName, modelName, iconPath, status);
} catch (UnsupportedEncodingException ex) {
} catch (UnsupportedEncodingException | NullPointerException ex) {
Log.e(TAG, "Error getting cast details from DNS-SD response", ex);
return;
}
@ -215,26 +223,23 @@ public class CastMediaRouteProvider extends MediaRouteProvider {
}
@Override
public void onServiceLost(NsdServiceInfo service) {
Log.d(TAG, "DiscoveryListener unimplemented Method: onServiceLost" + service);
// TODO: Remove chromecast route.
public void onServiceLost(NsdServiceInfo serviceInfo) {
String name = serviceInfo.getServiceName();
onChromeCastLost(name);
}
@Override
public void onDiscoveryStopped(String serviceType) {
Log.i(TAG, "DiscoveryListener unimplemented Method: onDiscoveryStopped " + serviceType);
CastMediaRouteProvider.this.state = State.NOT_DISCOVERING;
}
@Override
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "DiscoveryListener unimplemented Method: onStartDiscoveryFailed: Error code:" + errorCode);
CastMediaRouteProvider.this.state = State.NOT_DISCOVERING;
}
@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "DiscoveryListener unimplemented Method: onStopDiscoveryFailed: Error code:" + errorCode);
CastMediaRouteProvider.this.state = State.DISCOVERING;
}
};
@ -250,15 +255,19 @@ public class CastMediaRouteProvider extends MediaRouteProvider {
CastDevice castDevice = new CastDevice(id, name, host, port, deviceVersion, friendlyName, modelName, iconPath, status, capabilities);
this.castDevices.put(id, castDevice);
this.serviceCastIds.put(name, id);
}
Handler mainHandler = new Handler(this.getContext().getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
publishRoutes();
}
});
publishRoutesInMainThread();
}
private void onChromeCastLost(String name) {
String id = this.serviceCastIds.remove(name);
if (id != null) {
this.castDevices.remove(id);
}
publishRoutesInMainThread();
}
@Override
@ -289,6 +298,16 @@ public class CastMediaRouteProvider extends MediaRouteProvider {
return new CastMediaRouteController(this, routeId, castDevice.getAddress());
}
private void publishRoutesInMainThread() {
Handler mainHandler = new Handler(this.getContext().getMainLooper());
mainHandler.post(new Runnable() {
@Override
public void run() {
publishRoutes();
}
});
}
private void publishRoutes() {
MediaRouteProviderDescriptor.Builder builder = new MediaRouteProviderDescriptor.Builder();
for (CastDevice castDevice : this.castDevices.values()) {