Search in sources :

Example 41 with MtpObjectInfo

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

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)

Example 42 with MtpObjectInfo

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

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

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

the class DocumentLoaderTest method testError_GetObjectInfo.

public void testError_GetObjectInfo() throws Exception {
    mManager = new BlockableTestMtpManager(getContext()) {

        @Override
        MtpObjectInfo getObjectInfo(int deviceId, int objectHandle) throws IOException {
            if (objectHandle == DocumentLoader.NUM_INITIAL_ENTRIES) {
                throw new IOException();
            } else {
                return super.getObjectInfo(deviceId, objectHandle);
            }
        }
    };
    setUpLoader();
    setUpDocument(mManager, DocumentLoader.NUM_INITIAL_ENTRIES);
    try (final Cursor cursor = mLoader.queryChildDocuments(MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION, mParentIdentifier)) {
        // Even if MtpManager returns an error for a document, loading must complete.
        assertFalse(cursor.getExtras().getBoolean(DocumentsContract.EXTRA_LOADING));
    }
}
Also used : IOException(java.io.IOException) Cursor(android.database.Cursor) MtpObjectInfo(android.mtp.MtpObjectInfo)

Example 44 with MtpObjectInfo

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

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

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

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