use of android.hardware.usb.UsbDeviceConnection in project android_frameworks_base by AOSPA.
the class TestUtil method findMtpDevice.
private static UsbDevice findMtpDevice(UsbManager usbManager, MtpManager manager) throws IOException {
final HashMap<String, UsbDevice> devices = usbManager.getDeviceList();
if (devices.size() == 0) {
throw new IOException("Device not found.");
}
final UsbDevice device = devices.values().iterator().next();
// Tries to get ownership of the device in case that another application use it.
if (usbManager.hasPermission(device)) {
final UsbDeviceConnection connection = usbManager.openDevice(device);
for (int i = 0; i < device.getInterfaceCount(); i++) {
// Since the test runs real environment, we need to call claim interface with
// force = true to rob interfaces from other applications.
connection.claimInterface(device.getInterface(i), true);
connection.releaseInterface(device.getInterface(i));
}
connection.close();
}
manager.openDevice(device.getDeviceId());
return device;
}
use of android.hardware.usb.UsbDeviceConnection in project android_frameworks_base by ResurrectionRemix.
the class MtpManager method openDevice.
synchronized MtpDeviceRecord openDevice(int deviceId) throws IOException {
UsbDevice rawDevice = null;
for (final UsbDevice candidate : mManager.getDeviceList().values()) {
if (candidate.getDeviceId() == deviceId) {
rawDevice = candidate;
break;
}
}
ensureNotNull(rawDevice, "Not found USB device: " + deviceId);
if (!mManager.hasPermission(rawDevice)) {
mManager.grantPermission(rawDevice);
if (!mManager.hasPermission(rawDevice)) {
throw new IOException("Failed to grant a device permission.");
}
}
final MtpDevice device = new MtpDevice(rawDevice);
final UsbDeviceConnection connection = ensureNotNull(mManager.openDevice(rawDevice), "Failed to open a USB connection.");
if (!device.open(connection)) {
// We cannot open connection when another application use the device.
throw new BusyDeviceException();
}
// Handle devices that fail to obtain storages just after opening a MTP session.
final int[] storageIds = ensureNotNull(device.getStorageIds(), "Not found MTP storages in the device.");
mDevices.put(deviceId, device);
return createDeviceRecord(rawDevice);
}
use of android.hardware.usb.UsbDeviceConnection in project android_frameworks_base by crdroidandroid.
the class TestUtil method findMtpDevice.
private static UsbDevice findMtpDevice(UsbManager usbManager, MtpManager manager) throws IOException {
final HashMap<String, UsbDevice> devices = usbManager.getDeviceList();
if (devices.size() == 0) {
throw new IOException("Device not found.");
}
final UsbDevice device = devices.values().iterator().next();
// Tries to get ownership of the device in case that another application use it.
if (usbManager.hasPermission(device)) {
final UsbDeviceConnection connection = usbManager.openDevice(device);
for (int i = 0; i < device.getInterfaceCount(); i++) {
// Since the test runs real environment, we need to call claim interface with
// force = true to rob interfaces from other applications.
connection.claimInterface(device.getInterface(i), true);
connection.releaseInterface(device.getInterface(i));
}
connection.close();
}
manager.openDevice(device.getDeviceId());
return device;
}
use of android.hardware.usb.UsbDeviceConnection in project AndrOBD by fr3ts0n.
the class UsbCommService method start.
@Override
public void start() {
if (sPort != null) {
final UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
UsbDeviceConnection connection = usbManager.openDevice(sPort.getDriver().getDevice());
if (connection == null) {
connectionFailed();
return;
}
try {
sPort.open(connection);
sPort.setDTR(true);
sPort.setParameters(38400, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
log.info("Starting io manager ..");
Thread runner = new Thread(mSerialIoManager);
runner.setPriority(Thread.MAX_PRIORITY);
runner.start();
// we are connected -> signal connectionEstablished
connectionEstablished(sPort.toString());
} catch (IOException e) {
log.log(Level.SEVERE, "Error setting up device: " + e.getMessage(), e);
try {
sPort.close();
} catch (IOException e2) {
// Ignore.
}
connectionFailed();
sPort = null;
}
}
}
use of android.hardware.usb.UsbDeviceConnection in project remote-desktop-clients by iiordanov.
the class SpiceCommunicator method openUsbDevice.
/* Callbacks from jni and corresponding non-static methods */
public static int openUsbDevice(int vid, int pid) throws InterruptedException {
Log.i(TAG, "Attempting to open a USB device and return a file descriptor.");
if (!myself.usbEnabled || android.os.Build.VERSION.SDK_INT < 12) {
return -1;
}
String mapKey = Integer.toString(vid) + ":" + Integer.toString(pid);
myself.deviceToFdMap.put(mapKey, 0);
boolean deviceFound = false;
UsbDevice device = null;
HashMap<String, UsbDevice> stringDeviceMap = null;
int timeout = Constants.usbDeviceTimeout;
while (!deviceFound && timeout > 0) {
stringDeviceMap = myself.mUsbManager.getDeviceList();
Collection<UsbDevice> usbDevices = stringDeviceMap.values();
Iterator<UsbDevice> usbDeviceIter = usbDevices.iterator();
while (usbDeviceIter.hasNext()) {
UsbDevice ud = usbDeviceIter.next();
Log.i(TAG, "DEVICE: " + ud.toString());
if (ud.getVendorId() == vid && ud.getProductId() == pid) {
Log.i(TAG, "USB device successfully matched.");
deviceFound = true;
device = ud;
break;
}
}
timeout -= 100;
SystemClock.sleep(100);
}
int fd = -1;
// we request permission and wait for it to be granted or denied, or for a timeout to occur.
if (device != null) {
UsbDeviceConnection deviceConnection = myself.mUsbManager.openDevice(device);
if (deviceConnection != null) {
fd = deviceConnection.getFileDescriptor();
} else {
// Request permission to access the device.
synchronized (myself.deviceToFdMap.get(mapKey)) {
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(myself.context, 0, new Intent(Constants.ACTION_USB_PERMISSION), 0);
// TODO: Try putting this intent filter into the activity in the manifest file.
IntentFilter filter = new IntentFilter(Constants.ACTION_USB_PERMISSION);
myself.context.registerReceiver(myself.mUsbReceiver, filter);
myself.mUsbManager.requestPermission(device, mPermissionIntent);
// Wait for permission with a timeout.
myself.deviceToFdMap.get(mapKey).wait(Constants.usbDevicePermissionTimeout);
deviceConnection = myself.mUsbManager.openDevice(device);
if (deviceConnection != null) {
fd = deviceConnection.getFileDescriptor();
}
}
}
}
return fd;
}
Aggregations