From 546463dda3ec996b1f8368ea92409a1a40fceea2 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Fri, 26 Jul 2024 21:54:00 +0930 Subject: [PATCH] Handle android file permissions. --- widgets/FileSelect.gd | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/widgets/FileSelect.gd b/widgets/FileSelect.gd index 14e4732..10d32f5 100644 --- a/widgets/FileSelect.gd +++ b/widgets/FileSelect.gd @@ -13,6 +13,7 @@ const TYPE_DESCS := globals.TYPE_DESCS enum {SELECT_ROM, SELECT_SAVE, COMPLETE} var current_mode = SELECT_ROM +var android_permissions_granted := false var cached_cd_bin_paths := {} var dir := Directory.new() @@ -53,6 +54,23 @@ static func join_path(tokens) -> String: var path = '/'.join(tokens) return path if path else '/' +const REQUIRED_ANDROID_PERMISSIONS := PoolStringArray(['MANAGE_EXTERNAL_FILES', 'READ_EXTERNAL_FILES', 'WRITE_EXTERNAL_FILES']) +func check_android_permissions() -> bool: + if self.android_permissions_granted: + return true + var permissions_granted := OS.get_granted_permissions() + for perm in REQUIRED_ANDROID_PERMISSIONS: + if not (perm in permissions_granted): + return false + self.android_permissions_granted = true + return true +func update_android_permissions() -> void: + if not OS.has_feature('Android'): + return + if self.check_android_permissions(): + return + OS.request_permissions() + func update_view(): var error = dir.list_dir_begin(true, true) if error != OK: @@ -212,8 +230,10 @@ func init_labels(): func _ready() -> void: init_labels() - if OS.has_feature('windows'): + if OS.has_feature('Windows'): home_path = OS.get_environment('USERPROFILE') + elif OS.has_feature('Android'): + home_path = '/storage/emulated/0/Download/' else: home_path = OS.get_environment('HOME') # dir.open(OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS)) @@ -233,12 +253,19 @@ func _ready() -> void: print(ProjectSettings.globalize_path('user://')) print(ProjectSettings.globalize_path(dir.get_current_dir())) get_tree().connect('files_dropped', self, '_files_dropped') + get_tree().connect('on_request_permissions_result', self, '_on_request_permissions_result') + self.update_android_permissions() func _files_dropped(filenames: PoolStringArray, screen_idx: int) -> void: print('Files Dropped signal: screen=%d, files='%screen_idx, filenames) for filename in filenames: load_file(filename, true) +func _on_request_permissions_result(permission: String, granted: bool) -> void: + print('_on_request_permissions_result: %s = %s' % [permission, granted]) + # if granted and (permission in REQUIRED_ANDROID_PERMISSIONS): + self.call_deferred('update_view') + func _update_config() -> void: var d = self.dir.get_current_dir() if d != Common.config.get_value('filesystem', 'last_used_directory'):