Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions lib/common/extensions/string_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,49 @@ extension StringExtension on String {
return regex.hasMatch(this);
}

/// Returns the encoded URI as decoded.
/// Returns the storage volume and the path inside it of this Storage Access Framework URI of a document or a tree.
///
/// Decodes `:`, `/` and `␣`.
String get decoded {
var uri = this;
/// For example, `content://com.android.externalstorage.documents/tree/primary%3ADocuments%2Fbackups` gives the
/// volume `primary` and the path `Documents/backups`. The volume is `null` when the URI only gives a path, like the
/// `raw:` documents of the downloads provider.
///
/// Returns `null` if this string is not such an URI or if it doesn't contain a readable path.
({String? volume, String path})? get safPath {
final uri = Uri.tryParse(this);
if (uri == null || uri.scheme != 'content') {
return null;
}

// The document ID is after `document` if the URI has one, otherwise after `tree`
final segments = uri.pathSegments;
final documentIndex = segments.lastIndexOf('document');
final treeIndex = segments.indexOf('tree');
final idIndex = documentIndex != -1 ? documentIndex + 1 : (treeIndex != -1 ? treeIndex + 1 : -1);
if (idIndex == -1 || idIndex >= segments.length) {
return null;
}
final documentId = segments[idIndex];

// The downloads provider can give the absolute path of the document
if (documentId.startsWith('raw:')) {
return (volume: null, path: documentId.substring('raw:'.length));
}

// Only the external storage provider uses the `volume:path` format for its document IDs
final separatorIndex = documentId.indexOf(':');
if (uri.host != 'com.android.externalstorage.documents' || separatorIndex == -1) {
return null;
}

uri = uri.replaceAll('%3A', ':');
uri = uri.replaceAll('%2F', '/');
uri = uri.replaceAll('%20', ' ');
final volume = documentId.substring(0, separatorIndex);
final path = documentId.substring(separatorIndex + 1);

// The `home` volume is the documents directory of the internal storage
if (volume == 'home') {
return (volume: 'primary', path: path.isEmpty ? 'Documents' : 'Documents/$path');
}

return uri;
return (volume: volume, path: path);
}

/// Returns the first line of the string.
Expand Down
12 changes: 12 additions & 0 deletions lib/l10n/translations/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,18 @@
"@settings_auto_export_directory_description": {
"description": "Description of the settings tile to choose the directory where to save the automatic exports."
},
"settings_auto_export_directory_default": "Application storage (default)",
"@settings_auto_export_directory_default": {
"description": "Value of the settings tile to choose the directory where to save the automatic exports, when no directory was chosen and the exports are saved in the storage of the application."
},
"settings_auto_export_directory_internal_storage": "Internal storage",
"@settings_auto_export_directory_internal_storage": {
"description": "Name of the internal storage of the device, shown before the path of the directory where to save the automatic exports."
},
"settings_auto_export_directory_external_storage": "External storage",
"@settings_auto_export_directory_external_storage": {
"description": "Name of an external storage of the device (SD card, USB drive), shown before the path of the directory where to save the automatic exports."
},
"settings_security": "Security",
"@settings_security": {
"description": "Title of the settings page regarding the security."
Expand Down
29 changes: 28 additions & 1 deletion lib/pages/settings/pages/settings_backup_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,39 @@ class _SettingsBackupPageState extends ConsumerState<SettingsBackupPage> {
setState(() {});
}

/// Returns the automatic export directory as a readable path.
///
/// The default directory, in the application storage, is shown as such. A directory chosen by the user is shown as
/// its storage volume and its path inside it, or as its full URI if it can't be read.
String autoExportDirectoryLabel(BuildContext context) {
if (PreferenceKey.autoExportDirectory.preferenceOrDefault.isEmpty) {
return context.l.settings_auto_export_directory_default;
}

final directory = AutoExportUtils().autoExportDirectory;
final safPath = directory.safPath;
if (safPath == null) {
return Uri.decodeFull(directory);
}

final volume = safPath.volume;
if (volume == null) {
return safPath.path;
}

final volumeName = volume == 'primary'
? context.l.settings_auto_export_directory_internal_storage
: context.l.settings_auto_export_directory_external_storage;

return safPath.path.isEmpty ? volumeName : '$volumeName › ${safPath.path}';
}

@override
Widget build(BuildContext context) {
final enableAutoExport = PreferenceKey.enableAutoExport.preferenceOrDefault;
final autoExportFrequency = PreferenceKey.autoExportFrequency.preferenceOrDefault;
final autoExportEncryption = PreferenceKey.autoExportEncryption.preferenceOrDefault;
final autoExportDirectory = AutoExportUtils().autoExportDirectory.decoded;
final autoExportDirectory = autoExportDirectoryLabel(context);

return Scaffold(
appBar: TopNavigation(appbar: BasicAppBar(title: context.l.navigation_settings_backup)),
Expand Down
Loading