/** * Retrieve the Class for a given name and construct an instance of it. * * @throws VehicleInterfaceException If the named interfaced could not be * found or if its constructor threw an exception. * @see #build(Class, Context, String) */ public static VehicleInterface build(String interfaceName, Context context, String resource) throws VehicleInterfaceException { return build(findClass(interfaceName), context, resource); }
@Override public VehicleInterfaceDescriptor getVehicleInterfaceDescriptor() { VehicleInterfaceDescriptor descriptor = null; synchronized(VehicleService.this) { if(mVehicleInterface != null) { descriptor = new VehicleInterfaceDescriptor(mVehicleInterface); } } return descriptor; }
private VehicleInterfaceDescriptor(Parcel in) { readFromParcel(in); } }
@Override public boolean setResource(String otherUri) throws DataSourceException { if(mDeviceUri == UsbDeviceUtilities.DEFAULT_USB_DEVICE_URI && otherUri != null && !UriBasedVehicleInterfaceMixin.sameResource(mDeviceUri, otherUri)) { mDeviceUri = createUri(otherUri); stop(); start(); return true; } return false; }
interfaceType = VehicleInterfaceFactory.findClass(interfaceName); } catch(VehicleInterfaceException e) { Log.w(TAG, "Unable to find VI matching " + interfaceName + interfaceType)))) { Log.i(TAG, "Disabling currently active VI " + mVehicleInterface); mVehicleInterface.stop(); mPipeline.removeSource(mVehicleInterface); mVehicleInterface = null; interfaceType)) { try { mVehicleInterface = VehicleInterfaceFactory.build( interfaceType, VehicleService.this, resource); } catch(VehicleInterfaceException e) { } else { try { if(mVehicleInterface.setResource(resource)) { Log.d(TAG, "Changed resource of already " + "active interface " + mVehicleInterface);
private void setUri(String uri) throws DataSourceException { setUri(UriBasedVehicleInterfaceMixin.createUri(massageUri(uri))); }
/** * Return true if the address and port are valid. * * @return true if the address and port are valid. */ public static boolean validateResource(String uriString) { try { URI uri = UriBasedVehicleInterfaceMixin.createUri(massageUri(uriString)); return UriBasedVehicleInterfaceMixin.validateResource(uri) && uri.getPort() < 65536; } catch(DataSourceException e) { return false; } }
@Override public boolean setResource(String otherResource) throws DataSourceException { if(!UriBasedVehicleInterfaceMixin.sameResource(mUri, massageUri(otherResource))) { setUri(otherResource); try { if(mSocket != null) { mSocket.close(); } } catch(IOException e) { } return true; } return false; }
public NetworkVehicleInterface(Context context, String uriString) throws DataSourceException { this(context, UriBasedVehicleInterfaceMixin.createUri( massageUri(uriString))); }
/** * Convert the parameter to a URI and validate the correctness of its host * and port. * * @return true if the address and port are valid. */ public static boolean validateResource(String uriString) { if(uriString == null) { return false; } try { return validateResource(createUri(uriString)); } catch(DataSourceException e) { Log.d(TAG, "URI is not valid", e); return false; } }
@Override public boolean send(VehicleMessage command) { command.untimestamp(); boolean sent = false; synchronized(VehicleService.this) { if(mVehicleInterface != null && mVehicleInterface.isConnected()) { try { mVehicleInterface.receive(command); Log.d(TAG, "Sent " + command + " using interface " + mVehicleInterface); sent = true; } catch(DataSinkException e) { Log.w(TAG, mVehicleInterface + " unable to send command", e); } } else { Log.w(TAG, "No connected VI available to send command"); } } return sent; }
@Override public synchronized void onPipelineActivated() { mWakeLocker.acquireWakeLock(); moveToForeground(); if(mVehicleInterface != null && mVehicleInterface.isConnected()) { VehicleInterfaceDescriptor descriptor = new VehicleInterfaceDescriptor(mVehicleInterface); synchronized(mViConnectionListeners) { int i = mViConnectionListeners.beginBroadcast(); while(i > 0) { i--; try { mViConnectionListeners.getBroadcastItem(i).onConnected(descriptor); } catch(RemoteException e) { Log.w(TAG, "Couldn't notify VI connection " + "listener -- did it crash?", e); } } mViConnectionListeners.finishBroadcast(); } } }
/** * Obtain the Class object for a given VehicleInterface class name. * * The class must be in the classpath of the process' context, or an * exception will be thrown. * * @param interfaceName the canonical name of class implementing * {@link VehicleInterface} * @return the Class object, if found. * @throws VehicleInterfaceException if the named class could not be found * or loaded. * */ public static Class<? extends VehicleInterface> findClass( String interfaceName) throws VehicleInterfaceException { Class<? extends VehicleInterface> interfaceType; try { interfaceType = Class.forName(interfaceName).asSubclass( VehicleInterface.class); } catch(ClassNotFoundException e) { throw new VehicleInterfaceException( "Couldn't find vehicle interface type " + interfaceName, e); } return interfaceType; }
public VehicleInterfaceDescriptor(VehicleInterface vi) { this(vi.getClass(), vi.isConnected()); }
/** * Determine if two URIs refer to the same resource. * * The function safely attempts to convert the otherResource parameter to a * URI object before comparing. * * @return true if the address and port match the current in-use values. */ public static boolean sameResource(URI uri, String otherResource) { try { return createUri(otherResource).equals(uri); } catch(DataSourceException e) { return false; } }
protected void readFromParcel(Parcel in) { mConnected = in.readInt() == 1; try { mInterfaceClass = VehicleInterfaceFactory.findClass( in.readString()); } catch(VehicleInterfaceException e) { Log.w(TAG, "Unable to load class for vehicle interface by name", e); } }
private void setUri(URI uri) throws DataSourceResourceException { if(uri == null || !UriBasedVehicleInterfaceMixin.validateResource(uri)) { throw new DataSourceResourceException("URI is not valid"); } mUri = uri; } }
private void setVisibility( Class<? extends VehicleDataSource> vehicleInterface, final View view, VehicleInterfaceDescriptor viDescriptor) { setVisibility(view, viDescriptor != null && viDescriptor.getInterfaceClass() == vehicleInterface); }
Context.class, String.class); } catch(NoSuchMethodException e) { throw new VehicleInterfaceException(interfaceType + " doesn't have a proper constructor", e); error = e; throw new VehicleInterfaceException(message, error);