From 89ffaff58cecd9946d7dba3eb41d5369c4db255c Mon Sep 17 00:00:00 2001 From: Rafael Caetano Date: Mon, 15 Feb 2021 19:11:34 +0000 Subject: [PATCH] Fix BIOS directory validation requiring storage permission --- .../melonds/utils/ConfigurationUtils.kt | 47 +++++++++---------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/me/magnum/melonds/utils/ConfigurationUtils.kt b/app/src/main/java/me/magnum/melonds/utils/ConfigurationUtils.kt index 372ec29..f591eb5 100644 --- a/app/src/main/java/me/magnum/melonds/utils/ConfigurationUtils.kt +++ b/app/src/main/java/me/magnum/melonds/utils/ConfigurationUtils.kt @@ -5,8 +5,6 @@ import android.content.res.AssetFileDescriptor import android.net.Uri import androidx.documentfile.provider.DocumentFile import me.magnum.melonds.domain.model.ConsoleType -import me.magnum.melonds.utils.FileUtils.getAbsolutePathFromSAFUri -import java.io.File import java.io.FileNotFoundException object ConfigurationUtils { @@ -32,9 +30,8 @@ object ConfigurationUtils { return ConfigurationDirResult(ConfigurationDirStatus.UNSET, requiredFiles, fileResults.toTypedArray()) } - val dirPath = getAbsolutePathFromSAFUri(context, configurationDir) - val dir = File(dirPath) - if (!dir.isDirectory) { + val dirDocument = DocumentFile.fromTreeUri(context, configurationDir) + if (dirDocument?.isDirectory != true) { val requiredFiles = getRequiredFiles(consoleType) val fileResults = requiredFiles.map { it to ConfigurationFileStatus.MISSING } return ConfigurationDirResult(ConfigurationDirStatus.INVALID, requiredFiles, fileResults.toTypedArray()) @@ -43,16 +40,16 @@ object ConfigurationUtils { val requiredFiles = getRequiredFiles(consoleType) val fileResults = if (consoleType == ConsoleType.DS) { listOf( - "bios7.bin" to getDSBios7Status(context, dir), - "bios9.bin" to getDSBios9Status(context, dir), - "firmware.bin" to getDSFirmwareStatus(context, dir) + "bios7.bin" to getDSBios7Status(context, dirDocument), + "bios9.bin" to getDSBios9Status(context, dirDocument), + "firmware.bin" to getDSFirmwareStatus(context, dirDocument) ) } else { listOf( - "bios7.bin" to getDSiBios7Status(context, dir), - "bios9.bin" to getDSiBios9Status(context, dir), - "firmware.bin" to getDSiFirmwareStatus(context, dir), - "nand.bin" to getDSiNandStatus(dir) + "bios7.bin" to getDSiBios7Status(context, dirDocument), + "bios9.bin" to getDSiBios9Status(context, dirDocument), + "firmware.bin" to getDSiFirmwareStatus(context, dirDocument), + "nand.bin" to getDSiNandStatus(dirDocument) ) } val result = if (fileResults.any { it.second != ConfigurationFileStatus.PRESENT }) { @@ -63,16 +60,16 @@ object ConfigurationUtils { return ConfigurationDirResult(result, requiredFiles, fileResults.toTypedArray()) } - private fun getDSBios7Status(context: Context, configurationDir: File): ConfigurationFileStatus { + private fun getDSBios7Status(context: Context, configurationDir: DocumentFile): ConfigurationFileStatus { return getBiosFileStatus(context, configurationDir, "bios7.bin", 0x4000.toLong()) } - private fun getDSBios9Status(context: Context, configurationDir: File): ConfigurationFileStatus { + private fun getDSBios9Status(context: Context, configurationDir: DocumentFile): ConfigurationFileStatus { return getBiosFileStatus(context, configurationDir, "bios9.bin", 0x1000.toLong()) } - private fun getDSFirmwareStatus(context: Context, configurationDir: File): ConfigurationFileStatus { - val firmwareDocument = DocumentFile.fromFile(File(configurationDir, "firmware.bin")) + private fun getDSFirmwareStatus(context: Context, configurationDir: DocumentFile): ConfigurationFileStatus { + val firmwareDocument =configurationDir.findFile("firmware.bin") ?: return ConfigurationFileStatus.MISSING return try { context.contentResolver.openAssetFileDescriptor(firmwareDocument.uri, "r")?.use { when (it.length) { @@ -88,16 +85,16 @@ object ConfigurationUtils { } } - private fun getDSiBios7Status(context: Context, configurationDir: File): ConfigurationFileStatus { + private fun getDSiBios7Status(context: Context, configurationDir: DocumentFile): ConfigurationFileStatus { return getBiosFileStatus(context, configurationDir, "bios7.bin", 0x10000.toLong()) } - private fun getDSiBios9Status(context: Context, configurationDir: File): ConfigurationFileStatus { + private fun getDSiBios9Status(context: Context, configurationDir: DocumentFile): ConfigurationFileStatus { return getBiosFileStatus(context, configurationDir, "bios9.bin", 0x10000.toLong()) } - private fun getDSiFirmwareStatus(context: Context, configurationDir: File): ConfigurationFileStatus { - val firmwareDocument = DocumentFile.fromFile(File(configurationDir, "firmware.bin")) + private fun getDSiFirmwareStatus(context: Context, configurationDir: DocumentFile): ConfigurationFileStatus { + val firmwareDocument = configurationDir.findFile("firmware.bin") ?: return ConfigurationFileStatus.MISSING return try { context.contentResolver.openAssetFileDescriptor(firmwareDocument.uri, "r")?.use { when (it.length) { @@ -111,13 +108,13 @@ object ConfigurationUtils { } } - private fun getDSiNandStatus(configurationDir: File): ConfigurationFileStatus { - val firmwareDocument = DocumentFile.fromFile(File(configurationDir, "nand.bin")) - return if (firmwareDocument.isFile) ConfigurationFileStatus.PRESENT else ConfigurationFileStatus.MISSING + private fun getDSiNandStatus(configurationDir: DocumentFile): ConfigurationFileStatus { + val firmwareDocument = configurationDir.findFile("nand.bin") + return if (firmwareDocument?.isFile == true) ConfigurationFileStatus.PRESENT else ConfigurationFileStatus.MISSING } - private fun getBiosFileStatus(context: Context, configurationDir: File, fileName: String, requiredSize: Long): ConfigurationFileStatus { - val biosDocument = DocumentFile.fromFile(File(configurationDir, fileName)) + private fun getBiosFileStatus(context: Context, configurationDir: DocumentFile, fileName: String, requiredSize: Long): ConfigurationFileStatus { + val biosDocument = configurationDir.findFile(fileName) ?: return ConfigurationFileStatus.MISSING return try { context.contentResolver.openAssetFileDescriptor(biosDocument.uri, "r")?.use { when (it.length) {