2026-10-07 09:27:45 +08:00
|
|
|
|
/// 安卓公共存储读写服务。
|
2026-10-06 22:34:16 +08:00
|
|
|
|
///
|
|
|
|
|
|
/// 通过原生通道(MainActivity 中的 `seqstudio/docs`)实现:
|
|
|
|
|
|
/// - Android 10+(API 29+)走 MediaStore,无需存储权限(应用商店合规);
|
|
|
|
|
|
/// - Android 9 及以下直接写公共目录(配合 maxSdkVersion=28 的权限声明)。
|
|
|
|
|
|
///
|
2026-10-07 09:27:45 +08:00
|
|
|
|
/// 保存位置可配置:基础目录(Documents / Download / Pictures)+ 子文件夹名,
|
|
|
|
|
|
/// 设置保存在应用私有目录的 `save_location.json`,跨会话生效。
|
|
|
|
|
|
///
|
2026-10-06 22:34:16 +08:00
|
|
|
|
/// 桌面平台不可用([isAvailable] 为 false),此时应使用应用内文件浏览器。
|
|
|
|
|
|
library;
|
|
|
|
|
|
|
2026-10-07 09:27:45 +08:00
|
|
|
|
import 'dart:convert';
|
2026-10-06 22:34:16 +08:00
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
|
|
2026-10-07 09:27:45 +08:00
|
|
|
|
import 'storage.dart';
|
|
|
|
|
|
|
|
|
|
|
|
/// 安卓公共存储读写。
|
2026-10-06 22:34:16 +08:00
|
|
|
|
class AndroidDocs {
|
|
|
|
|
|
AndroidDocs._();
|
|
|
|
|
|
|
|
|
|
|
|
/// 原生通道。
|
|
|
|
|
|
static const MethodChannel _channel = MethodChannel('seqstudio/docs');
|
|
|
|
|
|
|
|
|
|
|
|
/// 是否可用(仅 Android)。
|
|
|
|
|
|
static bool get isAvailable => Platform.isAndroid;
|
|
|
|
|
|
|
2026-10-07 09:27:45 +08:00
|
|
|
|
/// 可选的基础目录:文档 / 下载 / 图片。
|
|
|
|
|
|
static const List<String> baseOptions = <String>['documents', 'downloads', 'pictures'];
|
|
|
|
|
|
|
|
|
|
|
|
/// 基础目录的展示名。
|
|
|
|
|
|
static String baseLabel(String base) => switch (base) {
|
|
|
|
|
|
'downloads' => 'Download',
|
|
|
|
|
|
'pictures' => 'Pictures',
|
|
|
|
|
|
_ => 'Documents',
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// 当前配置(懒加载一次)。
|
|
|
|
|
|
static Map<String, String> _settings = <String, String>{};
|
|
|
|
|
|
|
|
|
|
|
|
/// 设置文件(应用私有目录,不对外可见)。
|
|
|
|
|
|
static File get _settingsFile => File(
|
|
|
|
|
|
'${Storage.instance.appDir.path}${Platform.pathSeparator}save_location.json');
|
|
|
|
|
|
|
|
|
|
|
|
/// 读取当前保存位置配置。
|
|
|
|
|
|
static Map<String, String> settings() {
|
|
|
|
|
|
if (_settings.isNotEmpty) return _settings;
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (_settingsFile.existsSync()) {
|
|
|
|
|
|
final Map<String, dynamic> j = jsonDecode(_settingsFile.readAsStringSync()) as Map<String, dynamic>;
|
|
|
|
|
|
_settings = <String, String>{
|
|
|
|
|
|
'base': j['base'] as String? ?? 'documents',
|
|
|
|
|
|
'folder': j['folder'] as String? ?? 'SequenceDiagram',
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
|
if (_settings.isEmpty) {
|
|
|
|
|
|
_settings = <String, String>{'base': 'documents', 'folder': 'SequenceDiagram'};
|
|
|
|
|
|
}
|
|
|
|
|
|
return _settings;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 写入保存位置配置并立即生效。
|
|
|
|
|
|
static Future<void> saveSettings(String base, String folder) async {
|
|
|
|
|
|
_settings = <String, String>{
|
|
|
|
|
|
'base': baseOptions.contains(base) ? base : 'documents',
|
|
|
|
|
|
'folder': folder.trim().isEmpty ? 'SequenceDiagram' : folder.trim(),
|
|
|
|
|
|
};
|
|
|
|
|
|
try {
|
|
|
|
|
|
await _settingsFile.parent.create(recursive: true);
|
|
|
|
|
|
await _settingsFile.writeAsString(jsonEncode(_settings), flush: true);
|
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
|
}
|
2026-10-06 22:34:16 +08:00
|
|
|
|
|
2026-10-07 09:27:45 +08:00
|
|
|
|
/// 当前保存位置的展示描述(如 `Documents/SequenceDiagram`)。
|
|
|
|
|
|
static String location() {
|
|
|
|
|
|
final Map<String, String> s = settings();
|
|
|
|
|
|
return '${baseLabel(s['base'] ?? 'documents')}/${s['folder'] ?? 'SequenceDiagram'}';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 保存文件到配置的公共目录,返回展示位置。
|
2026-10-06 22:34:16 +08:00
|
|
|
|
static Future<String> saveFile(String name, Uint8List data, {String? mime}) async {
|
2026-10-07 09:27:45 +08:00
|
|
|
|
final Map<String, String> s = settings();
|
2026-10-06 22:34:16 +08:00
|
|
|
|
final Object? r = await _channel.invokeMethod<Object?>('saveFile', <String, Object?>{
|
|
|
|
|
|
'name': name,
|
|
|
|
|
|
'data': data,
|
|
|
|
|
|
'mime': mime,
|
2026-10-07 09:27:45 +08:00
|
|
|
|
'base': s['base'],
|
|
|
|
|
|
'folder': s['folder'],
|
2026-10-06 22:34:16 +08:00
|
|
|
|
});
|
|
|
|
|
|
return r as String;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-07 09:27:45 +08:00
|
|
|
|
/// 列出配置目录下的文件(`[{name, size}]`,新在前)。
|
2026-10-06 22:34:16 +08:00
|
|
|
|
static Future<List<Map<String, dynamic>>> listFiles() async {
|
2026-10-07 09:27:45 +08:00
|
|
|
|
final Map<String, String> s = settings();
|
|
|
|
|
|
final Object? r = await _channel.invokeMethod<Object?>('listFiles', <String, Object?>{
|
|
|
|
|
|
'base': s['base'],
|
|
|
|
|
|
'folder': s['folder'],
|
|
|
|
|
|
});
|
2026-10-06 22:34:16 +08:00
|
|
|
|
final List<Object?> list = (r as List<Object?>?) ?? const <Object?>[];
|
|
|
|
|
|
return list
|
|
|
|
|
|
.map((Object? e) => Map<String, dynamic>.from(e as Map<Object?, Object?>))
|
|
|
|
|
|
.toList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-07 09:27:45 +08:00
|
|
|
|
/// 读取配置目录下的文件内容;不存在返回 null。
|
2026-10-06 22:34:16 +08:00
|
|
|
|
static Future<Uint8List?> readFile(String name) async {
|
2026-10-07 09:27:45 +08:00
|
|
|
|
final Map<String, String> s = settings();
|
2026-10-06 22:34:16 +08:00
|
|
|
|
final Object? r = await _channel.invokeMethod<Object?>('readFile', <String, Object?>{
|
|
|
|
|
|
'name': name,
|
2026-10-07 09:27:45 +08:00
|
|
|
|
'base': s['base'],
|
|
|
|
|
|
'folder': s['folder'],
|
2026-10-06 22:34:16 +08:00
|
|
|
|
});
|
|
|
|
|
|
if (r == null) return null;
|
|
|
|
|
|
return r as Uint8List;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|