Add wake lock and device keepalive for reliable mobile calls

Prevents screen sleep during calls and auto-reconnects the Twilio device
after app backgrounding. JS handles Screen Wake Lock API and 30s keepalive
polling; Flutter coordinates via WakelockPlus and synthetic visibilitychange
events on app resume.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Claude
2026-04-27 05:52:39 -07:00
parent fe8ddd51bb
commit 3407f4c705
5 changed files with 197 additions and 34 deletions

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_android/webview_flutter_android.dart';
import '../services/push_notification_service.dart';
@@ -52,10 +53,27 @@ class _PhoneScreenState extends State<PhoneScreen> with WidgetsBindingObserver {
}
_initWebView();
_initPush();
WakelockPlus.enable();
debugPrint('TWP: Wake lock enabled');
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
debugPrint('TWP: App resumed — re-enabling wake lock');
WakelockPlus.enable();
// Dispatch synthetic visibilitychange to trigger JS-side recovery
_controller.runJavaScript(
'document.dispatchEvent(new Event("visibilitychange"));'
'Object.defineProperty(document, "hidden", {value: false, configurable: true});',
);
}
}
@override
void dispose() {
WakelockPlus.disable();
debugPrint('TWP: Wake lock disabled');
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}