Skip to content

Commit b2130ef

Browse files
[Android] Implement native file picker support
1 parent 0dfd18c commit b2130ef

File tree

12 files changed

+318
-7
lines changed

12 files changed

+318
-7
lines changed

doc/classes/DisplayServer.xml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@
139139
<description>
140140
Displays OS native dialog for selecting files or directories in the file system.
141141
Each filter string in the [param filters] array should be formatted like this: [code]*.txt,*.doc;Text Files[/code]. The description text of the filter is optional and can be omitted. See also [member FileDialog.filters].
142-
Callbacks have the following arguments: [code]status: bool, selected_paths: PackedStringArray, selected_filter_index: int[/code].
143-
[b]Note:[/b] This method is implemented if the display server has the [constant FEATURE_NATIVE_DIALOG_FILE] feature. Supported platforms include Linux (X11/Wayland), Windows, and macOS.
142+
Callbacks have the following arguments: [code]status: bool, selected_paths: PackedStringArray, selected_filter_index: int[/code]. [b]On Android,[/b] callback argument [code]selected_filter_index[/code] is always zero.
143+
[b]Note:[/b] This method is implemented if the display server has the [constant FEATURE_NATIVE_DIALOG_FILE] feature. Supported platforms include Linux (X11/Wayland), Windows, macOS, and Android.
144144
[b]Note:[/b] [param current_directory] might be ignored.
145-
[b]Note:[/b] On Linux, [param show_hidden] is ignored.
146-
[b]Note:[/b] On macOS, native file dialogs have no title.
145+
[b]Note:[/b] On Android, the filter strings in the [param filters] array should be specified using MIME types, for example:[code]image/png, image/jpeg"[/code]. Additionally, the [param mode] [constant FILE_DIALOG_MODE_OPEN_ANY] is not supported on Android.
146+
[b]Note:[/b] On Android and Linux, [param show_hidden] is ignored.
147+
[b]Note:[/b] On Android and macOS, native file dialogs have no title.
147148
[b]Note:[/b] On macOS, sandboxed apps will save security-scoped bookmarks to retain access to the opened folders across multiple sessions. Use [method OS.get_granted_permissions] to get a list of saved bookmarks.
148149
</description>
149150
</method>
@@ -1889,7 +1890,7 @@
18891890
Display server supports spawning text input dialogs using the operating system's native look-and-feel. See [method dialog_input_text]. [b]Windows, macOS[/b]
18901891
</constant>
18911892
<constant name="FEATURE_NATIVE_DIALOG_FILE" value="25" enum="Feature">
1892-
Display server supports spawning dialogs for selecting files or directories using the operating system's native look-and-feel. See [method file_dialog_show]. [b]Windows, macOS, Linux (X11/Wayland)[/b]
1893+
Display server supports spawning dialogs for selecting files or directories using the operating system's native look-and-feel. See [method file_dialog_show]. [b]Windows, macOS, Linux (X11/Wayland), Android[/b]
18931894
</constant>
18941895
<constant name="FEATURE_NATIVE_DIALOG_FILE_EXTRA" value="26" enum="Feature">
18951896
The display server supports all features of [constant FEATURE_NATIVE_DIALOG_FILE], with the added functionality of Options and native dialog file access to [code]res://[/code] and [code]user://[/code] paths. See [method file_dialog_show] and [method file_dialog_with_options_show]. [b]Windows, macOS, Linux (X11/Wayland)[/b]

