forked from eatvio/eatvio_app
feat(vide_calls): gms calls handling
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:audio_session/audio_session.dart';
|
||||
|
||||
typedef VideoCallAudioSessionFactory = Future<AudioSession> Function();
|
||||
|
||||
/// Configures and owns the platform audio session for an active VoIP call.
|
||||
class VideoCallAudioSessionCoordinator {
|
||||
VideoCallAudioSessionCoordinator({
|
||||
VideoCallAudioSessionFactory? audioSessionFactory,
|
||||
}) : _audioSessionFactory =
|
||||
audioSessionFactory ?? (() => AudioSession.instance);
|
||||
|
||||
final VideoCallAudioSessionFactory _audioSessionFactory;
|
||||
|
||||
static final AudioSessionConfiguration callConfiguration =
|
||||
AudioSessionConfiguration(
|
||||
avAudioSessionCategory: AVAudioSessionCategory.playAndRecord,
|
||||
avAudioSessionCategoryOptions:
|
||||
AVAudioSessionCategoryOptions.allowBluetooth |
|
||||
AVAudioSessionCategoryOptions.defaultToSpeaker,
|
||||
avAudioSessionMode: AVAudioSessionMode.voiceChat,
|
||||
avAudioSessionRouteSharingPolicy:
|
||||
AVAudioSessionRouteSharingPolicy.defaultPolicy,
|
||||
avAudioSessionSetActiveOptions: AVAudioSessionSetActiveOptions.none,
|
||||
androidAudioAttributes: const AndroidAudioAttributes(
|
||||
contentType: AndroidAudioContentType.speech,
|
||||
flags: AndroidAudioFlags.none,
|
||||
usage: AndroidAudioUsage.voiceCommunication,
|
||||
),
|
||||
androidAudioFocusGainType: AndroidAudioFocusGainType.gain,
|
||||
androidWillPauseWhenDucked: true,
|
||||
);
|
||||
|
||||
StreamSubscription<AudioInterruptionEvent>? _interruptionSubscription;
|
||||
AudioSession? _session;
|
||||
bool _configured = false;
|
||||
|
||||
bool get isActive => _session != null;
|
||||
|
||||
Future<void> start({
|
||||
required void Function(AudioInterruptionEvent event) onInterruption,
|
||||
}) async {
|
||||
final session = await _audioSessionFactory();
|
||||
_session = session;
|
||||
if (!_configured) {
|
||||
await session.configure(callConfiguration);
|
||||
_configured = true;
|
||||
}
|
||||
await session.setActive(true);
|
||||
await _interruptionSubscription?.cancel();
|
||||
_interruptionSubscription = session.interruptionEventStream.listen(
|
||||
onInterruption,
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> reactivate() async {
|
||||
final session = _session;
|
||||
if (session == null) return false;
|
||||
try {
|
||||
return await session.setActive(true);
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> stop() async {
|
||||
await _interruptionSubscription?.cancel();
|
||||
_interruptionSubscription = null;
|
||||
final session = _session;
|
||||
_session = null;
|
||||
if (session == null) return;
|
||||
try {
|
||||
await session.setActive(false);
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
|
||||
|
||||
abstract final class VideoCallForegroundBootstrap {
|
||||
static const int _serviceId = 1001;
|
||||
|
||||
static bool _initialized = false;
|
||||
|
||||
static void ensureInitialized() {
|
||||
if (_initialized) return;
|
||||
FlutterForegroundTask.init(
|
||||
androidNotificationOptions: AndroidNotificationOptions(
|
||||
channelId: 'video_call_ongoing',
|
||||
channelName: 'Video call',
|
||||
channelDescription: 'Ongoing video call notifications',
|
||||
channelImportance: NotificationChannelImportance.LOW,
|
||||
priority: NotificationPriority.LOW,
|
||||
onlyAlertOnce: true,
|
||||
),
|
||||
iosNotificationOptions: const IOSNotificationOptions(
|
||||
showNotification: true,
|
||||
playSound: false,
|
||||
),
|
||||
foregroundTaskOptions: ForegroundTaskOptions(
|
||||
eventAction: ForegroundTaskEventAction.nothing(),
|
||||
autoRunOnBoot: false,
|
||||
autoRunOnMyPackageReplaced: false,
|
||||
allowWakeLock: true,
|
||||
allowWifiLock: true,
|
||||
),
|
||||
);
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
static int get serviceId => _serviceId;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:eatvio_app/features/video_calls/services/video_call_foreground_bootstrap.dart';
|
||||
import 'package:eatvio_app/features/video_calls/services/video_call_task_handler.dart';
|
||||
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
class VideoCallForegroundCoordinator {
|
||||
Future<void> ensureNotificationPermission() async {
|
||||
final permission = await FlutterForegroundTask.checkNotificationPermission();
|
||||
if (permission == NotificationPermission.granted) return;
|
||||
await FlutterForegroundTask.requestNotificationPermission();
|
||||
}
|
||||
|
||||
Future<void> startOngoingCall({
|
||||
required String conversationUuid,
|
||||
required String notificationTitle,
|
||||
required String notificationBody,
|
||||
required String endCallButtonLabel,
|
||||
}) async {
|
||||
VideoCallForegroundBootstrap.ensureInitialized();
|
||||
await ensureNotificationPermission();
|
||||
|
||||
await FlutterForegroundTask.saveData(
|
||||
key: videoCallActiveConversationStorageKey,
|
||||
value: conversationUuid,
|
||||
);
|
||||
|
||||
if (await FlutterForegroundTask.isRunningService) {
|
||||
await FlutterForegroundTask.updateService(
|
||||
notificationTitle: notificationTitle,
|
||||
notificationText: notificationBody,
|
||||
notificationButtons: [
|
||||
NotificationButton(
|
||||
id: videoCallForegroundEndCallButtonId,
|
||||
text: endCallButtonLabel,
|
||||
),
|
||||
],
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
final micStatus = await Permission.microphone.status;
|
||||
if (!micStatus.isGranted) {
|
||||
await Permission.microphone.request();
|
||||
}
|
||||
}
|
||||
|
||||
await FlutterForegroundTask.startService(
|
||||
serviceId: VideoCallForegroundBootstrap.serviceId,
|
||||
serviceTypes: const [ForegroundServiceTypes.microphone],
|
||||
notificationTitle: notificationTitle,
|
||||
notificationText: notificationBody,
|
||||
notificationInitialRoute: '/video-call-screen',
|
||||
notificationButtons: [
|
||||
NotificationButton(
|
||||
id: videoCallForegroundEndCallButtonId,
|
||||
text: endCallButtonLabel,
|
||||
),
|
||||
],
|
||||
callback: videoCallForegroundTaskStartCallback,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> stopOngoingCall() async {
|
||||
if (!await FlutterForegroundTask.isRunningService) return;
|
||||
await FlutterForegroundTask.stopService();
|
||||
await FlutterForegroundTask.removeData(
|
||||
key: videoCallActiveConversationStorageKey,
|
||||
);
|
||||
}
|
||||
|
||||
Future<String?> readActiveConversationUuid() {
|
||||
return FlutterForegroundTask.getData<String>(
|
||||
key: videoCallActiveConversationStorageKey,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
typedef VideoCallLifecycleCallback = void Function(AppLifecycleState state);
|
||||
|
||||
class VideoCallLifecycleBinding {
|
||||
AppLifecycleListener? _listener;
|
||||
VideoCallLifecycleCallback? _callback;
|
||||
|
||||
@visibleForTesting
|
||||
void simulateLifecycle(AppLifecycleState state) => _callback?.call(state);
|
||||
|
||||
void attach(VideoCallLifecycleCallback onStateChanged) {
|
||||
_callback = onStateChanged;
|
||||
_listener?.dispose();
|
||||
_listener = AppLifecycleListener(
|
||||
onResume: () => onStateChanged(AppLifecycleState.resumed),
|
||||
onInactive: () => onStateChanged(AppLifecycleState.inactive),
|
||||
onHide: () => onStateChanged(AppLifecycleState.hidden),
|
||||
onPause: () => onStateChanged(AppLifecycleState.paused),
|
||||
onDetach: () => onStateChanged(AppLifecycleState.detached),
|
||||
);
|
||||
}
|
||||
|
||||
void detach() {
|
||||
_listener?.dispose();
|
||||
_listener = null;
|
||||
_callback = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:eatvio_app/core/app_router.dart';
|
||||
import 'package:eatvio_app/core/locator_service.dart';
|
||||
import 'package:eatvio_app/features/video_calls/managers/video_call_manager.dart';
|
||||
import 'package:eatvio_app/features/video_calls/presentation/pages/video_call_screen.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class VideoCallNotificationNavigator {
|
||||
static Future<void> openActiveCall({String? conversationUuid}) async {
|
||||
final manager = sl<VideoCallManager>();
|
||||
final uuid = conversationUuid ?? manager.conversationUuid;
|
||||
if (uuid.isEmpty) return;
|
||||
|
||||
for (var attempt = 0; attempt < 40; attempt++) {
|
||||
final ctx = AppRouter.navigatorKey.currentContext;
|
||||
if (ctx != null && ctx.mounted) {
|
||||
final router = GoRouter.of(ctx);
|
||||
final matches = router.routerDelegate.currentConfiguration.matches;
|
||||
final onCallRoute = matches.any(
|
||||
(m) => m.matchedLocation.contains(VideoCallScreen.path),
|
||||
);
|
||||
if (!onCallRoute) {
|
||||
await router.pushNamed(
|
||||
VideoCallScreen.name,
|
||||
extra: <String, dynamic>{
|
||||
'conversationUuid': uuid,
|
||||
'counterpartName': manager.counterpartName,
|
||||
'localParticipantName': manager.localParticipantName,
|
||||
},
|
||||
);
|
||||
}
|
||||
await manager.onCallScreenVisible(
|
||||
conversationUuid: uuid,
|
||||
counterpartName: manager.counterpartName,
|
||||
localParticipantName: manager.localParticipantName,
|
||||
);
|
||||
return;
|
||||
}
|
||||
await Future<void>.delayed(const Duration(milliseconds: 50));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
|
||||
|
||||
const String videoCallForegroundTaskActionKey = 'action';
|
||||
const String videoCallForegroundOpenAction = 'open';
|
||||
const String videoCallForegroundEndCallAction = 'end_call';
|
||||
const String videoCallForegroundEndCallButtonId = 'end_call';
|
||||
const String videoCallActiveConversationStorageKey = 'video_call_active_conversation_uuid';
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void videoCallForegroundTaskStartCallback() {
|
||||
FlutterForegroundTask.setTaskHandler(VideoCallTaskHandler());
|
||||
}
|
||||
|
||||
class VideoCallTaskHandler extends TaskHandler {
|
||||
@override
|
||||
Future<void> onStart(DateTime timestamp, TaskStarter starter) async {}
|
||||
|
||||
@override
|
||||
void onRepeatEvent(DateTime timestamp) {}
|
||||
|
||||
@override
|
||||
Future<void> onDestroy(DateTime timestamp, bool isTimeout) async {}
|
||||
|
||||
@override
|
||||
void onReceiveData(Object data) {}
|
||||
|
||||
@override
|
||||
void onNotificationButtonPressed(String id) {
|
||||
if (id == videoCallForegroundEndCallButtonId) {
|
||||
FlutterForegroundTask.sendDataToMain(<String, String>{
|
||||
videoCallForegroundTaskActionKey: videoCallForegroundEndCallAction,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onNotificationPressed() {
|
||||
FlutterForegroundTask.sendDataToMain(<String, String>{
|
||||
videoCallForegroundTaskActionKey: videoCallForegroundOpenAction,
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void onNotificationDismissed() {}
|
||||
}
|
||||
Reference in New Issue
Block a user