Handle android file permissions.

This commit is contained in:
Luke Hubmayer-Werner 2024-07-26 21:54:00 +09:30
parent ee2a207c30
commit 546463dda3
1 changed files with 28 additions and 1 deletions

View File

@ -13,6 +13,7 @@ const TYPE_DESCS := globals.TYPE_DESCS
enum {SELECT_ROM, SELECT_SAVE, COMPLETE} enum {SELECT_ROM, SELECT_SAVE, COMPLETE}
var current_mode = SELECT_ROM var current_mode = SELECT_ROM
var android_permissions_granted := false
var cached_cd_bin_paths := {} var cached_cd_bin_paths := {}
var dir := Directory.new() var dir := Directory.new()
@ -53,6 +54,23 @@ static func join_path(tokens) -> String:
var path = '/'.join(tokens) var path = '/'.join(tokens)
return path if path else '/' 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(): func update_view():
var error = dir.list_dir_begin(true, true) var error = dir.list_dir_begin(true, true)
if error != OK: if error != OK:
@ -212,8 +230,10 @@ func init_labels():
func _ready() -> void: func _ready() -> void:
init_labels() init_labels()
if OS.has_feature('windows'): if OS.has_feature('Windows'):
home_path = OS.get_environment('USERPROFILE') home_path = OS.get_environment('USERPROFILE')
elif OS.has_feature('Android'):
home_path = '/storage/emulated/0/Download/'
else: else:
home_path = OS.get_environment('HOME') home_path = OS.get_environment('HOME')
# dir.open(OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS)) # 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('user://'))
print(ProjectSettings.globalize_path(dir.get_current_dir())) print(ProjectSettings.globalize_path(dir.get_current_dir()))
get_tree().connect('files_dropped', self, '_files_dropped') 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: func _files_dropped(filenames: PoolStringArray, screen_idx: int) -> void:
print('Files Dropped signal: screen=%d, files='%screen_idx, filenames) print('Files Dropped signal: screen=%d, files='%screen_idx, filenames)
for filename in filenames: for filename in filenames:
load_file(filename, true) 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: func _update_config() -> void:
var d = self.dir.get_current_dir() var d = self.dir.get_current_dir()
if d != Common.config.get_value('filesystem', 'last_used_directory'): if d != Common.config.get_value('filesystem', 'last_used_directory'):