android: Show error if invalid keys file is selected

There aren't MIME types specific enough for filtering out files that aren't amiibo or production keys. So here we just check for the extensions "bin" or "keys" where appropriate and stop the process if incorrect. Previously you could select any document and it could cause the app to hang.
This commit is contained in:
Charles Lombardo 2023-04-24 01:59:41 -04:00 committed by bunnei
parent d8c102444c
commit 83dae1739c
2 changed files with 23 additions and 0 deletions

View File

@ -180,6 +180,10 @@ class MainActivity : AppCompatActivity() {
windowInsets
}
private fun hasExtension(path: String, extension: String): Boolean {
return path.substring(path.lastIndexOf(".") + 1).contains(extension)
}
val getGamesDirectory =
registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { result ->
if (result == null)
@ -212,6 +216,15 @@ class MainActivity : AppCompatActivity() {
if (result == null)
return@registerForActivityResult
if (!hasExtension(result.toString(), "keys")) {
Toast.makeText(
applicationContext,
R.string.invalid_keys_file,
Toast.LENGTH_SHORT
).show()
return@registerForActivityResult
}
val takeFlags =
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
contentResolver.takePersistableUriPermission(
@ -243,6 +256,15 @@ class MainActivity : AppCompatActivity() {
if (result == null)
return@registerForActivityResult
if (!hasExtension(result.toString(), "bin")) {
Toast.makeText(
applicationContext,
R.string.invalid_keys_file,
Toast.LENGTH_SHORT
).show()
return@registerForActivityResult
}
val takeFlags =
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
contentResolver.takePersistableUriPermission(

View File

@ -35,6 +35,7 @@
<string name="install_prod_keys_description">Required to decrypt retail games</string>
<string name="install_amiibo_keys">Install Amiibo Keys</string>
<string name="install_amiibo_keys_description">Required to use Amiibo in game</string>
<string name="invalid_keys_file">Invalid keys file selected</string>
<string name="install_keys_success">Keys successfully installed</string>
<string name="install_keys_failure">Keys file (prod.keys) is invalid</string>
<string name="install_amiibo_keys_failure">Keys file (key_retail.bin) is invalid</string>