From 16fe64ad0c6063956369374ca8e23b6a93f625ee Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Sun, 11 Jun 2023 01:41:58 -0400 Subject: [PATCH] android: Use ContentResolver to get file extension Fixes an issue where we try to resolve file extension from URIs. Sometimes the URI will not contain the file name at all and instead a string of numbers. Here we query the content resolver and guarantee that we get a file name every time. --- .../org/yuzu/yuzu_emu/ui/main/MainActivity.kt | 8 +++---- .../java/org/yuzu/yuzu_emu/utils/FileUtil.kt | 24 +++++++++++++++++-- .../app/src/main/res/values/strings.xml | 7 ++---- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt index 124f62f08f..3fca0a7e61 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt @@ -284,10 +284,10 @@ class MainActivity : AppCompatActivity(), ThemeProvider { if (result == null) return@registerForActivityResult - if (!FileUtil.hasExtension(result.toString(), "keys")) { + if (!FileUtil.hasExtension(result, "keys")) { MessageDialogFragment.newInstance( R.string.reading_keys_failure, - R.string.install_keys_failure_extension_description + R.string.install_prod_keys_failure_extension_description ).show(supportFragmentManager, MessageDialogFragment.TAG) return@registerForActivityResult } @@ -379,10 +379,10 @@ class MainActivity : AppCompatActivity(), ThemeProvider { if (result == null) return@registerForActivityResult - if (!FileUtil.hasExtension(result.toString(), "bin")) { + if (!FileUtil.hasExtension(result, "bin")) { MessageDialogFragment.newInstance( R.string.reading_keys_failure, - R.string.install_keys_failure_extension_description + R.string.install_amiibo_keys_failure_extension_description ).show(supportFragmentManager, MessageDialogFragment.TAG) return@registerForActivityResult } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/FileUtil.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/FileUtil.kt index 593dad8d31..492b1ad91e 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/FileUtil.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/FileUtil.kt @@ -7,7 +7,9 @@ import android.content.Context import android.database.Cursor import android.net.Uri import android.provider.DocumentsContract +import android.provider.OpenableColumns import androidx.documentfile.provider.DocumentFile +import org.yuzu.yuzu_emu.YuzuApplication import org.yuzu.yuzu_emu.model.MinimalDocumentFile import java.io.BufferedInputStream import java.io.File @@ -324,7 +326,25 @@ object FileUtil { } } - fun hasExtension(path: String, extension: String): Boolean { - return path.substring(path.lastIndexOf(".") + 1).contains(extension) + fun hasExtension(path: String, extension: String): Boolean = + path.substring(path.lastIndexOf(".") + 1).contains(extension) + + fun hasExtension(uri: Uri, extension: String): Boolean { + val fileName: String? + val cursor = YuzuApplication.appContext.contentResolver.query(uri, null, null, null, null) + val nameIndex = cursor?.getColumnIndex(OpenableColumns.DISPLAY_NAME) + cursor?.moveToFirst() + + if (nameIndex == null) { + return false + } + + fileName = cursor.getString(nameIndex) + cursor.close() + + if (fileName == null) { + return false + } + return fileName.substring(fileName.lastIndexOf(".") + 1).contains(extension) } } diff --git a/src/android/app/src/main/res/values/strings.xml b/src/android/app/src/main/res/values/strings.xml index 0ae69afb47..6e9d475573 100644 --- a/src/android/app/src/main/res/values/strings.xml +++ b/src/android/app/src/main/res/values/strings.xml @@ -65,11 +65,8 @@ Invalid keys file selected Keys successfully installed Error reading encryption keys - - 1. Verify your keys have the .keys extension.\n\n - 2. Keys must not be stored in the Downloads folder.\n\n - Resolve the issue(s) and try again. - + Verify your keys file has a .keys extension and try again. + Verify your keys file has a .bin extension and try again. Invalid encryption keys https://yuzu-emu.org/help/quickstart/#dumping-decryption-keys The selected file is incorrect or corrupt. Please redump your keys.