Android: Database improvements - remove non-existent games & folders.
This commit is contained in:
parent
d56f27857b
commit
b7dcbdbf57
|
@ -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,74 +150,91 @@ 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();
|
||||||
for (File file : children)
|
|
||||||
|
if (children != null)
|
||||||
{
|
{
|
||||||
if (!file.isHidden() && !file.isDirectory())
|
for (File file : children)
|
||||||
{
|
{
|
||||||
String filePath = file.getPath();
|
if (!file.isHidden() && !file.isDirectory())
|
||||||
|
|
||||||
int extensionStart = filePath.lastIndexOf('.');
|
|
||||||
if (extensionStart > 0)
|
|
||||||
{
|
{
|
||||||
String fileExtension = filePath.substring(extensionStart);
|
String filePath = file.getPath();
|
||||||
|
|
||||||
// Check that the file has an extension we care about before trying to read out of it.
|
int extensionStart = filePath.lastIndexOf('.');
|
||||||
if (allowedExtensions.contains(fileExtension))
|
if (extensionStart > 0)
|
||||||
{
|
{
|
||||||
String name = NativeLibrary.GetTitle(filePath);
|
String fileExtension = filePath.substring(extensionStart);
|
||||||
|
|
||||||
// If the game's title field is empty, use the filename.
|
// Check that the file has an extension we care about before trying to read out of it.
|
||||||
if (name.isEmpty())
|
if (allowedExtensions.contains(fileExtension))
|
||||||
{
|
{
|
||||||
name = filePath.substring(filePath.lastIndexOf("/") + 1);
|
String name = NativeLibrary.GetTitle(filePath);
|
||||||
}
|
|
||||||
|
|
||||||
ContentValues game = Game.asContentValues(NativeLibrary.GetPlatform(filePath),
|
// If the game's title field is empty, use the filename.
|
||||||
name,
|
if (name.isEmpty())
|
||||||
NativeLibrary.GetDescription(filePath).replace("\n", " "),
|
{
|
||||||
NativeLibrary.GetCountry(filePath),
|
name = filePath.substring(filePath.lastIndexOf("/") + 1);
|
||||||
filePath,
|
}
|
||||||
NativeLibrary.GetGameId(filePath),
|
|
||||||
NativeLibrary.GetCompany(filePath));
|
|
||||||
|
|
||||||
// Try to update an existing game first.
|
ContentValues game = Game.asContentValues(NativeLibrary.GetPlatform(filePath),
|
||||||
int rowsMatched = database.update(TABLE_NAME_GAMES, // Which table to update.
|
name,
|
||||||
game, // The values to fill the row with.
|
NativeLibrary.GetDescription(filePath).replace("\n", " "),
|
||||||
KEY_GAME_ID + " = ?", // The WHERE clause used to find the right row.
|
NativeLibrary.GetCountry(filePath),
|
||||||
new String[]{game.getAsString(KEY_GAME_ID)}); // The ? in WHERE clause is replaced with this,
|
filePath,
|
||||||
// which is provided as an array because there
|
NativeLibrary.GetGameId(filePath),
|
||||||
// could potentially be more than one argument.
|
NativeLibrary.GetCompany(filePath));
|
||||||
|
|
||||||
// If update fails, insert a new game instead.
|
// Try to update an existing game first.
|
||||||
if (rowsMatched == 0)
|
int rowsMatched = database.update(TABLE_NAME_GAMES, // Which table to update.
|
||||||
{
|
game, // The values to fill the row with.
|
||||||
Log.v("DolphinEmu", "Adding game: " + game.getAsString(KEY_GAME_TITLE));
|
KEY_GAME_ID + " = ?", // The WHERE clause used to find the right row.
|
||||||
database.insert(TABLE_NAME_GAMES, null, game);
|
new String[]{game.getAsString(KEY_GAME_ID)}); // The ? in WHERE clause is replaced with this,
|
||||||
}
|
// which is provided as an array because there
|
||||||
else
|
// could potentially be more than one argument.
|
||||||
{
|
|
||||||
Log.v("DolphinEmu", "Updated game: " + game.getAsString(KEY_GAME_TITLE));
|
// If update fails, insert a new game instead.
|
||||||
|
if (rowsMatched == 0)
|
||||||
|
{
|
||||||
|
Log.v("DolphinEmu", "Adding game: " + game.getAsString(KEY_GAME_TITLE));
|
||||||
|
database.insert(TABLE_NAME_GAMES, null, game);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.v("DolphinEmu", "Updated game: " + game.getAsString(KEY_GAME_TITLE));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue