rustdesk/lib/pages/server_page.dart

450 lines
14 KiB
Dart
Raw Normal View History

2022-02-02 17:25:56 +08:00
import 'dart:async';
2022-01-23 21:37:19 +08:00
import 'package:flutter/material.dart';
2022-02-25 21:54:05 +08:00
import 'package:flutter_hbb/main.dart';
import 'package:flutter_hbb/models/model.dart';
2022-02-02 17:25:56 +08:00
import 'package:provider/provider.dart';
2022-01-23 21:37:19 +08:00
import '../common.dart';
import 'home_page.dart';
import '../models/model.dart';
class ServerPage extends StatelessWidget implements PageShape {
@override
final title = "Share Screen";
@override
final icon = Icon(Icons.mobile_screen_share);
@override
final appBarActions = [
PopupMenuButton<String>(
itemBuilder: (context) {
return [
PopupMenuItem(
child: Text(translate("Change ID")),
value: "changeID",
enabled: false,
),
PopupMenuItem(
child: Text("Set your own password"),
value: "changePW",
enabled: false,
)
];
},
onSelected: (value) => debugPrint("PopupMenuItem onSelected:$value"))
];
2022-01-23 21:37:19 +08:00
@override
Widget build(BuildContext context) {
2022-02-08 22:45:48 +08:00
checkService();
2022-02-02 17:25:56 +08:00
return ChangeNotifierProvider.value(
value: FFI.serverModel,
child: SingleChildScrollView(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
ServerInfo(),
PermissionChecker(),
ConnectionManager(),
SizedBox.fromSize(size: Size(0, 15.0)), // Bottom padding
],
),
),
),
);
2022-01-23 21:37:19 +08:00
}
}
2022-02-08 22:45:48 +08:00
void checkService() {
// 检测当前服务状态,若已存在服务则异步更新数据回来
2022-02-10 02:07:53 +08:00
FFI.invokeMethod("check_service"); // jvm
FFI.serverModel.updateClientState();
2022-02-08 22:45:48 +08:00
}
2022-01-23 21:37:19 +08:00
class ServerInfo extends StatefulWidget {
@override
_ServerInfoState createState() => _ServerInfoState();
}
class _ServerInfoState extends State<ServerInfo> {
2022-02-08 22:45:48 +08:00
var _passwdShow = false;
2022-01-23 21:37:19 +08:00
// TODO set ID / PASSWORD
var _serverId = TextEditingController(text: "");
var _serverPasswd = TextEditingController(text: "");
2022-02-16 23:08:23 +08:00
var _emptyIdShow = translate("connecting_status");
2022-01-23 21:37:19 +08:00
@override
void initState() {
super.initState();
var id = FFI.getByName("server_id");
2022-02-10 02:07:53 +08:00
_serverId.text = id == "" ? _emptyIdShow : id;
_serverPasswd.text = FFI.getByName("server_password");
2022-02-10 02:07:53 +08:00
if (_serverId.text == _emptyIdShow || _serverPasswd.text == "") {
fetchConfigAgain();
}
2022-01-23 21:37:19 +08:00
}
@override
Widget build(BuildContext context) {
return myCard(Column(
mainAxisSize: MainAxisSize.min,
children: [
TextFormField(
readOnly: true,
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
color: MyTheme.accent),
controller: _serverId,
2022-01-23 21:37:19 +08:00
decoration: InputDecoration(
icon: const Icon(Icons.perm_identity),
2022-02-16 23:08:23 +08:00
labelText: translate("ID"),
2022-01-23 21:37:19 +08:00
labelStyle:
TextStyle(fontWeight: FontWeight.bold, color: MyTheme.accent50),
),
2022-02-17 15:22:14 +08:00
onSaved: (String? value) {},
2022-01-23 21:37:19 +08:00
),
TextFormField(
readOnly: true,
2022-02-08 22:45:48 +08:00
obscureText: !_passwdShow,
2022-01-23 21:37:19 +08:00
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
color: MyTheme.accent),
controller: _serverPasswd,
2022-01-23 21:37:19 +08:00
decoration: InputDecoration(
icon: const Icon(Icons.lock),
2022-02-16 23:08:23 +08:00
labelText: translate("Password"),
2022-01-23 21:37:19 +08:00
labelStyle: TextStyle(
fontWeight: FontWeight.bold, color: MyTheme.accent50),
suffix: IconButton(
icon: Icon(Icons.visibility),
onPressed: () {
setState(() {
_passwdShow = !_passwdShow;
});
})),
2022-02-17 15:22:14 +08:00
onSaved: (String? value) {},
2022-01-23 21:37:19 +08:00
),
],
));
}
2022-02-10 02:07:53 +08:00
fetchConfigAgain() async {
FFI.setByName("start_service");
var count = 0;
const maxCount = 10;
2022-02-10 02:07:53 +08:00
while (count < maxCount) {
if (_serverId.text != _emptyIdShow && _serverPasswd.text != "") {
break;
}
await Future.delayed(Duration(seconds: 2));
var id = FFI.getByName("server_id");
2022-02-10 02:07:53 +08:00
_serverId.text = id == "" ? _emptyIdShow : id;
_serverPasswd.text = FFI.getByName("server_password");
2022-02-10 02:07:53 +08:00
debugPrint(
"fetch id & passwd again at $count:id:${_serverId.text},passwd:${_serverPasswd.text}");
count++;
}
FFI.setByName("stop_service");
}
2022-01-23 21:37:19 +08:00
}
class PermissionChecker extends StatefulWidget {
@override
_PermissionCheckerState createState() => _PermissionCheckerState();
}
class _PermissionCheckerState extends State<PermissionChecker> {
@override
Widget build(BuildContext context) {
2022-02-02 17:25:56 +08:00
final serverModel = Provider.of<ServerModel>(context);
2022-01-23 21:37:19 +08:00
return myCard(Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2022-02-16 23:08:23 +08:00
cardTitle(translate("Configuration Permissions")),
2022-02-25 21:54:05 +08:00
PermissionRow(
translate("Media"), serverModel.mediaOk, _toAndroidInitService),
2022-01-23 21:37:19 +08:00
const Divider(height: 0),
2022-02-25 21:54:05 +08:00
PermissionRow(
translate("Input"), serverModel.inputOk, showInputWarnAlert),
2022-01-23 21:37:19 +08:00
const Divider(),
2022-02-02 17:25:56 +08:00
serverModel.mediaOk
? ElevatedButton.icon(
2022-02-25 22:16:51 +08:00
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red)),
2022-02-02 17:25:56 +08:00
icon: Icon(Icons.stop),
onPressed: _toAndroidStopService,
2022-02-16 23:08:23 +08:00
label: Text(translate("Stop service")))
2022-02-02 17:25:56 +08:00
: ElevatedButton.icon(
2022-01-23 21:37:19 +08:00
icon: Icon(Icons.play_arrow),
2022-02-02 17:25:56 +08:00
onPressed: _toAndroidInitService,
2022-02-16 23:08:23 +08:00
label: Text(translate("Start Service"))),
2022-01-23 21:37:19 +08:00
],
));
}
2022-02-02 17:25:56 +08:00
}
2022-01-23 21:37:19 +08:00
2022-02-25 22:16:51 +08:00
Widget getConnInfo(String name, String peerID) {
return Row(
children: [
CircleAvatar(child: Text(name[0]), backgroundColor: MyTheme.border),
SizedBox(width: 12),
Text(name, style: TextStyle(color: MyTheme.idColor)),
SizedBox(width: 8),
Text(peerID, style: TextStyle(color: MyTheme.idColor))
],
);
}
2022-02-25 21:54:05 +08:00
void showLoginReqAlert(String peerID, String name) async {
if (globalKey.currentContext == null) return;
await showDialog(
2022-02-25 21:54:05 +08:00
barrierDismissible: false,
context: globalKey.currentContext!,
builder: (alertContext) {
2022-02-28 16:11:21 +08:00
DialogManager.reset();
DialogManager.register(alertContext);
return AlertDialog(
2022-02-16 23:08:23 +08:00
title: Text("Control Request"),
2022-02-25 21:54:05 +08:00
content: Container(
height: 100,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
2022-02-16 23:08:23 +08:00
children: [
2022-02-25 21:54:05 +08:00
Text(translate("Do you accept?")),
SizedBox(height: 20),
2022-02-25 22:16:51 +08:00
getConnInfo(name, peerID),
2022-02-16 23:08:23 +08:00
],
2022-02-25 21:54:05 +08:00
)),
actions: [
TextButton(
2022-02-25 21:54:05 +08:00
child: Text(translate("Dismiss")),
onPressed: () {
FFI.setByName("login_res", "false");
2022-02-28 16:11:21 +08:00
DialogManager.reset();
2022-02-25 21:54:05 +08:00
}),
ElevatedButton(
2022-02-16 23:08:23 +08:00
child: Text(translate("Accept")),
onPressed: () {
FFI.setByName("login_res", "true");
2022-02-10 02:07:53 +08:00
if (!FFI.serverModel.isFileTransfer) {
_toAndroidStartCapture();
}
2022-02-10 02:07:53 +08:00
FFI.serverModel.setPeer(true);
2022-02-28 16:11:21 +08:00
DialogManager.reset();
}),
],
);
});
2022-02-28 16:11:21 +08:00
DialogManager.reset();
2022-01-23 21:37:19 +08:00
}
class PermissionRow extends StatelessWidget {
PermissionRow(this.name, this.isOk, this.onPressed);
final String name;
final bool isOk;
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2022-02-25 22:16:51 +08:00
Row(
children: [
SizedBox(
width: 60,
child: Text(name + ":",
style: TextStyle(fontSize: 16.0, color: MyTheme.accent50))),
SizedBox(
width: 50,
child: Text(isOk ? translate("ON") : translate("OFF"),
style: TextStyle(
fontSize: 16.0,
color: isOk ? Colors.green : Colors.grey)),
)
],
),
2022-01-23 21:37:19 +08:00
TextButton(
2022-02-02 17:25:56 +08:00
onPressed: isOk ? null : onPressed,
2022-02-16 23:08:23 +08:00
child: Text(
translate("OPEN"),
2022-01-23 21:37:19 +08:00
style: TextStyle(fontWeight: FontWeight.bold),
)),
],
);
}
}
2022-02-02 17:25:56 +08:00
class ConnectionManager extends StatelessWidget {
@override
Widget build(BuildContext context) {
final serverModel = Provider.of<ServerModel>(context);
return serverModel.isPeerStart
2022-02-02 17:25:56 +08:00
? myCard(Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2022-02-25 22:16:51 +08:00
cardTitle(translate("Connection")), // TODO t
2022-02-02 17:25:56 +08:00
Padding(
padding: EdgeInsets.symmetric(vertical: 5.0),
2022-02-25 22:16:51 +08:00
child: getConnInfo(serverModel.peerName, serverModel.peerID),
2022-02-02 17:25:56 +08:00
),
ElevatedButton.icon(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red)),
icon: Icon(Icons.close),
onPressed: () {
FFI.setByName("close_conn");
2022-02-08 22:45:48 +08:00
// _toAndroidStopCapture();
2022-02-02 17:25:56 +08:00
serverModel.setPeer(false);
},
2022-02-25 22:16:51 +08:00
label: Text(translate("Close")))
2022-02-02 17:25:56 +08:00
],
))
: SizedBox.shrink();
}
}
2022-01-23 21:37:19 +08:00
Widget cardTitle(String text) {
return Padding(
padding: EdgeInsets.symmetric(vertical: 5.0),
child: Text(
text,
style: TextStyle(
fontFamily: 'WorkSans',
fontWeight: FontWeight.bold,
2022-02-02 17:25:56 +08:00
fontSize: 22,
2022-01-24 19:06:53 +08:00
color: MyTheme.accent80,
2022-01-23 21:37:19 +08:00
),
));
}
Widget myCard(Widget child) {
2022-02-02 17:25:56 +08:00
return Container(
width: double.maxFinite,
child: Card(
margin: EdgeInsets.fromLTRB(15.0, 15.0, 15.0, 0),
child: Padding(
padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 30.0),
child: child,
),
));
}
Future<Null> _toAndroidInitService() async {
2022-02-10 02:07:53 +08:00
bool res = await FFI.invokeMethod("init_service");
2022-02-02 17:25:56 +08:00
FFI.setByName("start_service");
debugPrint("_toAndroidInitService:$res");
}
Future<Null> _toAndroidStartCapture() async {
2022-02-10 02:07:53 +08:00
bool res = await FFI.invokeMethod("start_capture");
2022-02-02 17:25:56 +08:00
debugPrint("_toAndroidStartCapture:$res");
}
2022-02-08 22:45:48 +08:00
// Future<Null> _toAndroidStopCapture() async {
2022-02-10 02:07:53 +08:00
// bool res = await FFI.invokeMethod("stop_capture");
2022-02-08 22:45:48 +08:00
// debugPrint("_toAndroidStopCapture:$res");
// }
2022-02-02 17:25:56 +08:00
Future<Null> _toAndroidStopService() async {
FFI.setByName("close_conn");
2022-02-10 02:07:53 +08:00
FFI.serverModel.setPeer(false);
2022-02-10 02:07:53 +08:00
bool res = await FFI.invokeMethod("stop_service");
FFI.setByName("stop_service");
2022-02-02 17:25:56 +08:00
debugPrint("_toAndroidStopSer:$res");
}
2022-02-08 22:45:48 +08:00
Future<Null> _toAndroidInitInput() async {
2022-02-10 02:07:53 +08:00
bool res = await FFI.invokeMethod("init_input");
2022-02-08 22:45:48 +08:00
debugPrint("_toAndroidInitInput:$res");
2022-01-23 21:37:19 +08:00
}
2022-02-10 02:07:53 +08:00
2022-02-25 21:54:05 +08:00
showInputWarnAlert() async {
if (globalKey.currentContext == null) return;
2022-02-28 16:11:21 +08:00
DialogManager.reset();
2022-02-25 21:54:05 +08:00
await showDialog<bool>(
context: globalKey.currentContext!,
builder: (alertContext) {
2022-02-25 22:16:51 +08:00
// TODO t
2022-02-28 16:11:21 +08:00
DialogManager.register(alertContext);
2022-02-25 21:54:05 +08:00
return AlertDialog(
title: Text("获取输入权限引导"),
// content: Text("请在接下来的系统设置页面 \n进入 [服务] 配置页面\n将[RustDesk Input]服务开启"),
2022-02-25 22:16:51 +08:00
content: Text.rich(TextSpan(style: TextStyle(), children: [
TextSpan(text: "请在接下来的系统设置页\n进入"),
TextSpan(text: " [服务] ", style: TextStyle(color: MyTheme.accent)),
TextSpan(text: "配置页面\n"),
TextSpan(
text: " [RustDesk Input] ",
style: TextStyle(color: MyTheme.accent)),
TextSpan(text: "服务开启")
])),
2022-02-25 21:54:05 +08:00
actions: [
TextButton(
child: Text(translate("Do nothing")),
onPressed: () {
2022-02-28 16:11:21 +08:00
DialogManager.reset();
2022-02-25 21:54:05 +08:00
}),
ElevatedButton(
child: Text(translate("Go System Setting")),
onPressed: () {
_toAndroidInitInput();
2022-02-28 16:11:21 +08:00
DialogManager.reset();
2022-02-25 21:54:05 +08:00
}),
],
);
});
2022-02-28 16:11:21 +08:00
DialogManager.reset();
2022-02-25 21:54:05 +08:00
}
2022-02-10 02:07:53 +08:00
void toAndroidChannelInit() {
FFI.setMethodCallHandler((method, arguments) {
2022-02-25 21:54:05 +08:00
debugPrint("flutter got android msg,$method,$arguments");
2022-02-10 02:07:53 +08:00
try {
switch (method) {
case "try_start_without_auth":
{
FFI.serverModel.updateClientState();
debugPrint(
"pre show loginAlert:${FFI.serverModel.isFileTransfer.toString()}");
2022-02-25 21:54:05 +08:00
showLoginReqAlert(FFI.serverModel.peerID, FFI.serverModel.peerName);
2022-02-10 02:07:53 +08:00
debugPrint("from jvm:try_start_without_auth done");
break;
}
case "start_capture":
{
2022-02-28 16:11:21 +08:00
DialogManager.reset();
2022-02-10 02:07:53 +08:00
FFI.serverModel.updateClientState();
break;
}
case "stop_capture":
{
2022-02-28 16:11:21 +08:00
DialogManager.reset();
2022-02-10 02:07:53 +08:00
FFI.serverModel.setPeer(false);
break;
}
case "on_permission_changed":
{
var name = arguments["name"] as String;
var value = arguments["value"] as String == "true";
debugPrint("from jvm:on_permission_changed,$name:$value");
FFI.serverModel.changeStatue(name, value);
break;
}
}
} catch (e) {
debugPrint("MethodCallHandler err:$e");
}
return "";
});
}