Skip to content

Commit

Permalink
Merge pull request dolphin-emu#8196 from JosJuice/android-applinkacti…
Browse files Browse the repository at this point in the history
…vity-race

Android: Fix race condition in AppLinkActivity
  • Loading branch information
Helios747 authored Aug 21, 2019
2 parents 2a95227 + e4ef219 commit 998c171
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.dolphinemu.dolphinemu.activities;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
Expand All @@ -26,6 +28,7 @@ public class AppLinkActivity extends FragmentActivity

private AppLinkHelper.PlayAction playAction;
private DirectoryStateReceiver directoryStateReceiver;
private BroadcastReceiver gameFileCacheReceiver;

@Override
protected void onCreate(Bundle savedInstanceState)
Expand Down Expand Up @@ -63,16 +66,19 @@ protected void onCreate(Bundle savedInstanceState)
*/
private void initResources()
{
IntentFilter statusIntentFilter = new IntentFilter(
IntentFilter directoryStateIntentFilter = new IntentFilter(
DirectoryInitialization.BROADCAST_ACTION);

IntentFilter gameFileCacheIntentFilter = new IntentFilter(
GameFileCacheService.BROADCAST_ACTION);

directoryStateReceiver =
new DirectoryStateReceiver(directoryInitializationState ->
{
if (directoryInitializationState ==
DirectoryInitialization.DirectoryInitializationState.DOLPHIN_DIRECTORIES_INITIALIZED)
{
play(playAction);
tryPlay(playAction);
}
else if (directoryInitializationState ==
DirectoryInitialization.DirectoryInitializationState.EXTERNAL_STORAGE_PERMISSION_NEEDED)
Expand All @@ -88,10 +94,23 @@ else if (directoryInitializationState ==
}
});

// Registers the DirectoryStateReceiver and its intent filters
LocalBroadcastManager.getInstance(this).registerReceiver(
directoryStateReceiver,
statusIntentFilter);
gameFileCacheReceiver =
new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if (DirectoryInitialization.areDolphinDirectoriesReady())
{
tryPlay(playAction);
}
}
};

LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
broadcastManager.registerReceiver(directoryStateReceiver, directoryStateIntentFilter);
broadcastManager.registerReceiver(gameFileCacheReceiver, gameFileCacheIntentFilter);

DirectoryInitialization.start(this);
GameFileCacheService.startLoad(this);
}
Expand All @@ -107,17 +126,31 @@ private void browse()
finish();
}

private void tryPlay(AppLinkHelper.PlayAction action)
{
// TODO: This approach of getting the game from the game file cache without rescanning
// the library means that we can fail to launch games if the cache file has been deleted.

GameFile game = GameFileCacheService.getGameFileByGameId(action.getGameId());

// If game == null and the load isn't done, wait for the next GameFileCacheService broadcast.
// If game == null and the load is done, call play with a null game, making us exit in failure.
if (game != null || GameFileCacheService.hasLoadedCache())
{
play(action, game);
}
}

/**
* Action if program(game) is selected
*/
private void play(AppLinkHelper.PlayAction action)
private void play(AppLinkHelper.PlayAction action, GameFile game)
{
Log.d(TAG, "Playing game "
+ action.getGameId()
+ " from channel "
+ action.getChannelId());

GameFile game = GameFileCacheService.getGameFileByGameId(action.getGameId());
if (game == null)
Log.e(TAG, "Invalid Game: " + action.getGameId());
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

/**
Expand All @@ -27,6 +28,8 @@ public final class GameFileCacheService extends IntentService

private static GameFileCache gameFileCache = null;
private static AtomicReference<GameFile[]> gameFiles = new AtomicReference<>(new GameFile[]{});
private static AtomicBoolean hasLoadedCache = new AtomicBoolean(false);
private static AtomicBoolean hasScannedLibrary = new AtomicBoolean(false);

public GameFileCacheService()
{
Expand Down Expand Up @@ -81,6 +84,16 @@ public static GameFile findSecondDisc(GameFile game)
return matchWithoutRevision;
}

public static boolean hasLoadedCache()
{
return hasLoadedCache.get();
}

public static boolean hasScannedLibrary()
{
return hasScannedLibrary.get();
}

private static void startService(Context context, String action)
{
Intent intent = new Intent(context, GameFileCacheService.class);
Expand Down Expand Up @@ -130,6 +143,8 @@ protected void onHandleIntent(Intent intent)
gameFileCache = temp;
gameFileCache.load();
updateGameFileArray();
hasLoadedCache.set(true);
sendBroadcast();
}
}

Expand All @@ -138,10 +153,11 @@ protected void onHandleIntent(Intent intent)
{
synchronized (gameFileCache)
{
if (gameFileCache.scanLibrary(this))
{
boolean changed = gameFileCache.scanLibrary(this);
if (changed)
updateGameFileArray();
}
hasScannedLibrary.set(true);
sendBroadcast();
}
}
}
Expand All @@ -151,6 +167,10 @@ private void updateGameFileArray()
GameFile[] gameFilesTemp = gameFileCache.getAllGames();
Arrays.sort(gameFilesTemp, (lhs, rhs) -> lhs.getTitle().compareToIgnoreCase(rhs.getTitle()));
gameFiles.set(gameFilesTemp);
}

private void sendBroadcast()
{
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BROADCAST_ACTION));
}
}

0 comments on commit 998c171

Please sign in to comment.