Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ import org.dolphinemu.dolphinemu.model.GameFileCache
import org.dolphinemu.dolphinemu.services.GameFileCacheManager
import org.dolphinemu.dolphinemu.utils.AfterDirectoryInitializationRunner
import org.dolphinemu.dolphinemu.utils.BooleanSupplier
import org.dolphinemu.dolphinemu.utils.CompletableFuture
import org.dolphinemu.dolphinemu.utils.ContentHandler
import org.dolphinemu.dolphinemu.utils.DirectoryInitialization
import org.dolphinemu.dolphinemu.utils.FileBrowserHelper
import org.dolphinemu.dolphinemu.utils.PermissionsHandler
import org.dolphinemu.dolphinemu.utils.ThreadUtil
import org.dolphinemu.dolphinemu.utils.WiiUtils
import java.util.Arrays
import java.util.concurrent.ExecutionException
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicReference

class MainPresenter(private val mainView: MainView, private val activity: FragmentActivity) {
private var dirToAdd: String? = null
Expand Down Expand Up @@ -104,8 +104,9 @@ class MainPresenter(private val mainView: MainView, private val activity: Fragme

fun onCreate() {
// Ask the user to grant write permission if relevant and not already granted
if (DirectoryInitialization.isWaitingForWriteAccess(activity))
if (DirectoryInitialization.isWaitingForWriteAccess(activity)) {
PermissionsHandler.requestWritePermission(activity)
}

val versionName = BuildConfig.VERSION_NAME
mainView.setVersionString(versionName)
Expand All @@ -118,85 +119,83 @@ class MainPresenter(private val mainView: MainView, private val activity: Fragme
}

fun launchFileListActivity() {
val intent =
if (DirectoryInitialization.preferOldFolderPicker(activity)) {
FileBrowserHelper.createDirectoryPickerIntent(
activity,
FileBrowserHelper.GAME_EXTENSIONS,
)
} else {
Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
}
val intent = if (DirectoryInitialization.preferOldFolderPicker(activity)) {
FileBrowserHelper.createDirectoryPickerIntent(
activity,
FileBrowserHelper.GAME_EXTENSIONS,
)
} else {
Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
}
requestDirectory.launch(intent)
}

fun handleOptionSelection(itemId: Int, activity: ComponentActivity): Boolean =
when (itemId) {
R.id.menu_settings -> {
mainView.launchSettingsActivity(MenuTag.SETTINGS)
true
}

R.id.menu_grid_options -> {
mainView.showGridOptions()
true
}
fun handleOptionSelection(itemId: Int, activity: ComponentActivity): Boolean = when (itemId) {
R.id.menu_settings -> {
mainView.launchSettingsActivity(MenuTag.SETTINGS)
true
}

R.id.menu_refresh -> {
mainView.setRefreshing(true)
GameFileCacheManager.startRescan()
true
}
R.id.menu_grid_options -> {
mainView.showGridOptions()
true
}

R.id.button_add_directory -> {
AfterDirectoryInitializationRunner().runWithLifecycle(activity) { launchFileListActivity() }
true
}
R.id.menu_refresh -> {
mainView.setRefreshing(true)
GameFileCacheManager.startRescan()
true
}

R.id.menu_open_file -> {
requestGameFile.launch("*/*")
true
}
R.id.button_add_directory -> {
AfterDirectoryInitializationRunner().runWithLifecycle(activity) { launchFileListActivity() }
true
}

R.id.menu_load_wii_system_menu -> {
launchWiiSystemMenu()
true
}
R.id.menu_open_file -> {
requestGameFile.launch("*/*")
true
}

R.id.menu_online_system_update -> {
AfterDirectoryInitializationRunner().runWithLifecycle(activity) { launchOnlineUpdate() }
true
}
R.id.menu_load_wii_system_menu -> {
launchWiiSystemMenu()
true
}

R.id.menu_install_wad -> {
AfterDirectoryInitializationRunner().runWithLifecycle(
activity
) { requestWadFile.launch("*/*") }
true
}
R.id.menu_online_system_update -> {
AfterDirectoryInitializationRunner().runWithLifecycle(activity) { launchOnlineUpdate() }
true
}

R.id.menu_import_wii_save -> {
AfterDirectoryInitializationRunner().runWithLifecycle(
activity
) { requestWiiSaveFile.launch("*/*") }
true
}
R.id.menu_install_wad -> {
AfterDirectoryInitializationRunner().runWithLifecycle(
activity
) { requestWadFile.launch("*/*") }
true
}

R.id.menu_import_nand_backup -> {
AfterDirectoryInitializationRunner().runWithLifecycle(
activity
) { requestNandBinFile.launch("*/*") }
true
}
R.id.menu_import_wii_save -> {
AfterDirectoryInitializationRunner().runWithLifecycle(
activity
) { requestWiiSaveFile.launch("*/*") }
true
}

R.id.menu_about -> {
showAboutDialog()
false
}
R.id.menu_import_nand_backup -> {
AfterDirectoryInitializationRunner().runWithLifecycle(
activity
) { requestNandBinFile.launch("*/*") }
true
}

else -> false
R.id.menu_about -> {
showAboutDialog()
false
}

else -> false
}

fun onResume() {
if (dirToAdd != null) {
GameFileCache.addGameFolder(dirToAdd!!)
Expand Down Expand Up @@ -226,24 +225,25 @@ class MainPresenter(private val mainView: MainView, private val activity: Fragme
val recursive = BooleanSetting.MAIN_RECURSIVE_ISO_PATHS.boolean
val childNames = ContentHandler.getChildNames(uri, recursive)
if (Arrays.stream(childNames).noneMatch {
FileBrowserHelper.GAME_EXTENSIONS
.contains(FileBrowserHelper.getExtension(it, false))
}) {
MaterialAlertDialogBuilder(activity)
.setMessage(
activity.getString(
R.string.wrong_file_extension_in_directory,
FileBrowserHelper.setToSortedDelimitedString(FileBrowserHelper.GAME_EXTENSIONS)
FileBrowserHelper.GAME_EXTENSIONS.contains(
FileBrowserHelper.getExtension(
it, false
)
)
.setPositiveButton(android.R.string.ok, null)
.show()
}) {
MaterialAlertDialogBuilder(activity).setMessage(
activity.getString(
R.string.wrong_file_extension_in_directory,
FileBrowserHelper.setToSortedDelimitedString(FileBrowserHelper.GAME_EXTENSIONS)
)
).setPositiveButton(android.R.string.ok, null).show()
}

val contentResolver = activity.contentResolver
val canonicalizedUri = contentResolver.canonicalize(uri)
if (canonicalizedUri != null)
if (canonicalizedUri != null) {
uri = canonicalizedUri
}

val takeFlags = result.flags and Intent.FLAG_GRANT_READ_URI_PERMISSION
activity.contentResolver.takePersistableUriPermission(uri, takeFlags)
Expand All @@ -253,10 +253,7 @@ class MainPresenter(private val mainView: MainView, private val activity: Fragme

fun installWAD(path: String?) {
ThreadUtil.runOnThreadAndShowResult(
activity,
R.string.import_in_progress,
0,
{
activity, R.string.import_in_progress, 0, {
val success = WiiUtils.installWAD(path!!)
val message =
if (success) R.string.wad_install_success else R.string.wad_install_failure
Expand All @@ -265,33 +262,28 @@ class MainPresenter(private val mainView: MainView, private val activity: Fragme
}

fun importWiiSave(path: String?) {
val canOverwriteFuture = CompletableFuture<Boolean>()
ThreadUtil.runOnThreadAndShowResult(
activity,
R.string.import_in_progress,
0,
{
activity, R.string.import_in_progress, 0, {
val canOverwrite = BooleanSupplier {
val latch = CountDownLatch(1)
val decision = AtomicReference<Boolean>()
activity.runOnUiThread {
MaterialAlertDialogBuilder(activity)
.setMessage(R.string.wii_save_exists)
MaterialAlertDialogBuilder(activity).setMessage(R.string.wii_save_exists)
.setCancelable(false)
.setPositiveButton(R.string.yes) { _: DialogInterface?, _: Int ->
canOverwriteFuture.complete(true)
}
.setNegativeButton(R.string.no) { _: DialogInterface?, _: Int ->
canOverwriteFuture.complete(false)
}
.show()
decision.set(true)
latch.countDown()
}.setNegativeButton(R.string.no) { _: DialogInterface?, _: Int ->
decision.set(false)
latch.countDown()
}.show()
}
try {
return@BooleanSupplier canOverwriteFuture.get()
} catch (e: ExecutionException) {
// Shouldn't happen
throw RuntimeException(e)
latch.await()
} catch (e: InterruptedException) {
throw RuntimeException(e)
}
decision.get() ?: false
}

val message: Int = when (WiiUtils.importWiiSave(path!!, canOverwrite)) {
Expand All @@ -306,23 +298,18 @@ class MainPresenter(private val mainView: MainView, private val activity: Fragme
}

fun importNANDBin(path: String?) {
MaterialAlertDialogBuilder(activity)
.setMessage(R.string.nand_import_warning)
MaterialAlertDialogBuilder(activity).setMessage(R.string.nand_import_warning)
.setNegativeButton(R.string.no) { dialog: DialogInterface, _: Int -> dialog.dismiss() }
.setPositiveButton(R.string.yes) { dialog: DialogInterface, _: Int ->
dialog.dismiss()
ThreadUtil.runOnThreadAndShowResult(
activity,
R.string.import_in_progress,
R.string.do_not_close_app,
{
activity, R.string.import_in_progress, R.string.do_not_close_app, {
// ImportNANDBin unfortunately doesn't provide any result value...
// It does however show a panic alert if something goes wrong.
WiiUtils.importNANDBin(path!!)
null
})
}
.show()
}.show()
}

private fun launchOnlineUpdate() {
Expand All @@ -332,8 +319,7 @@ class MainPresenter(private val mainView: MainView, private val activity: Fragme
launchUpdateProgressBarFragment(activity)
} else {
SystemMenuNotInstalledDialogFragment().show(
activity.supportFragmentManager,
SystemMenuNotInstalledDialogFragment.TAG
activity.supportFragmentManager, SystemMenuNotInstalledDialogFragment.TAG
)
}
}
Expand All @@ -344,8 +330,7 @@ class MainPresenter(private val mainView: MainView, private val activity: Fragme
EmulationActivity.launchSystemMenu(activity)
} else {
SystemMenuNotInstalledDialogFragment().show(
activity.supportFragmentManager,
SystemMenuNotInstalledDialogFragment.TAG
activity.supportFragmentManager, SystemMenuNotInstalledDialogFragment.TAG
)
}
}
Expand All @@ -372,8 +357,9 @@ class MainPresenter(private val mainView: MainView, private val activity: Fragme

private fun launchUpdateProgressBarFragment(activity: FragmentActivity) {
val progressBarFragment = SystemUpdateProgressBarDialogFragment()
progressBarFragment
.show(activity.supportFragmentManager, SystemUpdateProgressBarDialogFragment.TAG)
progressBarFragment.show(
activity.supportFragmentManager, SystemUpdateProgressBarDialogFragment.TAG
)
progressBarFragment.isCancelable = false
}
}
Expand Down
Loading