Skip to content

Commit b684cc3

Browse files
Update timeout callkit, stringee call model
1 parent 9f0643b commit b684cc3

4 files changed

Lines changed: 91 additions & 16 deletions

File tree

call_sample/lib/stringee_wrapper/call/stringee_call_model.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class StringeeCallModel extends ChangeNotifier {
6464
Timer? _timer;
6565
bool _startedTimer = false;
6666

67+
Timer? _callTimeOutTimer;
68+
6769
/// call time
6870
String _time = '00:00';
6971
String get time => _time;
@@ -85,6 +87,9 @@ class StringeeCallModel extends ChangeNotifier {
8587
_isClickedAnswer = value;
8688
}
8789

90+
// flag to check media state is connected
91+
bool _mediaStateDidConnected = false;
92+
8893
/// check to end call by reject or hangup
8994
bool get isShouldReject => isIncomingCall && !_isClickedAnswer;
9095

@@ -104,6 +109,14 @@ class StringeeCallModel extends ChangeNotifier {
104109
// if (!isIncomingCall) {
105110
// makeCall();
106111
// }
112+
113+
// start time out
114+
_callTimeOutTimer =
115+
Timer(Duration(seconds: StringeeWrapper().callTimeout), () {
116+
if (!_mediaStateDidConnected) {
117+
_endCall();
118+
}
119+
});
107120
}
108121

109122
_handleStringeeCallEvent(Map<dynamic, dynamic> event) async {
@@ -203,6 +216,10 @@ class StringeeCallModel extends ChangeNotifier {
203216

204217
void _handleMediaStateChangeEvent(StringeeMediaState state) {
205218
_mediaState = state;
219+
if (state == StringeeMediaState.connected) {
220+
_mediaStateDidConnected = true;
221+
_callTimeOutTimer?.cancel();
222+
}
206223
notifyListeners();
207224
}
208225

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'dart:async';
2+
3+
import '../call/stringee_call_model.dart';
4+
import 'callkeep_manager.dart';
5+
6+
class CallKitModel {
7+
// call uuid handled by callkit
8+
String? uuid;
9+
// call id of the stringee call
10+
String? callId;
11+
// serial of the stringee call
12+
int? serial;
13+
// stringee call model
14+
StringeeCallModel? callModel;
15+
16+
final int timeout;
17+
18+
/// [CallKitModel] constructor
19+
/// all params do not required
20+
/// if have [uuid] and do not have [callModel], start timeout to end call
21+
CallKitModel({
22+
this.uuid,
23+
this.callId,
24+
this.serial,
25+
this.timeout = 8,
26+
this.callModel,
27+
}) {
28+
_count = timeout;
29+
30+
if (callModel == null && uuid != null) {
31+
_startCountTimeout();
32+
}
33+
}
34+
35+
Timer? _timer;
36+
int _count = 0;
37+
int get count => _count;
38+
39+
_startCountTimeout() {
40+
_timer?.cancel();
41+
_timer = Timer(Duration(seconds: timeout), () {
42+
if (uuid != null) {
43+
CallkeepManager().callkeep.endCall(uuid!);
44+
}
45+
});
46+
}
47+
48+
stopCountTimeout() {
49+
_timer?.cancel();
50+
}
51+
}

call_sample/lib/stringee_wrapper/push_manager/callkeep_manager.dart

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
1+
import 'dart:async';
2+
13
import 'package:call_sample/stringee_wrapper/call/stringee_call_manager.dart';
24
import 'package:call_sample/stringee_wrapper/common/result.dart';
35
import 'package:callkeep/callkeep.dart';
46
import 'package:flutter/foundation.dart';
57
import 'package:uuid/uuid.dart';
68

79
import '../call/stringee_call_model.dart';
8-
9-
class CallKitModel {
10-
// call uuid handled by callkit
11-
String? uuid;
12-
// call id of the stringee call
13-
String? callId;
14-
// serial of the stringee call
15-
int? serial;
16-
17-
CallKitModel({
18-
this.uuid,
19-
this.callId,
20-
this.serial,
21-
});
22-
}
10+
import 'call_kit_model.dart';
2311

2412
class CallkeepManager {
2513
static final CallkeepManager _instance = CallkeepManager._internal();
@@ -36,11 +24,12 @@ class CallkeepManager {
3624
bool get isActiveAudio => _isActiveAudio;
3725

3826
// current call uuid handled by callkit
27+
// to handle multi call change to list if needed
3928
CallKitModel _currentCallKit = CallKitModel();
4029

4130
/// list of handled call uuids (handled by callkit) but not found in stringee calls
4231
/// end/answer call in callkit but not have stringee call to handle
43-
// final List<CallKitModel> _handledCallUuids = [];
32+
final List<CallKitModel> _handledCallUuids = [];
4433

4534
String _pushToken = '';
4635
String get pushToken => _pushToken;
@@ -61,6 +50,7 @@ class CallkeepManager {
6150
uuid: uuid,
6251
callId: stringeeCallModel.call.callId,
6352
serial: stringeeCallModel.call.serial,
53+
callModel: stringeeCallModel,
6454
);
6555
debugPrint(
6656
'reportOutgoingCallIfNeeded uuid: $uuid ${stringeeCallModel.call.to} ${stringeeCallModel.call.toAlias}');
@@ -79,6 +69,7 @@ class CallkeepManager {
7969
uuid: uuid,
8070
callId: stringeeCallModel.call.callId,
8171
serial: stringeeCallModel.call.serial,
72+
callModel: stringeeCallModel,
8273
);
8374
await callkeep.displayIncomingCall(
8475
uuid,
@@ -93,6 +84,17 @@ class CallkeepManager {
9384
// call already handled from pushkit
9485
// set current uuid to call model
9586
stringeeCallModel.setUuid(_currentCallKit.uuid!);
87+
_currentCallKit.callModel = stringeeCallModel;
88+
_currentCallKit.stopCountTimeout();
89+
90+
// check if call have answer in callkit before if needed
91+
if (_handledCallUuids
92+
.where((element) => element.uuid == _currentCallKit.uuid)
93+
.isNotEmpty) {
94+
StringeeCallManager().answerStringeeCall(stringeeCallModel);
95+
_handledCallUuids
96+
.removeWhere((element) => element.uuid == _currentCallKit.uuid);
97+
}
9698
} else {
9799
// TODO: - incoming all different current call from pushkit
98100
}
@@ -295,6 +297,9 @@ class CallkeepManager {
295297
StringeeCallManager().answerStringeeCall(call);
296298
} else {
297299
/// store uuid to handle later
300+
if (_currentCallKit.uuid != null) {
301+
_handledCallUuids.add(_currentCallKit);
302+
}
298303
}
299304
}
300305
}

call_sample/lib/stringee_wrapper/stringee_wrapper.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class StringeeWrapper {
4141

4242
StringeeListener? get stringeeListener => _stringeeListener;
4343

44+
// timeout for call
45+
int callTimeout = 60;
4446
bool _isEnablePush = false;
4547
bool get isEnablePush => _isEnablePush;
4648
bool get connected => _stringeeClient.hasConnected;

0 commit comments

Comments
 (0)