Skip to content

Commit a97b230

Browse files
Android: implement Twilio Programmable Voice Android SDK 4.5.0
- handle Call `onReconnecting` and Call `onReconnected`
1 parent ef55791 commit a97b230

File tree

5 files changed

+58
-28
lines changed

5 files changed

+58
-28
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ This is a React Native wrapper for Twilio Programmable Voice SDK that lets you m
44

55
# Twilio Programmable Voice SDK
66

7-
- Android 3.3.0 (bundled within this library)
7+
- Android 4.5.0 (bundled within this library)
88
- iOS 2.1.0 (specified by the app's own podfile)
99

1010
## Breaking changes in v4.0.0
1111

1212
It implements [react-native autolinking](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md) as many other native libraries > react-native 0.60.0
1313

14-
Android: update Firebase Messaging to 17.3.4. Remove the following block from your application's `AndroidManifest.xml`
14+
Android: update Firebase Messaging to 17.6.+. Remove the following block from your application's `AndroidManifest.xml`
1515
```xml
1616
<!-- [START instanceId_listener] -->
1717
<service
@@ -268,6 +268,12 @@ TwilioVoice.addEventListener('connectionDidConnect', function(data) {
268268
// call_to: string, // "client:bob"
269269
// }
270270
})
271+
TwilioVoice.addEventListener('connectionIsReconnecting', function(data) {
272+
// empty data
273+
})
274+
TwilioVoice.addEventListener('connectionDidReconnect', function(data) {
275+
// empty data
276+
})
271277
TwilioVoice.addEventListener('connectionDidDisconnect', function(data: mixed) {
272278
// | null
273279
// | {

android/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ buildscript {
66
jcenter()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.5.2'
10-
classpath 'com.google.gms:google-services:4.2.0'
9+
classpath 'com.android.tools.build:gradle:3.5.3'
10+
classpath 'com.google.gms:google-services:4.3.3'
1111

1212
// NOTE: Do not place your application dependencies here; they belong
1313
// in the individual module build.gradle files
@@ -54,9 +54,9 @@ dependencies {
5454
def supportLibVersion = rootProject.hasProperty('supportLibVersion') ? rootProject.supportLibVersion : DEFAULT_SUPPORT_LIB_VERSION
5555

5656
implementation fileTree(include: ['*.jar'], dir: 'libs')
57-
implementation 'com.twilio:voice-android:3.3.0'
57+
implementation 'com.twilio:voice-android:4.5.0'
5858
implementation "com.android.support:appcompat-v7:$supportLibVersion"
5959
implementation 'com.facebook.react:react-native:+'
60-
implementation 'com.google.firebase:firebase-messaging:17.+'
60+
implementation 'com.google.firebase:firebase-messaging:17.6.+'
6161
testImplementation 'junit:junit:4.12'
6262
}

android/src/main/java/com/hoxfon/react/RNTwilioVoice/EventManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class EventManager {
2323
public static final String EVENT_DEVICE_DID_RECEIVE_INCOMING = "deviceDidReceiveIncoming";
2424
public static final String EVENT_CALL_STATE_RINGING = "callStateRinging";
2525
public static final String EVENT_CALL_INVITE_CANCELLED = "callInviteCancelled";
26+
public static final String EVENT_CONNECTION_IS_RECONNECTING = "connectionIsReconnecting";
27+
public static final String EVENT_CONNECTION_DID_RECONNECT = "connectionDidReconnect";
2628

2729

2830
public EventManager(ReactApplicationContext context) {

android/src/main/java/com/hoxfon/react/RNTwilioVoice/TwilioVoiceModule.java

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
import static com.hoxfon.react.RNTwilioVoice.EventManager.EVENT_DEVICE_READY;
6565
import static com.hoxfon.react.RNTwilioVoice.EventManager.EVENT_CALL_STATE_RINGING;
6666
import static com.hoxfon.react.RNTwilioVoice.EventManager.EVENT_CALL_INVITE_CANCELLED;
67+
import static com.hoxfon.react.RNTwilioVoice.EventManager.EVENT_CONNECTION_IS_RECONNECTING;
68+
import static com.hoxfon.react.RNTwilioVoice.EventManager.EVENT_CONNECTION_DID_RECONNECT;
6769

6870
public class TwilioVoiceModule extends ReactContextBaseJavaModule implements ActivityEventListener, LifecycleEventListener {
6971

@@ -249,7 +251,6 @@ public void onRinging(Call call) {
249251
if (call != null) {
250252
params.putString("call_sid", call.getSid());
251253
params.putString("call_from", call.getFrom());
252-
params.putString("call_state", call.getState().name());
253254
}
254255
eventManager.sendEvent(EVENT_CALL_STATE_RINGING, params);
255256
}
@@ -282,26 +283,42 @@ public void onConnected(Call call) {
282283
eventManager.sendEvent(EVENT_CONNECTION_DID_CONNECT, params);
283284
}
284285

285-
// Prepare methods Twilio for v4
286-
// @Override
287-
// public void onReconnecting(Call call, CallException callException) {
288-
// if (BuildConfig.DEBUG) {
289-
// Log.d(TAG, "CALL RECONNECTING callListener().onReconnecting call state = "+call.getState());
290-
// }
291-
// // TODO implement
292-
//// eventManager.sendEvent(XXX, params);
293-
//
294-
// }
295-
//
296-
// @Override
297-
// public void onReconnected(Call call) {
298-
// if (BuildConfig.DEBUG) {
299-
// Log.d(TAG, "CALL RECONNECTED callListener().onReconnected call state = "+call.getState());
300-
// }
301-
//
302-
// // TODO implement
303-
//// eventManager.sendEvent(XXX, params);
304-
// }
286+
/**
287+
* `onReconnecting()` callback is raised when a network change is detected and Call is already in `CONNECTED`
288+
* `Call.State`. If the call is in `CONNECTING` or `RINGING` when network change happened the SDK will continue
289+
* attempting to connect, but a reconnect event will not be raised.
290+
*/
291+
@Override
292+
public void onReconnecting(@NonNull Call call, @NonNull CallException callException) {
293+
if (BuildConfig.DEBUG) {
294+
Log.d(TAG, "CALL RECONNECTING callListener().onReconnecting call state = "+call.getState());
295+
}
296+
WritableMap params = Arguments.createMap();
297+
if (call != null) {
298+
params.putString("call_sid", call.getSid());
299+
params.putString("call_from", call.getFrom());
300+
params.putString("call_to", call.getTo());
301+
}
302+
eventManager.sendEvent(EVENT_CONNECTION_IS_RECONNECTING, params);
303+
304+
}
305+
306+
/**
307+
* The call is successfully reconnected after reconnecting attempt.
308+
*/
309+
@Override
310+
public void onReconnected(@NonNull Call call) {
311+
if (BuildConfig.DEBUG) {
312+
Log.d(TAG, "CALL RECONNECTED callListener().onReconnected call state = "+call.getState());
313+
}
314+
WritableMap params = Arguments.createMap();
315+
if (call != null) {
316+
params.putString("call_sid", call.getSid());
317+
params.putString("call_from", call.getFrom());
318+
params.putString("call_to", call.getTo());
319+
}
320+
eventManager.sendEvent(EVENT_CONNECTION_DID_RECONNECT, params);
321+
}
305322

306323
@Override
307324
public void onDisconnected(Call call, CallException error) {
@@ -605,7 +622,9 @@ public void accept() {
605622
if (BuildConfig.DEBUG) {
606623
Log.d(TAG, "accept()");
607624
}
608-
AcceptOptions acceptOptions = new AcceptOptions.Builder().build();
625+
AcceptOptions acceptOptions = new AcceptOptions.Builder()
626+
.enableDscp(true)
627+
.build();
609628
activeCallInvite.accept(getReactApplicationContext(), acceptOptions, callListener);
610629
clearIncomingNotification(activeCallInvite.getCallSid());
611630

@@ -711,6 +730,7 @@ public void connect(ReadableMap params) {
711730
}
712731

713732
ConnectOptions connectOptions = new ConnectOptions.Builder(accessToken)
733+
.enableDscp(true)
714734
.params(twiMLParams)
715735
.build();
716736

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const _eventHandlers = {
1616
deviceNotReady: new Map(),
1717
deviceDidReceiveIncoming: new Map(),
1818
connectionDidConnect: new Map(),
19+
connectionIsReconnecting: new Map(),
20+
connectionDidReconnect: new Map(),
1921
connectionDidDisconnect: new Map(),
2022
callStateRinging: new Map(),
2123
callInviteCancelled: new Map(),

0 commit comments

Comments
 (0)