Skip to content

Commit

Permalink
merged latest
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuan Minh Bui committed Jun 16, 2020
2 parents 8f40964 + 59b2526 commit 650d447
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 75 deletions.
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ Be sure to set this only after your call is ready for two way audio; used both i
RNCallKeep.setCurrentCallActive(uuid);
```

- `uuid`: string
- The `uuid` used for `startCall` or `displayIncomingCall`

### isCallActive
_This feature is available only on IOS._

Returns true if the UUID passed matches an existing and answered call.
This will return true ONLY if the call exists and the user has already answered the call. It will return false
if the call does not exist or has not been answered. This is exposed to both React Native and Native sides.
This was exposed so a call can be canceled if ringing and the user answered on a different device.

```js
RNCallKeep.isCallActive(uuid);
```

- `uuid`: string
- The `uuid` used for `startCall` or `displayIncomingCall`

Expand Down Expand Up @@ -513,6 +528,14 @@ So we have to check if the application is reachable before making a call from th
RNCallKeep.addEventListener('checkReachability', () => {
RNCallKeep.setReachable();
});

```
### removeEventListener

Allows to remove the listener on an event.

```js
RNCallKeep.removeEventListener('checkReachability');
```

## Example
Expand Down Expand Up @@ -665,9 +688,7 @@ Since iOS 13, you'll have to report the incoming calls that wakes up your applic
// NSString *handle = @"caller number here";
// NSDictionary *extra = [payload.dictionaryPayload valueForKeyPath:@"custom.path.to.data"]; /* use this to pass any special data (ie. from your notification) down to RN. Can also be `nil` */

[RNCallKeep reportNewIncomingCall:uuid handle:handle handleType:@"generic" hasVideo:false localizedCallerName:callerName fromPushKit: YES payload:extra];

