add android server chat and multi chat;update android server page

This commit is contained in:
csf
2022-03-22 16:40:23 +08:00
parent 99b27b1fe4
commit 6ce7018f07
10 changed files with 424 additions and 306 deletions

View File

@@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:dash_chat/dash_chat.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hbb/pages/chat_page.dart';
@@ -6,39 +8,65 @@ import 'model.dart';
import 'native_model.dart';
class ChatModel with ChangeNotifier {
final List<ChatMessage> _messages = [];
// -1作为客户端模式的id客户端模式下此id唯一
// 其它正整数的id来自被控服务器模式下的其他客户端的id每个客户端有不同的id
// 注意 此id和peer_id不同服务端模式下的id等同于conn的顺序累加id
static final clientModeID = -1;
final Map<int, List<ChatMessage>> _messages = Map()..[clientModeID] = [];
final ChatUser me = ChatUser(
name:"me",
uid:"",
name: "me",
customProperties: Map()..["id"] = clientModeID
);
var _currentID = clientModeID;
get messages => _messages;
receive(String text){
get currentID => _currentID;
receive(int id, String text) {
if (text.isEmpty) return;
// first message show overlay icon
if (iconOverlayEntry == null){
if (iconOverlayEntry == null) {
showChatIconOverlay();
}
_messages.add(ChatMessage(text: text, user: ChatUser(
name:FFI.ffiModel.pi.username,
uid: FFI.getId(),
)));
if(!_messages.containsKey(id)){
_messages[id] = [];
}
// TODO peer info
_messages[id]?.add(ChatMessage(
text: text,
user: ChatUser(
name: FFI.ffiModel.pi.username,
uid: FFI.getId(),
)));
_currentID = id;
notifyListeners();
}
send(ChatMessage message){
_messages.add(message);
if(message.text != null && message.text!.isNotEmpty){
PlatformFFI.setByName("chat",message.text!);
send(ChatMessage message) {
if (message.text != null && message.text!.isNotEmpty) {
_messages[_currentID]?.add(message);
if (_currentID == clientModeID) {
PlatformFFI.setByName("chat_client_mode", message.text!);
} else {
final msg = Map()
..["id"] = _currentID
..["text"] = message.text!;
PlatformFFI.setByName("chat_server_mode", jsonEncode(msg));
}
}
notifyListeners();
}
release(){
release() {
hideChatIconOverlay();
hideChatWindowOverlay();
_messages.forEach((key, value) => value.clear());
_messages.clear();
notifyListeners();
}
}
}

View File

@@ -129,8 +129,10 @@ class FfiModel with ChangeNotifier {
Clipboard.setData(ClipboardData(text: evt['content']));
} else if (name == 'permission') {
FFI.ffiModel.updatePermission(evt);
} else if (name == 'chat') {
FFI.chatModel.receive(evt['text'] ?? "");
} else if (name == 'chat_client_mode') {
FFI.chatModel.receive(ChatModel.clientModeID,evt['text'] ?? "");
} else if (name == 'chat_server_mode') {
FFI.chatModel.receive(int.parse(evt['id'] as String),evt['text'] ?? "");
} else if (name == 'file_dir') {
FFI.fileModel.receiveFileDir(evt);
} else if (name == 'job_progress') {

View File

@@ -24,6 +24,7 @@ class PlatformFFI {
static Pointer<RgbaFrame>? _lastRgbaFrame;
static String _dir = '';
static String _homeDir = '';
static int? _androidVersion;
static F2? _getByName;
static F3? _setByName;
static F4? _freeRgba;
@@ -48,6 +49,8 @@ class PlatformFFI {
return packageInfo.version;
}
static int? get androidVersion => _androidVersion;
static String getByName(String name, [String arg = '']) {
if (_getByName == null) return '';
var a = name.toNativeUtf8();
@@ -94,6 +97,7 @@ class PlatformFFI {
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
name = '${androidInfo.brand}-${androidInfo.model}';
id = androidInfo.id.hashCode.toString();
_androidVersion = androidInfo.version.sdkInt;
} else {
IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
name = iosInfo.utsname.machine;

View File

@@ -1,29 +1,144 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import '../common.dart';
import '../pages/server_page.dart';
import 'model.dart';
final _emptyIdShow = translate("connecting_status");
class ServerModel with ChangeNotifier {
Timer? _interval;
bool _isStart = false;
bool _mediaOk = false;
bool _inputOk = false;
late bool _fileOk;
final _serverId = TextEditingController(text: _emptyIdShow);
final _serverPasswd = TextEditingController(text: "");
List<Client> _clients = [];
bool get isStart => _isStart;
bool get mediaOk => _mediaOk;
bool get inputOk => _inputOk;
bool get fileOk => _fileOk;
TextEditingController get serverId => _serverId;
TextEditingController get serverPasswd => _serverPasswd;
List<Client> get clients => _clients;
ServerModel();
ServerModel() {
()async{
await Future.delayed(Duration(seconds: 2));
final file = FFI.getByName('option', 'enable-file-transfer');
debugPrint("got file in option:$file");
if (file.isEmpty) {
_fileOk = false;
Map<String, String> res = Map()
..["name"] = "enable-file-transfer"
..["value"] = "N";
FFI.setByName('option', jsonEncode(res)); // 重新设置默认值
} else {
if (file == "Y") {
_fileOk = true;
} else {
_fileOk = false;
}
}
}();
}
toggleFile() {
_fileOk = !_fileOk;
Map<String, String> res = Map()
..["name"] = "enable-file-transfer"
..["value"] = _fileOk ? 'Y' : 'N';
debugPrint("save option:$res");
FFI.setByName('option', jsonEncode(res));
notifyListeners();
}
Future<Null> startService() async {
_isStart = true;
notifyListeners();
FFI.setByName("ensure_init_event_queue");
_interval = Timer.periodic(Duration(milliseconds: 30), (timer) {
FFI.ffiModel.update("", (_, __) {});
});
await FFI.invokeMethod("init_service");
FFI.setByName("start_service");
getIDPasswd();
}
Future<Null> stopService() async {
_isStart = false;
release();
FFI.serverModel.closeAll();
await FFI.invokeMethod("stop_service");
FFI.setByName("stop_service");
notifyListeners();
}
Future<Null> initInput() async {
await FFI.invokeMethod("init_input");
}
getIDPasswd() async {
if (_serverId.text != _emptyIdShow && _serverPasswd.text != "") {
return;
}
var count = 0;
const maxCount = 10;
while (count < maxCount) {
await Future.delayed(Duration(seconds: 1));
final id = FFI.getByName("server_id");
final passwd = FFI.getByName("server_password");
if (id.isEmpty) {
continue;
} else {
_serverId.text = id;
}
if (passwd.isEmpty) {
continue;
} else {
_serverPasswd.text = passwd;
}
debugPrint(
"fetch id & passwd again at $count:id:${_serverId.text},passwd:${_serverPasswd.text}");
count++;
if (_serverId.text != _emptyIdShow && _serverPasswd.text.isNotEmpty) {
break;
}
}
notifyListeners();
}
release() {
_interval?.cancel();
_interval = null;
}
changeStatue(String name, bool value) {
debugPrint("changeStatue value $value");
switch (name) {
case "media":
_mediaOk = value;
debugPrint("value $value,_isStart:$_isStart");
if(value && !_isStart){
startService();
}
break;
case "input":
_inputOk = value;
//TODO change option
break;
default:
return;
@@ -36,76 +151,77 @@ class ServerModel with ChangeNotifier {
debugPrint("getByName clients_state string:$res");
try {
final List clientsJson = jsonDecode(res);
_clients = clientsJson.map((clientJson) => Client.fromJson(jsonDecode(res))).toList();
_clients = clientsJson
.map((clientJson) => Client.fromJson(jsonDecode(res)))
.toList();
debugPrint("updateClientState:${_clients.toString()}");
notifyListeners();
} catch (e) {}
}
loginRequest(Map<String, dynamic> evt){
try{
loginRequest(Map<String, dynamic> evt) {
try {
final client = Client.fromJson(jsonDecode(evt["client"]));
final Map<String,dynamic> response = Map();
final Map<String, dynamic> response = Map();
response["id"] = client.id;
DialogManager.show((setState, close) => CustomAlertDialog(
title: Text("Control Request"),
content: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(translate("Do you accept?")),
SizedBox(height: 20),
clientInfo(client),
],
),
actions: [
TextButton(
child: Text(translate("Dismiss")),
onPressed: () {
response["res"] = false;
FFI.setByName("login_res", jsonEncode(response));
close();
}),
ElevatedButton(
child: Text(translate("Accept")),
onPressed: () async {
response["res"] = true;
FFI.setByName("login_res", jsonEncode(response));
if (!client.isFileTransfer) {
bool res = await FFI.invokeMethod("start_capture"); // to Android service
debugPrint("_toAndroidStartCapture:$res");
}
_clients.add(client);
notifyListeners();
close();
}),
]));
}catch (e){
title: Text("Control Request"),
content: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(translate("Do you accept?")),
SizedBox(height: 20),
clientInfo(client),
],
),
actions: [
TextButton(
child: Text(translate("Dismiss")),
onPressed: () {
response["res"] = false;
FFI.setByName("login_res", jsonEncode(response));
close();
}),
ElevatedButton(
child: Text(translate("Accept")),
onPressed: () async {
response["res"] = true;
FFI.setByName("login_res", jsonEncode(response));
if (!client.isFileTransfer) {
bool res = await FFI.invokeMethod(
"start_capture"); // to Android service
debugPrint("_toAndroidStartCapture:$res");
}
_clients.add(client);
notifyListeners();
close();
}),
]));
} catch (e) {
debugPrint("loginRequest failed,error:$e");
}
}
void onClientLogin(Map<String, dynamic> evt){
}
void onClientLogin(Map<String, dynamic> evt) {}
void onClientRemove(Map<String, dynamic> evt) {
try{
try {
final id = int.parse(evt['id'] as String);
Client client = _clients.singleWhere((c) => c.id == id);
_clients.remove(client);
notifyListeners();
}catch(e){
} catch (e) {
debugPrint("onClientRemove failed,error:$e");
}
}
closeAll(){
closeAll() {
_clients.forEach((client) {
FFI.setByName("close_conn",client.id.toString());
FFI.setByName("close_conn", client.id.toString());
});
_clients = [];
}
}
@@ -136,4 +252,3 @@ class Client {
return data;
}
}