-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwebtrit_callkeep_android.dart
More file actions
519 lines (428 loc) · 16.7 KB
/
Copy pathwebtrit_callkeep_android.dart
File metadata and controls
519 lines (428 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import 'dart:async';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:webtrit_callkeep_android/src/common/common.dart';
import 'package:webtrit_callkeep_platform_interface/webtrit_callkeep_platform_interface.dart';
/// The Android implementation of [WebtritCallkeepPlatform].
class WebtritCallkeepAndroid extends WebtritCallkeepPlatform {
/// Registers this class as the default instance of [WebtritCallkeepPlatform].
static void registerWith() {
WebtritCallkeepPlatform.instance = WebtritCallkeepAndroid();
}
// APIs for initializing the background service isolates to be used in the main isolate.
final _backgroundPushNotificationIsolateBootstrapApi = PHostBackgroundPushNotificationIsolateBootstrapApi();
// APIs for working with the background service isolates.
final _backgroundPushNotificationIsolateApi = PHostBackgroundPushNotificationIsolateApi();
final _pHostSmsReceptionApi = PHostSmsReceptionConfigApi();
final _pushRegistryApi = PPushRegistryHostApi();
final _api = PHostApi();
final _soundApi = PHostSoundApi();
final _connectionsApi = PHostConnectionsApi();
final _activityControlApi = PHostActivityControlApi();
int? _pushNotificationIsolatePluginCallbackHandle;
int? _onPushNotificationNotificationSync;
final _permissionsApi = PHostPermissionsApi();
final _diagnosticsApi = PHostDiagnosticsApi();
@override
void setDelegate(CallkeepDelegate? delegate) {
if (delegate != null) {
PDelegateFlutterApi.setUp(_CallkeepDelegateRelay(delegate));
} else {
PDelegateFlutterApi.setUp(null);
}
// Notify the Pigeon API that the delegate has been set.
_api.onDelegateSet();
}
@override
void setPushRegistryDelegate(PushRegistryDelegate? delegate) {
if (delegate != null) {
PPushRegistryDelegateFlutterApi.setUp(_PushRegistryDelegateRelay(delegate));
} else {
PPushRegistryDelegateFlutterApi.setUp(null);
}
}
@override
Future<String?> pushTokenForPushTypeVoIP() {
return _pushRegistryApi.pushTokenForPushTypeVoIP();
}
@override
Future<bool> isSetUp() {
return _api.isSetUp();
}
@override
Future<void> setUp(CallkeepOptions options) {
return _api.setUp(options.toPigeon());
}
@override
Future<void> tearDown() {
return _api.tearDown();
}
@override
Future<CallkeepIncomingCallError?> reportNewIncomingCall(
String callId,
CallkeepHandle handle,
String? displayName,
bool hasVideo,
) {
return _api
.reportNewIncomingCall(callId, handle.toPigeon(), displayName, hasVideo)
.then((value) => value?.value.toCallkeep());
}
@override
Future<void> reportConnectingOutgoingCall(String callId) {
return _api.reportConnectingOutgoingCall(callId);
}
@override
Future<void> reportConnectedOutgoingCall(String callId) {
return _api.reportConnectedOutgoingCall(callId);
}
@override
Future<void> reportUpdateCall(
String callId,
CallkeepHandle? handle,
String? displayName,
bool? hasVideo,
bool? proximityEnabled,
) {
return _api.reportUpdateCall(callId, handle?.toPigeon(), displayName, hasVideo, proximityEnabled);
}
@override
Future<void> reportEndCall(String callId, String displayName, CallkeepEndCallReason reason) {
return _api.reportEndCall(callId, displayName, PEndCallReason(value: reason.toPigeon()));
}
@override
Future<CallkeepCallRequestError?> startCall(
String callId,
CallkeepHandle handle,
String? displayNameOrContactIdentifier,
bool video,
bool proximityEnabled,
) {
return _api
.startCall(callId, handle.toPigeon(), displayNameOrContactIdentifier, video, proximityEnabled)
.then((value) => value?.value.toCallkeep());
}
@override
Future<CallkeepCallRequestError?> answerCall(String callId) {
return _api.answerCall(callId).then((value) => value?.value.toCallkeep());
}
@override
Future<CallkeepCallRequestError?> endCall(String callId) {
return _api.endCall(callId).then((value) => value?.value.toCallkeep());
}
@override
Future<CallkeepCallRequestError?> setHeld(String callId, bool onHold) {
return _api.setHeld(callId, onHold).then((value) => value?.value.toCallkeep());
}
@override
Future<CallkeepCallRequestError?> setMuted(String callId, bool muted) {
return _api.setMuted(callId, muted).then((value) => value?.value.toCallkeep());
}
@override
Future<CallkeepCallRequestError?> setSpeaker(String callId, bool enabled) {
return _api.setSpeaker(callId, enabled).then((value) => value?.value.toCallkeep());
}
@override
Future<CallkeepCallRequestError?> setAudioDevice(String callId, CallkeepAudioDevice device) {
return _api
.setAudioDevice(
callId,
PAudioDevice(type: PAudioDeviceType.values.byName(device.type.name), id: device.id, name: device.name),
)
.then((value) => value?.value.toCallkeep());
}
@override
Future<CallkeepCallRequestError?> sendDTMF(String callId, String key) {
return _api.sendDTMF(callId, key).then((value) => value?.value.toCallkeep());
}
@override
void setBackgroundServiceDelegate(CallkeepBackgroundServiceDelegate? delegate) {
if (delegate != null) {
PDelegateBackgroundServiceFlutterApi.setUp(_CallkeepBackgroundServiceDelegateRelay(delegate));
} else {
PDelegateBackgroundServiceFlutterApi.setUp(null);
}
}
@override
Future<CallkeepSpecialPermissionStatus> getFullScreenIntentPermissionStatus() {
return _permissionsApi.getFullScreenIntentPermissionStatus().then((value) => value.toCallkeep());
}
@override
Future<void> openFullScreenIntentSettings() {
return _permissionsApi.openFullScreenIntentSettings();
}
@override
Future<void> openSettings() {
return _permissionsApi.openSettings();
}
@override
Future<CallkeepAndroidBatteryMode> getBatteryMode() {
return _permissionsApi.getBatteryMode().then((value) => value.toCallkeep());
}
@override
Future<CallkeepAndroidCallDeliveryMode> getCallDeliveryMode() {
return _permissionsApi.getCallDeliveryMode().then((value) => value.toCallkeep());
}
@override
Future<Map<String, dynamic>> getDiagnosticReport() async {
final rawData = await _diagnosticsApi.getDiagnosticReport();
return rawData.cast<String, dynamic>();
}
@override
Future<Map<CallkeepPermission, CallkeepSpecialPermissionStatus>> requestPermissions(
List<CallkeepPermission> permissions,
) async {
final pigeonList = permissions.map((e) => e.toPigeon()).toList();
final results = await _permissionsApi.requestPermissions(pigeonList);
return {for (final result in results) result.permission.toCallkeep(): result.status.toCallkeep()};
}
@override
Future<Map<CallkeepPermission, CallkeepSpecialPermissionStatus>> checkPermissionsStatus(
List<CallkeepPermission> permissions,
) async {
final pigeonList = permissions.map((e) => e.toPigeon()).toList();
final results = await _permissionsApi.checkPermissionsStatus(pigeonList);
return {for (final result in results) result.permission.toCallkeep(): result.status.toCallkeep()};
}
@override
Future<void> playRingbackSound() {
return _soundApi.playRingbackSound();
}
@override
Future<void> stopRingbackSound() {
return _soundApi.stopRingbackSound();
}
@override
Future<CallkeepConnection?> getConnection(String callId) async {
return _connectionsApi.getConnection(callId).then((value) => value?.toCallkeep());
}
@override
Future<void> cleanConnections() {
return _connectionsApi.cleanConnections();
}
@override
Future<List<CallkeepConnection>> getConnections() async {
return _connectionsApi.getConnections().then((value) => value.map((it) => it.toCallkeep()).toList());
}
// Android background push notification service
// ------------------------------------------------------------------------------------------------
@override
Future<void> initializePushNotificationCallback(CallKeepPushNotificationSyncStatusHandle onSync) async {
// Initialization callback handle for the isolate plugin only once;
_pushNotificationIsolatePluginCallbackHandle =
_pushNotificationIsolatePluginCallbackHandle ??
PluginUtilities.getCallbackHandle(_isolatePluginCallbackDispatcher)?.toRawHandle();
_onPushNotificationNotificationSync =
_onPushNotificationNotificationSync ?? PluginUtilities.getCallbackHandle(onSync)?.toRawHandle();
if (_pushNotificationIsolatePluginCallbackHandle != null && _onPushNotificationNotificationSync != null) {
await _backgroundPushNotificationIsolateBootstrapApi.initializePushNotificationCallback(
callbackDispatcher: _pushNotificationIsolatePluginCallbackHandle!,
onNotificationSync: _onPushNotificationNotificationSync!,
);
}
}
@override
Future<CallkeepIncomingCallError?> incomingCallPushNotificationService(
String callId,
CallkeepHandle handle,
String? displayName,
bool hasVideo,
) {
return _backgroundPushNotificationIsolateBootstrapApi
.reportNewIncomingCall(callId, handle.toPigeon(), displayName, hasVideo)
.then((value) => value?.value.toCallkeep());
}
@override
Future<dynamic> endCallsBackgroundPushNotificationService() {
return _backgroundPushNotificationIsolateApi.endAllCalls();
}
@override
Future<dynamic> endCallBackgroundPushNotificationService(String callId) {
return _backgroundPushNotificationIsolateApi.endCall(callId);
}
@override
Future<dynamic> releaseCallBackgroundPushNotificationService(String callId) {
return _backgroundPushNotificationIsolateApi.releaseCall(callId);
}
@override
Future<dynamic> handoffCallBackgroundPushNotificationService(String callId) {
return _backgroundPushNotificationIsolateApi.handoffCall(callId);
}
// ------------------------------------------------------------------------------------------------
// Android background push notification service
// ------------------------------------------------------------------------------------------------
// Android SMS reception section
// ------------------------------------------------------------------------------------------------
/// Initializes SMS reception with the given prefix used to filter messages.
///
/// Only messages that begin with [messagePrefix] will be delivered to the Dart handler.
/// Initializes SMS reception with the given prefix and regex pattern used to filter and parse messages.
///
/// Only messages that begin with [messagePrefix] and match [regexPattern] will be delivered and parsed.
@override
Future<void> initializeSmsReception({
/// Prefix to match incoming SMS messages (e.g. "<#> WEBTRIT:")
required String messagePrefix,
/// Regex pattern with 4 capture groups: callId, handle, displayName, hasVideo
required String regexPattern,
}) {
return _pHostSmsReceptionApi.initializeSmsReception(messagePrefix: messagePrefix, regexPattern: regexPattern);
}
// ------------------------------------------------------------------------------------------------
// Android Activity Control section
// ------------------------------------------------------------------------------------------------
/// Allows the app's activity to be shown over the device lock screen.
///
/// This is an Android-only feature.
@override
Future<void> showOverLockscreen([bool enable = true]) {
return _activityControlApi.showOverLockscreen(enable);
}
/// Turns the screen on when the app's window is shown.
///
/// Typically used in conjunction with [showOverLockscreen].
/// This is an Android-only feature.
@override
Future<void> wakeScreenOnShow([bool enable = true]) {
return _activityControlApi.wakeScreenOnShow(enable);
}
/// Moves the entire task (app) to the background.
///
/// This is an Android-only feature.
/// Returns `true` if successful.
@override
Future<bool> sendToBackground() {
return _activityControlApi.sendToBackground();
}
/// Checks if the device screen is currently locked (keyguard is active).
///
/// Returns `false` on non-Android platforms.
@override
Future<bool> isDeviceLocked() {
return _activityControlApi.isDeviceLocked();
}
}
class _CallkeepDelegateRelay implements PDelegateFlutterApi {
const _CallkeepDelegateRelay(this._delegate);
final CallkeepDelegate _delegate;
@override
void continueStartCallIntent(PHandle handle, String? displayName, bool video) {
_delegate.continueStartCallIntent(handle.toCallkeep(), displayName, video);
}
@override
void didPushIncomingCall(PHandle handle, String? displayName, bool video, String callId, PIncomingCallError? error) {
_delegate.didPushIncomingCall(handle.toCallkeep(), displayName, video, callId, error?.value.toCallkeep());
}
@override
Future<bool> performStartCall(String callId, PHandle handle, String? displayNameOrContactIdentifier, bool video) {
return _delegate.performStartCall(callId, handle.toCallkeep(), displayNameOrContactIdentifier, video);
}
@override
Future<bool> performAnswerCall(String callId) {
return _delegate.performAnswerCall(callId);
}
@override
Future<bool> performEndCall(String callId) {
return _delegate.performEndCall(callId);
}
@override
Future<bool> performSetHeld(String callId, bool onHold) {
return _delegate.performSetHeld(callId, onHold);
}
@override
Future<bool> performSetMuted(String callId, bool muted) {
return _delegate.performSetMuted(callId, muted);
}
@override
Future<bool> performSendDTMF(String callId, String key) {
return _delegate.performSendDTMF(callId, key);
}
@override
Future<bool> performAudioDeviceSet(String callId, PAudioDevice device) {
return _delegate.performAudioDeviceSet(
callId,
CallkeepAudioDevice(
type: CallkeepAudioDeviceType.values.byName(device.type.name),
id: device.id,
name: device.name,
),
);
}
@override
Future<bool> performAudioDevicesUpdate(String callId, List<PAudioDevice> devices) {
return _delegate.performAudioDevicesUpdate(
callId,
devices.map((device) {
return CallkeepAudioDevice(
type: CallkeepAudioDeviceType.values.byName(device.type.name),
id: device.id,
name: device.name,
);
}).toList(),
);
}
@override
void didActivateAudioSession() {
_delegate.didActivateAudioSession();
}
@override
void didDeactivateAudioSession() {
_delegate.didDeactivateAudioSession();
}
@override
void didReset() {
_delegate.didReset();
}
}
class _PushRegistryDelegateRelay implements PPushRegistryDelegateFlutterApi {
const _PushRegistryDelegateRelay(this._delegate);
final PushRegistryDelegate _delegate;
@override
void didUpdatePushTokenForPushTypeVoIP(String? token) {
_delegate.didUpdatePushTokenForPushTypeVoIP(token);
}
}
class _CallkeepBackgroundServiceDelegateRelay implements PDelegateBackgroundServiceFlutterApi {
const _CallkeepBackgroundServiceDelegateRelay(this._delegate);
final CallkeepBackgroundServiceDelegate _delegate;
@override
Future<void> performEndCall(String callId) async {
return _delegate.performEndCall(callId);
}
@override
Future<void> performAnswerCall(String callId) async {
return _delegate.performAnswerCall(callId);
}
}
@pragma('vm:entry-point')
void _isolatePluginCallbackDispatcher() {
// Initialize the Flutter framework necessary for method channels and Pigeon.
WidgetsFlutterBinding.ensureInitialized();
// WebtritCallkeepAndroid().wakeUpBackgroundHandler();
// Set up the Pigeon API for the background service.
PDelegateBackgroundRegisterFlutterApi.setUp(_BackgroundServiceDelegate());
}
class _BackgroundServiceDelegate implements PDelegateBackgroundRegisterFlutterApi {
@override
Future<void> onWakeUpBackgroundHandler(
int userCallbackHandle,
PCallkeepServiceStatus status,
PCallkeepIncomingCallData? callData,
) async {
final handle = CallbackHandle.fromRawHandle(userCallbackHandle);
final closure = PluginUtilities.getCallbackFromHandle(handle)! as ForegroundStartServiceHandle;
await closure(status.toCallkeep(), callData?.toCallkeep());
}
@override
Future<void> onApplicationStatusChanged(int applicationStatusCallbackHandle, PCallkeepServiceStatus status) async {
final handle = CallbackHandle.fromRawHandle(applicationStatusCallbackHandle);
final closure = PluginUtilities.getCallbackFromHandle(handle)! as ForegroundChangeLifecycleHandle;
await closure(status.toCallkeep());
}
@override
Future<void> onNotificationSync(int pushNotificationSyncStatusHandle, PCallkeepIncomingCallData? callData) async {
final handle = CallbackHandle.fromRawHandle(pushNotificationSyncStatusHandle);
final closure = PluginUtilities.getCallbackFromHandle(handle)! as CallKeepPushNotificationSyncStatusHandle;
await closure(callData?.toCallkeep());
}
}