-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathRNStringeeClientModule.java
More file actions
executable file
·264 lines (229 loc) · 9.71 KB
/
RNStringeeClientModule.java
File metadata and controls
executable file
·264 lines (229 loc) · 9.71 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package com.stringeereactnative;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.stringee.StringeeClient;
import com.stringee.call.StringeeCall;
import com.stringee.call.StringeeCall2;
import com.stringee.exception.StringeeError;
import com.stringee.listener.StatusListener;
import com.stringee.listener.StringeeConnectionListener;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class RNStringeeClientModule extends ReactContextBaseJavaModule implements StringeeConnectionListener {
private StringeeManager mStringeeManager;
private StringeeClient mClient;
private ArrayList<String> jsEvents = new ArrayList<String>();
public RNStringeeClientModule(ReactApplicationContext context) {
super(context);
mStringeeManager = StringeeManager.getInstance();
}
@Override
public String getName() {
return "RNStringeeClient";
}
@ReactMethod
public void init() {
mClient = mStringeeManager.getClient();
if (mClient == null) {
mClient = new StringeeClient(getReactApplicationContext());
}
mClient.setConnectionListener(this);
mStringeeManager.setClient(mClient);
}
@ReactMethod
public void connect(String accessToken) {
if (mClient.isConnected()) {
if (contains(jsEvents, "onConnectionConnected")) {
WritableMap params = Arguments.createMap();
params.putString("userId", mClient.getUserId());
params.putInt("projectId", mClient.getProjectId());
params.putBoolean("isReconnecting", false);
sendEvent(getReactApplicationContext(), "onConnectionConnected", params);
}
} else {
mClient.connect(accessToken);
}
}
@ReactMethod
public void disconnect() {
if (mClient != null) {
mClient.disconnect();
}
}
@ReactMethod
public void registerPushToken(String token, final Callback callback) {
if (mClient == null) {
callback.invoke(false, -1, "StringeeClient is not initialized or connected");
return;
}
mClient.registerPushToken(token, new StatusListener() {
@Override
public void onSuccess() {
callback.invoke(true, 0, "Success");
}
@Override
public void onError(StringeeError error) {
callback.invoke(false, error.getCode(), error.getMessage());
}
});
}
@ReactMethod
public void unregisterPushToken(final String token, final Callback callback) {
if (mClient == null) {
callback.invoke(false, -1, "StringeeClient is not initialized or connected");
return;
}
mClient.unregisterPushToken(token, new StatusListener() {
@Override
public void onSuccess() {
callback.invoke(true, 0, "Success");
}
@Override
public void onError(StringeeError error) {
callback.invoke(false, error.getCode(), error.getMessage());
}
});
}
@ReactMethod
public void sendCustomMessage(String toUser, String msg, final Callback callback) {
if (mClient == null) {
callback.invoke(false, -1, "StringeeClient is not initialized or connected");
return;
}
try {
JSONObject jsonObject = new JSONObject(msg);
mClient.sendCustomMessage(toUser, jsonObject, new StatusListener() {
@Override
public void onSuccess() {
callback.invoke(true, 0, "Success");
}
@Override
public void onError(StringeeError error) {
callback.invoke(false, error.getCode(), error.getMessage());
}
});
} catch (JSONException e) {
e.printStackTrace();
callback.invoke(false, -2, "Message is not not in JSON format");
}
}
@Override
public void onConnectionConnected(StringeeClient stringeeClient, boolean b) {
if (contains(jsEvents, "onConnectionConnected")) {
WritableMap params = Arguments.createMap();
params.putString("userId", stringeeClient.getUserId());
params.putInt("projectId", stringeeClient.getProjectId());
params.putBoolean("isReconnecting", b);
sendEvent(getReactApplicationContext(), "onConnectionConnected", params);
}
}
@Override
public void onConnectionDisconnected(StringeeClient stringeeClient, boolean b) {
if (contains(jsEvents, "onConnectionDisconnected")) {
WritableMap params = Arguments.createMap();
params.putString("userId", stringeeClient.getUserId());
params.putInt("projectId", stringeeClient.getProjectId());
params.putBoolean("isReconnecting", b);
sendEvent(getReactApplicationContext(), "onConnectionDisconnected", params);
}
}
@Override
public void onIncomingCall(StringeeCall stringeeCall) {
if (contains(jsEvents, "onIncomingCall")) {
StringeeManager.getInstance().getCallsMap().put(stringeeCall.getCallId(), stringeeCall);
WritableMap params = Arguments.createMap();
if (mClient != null) {
params.putString("userId", mClient.getUserId());
}
params.putString("callId", stringeeCall.getCallId());
params.putString("from", stringeeCall.getFrom());
params.putString("to", stringeeCall.getTo());
params.putString("fromAlias", stringeeCall.getFromAlias());
params.putString("toAlias", stringeeCall.getToAlias());
int callType = 1;
if (stringeeCall.isPhoneToAppCall()) {
callType = 3;
}
params.putInt("callType", callType);
params.putBoolean("isVideoCall", stringeeCall.isVideoCall());
params.putString("customDataFromYourServer", stringeeCall.getCustomDataFromYourServer());
sendEvent(getReactApplicationContext(), "onIncomingCall", params);
}
}
@Override
public void onIncomingCall2(StringeeCall2 stringeeCall) {
if (contains(jsEvents, "onIncomingCall2")) {
StringeeManager.getInstance().getCalls2Map().put(stringeeCall.getCallId(), stringeeCall);
WritableMap params = Arguments.createMap();
if (mClient != null) {
params.putString("userId", mClient.getUserId());
}
params.putString("callId", stringeeCall.getCallId());
params.putString("from", stringeeCall.getFrom());
params.putString("to", stringeeCall.getTo());
params.putString("fromAlias", stringeeCall.getFromAlias());
params.putString("toAlias", stringeeCall.getToAlias());
int callType = 2;
params.putInt("callType", callType);
params.putBoolean("isVideoCall", stringeeCall.isVideoCall());
params.putString("customDataFromYourServer", stringeeCall.getCustomDataFromYourServer());
sendEvent(getReactApplicationContext(), "onIncomingCall2", params);
}
}
@Override
public void onConnectionError(StringeeClient stringeeClient, StringeeError stringeeError) {
if (contains(jsEvents, "onConnectionError")) {
WritableMap params = Arguments.createMap();
params.putInt("code", stringeeError.getCode());
params.putString("message", stringeeError.getMessage());
sendEvent(getReactApplicationContext(), "onConnectionError", params);
}
}
@Override
public void onRequestNewToken(StringeeClient stringeeClient) {
if (contains(jsEvents, "onRequestNewToken")) {
sendEvent(getReactApplicationContext(), "onRequestNewToken", null);
}
}
@Override
public void onCustomMessage(String s, JSONObject jsonObject) {
if (contains(jsEvents, "onCustomMessage")) {
WritableMap params = Arguments.createMap();
params.putString("from", s);
params.putString("data", jsonObject.toString());
sendEvent(getReactApplicationContext(), "onCustomMessage", params);
}
}
@Override
public void onTopicMessage(String s, JSONObject jsonObject) {
}
private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap eventData) {
reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, eventData);
}
@ReactMethod
public void setNativeEvent(String event) {
jsEvents.add(event);
}
@ReactMethod
public void removeNativeEvent(String event) {
jsEvents.remove(event);
}
private boolean contains(ArrayList array, String value) {
for (int i = 0; i < array.size(); i++) {
if (array.get(i).equals(value)) {
return true;
}
}
return false;
}
}