From aa62af000726c0fbcc67175770da7e2ac7163da7 Mon Sep 17 00:00:00 2001 From: Predidit <34627277+Predidit@users.noreply.github.com> Date: Thu, 12 Sep 2024 13:42:09 +0800 Subject: [PATCH] fix android multi window mode --- README.md | 2 +- .../kotlin/com/example/kazumi/MainActivity.kt | 16 ++++++++++-- lib/pages/video/video_controller.dart | 26 ++++++++++--------- lib/utils/utils.dart | 18 ++++++++++++- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index fb8e964e..ab091fd5 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ 对于 GNU/Linux 用户,可以从 Flathub 安装: -[![Get it on Flathub](https://flathub.org/api/badge?locale=zh-Hans)](https://flathub.org/apps/io.github.Predidit.Kazumi) +[![Get it on Flathub](https://flathub.org/api/badge?locale=en)](https://flathub.org/apps/io.github.Predidit.Kazumi) ## 贡献 diff --git a/android/app/src/main/kotlin/com/example/kazumi/MainActivity.kt b/android/app/src/main/kotlin/com/example/kazumi/MainActivity.kt index ce6bbe96..6b232063 100644 --- a/android/app/src/main/kotlin/com/example/kazumi/MainActivity.kt +++ b/android/app/src/main/kotlin/com/example/kazumi/MainActivity.kt @@ -1,6 +1,7 @@ package com.example.kazumi import android.content.Intent +import android.os.Build; import android.net.Uri import android.os.Bundle import androidx.annotation.NonNull @@ -23,9 +24,12 @@ class MainActivity: FlutterActivity() { } else { result.error("INVALID_ARGUMENT", "URL and MIME type required", null) } - } else { - result.notImplemented() } + if (call.method == "checkIfInMultiWindowMode") { + val isInMultiWindow = checkIfInMultiWindowMode() + result.success(isInMultiWindow) + } + result.notImplemented() } } @@ -35,4 +39,12 @@ class MainActivity: FlutterActivity() { intent.setDataAndType(Uri.parse(url), mimeType) startActivity(intent) } + + private fun checkIfInMultiWindowMode(): Boolean { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + this.isInMultiWindowMode + } else { + false + } + } } diff --git a/lib/pages/video/video_controller.dart b/lib/pages/video/video_controller.dart index 2cba6d61..e1d10347 100644 --- a/lib/pages/video/video_controller.dart +++ b/lib/pages/video/video_controller.dart @@ -76,10 +76,16 @@ abstract class _VideoPageController with Store { await windowManager.setFullScreen(true); return; } - await landScape(); await SystemChrome.setEnabledSystemUIMode( SystemUiMode.immersiveSticky, ); + if (Platform.isAndroid) { + bool isInMultiWindowMode = await Utils.isInMultiWindowMode(); + if (isInMultiWindowMode) { + return; + } + } + await landScape(); } //退出全屏显示 @@ -87,12 +93,9 @@ abstract class _VideoPageController with Store { if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) { await windowManager.setFullScreen(false); } - dynamic document; late SystemUiMode mode = SystemUiMode.edgeToEdge; try { - if (kIsWeb) { - document.exitFullscreen(); - } else if (Platform.isAndroid || Platform.isIOS) { + if (Platform.isAndroid || Platform.isIOS) { if (Platform.isAndroid && (await DeviceInfoPlugin().androidInfo).version.sdkInt < 29) { mode = SystemUiMode.manual; @@ -102,6 +105,12 @@ abstract class _VideoPageController with Store { overlays: SystemUiOverlay.values, ); if (Utils.isCompact()) { + if (Platform.isAndroid) { + bool isInMultiWindowMode = await Utils.isInMultiWindowMode(); + if (isInMultiWindowMode) { + return; + } + } verticalScreen(); } } @@ -118,21 +127,14 @@ abstract class _VideoPageController with Store { if (kIsWeb) { await document.documentElement?.requestFullscreen(); } else if (Platform.isAndroid || Platform.isIOS) { - // await SystemChrome.setEnabledSystemUIMode( - // SystemUiMode.immersiveSticky, - // overlays: [], - // ); await SystemChrome.setPreferredOrientations( [ DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight, ], ); - // await AutoOrientation.landscapeAutoMode(forceSensor: true); } } catch (exception, stacktrace) { - // debugPrint(exception.toString()); - // debugPrint(stacktrace.toString()); KazumiLogger() .log(Level.error, exception.toString(), stackTrace: stacktrace); } diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index 94e3de07..79e43ebe 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -32,7 +32,8 @@ class Utils { static String getRandomUA() { final random = Random(); - String randomElement = userAgentsList[random.nextInt(userAgentsList.length)]; + String randomElement = + userAgentsList[random.nextInt(userAgentsList.length)]; return randomElement; } @@ -445,4 +446,19 @@ class Utils { static bool isCompact() { return !isDesktop() && !isWideScreen(); } + + /// 判断是否分屏模式 (android only) + static Future isInMultiWindowMode() async { + if (Platform.isAndroid) { + const platform = MethodChannel('com.predidit.kazumi/intent'); + try { + final bool result = await platform.invokeMethod('checkIfInMultiWindowMode'); + return result; + } on PlatformException catch (e) { + print("Failed to check multi window mode: '${e.message}'."); + return false; + } + } + return false; + } }