feat: Android-Export über SAF speichern

Der Export-Dialog zeigt auf Android „Speichern unter…“ statt „Öffnen“ und
schreibt die Datei über den System-Dateidialog nach Downloads oder in einen
anderen Ordner. Der Rückgabewert ist eine content://-URI, die FileAccess
direkt schreiben kann – kein Plugin, keine Storage-Permission.

- ExportService.copy_file kopiert die erzeugte Datei an ein Ziel
- ExportPanel schaltet Beschriftung und Sichtbarkeit je Plattform um
  (configure_for_platform ist testbar)
- Ordner-anzeigen, Pfad-kopieren und „nach Export öffnen“ entfallen auf
  Android
- Tests für den Kopierpfad und beide Panel-Modi
This commit is contained in:
torsten 2026-11-15 18:51:14 +01:00
parent 79a0e354b2
commit e2ded06cab
6 changed files with 179 additions and 8 deletions

View file

@ -286,8 +286,13 @@ Schritte.
Panel-Tests ausgelagert.
- [x] Saison datengetrieben: Saison-Auswahl aus `data/`, Voreinstellung nach
`valid_from`, Regelstand in Statusleiste und Prüfdialog.
- [ ] Android-Export teilen: Share-Intent/SAF statt nur `user://exports/`.
Zurückgestellt am 2026-11-13: kein Testgerät verfügbar.
- [x] Android-Export über SAF: „Speichern unter…“ schreibt PDF/SVG/PNG über
den System-Dateidialog nach Downloads oder in einen anderen Ordner. Kein
Plugin, keine Storage-Permission (2026-11-14).
- [ ] Gerätetest Galaxy Tab S9+: Export → „Speichern unter…“ → Downloads,
PDF/SVG/PNG öffnen; Abbruch und fehlende Datei prüfen.
- [ ] Optional: Share-Intent (`ACTION_SEND`) für „Teilen…“ — braucht ein
Android-Plugin und einen Gradle-Build; erst wenn der SAF-Weg nicht reicht.
- [ ] `Ctrl+S`/`Ctrl+O`/`Ctrl+N` und sichtbarer Speicherstatus (auch ohne
Fenstertitel auf Android).

View file