doc/classes/FileDialog.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
</member>
147147
<member name="filters" type="PackedStringArray" setter="set_filters" getter="get_filters" default="PackedStringArray()">
148148
The available file type filters. Each filter string in the array should be formatted like this: [code]*.txt,*.doc;Text Files[/code]. The description text of the filter is optional and can be omitted.
149+
[b]Note:[/b] For android native dialog, MIME types are used like this: [code]image/*, application/pdf[/code].
149150
</member>
150151
<member name="mode_overrides_title" type="bool" setter="set_mode_overrides_title" getter="is_mode_overriding_title" default="true">
151152
If [code]true[/code], changing the [member file_mode] property will set the window title accordingly (e.g. setting [member file_mode] to [constant FILE_MODE_OPEN_FILE] will change the window title to "Open a File").
@@ -159,12 +160,13 @@
159160
</member>
160161
<member name="show_hidden_files" type="bool" setter="set_show_hidden_files" getter="is_showing_hidden_files" default="false">
161162
If [code]true[/code], the dialog will show hidden files.
162-
[b]Note:[/b] This property is ignored by native file dialogs on Linux.
163+
[b]Note:[/b] This property is ignored by native file dialogs on Android and Linux.
163164
</member>
164165
<member name="size" type="Vector2i" setter="set_size" getter="get_size" overrides="Window" default="Vector2i(640, 360)" />
165166
<member name="title" type="String" setter="set_title" getter="get_title" overrides="Window" default="&quot;Save a File&quot;" />
166167
<member name="use_native_dialog" type="bool" setter="set_use_native_dialog" getter="get_use_native_dialog" default="false">
167168
If [code]true[/code], and if supported by the current [DisplayServer], OS native dialog will be used instead of custom one.
169+
[b]Note:[/b] On Android, it is only supported when using [constant ACCESS_FILESYSTEM]. For access mode [constant ACCESS_RESOURCES] and [constant ACCESS_USERDATA], the system will fall back to custom FileDialog.
168170
[b]Note:[/b] On Linux and macOS, sandboxed apps always use native dialogs to access the host file system.
169171
[b]Note:[/b] On macOS, sandboxed apps will save security-scoped bookmarks to retain access to the opened folders across multiple sessions. Use [method OS.get_granted_permissions] to get a list of saved bookmarks.
170172
[b]Note:[/b] Native dialogs are isolated from the base process, file dialog properties can't be modified once the dialog is shown.

platform/android/display_server_android.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ bool DisplayServerAndroid::has_feature(Feature p_feature) const {
7272
//case FEATURE_MOUSE_WARP:
7373
//case FEATURE_NATIVE_DIALOG:
7474
case FEATURE_NATIVE_DIALOG_INPUT:
75-
//case FEATURE_NATIVE_DIALOG_FILE:
75+
case FEATURE_NATIVE_DIALOG_FILE:
7676
//case FEATURE_NATIVE_DIALOG_FILE_EXTRA:
7777
//case FEATURE_NATIVE_ICON:
7878
//case FEATURE_WINDOW_TRANSPARENCY:
@@ -190,6 +190,19 @@ void DisplayServerAndroid::emit_input_dialog_callback(String p_text) {
190190
}
191191
}
192192

193+
Error DisplayServerAndroid::file_dialog_show(const String &p_title, const String &p_current_directory, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback) {
194+
GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java();
195+
ERR_FAIL_NULL_V(godot_java, FAILED);
196+
file_picker_callback = p_callback;
197+
return godot_java->show_file_picker(p_current_directory, p_filename, p_mode, p_filters);
198+
}
199+
200+
void DisplayServerAndroid::emit_file_picker_callback(bool p_ok, const Vector<String> &p_selected_paths) {
201+
if (file_picker_callback.is_valid()) {
202+
file_picker_callback.call_deferred(p_ok, p_selected_paths, 0);
203+
}
204+
}
205+
193206
TypedArray<Rect2> DisplayServerAndroid::get_display_cutouts() const {
194207
GodotIOJavaWrapper *godot_io_java = OS_Android::get_singleton()->get_godot_io_java();
195208
ERR_FAIL_NULL_V(godot_io_java, Array());

platform/android/display_server_android.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class DisplayServerAndroid : public DisplayServer {
8888
Callable system_theme_changed;
8989

9090
Callable input_dialog_callback;
91+
Callable file_picker_callback;
9192

9293
void _window_callback(const Callable &p_callable, const Variant &p_arg, bool p_deferred = false) const;
9394

@@ -121,6 +122,9 @@ class DisplayServerAndroid : public DisplayServer {
121122
virtual Error dialog_input_text(String p_title, String p_description, String p_partial, const Callable &p_callback) override;
122123
void emit_input_dialog_callback(String p_text);
123124

125+
virtual Error file_dialog_show(const String &p_title, const String &p_current_directory, const String &p_filename, bool p_show_hidden, const FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback) override;
126+
void emit_file_picker_callback(bool p_ok, const Vector<String> &p_selected_paths);
127+
124128
virtual TypedArray<Rect2> get_display_cutouts() const override;
125129
virtual Rect2i get_display_safe_area() const override;
126130

platform/android/java/lib/src/org/godotengine/godot/Godot.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import com.google.android.vending.expansion.downloader.*
5757
import org.godotengine.godot.error.Error
5858
import org.godotengine.godot.input.GodotEditText
5959
import org.godotengine.godot.input.GodotInputHandler
60+
import org.godotengine.godot.io.FilePicker
6061
import org.godotengine.godot.io.directory.DirectoryAccessHandler
6162
import org.godotengine.godot.io.file.FileAccessHandler
6263
import org.godotengine.godot.plugin.AndroidRuntimePlugin
@@ -677,6 +678,9 @@ class Godot(private val context: Context) {
677678
for (plugin in pluginRegistry.allPlugins) {
678679
plugin.onMainActivityResult(requestCode, resultCode, data)
679680
}
681+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
682+
FilePicker.handleActivityResult(context, requestCode, resultCode, data)
683+
}
680684
}
681685

682686
/**
@@ -890,6 +894,13 @@ class Godot(private val context: Context) {
890894
mClipboard.setPrimaryClip(clip)
891895
}
892896

897+
@Keep
898+
private fun showFilePicker(currentDirectory: String, filename: String, fileMode: Int, filters: Array<String>) {
899+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
900+
FilePicker.showFilePicker(context, getActivity(), currentDirectory, filename, fileMode, filters)
901+
}
902+
}
903+
893904
/**
894905
* Popup a dialog to input text.
895906
*/

