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

@@ -150,7 +150,7 @@ class BackgroundStreamingService : Service() {
PowerManager.PARTIAL_WAKE_LOCK,
"Conduit::StreamingWakeLock"
).apply {
acquire(15 * 60 * 1000L) // 15 minutes max
acquire(3 * 60 * 60 * 1000L) // 3 hours max (refreshed every 5 minutes)
}
println("BackgroundStreamingService: Wake lock acquired")
}

View File

@@ -169,12 +169,16 @@ class BackgroundStreamingHandler {
return _streamStates[streamId];
}
/// Keep alive the background task (iOS only)
/// Keep alive the background task
///
/// On iOS: Refreshes background task to prevent early termination
/// On Android: Refreshes wake lock to keep service running
Future<void> keepAlive() async {
if (!Platform.isIOS) return;
if (!Platform.isIOS && !Platform.isAndroid) return;
try {
await _channel.invokeMethod('keepAlive');
DebugLogger.stream('keepalive-success', scope: 'background');
} catch (e) {
DebugLogger.error('keepalive-failed', scope: 'background', error: e);
}

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();