From 26f9d1f1223ad88d42383de75abfd3226cf03a2e Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Thu, 28 Sep 2023 13:30:59 -0400 Subject: [PATCH] android: Use application context for all FileUtil functions --- .../java/org/yuzu/yuzu_emu/NativeLibrary.kt | 14 ++++----- .../java/org/yuzu/yuzu_emu/YuzuApplication.kt | 2 +- .../fragments/HomeSettingsFragment.kt | 2 +- .../org/yuzu/yuzu_emu/ui/main/MainActivity.kt | 10 +++---- .../org/yuzu/yuzu_emu/utils/DocumentsTree.kt | 7 ++--- .../java/org/yuzu/yuzu_emu/utils/FileUtil.kt | 29 +++++++++---------- .../org/yuzu/yuzu_emu/utils/GameHelper.kt | 4 +-- .../yuzu/yuzu_emu/utils/GpuDriverHelper.kt | 7 +++-- 8 files changed, 34 insertions(+), 41 deletions(-) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt index 6e39e542b9..115f72710a 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt @@ -15,13 +15,9 @@ import androidx.annotation.Keep import androidx.fragment.app.DialogFragment import com.google.android.material.dialog.MaterialAlertDialogBuilder 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.utils.DocumentsTree.Companion.isNativePath -import org.yuzu.yuzu_emu.utils.FileUtil.exists -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.FileUtil import org.yuzu.yuzu_emu.utils.Log import org.yuzu.yuzu_emu.utils.SerializableHelper.serializable @@ -75,7 +71,7 @@ object NativeLibrary { return if (isNativePath(path!!)) { YuzuApplication.documentsTree!!.openContentUri(path, openmode) } else { - openContentUri(appContext, path, openmode) + FileUtil.openContentUri(path, openmode) } } @@ -85,7 +81,7 @@ object NativeLibrary { return if (isNativePath(path!!)) { YuzuApplication.documentsTree!!.getFileSize(path) } else { - getFileSize(appContext, path) + FileUtil.getFileSize(path) } } @@ -95,7 +91,7 @@ object NativeLibrary { return if (isNativePath(path!!)) { YuzuApplication.documentsTree!!.exists(path) } else { - exists(appContext, path) + FileUtil.exists(path) } } @@ -105,7 +101,7 @@ object NativeLibrary { return if (isNativePath(path!!)) { YuzuApplication.documentsTree!!.isDirectory(path) } else { - isDirectory(appContext, path) + FileUtil.isDirectory(path) } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/YuzuApplication.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/YuzuApplication.kt index 9561748cbb..8c053670c6 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/YuzuApplication.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/YuzuApplication.kt @@ -47,7 +47,7 @@ class YuzuApplication : Application() { application = this documentsTree = DocumentsTree() DirectoryInitialization.start() - GpuDriverHelper.initializeDriverParameters(applicationContext) + GpuDriverHelper.initializeDriverParameters() NativeLibrary.logDeviceInfo() createNotificationChannels() diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/HomeSettingsFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/HomeSettingsFragment.kt index 8923c0ea29..18857db2d7 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/HomeSettingsFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/HomeSettingsFragment.kt @@ -304,7 +304,7 @@ class HomeSettingsFragment : Fragment() { .setMessage(driverName) .setNegativeButton(android.R.string.cancel, null) .setNeutralButton(R.string.select_gpu_driver_default) { _: DialogInterface?, _: Int -> - GpuDriverHelper.installDefaultDriver(requireContext()) + GpuDriverHelper.installDefaultDriver() Toast.makeText( requireContext(), R.string.select_gpu_driver_use_default, 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 0fa5df5e5f..ac96c82071 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 @@ -343,7 +343,6 @@ class MainActivity : AppCompatActivity(), ThemeProvider { val dstPath = DirectoryInitialization.userDirectory + "/keys/" if (FileUtil.copyUriToInternalStorage( - applicationContext, result, dstPath, "prod.keys" @@ -446,7 +445,6 @@ class MainActivity : AppCompatActivity(), ThemeProvider { val dstPath = DirectoryInitialization.userDirectory + "/keys/" if (FileUtil.copyUriToInternalStorage( - applicationContext, result, dstPath, "key_retail.bin" @@ -493,20 +491,20 @@ class MainActivity : AppCompatActivity(), ThemeProvider { withContext(Dispatchers.IO) { // Ignore file exceptions when a user selects an invalid zip try { - GpuDriverHelper.installCustomDriver(applicationContext, result) + GpuDriverHelper.installCustomDriver(result) } catch (_: IOException) { } withContext(Dispatchers.Main) { installationDialog.dismiss() - val driverName = GpuDriverHelper.customDriverName - if (driverName != null) { + val driverData = GpuDriverHelper.customDriverData + if (driverData.name != null) { Toast.makeText( applicationContext, getString( R.string.select_gpu_driver_install_success, - driverName + driverData.name ), Toast.LENGTH_SHORT ).show() diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DocumentsTree.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DocumentsTree.kt index cf226ad945..eafcf9e427 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DocumentsTree.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/DocumentsTree.kt @@ -7,7 +7,6 @@ import android.net.Uri import androidx.documentfile.provider.DocumentFile import java.io.File import java.util.* -import org.yuzu.yuzu_emu.YuzuApplication import org.yuzu.yuzu_emu.model.MinimalDocumentFile class DocumentsTree { @@ -22,7 +21,7 @@ class DocumentsTree { fun openContentUri(filepath: String, openMode: String?): Int { 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 { @@ -30,7 +29,7 @@ class DocumentsTree { return if (node == null || node.isDirectory) { 0 } 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 */ private fun structTree(parent: DocumentsNode) { - val documents = FileUtil.listFiles(YuzuApplication.appContext, parent.uri!!) + val documents = FileUtil.listFiles(parent.uri!!) for (document in documents) { val node = DocumentsNode(document) node.parent = parent 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 c3f53f1c52..a5f89bba6c 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 @@ -3,7 +3,6 @@ package org.yuzu.yuzu_emu.utils -import android.content.Context import android.database.Cursor import android.net.Uri import android.provider.DocumentsContract @@ -29,6 +28,8 @@ object FileUtil { const val APPLICATION_OCTET_STREAM = "application/octet-stream" const val TEXT_PLAIN = "text/plain" + private val context get() = YuzuApplication.appContext + /** * Create a file from directory with filename. * @param context Application context @@ -36,11 +37,11 @@ object FileUtil { * @param filename file display name. * @return boolean */ - fun createFile(context: Context?, directory: String?, filename: String): DocumentFile? { + fun createFile(directory: String?, filename: String): DocumentFile? { var decodedFilename = filename try { 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) var mimeType = APPLICATION_OCTET_STREAM if (decodedFilename.endsWith(".txt")) { @@ -56,16 +57,15 @@ object FileUtil { /** * Create a directory from directory with filename. - * @param context Application context * @param directory parent path for directory. * @param directoryName directory display name. * @return boolean */ - fun createDir(context: Context?, directory: String?, directoryName: String?): DocumentFile? { + fun createDir(directory: String?, directoryName: String?): DocumentFile? { var decodedDirectoryName = directoryName try { 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) val isExist = parent.findFile(decodedDirectoryName) return isExist ?: parent.createDirectory(decodedDirectoryName) @@ -77,13 +77,12 @@ object FileUtil { /** * Open content uri and return file descriptor to JNI. - * @param context Application context * @param path Native content uri path * @param openMode will be one of "r", "r", "rw", "wa", "rwa" * @return file descriptor */ @JvmStatic - fun openContentUri(context: Context, path: String, openMode: String?): Int { + fun openContentUri(path: String, openMode: String?): Int { try { val uri = Uri.parse(path) val parcelFileDescriptor = context.contentResolver.openFileDescriptor(uri, openMode!!) @@ -103,11 +102,10 @@ object FileUtil { /** * Reference: https://stackoverflow.com/questions/42186820/documentfile-is-very-slow * This function will be faster than DoucmentFile.listFiles - * @param context Application context * @param uri Directory uri. * @return CheapDocument lists. */ - fun listFiles(context: Context, uri: Uri): Array { + fun listFiles(uri: Uri): Array { val resolver = context.contentResolver val columns = arrayOf( DocumentsContract.Document.COLUMN_DOCUMENT_ID, @@ -145,7 +143,7 @@ object FileUtil { * @param path Native content uri path * @return bool */ - fun exists(context: Context, path: String?): Boolean { + fun exists(path: String?): Boolean { var c: Cursor? = null try { val mUri = Uri.parse(path) @@ -165,7 +163,7 @@ object FileUtil { * @param path content uri path * @return bool */ - fun isDirectory(context: Context, path: String): Boolean { + fun isDirectory(path: String): Boolean { val resolver = context.contentResolver val columns = arrayOf( DocumentsContract.Document.COLUMN_MIME_TYPE @@ -210,10 +208,10 @@ object FileUtil { return filename } - fun getFilesName(context: Context, path: String): Array { + fun getFilesName(path: String): Array { val uri = Uri.parse(path) val files: MutableList = ArrayList() - for (file in listFiles(context, uri)) { + for (file in listFiles(uri)) { files.add(file.filename) } return files.toTypedArray() @@ -225,7 +223,7 @@ object FileUtil { * @return long file size */ @JvmStatic - fun getFileSize(context: Context, path: String): Long { + fun getFileSize(path: String): Long { val resolver = context.contentResolver val columns = arrayOf( DocumentsContract.Document.COLUMN_SIZE @@ -246,7 +244,6 @@ object FileUtil { } fun copyUriToInternalStorage( - context: Context, sourceUri: Uri?, destinationParentPath: String, destinationFilename: String diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameHelper.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameHelper.kt index e0ee29c9ba..9001ca9ab3 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameHelper.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameHelper.kt @@ -30,7 +30,7 @@ object GameHelper { // Ensure keys are loaded so that ROM metadata can be decrypted. NativeLibrary.reloadKeys() - addGamesRecursive(games, FileUtil.listFiles(context, gamesUri), 3) + addGamesRecursive(games, FileUtil.listFiles(gamesUri), 3) // Cache list of games found on disk val serializedGames = mutableSetOf() @@ -58,7 +58,7 @@ object GameHelper { if (it.isDirectory) { addGamesRecursive( games, - FileUtil.listFiles(YuzuApplication.appContext, it.uri), + FileUtil.listFiles(it.uri), depth - 1 ) } else { diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.kt index 1d4695a2af..296a8f1cfe 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.kt @@ -13,6 +13,7 @@ import java.io.IOException import java.util.zip.ZipInputStream import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.utils.FileUtil.copyUriToInternalStorage +import org.yuzu.yuzu_emu.YuzuApplication object GpuDriverHelper { private const val META_JSON_FILENAME = "meta.json" @@ -61,6 +62,7 @@ object GpuDriverHelper { // Initialize the driver installation directory. driverInstallationPath = context.filesDir.canonicalPath + "/gpu_driver/" + .filesDir.canonicalPath + "/gpu_driver/" } catch (e: IOException) { throw RuntimeException(e) } @@ -70,6 +72,7 @@ object GpuDriverHelper { // Initialize hook libraries directory. hookLibPath = context.applicationInfo.nativeLibraryDir + "/" + hookLibPath = YuzuApplication.appContext.applicationInfo.nativeLibraryDir + "/" // Initialize GPU driver. NativeLibrary.initializeGpuDriver( @@ -81,15 +84,15 @@ object GpuDriverHelper { } fun installDefaultDriver(context: Context) { + fun installDefaultDriver() { // Removing the installed driver will result in the backend using the default system driver. val driverInstallationDir = File(driverInstallationPath!!) deleteRecursive(driverInstallationDir) - initializeDriverParameters(context) } fun installCustomDriver(context: Context, driverPathUri: Uri?) { // Revert to system default in the event the specified driver is bad. - installDefaultDriver(context) + installDefaultDriver() // Ensure we have directories. initializeDirectories()