Skip to content

Commit c5103bf

Browse files
fix(android): prevent screen from turning off during active video calls in example application (#1057)
This PR fixes an issue in the Android example application where the device screen turns off automatically during an active video call due to screen timeout. ### **Problem** During an ongoing LiveKit call on Android, the screen is not kept awake, which leads to: - Poor user experience during video calls - Interrupted visibility when no user interaction occurs ### **Solution** This PR adds native Android window flag handling in MainActivity to ensure the screen remains active while a call is in progress: - Uses native flags to prevent screen timeout - Uses native flags as well to handle lock screen visibility ### **The flags are:** - **Enabled** when the room is active - **Cleared** when the room is disposed --------- Co-authored-by: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com>
1 parent 3ec0288 commit c5103bf

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
11
package io.livekit.example
22

3+
import android.os.Build
4+
import android.os.Bundle
5+
import android.view.WindowManager
36
import io.flutter.embedding.android.FlutterActivity
7+
import io.flutter.embedding.engine.FlutterEngine
8+
import io.flutter.plugin.common.MethodChannel
49

5-
class MainActivity: FlutterActivity() {
6-
}
10+
class MainActivity : FlutterActivity() {
11+
12+
private val CHANNEL = "livekit_incall"
13+
14+
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
15+
super.configureFlutterEngine(flutterEngine)
16+
17+
MethodChannel(
18+
flutterEngine.dartExecutor.binaryMessenger,
19+
CHANNEL
20+
).setMethodCallHandler { call, result ->
21+
when (call.method) {
22+
"enableInCall" -> {
23+
setInCallScreenFlags()
24+
result.success(null)
25+
}
26+
"disableInCall" -> {
27+
clearInCallScreenFlags()
28+
result.success(null)
29+
}
30+
else -> result.notImplemented()
31+
}
32+
}
33+
}
34+
35+
private fun setInCallScreenFlags() {
36+
val window = window
37+
38+
// Keep screen on during call
39+
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
40+
}
41+
42+
private fun clearInCallScreenFlags() {
43+
val window = window
44+
45+
// Remove keep screen on
46+
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
47+
}
48+
}

example/lib/pages/room.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:convert';
33
import 'dart:math' as math;
44

55
import 'package:flutter/material.dart';
6+
import 'package:flutter/services.dart';
67
import 'package:livekit_client/livekit_client.dart';
78

89
import '../exts.dart';
@@ -34,6 +35,11 @@ class _RoomPageState extends State<RoomPage> {
3435
@override
3536
void initState() {
3637
super.initState();
38+
39+
if (lkPlatformIs(PlatformType.android)) {
40+
unawaited(enableInCallFlags());
41+
}
42+
3743
// add callback for a `RoomEvent` as opposed to a `ParticipantEvent`
3844
widget.room.addListener(_onRoomDidUpdate);
3945
// add callbacks for finer grained events
@@ -63,6 +69,9 @@ class _RoomPageState extends State<RoomPage> {
6369
widget.room.removeListener(_onRoomDidUpdate);
6470
unawaited(_disposeRoomAsync());
6571
onWindowShouldClose = null;
72+
if (lkPlatformIs(PlatformType.android)) {
73+
unawaited(disableInCallFlags());
74+
}
6675
super.dispose();
6776
}
6877

@@ -71,6 +80,16 @@ class _RoomPageState extends State<RoomPage> {
7180
await widget.room.dispose();
7281
}
7382

83+
static const platform = MethodChannel('livekit_incall');
84+
85+
Future<void> enableInCallFlags() async {
86+
await platform.invokeMethod('enableInCall');
87+
}
88+
89+
Future<void> disableInCallFlags() async {
90+
await platform.invokeMethod('disableInCall');
91+
}
92+
7493
/// for more information, see [event types](https://docs.livekit.io/client/events/#events)
7594
void _setUpListeners() => _listener
7695
..on<RoomDisconnectedEvent>((event) async {

0 commit comments

Comments
 (0)