Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 88d2f781 authored by Daichi Hirono's avatar Daichi Hirono
Browse files

Remove fullpath from MtpDatabase.

If the remote MTP device is backed by typical file system, the file name
is unique among files in a directory. However, MTP protocol itself does
not guarantee the uniqueness of name so we cannot use fullpath as ID.

Instead of fullpath, we use artifical ID generated by MtpDatabase
itself. So we don't need to store fullpath in the database.

BUG=25162822

Change-Id: I06598ce631a3221ed72e11734dbdaefef4c6349c
parent b999b0cb
Loading
Loading
Loading
Loading
+5 −11
Original line number Diff line number Diff line
@@ -30,10 +30,9 @@ class MtpDatabase {

    private static final String TABLE_MTP_DOCUMENTS = "MtpDocuments";

    static final String COLUMN_DEVICE_ID = "deviceId";
    static final String COLUMN_STORAGE_ID = "storageId";
    static final String COLUMN_OBJECT_HANDLE = "objectHandle";
    static final String COLUMN_FULL_PATH = "fullPath";
    static final String COLUMN_DEVICE_ID = "device_id";
    static final String COLUMN_STORAGE_ID = "storage_id";
    static final String COLUMN_OBJECT_HANDLE = "object_handle";

    private static class OpenHelper extends SQLiteOpenHelper {
        private static final String CREATE_TABLE_QUERY =
@@ -43,7 +42,6 @@ class MtpDatabase {
                COLUMN_DEVICE_ID + " INTEGER NOT NULL," +
                COLUMN_STORAGE_ID + " INTEGER NOT NULL," +
                COLUMN_OBJECT_HANDLE + " INTEGER," +
                COLUMN_FULL_PATH + " TEXT NOT NULL," +
                DocumentsContract.Document.COLUMN_MIME_TYPE + " TEXT," +
                DocumentsContract.Document.COLUMN_DISPLAY_NAME + " TEXT NOT NULL," +
                DocumentsContract.Document.COLUMN_SUMMARY + " TEXT," +
@@ -93,8 +91,6 @@ class MtpDatabase {
            values.put(COLUMN_DEVICE_ID, root.mDeviceId);
            values.put(COLUMN_STORAGE_ID, root.mStorageId);
            values.putNull(COLUMN_OBJECT_HANDLE);
            values.put(
                    COLUMN_FULL_PATH, "/" + root.mDeviceId + "/" + escape(root.mDescription));
            values.put(Document.COLUMN_MIME_TYPE, DocumentsContract.Document.MIME_TYPE_DIR);
            values.put(Document.COLUMN_DISPLAY_NAME, root.mDescription);
            values.putNull(Document.COLUMN_SUMMARY);
@@ -113,7 +109,7 @@ class MtpDatabase {
    }

    @VisibleForTesting
    void putDocument(int deviceId, String parentFullPath, MtpObjectInfo info) throws Exception {
    void putDocument(int deviceId, MtpObjectInfo info) throws Exception {
        database.beginTransaction();
        try {
            final String mimeType = CursorHelper.formatTypeToMimeType(info.getFormat());
@@ -134,9 +130,7 @@ class MtpDatabase {
            values.put(COLUMN_DEVICE_ID, deviceId);
            values.put(COLUMN_STORAGE_ID, info.getStorageId());
            values.put(COLUMN_OBJECT_HANDLE, info.getObjectHandle());
            values.put(COLUMN_FULL_PATH, parentFullPath + "/" + escape(info.getName()));
            values.put(
                    Document.COLUMN_MIME_TYPE, CursorHelper.formatTypeToMimeType(info.getFormat()));
            values.put(Document.COLUMN_MIME_TYPE, mimeType);
            values.put(Document.COLUMN_DISPLAY_NAME, info.getName());
            values.putNull(Document.COLUMN_SUMMARY);
            values.put(
+19 −21
Original line number Diff line number Diff line
package com.android.mtp;


import android.database.Cursor;
import android.mtp.MtpConstants;
import android.mtp.MtpObjectInfo;
@@ -15,7 +14,6 @@ public class MtpDatabaseTest extends AndroidTestCase {
        MtpDatabase.COLUMN_DEVICE_ID,
        MtpDatabase.COLUMN_STORAGE_ID,
        MtpDatabase.COLUMN_OBJECT_HANDLE,
        MtpDatabase.COLUMN_FULL_PATH,
        DocumentsContract.Document.COLUMN_MIME_TYPE,
        DocumentsContract.Document.COLUMN_DISPLAY_NAME,
        DocumentsContract.Document.COLUMN_SUMMARY,
@@ -70,22 +68,23 @@ public class MtpDatabaseTest extends AndroidTestCase {
        assertEquals("deviceId", 0, cursor.getInt(1));
        assertEquals("storageId", 1, cursor.getInt(2));
        assertTrue("objectHandle", cursor.isNull(3));
        assertEquals("fullPath", "/0/Storage", cursor.getString(4));
        assertEquals("mimeType", DocumentsContract.Document.MIME_TYPE_DIR, cursor.getString(5));
        assertEquals("displayName", "Storage", cursor.getString(6));
        assertTrue("summary", cursor.isNull(7));
        assertTrue("lastModified", cursor.isNull(8));
        assertTrue("icon", cursor.isNull(9));
        assertEquals("flag", 0, cursor.getInt(10));
        assertEquals("size", 1000, cursor.getInt(11));
        assertEquals("mimeType", DocumentsContract.Document.MIME_TYPE_DIR, cursor.getString(4));
        assertEquals("displayName", "Storage", cursor.getString(5));
        assertTrue("summary", cursor.isNull(6));
        assertTrue("lastModified", cursor.isNull(7));
        assertTrue("icon", cursor.isNull(8));
        assertEquals("flag", 0, cursor.getInt(9));
        assertEquals("size", 1000, cursor.getInt(10));

        cursor.moveToNext();
        assertEquals("documentId", 2, cursor.getInt(0));
        assertEquals("fullPath", "/0/Storage", cursor.getString(4));
        assertEquals("displayName", "Storage", cursor.getString(5));

        cursor.moveToNext();
        assertEquals("documentId", 3, cursor.getInt(0));
        assertEquals("fullPath", "/0/%2F%40%23%25%26%3C%3EStorage", cursor.getString(4));
        assertEquals("displayName", "/@#%&<>Storage", cursor.getString(5));

        cursor.close();
    }

    public void testPutDocument() throws Exception {
@@ -96,7 +95,7 @@ public class MtpDatabaseTest extends AndroidTestCase {
        builder.setStorageId(5);
        builder.setFormat(MtpConstants.FORMAT_TEXT);
        builder.setCompressedSize(1000);
        database.putDocument(0, "/0/Storage", builder.build());
        database.putDocument(0, builder.build());

        final Cursor cursor = database.queryChildDocuments(COLUMN_NAMES);
        assertEquals(1, cursor.getCount());
@@ -105,17 +104,16 @@ public class MtpDatabaseTest extends AndroidTestCase {
        assertEquals("deviceId", 0, cursor.getInt(1));
        assertEquals("storageId", 5, cursor.getInt(2));
        assertEquals("objectHandle", 100, cursor.getInt(3));
        assertEquals("fullPath", "/0/Storage/test.txt", cursor.getString(4));
        assertEquals("mimeType", "text/plain", cursor.getString(5));
        assertEquals("displayName", "test.txt", cursor.getString(6));
        assertTrue("summary", cursor.isNull(7));
        assertTrue("lastModified", cursor.isNull(8));
        assertTrue("icon", cursor.isNull(9));
        assertEquals("mimeType", "text/plain", cursor.getString(4));
        assertEquals("displayName", "test.txt", cursor.getString(5));
        assertTrue("summary", cursor.isNull(6));
        assertTrue("lastModified", cursor.isNull(7));
        assertTrue("icon", cursor.isNull(8));
        assertEquals(
                "flag",
                DocumentsContract.Document.FLAG_SUPPORTS_DELETE |
                DocumentsContract.Document.FLAG_SUPPORTS_WRITE,
                cursor.getInt(10));
        assertEquals("size", 1000, cursor.getInt(11));
                cursor.getInt(9));
        assertEquals("size", 1000, cursor.getInt(10));
    }
}