Search in sources :

Example 11 with UsbDeviceConnection

use of android.hardware.usb.UsbDeviceConnection in project android_frameworks_base by AOSPA.

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);
}
Also used : UsbDeviceConnection(android.hardware.usb.UsbDeviceConnection) UsbDevice(android.hardware.usb.UsbDevice) MtpDevice(android.mtp.MtpDevice) IOException(java.io.IOException)

Example 12 with UsbDeviceConnection

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;
}
Also used : UsbDeviceConnection(android.hardware.usb.UsbDeviceConnection) UsbDevice(android.hardware.usb.UsbDevice) IOException(java.io.IOException)

Example 13 with UsbDeviceConnection

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);
}
Also used : UsbDeviceConnection(android.hardware.usb.UsbDeviceConnection) UsbDevice(android.hardware.usb.UsbDevice) MtpDevice(android.mtp.MtpDevice) IOException(java.io.IOException)

Example 14 with UsbDeviceConnection

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;
}
Also used : UsbDeviceConnection(android.hardware.usb.UsbDeviceConnection) UsbDevice(android.hardware.usb.UsbDevice) IOException(java.io.IOException)

Aggregations

UsbDeviceConnection (android.hardware.usb.UsbDeviceConnection)14 UsbDevice (android.hardware.usb.UsbDevice)10 IOException (java.io.IOException)10 MtpDevice (android.mtp.MtpDevice)5 PendingIntent (android.app.PendingIntent)4 Intent (android.content.Intent)4 UsbEndpoint (android.hardware.usb.UsbEndpoint)4 UsbInterface (android.hardware.usb.UsbInterface)4