From 57541d53d3ca1b0de4edac56ba65f7d2f1306470 Mon Sep 17 00:00:00 2001 From: Anish Mishra Date: Mon, 18 Nov 2024 15:42:05 +0530 Subject: Android: Extension support for native file dialog --- .../lib/src/org/godotengine/godot/io/FilePicker.kt | 46 ++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'platform/android/java/lib/src/org/godotengine') diff --git a/platform/android/java/lib/src/org/godotengine/godot/io/FilePicker.kt b/platform/android/java/lib/src/org/godotengine/godot/io/FilePicker.kt index 2befe0583b..19fb452892 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/io/FilePicker.kt +++ b/platform/android/java/lib/src/org/godotengine/godot/io/FilePicker.kt @@ -37,6 +37,7 @@ import android.net.Uri import android.os.Build import android.provider.DocumentsContract import android.util.Log +import android.webkit.MimeTypeMap import androidx.annotation.RequiresApi import org.godotengine.godot.GodotLib import org.godotengine.godot.io.file.MediaStoreData @@ -145,10 +146,11 @@ internal class FilePicker { if (fileMode != FILE_MODE_OPEN_DIR) { intent.type = "*/*" if (filters.isNotEmpty()) { - if (filters.size == 1) { - intent.type = filters[0] + val resolvedFilters = filters.map { resolveMimeType(it) }.distinct() + if (resolvedFilters.size == 1) { + intent.type = resolvedFilters[0] } else { - intent.putExtra(Intent.EXTRA_MIME_TYPES, filters) + intent.putExtra(Intent.EXTRA_MIME_TYPES, resolvedFilters.toTypedArray()) } } intent.addCategory(Intent.CATEGORY_OPENABLE) @@ -156,5 +158,43 @@ internal class FilePicker { intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true) activity?.startActivityForResult(intent, FILE_PICKER_REQUEST) } + + /** + * Retrieves the MIME type for a given file extension. + * + * @param ext the extension whose MIME type is to be determined. + * @return the MIME type as a string, or "application/octet-stream" if the type is unknown. + */ + private fun resolveMimeType(ext: String): String { + val mimeTypeMap = MimeTypeMap.getSingleton() + var input = ext + + // Fix for extensions like "*.txt" or ".txt". + if (ext.contains(".")) { + input = ext.substring(ext.indexOf(".") + 1); + } + + // Check if the input is already a valid MIME type. + if (mimeTypeMap.hasMimeType(input)) { + return input + } + + val resolvedMimeType = mimeTypeMap.getMimeTypeFromExtension(input) + if (resolvedMimeType != null) { + return resolvedMimeType + } + // Check for wildcard MIME types like "image/*". + if (input.contains("/*")) { + val category = input.substringBefore("/*") + return when (category) { + "image" -> "image/*" + "video" -> "video/*" + "audio" -> "audio/*" + else -> "application/octet-stream" + } + } + // Fallback to a generic MIME type if the input is neither a valid extension nor MIME type. + return "application/octet-stream" + } } } -- cgit v1.2.3 From 38e0532ffa43a8302a32d683ec7610b21ec0f4a1 Mon Sep 17 00:00:00 2001 From: David Snopek Date: Mon, 25 Nov 2024 15:58:06 -0600 Subject: Android: `OS.create_instance()` should return `-1` on failure --- platform/android/java/lib/src/org/godotengine/godot/Godot.kt | 2 +- platform/android/java/lib/src/org/godotengine/godot/GodotFragment.java | 2 +- platform/android/java/lib/src/org/godotengine/godot/GodotHost.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'platform/android/java/lib/src/org/godotengine') diff --git a/platform/android/java/lib/src/org/godotengine/godot/Godot.kt b/platform/android/java/lib/src/org/godotengine/godot/Godot.kt index 3ad8e6bc9e..f82b44c544 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/Godot.kt +++ b/platform/android/java/lib/src/org/godotengine/godot/Godot.kt @@ -1123,7 +1123,7 @@ class Godot(private val context: Context) { @Keep private fun createNewGodotInstance(args: Array): Int { - return primaryHost?.onNewGodotInstanceRequested(args) ?: 0 + return primaryHost?.onNewGodotInstanceRequested(args) ?: -1 } @Keep diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotFragment.java b/platform/android/java/lib/src/org/godotengine/godot/GodotFragment.java index d60595c0bb..7cfe3ef3e8 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/GodotFragment.java +++ b/platform/android/java/lib/src/org/godotengine/godot/GodotFragment.java @@ -474,7 +474,7 @@ public class GodotFragment extends Fragment implements IDownloaderClient, GodotH if (parentHost != null) { return parentHost.onNewGodotInstanceRequested(args); } - return 0; + return -1; } @Override diff --git a/platform/android/java/lib/src/org/godotengine/godot/GodotHost.java b/platform/android/java/lib/src/org/godotengine/godot/GodotHost.java index 344b73f799..60d1f01b21 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/GodotHost.java +++ b/platform/android/java/lib/src/org/godotengine/godot/GodotHost.java @@ -92,7 +92,7 @@ public interface GodotHost { * @return the id of the new instance. See {@code onGodotForceQuit} */ default int onNewGodotInstanceRequested(String[] args) { - return 0; + return -1; } /** -- cgit v1.2.3