diff options
Diffstat (limited to 'src/android')
-rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DirectoryInitialization.java | 72 | ||||
-rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DirectoryInitialization.kt | 66 |
2 files changed, 66 insertions, 72 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DirectoryInitialization.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DirectoryInitialization.java deleted file mode 100644 index 6464c2286..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DirectoryInitialization.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.yuzu.yuzu_emu.utils; - -import android.content.Context; -import android.content.Intent; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; - -import org.yuzu.yuzu_emu.NativeLibrary; - -import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; - -public final class DirectoryInitialization { - public static final String BROADCAST_ACTION = "org.yuzu.yuzu_emu.BROADCAST"; - public static final String EXTRA_STATE = "directoryState"; - private static volatile DirectoryInitializationState directoryState = null; - private static String userPath; - private static AtomicBoolean isDirectoryInitializationRunning = new AtomicBoolean(false); - - public static void start(Context context) { - // Can take a few seconds to run, so don't block UI thread. - ((Runnable) () -> init(context)).run(); - } - - private static void init(Context context) { - if (!isDirectoryInitializationRunning.compareAndSet(false, true)) - return; - - if (directoryState != DirectoryInitializationState.YUZU_DIRECTORIES_INITIALIZED) { - initializeInternalStorage(context); - NativeLibrary.InitializeEmulation(); - directoryState = DirectoryInitializationState.YUZU_DIRECTORIES_INITIALIZED; - } - - isDirectoryInitializationRunning.set(false); - sendBroadcastState(directoryState, context); - } - - public static boolean areDirectoriesReady() { - return directoryState == DirectoryInitializationState.YUZU_DIRECTORIES_INITIALIZED; - } - - public static String getUserDirectory() { - if (directoryState == null) { - throw new IllegalStateException("DirectoryInitialization has to run at least once!"); - } else if (isDirectoryInitializationRunning.get()) { - throw new IllegalStateException( - "DirectoryInitialization has to finish running first!"); - } - return userPath; - } - - public static void initializeInternalStorage(Context context) { - try { - userPath = context.getExternalFilesDir(null).getCanonicalPath(); - NativeLibrary.SetAppDirectory(userPath); - } catch(IOException e) { - e.printStackTrace(); - } - } - - private static void sendBroadcastState(DirectoryInitializationState state, Context context) { - Intent localIntent = - new Intent(BROADCAST_ACTION) - .putExtra(EXTRA_STATE, state); - LocalBroadcastManager.getInstance(context).sendBroadcast(localIntent); - } - - public enum DirectoryInitializationState { - YUZU_DIRECTORIES_INITIALIZED, - CANT_FIND_EXTERNAL_STORAGE - } -} diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DirectoryInitialization.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DirectoryInitialization.kt new file mode 100644 index 000000000..5016f7d6e --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DirectoryInitialization.kt @@ -0,0 +1,66 @@ +package org.yuzu.yuzu_emu.utils + +import android.content.Context +import android.content.Intent +import androidx.localbroadcastmanager.content.LocalBroadcastManager +import org.yuzu.yuzu_emu.NativeLibrary +import java.io.IOException +import java.util.concurrent.atomic.AtomicBoolean + +object DirectoryInitialization { + const val BROADCAST_ACTION = "org.yuzu.yuzu_emu.BROADCAST" + const val EXTRA_STATE = "directoryState" + + @Volatile + private var directoryState: DirectoryInitializationState? = null + private var userPath: String? = null + private val isDirectoryInitializationRunning = AtomicBoolean(false) + + @JvmStatic + fun start(context: Context) { + // Can take a few seconds to run, so don't block UI thread. + Runnable { init(context) }.run() + } + + private fun init(context: Context) { + if (!isDirectoryInitializationRunning.compareAndSet(false, true)) return + if (directoryState != DirectoryInitializationState.YUZU_DIRECTORIES_INITIALIZED) { + initializeInternalStorage(context) + NativeLibrary.InitializeEmulation() + directoryState = DirectoryInitializationState.YUZU_DIRECTORIES_INITIALIZED + } + isDirectoryInitializationRunning.set(false) + sendBroadcastState(directoryState, context) + } + + fun areDirectoriesReady(): Boolean { + return directoryState == DirectoryInitializationState.YUZU_DIRECTORIES_INITIALIZED + } + + val userDirectory: String? + get() { + checkNotNull(directoryState) { "DirectoryInitialization has to run at least once!" } + check(!isDirectoryInitializationRunning.get()) { "DirectoryInitialization has to finish running first!" } + return userPath + } + + private fun initializeInternalStorage(context: Context) { + try { + userPath = context.getExternalFilesDir(null)!!.canonicalPath + NativeLibrary.SetAppDirectory(userPath) + } catch (e: IOException) { + e.printStackTrace() + } + } + + private fun sendBroadcastState(state: DirectoryInitializationState?, context: Context) { + val localIntent = Intent(BROADCAST_ACTION) + .putExtra(EXTRA_STATE, state) + LocalBroadcastManager.getInstance(context).sendBroadcast(localIntent) + } + + enum class DirectoryInitializationState { + YUZU_DIRECTORIES_INITIALIZED, + CANT_FIND_EXTERNAL_STORAGE + } +} |