|
|
@ -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
|
|
|
|