Event handler

    Event handler


    Article summary

    In the Shoplive Player, notifications that occur are passed to the client through a Handler function for necessary processing.


    ShopLive.setHandler

    Set the ShopLiveHandler for handling events.

    fun setHandler(handler: ShopLiveHandler)

    Sample code

    class MainActivity : AppCompatActivity() {
    
        private val handler = object : ShopLiveHandler() {
    
            override fun handleNavigation(context: Context, url: String) {
              	Log.d(TAG, "handleNavigation >> url=$url")
            }
    
            override fun onChangeCampaignStatus(context: Context, campaignStatus: String) {
              	Log.d(TAG, "onChangeCampaignStatus >> $campaignStatus")
            }
        }
    
        private fun init() {
          	ShopLive.setHandler(handler)
        }
    }
    


    ShopLiveHandler.handleNavigation

    When you select a product, banner, etc. in Shoplive, information about the selected product or banner is delivered through the handleNavigation.

    When the url is passed to the handleNavigation, the player switches to PIP, so there is no need to call the ShopLive.startPictureInPicture().

    If you do not want to switch to PIP, refer to the ShopLive.setNextActionOnHandleNavigation interface.

    fun handleNavigation(context: Context, url: String)

    Parameter name

    Type

    Description

    context

    Context

    Context

    url

    String

    URL to go to when selecting a product or banner

    Sample code

    private val handler = object : ShopLiveHandler() {
        override fun handleNavigation(context: Context, url: String) {
            val type: ActionType = "your_action_type"
            when (type) {
                ActionType.PIP, ActionType.CLOSE -> {
                    val intent: Intent = Intent(this@SampleActivity, WebViewActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
                    intent.putExtra("url", url)
                    startActivity(intent)
                }
                ActionType.KEEP -> {
                    val webDialogFragment = WebViewDialogFragment(url)
                    ShopLive.showDialogFragment(webDialogFragment)
                }
            }
        }
    }

    Coupon

    This is the status used to set the coupon's activation status when completing the coupon processing.

    public enum CouponPopupStatus {
        SHOW,
        HIDE,
        KEEP
    }

    Sets the type of notification message that appears when completing coupon processing, if there is one.

    public enum CouponPopupResultAlertType {
        ALERT,
        TOAST
    }


    ShopLiveHandler.handleDownloadCoupon

    When a coupon is selected (tapped) in Shoplive, the coupon information is transmitted to the client. The client's coupon processing result is then communicated back to the Shoplive Android SDK through a callback, which is used to set the coupon status in the Shoplive Player.

    fun handleDownloadCoupon(context: Context, couponId: String, callback: ShopLiveHandlerCallback)

    Parameter name

    Type

    Description

    context

    Context

    Context

    couponId

    String

    Selected coupon ID

    callback

    ShopLiveHandlerCallback

    callback. ShopLive will notify you when the coupon is processed.

    callback ShopLiveHandlerCallback.couponResult

    When the coupon download is completed, it is called to deliver information regarding the success or failure of the result, along with any relevant messages.

    /**
    * @param isDownloadSuccess - true: success, false: fail
    * @param message - message to be delivered
    * @param couponStatus - SHOW: Showing coupon, HIDE: Hiding coupon, KEEP: Keeping current status
    * @param alertType - ALERT: popup, TOAST: toast message
    * */
    fun couponResult(
        isDownloadSuccess: Boolean, 
        message: String?, 
        couponStatus: ShopLive.CouponPopupStatus, 
        alertType: ShopLive.CouponPopupResultAlertType) {}

    Sample code

    private val handler = object : ShopLiveHandler() {
        override fun handleDownloadCoupon(
          context: Context,
          couponId: String,
          callback: ShopLiveHandlerCallback
        ) {
            val builder: AlertDialog.Builder = Builder(context)
            builder.setTitle("Coupon download")
            builder.setMessage("couponId $couponId")
            builder.setPositiveButton("success") { dialog, which ->
                // callback for success
                callback.couponResult(
                  true,
                  "Coupon download success",
                  ShopLive.CouponPopupStatus.HIDE,
                  ShopLive.CouponPopupResultAlertType.TOAST
                )
            }
    
            builder.setNegativeButton("Fail") { dialog, which ->
                // callback on fail
                callback.couponResult(
                  false,
                  "Coupon download fail",
                  ShopLive.CouponPopupStatus.SHOW,
                  ShopLive.CouponPopupResultAlertType.ALERT
                )
            }
            val dialog: Dialog = builder.create()
            dialog.show() 
        }
    }


    ShopLiveHandler.handleCustomAction

    In a pop-up menu, set the event to custom, and when the pop-up is clicked, event pass the pop-up information such as pop-up's id, type, and payload.

    fun handleCustomAction(context: Context, id: String, type: String, payload: JSONObject, callback: ShopLiveHandlerCallback)

    Parameter name

    Type

    Description

    context

    Context

    Context

    id

    String

    Coupon or banner ID

    type

    String

    Coupon or banner type

    payload

    JSONObject

    User-defined payload

    callback

    ShopLiveHandlerCallback

    customWhen processing is complete, a callback will notify ShopLive

    callback ShopLiveHandlerCallback.customActionResult

    To communicate the success or failure status, as well as any associated messages, when the customAction processing is completed, you call this to send those messages.

    /**
    * @param isSuccess - true: Success, false: Fail
    * @param message - Success or Fail message
    * @param couponStatus - SHOW, HIDE, KEEP
    * @param alertType - ALERT, TOAST
    * */
    fun customActionResult(
        isSuccess: Boolean, 
        message: String?, 
        couponStatus: ShopLive.CouponPopupStatu, 
        alertType: ShopLive.CouponPopupResultAlertType?) {}

    Sample code

    private val handler = object : ShopLiveHandler() {
    
        override fun handleCustomAction(
          context: Context,
          id: String,
          type: String,
          payload: JSONObject,
          callback: ShopLiveHandlerCallback
        ) {
            val builder: AlertDialog.Builder = Builder(context)
            builder.setTitle("Custom action(pop-up)")
            builder.setMessage("id: $id" + " type: " + R.attr.type + " payload: " + payload.toString())
            builder.setPositiveButton("Success") { dialog, which ->
                // Callback when succeeded
                callback.couponResult(
                  true,
                  "Custom action(Popup) succeeded.",
                  ShopLive.CouponPopupStatus.HIDE,
                  ShopLive.CouponPopupResultAlertType.TOAST
                )
            }
    
            builder.setNegativeButton("Failure") { dialog, which ->
                // Callback when failed
                callback.couponResult(
                  false,
                  "Custom action(Popup) failed.",
                  ShopLive.CouponPopupStatus.SHOW,
                  ShopLive.CouponPopupResultAlertType.ALERT
                )
            }
    
            val dialog: Dialog = builder.create()
            dialog.show()
        }
    }


    ShopLiveHandler.handleShare

    When you choose to share during a live broadcast, it is the Handler. To implement it directly without using the Android Share Sheet, you must override it.

    fun handleShare(context: Context, shareUrl: String) {}

    Parameter name

    Type

    Description

    context

    Context

    Context

    shareUrl

    String

    URL or scheme to share

    Sample code

    private val handler = object : ShopLiveHandler() {
        override fun handleShare(context: Context?, shareUrl: String?) {
          	Toast.makeText(context, "url=$shareUrl", Toast.LENGTH_SHORT).show()
        } 
    }


    ShopLiveHandler.onChangeCampaignStatus

    Handler when the broadcast state changes.

    /**
    * @param context - Context
    * @param campaignStatus - READY | ONAIR | CLOSED
    */
    fun onChangeCampaignStatus(context: Context, campaignStatus: String) {}

    Parameter name

    Type

    Description

    context

    Context

    Context

    campaignStatus

    String

    The status of a campaign: READY, ONAIR, CLOSED

    Sample code

    private val handler = object : ShopLiveHandler() {
        override fun onChangeCampaignStatus(context: Context, campaignStatus: String) {
          	Log.d(TAG, "onChangeCampaignStatus >> $campaignStatus")
        }
    }


    ShopLiveHandler.onCampaignInfo

    This is broadcast information in JSON Object format.

    fun onCampaignInfo(campaignInfo: JSONObject) {}

    Parameter name

    Type

    Description

    campaignInfo

    JSONObject

    The information of a campaign. For example:

    {"title": "TITLE_OF_A_CAMPAIGN"}

    Sample code

    private val handler = object : ShopLiveHandler() {
        override fun onCampaignInfo(campaignInfo: JSONObject) {
          	Log.d(TAG, "onCampaignInfo >> $campaignInfo") 
        }
    }


    ShopLiveHandler.onChangedPlayerStatus

    Receives the Shoplive Player status.

    fun onChangedPlayerStatus(isPipMode: Boolean, playerLifecycle: ShopLive.PlayerLifecycle) { }

    Parameter name

    Type

    Description

    isPipMode

    Boolean

    Current PIP.

    playerLifecycle

    ShopLive.PlayerLifecycle

    Shoplive Player Status: CREATED, CLOSING, DESTROYED

    enum PlayerLifcecycle

    Value

    Description

    CREATED

    Player created

    CLOSING

    Before the player ends

    DESTROYED

    Player terminated

    Sample code

    private val handler = object : ShopLiveHandler() {
        override fun onChangedPlayerStatus(
          isPipMode: Boolean,
          playerLifecycle: ShopLive.PlayerLifecycle) {
            when(playerLifecycle) {
                ShopLive.PlayerLifecycle.CREATED -> {
    
                }
                ShopLive.PlayerLifecycle.CLOSING -> {
    
                }
                ShopLive.PlayerLifecycle.DESTROYED -> {
    
                }
            }
        }
    }


    ShopLiveHandler.onSetUserName

    Called when the username has changed.

    fun onSetUserName(jsonObject: JSONObject) {}

    Parameter name

    Type

    Description

    jsonObject

    JSONObject

    The information of a user. For example:

    {"userId": "USER_ID", "userName": "USER_NAME"}

    playerLifecycle

    ShopLive.PlayerLifecycle

    Shoplive Player Status: CREATED, CLOSING, DESTROYED

    Sample code

    private val handler = object : ShopLiveHandler() {
        override fun onSetUserName(jsonObject: JSONObject?) {
         	 Log.d(TAG, "onSetUserName >> $jsonObject")
        }
    }


    ShopLiveHandler.handlePreview

    When you select the Shoplive preview screen, it's a Handler. Choosing the preview screen will lead you to the corresponding broadcast (default behavior). To implement it directly without entering the broadcast, you must override it.

    However, note that this event is only delivered when using the preview screen provided as an overlay on top of other apps. In the OS PIP (Picture-in-Picture) mode, the preview event won't be delivered, and it will automatically enter full-screen broadcast mode.

    fun handlePreview(context: Context, campaignKey: String) {}

    Parameter name

    Type

    Description

    conext

    Context

    Context

    campaignKey

    String

    Campaign (broadcast) key

    Sample code

    private val handler = object : ShopLiveHandler() {
        override fun handlePreview(context: Context, campaignKey: String) {
          	Log.d(TAG, "campaignKey=$campaignKey") 
        }
    }


    ShopLiveHandler.onReceivedCommand

    It is called when a command message is received.

    fun onReceivedCommand(context: Context, command: String, data: JSONObject) {}

    Parameter name

    Type

    Description

    conext

    Context

    Context

    command

    String

    The command to execute.

    data

    JSONObject

    The data to pass


    Sample code

    private val handler = object : ShopLiveHandler() {
        override fun onReceivedCommand(context: Context, command: String, data: JSONObject) {
            when(command) {
                "LOGIN_REQUIRED" -> {
    
                }
                "CLICK_PRODUCT_CART" -> {
    
                }
                "EVENT_LOG" -> {
                    // parsing example 1
                    val name = data.getString("name")
                    val feature = data.getString("feature")
                    val parameter = data.getString("parameter")
                    Toast.makeText(
                        context,
                        "name : $name, feature : $feature, parameter : $parameter",
                        Toast.LENGTH_SHORT
                    ).show()
                    // parsing example 2
                    val log = Gson().fromJson(data.toString(), ShopLiveLog.Data::class.java)
                    if (Options.isShowClickLog()) {
                    Toast.makeText(
                        context,
                        "name : ${log.name}, feature : ${log.feature}, campaignKey : ${log.campaignKey}, parameter : ${log.parameter}",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }
        }
    }

    ShopLiveHandler.onEvent

    User behavior can be tracked.

    ShopLiveLog.Data

    Property name

    Type

    Description

    name

    String

    The name of an event.

    feature

    String

    The types of events: click, show, action

    campaignKey

    String?

    The unique key of a broadcasting.

    parameter

    Map<String,Any>?

    The additional related parameters.

    Sample code

    private val handler = object : ShopLiveHandler() {
        ...
        override fun log(data: ShopLiveLog.Data) {
            Log.d(TAG, "name=${data.name} feature=${data.feature} campaignKey=${data.campaignKey} parameter=${data.parameter}")
        }
        ...
    }


    ShopLiveHandler.onError

    This conveys messages related to error situations occurring either before or during a broadcast.

    fun onError(context: Context, code: String, message: String) {}

    Parameter name

    Type

    Description

    conext

    Context

    Context

    code

    String

    An error code

    message

    String

    An error message

    Sample code

    private val handler = object : ShopLiveHandler() {
        override fun onError(context: Context, code: String, message: String) {
          	Log.d(TAG, "code=$code, message=$message")
        }
    }


    What's Next