mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-05-13 20:53:58 +08:00
handle foldable screen size
Signed-off-by: dom <githubaccount56556@proton.me>
This commit is contained in:
@@ -27,6 +27,7 @@ import java.io.File
|
|||||||
|
|
||||||
class MainActivity : AudioServiceActivity() {
|
class MainActivity : AudioServiceActivity() {
|
||||||
private lateinit var methodChannel: MethodChannel
|
private lateinit var methodChannel: MethodChannel
|
||||||
|
private var isFoldable = false
|
||||||
|
|
||||||
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
|
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
|
||||||
super.configureFlutterEngine(flutterEngine)
|
super.configureFlutterEngine(flutterEngine)
|
||||||
@@ -35,6 +36,7 @@ class MainActivity : AudioServiceActivity() {
|
|||||||
methodChannel.setMethodCallHandler { call, result ->
|
methodChannel.setMethodCallHandler { call, result ->
|
||||||
when (call.method) {
|
when (call.method) {
|
||||||
"back" -> back();
|
"back" -> back();
|
||||||
|
|
||||||
"biliSendCommAntifraud" -> {
|
"biliSendCommAntifraud" -> {
|
||||||
try {
|
try {
|
||||||
val action = call.argument<Int>("action") ?: 0
|
val action = call.argument<Int>("action") ?: 0
|
||||||
@@ -174,35 +176,54 @@ class MainActivity : AudioServiceActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
"maxScreenSize" -> {
|
"maxScreenSize" -> {
|
||||||
try {
|
maxScreenSize()?.let {
|
||||||
val density = resources.displayMetrics.density
|
result.success(it)
|
||||||
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) {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
"isFoldable" -> {
|
||||||
|
result.success(isFoldable)
|
||||||
|
}
|
||||||
|
|
||||||
else -> result.notImplemented()
|
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() {
|
private fun back() {
|
||||||
val intent = Intent(Intent.ACTION_MAIN).apply {
|
val intent = Intent(Intent.ACTION_MAIN).apply {
|
||||||
addCategory(Intent.CATEGORY_HOME)
|
addCategory(Intent.CATEGORY_HOME)
|
||||||
@@ -217,6 +238,14 @@ class MainActivity : AudioServiceActivity() {
|
|||||||
window.attributes.layoutInDisplayCutoutMode =
|
window.attributes.layoutInDisplayCutoutMode =
|
||||||
LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
|
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() {
|
override fun onDestroy() {
|
||||||
|
|||||||
@@ -1,13 +1,33 @@
|
|||||||
import 'dart:io' show Platform;
|
import 'dart:io' show Platform;
|
||||||
|
|
||||||
import 'package:PiliPlus/utils/utils.dart';
|
import 'package:PiliPlus/utils/utils.dart';
|
||||||
|
import 'package:flutter/services.dart' show MethodChannel;
|
||||||
|
|
||||||
abstract final class MaxScreenSize {
|
abstract final class MaxScreenSize {
|
||||||
static int? _maxWidth;
|
static int? _maxWidth;
|
||||||
static int? _maxHeight;
|
static int? _maxHeight;
|
||||||
|
|
||||||
static Future<void> init() async {
|
static Future<void> init() {
|
||||||
|
return Future.wait([_initFoldable(), _initScreenSize()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<void> _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<void> _initScreenSize() async {
|
||||||
final res = await Utils.channel.invokeMethod('maxScreenSize');
|
final res = await Utils.channel.invokeMethod('maxScreenSize');
|
||||||
|
_handleRes(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void _handleRes(dynamic res) {
|
||||||
if (res is Map) {
|
if (res is Map) {
|
||||||
_maxWidth = res['maxWidth'];
|
_maxWidth = res['maxWidth'];
|
||||||
_maxHeight = res['maxHeight'];
|
_maxHeight = res['maxHeight'];
|
||||||
|
|||||||
Reference in New Issue
Block a user