From 47e1108d572b48ef213c0a3d0e354f69a99c7c79 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 16 Jun 2019 15:32:36 +0200 Subject: [PATCH] Android: Add helper class AfterDirectoryInitializationRunner --- .../AfterDirectoryInitializationRunner.java | 50 +++++++++++++++++++ .../dolphinemu/utils/Analytics.java | 29 +---------- 2 files changed, 52 insertions(+), 27 deletions(-) create mode 100644 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AfterDirectoryInitializationRunner.java diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AfterDirectoryInitializationRunner.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AfterDirectoryInitializationRunner.java new file mode 100644 index 0000000000..0ce87b2b8a --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AfterDirectoryInitializationRunner.java @@ -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(); + } + } +} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Analytics.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Analytics.java index 6f3e07cbb5..cb7aec2056 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Analytics.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/Analytics.java @@ -18,8 +18,6 @@ import org.dolphinemu.dolphinemu.features.settings.utils.SettingsFile; 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 = @@ -35,31 +33,8 @@ public class Analytics 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)); } }