Skip to content

Commit

Permalink
Android: Add helper class AfterDirectoryInitializationRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
JosJuice committed Aug 21, 2019
1 parent 2a95227 commit 47e1108
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.dolphinemu.dolphinemu.utils;

import android.content.Context;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

public class AfterDirectoryInitializationRunner
{
private DirectoryStateReceiver directoryStateReceiver;

/**
* Executes a Runnable after directory initialization has finished.
*
* If this is called when directory initialization already is done,
* the Runnable will be executed immediately. If this is called before
* directory initialization is done, the Runnable will be executed
* after directory initialization finishes successfully, or never
* in case directory initialization doesn't finish successfully.
*
* Calling this function multiple times per object is not supported.
*/
public void run(Context context, Runnable runnable)
{
if (!DirectoryInitialization.areDolphinDirectoriesReady())
{
// Wait for directories to get initialized
IntentFilter statusIntentFilter = new IntentFilter(
DirectoryInitialization.BROADCAST_ACTION);

directoryStateReceiver = new DirectoryStateReceiver(directoryInitializationState ->
{
if (directoryInitializationState ==
DirectoryInitialization.DirectoryInitializationState.DOLPHIN_DIRECTORIES_INITIALIZED)
{
LocalBroadcastManager.getInstance(context).unregisterReceiver(directoryStateReceiver);
directoryStateReceiver = null;
runnable.run();
}
});
// Registers the DirectoryStateReceiver and its intent filters
LocalBroadcastManager.getInstance(context).registerReceiver(
directoryStateReceiver,
statusIntentFilter);
}
else
{
runnable.run();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

public class Analytics
{
private static DirectoryStateReceiver directoryStateReceiver;

private static final String analyticsAsked =
Settings.SECTION_ANALYTICS + "_" + SettingsFile.KEY_ANALYTICS_PERMISSION_ASKED;
private static final String analyticsEnabled =
Expand All @@ -35,31 +33,8 @@ public static void checkAnalyticsInit(Context context)
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (!preferences.getBoolean(analyticsAsked, false))
{
if (!DirectoryInitialization.areDolphinDirectoriesReady())
{
// Wait for directories to get initialized
IntentFilter statusIntentFilter = new IntentFilter(
DirectoryInitialization.BROADCAST_ACTION);

directoryStateReceiver = new DirectoryStateReceiver(directoryInitializationState ->
{
if (directoryInitializationState ==
DirectoryInitialization.DirectoryInitializationState.DOLPHIN_DIRECTORIES_INITIALIZED)
{
LocalBroadcastManager.getInstance(context).unregisterReceiver(directoryStateReceiver);
directoryStateReceiver = null;
showMessage(context, preferences);
}
});
// Registers the DirectoryStateReceiver and its intent filters
LocalBroadcastManager.getInstance(context).registerReceiver(
directoryStateReceiver,
statusIntentFilter);
}
else
{
showMessage(context, preferences);
}
new AfterDirectoryInitializationRunner().run(context,
() -> showMessage(context, preferences));
}
}

Expand Down

0 comments on commit 47e1108

Please sign in to comment.