1
0
Fork 0
forked from suyu/suyu

android: Use application context for all FileUtil functions

This commit is contained in:
Charles Lombardo 2023-09-28 13:30:59 -04:00
parent a0d56c855c
commit 26f9d1f122
8 changed files with 34 additions and 41 deletions

View file

@ -15,13 +15,9 @@ import androidx.annotation.Keep
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import org.yuzu.yuzu_emu.YuzuApplication.Companion.appContext
import org.yuzu.yuzu_emu.activities.EmulationActivity import org.yuzu.yuzu_emu.activities.EmulationActivity
import org.yuzu.yuzu_emu.utils.DocumentsTree.Companion.isNativePath import org.yuzu.yuzu_emu.utils.DocumentsTree.Companion.isNativePath
import org.yuzu.yuzu_emu.utils.FileUtil.exists import org.yuzu.yuzu_emu.utils.FileUtil
import org.yuzu.yuzu_emu.utils.FileUtil.getFileSize
import org.yuzu.yuzu_emu.utils.FileUtil.isDirectory
import org.yuzu.yuzu_emu.utils.FileUtil.openContentUri
import org.yuzu.yuzu_emu.utils.Log import org.yuzu.yuzu_emu.utils.Log
import org.yuzu.yuzu_emu.utils.SerializableHelper.serializable import org.yuzu.yuzu_emu.utils.SerializableHelper.serializable
@ -75,7 +71,7 @@ object NativeLibrary {
return if (isNativePath(path!!)) { return if (isNativePath(path!!)) {
YuzuApplication.documentsTree!!.openContentUri(path, openmode) YuzuApplication.documentsTree!!.openContentUri(path, openmode)
} else { } else {
openContentUri(appContext, path, openmode) FileUtil.openContentUri(path, openmode)
} }
} }
@ -85,7 +81,7 @@ object NativeLibrary {
return if (isNativePath(path!!)) { return if (isNativePath(path!!)) {
YuzuApplication.documentsTree!!.getFileSize(path) YuzuApplication.documentsTree!!.getFileSize(path)
} else { } else {
getFileSize(appContext, path) FileUtil.getFileSize(path)
} }
} }
@ -95,7 +91,7 @@ object NativeLibrary {
return if (isNativePath(path!!)) { return if (isNativePath(path!!)) {
YuzuApplication.documentsTree!!.exists(path) YuzuApplication.documentsTree!!.exists(path)
} else { } else {
exists(appContext, path) FileUtil.exists(path)
} }
} }
@ -105,7 +101,7 @@ object NativeLibrary {
return if (isNativePath(path!!)) { return if (isNativePath(path!!)) {
YuzuApplication.documentsTree!!.isDirectory(path) YuzuApplication.documentsTree!!.isDirectory(path)
} else { } else {
isDirectory(appContext, path) FileUtil.isDirectory(path)
} }
} }

View file

