Android: Fix rescanning on first app launch after cache clear

GameFileCacheService.startRescan (in MainPresenter.onResume)
does nothing if called before GameFileCacheService.startLoad.
Fixes a 3f71c36 regression where already added games would not
show up after app launch under specific circumstances.

Unfortunately the loading indicator still doesn't show up
during a rescan initiated by app launch, but that would
be more annoying to fix, so I will leave it for now.
This commit is contained in:
JosJuice 2021-03-16 22:58:23 +01:00
parent f44f20560d
commit ac65c7970c
4 changed files with 39 additions and 21 deletions

View File

@ -42,6 +42,7 @@ public final class GameFileCacheService extends IntentService
private static final AtomicReference<GameFile[]> gameFiles = private static final AtomicReference<GameFile[]> gameFiles =
new AtomicReference<>(new GameFile[]{}); new AtomicReference<>(new GameFile[]{});
private static final AtomicInteger unhandledIntents = new AtomicInteger(0); private static final AtomicInteger unhandledIntents = new AtomicInteger(0);
private static final AtomicInteger unhandledRescanIntents = new AtomicInteger(0);
public GameFileCacheService() public GameFileCacheService()
{ {
@ -105,11 +106,22 @@ public final class GameFileCacheService extends IntentService
return new String[]{gameFile.getPath(), secondFile.getPath()}; return new String[]{gameFile.getPath(), secondFile.getPath()};
} }
/**
* Returns true if in the process of either loading the cache or rescanning.
*/
public static boolean isLoading() public static boolean isLoading()
{ {
return unhandledIntents.get() != 0; 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) private static void startService(Context context, String action)
{ {
Intent intent = new Intent(context, GameFileCacheService.class); Intent intent = new Intent(context, GameFileCacheService.class);
@ -137,6 +149,7 @@ public final class GameFileCacheService extends IntentService
public static void startRescan(Context context) public static void startRescan(Context context)
{ {
unhandledIntents.getAndIncrement(); unhandledIntents.getAndIncrement();
unhandledRescanIntents.getAndIncrement();
new AfterDirectoryInitializationRunner().run(context, false, new AfterDirectoryInitializationRunner().run(context, false,
() -> startService(context, ACTION_RESCAN)); () -> startService(context, ACTION_RESCAN));
@ -173,7 +186,9 @@ public final class GameFileCacheService extends IntentService
} }
// Rescan the file system and update the game list cache with the results // 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()))
{
if (gameFileCache != null)
{ {
synchronized (gameFileCache) synchronized (gameFileCache)
{ {
@ -198,6 +213,9 @@ public final class GameFileCacheService extends IntentService
} }
} }
unhandledRescanIntents.decrementAndGet();
}
int intentsLeft = unhandledIntents.decrementAndGet(); int intentsLeft = unhandledIntents.decrementAndGet();
if (intentsLeft == 0) if (intentsLeft == 0)
{ {

View File

@ -82,8 +82,6 @@ public final class MainActivity extends AppCompatActivity
{ {
super.onResume(); super.onResume();
mPresenter.onResume();
if (DirectoryInitialization.shouldStart(this)) if (DirectoryInitialization.shouldStart(this))
{ {
DirectoryInitialization.start(this); DirectoryInitialization.start(this);
@ -91,6 +89,8 @@ public final class MainActivity extends AppCompatActivity
.run(this, false, this::setPlatformTabsAndStartGameFileCacheService); .run(this, false, this::setPlatformTabsAndStartGameFileCacheService);
} }
mPresenter.onResume();
// In case the user changed a setting that affects how games are displayed, // In case the user changed a setting that affects how games are displayed,
// such as system language, cover downloading... // such as system language, cover downloading...
forEachPlatformGamesView(PlatformGamesView::refetchMetadata); forEachPlatformGamesView(PlatformGamesView::refetchMetadata);

View File

@ -132,7 +132,7 @@ public final class MainPresenter
mDirToAdd = null; mDirToAdd = null;
} }
if (sShouldRescanLibrary && !GameFileCacheService.isLoading()) if (sShouldRescanLibrary && !GameFileCacheService.isRescanning())
{ {
new AfterDirectoryInitializationRunner().run(mContext, false, () -> new AfterDirectoryInitializationRunner().run(mContext, false, () ->
{ {

View File

@ -71,14 +71,14 @@ public final class TvMainActivity extends FragmentActivity
{ {
super.onResume(); super.onResume();
mPresenter.onResume();
if (DirectoryInitialization.shouldStart(this)) if (DirectoryInitialization.shouldStart(this))
{ {
DirectoryInitialization.start(this); DirectoryInitialization.start(this);
GameFileCacheService.startLoad(this); GameFileCacheService.startLoad(this);
} }
mPresenter.onResume();
// In case the user changed a setting that affects how games are displayed, // In case the user changed a setting that affects how games are displayed,
// such as system language, cover downloading... // such as system language, cover downloading...
refetchMetadata(); refetchMetadata();