1
+ package com.raven.ravenandroidsdk
2
+
3
+ import android.app.Application
4
+ import android.content.Context
5
+ import android.content.ContextWrapper
6
+ import com.raven.ravenandroidsdk.api.ApiProvider
7
+ import com.raven.ravenandroidsdk.api.controllers.CommunicationController
8
+ import com.raven.ravenandroidsdk.api.controllers.GetUserController
9
+ import com.raven.ravenandroidsdk.api.controllers.RemoveTokenController
10
+ import com.raven.ravenandroidsdk.api.controllers.UpdateDeliveryStatus
11
+ import com.raven.ravenandroidsdk.api.controllers.UpdateTokenController
12
+ import com.raven.ravenandroidsdk.api.controllers.UpdateUserController
13
+ import com.raven.ravenandroidsdk.api.dto.DeliveryStatus
14
+ import com.raven.ravenandroidsdk.models.*
15
+ import com.raven.ravenandroidsdk.api.dto.RavenMessage
16
+ import com.raven.ravenandroidsdk.api.dto.Recipient
17
+ import com.raven.ravenandroidsdk.models.User
18
+ import com.raven.ravenandroidsdk.utils.Prefs
19
+
20
+ object RavenSdk {
21
+
22
+ const val RAVEN_NOTIFICATION_ID = " raven_notification_id"
23
+
24
+ /*
25
+ Method to initialize Raven SDK when you want to trigger raven events via SDK.
26
+ Invoke on app launch
27
+ */
28
+ @Throws(IllegalArgumentException ::class )
29
+ fun initialize (context : Application , appId : String , secretKey : String ) {
30
+ initPrefs(context, appId)
31
+ ApiProvider (context, secretKey)
32
+ fetchCurrentUser()
33
+ }
34
+
35
+
36
+ /*
37
+ Method to initialize Raven SDK when you do not want to trigger raven events via SDK.
38
+ Invoke on app launch
39
+ */
40
+ @Throws(IllegalArgumentException ::class )
41
+ fun initialize (context : Application , appId : String ) {
42
+ initPrefs(context, appId)
43
+ ApiProvider (context)
44
+ fetchCurrentUser()
45
+ }
46
+
47
+
48
+ /*
49
+ Set user identifier.
50
+ Invoke once on login to set the user identifier
51
+ */
52
+ private fun setUserId (userId : String ) {
53
+
54
+ if (userId.isEmpty()) {
55
+ return
56
+ }
57
+
58
+ // check if user id is set, or its the first time
59
+ val currentUserId = Prefs .getString(PREF_USER_ID , null )
60
+ if (currentUserId == userId) {
61
+ return
62
+ }
63
+
64
+ // user switch without logout
65
+ if (currentUserId != null && currentUserId != userId) {
66
+ logout()
67
+ }
68
+
69
+ Prefs .putString(PREF_USER_ID , userId)
70
+ }
71
+
72
+
73
+ /*
74
+ Set user mobile
75
+ */
76
+ fun setUserMobile (userId : String , mobile : String ) {
77
+
78
+ // throws exception if uninitialized
79
+ checkIfSDKInitialized()
80
+
81
+ // set user id
82
+ setUserId(userId)
83
+
84
+ if (mobile.isEmpty() || userId.isEmpty()) {
85
+ return
86
+ }
87
+
88
+ // check if cached mobile is same as the argument
89
+ val currentUser = getCurrentUser()
90
+ if (currentUser?.mobile == mobile) {
91
+ return
92
+ }
93
+
94
+ // Check for currently set user id
95
+ val user = User (userId = userId, mobile = mobile)
96
+ UpdateUserController ().start(Prefs .getString(PREF_APP_ID , null ) ? : return , user)
97
+ }
98
+
99
+
100
+ /*
101
+ Set user
102
+ */
103
+ fun setUser (userId : String , mobile : String? = null, email : String? = null) {
104
+
105
+ // throws exception if uninitialized
106
+ checkIfSDKInitialized()
107
+
108
+ // set user id
109
+ setUserId(userId)
110
+
111
+ if (userId.isEmpty() || ((mobile == null || mobile.isEmpty()) && (email == null || email.isEmpty()))) {
112
+ return
113
+ }
114
+
115
+ // check if cached mobile/email is same as the argument
116
+ val currentUser = getCurrentUser()
117
+ if (currentUser?.mobile == mobile && currentUser?.email == email) {
118
+ return
119
+ }
120
+
121
+ val user = User (userId = userId, mobile = mobile, email = email)
122
+ UpdateUserController ().start(Prefs .getString(PREF_APP_ID , null ) ? : return , user)
123
+ }
124
+
125
+
126
+ /*
127
+ Set user email
128
+ */
129
+ fun setUserEmail (userId : String , email : String ) {
130
+
131
+ // throws exception if uninitialized
132
+ checkIfSDKInitialized()
133
+
134
+ // set user id
135
+ setUserId(userId)
136
+
137
+ if (email.isEmpty() || userId.isEmpty()) {
138
+ return
139
+ }
140
+
141
+ // check if cached mobile is same as the argument
142
+ val currentUser = getCurrentUser()
143
+ if (currentUser?.email == email) {
144
+ return
145
+ }
146
+
147
+ val user = User (userId = userId, email = email)
148
+ UpdateUserController ().start(Prefs .getString(PREF_APP_ID , null ) ? : return , user)
149
+ }
150
+
151
+
152
+ /*
153
+ Set user email
154
+ */
155
+ fun setDeviceToken (token : String ) {
156
+
157
+ // throws exception if uninitialized
158
+ checkIfSDKInitialized()
159
+
160
+ if (token.isEmpty()) {
161
+ return
162
+ }
163
+
164
+ // check if cached token is same as the argument
165
+ val currentUser = getCurrentUser()
166
+ var isUpdate = false
167
+ for (item in currentUser?.devices ? : arrayListOf ()) {
168
+ if (item.fcmToken == token && item.platform == PLATFORM ) {
169
+ return
170
+ }
171
+
172
+ if (item.deviceSid == Prefs .getString(PREF_USER_DEVICE_ID , null )) {
173
+ if (item.fcmToken != token && item.platform == PLATFORM ) {
174
+ isUpdate = true
175
+ }
176
+ }
177
+ }
178
+
179
+ val userId = Prefs .getString(PREF_USER_ID , null )
180
+ if (userId != null ) {
181
+ UpdateTokenController ().start(Prefs .getString(PREF_APP_ID , null ) ? : return , userId, token, isUpdate)
182
+ }
183
+ }
184
+
185
+
186
+ /*
187
+ Logout user
188
+ */
189
+ fun logout () {
190
+
191
+ // de register the device
192
+ val userId = Prefs .getString(PREF_USER_ID , null )
193
+ if (userId != null ) {
194
+ RemoveTokenController ().start(Prefs .getString(PREF_APP_ID , null ) ? : return , userId)
195
+ }
196
+
197
+ // reset the preferences
198
+ Prefs .putString(PREF_USER_ID , null )
199
+ Prefs .putString(PREF_RAVEN_USER , null )
200
+ }
201
+
202
+
203
+ /*
204
+ Update message delivery status
205
+ */
206
+ fun updateStatus (notificationId : String , status : Status ) {
207
+ val deliveryStatus = DeliveryStatus (notificationId = notificationId, type = status)
208
+ UpdateDeliveryStatus ().start(Prefs .getString(PREF_APP_ID , null ) ? : return , deliveryStatus)
209
+ }
210
+
211
+
212
+ /*
213
+ Method to get the currently active user from raven backend
214
+ */
215
+ private fun fetchCurrentUser () {
216
+
217
+ // throws exception if uninitialized
218
+ checkIfSDKInitialized()
219
+
220
+ // Check for currently set user id
221
+ val userId = Prefs .getString(PREF_USER_ID , null )
222
+
223
+ // if present, get the user from raven backend
224
+ if (userId != null && userId.isNotEmpty()) {
225
+ GetUserController ().start(Prefs .getString(PREF_APP_ID , null ) ? : return , userId)
226
+ }
227
+ }
228
+
229
+
230
+ private fun initPrefs (context : Context ? , appId : String ) {
231
+
232
+ if (appId.isEmpty()) {
233
+ throw IllegalArgumentException (" App ID cannot be empty" )
234
+ }
235
+
236
+ // Initialize Prefs lib
237
+ Prefs .init (context, context?.packageName + " _" + " raven" , ContextWrapper .MODE_PRIVATE )
238
+
239
+ Prefs .putString(PREF_APP_ID , appId)
240
+ }
241
+
242
+
243
+ private fun checkIfSDKInitialized () {
244
+ if (Prefs .getString(PREF_APP_ID , null ) == null ) {
245
+ throw IllegalStateException (" Raven Client SDK not initialized" )
246
+ }
247
+ }
248
+
249
+
250
+ @Throws(IllegalStateException ::class )
251
+ fun sendMessage (template : String , id : String , callback : RavenResponseCallback ? ) {
252
+
253
+ checkIfSDKInitialized()
254
+
255
+ val message = RavenMessage ()
256
+ message.event = template
257
+
258
+ val recipient = Recipient ()
259
+ recipient.userId = id
260
+ message.user = recipient
261
+
262
+ // api call
263
+ Prefs .getString(PREF_APP_ID , null )?.let { CommunicationController (callback).start(it, message) }
264
+ }
265
+
266
+
267
+ interface RavenResponseCallback {
268
+ fun onSuccess ()
269
+ fun onFailure (error : String? )
270
+ }
271
+ }
0 commit comments