@ -505,6 +505,7 @@ func _ready() -> void:
calibration_dialog.confirmed.connect(_on_calibration_confirmed)
export_panel.export_requested.connect(_on_export_requested)
export_panel.open_requested.connect(_on_export_open_pressed)
export_panel.save_as_requested.connect(_on_export_save_as_pressed)
export_panel.folder_requested.connect(_on_export_folder_pressed)
export_panel.copy_requested.connect(_on_export_copy_path_pressed)
export_panel.message_requested.connect(_show_message)
@ -6993,7 +6994,10 @@ func _perform_export(
_last_export_path = ProjectSettings.globalize_path(path)
_show_export_done()
if open_after:
_open_path_externally(_last_export_path)
if OS.get_name() == "Android":
_on_export_save_as_pressed()
else:
_open_path_externally(_last_export_path)
else:
_show_message("Export fehlgeschlagen.")
@ -7012,6 +7016,9 @@ func _open_path_externally(path: String) -> Error:
func _on_export_open_pressed() -> void:
if OS.get_name() == "Android":
_on_export_save_as_pressed()
return
var error := _open_path_externally(_last_export_path)
if error != OK:
_show_message(
@ -7043,6 +7050,43 @@ func _on_export_copy_path_pressed() -> void:
export_panel.set_copy_label("Kopiert")
## Opens Android's Storage Access Framework save dialog for the last export.
## The returned `content://` URI is written directly through [FileAccess], so
## no storage permission and no Android plugin are needed.
func _on_export_save_as_pressed() -> void:
if _last_export_path.is_empty() or not FileAccess.file_exists(_last_export_path):
_show_message("Exportdatei nicht gefunden.")
return
var extension := _last_export_path.get_extension()
var filters := PackedStringArray()
if not extension.is_empty():
filters.append("*." + extension)
DisplayServer.file_dialog_show(
"Export speichern",
OS.get_system_dir(OS.SYSTEM_DIR_DOWNLOADS),
_last_export_path.get_file(),
false,
DisplayServer.FILE_DIALOG_MODE_SAVE_FILE,
filters,
_on_export_save_as_selected
)
func _on_export_save_as_selected(
status: bool,
selected_paths: PackedStringArray,
_filter_index: int
) -> void:
if not status or selected_paths.is_empty():
return
var destination := selected_paths[0]
var error := ExportService.copy_file(_last_export_path, destination)
if error != OK:
_show_message("Speichern fehlgeschlagen (Fehler %d)." % error)
return
_show_message("Gespeichert:\n%s" % destination)
func export_svg_to_path(path: String, section_id: String = "") -> bool:
return ExportService.write_svg(_routine, path, section_id)

View file

@ -69,3 +69,24 @@ static func write_pdf(
return false
var pdf := RoutinePdfExporter.build(routine, section_id)
return write_bytes(path, pdf)
## Copies an exported document to a user-chosen destination.
##
## Android's Storage Access Framework returns a `content://` URI; Godot 4.7
## accepts it in [method FileAccess.open], so the same byte copy works for
## sandbox paths and SAF destinations.
static func copy_file(source: String, destination: String) -> Error:
if source.is_empty() or destination.is_empty():
return ERR_INVALID_PARAMETER
if not FileAccess.file_exists(source):
return ERR_FILE_NOT_FOUND
var bytes := FileAccess.get_file_as_bytes(source)
if bytes.is_empty():
return ERR_FILE_CANT_READ
var file := FileAccess.open(destination, FileAccess.WRITE)
if file == null:
return FileAccess.get_open_error()
file.store_buffer(bytes)
file.close()
return OK

View file

@ -14,6 +14,7 @@ signal export_requested(
open_after: bool
)
signal open_requested
signal save_as_requested
signal folder_requested
signal copy_requested
signal message_requested(text: String)
@ -32,6 +33,7 @@ const FORMAT_EXTENSIONS := ["svg", "png", "pdf"]
@onready var copy_button: Button = %ExportCopyPathButton
var _active_section: RoutineSection
var _is_android: bool = false
func _ready() -> void:
@ -46,9 +48,33 @@ func _ready() -> void:
scope_option.select(0)
dialog.confirmed.connect(_on_confirmed)
open_button.pressed.connect(func() -> void: open_requested.emit())
open_button.pressed.connect(func() -> void:
if _is_android:
save_as_requested.emit()
else:
open_requested.emit()
)
folder_button.pressed.connect(func() -> void: folder_requested.emit())
copy_button.pressed.connect(func() -> void: copy_requested.emit())
configure_for_platform(OS.get_name() == "Android")
## Switches the "done" dialog between desktop (open/folder/copy) and Android
## (save via the Storage Access Framework). Public so the headless test can
## exercise both modes without an Android device.
func configure_for_platform(android: bool) -> void:
_is_android = android
open_button.text = "Speichern unter…" if android else "Öffnen"
open_button.tooltip_text = (
"Datei in Downloads oder einem anderen Ordner speichern"
if android
else "Datei mit dem Standardprogramm öffnen"
)
folder_button.visible = not android
copy_button.visible = not android
open_after_check.visible = not android
if android:
open_after_check.button_pressed = false
## Fills and shows the export dialog. [param size] is the pre-fitted dialog
@ -66,6 +92,7 @@ func open_for(
name_edit.text = "kuer"
format_option.select(0)
scope_option.select(0)
configure_for_platform(_is_android)
dialog.popup_centered(size)
# AcceptDialog shrinks to its content minimum on popup; restore the target
# size so the scroll area has room.
@ -105,10 +132,17 @@ func _on_confirmed() -> void:
## Shows the "export done" dialog for [param path].
func show_done(path: String, size: Vector2i) -> void:
done_label.text = (
"Exportiert nach:\n%s\n\n"
+ "Du kannst die Datei direkt öffnen oder den Ordner anzeigen."
) % path
if _is_android:
done_label.text = (
"Exportiert nach:\n%s\n\n"
+ "Tippe auf „Speichern unter…“, um die Datei in Downloads oder "
+ "einem anderen Ordner abzulegen."
) % path
else:
done_label.text = (
"Exportiert nach:\n%s\n\n"
+ "Du kannst die Datei direkt öffnen oder den Ordner anzeigen."
) % path
open_button.disabled = path.is_empty() or not FileAccess.file_exists(path)
folder_button.disabled = path.is_empty()
copy_button.text = "Pfad kopieren"

View file

@ -109,6 +109,45 @@ func _run() -> void:
_check(panel.open_button.disabled, "missing files cannot be opened")
_check(panel.done_dialog.visible, "the done dialog opens")
# Android: the done dialog saves through the Storage Access Framework
# instead of opening a sandbox path.
var requests := {"save": 0, "open": 0}
panel.save_as_requested.connect(
func() -> void: requests["save"] = int(requests["save"]) + 1
)
panel.open_requested.connect(
func() -> void: requests["open"] = int(requests["open"]) + 1
)
panel.configure_for_platform(true)
_check(
panel.open_button.text == "Speichern unter…",
"Android offers save as"
)
_check(not panel.folder_button.visible, "Android hides the folder button")
_check(not panel.copy_button.visible, "Android hides the path copy button")
_check(
not panel.open_after_check.visible,
"Android hides open-after-export"
)
_check(
not panel.open_after_check.button_pressed,
"Android clears open-after-export"
)
panel.open_button.pressed.emit()
_check(
int(requests["save"]) == 1 and int(requests["open"]) == 0,
"Android routes the button to save as"
)
panel.configure_for_platform(false)
_check(
panel.open_button.text == "Öffnen",
"desktop restores the open label"
)
_check(
panel.folder_button.visible and panel.copy_button.visible,
"desktop restores the buttons"
)
panel.queue_free()
await process_frame
_finish()

View file

@ -116,6 +116,7 @@ func _run() -> void:
"the file stem drops forbidden characters"
)
var service_path := ExportService.build_path("service_test", "", "pdf")
ExportService.ensure_dir()
_check(ExportService.write_pdf(routine, service_path), "the service writes a PDF")
_check(
FileAccess.file_exists(service_path),
@ -396,6 +397,33 @@ func _run() -> void:
"missing export files are not passed to the shell"
)
# SAF/Share: copying an export to a user-chosen destination.
var copy_source := "user://export-copy-source.txt"
var copy_destination := "user://export-copy-destination.txt"
var source_file := FileAccess.open(copy_source, FileAccess.WRITE)
if source_file != null:
source_file.store_string("rinkplanner export")
source_file.close()
_check(
ExportService.copy_file(copy_source, copy_destination) == OK,
"an export can be copied to a user-chosen destination"
)
_check(
FileAccess.get_file_as_string(copy_destination) == "rinkplanner export",
"the copied export keeps its content"
)
_check(
ExportService.copy_file("user://missing-export.txt", copy_destination)
== ERR_FILE_NOT_FOUND,
"a missing copy source is reported"
)
_check(
ExportService.copy_file(copy_source, "") == ERR_INVALID_PARAMETER,
"an empty copy destination is rejected"
)
DirAccess.remove_absolute(ProjectSettings.globalize_path(copy_source))
DirAccess.remove_absolute(ProjectSettings.globalize_path(copy_destination))
scene.queue_free()
await process_frame
_finish()