You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-05 12:10:55 +00:00
[Android] Implement native file picker support
This commit is contained in:
@@ -57,6 +57,7 @@ import com.google.android.vending.expansion.downloader.*
|
||||
import org.godotengine.godot.error.Error
|
||||
import org.godotengine.godot.input.GodotEditText
|
||||
import org.godotengine.godot.input.GodotInputHandler
|
||||
import org.godotengine.godot.io.FilePicker
|
||||
import org.godotengine.godot.io.directory.DirectoryAccessHandler
|
||||
import org.godotengine.godot.io.file.FileAccessHandler
|
||||
import org.godotengine.godot.plugin.AndroidRuntimePlugin
|
||||
@@ -677,6 +678,9 @@ class Godot(private val context: Context) {
|
||||
for (plugin in pluginRegistry.allPlugins) {
|
||||
plugin.onMainActivityResult(requestCode, resultCode, data)
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
FilePicker.handleActivityResult(context, requestCode, resultCode, data)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -890,6 +894,13 @@ class Godot(private val context: Context) {
|
||||
mClipboard.setPrimaryClip(clip)
|
||||
}
|
||||
|
||||
@Keep
|
||||
private fun showFilePicker(currentDirectory: String, filename: String, fileMode: Int, filters: Array<String>) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
FilePicker.showFilePicker(context, getActivity(), currentDirectory, filename, fileMode, filters)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Popup a dialog to input text.
|
||||
*/
|
||||
|
||||
@@ -229,6 +229,11 @@ public class GodotLib {
|
||||
*/
|
||||
public static native void inputDialogCallback(String p_text);
|
||||
|
||||
/**
|
||||
* Invoked on the file picker closed.
|
||||
*/
|
||||
public static native void filePickerCallback(boolean p_ok, String[] p_selected_paths);
|
||||
|
||||
/**
|
||||
* Invoked on the GL thread to configure the height of the virtual keyboard.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
/**************************************************************************/
|
||||
/* FilePicker.kt */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
package org.godotengine.godot.io
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.provider.DocumentsContract
|
||||
import android.util.Log
|
||||
import androidx.annotation.RequiresApi
|
||||
import org.godotengine.godot.GodotLib
|
||||
import org.godotengine.godot.io.file.MediaStoreData
|
||||
|
||||
/**
|
||||
* Utility class for managing file selection and file picker activities.
|
||||
*
|
||||
* It provides methods to launch a file picker and handle the result, supporting various file modes,
|
||||
* including opening files, directories, and saving files.
|
||||
*/
|
||||
internal class FilePicker {
|
||||
companion object {
|
||||
private const val FILE_PICKER_REQUEST = 1000
|
||||
private val TAG = FilePicker::class.java.simpleName
|
||||
|
||||
// Constants for fileMode values
|
||||
private const val FILE_MODE_OPEN_FILE = 0
|
||||
private const val FILE_MODE_OPEN_FILES = 1
|
||||
private const val FILE_MODE_OPEN_DIR = 2
|
||||
private const val FILE_MODE_OPEN_ANY = 3
|
||||
private const val FILE_MODE_SAVE_FILE = 4
|
||||
|
||||
/**
|
||||
* Handles the result from a file picker activity and processes the selected file(s) or directory.
|
||||
*
|
||||
* @param context The context from which the file picker was launched.
|
||||
* @param requestCode The request code used when starting the file picker activity.
|
||||
* @param resultCode The result code returned by the activity.
|
||||
* @param data The intent data containing the selected file(s) or directory.
|
||||
*/
|
||||
@RequiresApi(Build.VERSION_CODES.Q)
|
||||
fun handleActivityResult(context: Context, requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
if (requestCode == FILE_PICKER_REQUEST) {
|
||||
if (resultCode == Activity.RESULT_CANCELED) {
|
||||
Log.d(TAG, "File picker canceled")
|
||||
GodotLib.filePickerCallback(false, emptyArray())
|
||||
return
|
||||
}
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
val selectedPaths: MutableList<String> = mutableListOf()
|
||||
// Handle multiple file selection.
|
||||
val clipData = data?.clipData
|
||||
if (clipData != null) {
|
||||
for (i in 0 until clipData.itemCount) {
|
||||
val uri = clipData.getItemAt(i).uri
|
||||
uri?.let {
|
||||
val filepath = MediaStoreData.getFilePathFromUri(context, uri)
|
||||
if (filepath != null) {
|
||||
selectedPaths.add(filepath)
|
||||
} else {
|
||||
Log.d(TAG, "null filepath URI: $it")
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
val uri: Uri? = data?.data
|
||||
uri?.let {
|
||||
val filepath = MediaStoreData.getFilePathFromUri(context, uri)
|
||||
if (filepath != null) {
|
||||
selectedPaths.add(filepath)
|
||||
} else {
|
||||
Log.d(TAG, "null filepath URI: $it")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedPaths.isNotEmpty()) {
|
||||
GodotLib.filePickerCallback(true, selectedPaths.toTypedArray())
|
||||
} else {
|
||||
GodotLib.filePickerCallback(false, emptyArray())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Launches a file picker activity with specified settings based on the mode, initial directory,
|
||||
* file type filters, and other parameters.
|
||||
*
|
||||
* @param context The context from which to start the file picker.
|
||||
* @param activity The activity instance used to initiate the picker. Required for activity results.
|
||||
* @param currentDirectory The directory path to start the file picker in.
|
||||
* @param filename The name of the file when using save mode.
|
||||
* @param fileMode The mode to operate in, specifying open, save, or directory select.
|
||||
* @param filters Array of MIME types to filter file selection.
|
||||
*/
|
||||
@RequiresApi(Build.VERSION_CODES.Q)
|
||||
fun showFilePicker(context: Context, activity: Activity?, currentDirectory: String, filename: String, fileMode: Int, filters: Array<String>) {
|
||||
val intent = when (fileMode) {
|
||||
FILE_MODE_OPEN_DIR -> Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
||||
FILE_MODE_SAVE_FILE -> Intent(Intent.ACTION_CREATE_DOCUMENT)
|
||||
else -> Intent(Intent.ACTION_OPEN_DOCUMENT)
|
||||
}
|
||||
val initialDirectory = MediaStoreData.getUriFromDirectoryPath(context, currentDirectory)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && initialDirectory != null) {
|
||||
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, initialDirectory)
|
||||
} else {
|
||||
Log.d(TAG, "Error cannot set initial directory")
|
||||
}
|
||||
if (fileMode == FILE_MODE_OPEN_FILES) {
|
||||
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true) // Set multi select for FILE_MODE_OPEN_FILES
|
||||
} else if (fileMode == FILE_MODE_SAVE_FILE) {
|
||||
intent.putExtra(Intent.EXTRA_TITLE, filename) // Set filename for FILE_MODE_SAVE_FILE
|
||||
}
|
||||
// ACTION_OPEN_DOCUMENT_TREE does not support intent type
|
||||
if (fileMode != FILE_MODE_OPEN_DIR) {
|
||||
intent.type = "*/*"
|
||||
if (filters.isNotEmpty()) {
|
||||
if (filters.size == 1) {
|
||||
intent.type = filters[0]
|
||||
} else {
|
||||
intent.putExtra(Intent.EXTRA_MIME_TYPES, filters)
|
||||
}
|
||||
}
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE)
|
||||
}
|
||||
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)
|
||||
activity?.startActivityForResult(intent, FILE_PICKER_REQUEST)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Environment
|
||||
import android.provider.MediaStore
|
||||
import android.util.Log
|
||||
import androidx.annotation.RequiresApi
|
||||
|
||||
import java.io.File
|
||||
@@ -46,6 +47,7 @@ import java.io.FileNotFoundException
|
||||
import java.io.FileOutputStream
|
||||
import java.nio.channels.FileChannel
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of [DataAccess] which handles access and interactions with file and data
|
||||
* under scoped storage via the MediaStore API.
|
||||
@@ -81,6 +83,10 @@ internal class MediaStoreData(context: Context, filePath: String, accessFlag: Fi
|
||||
private const val SELECTION_BY_PATH = "${MediaStore.Files.FileColumns.DISPLAY_NAME} = ? " +
|
||||
" AND ${MediaStore.Files.FileColumns.RELATIVE_PATH} = ?"
|
||||
|
||||
private const val AUTHORITY_MEDIA_DOCUMENTS = "com.android.providers.media.documents"
|
||||
private const val AUTHORITY_EXTERNAL_STORAGE_DOCUMENTS = "com.android.externalstorage.documents"
|
||||
private const val AUTHORITY_DOWNLOADS_DOCUMENTS = "com.android.providers.downloads.documents"
|
||||
|
||||
private fun getSelectionByPathArguments(path: String): Array<String> {
|
||||
return arrayOf(getMediaStoreDisplayName(path), getMediaStoreRelativePath(path))
|
||||
}
|
||||
@@ -230,6 +236,72 @@ internal class MediaStoreData(context: Context, filePath: String, accessFlag: Fi
|
||||
)
|
||||
return updated > 0
|
||||
}
|
||||
|
||||
fun getUriFromDirectoryPath(context: Context, directoryPath: String): Uri? {
|
||||
if (!directoryExists(directoryPath)) {
|
||||
return null
|
||||
}
|
||||
// Check if the path is under external storage.
|
||||
val externalStorageRoot = Environment.getExternalStorageDirectory().absolutePath
|
||||
if (directoryPath.startsWith(externalStorageRoot)) {
|
||||
val relativePath = directoryPath.replaceFirst(externalStorageRoot, "").trim('/')
|
||||
val uri = Uri.Builder()
|
||||
.scheme("content")
|
||||
.authority(AUTHORITY_EXTERNAL_STORAGE_DOCUMENTS)
|
||||
.appendPath("document")
|
||||
.appendPath("primary:$relativePath")
|
||||
.build()
|
||||
return uri
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun getFilePathFromUri(context: Context, uri: Uri): String? {
|
||||
// Converts content uri to filepath.
|
||||
val id = getIdFromUri(uri) ?: return null
|
||||
|
||||
if (uri.authority == AUTHORITY_EXTERNAL_STORAGE_DOCUMENTS) {
|
||||
val split = id.split(":")
|
||||
val fileName = split.last()
|
||||
val relativePath = split.dropLast(1).joinToString("/")
|
||||
val fullPath = File(Environment.getExternalStorageDirectory(), "$relativePath/$fileName").absolutePath
|
||||
return fullPath
|
||||
} else {
|
||||
val id = id.toLongOrNull() ?: return null
|
||||
val dataItems = queryById(context, id)
|
||||
return if (dataItems.isNotEmpty()) {
|
||||
val dataItem = dataItems[0]
|
||||
File(Environment.getExternalStorageDirectory(), File(dataItem.relativePath, dataItem.displayName).toString()).absolutePath
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getIdFromUri(uri: Uri): String? {
|
||||
return try {
|
||||
if (uri.authority == AUTHORITY_EXTERNAL_STORAGE_DOCUMENTS || uri.authority == AUTHORITY_MEDIA_DOCUMENTS || uri.authority == AUTHORITY_DOWNLOADS_DOCUMENTS) {
|
||||
val documentId = uri.lastPathSegment ?: throw IllegalArgumentException("Invalid URI: $uri")
|
||||
documentId.substringAfter(":")
|
||||
} else {
|
||||
throw IllegalArgumentException("Unsupported URI format: $uri")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.d(TAG, "Failed to parse ID from URI: $uri", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private fun directoryExists(path: String): Boolean {
|
||||
return try {
|
||||
val file = File(path)
|
||||
file.isDirectory && file.exists()
|
||||
} catch (e: SecurityException) {
|
||||
Log.d(TAG, "Failed to check directoryExists: $path", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private val id: Long
|
||||
|
||||
Reference in New Issue
Block a user