completion();
[RNCallKeep reportNewIncomingCall:uuid handle:handle handleType:@"generic" hasVideo:false localizedCallerName:callerName fromPushKit: YES payload:extra withCompletionHandler:completion];
}
```

Expand Down
19 changes: 19 additions & 0 deletions android/src/main/java/io/wazo/callkeep/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.wazo.callkeep;

public class Constants {
public static final String ACTION_ANSWER_CALL = "ACTION_ANSWER_CALL";
public static final String ACTION_AUDIO_SESSION = "ACTION_AUDIO_SESSION";
public static final String ACTION_CHECK_REACHABILITY = "ACTION_CHECK_REACHABILITY";
public static final String ACTION_DTMF_TONE = "ACTION_DTMF_TONE";
public static final String ACTION_END_CALL = "ACTION_END_CALL";
public static final String ACTION_HOLD_CALL = "ACTION_HOLD_CALL";
public static final String ACTION_MUTE_CALL = "ACTION_MUTE_CALL";
public static final String ACTION_ONGOING_CALL = "ACTION_ONGOING_CALL";
public static final String ACTION_UNHOLD_CALL = "ACTION_UNHOLD_CALL";
public static final String ACTION_UNMUTE_CALL = "ACTION_UNMUTE_CALL";
public static final String ACTION_WAKE_APP = "ACTION_WAKE_APP";

public static final String EXTRA_CALL_NUMBER = "EXTRA_CALL_NUMBER";
public static final String EXTRA_CALL_UUID = "EXTRA_CALL_UUID";
public static final String EXTRA_CALLER_NAME = "EXTRA_CALLER_NAME";
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;

import static io.wazo.callkeep.RNCallKeepModule.EXTRA_CALLER_NAME;
import static io.wazo.callkeep.RNCallKeepModule.EXTRA_CALL_NUMBER;
import static io.wazo.callkeep.RNCallKeepModule.EXTRA_CALL_UUID;
import static io.wazo.callkeep.Constants.EXTRA_CALLER_NAME;
import static io.wazo.callkeep.Constants.EXTRA_CALL_NUMBER;
import static io.wazo.callkeep.Constants.EXTRA_CALL_UUID;

import javax.annotation.Nullable;

Expand Down
45 changes: 29 additions & 16 deletions android/src/main/java/io/wazo/callkeep/RNCallKeepModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;

import java.lang.reflect.Array;
Expand All @@ -67,27 +68,26 @@

import static android.support.v4.app.ActivityCompat.requestPermissions;

import static io.wazo.callkeep.Constants.EXTRA_CALLER_NAME;
import static io.wazo.callkeep.Constants.EXTRA_CALL_UUID;
import static io.wazo.callkeep.Constants.EXTRA_CALL_NUMBER;
import static io.wazo.callkeep.Constants.ACTION_END_CALL;
import static io.wazo.callkeep.Constants.ACTION_ANSWER_CALL;
import static io.wazo.callkeep.Constants.ACTION_MUTE_CALL;
import static io.wazo.callkeep.Constants.ACTION_UNMUTE_CALL;
import static io.wazo.callkeep.Constants.ACTION_DTMF_TONE;
import static io.wazo.callkeep.Constants.ACTION_HOLD_CALL;
import static io.wazo.callkeep.Constants.ACTION_UNHOLD_CALL;
import static io.wazo.callkeep.Constants.ACTION_ONGOING_CALL;
import static io.wazo.callkeep.Constants.ACTION_AUDIO_SESSION;
import static io.wazo.callkeep.Constants.ACTION_CHECK_REACHABILITY;
import static io.wazo.callkeep.Constants.ACTION_WAKE_APP;

// @see https://github.com/kbagchiGWC/voice-quickstart-android/blob/9a2aff7fbe0d0a5ae9457b48e9ad408740dfb968/exampleConnectionService/src/main/java/com/twilio/voice/examples/connectionservice/VoiceConnectionServiceActivity.java
public class RNCallKeepModule extends ReactContextBaseJavaModule {
public static final int REQUEST_READ_PHONE_STATE = 1337;
public static final int REQUEST_REGISTER_CALL_PROVIDER = 394859;

public static final String CHECKING_PERMS = "CHECKING_PERMS";
public static final String EXTRA_CALLER_NAME = "EXTRA_CALLER_NAME";
public static final String EXTRA_CALL_UUID = "EXTRA_CALL_UUID";
public static final String EXTRA_CALL_NUMBER = "EXTRA_CALL_NUMBER";
public static final String ACTION_END_CALL = "ACTION_END_CALL";
public static final String ACTION_ANSWER_CALL = "ACTION_ANSWER_CALL";
public static final String ACTION_MUTE_CALL = "ACTION_MUTE_CALL";
public static final String ACTION_UNMUTE_CALL = "ACTION_UNMUTE_CALL";
public static final String ACTION_DTMF_TONE = "ACTION_DTMF_TONE";
public static final String ACTION_HOLD_CALL = "ACTION_HOLD_CALL";
public static final String ACTION_UNHOLD_CALL = "ACTION_UNHOLD_CALL";
public static final String ACTION_ONGOING_CALL = "ACTION_ONGOING_CALL";
public static final String ACTION_AUDIO_SESSION = "ACTION_AUDIO_SESSION";
public static final String ACTION_CHECK_REACHABILITY = "ACTION_CHECK_REACHABILITY";
public static final String ACTION_SHOW_INCOMING_CALL_UI = "ACTION_SHOW_INCOMING_CALL_UI";

private static final String E_ACTIVITY_DOES_NOT_EXIST = "E_ACTIVITY_DOES_NOT_EXIST";
private static final String REACT_NATIVE_MODULE_NAME = "RNCallKeep";
private static String[] permissions = { Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE, Manifest.permission.RECORD_AUDIO };
Expand Down Expand Up @@ -132,6 +132,7 @@ public void setup(ReadableMap options) {
this.registerPhoneAccount(this.getAppContext());
voiceBroadcastReceiver = new VoiceBroadcastReceiver();
registerReceiver();
VoiceConnectionService.setPhoneAccountHandle(handle);
VoiceConnectionService.setAvailable(true);
}
}
Expand Down Expand Up @@ -603,6 +604,18 @@ public void onReceive(Context context, Intent intent) {
args.putString("name", attributeMap.get(EXTRA_CALLER_NAME));
sendEventToJS("RNCallKeepShowIncomingCallUi", args);
break;
case ACTION_WAKE_APP:
Intent headlessIntent = new Intent(reactContext, RNCallKeepBackgroundMessagingService.class);
headlessIntent.putExtra("callUUID", attributeMap.get(EXTRA_CALL_UUID));
headlessIntent.putExtra("name", attributeMap.get(EXTRA_CALLER_NAME));
headlessIntent.putExtra("handle", attributeMap.get(EXTRA_CALL_NUMBER));
Log.d(TAG, "wakeUpApplication: " + attributeMap.get(EXTRA_CALL_UUID) + ", number : " + attributeMap.get(EXTRA_CALL_NUMBER) + ", displayName:" + attributeMap.get(EXTRA_CALLER_NAME));

ComponentName name = reactContext.startService(headlessIntent);
if (name != null) {
HeadlessJsTaskService.acquireWakeLockNow(reactContext);
}
break;
}
}
}
Expand Down
23 changes: 11 additions & 12 deletions android/src/main/java/io/wazo/callkeep/VoiceConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,17 @@

import java.util.HashMap;

import static io.wazo.callkeep.RNCallKeepModule.ACTION_ANSWER_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_AUDIO_SESSION;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_DTMF_TONE;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_END_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_HOLD_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_MUTE_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_UNHOLD_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_UNMUTE_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_SHOW_INCOMING_CALL_UI;
import static io.wazo.callkeep.RNCallKeepModule.EXTRA_CALLER_NAME;
import static io.wazo.callkeep.RNCallKeepModule.EXTRA_CALL_NUMBER;
import static io.wazo.callkeep.RNCallKeepModule.EXTRA_CALL_UUID;
import static io.wazo.callkeep.Constants.ACTION_ANSWER_CALL;
import static io.wazo.callkeep.Constants.ACTION_AUDIO_SESSION;
import static io.wazo.callkeep.Constants.ACTION_DTMF_TONE;
import static io.wazo.callkeep.Constants.ACTION_END_CALL;
import static io.wazo.callkeep.Constants.ACTION_HOLD_CALL;
import static io.wazo.callkeep.Constants.ACTION_MUTE_CALL;
import static io.wazo.callkeep.Constants.ACTION_UNHOLD_CALL;
import static io.wazo.callkeep.Constants.ACTION_UNMUTE_CALL;
import static io.wazo.callkeep.Constants.EXTRA_CALLER_NAME;
import static io.wazo.callkeep.Constants.EXTRA_CALL_NUMBER;
import static io.wazo.callkeep.Constants.EXTRA_CALL_UUID;

@TargetApi(Build.VERSION_CODES.M)
public class VoiceConnection extends Connection {
Expand Down
52 changes: 18 additions & 34 deletions android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;

import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.common.LifecycleState;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -54,20 +50,13 @@
import java.util.UUID;
import java.util.stream.Collectors;

import static io.wazo.callkeep.RNCallKeepModule.ACTION_ANSWER_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_AUDIO_SESSION;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_DTMF_TONE;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_END_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_HOLD_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_MUTE_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_ONGOING_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_UNHOLD_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_UNMUTE_CALL;
import static io.wazo.callkeep.RNCallKeepModule.ACTION_CHECK_REACHABILITY;
import static io.wazo.callkeep.RNCallKeepModule.EXTRA_CALLER_NAME;
import static io.wazo.callkeep.RNCallKeepModule.EXTRA_CALL_NUMBER;
import static io.wazo.callkeep.RNCallKeepModule.EXTRA_CALL_UUID;
import static io.wazo.callkeep.RNCallKeepModule.handle;
import static io.wazo.callkeep.Constants.ACTION_AUDIO_SESSION;
import static io.wazo.callkeep.Constants.ACTION_ONGOING_CALL;
import static io.wazo.callkeep.Constants.ACTION_CHECK_REACHABILITY;
import static io.wazo.callkeep.Constants.ACTION_WAKE_APP;
import static io.wazo.callkeep.Constants.EXTRA_CALLER_NAME;
import static io.wazo.callkeep.Constants.EXTRA_CALL_NUMBER;
import static io.wazo.callkeep.Constants.EXTRA_CALL_UUID;

// @see https://github.com/kbagchiGWC/voice-quickstart-android/blob/9a2aff7fbe0d0a5ae9457b48e9ad408740dfb968/exampleConnectionService/src/main/java/com/twilio/voice/examples/connectionservice/VoiceConnectionService.java
@TargetApi(Build.VERSION_CODES.M)
Expand All @@ -77,6 +66,7 @@ public class VoiceConnectionService extends ConnectionService {
private static Boolean isReachable;
private static String notReachableCallUuid;
private static ConnectionRequest currentConnectionRequest;
private static PhoneAccountHandle phoneAccountHandle;
private static String TAG = "RNCK:VoiceConnectionService";
public static Map<String, VoiceConnection> currentConnections = new HashMap<>();
public static Boolean hasOutgoingCall = false;
Expand All @@ -98,6 +88,10 @@ public VoiceConnectionService() {
currentConnectionService = this;
}

public static void setPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) {
VoiceConnectionService.phoneAccountHandle = phoneAccountHandle;
}

public static void setAvailable(Boolean value) {
Log.d(TAG, "setAvailable: " + (value ? "true" : "false"));
if (value) {
Expand Down Expand Up @@ -196,19 +190,11 @@ private Connection makeOutgoingCall(ConnectionRequest request, String uuid, Bool
}

private void wakeUpApplication(String uuid, String number, String displayName) {
Intent headlessIntent = new Intent(
this.getApplicationContext(),
RNCallKeepBackgroundMessagingService.class
);
headlessIntent.putExtra("callUUID", uuid);
headlessIntent.putExtra("name", displayName);
headlessIntent.putExtra("handle", number);
Log.d(TAG, "wakeUpApplication: " + uuid + ", number : " + number + ", displayName:" + displayName);

ComponentName name = this.getApplicationContext().startService(headlessIntent);
if (name != null) {
HeadlessJsTaskService.acquireWakeLockNow(this.getApplicationContext());
}
HashMap<String, String> extrasMap = new HashMap();
extrasMap.put(EXTRA_CALL_UUID, uuid);
extrasMap.put(EXTRA_CALLER_NAME, displayName);
extrasMap.put(EXTRA_CALL_NUMBER, number);
sendCallRequestToActivity(ACTION_WAKE_APP, extrasMap);
}

private void wakeUpAfterReachabilityTimeout(ConnectionRequest request) {
Expand Down Expand Up @@ -287,9 +273,7 @@ public void onConference(Connection connection1, Connection connection2) {
VoiceConnection voiceConnection1 = (VoiceConnection) connection1;
VoiceConnection voiceConnection2 = (VoiceConnection) connection2;

PhoneAccountHandle phoneAccountHandle = RNCallKeepModule.handle;

VoiceConference voiceConference = new VoiceConference(handle);
VoiceConference voiceConference = new VoiceConference(phoneAccountHandle);
voiceConference.addConnection(voiceConnection1);
voiceConference.addConnection(voiceConnection2);

Expand Down
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class RNCallKeep {

}

static removeEventListener(type: Events, handler: (args: any) => void) {
static removeEventListener(type: Events) {

}

Expand Down Expand Up @@ -114,7 +114,9 @@ export default class RNCallKeep {
static setReachable() {

}
static isCallActive(uuid: string): Promise<boolean> {

}
/**
* @description supportConnectionService method is available only on Android.
*/
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RNCallKeep {
return;
};

displayIncomingCall = (uuid, handle, localizedCallerName, handleType = 'number', hasVideo = false) => {
displayIncomingCall = (uuid, handle, localizedCallerName = '', handleType = 'number', hasVideo = false) => {
if (!isIOS) {
RNCallKeepModule.displayIncomingCall(uuid, handle, localizedCallerName);
return;
Expand Down Expand Up @@ -108,6 +108,8 @@ class RNCallKeep {
}
};

isCallActive = async(uuid) => await RNCallKeepModule.isCallActive(uuid);

endCall = (uuid) => RNCallKeepModule.endCall(uuid);

endAllCalls = () => RNCallKeepModule.endAllCalls();
Expand Down
11 changes: 11 additions & 0 deletions ios/RNCallKeep/RNCallKeep.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,19 @@ continueUserActivity:(NSUserActivity *)userActivity
fromPushKit:(BOOL)fromPushKit
payload:(NSDictionary * _Nullable)payload;

+ (void)reportNewIncomingCall:(NSString *)uuidString
handle:(NSString *)handle
handleType:(NSString *)handleType
hasVideo:(BOOL)hasVideo
localizedCallerName:(NSString * _Nullable)localizedCallerName
fromPushKit:(BOOL)fromPushKit
payload:(NSDictionary * _Nullable)payload
withCompletionHandler:(void (^_Nullable)(void))completion;

+ (void)endCallWithUUID:(NSString *)uuidString
reason:(int)reason;
+ (BOOL) getIsSetup;
+ (CXProvider *) getProvider;
+ (BOOL)isCallActive:(NSString *)uuidString;

@end
Loading

0 comments on commit 650d447

Please sign in to comment.