file connection permission

This commit is contained in:
csf 2022-04-20 22:37:47 +08:00
parent f5160b60b6
commit 7ec398eccb
3 changed files with 57 additions and 52 deletions

View File

@ -255,3 +255,54 @@ class AccessibilityListener extends StatelessWidget {
child: child);
}
}
class PermissionManager {
static Completer<bool>? _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<bool> check(String type) {
if (!permissions.contains(type))
return Future.error("Wrong permission!$type");
return FFI.invokeMethod("check_permission", type);
}
static Future<bool> request(String type) {
if (!permissions.contains(type))
return Future.error("Wrong permission!$type");
_current = type;
_completer = Completer<bool>();
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 = "";
}
}

View File

@ -508,54 +508,3 @@ showInputWarnAlert() {
],
));
}
class PermissionManager {
static Completer<bool>? _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<bool> check(String type) {
if (!permissions.contains(type))
return Future.error("Wrong permission!$type");
return FFI.invokeMethod("check_permission", type);
}
static Future<bool> request(String type) {
if (!permissions.contains(type))
return Future.error("Wrong permission!$type");
_current = type;
_completer = Completer<bool>();
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 = "";
}
}

View File

@ -65,10 +65,15 @@ class _ConnectionPageState extends State<ConnectionPage> {
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(