import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../models/saf_permission_state.dart'; class SafStorageService { static final SafStorageService _instance = SafStorageService._internal(); factory SafStorageService() => _instance; SafStorageService._internal(); static const MethodChannel _channel = MethodChannel( 'com.lxh.battery_health/saf', ); static const String _prefKeySafPath = 'saf_storage_path'; String? _cachedPath; Future init() async { final prefs = await SharedPreferences.getInstance(); _cachedPath = prefs.getString(_prefKeySafPath); if (kDebugMode) { print('[SAF] 初始化,缓存路径: $_cachedPath'); } } Future getPermissionState() async { if (_cachedPath == null || _cachedPath!.isEmpty) { return SafPermissionState.unauthorized; } try { final dir = Directory(_cachedPath!); if (!await dir.exists()) { _clearCache(); return SafPermissionState.expired; } return SafPermissionState.authorized; } catch (e) { _clearCache(); return SafPermissionState.expired; } } Future requestPermission() async { try { final String? selectedPath = await _channel.invokeMethod('selectFolder'); if (selectedPath == null || selectedPath.isEmpty) { return SafAuthorizationResult.denied(); } // 验证路径是否包含 mbattery_charger if (!selectedPath.contains('mbattery_charger')) { return SafAuthorizationResult.error('请选择 mbattery_charger 文件夹'); } _cachedPath = selectedPath; await _savePath(selectedPath); if (kDebugMode) { print('[SAF] 授权成功,路径: $selectedPath'); } return SafAuthorizationResult.success(selectedPath); } catch (e) { if (kDebugMode) { print('[SAF] 授权失败: $e'); } return SafAuthorizationResult.error('授权失败: $e'); } } Future logFileExists(String fileName) async { final state = await getPermissionState(); if (state != SafPermissionState.authorized) return false; try { final file = File('${_cachedPath!}/$fileName'); return await file.exists(); } catch (e) { return false; } } Future> readLogFile(String fileName) async { final state = await getPermissionState(); if (state != SafPermissionState.authorized) return []; try { final file = File('${_cachedPath!}/$fileName'); if (!await file.exists()) return []; return await file.readAsLines(); } catch (e) { if (kDebugMode) { print('[SAF] 读取文件失败: $e'); } return []; } } Future> listFiles() async { final state = await getPermissionState(); if (state != SafPermissionState.authorized) return []; try { final dir = Directory(_cachedPath!); if (!await dir.exists()) return []; final entities = await dir.list().toList(); return entities .where((e) => e is File) .map((e) => e.path.split(Platform.pathSeparator).last) .toList(); } catch (e) { if (kDebugMode) { print('[SAF] 列出文件失败: $e'); } return []; } } Future validatePermission() async { final state = await getPermissionState(); return state == SafPermissionState.authorized; } void _clearCache() { _cachedPath = null; _savePath(null); } Future _savePath(String? path) async { final prefs = await SharedPreferences.getInstance(); if (path != null && path.isNotEmpty) { await prefs.setString(_prefKeySafPath, path); } else { await prefs.remove(_prefKeySafPath); } } String? get cachedPath => _cachedPath; }