From df60fd91a86c314a5b64bc4d3ce3c15f391c50aa Mon Sep 17 00:00:00 2001 From: OpenSauce04 Date: Wed, 25 Sep 2024 17:48:53 +0100 Subject: [PATCH] Revert "Android: Android 15 URI fixes" This reverts commit 620abbc8dd35d4eea0a98be2178f1b58100b457e. --- .../android/fragments/HomeSettingsFragment.kt | 3 +- .../Lime3DSDirectoryDialogFragment.kt | 2 +- .../SelectUserDirectoryDialogFragment.kt | 3 +- .../github/lime3ds/android/utils/FileUtil.kt | 60 ++++++++----------- 4 files changed, 29 insertions(+), 39 deletions(-) diff --git a/src/android/app/src/main/java/io/github/lime3ds/android/fragments/HomeSettingsFragment.kt b/src/android/app/src/main/java/io/github/lime3ds/android/fragments/HomeSettingsFragment.kt index c9f62c9e8..bc6868fb7 100644 --- a/src/android/app/src/main/java/io/github/lime3ds/android/fragments/HomeSettingsFragment.kt +++ b/src/android/app/src/main/java/io/github/lime3ds/android/fragments/HomeSettingsFragment.kt @@ -5,7 +5,6 @@ package io.github.lime3ds.android.fragments import android.content.Intent -import android.net.Uri import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -160,7 +159,7 @@ class HomeSettingsFragment : Fragment() { R.string.select_lime3ds_user_folder, R.string.select_lime3ds_user_folder_home_description, R.drawable.ic_home, - { mainActivity?.openLime3DSDirectory?.launch(Uri.parse(null)) }, + { mainActivity?.openLime3DSDirectory?.launch(null) }, details = homeViewModel.userDir ), HomeSetting( diff --git a/src/android/app/src/main/java/io/github/lime3ds/android/fragments/Lime3DSDirectoryDialogFragment.kt b/src/android/app/src/main/java/io/github/lime3ds/android/fragments/Lime3DSDirectoryDialogFragment.kt index 930494d62..c6f7c67c0 100644 --- a/src/android/app/src/main/java/io/github/lime3ds/android/fragments/Lime3DSDirectoryDialogFragment.kt +++ b/src/android/app/src/main/java/io/github/lime3ds/android/fragments/Lime3DSDirectoryDialogFragment.kt @@ -60,7 +60,7 @@ class Lime3DSDirectoryDialogFragment : DialogFragment() { } .setNegativeButton(android.R.string.cancel) { _: DialogInterface?, _: Int -> if (!PermissionsHandler.hasWriteAccess(requireContext())) { - (requireActivity() as MainActivity)?.openLime3DSDirectory?.launch(Uri.parse(null)) + (requireActivity() as MainActivity)?.openLime3DSDirectory?.launch(null) } } .show() diff --git a/src/android/app/src/main/java/io/github/lime3ds/android/fragments/SelectUserDirectoryDialogFragment.kt b/src/android/app/src/main/java/io/github/lime3ds/android/fragments/SelectUserDirectoryDialogFragment.kt index 09e7848fc..b73f4b198 100644 --- a/src/android/app/src/main/java/io/github/lime3ds/android/fragments/SelectUserDirectoryDialogFragment.kt +++ b/src/android/app/src/main/java/io/github/lime3ds/android/fragments/SelectUserDirectoryDialogFragment.kt @@ -6,7 +6,6 @@ package io.github.lime3ds.android.fragments import android.app.Dialog import android.content.DialogInterface -import android.net.Uri import android.os.Bundle import androidx.fragment.app.DialogFragment import androidx.fragment.app.FragmentActivity @@ -27,7 +26,7 @@ class SelectUserDirectoryDialogFragment : DialogFragment() { .setTitle(R.string.select_lime3ds_user_folder) .setMessage(R.string.cannot_skip_directory_description) .setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int -> - mainActivity?.openLime3DSDirectory?.launch(Uri.parse(null)) + mainActivity?.openLime3DSDirectory?.launch(null) } .show() } diff --git a/src/android/app/src/main/java/io/github/lime3ds/android/utils/FileUtil.kt b/src/android/app/src/main/java/io/github/lime3ds/android/utils/FileUtil.kt index 9d6b2247d..802d8917e 100644 --- a/src/android/app/src/main/java/io/github/lime3ds/android/utils/FileUtil.kt +++ b/src/android/app/src/main/java/io/github/lime3ds/android/utils/FileUtil.kt @@ -1,4 +1,4 @@ -// Copyright Citra Emulator Project / Lime3DS Emulator Project +// Copyright 2023 Citra Emulator Project // Licensed under GPLv2 or any later version // Refer to the license.txt file included. @@ -16,7 +16,6 @@ import io.github.lime3ds.android.LimeApplication import io.github.lime3ds.android.model.CheapDocument import java.io.BufferedInputStream import java.io.File -import java.io.FileNotFoundException import java.io.FileOutputStream import java.io.IOException import java.io.InputStream @@ -101,21 +100,18 @@ object FileUtil { @JvmStatic fun openContentUri(path: String, openMode: String): Int { try { - val uri = Uri.parse(path) - context.contentResolver.openFileDescriptor(uri, openMode)?.use { parcelFileDescriptor -> - return parcelFileDescriptor.detachFd() - } ?: run { - Log.error("[FileUtil]: Cannot get the file descriptor from uri: $path") - return -1 - } - } catch (e: FileNotFoundException) { - Log.error("[FileUtil]: File not found for uri: $path. ${e.message}") - return -1 - } catch (e: SecurityException) { - Log.error("[FileUtil]: Security exception while accessing uri: $path, ${e.message}") - return -1 + context + .contentResolver + .openFileDescriptor(Uri.parse(path), openMode) + .use { parcelFileDescriptor -> + if (parcelFileDescriptor == null) { + Log.error("[FileUtil]: Cannot get the file descriptor from uri: $path") + return -1 + } + return parcelFileDescriptor.detachFd() + } } catch (e: Exception) { - Log.error("[FileUtil]: Unexpected error while opening content uri: $path. ${e.message}") + Log.error("[FileUtil]: Cannot open content uri, error: " + e.message) return -1 } } @@ -134,9 +130,8 @@ object FileUtil { DocumentsContract.Document.COLUMN_DISPLAY_NAME, DocumentsContract.Document.COLUMN_MIME_TYPE ) - var cursor: Cursor? = null - val results = mutableListOf() - + var c: Cursor? = null + val results: MutableList = ArrayList() try { val docId = if (isRootTreeUri(uri)) { DocumentsContract.getTreeDocumentId(uri) @@ -145,24 +140,21 @@ object FileUtil { } val childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri, docId) - cursor = context.contentResolver.query(childrenUri, columns, null, null, null) - - cursor?.use { c -> - while (c.moveToNext()) { - val documentId = c.getString(c.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DOCUMENT_ID)) - val documentName = c.getString(c.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DISPLAY_NAME)) - val documentMimeType = c.getString(c.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_MIME_TYPE)) - val documentUri = DocumentsContract.buildDocumentUriUsingTree(uri, documentId) - val document = CheapDocument(documentName, documentMimeType, documentUri) - results.add(document) - } - } ?: run { - Log.error("[FileUtil]: Cursor is null") + c = context.contentResolver.query(childrenUri, columns, null, null, null) + while (c!!.moveToNext()) { + val documentId = c.getString(0) + val documentName = c.getString(1) + val documentMimeType = c.getString(2) + val documentUri = DocumentsContract.buildDocumentUriUsingTree(uri, documentId) + val document = CheapDocument(documentName, documentMimeType, documentUri) + results.add(document) } } catch (e: Exception) { - Log.error("[FileUtil]: Cannot list files: ${e.message}") + Log.error("[FileUtil]: Cannot list file error: " + e.message) + } finally { + closeQuietly(c) } - return results.toTypedArray() + return results.toTypedArray() } /**