-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from CleverTap/develop
Update master with release 3.0.0
- Loading branch information
Showing
40 changed files
with
4,279 additions
and
2,054 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1 @@ | ||
pluginManagement { | ||
repositories { | ||
google() | ||
mavenCentral() | ||
} | ||
} | ||
|
||
plugins { | ||
id("com.android.library").version("8.3.0").apply(false) | ||
} | ||
|
||
rootProject.name = 'clevertap_plugin' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
android/src/main/java/com/clevertap/clevertap_plugin/CleverTapEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.clevertap.clevertap_plugin | ||
|
||
enum class CleverTapEvent( | ||
val eventName: String, | ||
val bufferable: Boolean = false | ||
) { | ||
|
||
CLEVERTAP_PROFILE_DID_INITIALIZE("CleverTapProfileDidInitialize", bufferable = true), | ||
CLEVERTAP_PROFILE_SYNC("CleverTapProfileSync"), | ||
CLEVERTAP_IN_APP_NOTIFICATION_DISMISSED("CleverTapInAppNotificationDismissed", bufferable = true), | ||
CLEVERTAP_IN_APP_NOTIFICATION_BEFORE_SHOW("CleverTapInAppNotificationBeforeShow", bufferable = true), | ||
CLEVERTAP_IN_APP_NOTIFICATION_SHOWED("CleverTapInAppNotificationShowed", bufferable = true), | ||
CLEVERTAP_INBOX_DID_INITIALIZE("CleverTapInboxDidInitialize", bufferable = true), | ||
CLEVERTAP_INBOX_MESSAGES_DID_UPDATE("CleverTapInboxMessagesDidUpdate"), | ||
CLEVERTAP_ON_INBOX_BUTTON_CLICK("CleverTapInboxMessageButtonTapped"), | ||
CLEVERTAP_ON_INBOX_MESSAGE_CLICK("CleverTapInboxMessageTapped"), | ||
CLEVERTAP_ON_INAPP_BUTTON_CLICK("CleverTapInAppNotificationButtonTapped", bufferable = true), | ||
CLEVERTAP_ON_DISPLAY_UNITS_LOADED("CleverTapDisplayUnitsLoaded", bufferable = true), | ||
CLEVERTAP_FEATURE_FLAGS_DID_UPDATE("CleverTapFeatureFlagsDidUpdate", bufferable = true), | ||
CLEVERTAP_PRODUCT_CONFIG_DID_INITIALIZE("CleverTapProductConfigDidInitialize", bufferable = true), | ||
CLEVERTAP_PRODUCT_CONFIG_DID_FETCH("CleverTapProductConfigDidFetch"), | ||
CLEVERTAP_PRODUCT_CONFIG_DID_ACTIVATE("CleverTapProductConfigDidActivate"), | ||
CLEVERTAP_PUSH_NOTIFICATION_CLICKED("CleverTapPushNotificationClicked", bufferable = true), | ||
CLEVERTAP_ON_PUSH_PERMISSION_RESPONSE("CleverTapPushPermissionResponseReceived"), | ||
CLEVERTAP_ON_PUSH_AMP_PAYLOAD_RECEIVED("CleverTapPushAmpPayloadReceived", bufferable = true), | ||
CLEVERTAP_ON_VARIABLES_CHANGED("CleverTapOnVariablesChanged"), | ||
CLEVERTAP_ON_ONE_TIME_VARIABLES_CHANGED("CleverTapOnOneTimeVariablesChanged"), | ||
CLEVERTAP_ON_VALUE_CHANGED("CleverTapOnValueChanged"), | ||
CLEVERTAP_CUSTOM_TEMPLATE_PRESENT("CleverTapCustomTemplatePresent", bufferable = true), | ||
CLEVERTAP_CUSTOM_FUNCTION_PRESENT("CleverTapCustomFunctionPresent", bufferable = true), | ||
CLEVERTAP_CUSTOM_TEMPLATE_CLOSE("CleverTapCustomTemplateClose"), | ||
CLEVERTAP_ON_FILE_VALUE_CHANGED("CleverTapOnFileValueChanged"), | ||
CLEVERTAP_ON_VARIABLES_CHANGED_AND_NO_DOWNLOADS_PENDING("CleverTapOnVariablesChangedAndNoDownloadsPending"), | ||
CLEVERTAP_ONCE_VARIABLES_CHANGED_AND_NO_DOWNLOADS_PENDING("CleverTapOnceVariablesChangedAndNoDownloadsPending"), | ||
CLEVERTAP_UNKNOWN("CleverTapUnknown"); | ||
|
||
override fun toString(): String { | ||
return eventName | ||
} | ||
|
||
companion object { | ||
|
||
@JvmStatic | ||
fun fromName(eventName: String): CleverTapEvent { | ||
return values().find { it.eventName == eventName } ?: CLEVERTAP_UNKNOWN | ||
} | ||
} | ||
} | ||
|
145 changes: 145 additions & 0 deletions
145
android/src/main/java/com/clevertap/clevertap_plugin/CleverTapEventEmitter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package com.clevertap.clevertap_plugin | ||
|
||
import android.util.Log | ||
import com.clevertap.clevertap_plugin.CleverTapPlugin.invokeMethodOnUiThread | ||
import java.util.LinkedList | ||
import java.util.Queue | ||
|
||
/** | ||
* CleverTapEventEmitter is responsible for emitting events to the React Native JavaScript layer. | ||
* It manages event buffers and allows events to be queued which is useful when needing to send an | ||
* event while the ReactContext of the application is not yet initialized. Only events specified as | ||
* [CleverTapEvent.bufferable] will be considered for buffering, all other events will be emitted | ||
* immediately. | ||
*/ | ||
object CleverTapEventEmitter { | ||
private const val LOG_TAG = "CleverTapEventEmitter" | ||
|
||
private var eventsBuffers: Map<CleverTapEvent, Buffer> = createBuffersMap(enableBuffers = true) | ||
|
||
/** | ||
* Enable buffering for the specified event. While enabled, all events which should be emitted | ||
* will be added to the buffer. Only buffers for events specified as [CleverTapEvent.bufferable] | ||
* can be enabled. | ||
* | ||
* @see [disableBuffer] | ||
*/ | ||
@JvmStatic | ||
fun enableBuffer(event: CleverTapEvent) { | ||
eventsBuffers[event]?.enabled = true | ||
} | ||
|
||
/** | ||
* Disable buffering for the specified event. This will only have effect for events specified as | ||
* [CleverTapEvent.bufferable]. | ||
* | ||
* @see [enableBuffer] | ||
*/ | ||
@JvmStatic | ||
fun disableBuffer(event: CleverTapEvent) { | ||
eventsBuffers[event]?.enabled = false | ||
} | ||
|
||
/** | ||
* Clear all the buffered events from all buffers and set whether all buffers should be enabled | ||
* after that. | ||
* | ||
* @param enableBuffers enable/disable all buffers after they are cleared | ||
*/ | ||
@JvmStatic | ||
fun resetAllBuffers(enableBuffers: Boolean) { | ||
eventsBuffers = createBuffersMap(enableBuffers) | ||
Log.i(LOG_TAG, "Buffers reset and enabled: $enableBuffers") | ||
} | ||
|
||
/** | ||
* Emit all currently buffered events for the specified event name. | ||
*/ | ||
@JvmStatic | ||
fun flushBuffer(event: CleverTapEvent) { | ||
val buffer = eventsBuffers[event] ?: return | ||
while (buffer.size() > 0) { | ||
val params = buffer.remove() | ||
sendEvent(event, params) | ||
} | ||
} | ||
|
||
@JvmStatic | ||
fun enableAll() { | ||
eventsBuffers.forEach { pair -> | ||
pair.value.enabled = true | ||
} | ||
} | ||
|
||
@JvmStatic | ||
fun disableAllAndFlush() { | ||
eventsBuffers.forEach { pair -> | ||
flushBuffer(pair.key) | ||
pair.value.enabled = false | ||
} | ||
} | ||
|
||
/** | ||
* Emit an event with specified params. It will be buffered if buffering for the event is enabled or | ||
* it will be send immediately otherwise. | ||
* | ||
* @param event The event to be emitted | ||
* @param params Optional event parameters | ||
* | ||
* @see [enableBuffer] | ||
* @see [disableBuffer] | ||
*/ | ||
@JvmStatic | ||
fun emit(event: CleverTapEvent, params: Any?) { | ||
if (eventsBuffers[event]?.enabled == true) { | ||
addToBuffer(event, params) | ||
} else { | ||
sendEvent(event, params) | ||
} | ||
} | ||
|
||
/** | ||
* Adds an event to the buffer for future emission. | ||
* Events will remain in the buffer until [flushBuffer] for the same event name is called. | ||
* | ||
* @param event The event to be buffered. | ||
* @param params Optional event parameters to be sent when the event is emitted. | ||
*/ | ||
private fun addToBuffer(event: CleverTapEvent, params: Any?) { | ||
val buffer = eventsBuffers[event] ?: return | ||
buffer.add(params) | ||
Log.i(LOG_TAG, "Event $event added to buffer.") | ||
} | ||
|
||
private fun sendEvent( | ||
event: CleverTapEvent, | ||
params: Any? | ||
) { | ||
try { | ||
if (event == CleverTapEvent.CLEVERTAP_UNKNOWN) { | ||
Log.i(LOG_TAG, "Not Sending event since its unknown") | ||
return | ||
} | ||
invokeMethodOnUiThread( | ||
EventNameMapper.fromCleverTapEvent(event), | ||
params | ||
) | ||
Log.i(LOG_TAG, "Sending event $event") | ||
} catch (t: Throwable) { | ||
Log.e(LOG_TAG, "Sending event $event failed", t) | ||
} | ||
} | ||
|
||
private fun createBuffersMap(enableBuffers: Boolean) = | ||
CleverTapEvent.values().filter { it.bufferable }.associateWith { | ||
Buffer(enabled = enableBuffers) | ||
} | ||
} | ||
|
||
private class Buffer(var enabled: Boolean) { | ||
private val items: Queue<Any?> by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { LinkedList() } | ||
|
||
fun add(item: Any?) = items.add(item) | ||
fun remove(): Any? = items.remove() | ||
fun size(): Int = items.size | ||
} |
Oops, something went wrong.