Skip to content

Commit 7c1598b

Browse files
authored
Merge pull request #11 from wazo-pbx/current_call_active
Allow to set current call as active
2 parents 5cb2162 + 4b05842 commit 7c1598b

File tree

4 files changed

+79
-57
lines changed

4 files changed

+79
-57
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ RNCallKeep.setup(options);
7272

7373
## Methods
7474

75-
### setActive
75+
### setAvailable
7676
_This feature is available only on Android._
7777

7878
Tell _ConnectionService_ that the device is ready to accept outgoing calls.
7979
If not the user will be stuck in the build UI screen without any actions.
8080
Eg: Call it with `false` when disconnected from the sip client, when your token expires ...
8181

8282
```js
83-
RNCallKeep.setActive(true);
83+
RNCallKeep.setAvailable(true);
8484
```
8585

8686
- `active`: boolean
@@ -144,6 +144,14 @@ RNCallKeep.endCall(uuid);
144144
- `uuid`: string
145145
- The `uuid` used for `startCall` or `displayIncomingCall`
146146

147+
### setCurrentCallActive
148+
149+
Mark the current call as active (eg: when the callee as answered).
150+
151+
```js
152+
RNCallKeep.setCurrentCallActive();
153+
```
154+
147155

148156
### setMutedCall
149157