platform/android/java/lib/src/org/godotengine/godot/GodotLib.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ public static native boolean initialize(Activity activity,
229229
*/
230230
public static native void inputDialogCallback(String p_text);
231231

232+
/**
233+
* Invoked on the file picker closed.
234+
*/
235+
public static native void filePickerCallback(boolean p_ok, String[] p_selected_paths);
236+
232237
/**
233238
* Invoked on the GL thread to configure the height of the virtual keyboard.
234239
*/
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/**************************************************************************/
2+
/* FilePicker.kt */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
package org.godotengine.godot.io
32+
33+
import android.app.Activity
34+
import android.content.Context
35+
import android.content.Intent
36+
import android.net.Uri
37+
import android.os.Build
38+
import android.provider.DocumentsContract
39+
import android.util.Log
40+
import androidx.annotation.RequiresApi
41+
import org.godotengine.godot.GodotLib
42+
import org.godotengine.godot.io.file.MediaStoreData
43+
44+
/**
45+
* Utility class for managing file selection and file picker activities.
46+
*
47+
* It provides methods to launch a file picker and handle the result, supporting various file modes,
48+
* including opening files, directories, and saving files.
49+
*/
50+
internal class FilePicker {
51+
companion object {
52+
private const val FILE_PICKER_REQUEST = 1000
53+
private val TAG = FilePicker::class.java.simpleName
54+
55+
// Constants for fileMode values
56+
private const val FILE_MODE_OPEN_FILE = 0
57+
private const val FILE_MODE_OPEN_FILES = 1
58+
private const val FILE_MODE_OPEN_DIR = 2
59+
private const val FILE_MODE_OPEN_ANY = 3
60+
private const val FILE_MODE_SAVE_FILE = 4
61+
62+
/**
63+
* Handles the result from a file picker activity and processes the selected file(s) or directory.
64+
*
65+
* @param context The context from which the file picker was launched.
66+
* @param requestCode The request code used when starting the file picker activity.
67+
* @param resultCode The result code returned by the activity.
68+
* @param data The intent data containing the selected file(s) or directory.
69+
*/
70+
@RequiresApi(Build.VERSION_CODES.Q)
71+
fun handleActivityResult(context: Context, requestCode: Int, resultCode: Int, data: Intent?) {
72+
if (requestCode == FILE_PICKER_REQUEST) {
73+
if (resultCode == Activity.RESULT_CANCELED) {
74+
Log.d(TAG, "File picker canceled")
75+
GodotLib.filePickerCallback(false, emptyArray())
76+
return
77+
}
78+
if (resultCode == Activity.RESULT_OK) {
79+
val selectedPaths: MutableList<String> = mutableListOf()
80+
// Handle multiple file selection.
81+
val clipData = data?.clipData
82+
if (clipData != null) {
83+
for (i in 0 until clipData.itemCount) {
84+
val uri = clipData.getItemAt(i).uri
85+
uri?.let {
86+
val filepath = MediaStoreData.getFilePathFromUri(context, uri)
87+
if (filepath != null) {
88+
selectedPaths.add(filepath)
89+
} else {
90+
Log.d(TAG, "null filepath URI: $it")
91+
}
92+
}
93+
}
94+
} else {
95+
val uri: Uri? = data?.data
96+
uri?.let {
97+
val filepath = MediaStoreData.getFilePathFromUri(context, uri)
98+
if (filepath != null) {
99+
selectedPaths.add(filepath)
100+
} else {
101+
Log.d(TAG, "null filepath URI: $it")
102+
}
103+
}
104+
}
105+
106+
if (selectedPaths.isNotEmpty()) {
107+
GodotLib.filePickerCallback(true, selectedPaths.toTypedArray())
108+
} else {
109+
GodotLib.filePickerCallback(false, emptyArray())
110+
}
111+
}
112+
}
113+
}
114+
115+
/**
116+
* Launches a file picker activity with specified settings based on the mode, initial directory,
117+
* file type filters, and other parameters.
118+
*
119+
* @param context The context from which to start the file picker.
120+
* @param activity The activity instance used to initiate the picker. Required for activity results.
121+
* @param currentDirectory The directory path to start the file picker in.
122+
* @param filename The name of the file when using save mode.
123+
* @param fileMode The mode to operate in, specifying open, save, or directory select.
124+
* @param filters Array of MIME types to filter file selection.
125+
*/
126+
@RequiresApi(Build.VERSION_CODES.Q)
127+
fun showFilePicker(context: Context, activity: Activity?, currentDirectory: String, filename: String, fileMode: Int, filters: Array<String>) {
128+
val intent = when (fileMode) {
129+
FILE_MODE_OPEN_DIR -> Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
130+
FILE_MODE_SAVE_FILE -> Intent(Intent.ACTION_CREATE_DOCUMENT)
131+
else -> Intent(Intent.ACTION_OPEN_DOCUMENT)
132+
}
133+
val initialDirectory = MediaStoreData.getUriFromDirectoryPath(context, currentDirectory)
134+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && initialDirectory != null) {
135+
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, initialDirectory)
136+
} else {
137+
Log.d(TAG, "Error cannot set initial directory")
138+
}
139+
if (fileMode == FILE_MODE_OPEN_FILES) {
140+
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true) // Set multi select for FILE_MODE_OPEN_FILES
141+
} else if (fileMode == FILE_MODE_SAVE_FILE) {
142+
intent.putExtra(Intent.EXTRA_TITLE, filename) // Set filename for FILE_MODE_SAVE_FILE
143+
}
144+
// ACTION_OPEN_DOCUMENT_TREE does not support intent type
145+
if (fileMode != FILE_MODE_OPEN_DIR) {
146+
intent.type = "*/*"
147+
if (filters.isNotEmpty()) {
148+
if (filters.size == 1) {
149+
intent.type = filters[0]
150+
} else {
151+
intent.putExtra(Intent.EXTRA_MIME_TYPES, filters)
152+
}
153+
}
154+
intent.addCategory(Intent.CATEGORY_OPENABLE)
155+
}
156+
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)
157+
activity?.startActivityForResult(intent, FILE_PICKER_REQUEST)
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)