Fix SQLiteDiskIOException in stability test

    Because Sprout has removable storage, the framework needs to maintain
    external database for keeping multiple databases between SD cards.
    When cleaning up databases for old external storage volumes, we can't
    delete .db-shm and .db-wal because these two files always have the
    latest data and .db doesn't have. Using camera or Google+ will access
    .db-shm and .db-wal to get the latest data and will cause SQLite Disk
    I/O exception if they are deleted.

    The fix is to modify the logic of cleaning up databases (only delete
    .db file, not to delete .db-shm and .db-wal file).

    Bug 18412563

    Review: https://partner-android-review.git.corp.google.com/#/c/187072

    Signed-off-by: Benson Huang <benson.huang@mediatek.com>

Change-Id: I484bd535ddebee09824a10f12241f6b2ce086b94
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index 990bd71..ee4aad3 100755
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -441,6 +441,15 @@
 
             // delete least recently used databases if we are over the limit
             String[] databases = mContext.databaseList();
+            // Don't delete wal auxiliary files(db-shm and db-wal) directly because db file may
+            // not be deleted, and it will cause Disk I/O error when accessing this database.
+            List<String> dbList = new ArrayList<String>();
+            for (String database : databases) {
+                if (database != null && database.endsWith(".db")) {
+                    dbList.add(database);
+                }
+            }
+            databases = dbList.toArray(new String[0]);
             int count = databases.length;
             int limit = MAX_EXTERNAL_DATABASES;