80 lines
2.7 KiB
Dart
80 lines
2.7 KiB
Dart
import 'package:flutter/services.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:star_lock/appRouters.dart';
|
||
import 'package:star_lock/app_settings/app_settings.dart';
|
||
import 'package:star_lock/tools/storage.dart';
|
||
|
||
class CallKitHandler {
|
||
static const MethodChannel _channel = MethodChannel('com.starlock/callkit');
|
||
static bool _isInitialized = false;
|
||
|
||
/// 初始化监听,无需context
|
||
static void setupListener() async {
|
||
if (_isInitialized) {
|
||
print('CallKitHandler.setupListener 已初始化,跳过');
|
||
return;
|
||
}
|
||
_isInitialized = true;
|
||
print('CallKitHandler.setupListener 初始化,准备拉取pending事件');
|
||
// 启动时主动拉取pending事件
|
||
final pendingEvent =
|
||
await _channel.invokeMethod<String>('get_pending_event');
|
||
print('CallKitHandler 拉取到pendingEvent: $pendingEvent');
|
||
if (pendingEvent == 'callkit_answered') {
|
||
print('启动时拉取到callkit_answered,跳转到对讲页面');
|
||
// 如需跳转页面可在此处实现
|
||
} else if (pendingEvent == 'callkit_declined') {
|
||
print('启动时拉取到callkit_declined,返回主页面');
|
||
} else {
|
||
print('启动时无pending事件');
|
||
}
|
||
// 注册实时监听
|
||
_channel.setMethodCallHandler((call) async {
|
||
print('CallKitHandler 收到原生事件: ${call.method}');
|
||
if (call.method == 'callkit_answered') {
|
||
print('跳转到对讲页面');
|
||
String currentRoute = Get.currentRoute;
|
||
print('当前路由: $currentRoute');
|
||
if (currentRoute != Routers.h264View) {
|
||
Get.toNamed(
|
||
Routers.h264View,
|
||
);
|
||
}
|
||
} else if (call.method == 'callkit_declined') {
|
||
print('返回主页面');
|
||
} else if (call.method == 'voip_token') {
|
||
print('收到VoIP Token: ${call.arguments}');
|
||
if (call.arguments != null) {
|
||
await Storage.setString(voipToken, call.arguments);
|
||
}
|
||
} else {
|
||
print('收到未知事件: ${call.method}');
|
||
}
|
||
});
|
||
print('CallKitHandler.setupListener 监听已注册');
|
||
}
|
||
|
||
/// 通知原生端结束CallKit通话
|
||
static Future<void> endCall() async {
|
||
print('CallKitHandler.endCall 通知原生端结束通话');
|
||
await _channel.invokeMethod('end_call');
|
||
}
|
||
|
||
/// 主动获取iOS VoIP Token
|
||
static Future<String?> getVoipToken() async {
|
||
try {
|
||
final String? token =
|
||
await _channel.invokeMethod<String>('get_voip_token');
|
||
print('CallKitHandler.getVoipToken 拉取到token: $token');
|
||
if (token != null) {
|
||
await Storage.setString(voipToken, token);
|
||
}
|
||
|
||
return token;
|
||
} catch (e) {
|
||
print('CallKitHandler.getVoipToken 异常: $e');
|
||
return null;
|
||
}
|
||
}
|
||
}
|