Skip to content

Commit 9f253e8

Browse files
Add support for pushing files and directories (UPLOAD)
1 parent 4a994e7 commit 9f253e8

1 file changed

Lines changed: 42 additions & 7 deletions

File tree

addons/android_device_explorer/device_explorer.gd

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,30 @@ func _on_menu_item_pressed(id: int) -> void:
181181
menu_button.get_popup().set_item_checked(0, show_all)
182182
_load_root()
183183

184-
func _show_save_as_dialog(remote_path: String, is_dir: bool) -> void:
184+
185+
func _show_file_dialog(remote_path: String, is_uploading: bool, is_dir: bool = false) -> void:
185186
var file_dialog = EditorFileDialog.new()
186187
file_dialog.access = FileDialog.ACCESS_FILESYSTEM
187-
file_dialog.file_mode = FileDialog.FILE_MODE_OPEN_DIR if is_dir else FileDialog.FILE_MODE_SAVE_FILE
188-
file_dialog.current_file = remote_path.get_file()
189-
file_dialog.file_selected.connect(_pull_single_file.bind(remote_path))
190-
file_dialog.dir_selected.connect(_pull_dir.bind(remote_path))
188+
189+
if is_uploading:
190+
file_dialog.file_mode = FileDialog.FILE_MODE_OPEN_ANY
191+
file_dialog.title = "Select file or directory to upload"
192+
file_dialog.file_selected.connect(_push.bind(remote_path))
193+
file_dialog.dir_selected.connect(_push.bind(remote_path))
194+
else:
195+
if is_dir:
196+
file_dialog.file_mode = FileDialog.FILE_MODE_OPEN_DIR
197+
file_dialog.title = "Save directory to..."
198+
file_dialog.dir_selected.connect(_pull_dir.bind(remote_path))
199+
else:
200+
file_dialog.file_mode = FileDialog.FILE_MODE_SAVE_FILE
201+
file_dialog.title = "Save file as..."
202+
file_dialog.current_file = remote_path.get_file()
203+
file_dialog.file_selected.connect(_pull_single_file.bind(remote_path))
204+
191205
add_child(file_dialog)
192206
file_dialog.popup_file_dialog()
193207

194-
195208
# ADB Handling--------------------------------------------------------------------------------------
196209

197210
func _run_adb(p_args: PackedStringArray) -> String:
@@ -278,6 +291,25 @@ func _pull_single_file(local_path: String, remote_path: String) -> void:
278291
_run_adb(["pull", temp_path, local_path])
279292
_run_adb(["shell", "rm", temp_path])
280293

294+
295+
func _push(local_path: String, remote_path: String) -> void:
296+
if not remote_path.begins_with(DATA_ROOT):
297+
_run_adb(["push", local_path, remote_path])
298+
return
299+
300+
# special handling for app's private data dir
301+
var temp_name = "temp_" + str(Time.get_ticks_usec())
302+
var temp_path = PULL_PUSH_TEMP.path_join(temp_name)
303+
304+
_run_adb(["push", local_path, temp_path])
305+
306+
var source_name = local_path.get_file()
307+
var exact_remote_path = remote_path.path_join(source_name)
308+
309+
var cp_cmd = "cp -r '%s' '%s'" % [temp_path, exact_remote_path]
310+
_run_adb(["shell", "run-as", PACKAGE_NAME, cp_cmd])
311+
_run_adb(["shell", "rm", "-rf", temp_path])
312+
281313
#---------------------------------------------------------------------------------------------------
282314

283315
func _get_icon_for_ext(path: String) -> String:
@@ -327,5 +359,8 @@ func _on_context_menu_item_pressed(id: int) -> void:
327359
match id:
328360
ContextMenu.SAVE_AS:
329361
var meta = item.get_metadata(0)
330-
_show_save_as_dialog(meta.path, meta.is_dir)
362+
_show_file_dialog(meta.path, false, meta.is_dir)
363+
ContextMenu.UPLOAD:
364+
var meta = item.get_metadata(0)
365+
_show_file_dialog(meta.path, true)
331366

0 commit comments

Comments
 (0)