/** * Attempt to construct an instance of URI from the given String. * * @param uriString the String representation of the possible URI. * @throws DataSourceException if the parameter is not a valid URI. */ public static URI createUri(String uriString) throws DataSourceException { if(uriString == null) { throw new DataSourceResourceException("URI string is null"); } try { return new URI(uriString); } catch(URISyntaxException e) { throw new DataSourceResourceException("Not a valid URI: " + uriString, e); } } }
/** * Return an integer vendor ID from a URI specifying a USB device. * * @param uri the USB device URI * @throws DataSourceResourceException If the URI doesn't match the * format usb://vendor_id/device_id */ public static int vendorFromUri(URI uri) throws DataSourceResourceException { try { return Integer.parseInt(uri.getAuthority(), 16); } catch(NumberFormatException e) { throw new DataSourceResourceException( "USB device must be of the format " + DEFAULT_USB_DEVICE_URI + " -- the given " + uri + " has a bad vendor ID"); } }
/** * Return an integer product ID from a URI specifying a USB device. * * @param uri the USB device URI * @throws DataSourceResourceException If the URI doesn't match the * format usb://vendor_id/device_id */ public static int productFromUri(URI uri) throws DataSourceResourceException { try { return Integer.parseInt(uri.getPath().substring(1), 16); } catch(NumberFormatException e) { throw new DataSourceResourceException( "USB device must be of the format " + DEFAULT_USB_DEVICE_URI + " -- the given " + uri + " has a bad product ID"); } catch(StringIndexOutOfBoundsException e) { throw new DataSourceResourceException( "USB device must be of the format " + DEFAULT_USB_DEVICE_URI + " -- the given " + uri + " has a bad product ID"); } } }
private void setUri(URI uri) throws DataSourceResourceException { if(uri == null || !UriBasedVehicleInterfaceMixin.validateResource(uri)) { throw new DataSourceResourceException("URI is not valid"); } mUri = uri; } }
private void setAddress(String address) throws DataSourceResourceException { if (address != null && !BluetoothAdapter.checkBluetoothAddress(address)) { throw new DataSourceResourceException("\"" + address + "\" is not a valid MAC address"); } mExplicitAddress = address; }
private UsbDevice findDevice(UsbManager manager, int vendorId, int productId) throws DataSourceResourceException { Log.d(TAG, "Looking for USB device with vendor ID " + vendorId + " and product ID " + productId); for(UsbDevice candidateDevice : manager.getDeviceList().values()) { if(candidateDevice.getVendorId() == vendorId && candidateDevice.getProductId() == productId) { Log.d(TAG, "Found USB device " + candidateDevice); return candidateDevice; } } throw new DataSourceResourceException("USB device with vendor " + "ID " + vendorId + " and product ID " + productId + " not found"); }
private static URI createUri(URI uri) throws DataSourceResourceException { if(uri == null || uri.toString().isEmpty()) { uri = UsbDeviceUtilities.DEFAULT_USB_DEVICE_URI; Log.i(TAG, "No USB device specified -- using default " + uri); } if(!validateResource(uri)) { throw new DataSourceResourceException( "USB device URI must have the usb:// scheme"); } // will throw an exception if not in the correct format UsbDeviceUtilities.vendorFromUri(uri); UsbDeviceUtilities.productFromUri(uri); return uri; }