handle foldable screen size

Signed-off-by: dom <githubaccount56556@proton.me>
This commit is contained in:
dom
2026-04-30 10:13:31 +08:00
parent 5937d83aa9
commit 3a9cafaf24
2 changed files with 71 additions and 22 deletions

View File

@@ -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<Int>("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<String, Int>? {
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() {