import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:streams_channel/streams_channel.dart'; class AndroidFileService { static const platform = MethodChannel('deckers.thibault/aves/storage'); static final StreamsChannel storageAccessChannel = StreamsChannel('deckers.thibault/aves/storageaccessstream'); static Future> getStorageVolumes() async { try { final result = await platform.invokeMethod('getStorageVolumes'); return (result as List).cast(); } on PlatformException catch (e) { debugPrint('getStorageVolumes failed with code=${e.code}, exception=${e.message}, details=${e.details}}'); } return []; } static Future hasGrantedPermissionToVolumeRoot(String path) async { try { final result = await platform.invokeMethod('hasGrantedPermissionToVolumeRoot', { 'path': path, }); return result as bool; } on PlatformException catch (e) { debugPrint('hasGrantedPermissionToVolumeRoot failed with code=${e.code}, exception=${e.message}, details=${e.details}}'); } return false; } // returns whether user granted access to volume root at `volumePath` static Future requestVolumeAccess(String volumePath) async { try { final completer = Completer(); storageAccessChannel.receiveBroadcastStream({ 'path': volumePath, }).listen( (data) => completer.complete(data as bool), onError: completer.completeError, onDone: () { if (!completer.isCompleted) completer.complete(false); }, cancelOnError: true, ); return completer.future; } on PlatformException catch (e) { debugPrint('requestVolumeAccess failed with code=${e.code}, exception=${e.message}, details=${e.details}}'); } return false; } }