use of com.connectsdk.service.DeviceService in project butter-android by butterproject.
the class BeamDeviceAdapter method getView.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.casting_dialog_listitem, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ConnectableDevice device = getItem(position);
int imgResource = R.drawable.ic_dlna;
Collection<DeviceService> services = device.getServices();
ArrayList<String> textServices = new ArrayList<>(services.size());
for (DeviceService service : services) {
if (service instanceof CastService) {
imgResource = R.drawable.ic_googlecast;
textServices.add("Google Cast");
} else if (service instanceof DLNAService) {
imgResource = R.drawable.ic_dlna;
textServices.add("DLNA");
} else if (service instanceof AirPlayService) {
imgResource = R.drawable.ic_airplay;
textServices.add("AirPlay");
} else if (service instanceof RokuService) {
imgResource = R.drawable.ic_dlna;
textServices.add("Roku");
} else if (service instanceof WebOSTVService) {
imgResource = R.drawable.ic_dlna;
textServices.add("webOS TV");
} else if (service instanceof NetcastTVService) {
imgResource = R.drawable.ic_dlna;
textServices.add("Netcast");
}
}
String serviceText;
if (textServices.size() == 0) {
serviceText = "Beaming Device";
} else {
serviceText = TextUtils.join(",", textServices);
}
holder.icon.setImageResource(imgResource);
holder.text1.setText(device.getFriendlyName());
holder.text2.setText(serviceText);
return convertView;
}
use of com.connectsdk.service.DeviceService in project butter-android by butterproject.
the class DefaultConnectableDeviceStore method updateDevice.
@Override
public void updateDevice(ConnectableDevice device) {
if (device == null || device.getServices().size() == 0)
return;
JSONObject storedDevice = getStoredDevice(device.getId());
if (storedDevice == null)
return;
try {
storedDevice.put(ConnectableDevice.KEY_LAST_IP, device.getLastKnownIPAddress());
storedDevice.put(ConnectableDevice.KEY_LAST_SEEN, device.getLastSeenOnWifi());
storedDevice.put(ConnectableDevice.KEY_LAST_CONNECTED, device.getLastConnected());
storedDevice.put(ConnectableDevice.KEY_LAST_DETECTED, device.getLastDetection());
JSONObject services = storedDevice.optJSONObject(ConnectableDevice.KEY_SERVICES);
if (services == null)
services = new JSONObject();
for (DeviceService service : device.getServices()) {
JSONObject serviceInfo = service.toJSONObject();
if (serviceInfo != null)
services.put(service.getServiceDescription().getUUID(), serviceInfo);
}
storedDevice.put(ConnectableDevice.KEY_SERVICES, services);
storedDevices.put(device.getId(), storedDevice);
activeDevices.put(device.getId(), device);
store();
} catch (JSONException e) {
e.printStackTrace();
}
}
use of com.connectsdk.service.DeviceService in project butter-android by butterproject.
the class DiscoveryManager method registerDeviceService.
/**
* Registers a DeviceService with DiscoveryManager and tells it which DiscoveryProvider to use to find it. Each DeviceService has a JSONObject of discovery parameters that its DiscoveryProvider will use to find it.
*
* @param deviceClass Class for object that should be instantiated when DeviceService is found
* @param discoveryClass Class for object that should discover this DeviceService. If a DiscoveryProvider of this class already exists, then the existing DiscoveryProvider will be used.
*/
public void registerDeviceService(Class<? extends DeviceService> deviceClass, Class<? extends DiscoveryProvider> discoveryClass) {
if (!DeviceService.class.isAssignableFrom(deviceClass))
return;
if (!DiscoveryProvider.class.isAssignableFrom(discoveryClass))
return;
try {
DiscoveryProvider discoveryProvider = null;
for (DiscoveryProvider dp : discoveryProviders) {
if (dp.getClass().isAssignableFrom(discoveryClass)) {
discoveryProvider = dp;
break;
}
}
if (discoveryProvider == null) {
Constructor<? extends DiscoveryProvider> myConstructor = discoveryClass.getConstructor(Context.class);
Object myObj = myConstructor.newInstance(context);
discoveryProvider = (DiscoveryProvider) myObj;
discoveryProvider.addListener(this);
discoveryProviders.add(discoveryProvider);
}
Method m = deviceClass.getMethod("discoveryFilter");
Object result = m.invoke(null);
DiscoveryFilter discoveryFilter = (DiscoveryFilter) result;
String serviceId = discoveryFilter.getServiceId();
deviceClasses.put(serviceId, deviceClass);
discoveryProvider.addDeviceFilter(discoveryFilter);
if (mSearching) {
discoveryProvider.restart();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
}
}
use of com.connectsdk.service.DeviceService in project butter-android by butterproject.
the class DiscoveryManager method addServiceDescriptionToDevice.
@SuppressWarnings("unchecked")
public void addServiceDescriptionToDevice(ServiceDescription desc, ConnectableDevice device) {
Log.d(Util.T, "Adding service " + desc.getServiceID() + " to device with address " + device.getIpAddress() + " and id " + device.getId());
Class<? extends DeviceService> deviceServiceClass = deviceClasses.get(desc.getServiceID());
if (deviceServiceClass == null)
return;
if (deviceServiceClass == DLNAService.class) {
if (desc.getLocationXML() == null)
return;
} else if (deviceServiceClass == NetcastTVService.class) {
if (!isNetcast(desc))
return;
}
ServiceConfig serviceConfig = null;
if (connectableDeviceStore != null)
serviceConfig = connectableDeviceStore.getServiceConfig(desc);
if (serviceConfig == null)
serviceConfig = new ServiceConfig(desc);
serviceConfig.setListener(DiscoveryManager.this);
boolean hasType = false;
boolean hasService = false;
for (DeviceService service : device.getServices()) {
if (service.getServiceDescription().getServiceID().equals(desc.getServiceID())) {
hasType = true;
if (service.getServiceDescription().getUUID().equals(desc.getUUID())) {
hasService = true;
}
break;
}
}
if (hasType) {
if (hasService) {
device.setServiceDescription(desc);
DeviceService alreadyAddedService = device.getServiceByName(desc.getServiceID());
if (alreadyAddedService != null)
alreadyAddedService.setServiceDescription(desc);
return;
}
device.removeServiceByName(desc.getServiceID());
}
DeviceService deviceService = DeviceService.getService(deviceServiceClass, desc, serviceConfig);
if (deviceService != null) {
deviceService.setServiceDescription(desc);
device.addService(deviceService);
}
}
use of com.connectsdk.service.DeviceService in project butter-android by butterproject.
the class DiscoveryManager method unregisterDeviceService.
/**
* Unregisters a DeviceService with DiscoveryManager. If no other DeviceServices are set to being discovered with the associated DiscoveryProvider, then that DiscoveryProvider instance will be stopped and shut down.
*
* @param deviceClass Class for DeviceService that should no longer be discovered
* @param discoveryClass Class for DiscoveryProvider that is discovering DeviceServices of deviceClass type
*/
public void unregisterDeviceService(Class<?> deviceClass, Class<?> discoveryClass) {
if (!DeviceService.class.isAssignableFrom(deviceClass)) {
return;
}
if (!DiscoveryProvider.class.isAssignableFrom(discoveryClass)) {
return;
}
try {
DiscoveryProvider discoveryProvider = null;
for (DiscoveryProvider dp : discoveryProviders) {
if (dp.getClass().isAssignableFrom(discoveryClass)) {
discoveryProvider = dp;
break;
}
}
if (discoveryProvider == null)
return;
Method m = deviceClass.getMethod("discoveryFilter");
Object result = m.invoke(null);
DiscoveryFilter discoveryFilter = (DiscoveryFilter) result;
String serviceId = discoveryFilter.getServiceId();
// do not remove provider if there is no such service
if (null == deviceClasses.remove(serviceId)) {
return;
}
discoveryProvider.removeDeviceFilter(discoveryFilter);
if (discoveryProvider.isEmpty()) {
discoveryProvider.stop();
discoveryProviders.remove(discoveryProvider);
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
Aggregations