Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #9592 from JosJuice/android-launch-rescan
Android: Fix rescanning on first app launch after cache clear
  • Loading branch information
lioncash committed Mar 22, 2021
2 parents 14cff8d + ac65c79 commit 210ddcf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
Expand Up @@ -42,6 +42,7 @@ public final class GameFileCacheService extends IntentService
private static final AtomicReference<GameFile[]> gameFiles =
new AtomicReference<>(new GameFile[]{});
private static final AtomicInteger unhandledIntents = new AtomicInteger(0);
private static final AtomicInteger unhandledRescanIntents = new AtomicInteger(0);

public GameFileCacheService()
{
Expand Down Expand Up @@ -105,11 +106,22 @@ public static String[] findSecondDiscAndGetPaths(GameFile gameFile)
return new String[]{gameFile.getPath(), secondFile.getPath()};
}

/**
* Returns true if in the process of either loading the cache or rescanning.
*/
public static boolean isLoading()
{
return unhandledIntents.get() != 0;
}

/**
* Returns true if in the process of rescanning.
*/
public static boolean isRescanning()
{
return unhandledRescanIntents.get() != 0;
}

private static void startService(Context context, String action)
{
Intent intent = new Intent(context, GameFileCacheService.class);
Expand Down Expand Up @@ -137,6 +149,7 @@ public static void startLoad(Context context)
public static void startRescan(Context context)
{
unhandledIntents.getAndIncrement();
unhandledRescanIntents.getAndIncrement();

new AfterDirectoryInitializationRunner().run(context, false,
() -> startService(context, ACTION_RESCAN));
Expand Down Expand Up @@ -173,29 +186,34 @@ protected void onHandleIntent(Intent intent)
}

// Rescan the file system and update the game list cache with the results
if (ACTION_RESCAN.equals(intent.getAction()) && gameFileCache != null)
if (ACTION_RESCAN.equals(intent.getAction()))
{
synchronized (gameFileCache)
if (gameFileCache != null)
{
boolean changed = gameFileCache.update();
if (changed)
{
updateGameFileArray();
sendBroadcast(CACHE_UPDATED);
}

boolean additionalMetadataChanged = gameFileCache.updateAdditionalMetadata();
if (additionalMetadataChanged)
synchronized (gameFileCache)
{
updateGameFileArray();
sendBroadcast(CACHE_UPDATED);
}

if (changed || additionalMetadataChanged)
{
gameFileCache.save();
boolean changed = gameFileCache.update();
if (changed)
{
updateGameFileArray();
sendBroadcast(CACHE_UPDATED);
}

boolean additionalMetadataChanged = gameFileCache.updateAdditionalMetadata();
if (additionalMetadataChanged)
{
updateGameFileArray();
sendBroadcast(CACHE_UPDATED);
}

if (changed || additionalMetadataChanged)
{
gameFileCache.save();
}
}
}

unhandledRescanIntents.decrementAndGet();
}

int intentsLeft = unhandledIntents.decrementAndGet();
Expand Down
Expand Up @@ -82,15 +82,15 @@ protected void onResume()
{
super.onResume();

mPresenter.onResume();

if (DirectoryInitialization.shouldStart(this))
{
DirectoryInitialization.start(this);
new AfterDirectoryInitializationRunner()
.run(this, false, this::setPlatformTabsAndStartGameFileCacheService);
}

mPresenter.onResume();

// In case the user changed a setting that affects how games are displayed,
// such as system language, cover downloading...
forEachPlatformGamesView(PlatformGamesView::refetchMetadata);
Expand Down
Expand Up @@ -132,7 +132,7 @@ public void onResume()
mDirToAdd = null;
}

if (sShouldRescanLibrary && !GameFileCacheService.isLoading())
if (sShouldRescanLibrary && !GameFileCacheService.isRescanning())
{
new AfterDirectoryInitializationRunner().run(mContext, false, () ->
{
Expand Down
Expand Up @@ -71,14 +71,14 @@ protected void onResume()
{
super.onResume();

mPresenter.onResume();

if (DirectoryInitialization.shouldStart(this))
{
DirectoryInitialization.start(this);
GameFileCacheService.startLoad(this);
}

mPresenter.onResume();

// In case the user changed a setting that affects how games are displayed,
// such as system language, cover downloading...
refetchMetadata();
Expand Down

0 comments on commit 210ddcf

Please sign in to comment.