Search in sources :

Example 1 with DeviceService

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;
}
Also used : NetcastTVService(com.connectsdk.service.NetcastTVService) ConnectableDevice(com.connectsdk.device.ConnectableDevice) AirPlayService(com.connectsdk.service.AirPlayService) CastService(com.connectsdk.service.CastService) DeviceService(com.connectsdk.service.DeviceService) ArrayList(java.util.ArrayList) WebOSTVService(com.connectsdk.service.WebOSTVService) DLNAService(com.connectsdk.service.DLNAService) RokuService(com.connectsdk.service.RokuService)

Example 2 with DeviceService

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();
    }
}
Also used : JSONObject(org.json.JSONObject) DeviceService(com.connectsdk.service.DeviceService) JSONException(org.json.JSONException)

Example 3 with DeviceService

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();
    }
}
Also used : DeviceService(com.connectsdk.service.DeviceService) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with DeviceService

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);
    }
}
Also used : NetcastTVService(com.connectsdk.service.NetcastTVService) ServiceConfig(com.connectsdk.service.config.ServiceConfig) DeviceService(com.connectsdk.service.DeviceService)

Example 5 with 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();
    }
}
Also used : DeviceService(com.connectsdk.service.DeviceService) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

DeviceService (com.connectsdk.service.DeviceService)11 NetcastTVService (com.connectsdk.service.NetcastTVService)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 EditText (android.widget.EditText)1 TextView (android.widget.TextView)1 ConnectableDevice (com.connectsdk.device.ConnectableDevice)1 AirPlayService (com.connectsdk.service.AirPlayService)1 CastService (com.connectsdk.service.CastService)1 DLNAService (com.connectsdk.service.DLNAService)1 RokuService (com.connectsdk.service.RokuService)1 WebOSTVService (com.connectsdk.service.WebOSTVService)1 CapabilityPriorityLevel (com.connectsdk.service.capability.CapabilityMethods.CapabilityPriorityLevel)1 ServiceConfig (com.connectsdk.service.config.ServiceConfig)1 ArrayList (java.util.ArrayList)1