diff --git a/lib/common.dart b/lib/common.dart index 1c14aeb0c..677c8700e 100644 --- a/lib/common.dart +++ b/lib/common.dart @@ -255,3 +255,54 @@ class AccessibilityListener extends StatelessWidget { child: child); } } + +class PermissionManager { + static Completer? _completer; + static Timer? _timer; + static var _current = ""; + + static final permissions = ["audio", "file"]; + + static bool isWaitingFile() { + if (_completer != null) { + return !_completer!.isCompleted && _current == "file"; + } + return false; + } + + static Future check(String type) { + if (!permissions.contains(type)) + return Future.error("Wrong permission!$type"); + return FFI.invokeMethod("check_permission", type); + } + + static Future request(String type) { + if (!permissions.contains(type)) + return Future.error("Wrong permission!$type"); + + _current = type; + _completer = Completer(); + FFI.invokeMethod("request_permission", type); + + // timeout + _timer?.cancel(); + _timer = Timer(Duration(seconds: 60), () { + if (_completer == null) return; + if (!_completer!.isCompleted) { + _completer!.complete(false); + } + _completer = null; + _current = ""; + }); + return _completer!.future; + } + + static complete(String type, bool res) { + if (type != _current) { + res = false; + } + _timer?.cancel(); + _completer?.complete(res); + _current = ""; + } +} diff --git a/lib/models/server_model.dart b/lib/models/server_model.dart index 3215d0a90..91edba104 100644 --- a/lib/models/server_model.dart +++ b/lib/models/server_model.dart @@ -508,54 +508,3 @@ showInputWarnAlert() { ], )); } - -class PermissionManager { - static Completer? _completer; - static Timer? _timer; - static var _current = ""; - - static final permissions = ["audio", "file"]; - - static bool isWaitingFile() { - if (_completer != null) { - return !_completer!.isCompleted && _current == "file"; - } - return false; - } - - static Future check(String type) { - if (!permissions.contains(type)) - return Future.error("Wrong permission!$type"); - return FFI.invokeMethod("check_permission", type); - } - - static Future request(String type) { - if (!permissions.contains(type)) - return Future.error("Wrong permission!$type"); - - _current = type; - _completer = Completer(); - FFI.invokeMethod("request_permission", type); - - // timeout - _timer?.cancel(); - _timer = Timer(Duration(seconds: 60), () { - if (_completer == null) return; - if (!_completer!.isCompleted) { - _completer!.complete(false); - } - _completer = null; - _current = ""; - }); - return _completer!.future; - } - - static complete(String type, bool res) { - if (type != _current) { - res = false; - } - _timer?.cancel(); - _completer?.complete(res); - _current = ""; - } -} diff --git a/lib/pages/connection_page.dart b/lib/pages/connection_page.dart index 6f039e81f..1fe4dc6b0 100644 --- a/lib/pages/connection_page.dart +++ b/lib/pages/connection_page.dart @@ -65,10 +65,15 @@ class _ConnectionPageState extends State { connect(id); } - void connect(String id, {bool isFileTransfer = false}) { + void connect(String id, {bool isFileTransfer = false}) async { if (id == '') return; id = id.replaceAll(' ', ''); if (isFileTransfer) { + if (!await PermissionManager.check("file")) { + if (!await PermissionManager.request("file")) { + return; + } + } Navigator.push( context, MaterialPageRoute(