Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task(SDK-4057) - Introduces pt_render_terminal to render the final notification #749

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public class PTConstants {

public static final String PT_TIMER_THRESHOLD = "pt_timer_threshold";

public static final String PT_RENDER_TERMINAL = "pt_render_terminal";

public static final String PT_INPUT_LABEL = "pt_input_label";

public static final String PT_INPUT_KEY = "pt_input_reply";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.clevertap.android.pushtemplates

import android.app.ActivityOptions
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ContentResolver
Expand All @@ -15,6 +14,7 @@ import android.os.Build.VERSION_CODES
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.Builder
import com.clevertap.android.pushtemplates.PTConstants.*
import com.clevertap.android.pushtemplates.content.FiveIconBigContentView
import com.clevertap.android.pushtemplates.content.FiveIconSmallContentView
import com.clevertap.android.pushtemplates.styles.*
Expand Down Expand Up @@ -58,6 +58,7 @@ class TemplateRenderer : INotificationRenderer, AudibleNotification {
internal var pt_small_view: String? = null
internal var smallIcon = 0
internal var pt_dot = 0
internal var pt_render_terminal = true
var pt_timer_threshold = 0
internal var pt_input_label: String? = null
var pt_input_feedback: String? = null
Expand Down Expand Up @@ -165,20 +166,21 @@ class TemplateRenderer : INotificationRenderer, AudibleNotification {
if (ValidatorFactory.getValidator(TemplateType.ZERO_BEZEL, this)?.validate() == true)
return ZeroBezelStyle(this).builderFromStyle(context, extras, notificationId, nb)

TemplateType.TIMER -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (ValidatorFactory.getValidator(TemplateType.TIMER, this)?.validate() == true) {
val timerEnd = getTimerEnd()
if (timerEnd != null) {
timerRunner(context, extras, notificationId, timerEnd)
return TimerStyle(this, extras).builderFromStyle(
context,
extras,
notificationId,
nb
).setTimeoutAfter(timerEnd.toLong())
TemplateType.TIMER -> if (VERSION.SDK_INT >= VERSION_CODES.N) {
ValidatorFactory.getValidator(TemplateType.TIMER, this)
?.takeIf { it.validate() }
?.let {
getTimerEnd()?.let { timerEnd ->
if (pt_render_terminal) {
timerRunner(context, extras, notificationId, timerEnd)
}
return TimerStyle(this, extras)
.builderFromStyle(context, extras, notificationId, nb)
.setTimeoutAfter(timerEnd.toLong())
}
}
}
} else {
}
else {
PTLog.debug("Push Templates SDK supports Timer Notifications only on or above Android Nougat, reverting to basic template")
if (ValidatorFactory.getValidator(TemplateType.BASIC, this)?.validate() == true) {
return BasicStyle(this).builderFromStyle(context, extras, notificationId, nb)
Expand Down Expand Up @@ -215,17 +217,17 @@ class TemplateRenderer : INotificationRenderer, AudibleNotification {

private fun getTimerEnd(): Int? {
var timer_end: Int? = null
if (pt_timer_threshold != -1 && pt_timer_threshold >= PTConstants.PT_TIMER_MIN_THRESHOLD) {
timer_end = pt_timer_threshold * PTConstants.ONE_SECOND + PTConstants.ONE_SECOND
} else if (pt_timer_end >= PTConstants.PT_TIMER_MIN_THRESHOLD) {
timer_end = pt_timer_end * PTConstants.ONE_SECOND + PTConstants.ONE_SECOND
if (pt_timer_threshold != -1 && pt_timer_threshold >= PT_TIMER_MIN_THRESHOLD) {
timer_end = pt_timer_threshold * ONE_SECOND + ONE_SECOND
} else if (pt_timer_end >= PT_TIMER_MIN_THRESHOLD) {
timer_end = pt_timer_end * ONE_SECOND + ONE_SECOND
} else {
PTLog.debug("Not rendering notification Timer End value lesser than threshold (10 seconds) from current time: " + PTConstants.PT_TIMER_END)
PTLog.debug("Not rendering notification Timer End value lesser than threshold (10 seconds) from current time: " + PT_TIMER_END)
}
return timer_end
}

@RequiresApi(Build.VERSION_CODES.M)
@RequiresApi(VERSION_CODES.M)
private fun timerRunner(context: Context, extras: Bundle, notificationId: Int, delay: Int?) {
val handler = Handler(Looper.getMainLooper())

Expand All @@ -241,13 +243,13 @@ class TemplateRenderer : INotificationRenderer, AudibleNotification {
val basicTemplateBundle = extras.clone() as Bundle
basicTemplateBundle.remove("wzrk_rnv")
basicTemplateBundle.putString(Constants.WZRK_PUSH_ID, null) // skip dupe check
basicTemplateBundle.putString(PTConstants.PT_ID, "pt_basic") // set to basic
basicTemplateBundle.putString(PT_ID, "pt_basic") // set to basic


/**
* Update existing payload bundle with new title,msg,img for Basic template
*/
val ptJsonStr = basicTemplateBundle.getString(PTConstants.PT_JSON)
val ptJsonStr = basicTemplateBundle.getString(PT_JSON)
var ptJsonObj: JSONObject? = null
if (ptJsonStr != null) {
try {
Expand All @@ -258,33 +260,33 @@ class TemplateRenderer : INotificationRenderer, AudibleNotification {
}

if (pt_title_alt != null && pt_title_alt!!.isNotEmpty()) {
ptJsonObj?.put(PTConstants.PT_TITLE,pt_title_alt) ?: basicTemplateBundle.putString(
PTConstants.PT_TITLE,
ptJsonObj?.put(PT_TITLE,pt_title_alt) ?: basicTemplateBundle.putString(
PT_TITLE,
pt_title_alt
)
}
if (pt_big_img_alt != null && pt_big_img_alt!!.isNotEmpty()) {
ptJsonObj?.put(PTConstants.PT_BIG_IMG, pt_big_img_alt) ?: basicTemplateBundle.putString(
PTConstants.PT_BIG_IMG,
ptJsonObj?.put(PT_BIG_IMG, pt_big_img_alt) ?: basicTemplateBundle.putString(
PT_BIG_IMG,
pt_big_img_alt
)
}
if (pt_msg_alt != null && pt_msg_alt!!.isNotEmpty()) {
ptJsonObj?.put(PTConstants.PT_MSG, pt_msg_alt) ?: basicTemplateBundle.putString(
PTConstants.PT_MSG,
ptJsonObj?.put(PT_MSG, pt_msg_alt) ?: basicTemplateBundle.putString(
PT_MSG,
pt_msg_alt
)
}


if (ptJsonObj != null) {
basicTemplateBundle.putString(
PTConstants.PT_JSON,
PT_JSON,
ptJsonObj.toString()
)
}
// force random id generation
basicTemplateBundle.putString(PTConstants.PT_COLLAPSE_KEY, null)
basicTemplateBundle.putString(PT_COLLAPSE_KEY, null)
basicTemplateBundle.putString(Constants.WZRK_COLLAPSE, null)
basicTemplateBundle.remove(Constants.PT_NOTIF_ID)
val templateRenderer: INotificationRenderer =
Expand Down Expand Up @@ -314,7 +316,7 @@ class TemplateRenderer : INotificationRenderer, AudibleNotification {
}

override fun getActionButtonIconKey(): String {
return PTConstants.PT_NOTIF_ICON
return PT_NOTIF_ICON
}

override fun getCollapseKey(extras: Bundle): Any? {
Expand Down Expand Up @@ -356,8 +358,8 @@ class TemplateRenderer : INotificationRenderer, AudibleNotification {
}

private fun setUp(context: Context, extras: Bundle, config: CleverTapInstanceConfig?) {
pt_id = extras.getString(PTConstants.PT_ID)
val pt_json = extras.getString(PTConstants.PT_JSON)
pt_id = extras.getString(PT_ID)
val pt_json = extras.getString(PT_JSON)
if (pt_id != null) {
templateType = TemplateType.fromString(pt_id)
var newExtras: Bundle? = null
Expand All @@ -370,46 +372,49 @@ class TemplateRenderer : INotificationRenderer, AudibleNotification {
}
if (newExtras != null) extras.putAll(newExtras)
}
pt_msg = extras.getString(PTConstants.PT_MSG)
pt_msg_summary = extras.getString(PTConstants.PT_MSG_SUMMARY)
pt_msg_clr = extras.getString(PTConstants.PT_MSG_COLOR)
pt_title = extras.getString(PTConstants.PT_TITLE)
pt_title_clr = extras.getString(PTConstants.PT_TITLE_COLOR)
pt_meta_clr = extras.getString(PTConstants.PT_META_CLR)
pt_bg = extras.getString(PTConstants.PT_BG)
pt_big_img = extras.getString(PTConstants.PT_BIG_IMG)
pt_large_icon = extras.getString(PTConstants.PT_NOTIF_ICON)
pt_small_view = extras.getString(PTConstants.PT_SMALL_VIEW)
pt_msg = extras.getString(PT_MSG)
pt_msg_summary = extras.getString(PT_MSG_SUMMARY)
pt_msg_clr = extras.getString(PT_MSG_COLOR)
pt_title = extras.getString(PT_TITLE)
pt_title_clr = extras.getString(PT_TITLE_COLOR)
pt_meta_clr = extras.getString(PT_META_CLR)
pt_bg = extras.getString(PT_BG)
pt_big_img = extras.getString(PT_BIG_IMG)
pt_large_icon = extras.getString(PT_NOTIF_ICON)
pt_small_view = extras.getString(PT_SMALL_VIEW)
imageList = Utils.getImageListFromExtras(extras)
deepLinkList = Utils.getDeepLinkListFromExtras(extras)
bigTextList = Utils.getBigTextFromExtras(extras)
smallTextList = Utils.getSmallTextFromExtras(extras)
priceList = Utils.getPriceFromExtras(extras)
pt_rating_default_dl = extras.getString(PTConstants.PT_DEFAULT_DL)
pt_rating_default_dl = extras.getString(PT_DEFAULT_DL)
pt_timer_threshold = Utils.getTimerThreshold(extras)
pt_input_label = extras.getString(PTConstants.PT_INPUT_LABEL)
pt_input_feedback = extras.getString(PTConstants.PT_INPUT_FEEDBACK)
pt_input_auto_open = extras.getString(PTConstants.PT_INPUT_AUTO_OPEN)
pt_dismiss_on_click = extras.getString(PTConstants.PT_DISMISS_ON_CLICK)
pt_chrono_title_clr = extras.getString(PTConstants.PT_CHRONO_TITLE_COLOUR)
pt_product_display_action = extras.getString(PTConstants.PT_PRODUCT_DISPLAY_ACTION)
pt_product_display_action_clr = extras.getString(PTConstants.PT_PRODUCT_DISPLAY_ACTION_COLOUR)

// true by default if not present in extras
pt_render_terminal = extras.getString(PT_RENDER_TERMINAL)?.equals("true", ignoreCase = true) ?: true
pt_input_label = extras.getString(PT_INPUT_LABEL)
pt_input_feedback = extras.getString(PT_INPUT_FEEDBACK)
pt_input_auto_open = extras.getString(PT_INPUT_AUTO_OPEN)
pt_dismiss_on_click = extras.getString(PT_DISMISS_ON_CLICK)
pt_chrono_title_clr = extras.getString(PT_CHRONO_TITLE_COLOUR)
pt_product_display_action = extras.getString(PT_PRODUCT_DISPLAY_ACTION)
pt_product_display_action_clr = extras.getString(PT_PRODUCT_DISPLAY_ACTION_COLOUR)
pt_timer_end = Utils.getTimerEnd(extras)
pt_big_img_alt = extras.getString(PTConstants.PT_BIG_IMG_ALT)
pt_msg_alt = extras.getString(PTConstants.PT_MSG_ALT)
pt_title_alt = extras.getString(PTConstants.PT_TITLE_ALT)
pt_product_display_linear = extras.getString(PTConstants.PT_PRODUCT_DISPLAY_LINEAR)
pt_big_img_alt = extras.getString(PT_BIG_IMG_ALT)
pt_msg_alt = extras.getString(PT_MSG_ALT)
pt_title_alt = extras.getString(PT_TITLE_ALT)
pt_product_display_linear = extras.getString(PT_PRODUCT_DISPLAY_LINEAR)
pt_product_display_action_text_clr =
extras.getString(PTConstants.PT_PRODUCT_DISPLAY_ACTION_TEXT_COLOUR)
pt_small_icon_clr = extras.getString(PTConstants.PT_SMALL_ICON_COLOUR)
pt_cancel_notif_id = extras.getString(PTConstants.PT_CANCEL_NOTIF_ID)
extras.getString(PT_PRODUCT_DISPLAY_ACTION_TEXT_COLOUR)
pt_small_icon_clr = extras.getString(PT_SMALL_ICON_COLOUR)
pt_cancel_notif_id = extras.getString(PT_CANCEL_NOTIF_ID)
pt_cancel_notif_ids = Utils.getNotificationIds(context)
actions = Utils.getActionKeys(extras)
pt_subtitle = extras.getString(PTConstants.PT_SUBTITLE)
pt_collapse_key = extras[PTConstants.PT_COLLAPSE_KEY]
pt_subtitle = extras.getString(PT_SUBTITLE)
pt_collapse_key = extras[PT_COLLAPSE_KEY]
pt_flip_interval = Utils.getFlipInterval(extras)
pID = extras.getString(Constants.WZRK_PUSH_ID)
pt_manual_carousel_type = extras.getString(PTConstants.PT_MANUAL_CAROUSEL_TYPE)
pt_manual_carousel_type = extras.getString(PT_MANUAL_CAROUSEL_TYPE)
if (config != null) {
this.config = config
}
Expand Down
Loading