import 'dart:io'; import 'package:flutter/services.dart'; import 'package:matrix/matrix.dart'; /// The Android foreground service that keeps a call going with the app in /// the background (android/.../CallService.kt): without it Android cuts a /// backgrounded app off from the microphone and camera, and screen sharing /// can't start at all. Its notification shows the call, opens it and hangs /// up. Nothing on other platforms. class CallService { static const _channel = MethodChannel('warp/call_service'); /// Hang up from the notification, and tapping it. static void listen({ required void Function() onHangUp, required void Function() onOpen, }) { if (!Platform.isAndroid) return; _channel.setMethodCallHandler((call) async { switch (call.method) { case 'hangUp': onHangUp(); case 'openCall': onOpen(); } }); } /// Starts the service, or changes what it declares: the camera while it /// is on, the screen while it is shared. False if Android refused. static Future update({ required String title, required DateTime startedAt, required bool camera, required bool screen, }) async { if (!Platform.isAndroid) return true; try { return await _channel.invokeMethod('update', { 'title': title, 'startedAt': startedAt.millisecondsSinceEpoch, 'camera': camera, 'screen': screen, }) ?? false; } catch (e) { Logs().w('Could not start the call service', e); return false; } } static Future stop() async { if (!Platform.isAndroid) return; try { await _channel.invokeMethod('stop'); } catch (e) { Logs().w('Could not stop the call service', e); } } }