feat: enhance background streaming functionality with improved wake lock management

- Updated the wake lock duration in BackgroundStreamingHandler to 3 hours, ensuring the service remains active for longer periods.
- Modified the keepAlive method to support both iOS and Android, allowing for better background task management across platforms.
- Implemented a periodic keep-alive timer in VoiceCallService to refresh the wake lock every 5 minutes, enhancing service reliability during voice calls.
- Added debug logging for successful keep-alive invocations, improving traceability of background operations.
This commit is contained in:
cogwheel0
2025-10-10 19:59:17 +05:30
parent 86892d21c8
commit 4eb1191748
3 changed files with 26 additions and 4 deletions

View File

@@ -42,6 +42,7 @@ class VoiceCallService {
bool _isDisposed = false;
bool _isMuted = false;
SocketEventSubscription? _socketSubscription;
Timer? _keepAliveTimer;
final StreamController<VoiceCallState> _stateController =
StreamController<VoiceCallState>.broadcast();
@@ -137,6 +138,13 @@ class VoiceCallService {
_voiceCallStreamId,
], requiresMicrophone: true);
// Set up periodic keep-alive to refresh wake lock (every 5 minutes)
_keepAliveTimer?.cancel();
_keepAliveTimer = Timer.periodic(
const Duration(minutes: 5),
(_) => BackgroundStreamingHandler.instance.keepAlive(),
);
// Set up socket event listener for assistant responses
_socketSubscription = _socketService.addChatEventHandler(
conversationId: conversationId,
@@ -149,6 +157,8 @@ class VoiceCallService {
await _startListening();
} catch (e) {
_updateState(VoiceCallState.error);
_keepAliveTimer?.cancel();
_keepAliveTimer = null;
await WakelockPlus.disable();
await _notificationService.cancelNotification();
await BackgroundStreamingHandler.instance.stopBackgroundExecution(const [
@@ -334,6 +344,10 @@ class VoiceCallService {
Future<void> stopCall() async {
if (_isDisposed) return;
// Cancel keep-alive timer
_keepAliveTimer?.cancel();
_keepAliveTimer = null;
await _transcriptSubscription?.cancel();
await _intensitySubscription?.cancel();
_socketSubscription?.dispose();
@@ -436,6 +450,10 @@ class VoiceCallService {
Future<void> dispose() async {
_isDisposed = true;
// Cancel keep-alive timer
_keepAliveTimer?.cancel();
_keepAliveTimer = null;
await _transcriptSubscription?.cancel();
await _intensitySubscription?.cancel();
_socketSubscription?.dispose();