just format

This commit is contained in:
csf 2022-04-13 15:33:23 +08:00
parent bb21435faa
commit ba2b30f488

View File

@ -18,7 +18,7 @@ class ServerModel with ChangeNotifier {
final _serverId = TextEditingController(text: _emptyIdShow); final _serverId = TextEditingController(text: _emptyIdShow);
final _serverPasswd = TextEditingController(text: ""); final _serverPasswd = TextEditingController(text: "");
Map<int,Client> _clients = {}; Map<int, Client> _clients = {};
bool get isStart => _isStart; bool get isStart => _isStart;
@ -34,10 +34,10 @@ class ServerModel with ChangeNotifier {
TextEditingController get serverPasswd => _serverPasswd; TextEditingController get serverPasswd => _serverPasswd;
Map<int,Client> get clients => _clients; Map<int, Client> get clients => _clients;
ServerModel() { ServerModel() {
()async{ () async {
/** /**
* 1. check android permission * 1. check android permission
* 2. check config * 2. check config
@ -48,25 +48,27 @@ class ServerModel with ChangeNotifier {
await Future.delayed(Duration(seconds: 1)); await Future.delayed(Duration(seconds: 1));
// audio // audio
if(androidVersion<30 || !await PermissionManager.check("audio")){ if (androidVersion < 30 || !await PermissionManager.check("audio")) {
_audioOk = false; _audioOk = false;
FFI.setByName('option', jsonEncode( FFI.setByName(
Map() 'option',
jsonEncode(Map()
..["name"] = "enable-audio" ..["name"] = "enable-audio"
..["value"] = "N")); ..["value"] = "N"));
}else{ } else {
final audioOption = FFI.getByName('option', 'enable-audio'); final audioOption = FFI.getByName('option', 'enable-audio');
_audioOk = audioOption.isEmpty; _audioOk = audioOption.isEmpty;
} }
// file // file
if(!await PermissionManager.check("file")) { if (!await PermissionManager.check("file")) {
_fileOk = false; _fileOk = false;
FFI.setByName('option', jsonEncode( FFI.setByName(
Map() 'option',
jsonEncode(Map()
..["name"] = "enable-file-transfer" ..["name"] = "enable-file-transfer"
..["value"] = "N")); ..["value"] = "N"));
} else{ } else {
final fileOption = FFI.getByName('option', 'enable-file-transfer'); final fileOption = FFI.getByName('option', 'enable-file-transfer');
_fileOk = fileOption.isEmpty; _fileOk = fileOption.isEmpty;
} }
@ -81,9 +83,9 @@ class ServerModel with ChangeNotifier {
} }
toggleAudio() async { toggleAudio() async {
if(!_audioOk && !await PermissionManager.check("audio")){ if (!_audioOk && !await PermissionManager.check("audio")) {
final res = await PermissionManager.request("audio"); final res = await PermissionManager.request("audio");
if(!res){ if (!res) {
// TODO handle fail // TODO handle fail
return; return;
} }
@ -98,9 +100,9 @@ class ServerModel with ChangeNotifier {
} }
toggleFile() async { toggleFile() async {
if(!_fileOk && !await PermissionManager.check("file")){ if (!_fileOk && !await PermissionManager.check("file")) {
final res = await PermissionManager.request("file"); final res = await PermissionManager.request("file");
if(!res){ if (!res) {
// TODO handle fail // TODO handle fail
return; return;
} }
@ -114,47 +116,57 @@ class ServerModel with ChangeNotifier {
notifyListeners(); notifyListeners();
} }
toggleInput(){ toggleInput() {
if(_inputOk){ if (_inputOk) {
FFI.invokeMethod("stop_input"); FFI.invokeMethod("stop_input");
}else{ } else {
showInputWarnAlert(); showInputWarnAlert();
} }
} }
toggleService() async { toggleService() async {
if(_isStart){ if (_isStart) {
final res = await DialogManager.show<bool>((setState, close) => CustomAlertDialog( final res =
title: Row(children: [ await DialogManager.show<bool>((setState, close) => CustomAlertDialog(
Icon(Icons.warning_amber_sharp, title: Row(children: [
color: Colors.redAccent, size: 28), Icon(Icons.warning_amber_sharp,
SizedBox(width: 10), color: Colors.redAccent, size: 28),
Text(translate("Warning")), SizedBox(width: 10),
]), Text(translate("Warning")),
content: Text(translate("android_stop_service_tip")), ]),
actions: [ content: Text(translate("android_stop_service_tip")),
TextButton(onPressed: ()=>close(), child: Text(translate("Cancel"))), actions: [
ElevatedButton(onPressed: ()=>close(true), child: Text(translate("OK"))), TextButton(
], onPressed: () => close(),
)); child: Text(translate("Cancel"))),
if(res == true){ ElevatedButton(
onPressed: () => close(true),
child: Text(translate("OK"))),
],
));
if (res == true) {
stopService(); stopService();
} }
}else{ } else {
final res = await DialogManager.show<bool>((setState, close) => CustomAlertDialog( final res =
title: Row(children: [ await DialogManager.show<bool>((setState, close) => CustomAlertDialog(
Icon(Icons.warning_amber_sharp, title: Row(children: [
color: Colors.redAccent, size: 28), Icon(Icons.warning_amber_sharp,
SizedBox(width: 10), color: Colors.redAccent, size: 28),
Text(translate("Warning")), SizedBox(width: 10),
]), Text(translate("Warning")),
content: Text(translate("android_service_will_start_tip")), ]),
actions: [ content: Text(translate("android_service_will_start_tip")),
TextButton(onPressed: ()=>close(), child: Text(translate("Cancel"))), actions: [
ElevatedButton(onPressed: ()=>close(true), child: Text(translate("OK"))), TextButton(
], onPressed: () => close(),
)); child: Text(translate("Cancel"))),
if(res == true){ ElevatedButton(
onPressed: () => close(true),
child: Text(translate("OK"))),
],
));
if (res == true) {
startService(); startService();
} }
} }
@ -172,7 +184,7 @@ class ServerModel with ChangeNotifier {
getIDPasswd(); getIDPasswd();
} }
Future<Null> stopService() async { Future<Null> stopService() async {
_isStart = false; _isStart = false;
_interval?.cancel(); _interval?.cancel();
_interval = null; _interval = null;
@ -188,21 +200,21 @@ class ServerModel with ChangeNotifier {
Future<bool> updatePassword(String pw) async { Future<bool> updatePassword(String pw) async {
final oldPasswd = _serverPasswd.text; final oldPasswd = _serverPasswd.text;
FFI.setByName("update_password",pw); FFI.setByName("update_password", pw);
await Future.delayed(Duration(milliseconds: 500)); await Future.delayed(Duration(milliseconds: 500));
await getIDPasswd(force: true); await getIDPasswd(force: true);
// check result // check result
if(pw == ""){ if (pw == "") {
if(_serverPasswd.text.isNotEmpty && _serverPasswd.text!= oldPasswd){ if (_serverPasswd.text.isNotEmpty && _serverPasswd.text != oldPasswd) {
return true; return true;
}else{ } else {
return false; return false;
} }
}else{ } else {
if(_serverPasswd.text == pw){ if (_serverPasswd.text == pw) {
return true; return true;
}else{ } else {
return false; return false;
} }
} }
@ -240,18 +252,17 @@ class ServerModel with ChangeNotifier {
notifyListeners(); notifyListeners();
} }
changeStatue(String name, bool value) { changeStatue(String name, bool value) {
debugPrint("changeStatue value $value"); debugPrint("changeStatue value $value");
switch (name) { switch (name) {
case "media": case "media":
_mediaOk = value; _mediaOk = value;
if(value && !_isStart){ if (value && !_isStart) {
startService(); startService();
} }
break; break;
case "input": case "input":
if(_inputOk!= value){ if (_inputOk != value) {
Map<String, String> res = Map() Map<String, String> res = Map()
..["name"] = "enable-keyboard" ..["name"] = "enable-keyboard"
..["value"] = value ? '' : 'N'; ..["value"] = value ? '' : 'N';
@ -269,7 +280,7 @@ class ServerModel with ChangeNotifier {
var res = FFI.getByName("clients_state"); var res = FFI.getByName("clients_state");
try { try {
final List clientsJson = jsonDecode(res); final List clientsJson = jsonDecode(res);
for (var clientJson in clientsJson){ for (var clientJson in clientsJson) {
final client = Client.fromJson(jsonDecode(clientJson)); final client = Client.fromJson(jsonDecode(clientJson));
_clients[client.id] = client; _clients[client.id] = client;
} }
@ -283,72 +294,79 @@ class ServerModel with ChangeNotifier {
final client = Client.fromJson(jsonDecode(evt["client"])); final client = Client.fromJson(jsonDecode(evt["client"]));
final Map<String, dynamic> response = Map(); final Map<String, dynamic> response = Map();
response["id"] = client.id; response["id"] = client.id;
DialogManager.show((setState, close) => CustomAlertDialog( DialogManager.show(
title: Row( (setState, close) => CustomAlertDialog(
mainAxisAlignment: MainAxisAlignment.spaceBetween, title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(translate(client.isFileTransfer
? "File Connection"
: "Screen Connection")),
IconButton(onPressed: close, icon: Icon(Icons.close))
]),
content: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text(translate(client.isFileTransfer?"File Connection":"Screen Connection")), Text(translate("Do you accept?")),
IconButton(onPressed: close, icon: Icon(Icons.close)) clientInfo(client),
]), Text(
content: Column( translate("android_new_connection_tip"),
mainAxisSize: MainAxisSize.min, style: TextStyle(color: Colors.black54),
mainAxisAlignment: MainAxisAlignment.center, ),
crossAxisAlignment: CrossAxisAlignment.start, ],
children: [ ),
Text(translate("Do you accept?")), actions: [
clientInfo(client), TextButton(
Text(translate("android_new_connection_tip"),style: TextStyle(color: Colors.black54),), child: Text(translate("Dismiss")),
onPressed: () {
response["res"] = false;
FFI.setByName("login_res", jsonEncode(response));
FFI.invokeMethod("cancel_notification", client.id);
close();
}),
ElevatedButton(
child: Text(translate("Accept")),
onPressed: () async {
response["res"] = true;
FFI.setByName("login_res", jsonEncode(response));
if (!client.isFileTransfer) {
FFI.invokeMethod("start_capture");
}
FFI.invokeMethod("cancel_notification", client.id);
_clients[client.id] = client;
notifyListeners();
close();
}),
], ],
onWillPop: () async => true,
), ),
actions: [ barrierDismissible: true);
TextButton(
child: Text(translate("Dismiss")),
onPressed: () {
response["res"] = false;
FFI.setByName("login_res", jsonEncode(response));
FFI.invokeMethod("cancel_notification",client.id);
close();
}),
ElevatedButton(
child: Text(translate("Accept")),
onPressed: () async {
response["res"] = true;
FFI.setByName("login_res", jsonEncode(response));
if (!client.isFileTransfer) {
FFI.invokeMethod("start_capture");
}
FFI.invokeMethod("cancel_notification",client.id);
_clients[client.id] = client;
notifyListeners();
close();
}),
],onWillPop: ()async=>true,),barrierDismissible: true);
} catch (e) { } catch (e) {
debugPrint("loginRequest failed,error:$e"); debugPrint("loginRequest failed,error:$e");
} }
} }
void onClientAuthorized(Map<String, dynamic> evt) { void onClientAuthorized(Map<String, dynamic> evt) {
try{ try {
final client = Client.fromJson(jsonDecode(evt['client'])); final client = Client.fromJson(jsonDecode(evt['client']));
// reset the login dialog, to-do,it will close any showing dialog // reset the login dialog, to-do,it will close any showing dialog
DialogManager.reset(); DialogManager.reset();
_clients[client.id] = client; _clients[client.id] = client;
notifyListeners(); notifyListeners();
}catch(e){ } catch (e) {}
}
} }
void onClientRemove(Map<String, dynamic> evt) { void onClientRemove(Map<String, dynamic> evt) {
try { try {
final id = int.parse(evt['id'] as String); final id = int.parse(evt['id'] as String);
if(_clients.containsKey(id)){ if (_clients.containsKey(id)) {
_clients.remove(id); _clients.remove(id);
}else{ } else {
// reset the login dialog, to-do,it will close any showing dialog // reset the login dialog, to-do,it will close any showing dialog
DialogManager.reset(); DialogManager.reset();
FFI.invokeMethod("cancel_notification",id); FFI.invokeMethod("cancel_notification", id);
} }
notifyListeners(); notifyListeners();
} catch (e) { } catch (e) {
@ -357,7 +375,7 @@ class ServerModel with ChangeNotifier {
} }
closeAll() { closeAll() {
_clients.forEach((id,client) { _clients.forEach((id, client) {
FFI.setByName("close_conn", id.toString()); FFI.setByName("close_conn", id.toString());
}); });
_clients.clear(); _clients.clear();
@ -375,7 +393,8 @@ class Client {
bool audio = false; bool audio = false;
late ChatUser chatUser; late ChatUser chatUser;
Client(this.authorized, this.isFileTransfer, this.name, this.peerId,this.keyboard,this.clipboard,this.audio); Client(this.authorized, this.isFileTransfer, this.name, this.peerId,
this.keyboard, this.clipboard, this.audio);
Client.fromJson(Map<String, dynamic> json) { Client.fromJson(Map<String, dynamic> json) {
id = json['id']; id = json['id'];
@ -383,12 +402,12 @@ class Client {
isFileTransfer = json['is_file_transfer']; isFileTransfer = json['is_file_transfer'];
name = json['name']; name = json['name'];
peerId = json['peer_id']; peerId = json['peer_id'];
keyboard= json['keyboard']; keyboard = json['keyboard'];
clipboard= json['clipboard']; clipboard = json['clipboard'];
audio= json['audio']; audio = json['audio'];
chatUser = ChatUser( chatUser = ChatUser(
uid:peerId, uid: peerId,
name: name, name: name,
); );
} }
@ -446,34 +465,34 @@ class PermissionManager {
static Timer? _timer; static Timer? _timer;
static var _current = ""; static var _current = "";
static final permissions = ["audio","file"]; static final permissions = ["audio", "file"];
static bool isWaitingFile(){ static bool isWaitingFile() {
if(_completer != null){ if (_completer != null) {
return !_completer!.isCompleted && _current == "file"; return !_completer!.isCompleted && _current == "file";
} }
return false; return false;
} }
static Future<bool> check(String type){ static Future<bool> check(String type) {
if(!permissions.contains(type)) if (!permissions.contains(type))
return Future.error("Wrong permission!$type"); return Future.error("Wrong permission!$type");
return FFI.invokeMethod("check_permission",type); return FFI.invokeMethod("check_permission", type);
} }
static Future<bool> request(String type){ static Future<bool> request(String type) {
if(!permissions.contains(type)) if (!permissions.contains(type))
return Future.error("Wrong permission!$type"); return Future.error("Wrong permission!$type");
_current = type; _current = type;
_completer = Completer<bool>(); _completer = Completer<bool>();
FFI.invokeMethod("request_permission",type); FFI.invokeMethod("request_permission", type);
// timeout // timeout
_timer?.cancel(); _timer?.cancel();
_timer = Timer(Duration(seconds: 60),(){ _timer = Timer(Duration(seconds: 60), () {
if(_completer == null) return; if (_completer == null) return;
if(!_completer!.isCompleted){ if (!_completer!.isCompleted) {
_completer!.complete(false); _completer!.complete(false);
} }
_completer = null; _completer = null;
@ -482,8 +501,8 @@ class PermissionManager {
return _completer!.future; return _completer!.future;
} }
static complete(String type,bool res){ static complete(String type, bool res) {
if(type != _current){ if (type != _current) {
res = false; res = false;
} }
_timer?.cancel(); _timer?.cancel();