Skip to content

[Android] Add support of self managed call #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ RNCallKeep.setup(options).then(accepted => {});
The image to use in the Android Phone application's native UI for enabling/disabling calling accounts. Should be a 48x48 HDPI
grayscale PNG image. Must be in your drawable resources for the parent application. Must be lowercase and underscore (_) characters
only, as Java doesn't like capital letters on resources.
- `selfManaged`: boolean (optional)
If provided and set to `true`, the app will be a standalone calling app and don't want their calls to be integrated into the built-in phone app.
- `additionalPermissions`: [PermissionsAndroid] (optional)
Any additional permissions you'd like your app to have at first launch. Can be used to simplify permission flows and avoid
multiple popups to the user at different times.
Expand Down Expand Up @@ -422,6 +424,19 @@ RNCallKeep.addEventListener('didReceiveStartCallAction', ({ handle, callUUID, na
- `name` (string)
- Name of the callee

### - showIncomingCall

Callback for `RNCallKeep.showIncomingCall`

```js
RNCallKeep.addEventListener('showIncomingCall', ({ callUUID }) => {
// Do your normal `Show incoming call` actions here.
});
```

- `callUUID` (string)
- The UUID of the call

### - answerCall

User answer the incoming call
Expand Down
5 changes: 5 additions & 0 deletions actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const RNCallKeepModule = NativeModules.RNCallKeep;
const eventEmitter = new NativeEventEmitter(RNCallKeepModule);

const RNCallKeepDidReceiveStartCallAction = 'RNCallKeepDidReceiveStartCallAction';
const RNCallKeepPerformShowIncomingCallAction = 'RNCallKeepPerformShowIncomingCallAction';
const RNCallKeepPerformAnswerCallAction = 'RNCallKeepPerformAnswerCallAction';
const RNCallKeepPerformEndCallAction = 'RNCallKeepPerformEndCallAction';
const RNCallKeepDidActivateAudioSession = 'RNCallKeepDidActivateAudioSession';
Expand All @@ -26,6 +27,9 @@ const didReceiveStartCallAction = handler => {
return eventEmitter.addListener(RNCallKeepDidReceiveStartCallAction, (data) => handler(data));
};

const showIncomingCall = handler =>
eventEmitter.addListener(RNCallKeepPerformShowIncomingCallAction, (data) => handler(data));

const answerCall = handler =>
eventEmitter.addListener(RNCallKeepPerformAnswerCallAction, (data) => handler(data));

Expand Down Expand Up @@ -63,6 +67,7 @@ export const emit = (eventName, payload) => eventEmitter.emit(eventName, payload

export const listeners = {
didReceiveStartCallAction,
showIncomingCall,
answerCall,
endCall,
didActivateAudioSession,
Expand Down
1 change: 1 addition & 0 deletions android/src/main/java/io/wazo/callkeep/Constants.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.wazo.callkeep;

public class Constants {
public static final String ACTION_SHOW_INCOMING_CALL = "ACTION_SHOW_INCOMING_CALL";
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";
Expand Down
10 changes: 10 additions & 0 deletions android/src/main/java/io/wazo/callkeep/RNCallKeepModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
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_SHOW_INCOMING_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;
Expand Down Expand Up @@ -487,6 +488,10 @@ private void registerPhoneAccount(Context appContext) {
builder.setIcon(icon);
}

if (_settings != null && _settings.hasKey("selfManaged") && _settings.getBoolean("selfManaged")) {
builder.setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED);
}

PhoneAccount account = builder.build();

telephonyManager = (TelephonyManager) this.getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
Expand Down Expand Up @@ -532,6 +537,7 @@ private void registerReceiver() {
if (!isReceiverRegistered) {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_END_CALL);
intentFilter.addAction(ACTION_SHOW_INCOMING_CALL);
intentFilter.addAction(ACTION_ANSWER_CALL);
intentFilter.addAction(ACTION_MUTE_CALL);
intentFilter.addAction(ACTION_UNMUTE_CALL);
Expand Down Expand Up @@ -574,6 +580,10 @@ public void onReceive(Context context, Intent intent) {
args.putString("callUUID", attributeMap.get(EXTRA_CALL_UUID));
sendEventToJS("RNCallKeepPerformEndCallAction", args);
break;
case ACTION_SHOW_INCOMING_CALL:
args.putString("callUUID", attributeMap.get(EXTRA_CALL_UUID));
sendEventToJS("RNCallKeepPerformShowIncomingCallAction", args);
break;
case ACTION_ANSWER_CALL:
args.putString("callUUID", attributeMap.get(EXTRA_CALL_UUID));
sendEventToJS("RNCallKeepPerformAnswerCallAction", args);
Expand Down
9 changes: 9 additions & 0 deletions android/src/main/java/io/wazo/callkeep/VoiceConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
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_SHOW_INCOMING_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;
Expand Down Expand Up @@ -91,6 +92,14 @@ public void onCallAudioStateChanged(CallAudioState state) {
sendCallRequestToActivity(isMuted ? ACTION_MUTE_CALL : ACTION_UNMUTE_CALL, handle);
}

@Override
public void onShowIncomingCallUi() {
super.onShowIncomingCallUi();
Log.d(TAG, "onShowIncomingCallUi called");

sendCallRequestToActivity(ACTION_SHOW_INCOMING_CALL, handle);
}

@Override
public void onAnswer() {
super.onAnswer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.telecom.ConnectionRequest;
import android.telecom.ConnectionService;
import android.telecom.DisconnectCause;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.util.Log;
Expand Down Expand Up @@ -248,6 +249,12 @@ private Connection createConnection(ConnectionRequest request) {
connection.setExtras(extras);
currentConnections.put(extras.getString(EXTRA_CALL_UUID), connection);

TelecomManager telecomManager = (TelecomManager) this.getApplicationContext().getSystemService(Context.TELECOM_SERVICE);
PhoneAccount phoneAccount = telecomManager.getPhoneAccount(request.getAccountHandle());
if ((phoneAccount.getCapabilities() & PhoneAccount.CAPABILITY_SELF_MANAGED) != 0) {
connection.setConnectionProperties(Connection.PROPERTY_SELF_MANAGED);
}

// Get other connections for conferencing
Map<String, VoiceConnection> otherConnections = new HashMap<>();
for (Map.Entry<String, VoiceConnection> entry : currentConnections.entrySet()) {
Expand Down
8 changes: 8 additions & 0 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ RNCallKeep.setup({
alertDescription: 'This application needs to access your phone accounts',
cancelButton: 'Cancel',
okButton: 'ok',
selfManaged: true,
},
});

Expand Down Expand Up @@ -107,6 +108,11 @@ export default function App() {
}, 3000);
};

const showIncomingCall = ({ callUUID }) => {
const number = calls[callUUID];
log(`[showIncomingCall] ${format(callUUID)}, number: ${number}`);
}

const answerCall = ({ callUUID }) => {
const number = calls[callUUID];
log(`[answerCall] ${format(callUUID)}, number: ${number}`);
Expand Down Expand Up @@ -197,6 +203,7 @@ export default function App() {
};

useEffect(() => {
RNCallKeep.addEventListener('showIncomingCall', showIncomingCall);
RNCallKeep.addEventListener('answerCall', answerCall);
RNCallKeep.addEventListener('didPerformDTMFAction', didPerformDTMFAction);
RNCallKeep.addEventListener('didReceiveStartCallAction', didReceiveStartCallAction);
Expand All @@ -205,6 +212,7 @@ export default function App() {
RNCallKeep.addEventListener('endCall', endCall);

return () => {
RNCallKeep.removeEventListener('showIncomingCall', showIncomingCall);
RNCallKeep.removeEventListener('answerCall', answerCall);
RNCallKeep.removeEventListener('didPerformDTMFAction', didPerformDTMFAction);
RNCallKeep.removeEventListener('didReceiveStartCallAction', didReceiveStartCallAction);
Expand Down
1 change: 1 addition & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />

<!-- These require runtime permissions on M -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Expand Down
3 changes: 3 additions & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

android.useAndroidX=true
android.enableJetifier=true
5 changes: 3 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"react-dom": "^16.8.6",
"react-native": "0.59.8",
"react-native-background-timer": "^2.1.1",
"react-native-callkeep": "3.0.7",
"react-native-callkeep": "https://github.com/luxiliu/react-native-callkeep#allow-self-managed-call",
"react-native-device-info": "^2.3.2",
"react-native-gesture-handler": "~1.3.0",
"react-native-reanimated": "~1.1.0",
Expand All @@ -20,7 +20,8 @@
"uuid": "^3.3.2"
},
"devDependencies": {
"babel-preset-expo": "^6.0.0"
"babel-preset-expo": "^6.0.0",
"jetifier": "^1.6.6"
},
"private": true
}
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare module 'react-native-callkeep' {
export type Events =
'didReceiveStartCallAction' |
'showIncomingCall' |
'answerCall' |
'endCall' |
'didActivateAudioSession' |
Expand Down Expand Up @@ -30,6 +31,7 @@ declare module 'react-native-callkeep' {
cancelButton: string,
okButton: string,
imageName?: string,
selfManaged?: boolean,
additionalPermissions: string[],
},
}
Expand Down