Android: Split up GameFileCacheService.onHandleIntent

This commit is contained in:
JosJuice 2021-11-16 00:04:45 +01:00
parent 7292ac0ce5
commit 31bfbca923
1 changed files with 58 additions and 37 deletions

View File

@ -132,8 +132,9 @@ public final class GameFileCacheService extends IntentService
} }
/** /**
* Asynchronously loads the game file cache from disk without checking * Asynchronously loads the game file cache from disk, without checking
* which games are present on the file system. * if the games are still present in the user's configured folders.
* If this has already been called, calling it again has no effect.
*/ */
public static void startLoad(Context context) public static void startLoad(Context context)
{ {
@ -182,8 +183,32 @@ public final class GameFileCacheService extends IntentService
@Override @Override
protected void onHandleIntent(Intent intent) protected void onHandleIntent(Intent intent)
{ {
// Load the game list cache if it isn't already loaded, otherwise do nothing if (ACTION_LOAD.equals(intent.getAction()))
if (ACTION_LOAD.equals(intent.getAction()) && gameFileCache == null) {
load();
}
if (ACTION_RESCAN.equals(intent.getAction()))
{
rescan();
unhandledRescanIntents.decrementAndGet();
}
int intentsLeft = unhandledIntents.decrementAndGet();
if (intentsLeft == 0)
{
sendBroadcast(DONE_LOADING);
}
}
/**
* Loads the game file cache from disk, without checking if the
* games are still present in the user's configured folders.
* If this has already been called, calling it again has no effect.
*/
private void load()
{
if (gameFileCache == null)
{ {
GameFileCache temp = new GameFileCache(); GameFileCache temp = new GameFileCache();
synchronized (temp) synchronized (temp)
@ -197,45 +222,41 @@ public final class GameFileCacheService extends IntentService
} }
} }
} }
}
// Rescan the file system and update the game list cache with the results /**
if (ACTION_RESCAN.equals(intent.getAction())) * Scans for games in the user's configured folders,
* updating the game file cache with the results.
* If load hasn't been called before this, this has no effect.
*/
private void rescan()
{
if (gameFileCache != null)
{ {
if (gameFileCache != null) String[] gamePaths = GameFileCache.getAllGamePaths();
boolean changed;
synchronized (gameFileCache)
{ {
String[] gamePaths = GameFileCache.getAllGamePaths(); changed = gameFileCache.update(gamePaths);
}
boolean changed; if (changed)
synchronized (gameFileCache) {
{ updateGameFileArray();
changed = gameFileCache.update(gamePaths); sendBroadcast(CACHE_UPDATED);
}
if (changed)
{
updateGameFileArray();
sendBroadcast(CACHE_UPDATED);
}
boolean additionalMetadataChanged = gameFileCache.updateAdditionalMetadata();
if (additionalMetadataChanged)
{
updateGameFileArray();
sendBroadcast(CACHE_UPDATED);
}
if (changed || additionalMetadataChanged)
{
gameFileCache.save();
}
} }
unhandledRescanIntents.decrementAndGet(); boolean additionalMetadataChanged = gameFileCache.updateAdditionalMetadata();
} if (additionalMetadataChanged)
{
updateGameFileArray();
sendBroadcast(CACHE_UPDATED);
}
int intentsLeft = unhandledIntents.decrementAndGet(); if (changed || additionalMetadataChanged)
if (intentsLeft == 0) {
{ gameFileCache.save();
sendBroadcast(DONE_LOADING); }
} }
} }