2022-02-15 09:25:13 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
import 'package:callkeep/callkeep.dart';
|
2022-09-10 12:12:52 +02:00
|
|
|
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2022-02-15 09:25:13 +01:00
|
|
|
import 'package:matrix/matrix.dart';
|
2022-09-10 12:12:52 +02:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2022-02-15 09:25:13 +01:00
|
|
|
|
|
|
|
import 'package:fluffychat/utils/voip_plugin.dart';
|
|
|
|
|
|
|
|
class CallKeeper {
|
2022-09-10 12:12:52 +02:00
|
|
|
CallKeeper(this.callKeepManager, this.call) {
|
|
|
|
call.onCallStateChanged.stream.listen(_handleCallState);
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CallKeepManager callKeepManager;
|
2022-09-10 12:12:52 +02:00
|
|
|
bool? held = false;
|
|
|
|
bool? muted = false;
|
2022-02-15 09:25:13 +01:00
|
|
|
bool connected = false;
|
2022-09-10 12:12:52 +02:00
|
|
|
CallSession call;
|
2022-02-15 09:25:13 +01:00
|
|
|
|
2022-09-10 12:12:52 +02:00
|
|
|
// update native caller to show what remote user has done.
|
2022-02-15 09:25:13 +01:00
|
|
|
void _handleCallState(CallState state) {
|
2022-09-10 12:12:52 +02:00
|
|
|
Logs().i('CallKeepManager::handleCallState: ${state.toString()}');
|
2022-02-15 09:25:13 +01:00
|
|
|
switch (state) {
|
|
|
|
case CallState.kConnecting:
|
2022-09-10 12:12:52 +02:00
|
|
|
Logs().v('callkeep connecting');
|
2022-02-15 09:25:13 +01:00
|
|
|
break;
|
|
|
|
case CallState.kConnected:
|
2022-09-10 12:12:52 +02:00
|
|
|
Logs().v('callkeep connected');
|
2022-02-15 09:25:13 +01:00
|
|
|
if (!connected) {
|
2022-09-10 12:12:52 +02:00
|
|
|
callKeepManager.answer(call.callId);
|
2022-02-15 09:25:13 +01:00
|
|
|
} else {
|
2022-09-10 12:12:52 +02:00
|
|
|
callKeepManager.setMutedCall(call.callId, false);
|
|
|
|
callKeepManager.setOnHold(call.callId, false);
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CallState.kEnded:
|
2022-09-10 12:12:52 +02:00
|
|
|
callKeepManager.hangup(call.callId);
|
2022-02-15 09:25:13 +01:00
|
|
|
break;
|
|
|
|
/* TODO:
|
|
|
|
case CallState.kMuted:
|
|
|
|
callKeepManager.setMutedCall(uuid, true);
|
|
|
|
break;
|
|
|
|
case CallState.kHeld:
|
|
|
|
callKeepManager.setOnHold(uuid, true);
|
|
|
|
break;
|
|
|
|
*/
|
|
|
|
case CallState.kFledgling:
|
|
|
|
// TODO: Handle this case.
|
|
|
|
break;
|
|
|
|
case CallState.kInviteSent:
|
|
|
|
// TODO: Handle this case.
|
|
|
|
break;
|
|
|
|
case CallState.kWaitLocalMedia:
|
|
|
|
// TODO: Handle this case.
|
|
|
|
break;
|
|
|
|
case CallState.kCreateOffer:
|
|
|
|
// TODO: Handle this case.
|
|
|
|
break;
|
|
|
|
case CallState.kCreateAnswer:
|
|
|
|
// TODO: Handle this case.
|
|
|
|
break;
|
|
|
|
case CallState.kRinging:
|
|
|
|
// TODO: Handle this case.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:12:52 +02:00
|
|
|
Map<String?, CallKeeper> calls = <String?, CallKeeper>{};
|
|
|
|
|
2022-02-15 09:25:13 +01:00
|
|
|
class CallKeepManager {
|
|
|
|
factory CallKeepManager() {
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
CallKeepManager._internal() {
|
|
|
|
_callKeep = FlutterCallkeep();
|
|
|
|
}
|
|
|
|
|
|
|
|
static final CallKeepManager _instance = CallKeepManager._internal();
|
|
|
|
|
|
|
|
late FlutterCallkeep _callKeep;
|
|
|
|
VoipPlugin? _voipPlugin;
|
|
|
|
|
2022-09-10 12:12:52 +02:00
|
|
|
String get appName => 'FluffyChat';
|
|
|
|
Future<bool> get hasPhoneAccountEnabled async =>
|
|
|
|
await _callKeep.hasPhoneAccount();
|
2022-02-15 09:25:13 +01:00
|
|
|
|
|
|
|
Map<String, dynamic> get alertOptions => <String, dynamic>{
|
|
|
|
'alertTitle': 'Permissions required',
|
2022-09-10 12:12:52 +02:00
|
|
|
'alertDescription':
|
|
|
|
'Allow $appName to register as a calling account? This will allow calls to be handled by the native android dialer.',
|
2022-02-15 09:25:13 +01:00
|
|
|
'cancelButton': 'Cancel',
|
|
|
|
'okButton': 'ok',
|
|
|
|
// Required to get audio in background when using Android 11
|
|
|
|
'foregroundService': {
|
2022-09-10 12:12:52 +02:00
|
|
|
'channelId': 'com.fluffy.fluffychat',
|
2022-02-15 09:25:13 +01:00
|
|
|
'channelName': 'Foreground service for my app',
|
|
|
|
'notificationTitle': '$appName is running on background',
|
|
|
|
'notificationIcon': 'mipmap/ic_notification_launcher',
|
|
|
|
},
|
2022-09-10 12:12:52 +02:00
|
|
|
'additionalPermissions': [''],
|
2022-02-15 09:25:13 +01:00
|
|
|
};
|
2022-09-10 12:12:52 +02:00
|
|
|
bool setupDone = false;
|
|
|
|
Future<void> showCallkitIncoming(CallSession call) async {
|
|
|
|
if (!setupDone) {
|
2022-02-15 09:25:13 +01:00
|
|
|
await _callKeep.setup(
|
|
|
|
null,
|
|
|
|
<String, dynamic>{
|
|
|
|
'ios': <String, dynamic>{
|
|
|
|
'appName': appName,
|
|
|
|
},
|
|
|
|
'android': alertOptions,
|
|
|
|
},
|
|
|
|
backgroundMode: true);
|
2022-09-10 12:12:52 +02:00
|
|
|
}
|
|
|
|
setupDone = true;
|
|
|
|
await displayIncomingCall(call);
|
|
|
|
call.onCallStateChanged.stream.listen((state) {
|
|
|
|
if (state == CallState.kEnded) {
|
|
|
|
_callKeep.endAllCalls();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
call.onCallEventChanged.stream.listen(
|
|
|
|
(event) {
|
2022-02-15 09:25:13 +01:00
|
|
|
if (event == CallEvent.kLocalHoldUnhold) {
|
|
|
|
Logs().i(
|
|
|
|
'Call hold event: local ${call.localHold}, remote ${call.remoteOnHold}');
|
|
|
|
}
|
2022-09-10 12:12:52 +02:00
|
|
|
},
|
|
|
|
);
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 12:12:52 +02:00
|
|
|
void removeCall(String? callUUID) {
|
2022-02-15 09:25:13 +01:00
|
|
|
calls.remove(callUUID);
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:12:52 +02:00
|
|
|
void addCall(String? callUUID, CallKeeper callKeeper) {
|
|
|
|
if (calls.containsKey(callUUID)) return;
|
2022-02-15 09:25:13 +01:00
|
|
|
calls[callUUID] = callKeeper;
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:12:52 +02:00
|
|
|
void setCallHeld(String? callUUID, bool? held) {
|
2022-02-15 09:25:13 +01:00
|
|
|
calls[callUUID]!.held = held;
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:12:52 +02:00
|
|
|
void setCallMuted(String? callUUID, bool? muted) {
|
2022-02-15 09:25:13 +01:00
|
|
|
calls[callUUID]!.muted = muted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void didDisplayIncomingCall(CallKeepDidDisplayIncomingCall event) {
|
|
|
|
final callUUID = event.callUUID;
|
|
|
|
final number = event.handle;
|
|
|
|
Logs().v('[displayIncomingCall] $callUUID number: $number');
|
2022-09-10 12:12:52 +02:00
|
|
|
// addCall(callUUID, CallKeeper(this null));
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void onPushKitToken(CallKeepPushKitToken event) {
|
|
|
|
Logs().v('[onPushKitToken] token => ${event.token}');
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> initialize() async {
|
|
|
|
_callKeep.on(CallKeepPerformAnswerCallAction(), answerCall);
|
|
|
|
_callKeep.on(CallKeepDidPerformDTMFAction(), didPerformDTMFAction);
|
|
|
|
_callKeep.on(
|
|
|
|
CallKeepDidReceiveStartCallAction(), didReceiveStartCallAction);
|
|
|
|
_callKeep.on(CallKeepDidToggleHoldAction(), didToggleHoldCallAction);
|
|
|
|
_callKeep.on(
|
|
|
|
CallKeepDidPerformSetMutedCallAction(), didPerformSetMutedCallAction);
|
|
|
|
_callKeep.on(CallKeepPerformEndCallAction(), endCall);
|
|
|
|
_callKeep.on(CallKeepPushKitToken(), onPushKitToken);
|
|
|
|
_callKeep.on(CallKeepDidDisplayIncomingCall(), didDisplayIncomingCall);
|
2022-09-10 12:12:52 +02:00
|
|
|
Logs().i('[VOIP] Initialized');
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> hangup(String callUUID) async {
|
|
|
|
await _callKeep.endCall(callUUID);
|
|
|
|
removeCall(callUUID);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> reject(String callUUID) async {
|
|
|
|
await _callKeep.rejectCall(callUUID);
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:12:52 +02:00
|
|
|
Future<void> answer(String? callUUID) async {
|
|
|
|
final keeper = calls[callUUID]!;
|
|
|
|
if (!keeper.connected) {
|
|
|
|
await _callKeep.answerIncomingCall(callUUID!);
|
2022-02-15 09:25:13 +01:00
|
|
|
keeper.connected = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setOnHold(String callUUID, bool held) async {
|
|
|
|
await _callKeep.setOnHold(callUUID, held);
|
|
|
|
setCallHeld(callUUID, held);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> setMutedCall(String callUUID, bool muted) async {
|
|
|
|
await _callKeep.setMutedCall(callUUID, muted);
|
|
|
|
setCallMuted(callUUID, muted);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateDisplay(String callUUID) async {
|
|
|
|
// Workaround because Android doesn't display well displayName, se we have to switch ...
|
|
|
|
if (isIOS) {
|
|
|
|
await _callKeep.updateDisplay(callUUID,
|
2022-09-10 12:12:52 +02:00
|
|
|
displayName: 'New Name', handle: callUUID);
|
2022-02-15 09:25:13 +01:00
|
|
|
} else {
|
|
|
|
await _callKeep.updateDisplay(callUUID,
|
2022-09-10 12:12:52 +02:00
|
|
|
displayName: callUUID, handle: 'New Name');
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<CallKeeper> displayIncomingCall(CallSession call) async {
|
2022-09-10 12:12:52 +02:00
|
|
|
final callKeeper = CallKeeper(this, call);
|
|
|
|
addCall(call.callId, callKeeper);
|
|
|
|
await _callKeep.displayIncomingCall(
|
|
|
|
call.callId,
|
|
|
|
'${call.displayName!} (FluffyChat)',
|
|
|
|
localizedCallerName: '${call.displayName!} (FluffyChat)',
|
|
|
|
handleType: 'number',
|
|
|
|
hasVideo: call.type == CallType.kVideo,
|
|
|
|
);
|
2022-02-15 09:25:13 +01:00
|
|
|
return callKeeper;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> checkoutPhoneAccountSetting(BuildContext context) async {
|
2022-09-10 12:12:52 +02:00
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
barrierDismissible: true,
|
|
|
|
useRootNavigator: false,
|
|
|
|
builder: (_) => AlertDialog(
|
|
|
|
title: Text(L10n.of(context)!.callingPermissions),
|
|
|
|
content: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
ListTile(
|
|
|
|
onTap: () => openCallingAccountsPage(context),
|
|
|
|
title: Text(L10n.of(context)!.callingAccount),
|
|
|
|
subtitle: Text(L10n.of(context)!.callingAccountDetails),
|
|
|
|
trailing: const Icon(Icons.phone),
|
|
|
|
),
|
|
|
|
const Divider(),
|
|
|
|
ListTile(
|
|
|
|
onTap: () =>
|
|
|
|
FlutterForegroundTask.openSystemAlertWindowSettings(true),
|
|
|
|
title: Text(L10n.of(context)!.appearOnTop),
|
|
|
|
subtitle: Text(L10n.of(context)!.appearOnTopDetails),
|
|
|
|
trailing: const Icon(Icons.file_upload_rounded),
|
|
|
|
),
|
|
|
|
const Divider(),
|
|
|
|
ListTile(
|
|
|
|
onTap: () => openAppSettings(),
|
|
|
|
title: Text(L10n.of(context)!.otherCallingPermissions),
|
|
|
|
trailing: const Icon(Icons.mic),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void openCallingAccountsPage(BuildContext context) async {
|
2022-02-15 09:25:13 +01:00
|
|
|
await _callKeep.setup(context, <String, dynamic>{
|
|
|
|
'ios': <String, dynamic>{
|
|
|
|
'appName': appName,
|
|
|
|
},
|
|
|
|
'android': alertOptions,
|
|
|
|
});
|
|
|
|
final hasPhoneAccount = await _callKeep.hasPhoneAccount();
|
2022-09-10 12:12:52 +02:00
|
|
|
Logs().e(hasPhoneAccount.toString());
|
2022-02-15 09:25:13 +01:00
|
|
|
if (!hasPhoneAccount) {
|
|
|
|
await _callKeep.hasDefaultPhoneAccount(context, alertOptions);
|
2022-09-10 12:12:52 +02:00
|
|
|
} else {
|
|
|
|
await _callKeep.openPhoneAccounts();
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CallActions.
|
|
|
|
Future<void> answerCall(CallKeepPerformAnswerCallAction event) async {
|
|
|
|
final callUUID = event.callUUID;
|
|
|
|
final keeper = calls[event.callUUID]!;
|
|
|
|
if (!keeper.connected) {
|
2022-09-10 12:12:52 +02:00
|
|
|
Logs().e('answered');
|
2022-02-15 09:25:13 +01:00
|
|
|
// Answer Call
|
2022-09-10 12:12:52 +02:00
|
|
|
keeper.call.answer();
|
2022-02-15 09:25:13 +01:00
|
|
|
keeper.connected = true;
|
|
|
|
}
|
|
|
|
Timer(const Duration(seconds: 1), () {
|
|
|
|
_callKeep.setCurrentCallActive(callUUID!);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> endCall(CallKeepPerformEndCallAction event) async {
|
|
|
|
final keeper = calls[event.callUUID];
|
2022-09-10 12:12:52 +02:00
|
|
|
keeper?.call.hangup();
|
|
|
|
removeCall(event.callUUID);
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> didPerformDTMFAction(CallKeepDidPerformDTMFAction event) async {
|
|
|
|
final keeper = calls[event.callUUID]!;
|
2022-09-10 12:12:52 +02:00
|
|
|
keeper.call.sendDTMF(event.digits!);
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> didReceiveStartCallAction(
|
|
|
|
CallKeepDidReceiveStartCallAction event) async {
|
|
|
|
if (event.handle == null) {
|
|
|
|
// @TODO: sometime we receive `didReceiveStartCallAction` with handle` undefined`
|
|
|
|
return;
|
|
|
|
}
|
2022-09-10 12:12:52 +02:00
|
|
|
final callUUID = event.callUUID!;
|
2022-02-15 09:25:13 +01:00
|
|
|
if (event.callUUID == null) {
|
|
|
|
final call =
|
|
|
|
await _voipPlugin!.voip.inviteToCall(event.handle!, CallType.kVideo);
|
2022-09-10 12:12:52 +02:00
|
|
|
addCall(callUUID, CallKeeper(this, call));
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
await _callKeep.startCall(callUUID, event.handle!, event.handle!);
|
|
|
|
Timer(const Duration(seconds: 1), () {
|
|
|
|
_callKeep.setCurrentCallActive(callUUID);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> didPerformSetMutedCallAction(
|
|
|
|
CallKeepDidPerformSetMutedCallAction event) async {
|
2022-09-10 12:12:52 +02:00
|
|
|
final keeper = calls[event.callUUID];
|
|
|
|
if (event.muted!) {
|
|
|
|
keeper!.call.setMicrophoneMuted(true);
|
2022-02-15 09:25:13 +01:00
|
|
|
} else {
|
2022-09-10 12:12:52 +02:00
|
|
|
keeper!.call.setMicrophoneMuted(false);
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
2022-09-10 12:12:52 +02:00
|
|
|
setCallMuted(event.callUUID, event.muted);
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> didToggleHoldCallAction(
|
|
|
|
CallKeepDidToggleHoldAction event) async {
|
2022-09-10 12:12:52 +02:00
|
|
|
final keeper = calls[event.callUUID];
|
|
|
|
if (event.hold!) {
|
|
|
|
keeper!.call.setRemoteOnHold(true);
|
2022-02-15 09:25:13 +01:00
|
|
|
} else {
|
2022-09-10 12:12:52 +02:00
|
|
|
keeper!.call.setRemoteOnHold(false);
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
2022-09-10 12:12:52 +02:00
|
|
|
setCallHeld(event.callUUID, event.hold);
|
2022-02-15 09:25:13 +01:00
|
|
|
}
|
|
|
|
}
|