Search in sources :

Example 21 with UsbDevice

use of android.hardware.usb.UsbDevice in project android_frameworks_base by DirtyUnicorns.

the class ReceiverActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(getIntent().getAction())) {
        final UsbDevice device = getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
        try {
            final MtpDocumentsProvider provider = MtpDocumentsProvider.getInstance();
            provider.openDevice(device.getDeviceId());
            final String deviceRootId = provider.getDeviceDocumentId(device.getDeviceId());
            final Uri uri = DocumentsContract.buildRootUri(MtpDocumentsProvider.AUTHORITY, deviceRootId);
            final Intent intent = new Intent(DocumentsContract.ACTION_BROWSE);
            intent.setData(uri);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.putExtra(DocumentsContract.EXTRA_FANCY_FEATURES, true);
            this.startActivity(intent);
        } catch (IOException exception) {
            Log.e(MtpDocumentsProvider.TAG, "Failed to open device", exception);
        }
    }
    finish();
}
Also used : UsbDevice(android.hardware.usb.UsbDevice) Intent(android.content.Intent) IOException(java.io.IOException) Uri(android.net.Uri)

Example 22 with UsbDevice

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

the class TestUtil method setupMtpDevice.

/**
     * Requests permission for a MTP device and returns the first MTP device that has at least one
     * storage.
     */
static UsbDevice setupMtpDevice(TestResultInstrumentation instrumentation, UsbManager usbManager, MtpManager manager) {
    while (true) {
        try {
            final UsbDevice device = findMtpDevice(usbManager, manager);
            waitForStorages(instrumentation, manager, device.getDeviceId());
            return device;
        } catch (IOException exp) {
            instrumentation.show(Objects.toString(exp.getMessage()));
            SystemClock.sleep(1000);
            // again.
            continue;
        }
    }
}
Also used : UsbDevice(android.hardware.usb.UsbDevice) IOException(java.io.IOException)

Example 23 with UsbDevice

use of android.hardware.usb.UsbDevice in project android_frameworks_base by crdroidandroid.

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 24 with UsbDevice

use of android.hardware.usb.UsbDevice in project android_frameworks_base by crdroidandroid.

the class TestUtil method setupMtpDevice.

/**
     * Requests permission for a MTP device and returns the first MTP device that has at least one
     * storage.
     */
static UsbDevice setupMtpDevice(TestResultInstrumentation instrumentation, UsbManager usbManager, MtpManager manager) {
    while (true) {
        try {
            final UsbDevice device = findMtpDevice(usbManager, manager);
            waitForStorages(instrumentation, manager, device.getDeviceId());
            return device;
        } catch (IOException exp) {
            instrumentation.show(Objects.toString(exp.getMessage()));
            SystemClock.sleep(1000);
            // again.
            continue;
        }
    }
}
Also used : UsbDevice(android.hardware.usb.UsbDevice) IOException(java.io.IOException)

Example 25 with UsbDevice

use of android.hardware.usb.UsbDevice in project android_frameworks_base by crdroidandroid.

the class ReceiverActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(getIntent().getAction())) {
        final UsbDevice device = getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
        try {
            final MtpDocumentsProvider provider = MtpDocumentsProvider.getInstance();
            provider.openDevice(device.getDeviceId());
            final String deviceRootId = provider.getDeviceDocumentId(device.getDeviceId());
            final Uri uri = DocumentsContract.buildRootUri(MtpDocumentsProvider.AUTHORITY, deviceRootId);
            final Intent intent = new Intent(DocumentsContract.ACTION_BROWSE);
            intent.setData(uri);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.putExtra(DocumentsContract.EXTRA_FANCY_FEATURES, true);
            this.startActivity(intent);
        } catch (IOException exception) {
            Log.e(MtpDocumentsProvider.TAG, "Failed to open device", exception);
        }
    }
    finish();
}
Also used : UsbDevice(android.hardware.usb.UsbDevice) Intent(android.content.Intent) IOException(java.io.IOException) Uri(android.net.Uri)

Aggregations

UsbDevice (android.hardware.usb.UsbDevice)49 IOException (java.io.IOException)20 UsbDeviceConnection (android.hardware.usb.UsbDeviceConnection)10 Intent (android.content.Intent)9 UsbEndpoint (android.hardware.usb.UsbEndpoint)5 UsbInterface (android.hardware.usb.UsbInterface)5 MtpDevice (android.mtp.MtpDevice)5 Uri (android.net.Uri)5 PendingIntent (android.app.PendingIntent)4 IntentFilter (android.content.IntentFilter)4 UsbConfiguration (android.hardware.usb.UsbConfiguration)4 MotionEvent (android.view.MotionEvent)4 SurfaceView (android.view.SurfaceView)4 View (android.view.View)4 TextView (android.widget.TextView)4 UsbManager (android.hardware.usb.UsbManager)1 Parcelable (android.os.Parcelable)1