forked from react-native-webrtc/react-native-incall-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
170 lines (144 loc) · 5.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict';
var _InCallManager = require('react-native').NativeModules.InCallManager;
import {
Platform,
Vibration,
} from 'react-native';
class InCallManager {
constructor() {
this.vibrate = false;
this.audioUriMap = {
ringtone: { _BUNDLE_: null, _DEFAULT_: null},
ringback: { _BUNDLE_: null, _DEFAULT_: null},
busytone: { _BUNDLE_: null, _DEFAULT_: null},
};
}
start(setup) {
setup = (setup === undefined) ? {} : setup;
let auto = (setup.auto === false) ? false : true;
let media = (setup.media === 'video') ? 'video' : 'audio';
let ringback = (!!setup.ringback) ? (typeof setup.ringback === 'string') ? setup.ringback : "" : "";
_InCallManager.start(media, auto, ringback);
}
stop(setup) {
setup = (setup === undefined) ? {} : setup;
let busytone = (!!setup.busytone) ? (typeof setup.busytone === 'string') ? setup.busytone : "" : "";
_InCallManager.stop(busytone);
}
turnScreenOff() {
_InCallManager.turnScreenOff();
}
turnScreenOn() {
_InCallManager.turnScreenOn();
}
async getIsWiredHeadsetPluggedIn() {
let isPluggedIn = await _InCallManager.getIsWiredHeadsetPluggedIn();
return { isWiredHeadsetPluggedIn: isPluggedIn };
}
setFlashOn(enable, brightness) {
if (Platform.OS === 'ios') {
enable = (enable === true) ? true : false;
brightness = (typeof brightness === 'number') ? brightness : 0;
_InCallManager.setFlashOn(enable, brightness);
} else {
console.log("Android doesn't support setFlashOn(enable, brightness)");
}
}
setKeepScreenOn(enable) {
enable = (enable === true) ? true : false;
_InCallManager.setKeepScreenOn(enable);
}
setSpeakerphoneOn(enable) {
enable = (enable === true) ? true : false;
_InCallManager.setSpeakerphoneOn(enable);
}
setForceSpeakerphoneOn(_flag) {
let flag = (typeof _flag === "boolean") ? (_flag) ? 1 : -1 : 0;
_InCallManager.setForceSpeakerphoneOn(flag);
}
setMicrophoneMute(enable) {
enable = (enable === true) ? true : false;
_InCallManager.setMicrophoneMute(enable);
}
startRingtone(ringtone, vibrate_pattern, ios_category, seconds) {
ringtone = (typeof ringtone === 'string') ? ringtone : "_DEFAULT_";
this.vibrate = (Array.isArray(vibrate_pattern)) ? true : false;
ios_category = (ios_category === 'playback') ? 'playback' : "default";
seconds = (typeof seconds === 'number' && seconds > 0) ? parseInt(seconds) : -1; // --- android only, default looping
if (Platform.OS === 'android') {
_InCallManager.startRingtone(ringtone, seconds);
} else {
_InCallManager.startRingtone(ringtone, ios_category);
}
// --- should not use repeat, it may cause infinite loop in some cases.
if (this.vibrate) {
Vibration.vibrate(vibrate_pattern, false); // --- ios needs RN 0.34 to support vibration pattern
}
}
stopRingtone() {
if (this.vibrate) {
Vibration.cancel();
}
_InCallManager.stopRingtone();
}
startProximitySensor() {
_InCallManager.startProximitySensor();
}
stopProximitySensor() {
_InCallManager.stopProximitySensor();
}
startRingback(ringback) {
ringback = (typeof ringback === 'string') ? ringback : "_DTMF_";
_InCallManager.startRingback(ringback);
}
stopRingback() {
_InCallManager.stopRingback();
}
pokeScreen(_timeout) {
if (Platform.OS === 'android') {
let timeout = (typeof _timeout === "number" && _timeout > 0) ? _timeout : 3000; // --- default 3000 ms
_InCallManager.pokeScreen(timeout);
} else {
console.log("ios doesn't support pokeScreen()");
}
}
async getAudioUri(audioType, fileType) {
if (typeof this.audioUriMap[audioType] === "undefined") {
return null;
}
if (this.audioUriMap[audioType][fileType]) {
return this.audioUriMap[audioType][fileType];
} else {
try {
let result = await _InCallManager.getAudioUriJS(audioType, fileType);
if (typeof result === 'string' && result.length > 0) {
this.audioUriMap[audioType][fileType] = result;
return result
} else {
return null;
}
} catch (err) {
return null;
}
}
}
async chooseAudioRoute(route) {
let result = await _InCallManager.chooseAudioRoute(route);
return result;
}
async requestAudioFocus() {
if (Platform.OS === 'android') {
return await _InCallManager.requestAudioFocusJS();
} else {
console.log("ios doesn't support requestAudioFocus()");
}
}
async abandonAudioFocus() {
if (Platform.OS === 'android') {
return await _InCallManager.abandonAudioFocusJS();
} else {
console.log("ios doesn't support requestAudioFocus()");
}
}
}
export default new InCallManager();