Android: Add s prefix to static variables in GameFileCacheManager

This commit is contained in:
JosJuice 2022-09-27 18:20:22 +02:00
parent 199d2be939
commit f857fa6529
1 changed files with 41 additions and 41 deletions

View File

@ -23,14 +23,14 @@ import java.util.concurrent.Executors;
*/ */
public final class GameFileCacheManager public final class GameFileCacheManager
{ {
private static GameFileCache gameFileCache = null; private static GameFileCache sGameFileCache = null;
private static final MutableLiveData<GameFile[]> gameFiles = private static final MutableLiveData<GameFile[]> sGameFiles =
new MutableLiveData<>(new GameFile[]{}); new MutableLiveData<>(new GameFile[]{});
private static boolean runRescanAfterLoad = false; private static boolean sRunRescanAfterLoad = false;
private static final ExecutorService executor = Executors.newFixedThreadPool(1); private static final ExecutorService sExecutor = Executors.newFixedThreadPool(1);
private static final MutableLiveData<Boolean> loadInProgress = new MutableLiveData<>(false); private static final MutableLiveData<Boolean> sLoadInProgress = new MutableLiveData<>(false);
private static final MutableLiveData<Boolean> rescanInProgress = new MutableLiveData<>(false); private static final MutableLiveData<Boolean> sRescanInProgress = new MutableLiveData<>(false);
private GameFileCacheManager() private GameFileCacheManager()
{ {
@ -38,12 +38,12 @@ public final class GameFileCacheManager
public static LiveData<GameFile[]> getGameFiles() public static LiveData<GameFile[]> getGameFiles()
{ {
return gameFiles; return sGameFiles;
} }
public static List<GameFile> getGameFilesForPlatform(Platform platform) public static List<GameFile> getGameFilesForPlatform(Platform platform)
{ {
GameFile[] allGames = gameFiles.getValue(); GameFile[] allGames = sGameFiles.getValue();
ArrayList<GameFile> platformGames = new ArrayList<>(); ArrayList<GameFile> platformGames = new ArrayList<>();
for (GameFile game : allGames) for (GameFile game : allGames)
{ {
@ -57,7 +57,7 @@ public final class GameFileCacheManager
public static GameFile getGameFileByGameId(String gameId) public static GameFile getGameFileByGameId(String gameId)
{ {
GameFile[] allGames = gameFiles.getValue(); GameFile[] allGames = sGameFiles.getValue();
for (GameFile game : allGames) for (GameFile game : allGames)
{ {
if (game.getGameId().equals(gameId)) if (game.getGameId().equals(gameId))
@ -72,7 +72,7 @@ public final class GameFileCacheManager
{ {
GameFile matchWithoutRevision = null; GameFile matchWithoutRevision = null;
GameFile[] allGames = gameFiles.getValue(); GameFile[] allGames = sGameFiles.getValue();
for (GameFile otherGame : allGames) for (GameFile otherGame : allGames)
{ {
if (game.getGameId().equals(otherGame.getGameId()) && if (game.getGameId().equals(otherGame.getGameId()) &&
@ -102,7 +102,7 @@ public final class GameFileCacheManager
*/ */
public static LiveData<Boolean> isLoading() public static LiveData<Boolean> isLoading()
{ {
return loadInProgress; return sLoadInProgress;
} }
/** /**
@ -110,12 +110,12 @@ public final class GameFileCacheManager
*/ */
public static LiveData<Boolean> isRescanning() public static LiveData<Boolean> isRescanning()
{ {
return rescanInProgress; return sRescanInProgress;
} }
public static boolean isLoadingOrRescanning() public static boolean isLoadingOrRescanning()
{ {
return loadInProgress.getValue() || rescanInProgress.getValue(); return sLoadInProgress.getValue() || sRescanInProgress.getValue();
} }
/** /**
@ -125,11 +125,11 @@ public final class GameFileCacheManager
*/ */
public static void startLoad(Context context) public static void startLoad(Context context)
{ {
if (!loadInProgress.getValue()) if (!sLoadInProgress.getValue())
{ {
loadInProgress.setValue(true); sLoadInProgress.setValue(true);
new AfterDirectoryInitializationRunner().runWithoutLifecycle( new AfterDirectoryInitializationRunner().runWithoutLifecycle(
() -> executor.execute(GameFileCacheManager::load)); () -> sExecutor.execute(GameFileCacheManager::load));
} }
} }
@ -141,11 +141,11 @@ public final class GameFileCacheManager
*/ */
public static void startRescan(Context context) public static void startRescan(Context context)
{ {
if (!rescanInProgress.getValue()) if (!sRescanInProgress.getValue())
{ {
rescanInProgress.setValue(true); sRescanInProgress.setValue(true);
new AfterDirectoryInitializationRunner().runWithoutLifecycle( new AfterDirectoryInitializationRunner().runWithoutLifecycle(
() -> executor.execute(GameFileCacheManager::rescan)); () -> sExecutor.execute(GameFileCacheManager::rescan));
} }
} }
@ -153,8 +153,8 @@ public final class GameFileCacheManager
{ {
// Common case: The game is in the cache, so just grab it from there. // Common case: The game is in the cache, so just grab it from there.
// (Actually, addOrGet already checks for this case, but we want to avoid calling it if possible // (Actually, addOrGet already checks for this case, but we want to avoid calling it if possible
// because onHandleIntent may hold a lock on gameFileCache for extended periods of time.) // because onHandleIntent may hold a lock on sGameFileCache for extended periods of time.)
GameFile[] allGames = gameFiles.getValue(); GameFile[] allGames = sGameFiles.getValue();
for (GameFile game : allGames) for (GameFile game : allGames)
{ {
if (game.getPath().equals(gamePath)) if (game.getPath().equals(gamePath))
@ -165,9 +165,9 @@ public final class GameFileCacheManager
// Unusual case: The game wasn't found in the cache. // Unusual case: The game wasn't found in the cache.
// Scan the game and add it to the cache so that we can return it. // Scan the game and add it to the cache so that we can return it.
synchronized (gameFileCache) synchronized (sGameFileCache)
{ {
return gameFileCache.addOrGet(gamePath); return sGameFileCache.addOrGet(gamePath);
} }
} }
@ -178,30 +178,30 @@ public final class GameFileCacheManager
*/ */
private static void load() private static void load()
{ {
if (gameFileCache == null) if (sGameFileCache == null)
{ {
GameFileCache temp = new GameFileCache(); GameFileCache temp = new GameFileCache();
synchronized (temp) synchronized (temp)
{ {
gameFileCache = temp; sGameFileCache = temp;
gameFileCache.load(); sGameFileCache.load();
if (gameFileCache.getSize() != 0) if (sGameFileCache.getSize() != 0)
{ {
updateGameFileArray(); updateGameFileArray();
} }
} }
} }
if (runRescanAfterLoad) if (sRunRescanAfterLoad)
{ {
rescanInProgress.postValue(true); sRescanInProgress.postValue(true);
} }
loadInProgress.postValue(false); sLoadInProgress.postValue(false);
if (runRescanAfterLoad) if (sRunRescanAfterLoad)
{ {
runRescanAfterLoad = false; sRunRescanAfterLoad = false;
rescan(); rescan();
} }
} }
@ -214,25 +214,25 @@ public final class GameFileCacheManager
*/ */
private static void rescan() private static void rescan()
{ {
if (gameFileCache == null) if (sGameFileCache == null)
{ {
runRescanAfterLoad = true; sRunRescanAfterLoad = true;
} }
else else
{ {
String[] gamePaths = GameFileCache.getAllGamePaths(); String[] gamePaths = GameFileCache.getAllGamePaths();
boolean changed; boolean changed;
synchronized (gameFileCache) synchronized (sGameFileCache)
{ {
changed = gameFileCache.update(gamePaths); changed = sGameFileCache.update(gamePaths);
} }
if (changed) if (changed)
{ {
updateGameFileArray(); updateGameFileArray();
} }
boolean additionalMetadataChanged = gameFileCache.updateAdditionalMetadata(); boolean additionalMetadataChanged = sGameFileCache.updateAdditionalMetadata();
if (additionalMetadataChanged) if (additionalMetadataChanged)
{ {
updateGameFileArray(); updateGameFileArray();
@ -240,17 +240,17 @@ public final class GameFileCacheManager
if (changed || additionalMetadataChanged) if (changed || additionalMetadataChanged)
{ {
gameFileCache.save(); sGameFileCache.save();
} }
} }
rescanInProgress.postValue(false); sRescanInProgress.postValue(false);
} }
private static void updateGameFileArray() private static void updateGameFileArray()
{ {
GameFile[] gameFilesTemp = gameFileCache.getAllGames(); GameFile[] gameFilesTemp = sGameFileCache.getAllGames();
Arrays.sort(gameFilesTemp, (lhs, rhs) -> lhs.getTitle().compareToIgnoreCase(rhs.getTitle())); Arrays.sort(gameFilesTemp, (lhs, rhs) -> lhs.getTitle().compareToIgnoreCase(rhs.getTitle()));
gameFiles.postValue(gameFilesTemp); sGameFiles.postValue(gameFilesTemp);
} }
} }