Skip to content

Commit

Permalink
feat: Add backgroundMode for setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Sep 27, 2021
1 parent dd1c872 commit 5abd43e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
48 changes: 27 additions & 21 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
(CallKeepPerformAnswerCallAction event) {
print(
'backgroundMessage: CallKeepPerformAnswerCallAction ${event.callUUID}');
_callKeep.startCall(event.callUUID, callerId, callerNmae);

Timer(const Duration(seconds: 1), () {
print(
'[setCurrentCallActive] $callUUID, callerId: $callerId, callerName: $callerNmae');
Expand All @@ -54,24 +52,28 @@ Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
print('backgroundMessage: CallKeepPerformEndCallAction ${event.callUUID}');
});
if (!_callKeepInited) {
_callKeep.setup(null, <String, dynamic>{
'ios': {
'appName': 'CallKeepDemo',
},
'android': {
'alertTitle': 'Permissions required',
'alertDescription':
'This application needs to access your phone accounts',
'cancelButton': 'Cancel',
'okButton': 'ok',
'foregroundService': {
'channelId': 'com.company.my',
'channelName': 'Foreground service for my app',
'notificationTitle': 'My app is running on background',
'notificationIcon': 'Path to the resource icon of the notification',
_callKeep.setup(
null,
<String, dynamic>{
'ios': {
'appName': 'CallKeepDemo',
},
'android': {
'alertTitle': 'Permissions required',
'alertDescription':
'This application needs to access your phone accounts',
'cancelButton': 'Cancel',
'okButton': 'ok',
'foregroundService': {
'channelId': 'com.company.my',
'channelName': 'Foreground service for my app',
'notificationTitle': 'My app is running on background',
'notificationIcon':
'Path to the resource icon of the notification',
},
},
},
},
});
backgroundMode: true);
_callKeepInited = true;
}

Expand Down Expand Up @@ -161,8 +163,6 @@ class _MyAppState extends State<HomePage> {
final String callUUID = event.callUUID;
final String number = calls[callUUID].number;
print('[answerCall] $callUUID, number: $number');

_callKeep.startCall(event.callUUID, number, number);
Timer(const Duration(seconds: 1), () {
print('[setCurrentCallActive] $callUUID, number: $number');
_callKeep.setCurrentCallActive(callUUID);
Expand Down Expand Up @@ -320,6 +320,12 @@ class _MyAppState extends State<HomePage> {
'This application needs to access your phone accounts',
'cancelButton': 'Cancel',
'okButton': 'ok',
'foregroundService': {
'channelId': 'com.company.my',
'channelName': 'Foreground service for my app',
'notificationTitle': 'My app is running on background',
'notificationIcon': 'Path to the resource icon of the notification',
},
},
});

Expand Down
21 changes: 13 additions & 8 deletions lib/src/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ class FlutterCallkeep extends EventManager {
static const MethodChannel _event = MethodChannel('FlutterCallKeep.Event');
BuildContext? _context;

Future<void> setup(
BuildContext? context, Map<String, dynamic> options) async {
Future<void> setup(BuildContext? context, Map<String, dynamic> options,
{bool backgroundMode = false}) async {
_context = context;
if (!isIOS) {
await _setupAndroid(options['android'] as Map<String, dynamic>);
await _setupAndroid(
options['android'] as Map<String, dynamic>, backgroundMode);
return;
}
await _setupIOS(options['ios'] as Map<String, dynamic>);
Expand Down Expand Up @@ -297,8 +298,14 @@ class FlutterCallkeep extends EventManager {
.invokeMethod<void>('setup', <String, dynamic>{'options': options});
}

Future<bool> _setupAndroid(Map<String, dynamic> options) async {
Future<bool> _setupAndroid(
Map<String, dynamic> options, bool backgroundMode) async {
await _channel.invokeMethod<void>('setup', {'options': options});

if (backgroundMode) {
return true;
}

final showAccountAlert = await _checkPhoneAccountPermission(
options['additionalPermissions'] as List<String>);
final shouldOpenAccounts = await _alert(options, showAccountAlert);
Expand All @@ -310,9 +317,7 @@ class FlutterCallkeep extends EventManager {
return false;
}

Future<void> openPhoneAccounts() async {
_openPhoneAccounts();
}
Future<void> openPhoneAccounts() => _openPhoneAccounts();

Future<void> _openPhoneAccounts() async {
if (!Platform.isAndroid) {
Expand Down Expand Up @@ -405,7 +410,7 @@ class FlutterCallkeep extends EventManager {
emit(CallKeepDidActivateAudioSession());
break;
case 'CallKeepDidDeactivateAudioSession':
emit(CallKeepDidActivateAudioSession());
emit(CallKeepDidDeactivateAudioSession());
break;
case 'CallKeepDidDisplayIncomingCall':
emit(CallKeepDidDisplayIncomingCall.fromMap(data));
Expand Down

0 comments on commit 5abd43e

Please sign in to comment.