/** * Checks whether a given USB device equals the device wrapped by this instance; useful for * checking whether {@link UsbBroadcastHandler} callbacks are for a particular instance of * UsbDeviceWithInfo. * * @param otherDevice to compare this device to * @return true if otherDevice equals the USB device wrapped by this instance */ public boolean matches(UsbDevice otherDevice) { return this.device.equals(otherDevice); }
@Override public void onReceive(Context context, Intent intent) { if (webcam == null) { return; } final UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (usbDevice.equals(webcam.getDevice())) { Timber.d("Active Webcam detached. Terminating connection."); stopStreaming(); } } };
/** * Get MIDI output device for specified UsbDevice, if available. * * @param usbDevice the UsbDevice * @return {@link Set<MidiOutputDevice>} */ @NonNull public final Set<MidiOutputDevice> getMidiOutputDevices(@NonNull UsbDevice usbDevice) { if (deviceConnectionWatcher != null) { deviceConnectionWatcher.checkConnectedDevicesImmediately(); } Set<MidiOutputDevice> result = new HashSet<>(); for (MidiOutputDevice midiOutputDevice : midiOutputDevices) { if (midiOutputDevice.getUsbDevice().equals(usbDevice)) { result.add(midiOutputDevice); } } return Collections.unmodifiableSet(result); }
private void detachDevice(UsbDevice usbDevice) { for (Map.Entry<String, UsbPartition> root : mRoots.entrySet()) { if (root.getValue().device.equals(usbDevice)) { Log.d(TAG, "remove rootId " + root.getKey()); mRoots.remove(root.getKey()); mFileCache.evictAll(); notifyRootsChanged(); break; } } }
@Override public void onReceive(Context context, Intent intent) { UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (isConnected() && BaseUsbConnection.this.usbDevice.equals(usbDevice)) { logger.log("WALT was detached"); disconnect(); } } };
if (device.equals(grantingDevice)) {
@Override public void onReceive(Context context, Intent intent) { if (usbDevice == null) { logger.log("USB device was not properly opened"); return; } if(intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false) && usbDevice.equals(intent.getParcelableExtra(UsbManager.EXTRA_DEVICE))){ usbConnection = usbManager.openDevice(usbDevice); BaseUsbConnection.this.context.registerReceiver(disconnectReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED)); onConnect(); broadcastManager.sendBroadcast(new Intent(getConnectIntent())); } else { logger.log("Could not get permission to open the USB device"); } BaseUsbConnection.this.context.unregisterReceiver(respondToUsbPermission); } };
private void handleUsbDeviceDetached(Context context, UsbDevice usbDevice) { Iterator<Map.Entry<Integer, CardDevice>> it = cardDevices.entrySet().iterator(); while (it.hasNext()) { final CardDevice cardDevice = it.next().getValue(); if (!(cardDevice instanceof UsbCardDevice)) { continue; } UsbCardDevice usbCardDevice = (UsbCardDevice) cardDevice; if (!usbCardDevice.getUsbDevice().equals(usbDevice)) { continue; } it.remove(); usbCardDevice.close(); Intent broadcastIntent = new Intent(ACTION_UPDATE); broadcastIntent.putExtra(EXTRA_DEVICE_WAS_ADDED, false); broadcastIntent.putExtra(EXTRA_DEVICE_NAME, cardDevice.getClass().getAnnotation(UsbCardDevice.Metadata.class).name()); LocalBroadcastManager.getInstance(context).sendBroadcast(broadcastIntent); new Handler(context.getMainLooper()).post(new Runnable() { @Override public void run() { // noinspection StatementWithEmptyBody while (cardDevices.values().remove(cardDevice)) { } } }); break; } seenUsbDevices.remove(usbDevice); }
private void discoverDevice(UsbDevice device) { for (UsbMassStorageDevice massStorageDevice : UsbMassStorageDevice.getMassStorageDevices(getContext())) { if (device.equals(massStorageDevice.getUsbDevice())) { if (hasPermission(device)) { addRoot(massStorageDevice); } else { requestPermission(device); } } } }