11@tool
22extends VBoxContainer
33
4- const ADB_PATH := "/home/anish/Android/Sdk/platform-tools/adb"
5- const PACKAGE_NAME := "org.godotengine.editor.v4.debug"
6- const DATA_ROOT := "/data/data/" + PACKAGE_NAME
74const TMP_DIR := "/data/local/tmp"
85const STORAGE_ROOT := "/storage/emulated/0"
96const PULL_PUSH_TEMP = TMP_DIR + "/godot-device-explorer-plugin"
7+ const ADB_PATH_SETTING = "_android-device-explorer/adb_path"
8+ const PACKAGE_NAME_SETTING = "_android-device-explorer/package_name"
9+
10+ var adb_path : String
11+ var package_name : String
12+ var app_data_dir : String
1013
1114var tree : Tree
1215var devices_btn : OptionButton
13- var menu_button : MenuButton
16+ var dock_menu_button : MenuButton
1417
15- var current_device := ""
18+ var current_device : String
1619var show_all := false
1720
1821enum ContextMenu {
@@ -26,6 +29,21 @@ enum ContextMenu {
2629}
2730
2831func _ready () -> void :
32+ var saved_adb_path = EditorInterface .get_editor_settings ().get_setting (ADB_PATH_SETTING )
33+ var saved_package_name = EditorInterface .get_editor_settings ().get_setting (PACKAGE_NAME_SETTING )
34+
35+ if saved_adb_path != null :
36+ adb_path = saved_adb_path
37+ else :
38+ adb_path = EditorInterface .get_editor_settings ().get_setting ("export/android/android_sdk_path" ).path_join ("platform-tools/adb" )
39+
40+ if saved_package_name != null :
41+ package_name = saved_package_name
42+ app_data_dir = "/data/data/" + package_name
43+
44+ if package_name .is_empty () or adb_path .is_empty ():
45+ _show_config_dilaog ()
46+
2947 _setup_ui ()
3048 _load_devices ()
3149
@@ -42,16 +60,19 @@ func _setup_ui() -> void:
4260
4361 var reload_btn = Button .new ()
4462 reload_btn .icon = get_theme_icon ("Reload" , "EditorIcons" )
63+ reload_btn .tooltip_text = "Reload"
4564 reload_btn .pressed .connect (_load_devices )
4665 hbox .add_child (reload_btn )
4766
48- menu_button = MenuButton .new ()
49- menu_button .icon = get_theme_icon ("GuiTabMenuHl" , "EditorIcons" )
50- var popup := menu_button .get_popup ()
67+ dock_menu_button = MenuButton .new ()
68+ dock_menu_button .icon = get_theme_icon ("GuiTabMenuHl" , "EditorIcons" )
69+ var popup := dock_menu_button .get_popup ()
5170 popup .add_check_item ("Show Full Filesystem" , 0 )
5271 popup .set_item_checked (0 , show_all )
53- popup .id_pressed .connect (_on_menu_item_pressed )
54- hbox .add_child (menu_button )
72+ popup .add_separator ()
73+ popup .add_icon_item (get_theme_icon ("GDScript" , "EditorIcons" ), "Open config Window" , 1 )
74+ popup .id_pressed .connect (_on_dock_menu_item_pressed )
75+ hbox .add_child (dock_menu_button )
5576
5677 tree = Tree .new ()
5778 tree .size_flags_vertical = Control .SIZE_EXPAND_FILL
@@ -67,6 +88,16 @@ func _on_item_mouse_selected(pos: Vector2, mouse_button_index: int) -> void:
6788 create_context_menu ()
6889
6990
91+ func _on_dock_menu_item_pressed (id : int ) -> void :
92+ match id :
93+ 0 :
94+ show_all = ! show_all
95+ dock_menu_button .get_popup ().set_item_checked (0 , show_all )
96+ _load_root ()
97+ 1 :
98+ _show_config_dilaog ()
99+
100+
70101func _load_devices () -> void :
71102 var device_list := _get_devices ()
72103 devices_btn .clear ()
@@ -93,6 +124,9 @@ func _on_device_selected(index: int) -> void:
93124
94125func _load_root () -> void :
95126 tree .clear ()
127+ if current_device .is_empty ():
128+ return
129+
96130 var root := tree .create_item ()
97131
98132 if show_all :
@@ -102,7 +136,7 @@ func _load_root() -> void:
102136 _on_dir_expanded (root )
103137 else :
104138 root .set_text (0 , "Device Scopes" )
105- _create_tree_item (root , "App Data" , DATA_ROOT , true )
139+ _create_tree_item (root , "App Data" , app_data_dir , true )
106140 _create_tree_item (root , "Temp Storage" , TMP_DIR , true )
107141 _create_tree_item (root , "Internal Storage" , STORAGE_ROOT , true )
108142
@@ -174,14 +208,7 @@ func _populate_special_data_dir(parent: TreeItem) -> void:
174208 var data_node = _create_tree_item (parent , "data" , "/data/data" , true , "" , true )
175209 var local_node = _create_tree_item (parent , "local" , "/data/local" , true , "" , true )
176210 var tmp_node = _create_tree_item (local_node , "tmp" , TMP_DIR , true )
177- var pkg_node = _create_tree_item (data_node , PACKAGE_NAME , DATA_ROOT , true )
178-
179-
180- func _on_menu_item_pressed (id : int ) -> void :
181- if id == 0 :
182- show_all = ! show_all
183- menu_button .get_popup ().set_item_checked (0 , show_all )
184- _load_root ()
211+ var pkg_node = _create_tree_item (data_node , package_name , app_data_dir , true )
185212
186213
187214func _show_file_dialog (remote_path : String , is_uploading : bool , is_dir : bool = false ) -> void :
@@ -239,7 +266,46 @@ func _show_create_dialog(remote_path: String, creating_dir: bool) -> void:
239266 dialog .visibility_changed .connect (_dialog_visibility_changed .bind (dialog ))
240267
241268
242- func _dialog_visibility_changed (dialog : AcceptDialog ) -> void :
269+ func _show_config_dilaog () -> void :
270+ var dialog = ConfirmationDialog .new ()
271+ dialog .title = "Device Explorer Configuration"
272+ dialog .ok_button_text = "Save"
273+ dialog .exclusive = false
274+ var vbox := VBoxContainer .new ()
275+ vbox .size_flags_vertical = Control .SIZE_EXPAND_FILL
276+ dialog .add_child (vbox )
277+
278+ var adb_label := Label .new ()
279+ adb_label .text = "ADB Path:"
280+ vbox .add_child (adb_label )
281+ var adb_input := LineEdit .new ()
282+ adb_input .placeholder_text = "Enter ADB executable path..."
283+ adb_input .text = adb_path
284+ adb_input .size_flags_horizontal = Control .SIZE_EXPAND_FILL
285+ vbox .add_child (adb_input )
286+
287+ var package_label := Label .new ()
288+ package_label .text = "Package Name:"
289+ vbox .add_child (package_label )
290+ var pkg_input := LineEdit .new ()
291+ pkg_input .placeholder_text = "Enter app package name..."
292+ pkg_input .text = package_name
293+ pkg_input .size_flags_horizontal = Control .SIZE_EXPAND_FILL
294+ vbox .add_child (pkg_input )
295+
296+ dialog .confirmed .connect (func ():
297+ adb_path = adb_input .text
298+ package_name = pkg_input .text
299+ app_data_dir = "/data/data/" + package_name
300+ EditorInterface .get_editor_settings ().set_setting (ADB_PATH_SETTING , adb_path )
301+ EditorInterface .get_editor_settings ().set_setting (PACKAGE_NAME_SETTING , package_name )
302+ )
303+ add_child (dialog )
304+ dialog .popup_centered (Vector2i (400 , 180 ))
305+ dialog .visibility_changed .connect (_dialog_visibility_changed .bind (dialog ))
306+
307+
308+ func _dialog_visibility_changed (dialog : Window ) -> void :
243309 if not dialog .visible :
244310 dialog .queue_free ()
245311
@@ -248,13 +314,13 @@ func _dialog_visibility_changed(dialog: AcceptDialog) -> void:
248314
249315func _run_adb (p_args : PackedStringArray ) -> String :
250316 var args : PackedStringArray = []
251- if current_device != "" :
317+ if not current_device . is_empty () :
252318 args .append_array (["-s" , current_device ])
253319 args .append_array (p_args )
254320
255321 var output := []
256- OS .execute (ADB_PATH , args , output , true )
257- # print(output)
322+ OS .execute (adb_path , args , output , true )
323+ print (output )
258324 return output [0 ] if output .size () > 0 else ""
259325
260326
@@ -272,8 +338,8 @@ func _get_devices() -> Array[String]:
272338func _list_dir (path : String ) -> Array :
273339 var args := ["shell" ]
274340 var ls_cmd = "ls -1 -F '%s '" % path
275- if path .begins_with (DATA_ROOT ):
276- args .append_array (["run-as" , PACKAGE_NAME , ls_cmd ])
341+ if path .begins_with (app_data_dir ):
342+ args .append_array (["run-as" , package_name , ls_cmd ])
277343 else :
278344 args .append (ls_cmd )
279345
@@ -288,13 +354,13 @@ func _list_dir(path: String) -> Array:
288354
289355
290356func _pull_dir (local_path : String , remote_path : String ) -> void :
291- if not remote_path .begins_with (DATA_ROOT ):
357+ if not remote_path .begins_with (app_data_dir ):
292358 _run_adb (["pull" , remote_path , local_path ])
293359 return
294360
295361 # special handling for app's private data dir
296362 var ls_cmd = "ls -1 -F '%s '" % remote_path
297- var output = _run_adb (["shell" , "run-as" , PACKAGE_NAME , ls_cmd ])
363+ var output = _run_adb (["shell" , "run-as" , package_name , ls_cmd ])
298364
299365 local_path = local_path .path_join (remote_path .get_file ()) # Create the base directory in local path
300366 if not DirAccess .dir_exists_absolute (local_path ):
@@ -316,7 +382,7 @@ func _pull_dir(local_path: String, remote_path: String) -> void:
316382
317383
318384func _pull_single_file (local_path : String , remote_path : String ) -> void :
319- if not remote_path .begins_with (DATA_ROOT ):
385+ if not remote_path .begins_with (app_data_dir ):
320386 _run_adb (["pull" , remote_path , local_path ])
321387 return
322388
@@ -326,13 +392,13 @@ func _pull_single_file(local_path: String, remote_path: String) -> void:
326392
327393 _run_adb (["shell" , "touch" , temp_path ])
328394 var cp_cmd = "cp '%s ' '%s '" % [remote_path , temp_path ]
329- _run_adb (["shell" , "run-as" , PACKAGE_NAME , cp_cmd ])
395+ _run_adb (["shell" , "run-as" , package_name , cp_cmd ])
330396 _run_adb (["pull" , temp_path , local_path ])
331397 _run_adb (["shell" , "rm" , temp_path ])
332398
333399
334400func _push (local_path : String , remote_path : String ) -> void :
335- if not remote_path .begins_with (DATA_ROOT ):
401+ if not remote_path .begins_with (app_data_dir ):
336402 _run_adb (["push" , local_path , remote_path ])
337403 return
338404
@@ -346,7 +412,7 @@ func _push(local_path: String, remote_path: String) -> void:
346412 var exact_remote_path = remote_path .path_join (source_name )
347413
348414 var cp_cmd = "cp -r '%s ' '%s '" % [temp_path , exact_remote_path ]
349- _run_adb (["shell" , "run-as" , PACKAGE_NAME , cp_cmd ])
415+ _run_adb (["shell" , "run-as" , package_name , cp_cmd ])
350416 _run_adb (["shell" , "rm" , "-rf" , temp_path ])
351417
352418 # Finally refresh the tree view to show newly uploaded file
@@ -355,8 +421,8 @@ func _push(local_path: String, remote_path: String) -> void:
355421
356422func _delete (remote_path : String ) -> void :
357423 var delete_cmd = "rm -r '%s '" % remote_path
358- if remote_path .begins_with (DATA_ROOT ):
359- var a = _run_adb (["shell" , "run-as" , PACKAGE_NAME , delete_cmd ])
424+ if remote_path .begins_with (app_data_dir ):
425+ var a = _run_adb (["shell" , "run-as" , package_name , delete_cmd ])
360426 print (a )
361427 else :
362428 _run_adb (["shell" , delete_cmd ])
@@ -372,8 +438,8 @@ func _create_file_or_directory(path: String, creating_dir: bool) -> void:
372438 else :
373439 cmd = "touch '%s '" % path
374440
375- if path .begins_with (DATA_ROOT ):
376- var a = _run_adb (["shell" , "run-as" , PACKAGE_NAME , cmd ])
441+ if path .begins_with (app_data_dir ):
442+ var a = _run_adb (["shell" , "run-as" , package_name , cmd ])
377443 print (a )
378444 else :
379445 _run_adb (["shell" , cmd ])
0 commit comments