From bc00375c4496b16d76d520a7f80c51338fefb302 Mon Sep 17 00:00:00 2001 From: csf Date: Tue, 12 Apr 2022 20:54:29 +0800 Subject: [PATCH] android handle media projection canceled --- .../com/carriez/flutter_hbb/MainActivity.kt | 24 ++++--- lib/pages/server_page.dart | 68 +++++++++++-------- 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainActivity.kt b/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainActivity.kt index 90a26b2c8..fa79800b6 100644 --- a/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainActivity.kt +++ b/android/app/src/main/kotlin/com/carriez/flutter_hbb/MainActivity.kt @@ -43,7 +43,7 @@ class MainActivity : FlutterActivity() { Intent(activity, MainService::class.java).also { bindService(it, serviceConnection, Context.BIND_AUTO_CREATE) } - if(mainService?.isReady == true){ + if (mainService?.isReady == true) { result.success(false) return@setMethodCallHandler } @@ -58,7 +58,7 @@ class MainActivity : FlutterActivity() { } } "stop_service" -> { - Log.d(logTag,"Stop service") + Log.d(logTag, "Stop service") mainService?.let { it.destroy() result.success(true) @@ -67,14 +67,14 @@ class MainActivity : FlutterActivity() { } } "check_permission" -> { - if(call.arguments is String){ + if (call.arguments is String) { result.success(checkPermission(context, call.arguments as String)) } else { result.success(false) } } "request_permission" -> { - if(call.arguments is String){ + if (call.arguments is String) { requestPermission(context, call.arguments as String) result.success(true) } else { @@ -117,14 +117,13 @@ class MainActivity : FlutterActivity() { "cancel_notification" -> { try { val id = call.arguments as Int - Log.d(logTag,"cancel_notification id:$id") mainService?.cancelNotification(id) - }finally { + } finally { result.success(true) } } else -> { - result.error("-1","No such method",null) + result.error("-1", "No such method", null) } } } @@ -181,10 +180,13 @@ class MainActivity : FlutterActivity() { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) - if (requestCode == MEDIA_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) { - Log.d(logTag, "got mediaProjectionResultIntent ok") - mediaProjectionResultIntent = data - initService() + if (requestCode == MEDIA_REQUEST_CODE) { + if (resultCode == Activity.RESULT_OK && data != null) { + mediaProjectionResultIntent = data + initService() + } else { + flutterMethodChannel.invokeMethod("on_media_projection_canceled", null) + } } } diff --git a/lib/pages/server_page.dart b/lib/pages/server_page.dart index c98522b0b..5db82ddf6 100644 --- a/lib/pages/server_page.dart +++ b/lib/pages/server_page.dart @@ -82,8 +82,8 @@ class ServerPage extends StatelessWidget implements PageShape { void checkService() async { FFI.invokeMethod("check_service"); // jvm // for Android 10/11,MANAGE_EXTERNAL_STORAGE permission from a system setting page - if(PermissionManager.isWaitingFile() && !FFI.serverModel.fileOk){ - PermissionManager.complete("file",await PermissionManager.check("file")); + if (PermissionManager.isWaitingFile() && !FFI.serverModel.fileOk) { + PermissionManager.complete("file", await PermissionManager.check("file")); debugPrint("file permission finished"); } } @@ -280,15 +280,20 @@ class ConnectionManager extends StatelessWidget { children: [ clientInfo(entry.value), entry.value.isFileTransfer - ?SizedBox.shrink() - :IconButton(onPressed: (){ - FFI.chatModel.changeCurrentID(entry.value.id); - final bar = navigationBarKey.currentWidget; - if(bar!=null){ - bar as BottomNavigationBar; - bar.onTap!(1); - } - }, icon: Icon(Icons.chat,color: MyTheme.accent80,)) + ? SizedBox.shrink() + : IconButton( + onPressed: () { + FFI.chatModel.changeCurrentID(entry.value.id); + final bar = navigationBarKey.currentWidget; + if (bar != null) { + bar as BottomNavigationBar; + bar.onTap!(1); + } + }, + icon: Icon( + Icons.chat, + color: MyTheme.accent80, + )) ], ), ElevatedButton.icon( @@ -360,24 +365,24 @@ Widget clientInfo(Client client) { return Padding( padding: EdgeInsets.symmetric(vertical: 8), child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ - Row( - children: [ - CircleAvatar( - child: Text(client.name[0]), backgroundColor: MyTheme.border), - SizedBox(width: 12), - Column( - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text(client.name, - style: TextStyle(color: MyTheme.idColor, fontSize: 20)), - SizedBox(width: 8), - Text(client.peerId, - style: TextStyle(color: MyTheme.idColor, fontSize: 10)) - ]) - ], - ), - ])); + Row( + children: [ + CircleAvatar( + child: Text(client.name[0]), backgroundColor: MyTheme.border), + SizedBox(width: 12), + Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text(client.name, + style: TextStyle(color: MyTheme.idColor, fontSize: 20)), + SizedBox(width: 8), + Text(client.peerId, + style: TextStyle(color: MyTheme.idColor, fontSize: 10)) + ]) + ], + ), + ])); } void toAndroidChannelInit() { @@ -406,6 +411,11 @@ void toAndroidChannelInit() { PermissionManager.complete(type, result); break; } + case "on_media_projection_canceled": + { + FFI.serverModel.stopService(); + break; + } } } catch (e) { debugPrint("MethodCallHandler err:$e");