yuzu-emu
/
yuzu
Archived
1
0
Fork 0

android: Clean up file extension checks

This commit is contained in:
Charles Lombardo 2023-06-20 17:23:20 -04:00
parent a67bdeb2c2
commit 0f31039831
5 changed files with 19 additions and 50 deletions

View File

@ -29,7 +29,6 @@ import org.yuzu.yuzu_emu.layout.AutofitGridLayoutManager
import org.yuzu.yuzu_emu.model.Game import org.yuzu.yuzu_emu.model.Game
import org.yuzu.yuzu_emu.model.GamesViewModel import org.yuzu.yuzu_emu.model.GamesViewModel
import org.yuzu.yuzu_emu.model.HomeViewModel import org.yuzu.yuzu_emu.model.HomeViewModel
import org.yuzu.yuzu_emu.utils.FileUtil
class SearchFragment : Fragment() { class SearchFragment : Fragment() {
private var _binding: FragmentSearchBinding? = null private var _binding: FragmentSearchBinding? = null
@ -128,10 +127,7 @@ class SearchFragment : Fragment() {
R.id.chip_homebrew -> baseList.filter { it.isHomebrew } R.id.chip_homebrew -> baseList.filter { it.isHomebrew }
R.id.chip_retail -> baseList.filter { R.id.chip_retail -> baseList.filter { !it.isHomebrew }
FileUtil.hasExtension(it.path, "xci") ||
FileUtil.hasExtension(it.path, "nsp")
}
else -> baseList else -> baseList
} }

View File

@ -43,7 +43,7 @@ class Game(
companion object { companion object {
val extensions: Set<String> = HashSet( val extensions: Set<String> = HashSet(
listOf(".xci", ".nsp", ".nca", ".nro") listOf("xci", "nsp", "nca", "nro")
) )
} }
} }

View File

@ -294,7 +294,7 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
return@registerForActivityResult return@registerForActivityResult
} }
if (!FileUtil.hasExtension(result, "keys")) { if (FileUtil.getExtension(result) != "keys") {
MessageDialogFragment.newInstance( MessageDialogFragment.newInstance(
R.string.reading_keys_failure, R.string.reading_keys_failure,
R.string.install_prod_keys_failure_extension_description R.string.install_prod_keys_failure_extension_description
@ -391,7 +391,7 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
return@registerForActivityResult return@registerForActivityResult
} }
if (!FileUtil.hasExtension(result, "bin")) { if (FileUtil.getExtension(result) != "bin") {
MessageDialogFragment.newInstance( MessageDialogFragment.newInstance(
R.string.reading_keys_failure, R.string.reading_keys_failure,
R.string.install_amiibo_keys_failure_extension_description R.string.install_amiibo_keys_failure_extension_description

View File

@ -7,7 +7,6 @@ import android.content.Context
import android.database.Cursor import android.database.Cursor
import android.net.Uri import android.net.Uri
import android.provider.DocumentsContract import android.provider.DocumentsContract
import android.provider.OpenableColumns
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import java.io.BufferedInputStream import java.io.BufferedInputStream
import java.io.File import java.io.File
@ -185,19 +184,18 @@ object FileUtil {
/** /**
* Get file display name from given path * Get file display name from given path
* @param path content uri path * @param uri content uri
* @return String display name * @return String display name
*/ */
fun getFilename(context: Context, path: String): String { fun getFilename(uri: Uri): String {
val resolver = context.contentResolver val resolver = YuzuApplication.appContext.contentResolver
val columns = arrayOf( val columns = arrayOf(
DocumentsContract.Document.COLUMN_DISPLAY_NAME DocumentsContract.Document.COLUMN_DISPLAY_NAME
) )
var filename = "" var filename = ""
var c: Cursor? = null var c: Cursor? = null
try { try {
val mUri = Uri.parse(path) c = resolver.query(uri, columns, null, null, null)
c = resolver.query(mUri, columns, null, null, null)
c!!.moveToNext() c!!.moveToNext()
filename = c.getString(0) filename = c.getString(0)
} catch (e: Exception) { } catch (e: Exception) {
@ -326,25 +324,9 @@ object FileUtil {
} }
} }
fun hasExtension(path: String, extension: String): Boolean = fun getExtension(uri: Uri): String {
path.substring(path.lastIndexOf(".") + 1).contains(extension) val fileName = getFilename(uri)
return fileName.substring(fileName.lastIndexOf(".") + 1)
fun hasExtension(uri: Uri, extension: String): Boolean { .lowercase()
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)
} }
} }

View File

@ -6,7 +6,6 @@ package org.yuzu.yuzu_emu.utils
import android.content.SharedPreferences import android.content.SharedPreferences
import android.net.Uri import android.net.Uri
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import java.util.*
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.NativeLibrary
@ -33,15 +32,9 @@ object GameHelper {
val children = FileUtil.listFiles(context, gamesUri) val children = FileUtil.listFiles(context, gamesUri)
for (file in children) { for (file in children) {
if (!file.isDirectory) { if (!file.isDirectory) {
val filename = file.uri.toString()
val extensionStart = filename.lastIndexOf('.')
if (extensionStart > 0) {
val fileExtension = filename.substring(extensionStart)
// Check that the file has an extension we care about before trying to read out of it. // Check that the file has an extension we care about before trying to read out of it.
if (Game.extensions.contains(fileExtension.lowercase(Locale.getDefault()))) { if (Game.extensions.contains(FileUtil.getExtension(file.uri))) {
games.add(getGame(filename)) games.add(getGame(file.uri))
}
} }
} }
} }
@ -59,21 +52,19 @@ object GameHelper {
return games.toList() return games.toList()
} }
private fun getGame(filePath: String): Game { private fun getGame(uri: Uri): Game {
val filePath = uri.toString()
var name = NativeLibrary.getTitle(filePath) var name = NativeLibrary.getTitle(filePath)
// If the game's title field is empty, use the filename. // If the game's title field is empty, use the filename.
if (name.isEmpty()) { if (name.isEmpty()) {
name = filePath.substring(filePath.lastIndexOf("/") + 1) name = FileUtil.getFilename(uri)
} }
var gameId = NativeLibrary.getGameId(filePath) var gameId = NativeLibrary.getGameId(filePath)
// If the game's ID field is empty, use the filename without extension. // If the game's ID field is empty, use the filename without extension.
if (gameId.isEmpty()) { if (gameId.isEmpty()) {
gameId = filePath.substring( gameId = name.substring(0, name.lastIndexOf("."))
filePath.lastIndexOf("/") + 1,
filePath.lastIndexOf(".")
)
} }
val newGame = Game( val newGame = Game(