Skip to content

Commit 00c49c6

Browse files
committed
Fix android functionality
1 parent 7d73060 commit 00c49c6

File tree

5 files changed

+147
-103
lines changed

5 files changed

+147
-103
lines changed

actions.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,25 @@ const didReceiveStartCallAction = handler => {
2828
};
2929

3030
const answerCall = handler =>
31-
eventEmitter.addListener(RNCallKeepPerformAnswerCallAction, (data) => handler(isIOS ? data : {}));
31+
eventEmitter.addListener(RNCallKeepPerformAnswerCallAction, (data) => handler(data));
3232

3333
const endCall = handler =>
34-
eventEmitter.addListener(RNCallKeepPerformEndCallAction, (data) => handler(isIOS ? data : {}));
34+
eventEmitter.addListener(RNCallKeepPerformEndCallAction, (data) => handler(data));
3535

3636
const didActivateAudioSession = handler =>
3737
eventEmitter.addListener(RNCallKeepDidActivateAudioSession, handler);
3838

3939
const didDisplayIncomingCall = handler =>
40-
eventEmitter.addListener(RNCallKeepDidDisplayIncomingCall, (data) => handler(isIOS ? data.error : null));
40+
eventEmitter.addListener(RNCallKeepDidDisplayIncomingCall, (data) => handler(data));
4141

4242
const didPerformSetMutedCallAction = handler =>
43-
eventEmitter.addListener(RNCallKeepDidPerformSetMutedCallAction, (data) => handler(data.muted));
43+
eventEmitter.addListener(RNCallKeepDidPerformSetMutedCallAction, (data) => handler(data));
4444

4545
const didToggleHoldCallAction = handler =>
4646
eventEmitter.addListener(RNCallKeepDidToggleHoldAction, handler);
4747

4848
const didPerformDTMFAction = handler =>
49-
eventEmitter.addListener(RNCallKeepDidPerformDTMFAction, (data) => {
50-
const payload = isIOS ? { dtmf: data.digits, callUUID: data.callUUID } : data;
51-
52-
return handler(payload);
53-
});
49+
eventEmitter.addListener(RNCallKeepDidPerformDTMFAction, (data) => handler(data));
5450

