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

Commit f11f14cb authored by Daichi Hirono's avatar Daichi Hirono Committed by Android (Google) Code Review
Browse files

Merge "Return NULL available bytes when the MTP device is closed."

parents 2d8d083d 83983d75
Loading
Loading
Loading
Loading
+19 −14
Original line number Diff line number Diff line
@@ -165,13 +165,14 @@ class MtpDatabase {
                            cursor.close();
                        }

                        if (storageCursor.getCount() != 0) {
                            long capacityBytes = 0;
                            long availableBytes = 0;
                            int capacityIndex = cursor.getColumnIndex(Root.COLUMN_CAPACITY_BYTES);
                            int availableIndex = cursor.getColumnIndex(Root.COLUMN_AVAILABLE_BYTES);
                            while (storageCursor.moveToNext()) {
                            // If requested columnNames does not include COLUMN_XXX_BYTES, we don't
                            // calculate corresponding values.
                                // If requested columnNames does not include COLUMN_XXX_BYTES, we
                                // don't calculate corresponding values.
                                if (capacityIndex != -1) {
                                    capacityBytes += cursor.getLong(capacityIndex);
                                }
@@ -181,6 +182,10 @@ class MtpDatabase {
                            }
                            values.put(Root.COLUMN_CAPACITY_BYTES, capacityBytes);
                            values.put(Root.COLUMN_AVAILABLE_BYTES, availableBytes);
                        } else {
                            values.putNull(Root.COLUMN_CAPACITY_BYTES);
                            values.putNull(Root.COLUMN_AVAILABLE_BYTES);
                        }
                    }
                } finally {
                    storageCursor.close();
+20 −0
Original line number Diff line number Diff line
@@ -899,4 +899,24 @@ public class MtpDatabaseTest extends AndroidTestCase {
        mDatabase.getMapper().stopAddingDocuments(null);
        assertEquals("1", mDatabase.getDocumentIdForDevice(100));
    }

    public void testGetClosedDevice() {
        mDatabase.getMapper().startAddingDocuments(null);
        mDatabase.getMapper().putDeviceDocument(new MtpDeviceRecord(
                0, "Device", /* opened is */ false , new MtpRoot[0], null, null));
        mDatabase.getMapper().stopAddingDocuments(null);

        final String[] columns = new String [] {
                DocumentsContract.Root.COLUMN_ROOT_ID,
                DocumentsContract.Root.COLUMN_TITLE,
                DocumentsContract.Root.COLUMN_AVAILABLE_BYTES
        };
        try (final Cursor cursor = mDatabase.queryRoots(columns)) {
            assertEquals(1, cursor.getCount());
            assertTrue(cursor.moveToNext());
            assertEquals(1, cursor.getLong(0));
            assertEquals("Device", cursor.getString(1));
            assertTrue(cursor.isNull(2));
        }
    }
}