|
| 1 | +package com.rogchap.react.modules.twilio; |
| 2 | + |
| 3 | +import com.facebook.react.bridge.ReactContextBaseJavaModule; |
| 4 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 5 | +import com.facebook.react.bridge.ReactContext; |
| 6 | +import com.facebook.react.bridge.ReadableMap; |
| 7 | +import com.facebook.react.bridge.ReactMethod; |
| 8 | + |
| 9 | +import com.twilio.client.Device; |
| 10 | +import com.twilio.client.Connection; |
| 11 | +import com.twilio.client.ConnectionListener; |
| 12 | +import com.twilio.client.Twilio; |
| 13 | + |
| 14 | +import java.net.URL; |
| 15 | +import java.net.URLConnection; |
| 16 | +import java.io.InputStream; |
| 17 | +import java.io.InputStreamReader; |
| 18 | +import java.io.BufferedReader; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | + |
| 23 | +public class TwilioModule extends ReactContextBaseJavaModule implements ConnectionListener { |
| 24 | + |
| 25 | + private ReactContext _reactContext; |
| 26 | + private Device _phone; |
| 27 | + private Connection _connection; |
| 28 | + private Connection _pendingConnection; |
| 29 | + |
| 30 | + public TwilioModule(ReactApplicationContext reactContext) { |
| 31 | + super(reactContext); |
| 32 | + _reactContext = reactContext; |
| 33 | + } |
| 34 | + |
| 35 | + private void sendEvent(String eventName, @Nullable WritableMap params) { |
| 36 | + _reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) |
| 37 | + .emit(eventName, params); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String getName() { |
| 42 | + return "Twilio"; |
| 43 | + } |
| 44 | + |
| 45 | + @ReactMethod |
| 46 | + public void initWithTokenUrl(String tokenUrl) { |
| 47 | + StringBuilder sb = new StringBuilder(); |
| 48 | + try { |
| 49 | + URLConnection conn = new URL(tokenUrl).openConnection(); |
| 50 | + InputStream in = conn.getInputStream(); |
| 51 | + BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); |
| 52 | + while ((line = reader.readLine()) != null) { |
| 53 | + sb.append(line); |
| 54 | + } |
| 55 | + } catch (Exception e) { |
| 56 | + Log.e(e.getMessage(), e); |
| 57 | + } |
| 58 | + initWithToken(sb.toString()); |
| 59 | + } |
| 60 | + |
| 61 | + @ReactMethod |
| 62 | + public void initWithToken(String token) { |
| 63 | + if (!Twilio.isInitialized()) { |
| 64 | + Twilio.initialize(getApplicationContext(), new Twilio.InitListener() { |
| 65 | + @Override |
| 66 | + public void onInitialized() { |
| 67 | + try { |
| 68 | + if (_phone == null) { |
| 69 | + _phone = Twilio.createDevice(capabilityToken, this); |
| 70 | + } |
| 71 | + } catch (Exception e) { |
| 72 | + Log.e(e.getMessage(), e); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onError(Exception e) { |
| 78 | + Log.e(e.getMessage(), e); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @ReactMethod |
| 85 | + public void connect(ReadableMap params) { |
| 86 | + if (_phone != null) { |
| 87 | + _connection = _phone.connect(params, this); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @ReactMethod |
| 92 | + public void disconnect() { |
| 93 | + if (_connection != null) { |
| 94 | + _connection.disconnect(); |
| 95 | + _connection = null; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @ReactMethod |
| 100 | + public void accept() { |
| 101 | + _pendingConnection.accept(); |
| 102 | + _pendingConnection.setConnectionListener(this); |
| 103 | + _connection = _pendingConnection; |
| 104 | + _pendingConnection = null; |
| 105 | + } |
| 106 | + |
| 107 | + @ReactMethod |
| 108 | + public void reject() { |
| 109 | + _pendingConnection.reject(); |
| 110 | + } |
| 111 | + |
| 112 | + @ReactMethod |
| 113 | + public void ignore() { |
| 114 | + _pendingConnection.ignore(); |
| 115 | + } |
| 116 | + |
| 117 | + @ReactMethod |
| 118 | + public void setMuted(Boolean isMuted) { |
| 119 | + if (_connection && _connection.getState() == Connection.State.CONNECTED) { |
| 120 | + _connection.setMuted(isMuted); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /* ConnectionListener */ |
| 125 | + |
| 126 | + @Override |
| 127 | + public void onConnecting(Connection connection) { |
| 128 | + sendEvent("connectionDidStartConnecting", connection.getParameters()); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void onConnected(Connection connection) { |
| 133 | + sendEvent("connectionDidConnect", connection.getParameters()); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void onDisconnected(Connection connection) { |
| 138 | + if (connection == _connection) { |
| 139 | + _connection = null; |
| 140 | + } |
| 141 | + if (connection == _pendingConnection) { |
| 142 | + _pendingConnection = null; |
| 143 | + } |
| 144 | + sendEvent("connectionDidDisconnect", connection.getParameters()); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void onDisconnected(Connection connection, int errorCode, String errorMessage) { |
| 149 | + Map errors = new HashMap(); |
| 150 | + errors.put("err", errorMessage); |
| 151 | + sendEvent("connectionDidFail", errors); |
| 152 | + } |
| 153 | +} |
0 commit comments