5551
export const listeners = {
5652
didReceiveStartCallAction,

android/src/main/java/io/wazo/callkeep/RNCallKeepModule.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class RNCallKeepModule extends ReactContextBaseJavaModule {
5454

5555
public static final String CHECKING_PERMS = "CHECKING_PERMS";
5656
public static final String EXTRA_CALLER_NAME = "EXTRA_CALLER_NAME";
57+
public static final String EXTRA_CALL_UUID = "EXTRA_CALL_UUID";
5758
public static final String ACTION_END_CALL = "ACTION_END_CALL";
5859
public static final String ACTION_ANSWER_CALL = "ACTION_ANSWER_CALL";
5960
public static final String ACTION_MUTE_CALL = "ACTION_MUTE_CALL";
@@ -105,7 +106,7 @@ public String getName() {
105106
}
106107

107108
@ReactMethod
108-
public void displayIncomingCall(String number, String callerName) {
109+
public void displayIncomingCall(String uuid, String number, String callerName) {
109110
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
110111
return;
111112
}
@@ -115,6 +116,7 @@ public void displayIncomingCall(String number, String callerName) {
115116

116117
extras.putParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS, uri);
117118
extras.putString(EXTRA_CALLER_NAME, callerName);
119+
extras.putString(EXTRA_CALL_UUID, uuid);
118120

119121
telecomManager.addNewIncomingCall(handle, extras);
120122
}
@@ -339,10 +341,12 @@ public void onReceive(Context context, Intent intent) {
339341

340342
switch (intent.getAction()) {
341343
case ACTION_END_CALL:
342-
sendEventToJS("RNCallKeepPerformEndCallAction", null);
344+
args.putString("callUUID", intent.getStringExtra("attribute"));
345+
sendEventToJS("RNCallKeepPerformEndCallAction", args);
343346
break;
344347
case ACTION_ANSWER_CALL:
345-
sendEventToJS("RNCallKeepPerformAnswerCallAction", null);
348+
args.putString("callUUID", intent.getStringExtra("attribute"));
349+
sendEventToJS("RNCallKeepPerformAnswerCallAction", args);
346350
break;
347351
case ACTION_HOLD_CALL:
348352
args.putBoolean("hold", true);
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package io.wazo.callkeep;
2+
3+
import android.annotation.TargetApi;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.os.Build;
7+
import android.os.Bundle;
8+
import android.os.Handler;
9+
import android.support.annotation.Nullable;
10+
import android.support.v4.content.LocalBroadcastManager;
11+
import android.telecom.CallAudioState;
12+
import android.telecom.Connection;
13+
import android.telecom.DisconnectCause;
14+
import android.util.Log;
15+
16+
import org.json.JSONException;
17+
import org.json.JSONObject;
18+
19+
import static io.wazo.callkeep.RNCallKeepModule.ACTION_ANSWER_CALL;
20+
import static io.wazo.callkeep.RNCallKeepModule.ACTION_AUDIO_SESSION;
21+
import static io.wazo.callkeep.RNCallKeepModule.ACTION_DTMF_TONE;
22+
import static io.wazo.callkeep.RNCallKeepModule.ACTION_END_CALL;
23+
import static io.wazo.callkeep.RNCallKeepModule.ACTION_HOLD_CALL;
24+
import static io.wazo.callkeep.RNCallKeepModule.ACTION_MUTE_CALL;
25+
import static io.wazo.callkeep.RNCallKeepModule.ACTION_UNHOLD_CALL;
26+
import static io.wazo.callkeep.RNCallKeepModule.ACTION_UNMUTE_CALL;
27+
import static io.wazo.callkeep.RNCallKeepModule.EXTRA_CALL_UUID;
28+
29+
@TargetApi(Build.VERSION_CODES.M)
30+
public class VoiceConnection extends Connection {private String TAG = "VoiceConnection";
31+
private boolean isMuted = false;
32+
private String handle = "";
33+
private Context context;
34+
35+
VoiceConnection(Context context, String handle) {
36+
super();
37+
this.handle = handle;
38+
this.context = context;
39+
}
40+
41+
@Override
42+
public void onExtrasChanged(Bundle extras) {
43+
super.onExtrasChanged(extras);
44+
handle = extras.getString(EXTRA_CALL_UUID);
45+
}
46+
47+
@Override
48+
public void onCallAudioStateChanged(CallAudioState state) {
49+
if (state.isMuted() == this.isMuted) {
50+
return;
51+
}
52+
53+
this.isMuted = state.isMuted();
54+
sendCallRequestToActivity(isMuted ? ACTION_MUTE_CALL : ACTION_UNMUTE_CALL, handle);
55+
}
56+
57+
@Override
58+
public void onAnswer() {
59+
super.onAnswer();
60+
61+
setActive();
62+
setAudioModeIsVoip(true);
63+
64+
sendCallRequestToActivity(ACTION_ANSWER_CALL, handle);
65+
}
66+
67+
@Override
68+
public void onPlayDtmfTone(char dtmf) {
69+
sendCallRequestToActivity(ACTION_DTMF_TONE, String.valueOf(dtmf));
70+
}
71+
72+
@Override
73+
public void onDisconnect() {
74+
super.onDisconnect();
75+
setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
76+
sendCallRequestToActivity(ACTION_END_CALL, handle);
77+
destroy();
78+
}
79+
80+
@Override
81+
public void onAbort() {
82+
super.onAbort();
83+
84+
setDisconnected(new DisconnectCause(DisconnectCause.CANCELED));
85+
sendCallRequestToActivity(ACTION_END_CALL, handle);
86+
destroy();
87+
}
88+
89+
@Override
90+
public void onHold() {
91+
super.onHold();
92+
sendCallRequestToActivity(ACTION_HOLD_CALL, handle);
93+
}
94+
95+
@Override
96+
public void onUnhold() {
97+
super.onUnhold();
98+
sendCallRequestToActivity(ACTION_UNHOLD_CALL, handle);
99+
setActive();
100+
}
101+
102+
@Override
103+
public void onReject() {
104+
super.onReject();
105+
106+
setDisconnected(new DisconnectCause(DisconnectCause.CANCELED));
107+
sendCallRequestToActivity(ACTION_END_CALL, handle);
108+
destroy();
109+
}
110+
111+
/*
112+
* Send call request to the RNCallKeepModule
113+
*/
114+
private void sendCallRequestToActivity(final String action, @Nullable final String attribute) {
115+
final VoiceConnection instance = this;
116+
final Handler handler = new Handler();
117+
118+
handler.post(new Runnable() {
119+
@Override
120+
public void run() {
121+
Intent intent = new Intent(action);
122+
if (attribute != null) {
123+
intent.putExtra("attribute", attribute);
124+
}
125+
126+
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
127+
}
128+
});
129+
}
130+
}

android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java

Lines changed: 3 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@
4343
import static io.wazo.callkeep.RNCallKeepModule.ACTION_UNHOLD_CALL;
4444
import static io.wazo.callkeep.RNCallKeepModule.ACTION_UNMUTE_CALL;
4545
import static io.wazo.callkeep.RNCallKeepModule.EXTRA_CALLER_NAME;
46+
import static io.wazo.callkeep.RNCallKeepModule.EXTRA_CALL_UUID;
4647

4748
// @see https://github.com/kbagchiGWC/voice-quickstart-android/blob/9a2aff7fbe0d0a5ae9457b48e9ad408740dfb968/exampleConnectionService/src/main/java/com/twilio/voice/examples/connectionservice/VoiceConnectionService.java
4849
@TargetApi(Build.VERSION_CODES.M)
4950
public class VoiceConnectionService extends ConnectionService {
5051
private static Connection connection;
5152
private static Boolean isAvailable = false;
53+
private static String TAG = "VoiceConnectionService";
5254

5355
public static Connection getConnection() {
5456
return connection;
@@ -92,97 +94,9 @@ private Boolean canMakeOutgoingCall() {
9294
}
9395

9496
private Connection createConnection(ConnectionRequest request) {
95-
connection = new Connection() {
96-
private boolean isMuted = false;
97-
98-
@Override
99-
public void onCallAudioStateChanged(CallAudioState state) {
100-
if (state.isMuted() == this.isMuted) {
101-
return;
102-
}
103-
104-
this.isMuted = state.isMuted();
105-
106-
sendCallRequestToActivity(isMuted ? ACTION_MUTE_CALL : ACTION_UNMUTE_CALL, null);
107-
}
108-
109-
@Override
110-
public void onAnswer() {
111-
super.onAnswer();
112-
if (connection == null) {
113-
return;
114-
}
115-
116-
connection.setActive();
117-
connection.setAudioModeIsVoip(true);
118-
119-
sendCallRequestToActivity(ACTION_ANSWER_CALL, null);
120-
sendCallRequestToActivity(ACTION_AUDIO_SESSION, null);
121-
}
122-
123-
@Override
124-
public void onPlayDtmfTone(char dtmf) {
125-
sendCallRequestToActivity(ACTION_DTMF_TONE, String.valueOf(dtmf));
126-
}
127-
128-
@Override
129-
public void onDisconnect() {
130-
super.onDisconnect();
131-
if (connection == null) {
132-
return;
133-
}
134-
135-
connection.setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
136-
connection.destroy();
137-
connection = null;
138-
139-
sendCallRequestToActivity(ACTION_END_CALL, null);
140-
}
141-
142-
@Override
143-
public void onAbort() {
144-
super.onAbort();
145-
if (connection == null) {
146-
return;
147-
}
148-
149-
connection.setDisconnected(new DisconnectCause(DisconnectCause.CANCELED));
150-
connection.destroy();
151-
152-
sendCallRequestToActivity(ACTION_END_CALL, null);
153-
}
154-
155-
@Override
156-
public void onHold() {
157-
super.onHold();
158-
connection.setOnHold();
159-
160-
sendCallRequestToActivity(ACTION_HOLD_CALL, null);
161-
}
162-
163-
@Override
164-
public void onUnhold() {
165-
super.onUnhold();
166-
connection.setActive();
167-
168-
sendCallRequestToActivity(ACTION_UNHOLD_CALL, null);
169-
}
170-
171-
@Override
172-
public void onReject() {
173-
super.onReject();
174-
if (connection == null) {
175-
return;
176-
}
177-
178-
connection.setDisconnected(new DisconnectCause(DisconnectCause.CANCELED));
179-
connection.destroy();
180-
181-
sendCallRequestToActivity(ACTION_END_CALL, null);
182-
}
183-
};
18497

18598
Bundle extra = request.getExtras();
99+
connection = new VoiceConnection(this, extra.getString(EXTRA_CALL_UUID));
186100

187101
connection.setConnectionCapabilities(Connection.CAPABILITY_MUTE | Connection.CAPABILITY_HOLD | Connection.CAPABILITY_SUPPORT_HOLD);
188102
connection.setAddress(request.getAddress(), TelecomManager.PRESENTATION_ALLOWED);

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class RNCallKeep {
4747

4848
displayIncomingCall = (uuid, handle, localizedCallerName, handleType = 'number', hasVideo = false) => {
4949
if (!isIOS) {
50-
RNCallKeepModule.displayIncomingCall(handle, localizedCallerName);
50+
RNCallKeepModule.displayIncomingCall(uuid, handle, localizedCallerName);
5151
return;
5252
}
5353

@@ -56,7 +56,7 @@ class RNCallKeep {
5656

5757
startCall = (uuid, handle, handleType = 'number', hasVideo = false, contactIdentifier) => {
5858
if (!isIOS) {
59-
RNCallKeepModule.startCall(handle, contactIdentifier);
59+
RNCallKeepModule.startCall(uuid, andle, contactIdentifier);
6060
return;
6161
}
6262

0 commit comments

Comments
 (0)