From 0ed605717fd7faf28d1392ead097e9815cfe038c Mon Sep 17 00:00:00 2001 From: open-trade Date: Mon, 21 Dec 2020 19:05:31 +0800 Subject: [PATCH] OS password --- flutter_hbb/lib/model.dart | 12 ++++++++ flutter_hbb/lib/remote_page.dart | 51 +++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/flutter_hbb/lib/model.dart b/flutter_hbb/lib/model.dart index e76084b2f..2ce9398e7 100644 --- a/flutter_hbb/lib/model.dart +++ b/flutter_hbb/lib/model.dart @@ -690,6 +690,18 @@ Future> getPreference(String id) async { return m; } +Future getPassword(String id) async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + var p = prefs.getString('peer' + id + '-password'); + if (p == null) return ""; + return p; +} + +void savePassword(String id, String password) async { + SharedPreferences prefs = await SharedPreferences.getInstance(); + prefs.setString('peer' + id + '-password', password); +} + void initializeCursorAndCanvas() async { var p = await getPreference(FFI.id); int currentDisplay = 0; diff --git a/flutter_hbb/lib/remote_page.dart b/flutter_hbb/lib/remote_page.dart index 6cb968e0a..d7cfd258b 100644 --- a/flutter_hbb/lib/remote_page.dart +++ b/flutter_hbb/lib/remote_page.dart @@ -751,7 +751,7 @@ void showOptions(BuildContext context) { }, () async => true, true, 0); } -void showActions(BuildContext context) { +void showActions(BuildContext context) async { final size = MediaQuery.of(context).size; final x = 120.0; final y = size.height; @@ -763,6 +763,21 @@ void showActions(BuildContext context) { FFI.ffiModel.permissions['clipboard'] != false) { more.add(PopupMenuItem(child: Text('Paste'), value: 'paste')); } + var password = await getPassword(FFI.id); + more.add(PopupMenuItem( + child: Row( + children: ([ + Text('OS Password'), + FlatButton( + textColor: MyTheme.accent, + onPressed: () { + showSetOSPassword(context); + Navigator.pop(context); + }, + child: Icon(Icons.edit), + ) + ])), + value: 'enter_os_password')); () async { var value = await showMenu( context: context, @@ -788,6 +803,40 @@ void showActions(BuildContext context) { FFI.setByName('input_string', '${data.text}'); } }(); + } else if (value == 'enter_os_password') { + if (password != "") FFI.setByName('input_string', password); } }(); } + +void showSetOSPassword(BuildContext context) async { + final controller = TextEditingController(); + var password = await getPassword(FFI.id); + controller.text = password; + showAlertDialog( + context, + (setState) => Tuple3( + Text('Password required'), + Column(mainAxisSize: MainAxisSize.min, children: [ + PasswordWidget(controller: controller), + ]), + [ + FlatButton( + textColor: MyTheme.accent, + onPressed: () { + Navigator.pop(context); + }, + child: Text('Cancel'), + ), + FlatButton( + textColor: MyTheme.accent, + onPressed: () { + var text = controller.text.trim(); + savePassword(FFI.id, text); + Navigator.pop(context); + }, + child: Text('OK'), + ), + ], + )); +}