-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCleverTapSignedCallModule.kt
210 lines (186 loc) · 7.46 KB
/
CleverTapSignedCallModule.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
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.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
class CleverTapSignedCallModule(private val reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
private var mSignedCall: SignedCallAPI? = null
private var cleverTapAPI: CleverTapAPI? = null
private lateinit var outgoingCallResponse: OutgoingCallResponse
init {
cleverTapAPI = CleverTapAPI.getDefaultInstance(reactContext)
registerListeners(reactContext)
}
companion object {
const val NAME = "CleverTapSignedCall"
const val ERROR_CLEVERTAP_INSTANCE_NOT_INITIALIZED = "CleverTap Instance is not initialized"
}
/**
* Exports the Name of the Android module. TypeScript/Javascript part of the package used this
* name to communicate with this NativeModule class.
*/
override fun getName(): String {
return NAME
}
@ReactMethod
fun addListener(eventName: String?) {
// Keep: Required for RN built in Event Emitter Calls.
}
@ReactMethod
fun removeListeners(count: Int?) {
// Keep: Required for RN built in Event Emitter Calls.
}
/** Exports constants for Typescript or Javascript part of this package. */
override fun getConstants(): MutableMap<String, String> =
hashMapOf(
ON_CALL_STATUS_CHANGED to ON_CALL_STATUS_CHANGED,
ON_MISSED_CALL_ACTION_CLICKED to ON_MISSED_CALL_ACTION_CLICKED
)
private fun getSignedCallAPI(): SignedCallAPI {
if (mSignedCall == null) {
mSignedCall = SignedCallAPI.getInstance()
}
return mSignedCall!!
}
@SuppressLint("RestrictedApi")
private 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")
@ReactMethod
fun trackSdkVersion(sdkName: String, sdkVersion: Int) {
cleverTapAPI?.let { cleverTapAPI!!.setCustomSdkVersion(sdkName, sdkVersion) }
?: run {
log(message = "$ERROR_CLEVERTAP_INSTANCE_NOT_INITIALIZED to track the SDK Version")
}
}
@ReactMethod
fun setDebugLevel(logLevel: Int) {
SignedCallAPI.setDebugLevel(logLevel.toSignedCallLogLevel())
}
/**
* Retrieves the init-properties from the readableMap and initializes the Signed Call Android SDK
*/
@ReactMethod
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 */
@ReactMethod
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.
*/
@ReactMethod
fun getBackToCall(promise: Promise) {
promise.resolve(getSignedCallAPI().callController?.getBackToCall(reactContext))
}
/**
* Retrieves the current call state.
* @return The current call state.
*/
@ReactMethod
fun getCallState(promise: Promise) {
promise.resolve(getSignedCallAPI().callController?.callState?.formattedCallState())
}
/** Logs out the Signed Call SDK session */
@ReactMethod
fun logout() {
getSignedCallAPI().logout(reactContext)
}
/** Ends the active call, if any. */
@ReactMethod
fun hangupCall() {
getSignedCallAPI().callController?.endCall()
}
/** Disconnects the signalling socket */
@ReactMethod
fun disconnectSignallingSocket() {
getSignedCallAPI().disconnectSignallingSocket(reactContext)
}
}