From 299bd11481fc077d5dc37b9aa6703b705e59bf88 Mon Sep 17 00:00:00 2001 From: csf Date: Wed, 30 Mar 2022 23:09:19 +0800 Subject: [PATCH] android server update password --- .../kotlin/com/carriez/flutter_hbb/common.kt | 41 -------- lib/models/server_model.dart | 26 ++++- lib/pages/server_page.dart | 52 +++++++--- lib/widgets/dialog.dart | 98 +++++++++++++++++++ 4 files changed, 158 insertions(+), 59 deletions(-) diff --git a/android/app/src/main/kotlin/com/carriez/flutter_hbb/common.kt b/android/app/src/main/kotlin/com/carriez/flutter_hbb/common.kt index 09f1d0eee..1b1c5944f 100644 --- a/android/app/src/main/kotlin/com/carriez/flutter_hbb/common.kt +++ b/android/app/src/main/kotlin/com/carriez/flutter_hbb/common.kt @@ -40,47 +40,6 @@ fun testVP9Support(): Boolean { return res != null } -fun createNormalNotification( - ctx: Context, - title: String, - text: String, - type: String -): Notification { - val channelId = - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - val channelId = "RustDeskNormal" - val channelName = "RustDesk通知消息" - val channel = NotificationChannel( - channelId, - channelName, NotificationManager.IMPORTANCE_DEFAULT - ).apply { - description = "Share your Android Screen with RustDeskService" - } - channel.lightColor = Color.BLUE - channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC - val service = ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - service.createNotificationChannel(channel) - channelId - } else { - "" - } - val intent = Intent(ctx, MainActivity::class.java).apply { - flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED - action = Intent.ACTION_MAIN // 不设置会造成每次都重新启动一个新的Activity - addCategory(Intent.CATEGORY_LAUNCHER) - putExtra("type", type) - } - val pendingIntent = PendingIntent.getActivity(ctx, 0, intent, FLAG_UPDATE_CURRENT) - return NotificationCompat.Builder(ctx, channelId) - .setSmallIcon(R.mipmap.ic_launcher) - .setContentTitle(title) - .setPriority(NotificationCompat.PRIORITY_HIGH) // 这里如果设置为低则不显示 - .setContentText(text) - .setContentIntent(pendingIntent) - .setAutoCancel(true) - .build() -} - fun checkPermissions(context: Context) { XXPermissions.with(context) .permission(Permission.RECORD_AUDIO) diff --git a/lib/models/server_model.dart b/lib/models/server_model.dart index 83810cff5..23367e0bd 100644 --- a/lib/models/server_model.dart +++ b/lib/models/server_model.dart @@ -142,8 +142,30 @@ class ServerModel with ChangeNotifier { await FFI.invokeMethod("init_input"); } - getIDPasswd() async { - if (_serverId.text != _emptyIdShow && _serverPasswd.text != "") { + Future updatePassword(String pw) async { + final oldPasswd = _serverPasswd.text; + FFI.setByName("update_password",pw); + await Future.delayed(Duration(milliseconds: 500)); + await getIDPasswd(force: true); + + // check result + if(pw == ""){ + if(_serverPasswd.text.isNotEmpty && _serverPasswd.text!= oldPasswd){ + return true; + }else{ + return false; + } + }else{ + if(_serverPasswd.text == pw){ + return true; + }else{ + return false; + } + } + } + + getIDPasswd({bool force = false}) async { + if (!force && _serverId.text != _emptyIdShow && _serverPasswd.text != "") { return; } var count = 0; diff --git a/lib/pages/server_page.dart b/lib/pages/server_page.dart index af28295ab..47fe6f360 100644 --- a/lib/pages/server_page.dart +++ b/lib/pages/server_page.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_hbb/models/model.dart'; +import 'package:flutter_hbb/widgets/dialog.dart'; import 'package:provider/provider.dart'; import '../common.dart'; @@ -16,22 +17,41 @@ class ServerPage extends StatelessWidget implements PageShape { @override final appBarActions = [ - PopupMenuButton( - itemBuilder: (context) { - return [ - PopupMenuItem( - child: Text(translate("Change ID")), - value: "changeID", - enabled: false, - ), - PopupMenuItem( - child: Text(translate("Set your own password")), - value: "changePW", - enabled: false, - ) - ]; - }, - onSelected: (value) => debugPrint("PopupMenuItem onSelected:$value")) + PopupMenuButton(itemBuilder: (context) { + return [ + PopupMenuItem( + child: Text(translate("Change ID")), + value: "changeID", + enabled: false, + ), + PopupMenuItem( + child: Text(translate("Set your own password")), + value: "changePW", + enabled: FFI.serverModel.isStart, + ), + PopupMenuItem( + child: Text(translate("Refresh random password")), + value: "refreshPW", + enabled: FFI.serverModel.isStart, + ) + ]; + }, onSelected: (value) { + if (value == "changeID") { + // TODO + } else if (value == "changePW") { + updatePasswordDialog(); + } else if (value == "refreshPW") { + () async { + showLoading(translate("Waiting")); + if(await FFI.serverModel.updatePassword("")){ + showSuccess(); + }else{ + showError(); + } + debugPrint("end updatePassword"); + }(); + } + }) ]; @override diff --git a/lib/widgets/dialog.dart b/lib/widgets/dialog.dart index 03479f5a6..a5e7d24e3 100644 --- a/lib/widgets/dialog.dart +++ b/lib/widgets/dialog.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter_easyloading/flutter_easyloading.dart'; import '../common.dart'; import '../models/model.dart'; @@ -6,6 +7,103 @@ void clientClose() { msgBox('', 'Close', 'Are you sure to close the connection?'); } +const SEC1 = Duration(seconds: 1); +void showSuccess({Duration duration = SEC1}){ + EasyLoading.dismiss(); + EasyLoading.showSuccess(translate("Successful"), + duration: duration, + dismissOnTap: true, + maskType: EasyLoadingMaskType.black); +} + +void showError({Duration duration = SEC1}){ + EasyLoading.dismiss(); + EasyLoading.showError(translate("Error"), + duration: duration, + dismissOnTap: true, + maskType: EasyLoadingMaskType.black); +} + +void updatePasswordDialog(){ + final p0 = TextEditingController(); + final p1 = TextEditingController(); + var validateLength = false; + var validateSame = false; + DialogManager.show((setState, close) { + return CustomAlertDialog( + title: Text(translate('Set your own password')), + content: Form( + autovalidateMode: AutovalidateMode.onUserInteraction, + child: Column(mainAxisSize: MainAxisSize.min, children: [ + TextFormField( + autofocus: true, + obscureText: true, + keyboardType: TextInputType.visiblePassword, + decoration: InputDecoration( + labelText: translate('Password'), + ), + controller: p0, + validator: (v) { + if (v == null) return null; + final val = v.trim().length > 5; + if (validateLength != val) { + // use delay to make setState success + Future.delayed(Duration(microseconds: 1), + () => setState(() => validateLength = val)); + } + return val + ? null + : translate('Too short, at least 6 characters.'); + }, + ), + TextFormField( + obscureText: true, + keyboardType: TextInputType.visiblePassword, + decoration: InputDecoration( + labelText: translate('Confirmation'), + ), + controller: p1, + validator: (v) { + if (v == null) return null; + final val = p0.text == v; + if (validateSame != val) { + Future.delayed(Duration(microseconds: 1), + () => setState(() => validateSame = val)); + } + return val + ? null + : translate('The confirmation is not identical.'); + }, + ), + ])), + actions: [ + TextButton( + style: flatButtonStyle, + onPressed: () { + close(); + }, + child: Text(translate('Cancel')), + ), + TextButton( + style: flatButtonStyle, + onPressed: (validateLength && validateSame) + ? () async { + close(); + showLoading(translate("Waiting")); + if(await FFI.serverModel.updatePassword(p0.text)){ + showSuccess(); + }else{ + showError(); + } + } + : null, + child: Text(translate('OK')), + ), + ], + ); + }); +} + void enterPasswordDialog(String id) { final controller = TextEditingController(); var remember = FFI.getByName('remember', id) == 'true';