105 lines
3.0 KiB
Dart
105 lines
3.0 KiB
Dart
import 'package:flutter/services.dart';
|
|
|
|
/// 当前 Android 设备的运行环境。
|
|
class AppRuntimeEnvironment {
|
|
final bool isEmulator;
|
|
final int displayCount;
|
|
|
|
const AppRuntimeEnvironment({
|
|
required this.isEmulator,
|
|
required this.displayCount,
|
|
});
|
|
|
|
factory AppRuntimeEnvironment.fromMap(Map<dynamic, dynamic>? data) {
|
|
return AppRuntimeEnvironment(
|
|
isEmulator: data?['isEmulator'] == true,
|
|
displayCount: (data?['displayCount'] as num?)?.toInt() ?? 1,
|
|
);
|
|
}
|
|
}
|
|
|
|
class AppNetworkSnapshot {
|
|
final bool connected;
|
|
final String type;
|
|
final int receivedBytes;
|
|
final int transmittedBytes;
|
|
|
|
const AppNetworkSnapshot({
|
|
required this.connected,
|
|
required this.type,
|
|
required this.receivedBytes,
|
|
required this.transmittedBytes,
|
|
});
|
|
|
|
factory AppNetworkSnapshot.fromMap(Map<dynamic, dynamic>? data) {
|
|
return AppNetworkSnapshot(
|
|
connected: data?['connected'] == true,
|
|
type: data?['type']?.toString() ?? 'offline',
|
|
receivedBytes: (data?['rxBytes'] as num?)?.toInt() ?? 0,
|
|
transmittedBytes: (data?['txBytes'] as num?)?.toInt() ?? 0,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 统一管理虚拟机识别和硬件手动输入模式,避免各业务页面自行猜测设备能力。
|
|
abstract class AppRuntimeTool {
|
|
static const MethodChannel _channel = MethodChannel(
|
|
'biz_xfj/runtime_environment',
|
|
);
|
|
|
|
static AppRuntimeEnvironment _environment = const AppRuntimeEnvironment(
|
|
isEmulator: false,
|
|
displayCount: 1,
|
|
);
|
|
static bool _initialized = false;
|
|
static bool _manualHardwareInput = false;
|
|
|
|
static bool get isEmulator => _environment.isEmulator;
|
|
|
|
static int get displayCount => _environment.displayCount;
|
|
|
|
/// 虚拟机始终使用手动输入;真机可在设置中主动开启手动输入模式。
|
|
static bool get useManualHardwareInput => isEmulator || _manualHardwareInput;
|
|
|
|
static bool get canUsePhysicalHardware => !useManualHardwareInput;
|
|
|
|
static bool get hasMultipleDisplays => displayCount >= 2;
|
|
|
|
static Future<void> initializePlatform() async {
|
|
if (_initialized) return;
|
|
try {
|
|
final data = await _channel.invokeMapMethod<dynamic, dynamic>(
|
|
'getRuntimeEnvironment',
|
|
);
|
|
_environment = AppRuntimeEnvironment.fromMap(data);
|
|
} on PlatformException {
|
|
// 原生通道异常时按单屏处理,避免错误地启动副屏。
|
|
_environment = const AppRuntimeEnvironment(
|
|
isEmulator: false,
|
|
displayCount: 1,
|
|
);
|
|
}
|
|
_initialized = true;
|
|
}
|
|
|
|
static Future<AppNetworkSnapshot> getNetworkSnapshot() async {
|
|
try {
|
|
final data = await _channel.invokeMapMethod<dynamic, dynamic>(
|
|
'getNetworkSnapshot',
|
|
);
|
|
return AppNetworkSnapshot.fromMap(data);
|
|
} on PlatformException {
|
|
return const AppNetworkSnapshot(
|
|
connected: false,
|
|
type: 'offline',
|
|
receivedBytes: 0,
|
|
transmittedBytes: 0,
|
|
);
|
|
}
|
|
}
|
|
|
|
static void applyManualHardwareInput(bool enabled) {
|
|
_manualHardwareInput = enabled;
|
|
}
|
|
}
|