Search in sources :

Example 36 with MtpObjectInfo

use of android.mtp.MtpObjectInfo in project android_frameworks_base by AOSPA.

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 37 with MtpObjectInfo

use of android.mtp.MtpObjectInfo in project android_frameworks_base by AOSPA.

the class TestMtpManager method createDocument.

@Override
int createDocument(int deviceId, MtpObjectInfo objectInfo, ParcelFileDescriptor source) throws IOException {
    Assert.assertNotSame(0, objectInfo.getStorageId());
    Assert.assertNotSame(-1, objectInfo.getStorageId());
    Assert.assertNotSame(0, objectInfo.getParent());
    final String key = pack(deviceId, CREATED_DOCUMENT_HANDLE);
    if (mObjectInfos.containsKey(key)) {
        throw new IOException();
    }
    final MtpObjectInfo newInfo = new MtpObjectInfo.Builder(objectInfo).setObjectHandle(CREATED_DOCUMENT_HANDLE).build();
    mObjectInfos.put(key, newInfo);
    if (objectInfo.getFormat() != 0x3001) {
        try (final ParcelFileDescriptor.AutoCloseInputStream inputStream = new ParcelFileDescriptor.AutoCloseInputStream(source)) {
            final byte[] buffer = new byte[objectInfo.getCompressedSize()];
            if (inputStream.read(buffer, 0, objectInfo.getCompressedSize()) != objectInfo.getCompressedSize()) {
                throw new IOException();
            }
            mImportFileBytes.put(pack(deviceId, CREATED_DOCUMENT_HANDLE), buffer);
        }
    }
    return CREATED_DOCUMENT_HANDLE;
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException) MtpObjectInfo(android.mtp.MtpObjectInfo)

Example 38 with MtpObjectInfo

use of android.mtp.MtpObjectInfo in project android_frameworks_base by AOSPA.

the class MtpDocumentsProviderTest method setupDocuments.

private String[] setupDocuments(int deviceId, int storageId, int parentHandle, String parentDocumentId, MtpObjectInfo[] objects) throws FileNotFoundException {
    final int[] handles = new int[objects.length];
    int i = 0;
    for (final MtpObjectInfo info : objects) {
        handles[i] = info.getObjectHandle();
        mMtpManager.setObjectInfo(deviceId, info);
    }
    mMtpManager.setObjectHandles(deviceId, storageId, parentHandle, handles);
    return getStrings(mProvider.queryChildDocuments(parentDocumentId, strings(DocumentsContract.Document.COLUMN_DOCUMENT_ID), null));
}
Also used : MtpObjectInfo(android.mtp.MtpObjectInfo)

Example 39 with MtpObjectInfo

use of android.mtp.MtpObjectInfo in project android_frameworks_base by AOSPA.

the class MtpDocumentsProviderTest method testOpenDocument_shortBytes.

public void testOpenDocument_shortBytes() throws Exception {
    mMtpManager = new TestMtpManager(getContext()) {

        @Override
        MtpObjectInfo getObjectInfo(int deviceId, int objectHandle) throws IOException {
            if (objectHandle == 1) {
                return new MtpObjectInfo.Builder(super.getObjectInfo(deviceId, objectHandle)).setObjectHandle(1).setCompressedSize(1024 * 1024).build();
            }
            return super.getObjectInfo(deviceId, objectHandle);
        }
    };
    setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY);
    setupRoots(0, new MtpRoot[] { new MtpRoot(0, 0, "Storage", 0, 0, "") });
    final byte[] bytes = "Hello world".getBytes();
    setupDocuments(0, 0, MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, "1", new MtpObjectInfo[] { new MtpObjectInfo.Builder().setName("test.txt").setObjectHandle(1).setCompressedSize(bytes.length).setParent(-1).build() });
    mMtpManager.setImportFileBytes(0, 1, bytes);
    try (final ParcelFileDescriptor fd = mProvider.openDocument("3", "r", null)) {
        final byte[] readBytes = new byte[1024 * 1024];
        assertEquals(11, Os.read(fd.getFileDescriptor(), readBytes, 0, readBytes.length));
    }
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException) MtpObjectInfo(android.mtp.MtpObjectInfo)

Example 40 with MtpObjectInfo

use of android.mtp.MtpObjectInfo in project android_frameworks_base by AOSPA.

the class MtpFileWriter method flush.

void flush(MtpManager manager, MtpDatabase database, int[] operationsSupported) throws IOException, ErrnoException {
    // Skip unnecessary flush.
    if (!mDirty) {
        return;
    }
    // Get the placeholder object info.
    final Identifier identifier = database.createIdentifier(mDocumentId);
    final MtpObjectInfo placeholderObjectInfo = manager.getObjectInfo(identifier.mDeviceId, identifier.mObjectHandle);
    // Delete the target object info if it already exists (as a placeholder).
    manager.deleteDocument(identifier.mDeviceId, identifier.mObjectHandle);
    // Create the target object info with a correct file size and upload the file.
    final long size = Os.lseek(mCacheFd.getFileDescriptor(), 0, OsConstants.SEEK_END);
    final MtpObjectInfo targetObjectInfo = new MtpObjectInfo.Builder(placeholderObjectInfo).setCompressedSize(size).build();
    Os.lseek(mCacheFd.getFileDescriptor(), 0, OsConstants.SEEK_SET);
    final int newObjectHandle = manager.createDocument(identifier.mDeviceId, targetObjectInfo, mCacheFd);
    final MtpObjectInfo newObjectInfo = manager.getObjectInfo(identifier.mDeviceId, newObjectHandle);
    final Identifier parentIdentifier = database.getParentIdentifier(identifier.mDocumentId);
    database.updateObject(identifier.mDocumentId, identifier.mDeviceId, parentIdentifier.mDocumentId, operationsSupported, newObjectInfo, size);
    mDirty = false;
}
Also used : MtpObjectInfo(android.mtp.MtpObjectInfo)

Aggregations

MtpObjectInfo (android.mtp.MtpObjectInfo)53 IOException (java.io.IOException)20 ParcelFileDescriptor (android.os.ParcelFileDescriptor)15 Cursor (android.database.Cursor)10 Intent (android.content.Intent)6 Bitmap (android.graphics.Bitmap)6 MtpDevice (android.mtp.MtpDevice)6 ImageView (android.widget.ImageView)6 TextView (android.widget.TextView)6 ArrayList (java.util.ArrayList)6 Date (java.util.Date)6 SQLiteDiskIOException (android.database.sqlite.SQLiteDiskIOException)5 Point (android.graphics.Point)5 FileNotFoundException (java.io.FileNotFoundException)5