@ -47,7 +47,7 @@ class YuzuApplication : Application() {
application = this application = this
documentsTree = DocumentsTree() documentsTree = DocumentsTree()
DirectoryInitialization.start() DirectoryInitialization.start()
GpuDriverHelper.initializeDriverParameters(applicationContext) GpuDriverHelper.initializeDriverParameters()
NativeLibrary.logDeviceInfo() NativeLibrary.logDeviceInfo()
createNotificationChannels() createNotificationChannels()

View file

@ -304,7 +304,7 @@ class HomeSettingsFragment : Fragment() {
.setMessage(driverName) .setMessage(driverName)
.setNegativeButton(android.R.string.cancel, null) .setNegativeButton(android.R.string.cancel, null)
.setNeutralButton(R.string.select_gpu_driver_default) { _: DialogInterface?, _: Int -> .setNeutralButton(R.string.select_gpu_driver_default) { _: DialogInterface?, _: Int ->
GpuDriverHelper.installDefaultDriver(requireContext()) GpuDriverHelper.installDefaultDriver()
Toast.makeText( Toast.makeText(
requireContext(), requireContext(),
R.string.select_gpu_driver_use_default, R.string.select_gpu_driver_use_default,

View file

@ -343,7 +343,6 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
val dstPath = DirectoryInitialization.userDirectory + "/keys/" val dstPath = DirectoryInitialization.userDirectory + "/keys/"
if (FileUtil.copyUriToInternalStorage( if (FileUtil.copyUriToInternalStorage(
applicationContext,
result, result,
dstPath, dstPath,
"prod.keys" "prod.keys"
@ -446,7 +445,6 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
val dstPath = DirectoryInitialization.userDirectory + "/keys/" val dstPath = DirectoryInitialization.userDirectory + "/keys/"
if (FileUtil.copyUriToInternalStorage( if (FileUtil.copyUriToInternalStorage(
applicationContext,
result, result,
dstPath, dstPath,
"key_retail.bin" "key_retail.bin"
@ -493,20 +491,20 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
// Ignore file exceptions when a user selects an invalid zip // Ignore file exceptions when a user selects an invalid zip
try { try {
GpuDriverHelper.installCustomDriver(applicationContext, result) GpuDriverHelper.installCustomDriver(result)
} catch (_: IOException) { } catch (_: IOException) {
} }
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
installationDialog.dismiss() installationDialog.dismiss()
val driverName = GpuDriverHelper.customDriverName val driverData = GpuDriverHelper.customDriverData
if (driverName != null) { if (driverData.name != null) {
Toast.makeText( Toast.makeText(
applicationContext, applicationContext,
getString( getString(
R.string.select_gpu_driver_install_success, R.string.select_gpu_driver_install_success,
driverName driverData.name
), ),
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()

View file

@ -7,7 +7,6 @@ import android.net.Uri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import java.io.File import java.io.File
import java.util.* import java.util.*
import org.yuzu.yuzu_emu.YuzuApplication
import org.yuzu.yuzu_emu.model.MinimalDocumentFile import org.yuzu.yuzu_emu.model.MinimalDocumentFile
class DocumentsTree { class DocumentsTree {
@ -22,7 +21,7 @@ class DocumentsTree {
fun openContentUri(filepath: String, openMode: String?): Int { fun openContentUri(filepath: String, openMode: String?): Int {
val node = resolvePath(filepath) ?: return -1 val node = resolvePath(filepath) ?: return -1
return FileUtil.openContentUri(YuzuApplication.appContext, node.uri.toString(), openMode) return FileUtil.openContentUri(node.uri.toString(), openMode)
} }
fun getFileSize(filepath: String): Long { fun getFileSize(filepath: String): Long {
@ -30,7 +29,7 @@ class DocumentsTree {
return if (node == null || node.isDirectory) { return if (node == null || node.isDirectory) {
0 0
} else { } else {
FileUtil.getFileSize(YuzuApplication.appContext, node.uri.toString()) FileUtil.getFileSize(node.uri.toString())
} }
} }
@ -67,7 +66,7 @@ class DocumentsTree {
* @param parent parent node of this level * @param parent parent node of this level
*/ */
private fun structTree(parent: DocumentsNode) { private fun structTree(parent: DocumentsNode) {
val documents = FileUtil.listFiles(YuzuApplication.appContext, parent.uri!!) val documents = FileUtil.listFiles(parent.uri!!)
for (document in documents) { for (document in documents) {
val node = DocumentsNode(document) val node = DocumentsNode(document)
node.parent = parent node.parent = parent

View file

@ -3,7 +3,6 @@
package org.yuzu.yuzu_emu.utils package org.yuzu.yuzu_emu.utils
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
@ -29,6 +28,8 @@ object FileUtil {
const val APPLICATION_OCTET_STREAM = "application/octet-stream" const val APPLICATION_OCTET_STREAM = "application/octet-stream"
const val TEXT_PLAIN = "text/plain" const val TEXT_PLAIN = "text/plain"
private val context get() = YuzuApplication.appContext
/** /**
* Create a file from directory with filename. * Create a file from directory with filename.
* @param context Application context * @param context Application context
@ -36,11 +37,11 @@ object FileUtil {
* @param filename file display name. * @param filename file display name.
* @return boolean * @return boolean
*/ */
fun createFile(context: Context?, directory: String?, filename: String): DocumentFile? { fun createFile(directory: String?, filename: String): DocumentFile? {
var decodedFilename = filename var decodedFilename = filename
try { try {
val directoryUri = Uri.parse(directory) val directoryUri = Uri.parse(directory)
val parent = DocumentFile.fromTreeUri(context!!, directoryUri) ?: return null val parent = DocumentFile.fromTreeUri(context, directoryUri) ?: return null
decodedFilename = URLDecoder.decode(decodedFilename, DECODE_METHOD) decodedFilename = URLDecoder.decode(decodedFilename, DECODE_METHOD)
var mimeType = APPLICATION_OCTET_STREAM var mimeType = APPLICATION_OCTET_STREAM
if (decodedFilename.endsWith(".txt")) { if (decodedFilename.endsWith(".txt")) {
@ -56,16 +57,15 @@ object FileUtil {
/** /**
* Create a directory from directory with filename. * Create a directory from directory with filename.
* @param context Application context
* @param directory parent path for directory. * @param directory parent path for directory.
* @param directoryName directory display name. * @param directoryName directory display name.
* @return boolean * @return boolean
*/ */
fun createDir(context: Context?, directory: String?, directoryName: String?): DocumentFile? { fun createDir(directory: String?, directoryName: String?): DocumentFile? {
var decodedDirectoryName = directoryName var decodedDirectoryName = directoryName
try { try {
val directoryUri = Uri.parse(directory) val directoryUri = Uri.parse(directory)
val parent = DocumentFile.fromTreeUri(context!!, directoryUri) ?: return null val parent = DocumentFile.fromTreeUri(context, directoryUri) ?: return null
decodedDirectoryName = URLDecoder.decode(decodedDirectoryName, DECODE_METHOD) decodedDirectoryName = URLDecoder.decode(decodedDirectoryName, DECODE_METHOD)
val isExist = parent.findFile(decodedDirectoryName) val isExist = parent.findFile(decodedDirectoryName)
return isExist ?: parent.createDirectory(decodedDirectoryName) return isExist ?: parent.createDirectory(decodedDirectoryName)
@ -77,13 +77,12 @@ object FileUtil {
/** /**
* Open content uri and return file descriptor to JNI. * Open content uri and return file descriptor to JNI.
* @param context Application context
* @param path Native content uri path * @param path Native content uri path
* @param openMode will be one of "r", "r", "rw", "wa", "rwa" * @param openMode will be one of "r", "r", "rw", "wa", "rwa"
* @return file descriptor * @return file descriptor
*/ */
@JvmStatic @JvmStatic
fun openContentUri(context: Context, path: String, openMode: String?): Int { fun openContentUri(path: String, openMode: String?): Int {
try { try {
val uri = Uri.parse(path) val uri = Uri.parse(path)
val parcelFileDescriptor = context.contentResolver.openFileDescriptor(uri, openMode!!) val parcelFileDescriptor = context.contentResolver.openFileDescriptor(uri, openMode!!)
@ -103,11 +102,10 @@ object FileUtil {
/** /**
* Reference: https://stackoverflow.com/questions/42186820/documentfile-is-very-slow * Reference: https://stackoverflow.com/questions/42186820/documentfile-is-very-slow
* This function will be faster than DoucmentFile.listFiles * This function will be faster than DoucmentFile.listFiles
* @param context Application context
* @param uri Directory uri. * @param uri Directory uri.
* @return CheapDocument lists. * @return CheapDocument lists.
*/ */
fun listFiles(context: Context, uri: Uri): Array<MinimalDocumentFile> { fun listFiles(uri: Uri): Array<MinimalDocumentFile> {
val resolver = context.contentResolver val resolver = context.contentResolver
val columns = arrayOf( val columns = arrayOf(
DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DOCUMENT_ID,
@ -145,7 +143,7 @@ object FileUtil {
* @param path Native content uri path * @param path Native content uri path
* @return bool * @return bool
*/ */
fun exists(context: Context, path: String?): Boolean { fun exists(path: String?): Boolean {
var c: Cursor? = null var c: Cursor? = null
try { try {
val mUri = Uri.parse(path) val mUri = Uri.parse(path)
@ -165,7 +163,7 @@ object FileUtil {
* @param path content uri path * @param path content uri path
* @return bool * @return bool
*/ */
fun isDirectory(context: Context, path: String): Boolean { fun isDirectory(path: String): Boolean {
val resolver = context.contentResolver val resolver = context.contentResolver
val columns = arrayOf( val columns = arrayOf(
DocumentsContract.Document.COLUMN_MIME_TYPE DocumentsContract.Document.COLUMN_MIME_TYPE
@ -210,10 +208,10 @@ object FileUtil {
return filename return filename
} }
fun getFilesName(context: Context, path: String): Array<String> { fun getFilesName(path: String): Array<String> {
val uri = Uri.parse(path) val uri = Uri.parse(path)
val files: MutableList<String> = ArrayList() val files: MutableList<String> = ArrayList()
for (file in listFiles(context, uri)) { for (file in listFiles(uri)) {
files.add(file.filename) files.add(file.filename)
} }
return files.toTypedArray() return files.toTypedArray()
@ -225,7 +223,7 @@ object FileUtil {
* @return long file size * @return long file size
*/ */
@JvmStatic @JvmStatic
fun getFileSize(context: Context, path: String): Long { fun getFileSize(path: String): Long {
val resolver = context.contentResolver val resolver = context.contentResolver
val columns = arrayOf( val columns = arrayOf(
DocumentsContract.Document.COLUMN_SIZE DocumentsContract.Document.COLUMN_SIZE
@ -246,7 +244,6 @@ object FileUtil {
} }
fun copyUriToInternalStorage( fun copyUriToInternalStorage(
context: Context,
sourceUri: Uri?, sourceUri: Uri?,
destinationParentPath: String, destinationParentPath: String,
destinationFilename: String destinationFilename: String

View file

@ -30,7 +30,7 @@ object GameHelper {
// Ensure keys are loaded so that ROM metadata can be decrypted. // Ensure keys are loaded so that ROM metadata can be decrypted.
NativeLibrary.reloadKeys() NativeLibrary.reloadKeys()
addGamesRecursive(games, FileUtil.listFiles(context, gamesUri), 3) addGamesRecursive(games, FileUtil.listFiles(gamesUri), 3)
// Cache list of games found on disk // Cache list of games found on disk
val serializedGames = mutableSetOf<String>() val serializedGames = mutableSetOf<String>()
@ -58,7 +58,7 @@ object GameHelper {
if (it.isDirectory) { if (it.isDirectory) {
addGamesRecursive( addGamesRecursive(
games, games,
FileUtil.listFiles(YuzuApplication.appContext, it.uri), FileUtil.listFiles(it.uri),
depth - 1 depth - 1
) )
} else { } else {

View file

@ -13,6 +13,7 @@ import java.io.IOException
import java.util.zip.ZipInputStream import java.util.zip.ZipInputStream
import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.NativeLibrary
import org.yuzu.yuzu_emu.utils.FileUtil.copyUriToInternalStorage import org.yuzu.yuzu_emu.utils.FileUtil.copyUriToInternalStorage
import org.yuzu.yuzu_emu.YuzuApplication
object GpuDriverHelper { object GpuDriverHelper {
private const val META_JSON_FILENAME = "meta.json" private const val META_JSON_FILENAME = "meta.json"
@ -61,6 +62,7 @@ object GpuDriverHelper {
// Initialize the driver installation directory. // Initialize the driver installation directory.
driverInstallationPath = context.filesDir.canonicalPath + "/gpu_driver/" driverInstallationPath = context.filesDir.canonicalPath + "/gpu_driver/"
.filesDir.canonicalPath + "/gpu_driver/"
} catch (e: IOException) { } catch (e: IOException) {
throw RuntimeException(e) throw RuntimeException(e)
} }
@ -70,6 +72,7 @@ object GpuDriverHelper {
// Initialize hook libraries directory. // Initialize hook libraries directory.
hookLibPath = context.applicationInfo.nativeLibraryDir + "/" hookLibPath = context.applicationInfo.nativeLibraryDir + "/"
hookLibPath = YuzuApplication.appContext.applicationInfo.nativeLibraryDir + "/"
// Initialize GPU driver. // Initialize GPU driver.
NativeLibrary.initializeGpuDriver( NativeLibrary.initializeGpuDriver(
@ -81,15 +84,15 @@ object GpuDriverHelper {
} }
fun installDefaultDriver(context: Context) { fun installDefaultDriver(context: Context) {
fun installDefaultDriver() {
// Removing the installed driver will result in the backend using the default system driver. // Removing the installed driver will result in the backend using the default system driver.
val driverInstallationDir = File(driverInstallationPath!!) val driverInstallationDir = File(driverInstallationPath!!)
deleteRecursive(driverInstallationDir) deleteRecursive(driverInstallationDir)
initializeDriverParameters(context)
} }
fun installCustomDriver(context: Context, driverPathUri: Uri?) { fun installCustomDriver(context: Context, driverPathUri: Uri?) {
// Revert to system default in the event the specified driver is bad. // Revert to system default in the event the specified driver is bad.
installDefaultDriver(context) installDefaultDriver()
// Ensure we have directories. // Ensure we have directories.
initializeDirectories() initializeDirectories()