diff --git a/android/app/src/main/kotlin/com/example/piliplus/MainActivity.kt b/android/app/src/main/kotlin/com/example/piliplus/MainActivity.kt index 28dcbd39f..28997248a 100644 --- a/android/app/src/main/kotlin/com/example/piliplus/MainActivity.kt +++ b/android/app/src/main/kotlin/com/example/piliplus/MainActivity.kt @@ -27,6 +27,7 @@ import java.io.File class MainActivity : AudioServiceActivity() { private lateinit var methodChannel: MethodChannel + private var isFoldable = false override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) @@ -35,6 +36,7 @@ class MainActivity : AudioServiceActivity() { methodChannel.setMethodCallHandler { call, result -> when (call.method) { "back" -> back(); + "biliSendCommAntifraud" -> { try { val action = call.argument("action") ?: 0 @@ -174,35 +176,54 @@ class MainActivity : AudioServiceActivity() { } "maxScreenSize" -> { - try { - val density = resources.displayMetrics.density - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - val maxBounds = windowManager.maximumWindowMetrics.bounds - result.success( - mapOf( - "maxWidth" to (maxBounds.width() / density).roundToInt(), - "maxHeight" to (maxBounds.height() / density).roundToInt(), - ) - ) - } else { - val realSizePoint = Point() - windowManager.defaultDisplay.getRealSize(realSizePoint) - result.success( - mapOf( - "maxWidth" to (realSizePoint.x / density).roundToInt(), - "maxHeight" to (realSizePoint.y / density).roundToInt(), - ) - ) - } - } catch (e: Exception) { + maxScreenSize()?.let { + result.success(it) } } + "isFoldable" -> { + result.success(isFoldable) + } + else -> result.notImplemented() } } } + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + if (isFoldable) { + maxScreenSize()?.let { + MethodChannel( + flutterEngine!!.dartExecutor.binaryMessenger, + "ScreenChannel" + ).invokeMethod("onConfigChanged", it) + } + } + } + + private fun maxScreenSize(): Map? { + try { + val density = resources.displayMetrics.density + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + val maxBounds = windowManager.maximumWindowMetrics.bounds + return mapOf( + "maxWidth" to (maxBounds.width() / density).roundToInt(), + "maxHeight" to (maxBounds.height() / density).roundToInt(), + ) + } else { + val realSizePoint = Point() + windowManager.defaultDisplay.getRealSize(realSizePoint) + return mapOf( + "maxWidth" to (realSizePoint.x / density).roundToInt(), + "maxHeight" to (realSizePoint.y / density).roundToInt(), + ) + } + } catch (e: Exception) { + return null + } + } + private fun back() { val intent = Intent(Intent.ACTION_MAIN).apply { addCategory(Intent.CATEGORY_HOME) @@ -217,6 +238,14 @@ class MainActivity : AudioServiceActivity() { window.attributes.layoutInDisplayCutoutMode = LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + try { + isFoldable = + packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_HINGE_ANGLE) + } catch (e: Exception) { + } + } } override fun onDestroy() { diff --git a/lib/utils/max_screen_size.dart b/lib/utils/max_screen_size.dart index bd9b04d3e..cc51bfed6 100644 --- a/lib/utils/max_screen_size.dart +++ b/lib/utils/max_screen_size.dart @@ -1,13 +1,33 @@ import 'dart:io' show Platform; import 'package:PiliPlus/utils/utils.dart'; +import 'package:flutter/services.dart' show MethodChannel; abstract final class MaxScreenSize { static int? _maxWidth; static int? _maxHeight; - static Future init() async { + static Future init() { + return Future.wait([_initFoldable(), _initScreenSize()]); + } + + static Future _initFoldable() async { + final isFoldable = await Utils.channel.invokeMethod('isFoldable'); + if (isFoldable == true) { + const MethodChannel('ScreenChannel').setMethodCallHandler((call) async { + if (call.method == 'onConfigChanged') { + _handleRes(call.arguments); + } + }); + } + } + + static Future _initScreenSize() async { final res = await Utils.channel.invokeMethod('maxScreenSize'); + _handleRes(res); + } + + static void _handleRes(dynamic res) { if (res is Map) { _maxWidth = res['maxWidth']; _maxHeight = res['maxHeight'];