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

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

Merge "Stop reopening MTP devices."

parents 42a5e0e3 be388489
Loading
Loading
Loading
Loading
+0 −26
Original line number Original line Diff line number Diff line
@@ -307,32 +307,6 @@ class MtpDatabase {
        }
        }
    }
    }


    /**
     * Returns the set of device ID stored in the database.
     */
    int[] getDeviceIds() {
        final Cursor cursor = mDatabase.query(
                true,
                TABLE_DOCUMENTS,
                strings(COLUMN_DEVICE_ID),
                null,
                null,
                null,
                null,
                null,
                null);
        try {
            final int[] ids = new int[cursor.getCount()];
            for (int i = 0; i < ids.length; i++) {
                cursor.moveToNext();
                ids[i] = cursor.getInt(0);
            }
            return ids;
        } finally {
            cursor.close();
        }
    }

    private boolean deleteDocumentsAndRoots(String selection, String[] args) {
    private boolean deleteDocumentsAndRoots(String selection, String[] args) {
        mDatabase.beginTransaction();
        mDatabase.beginTransaction();
        try {
        try {
+1 −9
Original line number Original line Diff line number Diff line
@@ -293,19 +293,11 @@ public class MtpDocumentsProvider extends DocumentsProvider {
    }
    }


    /**
    /**
     * Reopens MTP devices based on database state.
     * Clears MTP identifier in the database.
     */
     */
    private void resume() {
    private void resume() {
        synchronized (mDeviceListLock) {
        synchronized (mDeviceListLock) {
            mDatabase.getMapper().clearMapping();
            mDatabase.getMapper().clearMapping();
            final int[] ids = mDatabase.getDeviceIds();
            for (final int id : ids) {
                try {
                    openDevice(id);
                } catch (IOException exception) {
                    mDatabase.removeDeviceRows(id);
                }
            }
        }
        }
    }
    }


+0 −28
Original line number Original line Diff line number Diff line
@@ -408,34 +408,6 @@ public class MtpDocumentsProviderTest extends AndroidTestCase {
                        MtpDocumentsProvider.AUTHORITY, "1")));
                        MtpDocumentsProvider.AUTHORITY, "1")));
    }
    }


    @MediumTest
    public void testPauseAndResume() throws Exception {
        setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_FILE);
        mMtpManager.addValidDevice(0);
        mProvider.openDevice(0);
        setupRoots(0, new MtpRoot[] { new MtpRoot(0, 0, "Device", "Storage", 0, 0, "")});

        {
            final Cursor cursor = mProvider.queryRoots(
                    strings(DocumentsContract.Root.COLUMN_ROOT_ID));
            cursor.moveToNext();
            assertEquals(1, cursor.getInt(0));
        }

        mProvider.shutdown();
        setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_FILE);

        {
            // We can still fetch roots after relaunching the provider.
            final Cursor cursor = mProvider.queryRoots(
                    strings(DocumentsContract.Root.COLUMN_ROOT_ID));
            assertEquals(1, cursor.getCount());
            cursor.moveToNext();
            assertEquals(1, cursor.getInt(0));
            assertEquals(1, mMtpManager.getOpenedDeviceIds().length);
        }
    }

    private void setupProvider(int flag) {
    private void setupProvider(int flag) {
        mDatabase = new MtpDatabase(getContext(), flag);
        mDatabase = new MtpDatabase(getContext(), flag);
        mProvider = new MtpDocumentsProvider();
        mProvider = new MtpDocumentsProvider();