Android: Database improvements - remove non-existent games & folders.

This commit is contained in:
sigmabeta 2015-06-08 20:27:12 -04:00
parent d56f27857b
commit b7dcbdbf57
2 changed files with 86 additions and 43 deletions

View File

@ -111,10 +111,35 @@ public final class GameDatabase extends SQLiteOpenHelper
public void scanLibrary(SQLiteDatabase database) public void scanLibrary(SQLiteDatabase database)
{ {
// TODO Before scanning known folders, go through the game table and remove any entries for which the file itself is missing. // Before scanning known folders, go through the game table and remove any entries for which the file itself is missing.
Cursor fileCursor = database.query(TABLE_NAME_GAMES,
null, // Get all columns.
null, // Get all rows.
null,
null, // No grouping.
null,
null); // Order of games is irrelevant.
// Possibly overly defensive, but ensures that moveToNext() does not skip a row.
fileCursor.moveToPosition(-1);
while (fileCursor.moveToNext())
{
String gamePath = fileCursor.getString(GAME_COLUMN_PATH);
File game = new File(gamePath);
if (!game.exists())
{
Log.e("DolphinEmu", "Game file no longer exists. Removing from the library: " + gamePath);
database.delete(TABLE_NAME_GAMES,
KEY_DB_ID + " = ?",
new String[]{Long.toString(fileCursor.getLong(COLUMN_DB_ID))});
}
}
// Get a cursor listing all the folders the user has added to the library. // Get a cursor listing all the folders the user has added to the library.
Cursor cursor = database.query(TABLE_NAME_FOLDERS, Cursor folderCursor = database.query(TABLE_NAME_FOLDERS,
null, // Get all columns. null, // Get all columns.
null, // Get all rows. null, // Get all rows.
null, null,
@ -125,19 +150,22 @@ public final class GameDatabase extends SQLiteOpenHelper
Set<String> allowedExtensions = new HashSet<String>(Arrays.asList(".dff", ".dol", ".elf", ".gcm", ".gcz", ".iso", ".wad", ".wbfs")); Set<String> allowedExtensions = new HashSet<String>(Arrays.asList(".dff", ".dol", ".elf", ".gcm", ".gcz", ".iso", ".wad", ".wbfs"));
// Possibly overly defensive, but ensures that moveToNext() does not skip a row. // Possibly overly defensive, but ensures that moveToNext() does not skip a row.
cursor.moveToPosition(-1); folderCursor.moveToPosition(-1);
// Iterate through all results of the DB query (i.e. all folders in the library.) // Iterate through all results of the DB query (i.e. all folders in the library.)
while (cursor.moveToNext()) while (folderCursor.moveToNext())
{ {
String folderPath = cursor.getString(FOLDER_COLUMN_PATH); String folderPath = folderCursor.getString(FOLDER_COLUMN_PATH);
File folder = new File(folderPath); File folder = new File(folderPath);
Log.i("DolphinEmu", "Reading files from library folder: " + folderPath); Log.i("DolphinEmu", "Reading files from library folder: " + folderPath);
// Iterate through every file in the folder. // Iterate through every file in the folder.
File[] children = folder.listFiles(); File[] children = folder.listFiles();
if (children != null)
{
for (File file : children) for (File file : children)
{ {
if (!file.isHidden() && !file.isDirectory()) if (!file.isHidden() && !file.isDirectory())
@ -191,8 +219,22 @@ public final class GameDatabase extends SQLiteOpenHelper
} }
} }
} }
// If the folder is empty because it no longer exists, remove it from the library.
else if (!folder.exists())
{
Log.e("DolphinEmu", "Folder no longer exists. Removing from the library: " + folderPath);
database.delete(TABLE_NAME_FOLDERS,
KEY_DB_ID + " = ?",
new String[]{Long.toString(folderCursor.getLong(COLUMN_DB_ID))});
}
else
{
Log.e("DolphinEmu", "Folder contains no games: " + folderPath);
}
}
cursor.close();
folderCursor.close();
database.close(); database.close();
} }
} }

View File

@ -98,6 +98,7 @@ public final class GameProvider extends ContentProvider
{ {
if (table.equals(REFRESH_LIBRARY)) if (table.equals(REFRESH_LIBRARY))
{ {
Log.i("DolphinEmu", "URI specified table REFRESH_LIBRARY. No insertion necessary; refreshing library contents...");
mDbHelper.scanLibrary(database); mDbHelper.scanLibrary(database);
return uri; return uri;
} }