-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCleverTapSignedCallModuleImpl.kt
240 lines (208 loc) · 8.21 KB
/
CleverTapSignedCallModuleImpl.kt
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
package com.clevertap.rnsignedcallandroid
import android.annotation.SuppressLint
import com.clevertap.android.sdk.CleverTapAPI
import com.clevertap.android.signedcall.exception.CallException
import com.clevertap.android.signedcall.exception.InitException
import com.clevertap.android.signedcall.init.SignedCallAPI
import com.clevertap.android.signedcall.init.SignedCallInitConfiguration
import com.clevertap.android.signedcall.interfaces.OutgoingCallResponse
import com.clevertap.android.signedcall.interfaces.SignedCallInitResponse
import com.clevertap.android.signedcall.utils.SignedCallUtils
import com.clevertap.rnsignedcallandroid.internal.Events.ON_CALL_STATUS_CHANGED
import com.clevertap.rnsignedcallandroid.internal.Events.ON_MISSED_CALL_ACTION_CLICKED
import com.clevertap.rnsignedcallandroid.internal.events.EventEmitter
import com.clevertap.rnsignedcallandroid.internal.util.InitConfigSerializer.getInitConfigFromReadableMap
import com.clevertap.rnsignedcallandroid.internal.util.PayloadConverter.formattedCallState
import com.clevertap.rnsignedcallandroid.internal.util.PayloadConverter.signedCallResponseToWritableMap
import com.clevertap.rnsignedcallandroid.internal.util.PayloadConverter.toSignedCallLogLevel
import com.clevertap.rnsignedcallandroid.internal.util.PayloadConverter.toWriteableMap
import com.clevertap.rnsignedcallandroid.internal.util.Utils.log
import com.clevertap.rnsignedcallandroid.internal.util.toJson
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.ReadableMap
class CleverTapSignedCallModuleImpl(private val reactContext: ReactApplicationContext) {
private var mSignedCall: SignedCallAPI? = null
private var cleverTapAPI: CleverTapAPI? = null
private lateinit var outgoingCallResponse: OutgoingCallResponse
companion object {
/**
* Exports the Name of the Android module. TypeScript/Javascript part of the package used this
* name to communicate with this NativeModule class.
*/
const val NAME = "CleverTapSignedCall"
const val ERROR_CLEVERTAP_INSTANCE_NOT_INITIALIZED = "CleverTap Instance is not initialized"
}
init {
cleverTapAPI = CleverTapAPI.getDefaultInstance(reactContext)
registerListeners(reactContext)
}
fun addListener(eventName: String?) {
// Keep: Required for RN built in Event Emitter Calls.
}
fun removeListeners(count: Int?) {
// Keep: Required for RN built in Event Emitter Calls.
}
private fun getSignedCallAPI(): SignedCallAPI {
if (mSignedCall == null) {
mSignedCall = SignedCallAPI.getInstance()
}
return mSignedCall!!
}
@SuppressLint("RestrictedApi")
fun registerListeners(context: ReactContext) {
if (!SignedCallUtils.isAppInBackground()) {
SignedCallAPI.getInstance().registerVoIPCallStatusListener { data ->
log(message = "SignedCallOnCallStatusListener is invoked in foreground or background: $data")
EventEmitter.emit(context, ON_CALL_STATUS_CHANGED, data.toWriteableMap())
}
SignedCallAPI.getInstance().setMissedCallNotificationOpenedHandler { _, data ->
log(message = "MissedCallNotificationOpenedHandler is invoked in foreground or background: $data")
EventEmitter.emit(context, ON_MISSED_CALL_ACTION_CLICKED, data.toWriteableMap())
}
}
}
@SuppressLint("RestrictedApi")
fun trackSdkVersion(sdkName: String, sdkVersion: Int, promise: Promise) {
cleverTapAPI?.let {
cleverTapAPI!!.setCustomSdkVersion(sdkName, sdkVersion)
promise.resolve(null)
}
?: run {
log(message = "$ERROR_CLEVERTAP_INSTANCE_NOT_INITIALIZED to track the SDK Version")
promise.reject("$ERROR_CLEVERTAP_INSTANCE_NOT_INITIALIZED to track the SDK Version")
}
}
fun setDebugLevel(logLevel: Int) {
SignedCallAPI.setDebugLevel(logLevel.toSignedCallLogLevel())
}
/**
* Retrieves the init-properties from the readableMap and initializes the Signed Call Android SDK
*/
fun initialize(initProperties: ReadableMap?, promise: Promise) {
val signedCallAPI: SignedCallAPI = getSignedCallAPI()
initProperties?.let {
try {
val initConfiguration: SignedCallInitConfiguration? =
getInitConfigFromReadableMap(it, reactContext.applicationContext)
signedCallAPI.init(
reactContext.applicationContext,
initConfiguration,
cleverTapAPI,
object : SignedCallInitResponse {
override fun onSuccess() {
promise.resolve(signedCallResponseToWritableMap(exception = null))
}
override fun onFailure(initException: InitException) {
promise.resolve(signedCallResponseToWritableMap(initException))
}
}
)
} catch (throwable: Throwable) {
val errorMessage = "Exception while initializing the Signed Call native module"
log(message = errorMessage + ": " + throwable.localizedMessage)
promise.reject(errorMessage, throwable)
}
}
}
/** Sends the call-details to initiate a VoIP call */
fun call(
receiverCuid: String,
callContext: String,
callProperties: ReadableMap?,
promise: Promise
) {
val signedCallAPI: SignedCallAPI = getSignedCallAPI()
try {
val callOptions = callProperties?.toJson()
outgoingCallResponse = object : OutgoingCallResponse {
override fun onSuccess() {
promise.resolve(signedCallResponseToWritableMap(exception = null))
}
override fun onFailure(callException: CallException?) {
promise.resolve(signedCallResponseToWritableMap(callException))
}
}
signedCallAPI.call(
reactContext,
receiverCuid,
callContext,
callOptions,
outgoingCallResponse
)
} catch (throwable: Throwable) {
val errorMessage = "Exception while initiating the VoIP Call"
log(message = errorMessage + ": " + throwable.localizedMessage)
promise.reject(errorMessage, throwable)
}
}
/**
* Attempts to return to the active call screen.
*
* This method checks if there is an active call and if the client is on VoIP call.
* If both conditions are met, it starts the call screen activity.
*/
fun getBackToCall(promise: Promise) {
promiseHandler(getSignedCallAPI().callController?.getBackToCall(reactContext), promise)
}
/**
* Retrieves the current call state.
* @return The current call state.
*/
fun getCallState(promise: Promise) {
promiseHandler(getSignedCallAPI().callController?.callState?.formattedCallState(), promise)
}
/** Logs out the Signed Call SDK session */
fun logout(promise: Promise) {
getSignedCallAPI().logout(reactContext.applicationContext)
promiseHandler(true, promise)
}
/** Ends the active call, if any. */
fun hangupCall(promise: Promise) {
promiseHandler(getSignedCallAPI().callController?.endCall(), promise)
}
/** Disconnects the signalling socket */
fun disconnectSignallingSocket(promise: Promise) {
promiseHandler(
getSignedCallAPI().disconnectSignallingSocket(reactContext.applicationContext),
promise
)
}
/** Exports constants for Typescript or Javascript part of this package. */
fun getConstants(): MutableMap<String, String> {
return mutableMapOf(
ON_CALL_STATUS_CHANGED to ON_CALL_STATUS_CHANGED,
ON_MISSED_CALL_ACTION_CLICKED to ON_MISSED_CALL_ACTION_CLICKED
)
}
/**
* Checks if the Signed Call SDK is initialized.
*
*/
fun isInitialized(promise: Promise) {
promiseHandler(getSignedCallAPI().isInitialized(reactContext.applicationContext), promise)
}
/**
* Dismisses the missed call notification.
*
* This method is intended to be called after a VoIP call use case is completed
*
*/
fun dismissMissedCallNotification(promise: Promise) {
promiseHandler(
getSignedCallAPI().dismissMissedCallNotification(reactContext.applicationContext),
promise
)
}
private fun promiseHandler(promiseFunction: Any?, promise: Promise) {
try {
when (promiseFunction) {
is Unit -> promise.resolve(null)
else -> promise.resolve(promiseFunction)
}
} catch (e: Throwable) {
promise.reject(e)
}
}
}