Search in sources :

Example 6 with MtpDevice

use of android.mtp.MtpDevice in project android_frameworks_base by ResurrectionRemix.

the class MtpClient method getObjectList.

/**
     * Retrieves a list of {@link android.mtp.MtpObjectInfo} for all objects
     * on the MTP or PTP device with the given USB device name and given storage ID
     * and/or object handle.
     * If the object handle is zero, then all objects in the root of the storage unit
     * will be returned. Otherwise, all immediate children of the object will be returned.
     * If the storage ID is also zero, then all objects on all storage units will be returned.
     *
     * @param deviceName the name of the USB device
     * @param storageId the ID of the storage unit to query, or zero for all
     * @param objectHandle the handle of the parent object to query, or zero for the storage root
     * @return the list of MtpObjectInfo
     */
public List<MtpObjectInfo> getObjectList(String deviceName, int storageId, int objectHandle) {
    MtpDevice device = getDevice(deviceName);
    if (device == null) {
        return null;
    }
    if (objectHandle == 0) {
        // all objects in root of storage
        objectHandle = 0xFFFFFFFF;
    }
    int[] handles = device.getObjectHandles(storageId, 0, objectHandle);
    if (handles == null) {
        return null;
    }
    int length = handles.length;
    ArrayList<MtpObjectInfo> objectList = new ArrayList<MtpObjectInfo>(length);
    for (int i = 0; i < length; i++) {
        MtpObjectInfo info = device.getObjectInfo(handles[i]);
        if (info == null) {
            Log.w(TAG, "getObjectInfo failed");
        } else {
            objectList.add(info);
        }
    }
    return objectList;
}
Also used : MtpDevice(android.mtp.MtpDevice) ArrayList(java.util.ArrayList) MtpObjectInfo(android.mtp.MtpObjectInfo)

Example 7 with MtpDevice

use of android.mtp.MtpDevice in project android_frameworks_base by ResurrectionRemix.

the class MtpClient method getStorageList.

/**
     * Retrieves a list of all {@link android.mtp.MtpStorageInfo}
     * for the MTP or PTP device with the given USB device name
     *
     * @param deviceName the name of the USB device
     * @return the list of MtpStorageInfo
     */
public List<MtpStorageInfo> getStorageList(String deviceName) {
    MtpDevice device = getDevice(deviceName);
    if (device == null) {
        return null;
    }
    int[] storageIds = device.getStorageIds();
    if (storageIds == null) {
        return null;
    }
    int length = storageIds.length;
    ArrayList<MtpStorageInfo> storageList = new ArrayList<MtpStorageInfo>(length);
    for (int i = 0; i < length; i++) {
        MtpStorageInfo info = device.getStorageInfo(storageIds[i]);
        if (info == null) {
            Log.w(TAG, "getStorageInfo failed");
        } else {
            storageList.add(info);
        }
    }
    return storageList;
}
Also used : MtpDevice(android.mtp.MtpDevice) ArrayList(java.util.ArrayList) MtpStorageInfo(android.mtp.MtpStorageInfo)

Example 8 with MtpDevice

use of android.mtp.MtpDevice in project android_frameworks_base by ResurrectionRemix.

the class MtpManager method getRoots.

private MtpRoot[] getRoots(int deviceId) throws IOException {
    final MtpDevice device = getDevice(deviceId);
    synchronized (device) {
        final int[] storageIds = ensureNotNull(device.getStorageIds(), "Failed to obtain storage IDs.");
        final ArrayList<MtpRoot> roots = new ArrayList<>();
        for (int i = 0; i < storageIds.length; i++) {
            final MtpStorageInfo info = device.getStorageInfo(storageIds[i]);
            if (info == null) {
                continue;
            }
            roots.add(new MtpRoot(device.getDeviceId(), info));
        }
        return roots.toArray(new MtpRoot[roots.size()]);
    }
}
Also used : MtpDevice(android.mtp.MtpDevice) ArrayList(java.util.ArrayList) MtpStorageInfo(android.mtp.MtpStorageInfo)

Example 9 with MtpDevice

use of android.mtp.MtpDevice in project android_frameworks_base by DirtyUnicorns.

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 10 with MtpDevice

use of android.mtp.MtpDevice in project android_frameworks_base by DirtyUnicorns.

the class MtpClient method getObjectList.

/**
     * Retrieves a list of {@link android.mtp.MtpObjectInfo} for all objects
     * on the MTP or PTP device with the given USB device name and given storage ID
     * and/or object handle.
     * If the object handle is zero, then all objects in the root of the storage unit
     * will be returned. Otherwise, all immediate children of the object will be returned.
     * If the storage ID is also zero, then all objects on all storage units will be returned.
     *
     * @param deviceName the name of the USB device
     * @param storageId the ID of the storage unit to query, or zero for all
     * @param objectHandle the handle of the parent object to query, or zero for the storage root
     * @return the list of MtpObjectInfo
     */
public List<MtpObjectInfo> getObjectList(String deviceName, int storageId, int objectHandle) {
    MtpDevice device = getDevice(deviceName);
    if (device == null) {
        return null;
    }
    if (objectHandle == 0) {
        // all objects in root of storage
        objectHandle = 0xFFFFFFFF;
    }
    int[] handles = device.getObjectHandles(storageId, 0, objectHandle);
    if (handles == null) {
        return null;
    }
    int length = handles.length;
    ArrayList<MtpObjectInfo> objectList = new ArrayList<MtpObjectInfo>(length);
    for (int i = 0; i < length; i++) {
        MtpObjectInfo info = device.getObjectInfo(handles[i]);
        if (info == null) {
            Log.w(TAG, "getObjectInfo failed");
        } else {
            objectList.add(info);
        }
    }
    return objectList;
}
Also used : MtpDevice(android.mtp.MtpDevice) ArrayList(java.util.ArrayList) MtpObjectInfo(android.mtp.MtpObjectInfo)

Aggregations

MtpDevice (android.mtp.MtpDevice)27 ArrayList (java.util.ArrayList)17 MtpStorageInfo (android.mtp.MtpStorageInfo)11 IOException (java.io.IOException)10 MtpObjectInfo (android.mtp.MtpObjectInfo)6 UsbDevice (android.hardware.usb.UsbDevice)5 UsbDeviceConnection (android.hardware.usb.UsbDeviceConnection)5 MtpDeviceInfo (android.mtp.MtpDeviceInfo)5