Skip to content

Commit 7a7830b

Browse files
committed
Allow to mute/unmute and hold/unhold a call
1 parent 8ef2532 commit 7a7830b

File tree

1 file changed

+76
-8
lines changed

1 file changed

+76
-8
lines changed

example/App.js

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ const styles = StyleSheet.create({
2020
marginTop: 20,
2121
marginBottom: 20,
2222
},
23+
callButtons: {
24+
flexDirection: 'row',
25+
justifyContent: 'space-between',
26+
paddingHorizontal: 30,
27+
width: '100%',
28+
},
2329
logContainer: {
2430
flex: 3,
2531
width: '100%',
@@ -50,17 +56,34 @@ const getRandomNumber = () => String(Math.floor(Math.random() * 100000));
5056

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

5563
const log = (text) => {
5664
console.info(text);
5765
setLog(logText + "\n" + text);
5866
};
5967

60-
const addCall = (callUUID, number) => setCalls({ ...calls, [callUUID]: number });
68+
const addCall = (callUUID, number) => {
69+
setHeldCalls({ ...heldCalls, [callUUID]: false });
70+
setCalls({ ...calls, [callUUID]: number });
71+
};
72+
6173
const removeCall = (callUUID) => {
6274
const { [callUUID]: _, ...updated } = calls;
75+
const { [callUUID]: __, ...updatedHeldCalls } = heldCalls;
76+
6377
setCalls(updated);
78+
setCalls(updatedHeldCalls);
79+
};
80+
81+
const setCallHeld = (callUUID, held) => {
82+
setHeldCalls({ ...heldCalls, [callUUID]: held });
83+
};
84+
85+
const setCallMuted = (callUUID, muted) => {
86+
setMutedCalls({ ...mutedCalls, [callUUID]: muted });
6487
};
6588

6689
const displayIncomingCall = (number) => {
@@ -87,7 +110,11 @@ export default function App() {
87110
log(`[answerCall] ${format(callUUID)}, number: ${number}`);
88111

89112
RNCallKeep.startCall(callUUID, number, number);
90-
RNCallKeep.setCurrentCallActive(callUUID);
113+
114+
BackgroundTimer.setTimeout(() => {
115+
log(`[setCurrentCallActive] ${format(callUUID)}, number: ${number}`);
116+
RNCallKeep.setCurrentCallActive(callUUID);
117+
}, 1000);
91118
};
92119

93120
const didPerformDTMFAction = ({ callUUID, digits }) => {
@@ -106,23 +133,30 @@ export default function App() {
106133
log(`[didReceiveStartCallAction] ${callUUID}, number: ${handle}`);
107134

108135
RNCallKeep.startCall(callUUID, handle, handle);
109-
RNCallKeep.setCurrentCallActive(callUUID);
136+
137+
BackgroundTimer.setTimeout(() => {
138+
log(`[setCurrentCallActive] ${format(callUUID)}, number: ${handle}`);
139+
RNCallKeep.setCurrentCallActive(callUUID);
140+
}, 1000);
110141
};
111142

112143
const didPerformSetMutedCallAction = ({ muted, callUUID }) => {
113144
const number = calls[callUUID];
114145
log(`[didPerformSetMutedCallAction] ${format(callUUID)}, number: ${number} (${muted})`);
146+
147+
setCallMuted(callUUID, muted);
115148
};
116149

117150
const didToggleHoldCallAction = ({ hold, callUUID }) => {
118151
const number = calls[callUUID];
119152
log(`[didToggleHoldCallAction] ${format(callUUID)}, number: ${number} (${hold})`);
153+
154+
setCallHeld(callUUID, hold);
120155
};
121156

122157
const endCall = ({ callUUID }) => {
123158
const handle = calls[callUUID];
124159
log(`[endCall] ${format(callUUID)}, number: ${handle}`);
125-
console.log('handle', handle);
126160

127161
removeCall(callUUID);
128162
};
@@ -132,6 +166,22 @@ export default function App() {
132166
removeCall(callUUID);
133167
};
134168

169+
const setOnHold = (callUUID, held) => {
170+
const handle = calls[callUUID];
171+
RNCallKeep.setOnHold(callUUID, held);
172+
log(`[setOnHold: ${held}] ${format(callUUID)}, number: ${handle}`);
173+
174+
setCallHeld(callUUID, held);
175+
};
176+
177+
const setOnMute = (callUUID, muted) => {
178+
const handle = calls[callUUID];
179+
RNCallKeep.setMutedCall(callUUID, muted);
180+
log(`[setMutedCall: ${muted}] ${format(callUUID)}, number: ${handle}`);
181+
182+
setCallMuted(callUUID, muted);
183+
};
184+
135185
useEffect(() => {
136186
RNCallKeep.addEventListener('answerCall', answerCall);
137187
RNCallKeep.addEventListener('didPerformDTMFAction', didPerformDTMFAction);
@@ -148,7 +198,7 @@ export default function App() {
148198
RNCallKeep.removeEventListener('didToggleHoldCallAction', didToggleHoldCallAction);
149199
RNCallKeep.removeEventListener('endCall', endCall);
150200
}
151-
});
201+
}, []);
152202

153203
if (Platform.OS === 'ios' && DeviceInfo.isEmulator()) {
154204
return <Text style={styles.container}>CallKeep doesn't work on iOS emulator</Text>;
@@ -165,9 +215,27 @@ export default function App() {
165215
</TouchableOpacity>
166216

167217
{Object.keys(calls).map(callUUID => (
168-
<TouchableOpacity key={callUUID} onPress={() => hangup(callUUID)} style={styles.button} hitSlop={hitSlop}>
169-
<Text>Hangup {calls[callUUID]}</Text>
170-
</TouchableOpacity>
218+
<View key={callUUID} style={styles.callButtons}>
219+
<TouchableOpacity
220+
onPress={() => setOnHold(callUUID, !heldCalls[callUUID])}
221+
style={styles.button}
222+
hitSlop={hitSlop}
223+
>
224+
<Text>{heldCalls[callUUID] ? 'Unhold' : 'Hold'} {calls[callUUID]}</Text>
225+
</TouchableOpacity>
226+
227+
<TouchableOpacity
228+
onPress={() => setOnMute(callUUID, !mutedCalls[callUUID])}
229+
style={styles.button}
230+
hitSlop={hitSlop}
231+
>
232+
<Text>{mutedCalls[callUUID] ? 'Unmute' : 'Mute'} {calls[callUUID]}</Text>
233+
</TouchableOpacity>
234+
235+
<TouchableOpacity onPress={() => hangup(callUUID)} style={styles.button} hitSlop={hitSlop}>
236+
<Text>Hangup {calls[callUUID]}</Text>
237+
</TouchableOpacity>
238+
</View>
171239
))}
172240

173241
<ScrollView style={styles.logContainer}>

0 commit comments

Comments
 (0)