Skip to content

Add example (follows #64) #77

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

Merged
merged 3 commits into from
Aug 14, 2019
Merged
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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016-2018 The CallKeep Authors (see the AUTHORS file)
Copyright (c) 2016-2019 The CallKeep Authors (see the AUTHORS file)

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ RNCallKeep.addEventListener('didPerformDTMFAction', ({ digits, callUUID }) => {

## Example

A full example is available in the [wazo-react-native-demo](https://github.com/wazo-pbx/wazo-react-native-demo) repository.
A full example is available in the [example](https://github.com/react-native-webrtc/react-native-callkeep/tree/master/example) folder.

```javascript
import React from 'react';
Expand Down Expand Up @@ -584,10 +584,6 @@ class RNCallKeepExample extends React.Component {
}
```

## Notes

- Call setup once to initiate callkeep.

## Debug

### Android
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2018 The CallKeep Authors (see the AUTHORS file)
* Copyright (c) 2016-2019 The CallKeep Authors (see the AUTHORS file)
* SPDX-License-Identifier: ISC, MIT
*
* Permission to use, copy, modify, and distribute this software for any
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2018 The CallKeep Authors (see the AUTHORS file)
* Copyright (c) 2016-2019 The CallKeep Authors (see the AUTHORS file)
* SPDX-License-Identifier: ISC, MIT
*
* Permission to use, copy, modify, and distribute this software for any
Expand Down
17 changes: 17 additions & 0 deletions android/src/main/java/io/wazo/callkeep/VoiceConference.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright (c) 2016-2019 The CallKeep Authors (see the AUTHORS file)
* SPDX-License-Identifier: ISC, MIT
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package io.wazo.callkeep;

import android.telecom.Conference;
Expand Down
17 changes: 17 additions & 0 deletions android/src/main/java/io/wazo/callkeep/VoiceConnection.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright (c) 2016-2019 The CallKeep Authors (see the AUTHORS file)
* SPDX-License-Identifier: ISC, MIT
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package io.wazo.callkeep;

import android.annotation.TargetApi;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2018 The CallKeep Authors (see the AUTHORS file)
* Copyright (c) 2016-2019 The CallKeep Authors (see the AUTHORS file)
* SPDX-License-Identifier: ISC, MIT
*
* Permission to use, copy, modify, and distribute this software for any
Expand Down
13 changes: 13 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules/**/*
.expo/*
npm-debug.*
*.jks
*.p8
*.p12
*.key
*.mobileprovision
*.orig.*
web-build/
web-report/

ios/Pods/
1 change: 1 addition & 0 deletions example/.watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
248 changes: 248 additions & 0 deletions example/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
import React, { useState, useEffect } from 'react';
import { Platform, StyleSheet, Text, View, TouchableOpacity, ScrollView } from 'react-native';
import uuid from 'uuid';
import RNCallKeep from 'react-native-callkeep';
import BackgroundTimer from 'react-native-background-timer';
import DeviceInfo from 'react-native-device-info';

BackgroundTimer.start();

const hitSlop = { top: 10, left: 10, right: 10, bottom: 10};
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 20,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
button: {
marginTop: 20,
marginBottom: 20,
},
callButtons: {
flexDirection: 'row',
justifyContent: 'space-between',
paddingHorizontal: 30,
width: '100%',
},
logContainer: {
flex: 3,
width: '100%',
backgroundColor: '#D9D9D9',
},
log: {
fontSize: 10,
}
});

RNCallKeep.setup({
ios: {
appName: 'CallKeepDemo',
},
android: {
alertTitle: 'Permissions required',
alertDescription: 'This application needs to access your phone accounts',
cancelButton: 'Cancel',
okButton: 'ok',
},
});

const getNewUuid = () => uuid.v4().toLowerCase();

const format = uuid => uuid.split('-')[0];

const getRandomNumber = () => String(Math.floor(Math.random() * 100000));

export default function App() {
const [logText, setLog] = useState('');
const [heldCalls, setHeldCalls] = useState({}); // callKeep uuid: held
const [mutedCalls, setMutedCalls] = useState({}); // callKeep uuid: muted
const [calls, setCalls] = useState({}); // callKeep uuid: number

const log = (text) => {
console.info(text);
setLog(logText + "\n" + text);
};

const addCall = (callUUID, number) => {
setHeldCalls({ ...heldCalls, [callUUID]: false });
setCalls({ ...calls, [callUUID]: number });
};

const removeCall = (callUUID) => {
const { [callUUID]: _, ...updated } = calls;
const { [callUUID]: __, ...updatedHeldCalls } = heldCalls;

setCalls(updated);
setCalls(updatedHeldCalls);
};

const setCallHeld = (callUUID, held) => {
setHeldCalls({ ...heldCalls, [callUUID]: held });
};

const setCallMuted = (callUUID, muted) => {
setMutedCalls({ ...mutedCalls, [callUUID]: muted });
};

const displayIncomingCall = (number) => {
const callUUID = getNewUuid();
addCall(callUUID, number);

log(`[displayIncomingCall] ${format(callUUID)}, number: ${number}`);

RNCallKeep.displayIncomingCall(callUUID, number, number, 'number', false);
};

const displayIncomingCallNow = () => {
displayIncomingCall(getRandomNumber());
};

const displayIncomingCallDelayed = () => {
BackgroundTimer.setTimeout(() => {
displayIncomingCall(getRandomNumber());
}, 3000);
};

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

RNCallKeep.startCall(callUUID, number, number);

BackgroundTimer.setTimeout(() => {
log(`[setCurrentCallActive] ${format(callUUID)}, number: ${number}`);
RNCallKeep.setCurrentCallActive(callUUID);
}, 1000);
};

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

const didReceiveStartCallAction = ({ handle }) => {
if (!handle) {
// @TODO: sometime we receive `didReceiveStartCallAction` with handle` undefined`
return;
}
const callUUID = getNewUuid();
addCall(callUUID, handle);

log(`[didReceiveStartCallAction] ${callUUID}, number: ${handle}`);

RNCallKeep.startCall(callUUID, handle, handle);

BackgroundTimer.setTimeout(() => {
log(`[setCurrentCallActive] ${format(callUUID)}, number: ${handle}`);
RNCallKeep.setCurrentCallActive(callUUID);
}, 1000);
};

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

setCallMuted(callUUID, muted);
};

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

setCallHeld(callUUID, hold);
};

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

removeCall(callUUID);
};

const hangup = (callUUID) => {
RNCallKeep.endCall(callUUID);
removeCall(callUUID);
};

const setOnHold = (callUUID, held) => {
const handle = calls[callUUID];
RNCallKeep.setOnHold(callUUID, held);
log(`[setOnHold: ${held}] ${format(callUUID)}, number: ${handle}`);

setCallHeld(callUUID, held);
};

const setOnMute = (callUUID, muted) => {
const handle = calls[callUUID];
RNCallKeep.setMutedCall(callUUID, muted);
log(`[setMutedCall: ${muted}] ${format(callUUID)}, number: ${handle}`);

setCallMuted(callUUID, muted);
};

useEffect(() => {
RNCallKeep.addEventListener('answerCall', answerCall);
RNCallKeep.addEventListener('didPerformDTMFAction', didPerformDTMFAction);
RNCallKeep.addEventListener('didReceiveStartCallAction', didReceiveStartCallAction);
RNCallKeep.addEventListener('didPerformSetMutedCallAction', didPerformSetMutedCallAction);
RNCallKeep.addEventListener('didToggleHoldCallAction', didToggleHoldCallAction);
RNCallKeep.addEventListener('endCall', endCall);

return () => {
RNCallKeep.removeEventListener('answerCall', answerCall);
RNCallKeep.removeEventListener('didPerformDTMFAction', didPerformDTMFAction);
RNCallKeep.removeEventListener('didReceiveStartCallAction', didReceiveStartCallAction);
RNCallKeep.removeEventListener('didPerformSetMutedCallAction', didPerformSetMutedCallAction);
RNCallKeep.removeEventListener('didToggleHoldCallAction', didToggleHoldCallAction);
RNCallKeep.removeEventListener('endCall', endCall);
}
}, []);

if (Platform.OS === 'ios' && DeviceInfo.isEmulator()) {
return <Text style={styles.container}>CallKeep doesn't work on iOS emulator</Text>;
}

return (
<View style={styles.container}>
<TouchableOpacity onPress={displayIncomingCallNow} style={styles.button} hitSlop={hitSlop}>
<Text>Display incoming call now</Text>
</TouchableOpacity>

<TouchableOpacity onPress={displayIncomingCallDelayed} style={styles.button} hitSlop={hitSlop}>
<Text>Display incoming call now in 3s</Text>
</TouchableOpacity>

{Object.keys(calls).map(callUUID => (
<View key={callUUID} style={styles.callButtons}>
<TouchableOpacity
onPress={() => setOnHold(callUUID, !heldCalls[callUUID])}
style={styles.button}
hitSlop={hitSlop}
>
<Text>{heldCalls[callUUID] ? 'Unhold' : 'Hold'} {calls[callUUID]}</Text>
</TouchableOpacity>

<TouchableOpacity
onPress={() => setOnMute(callUUID, !mutedCalls[callUUID])}
style={styles.button}
hitSlop={hitSlop}
>
<Text>{mutedCalls[callUUID] ? 'Unmute' : 'Mute'} {calls[callUUID]}</Text>
</TouchableOpacity>

<TouchableOpacity onPress={() => hangup(callUUID)} style={styles.button} hitSlop={hitSlop}>
<Text>Hangup {calls[callUUID]}</Text>
</TouchableOpacity>
</View>
))}

<ScrollView style={styles.logContainer}>
<Text style={styles.log}>
{logText}
</Text>
</ScrollView>
</View>
);
}
33 changes: 33 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# CallKeep example



## How to install it

```
# Install dependancies
yarn install

cd ios
pod install
```

## How to use it

```
# Start metro bundler
yarn start

# Start the application (in another term)
yarn android # or yarn ios
```


## How this example was setted up

```sh
expo init CallKeepDemo
expo eject
yarn add react-native-callkeep
./node_modules/.bin/react-native link react-native-callkeep
```
Loading