@@ -323,7 +331,7 @@ class RNCallKeepExample extends React.Component {
323331

324332
try {
325333
RNCallKeep.setup(options);
326-
RNCallKeep.setActive(true); // Only used for Android, see doc above.
334+
RNCallKeep.setAvailable(true); // Only used for Android, see doc above.
327335
} catch (err) {
328336
console.error('initializeCallKeep error:', err.message);
329337
}

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ public RNCallKeepModule(ReactApplicationContext reactContext) {
7979

8080
this.reactContext = reactContext;
8181

82-
VoiceConnectionService.setActive(false);
82+
VoiceConnectionService.setAvailable(false);
8383

84-
if (isAvailable()) {
84+
if (isConnectionServiceAvailable()) {
8585
this.registerPhoneAccount(this.getAppContext());
8686
voiceBroadcastReceiver = new VoiceBroadcastReceiver();
8787
registerReceiver();
88-
VoiceConnectionService.setActive(false);
88+
VoiceConnectionService.setAvailable(true);
8989
}
9090
}
9191

@@ -105,7 +105,7 @@ public String getName() {
105105

106106
@ReactMethod
107107
public void displayIncomingCall(String number, String callerName) {
108-
if (!isAvailable() || !hasPhoneAccount()) {
108+
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
109109
return;
110110
}
111111

@@ -120,7 +120,7 @@ public void displayIncomingCall(String number, String callerName) {
120120

121121
@ReactMethod
122122
public void startCall(String number, String callerName) {
123-
if (!isAvailable() || !hasPhoneAccount()) {
123+
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
124124
return;
125125
}
126126

@@ -138,7 +138,7 @@ public void startCall(String number, String callerName) {
138138

139139
@ReactMethod
140140
public void endCall() {
141-
if (!isAvailable() || !hasPhoneAccount()) {
141+
if (!isConnectionServiceAvailable() || !hasPhoneAccount()) {
142142
return;
143143
}
144144

@@ -154,7 +154,7 @@ public void endCall() {
154154

155155
@ReactMethod
156156
public void checkPhoneAccountPermission(Promise promise) {
157-
if (!isAvailable()) {
157+
if (!isConnectionServiceAvailable()) {
158158
promise.reject(E_ACTIVITY_DOES_NOT_EXIST, "ConnectionService not available for this version of Android.");
159159
return;
160160
}
@@ -178,13 +178,23 @@ public void hasPhoneAccount(Promise promise) {
178178
}
179179

180180
@ReactMethod
181-
public void setActive(Boolean active) {
182-
VoiceConnectionService.setActive(active);
181+
public void setAvailable(Boolean active) {
182+
VoiceConnectionService.setAvailable(active);
183+
}
184+
185+
@ReactMethod
186+
public void setCurrentCallActive() {
187+
Connection conn = VoiceConnectionService.getConnection();
188+
if (conn == null) {
189+
return;
190+
}
191+
192+
conn.setActive();
183193
}
184194

185195
@ReactMethod
186196
public void openPhoneAccounts() {
187-
if (!isAvailable()) {
197+
if (!isConnectionServiceAvailable()) {
188198
return;
189199
}
190200

@@ -204,13 +214,13 @@ public void openPhoneAccounts() {
204214
}
205215

206216
@ReactMethod
207-
public static Boolean isAvailable() {
217+
public static Boolean isConnectionServiceAvailable() {
208218
// PhoneAccount is available since api level 23
209219
return Build.VERSION.SDK_INT >= 23;
210220
}
211221

212222
private void registerPhoneAccount(Context appContext) {
213-
if (!isAvailable()) {
223+
if (!isConnectionServiceAvailable()) {
214224
return;
215225
}
216226

@@ -257,7 +267,7 @@ private Boolean checkPermissions(String[] permissions, int id) {
257267
}
258268

259269
private static boolean hasPhoneAccount() {
260-
if (!isAvailable()) {
270+
if (!isConnectionServiceAvailable()) {
261271
return false;
262272
}
263273

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.telecom.DisconnectCause;
3232
import android.telecom.PhoneAccountHandle;
3333
import android.telecom.TelecomManager;
34+
import android.util.Log;
3435

3536
import static io.wazo.callkeep.RNCallKeepModule.ACTION_ANSWER_CALL;
3637
import static io.wazo.callkeep.RNCallKeepModule.ACTION_AUDIO_SESSION;
@@ -47,14 +48,14 @@
4748
@TargetApi(Build.VERSION_CODES.M)
4849
public class VoiceConnectionService extends ConnectionService {
4950
private static Connection connection;
50-
private static Boolean isActive = false;
51+
private static Boolean isAvailable = false;
5152

5253
public static Connection getConnection() {
5354
return connection;
5455
}
5556

56-
public static void setActive(Boolean value) {
57-
isActive = value;
57+
public static void setAvailable(Boolean value) {
58+
isAvailable = value;
5859
}
5960

6061

@@ -87,7 +88,7 @@ public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManage
8788
}
8889

8990
private Boolean canMakeOutgoingCall() {
90-
return isActive;
91+
return isAvailable;
9192
}
9293

9394
private Connection createConnection(ConnectionRequest request) {

index.js

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,101 +13,104 @@ class RNCallKeep {
1313
}
1414

1515

16-
addEventListener(type, handler) {
16+
addEventListener = (type, handler) => {
1717
const listener = listeners[type](handler);
1818

1919
this._callkitEventHandlers.set(handler, listener);
20-
}
20+
};
2121

22-
removeEventListener(type, handler) {
22+
removeEventListener = (type, handler) => {
2323
const listener = this._callkitEventHandlers.get(handler);
2424
if (!listener) {
2525
return;
2626
}
2727

2828
listener.remove();
2929
this._callkitEventHandlers.delete(handler);
30-
}
30+
};
3131

32-
setup(options) {
32+
setup = (options) => {
3333
if (!isIOS) {
3434
return (async () => {
3535
return this._setupAndroid(options.android);
3636
})();
3737
}
3838

3939
return this._setupIOS(options.ios);
40-
}
40+
};
4141

42-
displayIncomingCall(uuid, handle, localizedCallerName, handleType = 'number', hasVideo = false) {
42+
displayIncomingCall = (uuid, handle, localizedCallerName, handleType = 'number', hasVideo = false) => {
4343
if (!isIOS) {
4444
RNCallKeepModule.displayIncomingCall(handle, localizedCallerName);
4545
return;
4646
}
4747

4848
RNCallKeepModule.displayIncomingCall(uuid, handle, handleType, hasVideo, localizedCallerName);
49-
}
49+
};
5050

51-
startCall(uuid, handle, handleType = 'number', hasVideo = false, contactIdentifier) {
51+
startCall = (uuid, handle, handleType = 'number', hasVideo = false, contactIdentifier) => {
5252
if (!isIOS) {
5353
RNCallKeepModule.startCall(handle, contactIdentifier);
5454
return;
5555
}
5656

5757
RNCallKeepModule.startCall(uuid, handle, handleType, hasVideo, contactIdentifier);
58-
}
58+
};
5959

60-
reportConnectedOutgoingCallWithUUID(uuid) {
60+
reportConnectedOutgoingCallWithUUID = (uuid) => {
6161
RNCallKeepModule.reportConnectedOutgoingCallWithUUID(uuid);
62-
}
62+
};
6363

64-
endCall(uuid) {
64+
endCall = (uuid) => {
6565
isIOS ? RNCallKeepModule.endCall(uuid) : RNCallKeepModule.endCall();
66-
}
66+
};
6767

68-
endAllCalls() {
68+
endAllCalls = () => {
6969
isIOS ? RNCallKeepModule.endAllCalls() : RNCallKeepModule.endCall();
70-
}
70+
};
7171

72-
supportConnectionService() {
73-
return supportConnectionService;
74-
}
72+
supportConnectionService = () => supportConnectionService;
7573

76-
async hasPhoneAccount() {
77-
return isIOS ? true : await RNCallKeepModule.hasPhoneAccount();
78-
}
74+
hasPhoneAccount = async () =>
75+
isIOS ? true : await RNCallKeepModule.hasPhoneAccount();
7976

80-
setMutedCAll(uuid, muted) {
77+
setMutedCall = (uuid, muted) => {
8178
if (!isIOS) {
8279
// Can't mute on Android
8380
return;
8481
}
8582

8683
RNCallKeepModule.setMutedCall(uuid, muted);
87-
}
84+
};
8885

89-
checkIfBusy() {
90-
return Platform.OS === 'ios'
86+
checkIfBusy = () =>
87+
Platform.OS === 'ios'
9188
? RNCallKeepModule.checkIfBusy()
9289
: Promise.reject('RNCallKeep.checkIfBusy was called from unsupported OS');
93-
};
9490

95-
checkSpeaker() {
96-
return Platform.OS === 'ios'
91+
checkSpeaker = () =>
92+
Platform.OS === 'ios'
9793
? RNCallKeepModule.checkSpeaker()
9894
: Promise.reject('RNCallKeep.checkSpeaker was called from unsupported OS');
99-
}
10095

101-
setActive = (state) => {
96+
setAvailable = (state) => {
10297
if (isIOS) {
10398
return;
10499
}
105100

106101
// Tell android that we are able to make outgoing calls
107-
RNCallKeepModule.setActive(state);
108-
}
102+
RNCallKeepModule.setAvailable(state);
103+
};
109104

110-
_setupIOS(options) {
105+
setCurrentCallActive = () => {
106+
if (isIOS) {
107+
return;
108+
}
109+
110+
RNCallKeepModule.setCurrentCallActive();
111+
};
112+
113+
_setupIOS = (options) => {
111114
if (!options.appName) {
112115
throw new Error('RNCallKeep.setup: option "appName" is required');
113116
}
@@ -116,9 +119,9 @@ class RNCallKeep {
116119
}
117120

118121
RNCallKeepModule.setup(options);
119-
}
122+
};
120123

121-
async _setupAndroid(options) {
124+
_setupAndroid = async (options) => {
122125
const hasAccount = await RNCallKeepModule.checkPhoneAccountPermission();
123126
if (hasAccount) {
124127
return;
@@ -139,7 +142,7 @@ class RNCallKeep {
139142
],
140143
{ cancelable: true },
141144
);
142-
}
145+
};
143146

144147
/*
145148
static holdCall(uuid, onHold) {

0 commit comments

Comments
 (0)