package com.smartechbasereactnative import android.app.Application import android.content.Context import android.content.Intent import android.content.IntentFilter import android.os.Build import android.os.Handler import android.os.Looper import android.util.Log import com.netcore.android.Smartech import java.lang.ref.WeakReference class SmartechBasePlugin private constructor() : SmartechDeeplinkReceivers.OnDeeplinkReceived { private var deeplinkCallbackHandler: SmartechDeeplinkCallbackHandler? = null private val maxDelayTime = 3000L private val delayDiff = 100L private val startDelayTime = 400L private lateinit var smartech: Smartech fun setDeeplinkHandlerListener(deeplinkCallbackHandler: SmartechDeeplinkCallbackHandler?) { this.deeplinkCallbackHandler = deeplinkCallbackHandler } fun init(context: Application) { try { Log.v(TAG, "SmartechBasePlugin - initialisation started.") smartech = Smartech.getInstance((WeakReference(context))) smartech.initializeSdk(context) SmartechDeeplinkReceivers.setRegisterCallback(this) val deeplinkReceiver = SmartechDeeplinkReceivers() val filter = IntentFilter("com.smartech.EVENT_PN_INBOX_CLICK") if (Build.VERSION.SDK_INT >= 34) { context.registerReceiver(deeplinkReceiver, filter, Context.RECEIVER_EXPORTED) } else { context.registerReceiver(deeplinkReceiver, filter) } } catch (e: Exception) { e.printStackTrace() } } fun setDebugLevel(debugLevel: Int) { if (this::smartech.isInitialized) { smartech.setDebugLevel(debugLevel) } } override fun onDeeplinkReceived(intent: Intent) { try { Log.v(TAG, "SmartechBasePlugin - onDeeplinkReceived executed") if (deeplinkCallbackHandler == null) { Log.v(TAG, "SmartechBasePlugin - deeplink callback handler is null") initDeeplinkCallbackHandler(startDelayTime, intent) } else { Log.v(TAG, "SmartechBasePlugin - deeplink callback handler is available") deeplinkCallbackHandler!!.onDeeplinkIntentReceived(intent) } } catch (e: Exception) { e.printStackTrace() } } /** * Initializes the deeplink callback handler with a specified delay and intent. * This method recursively tries to set up the handler if it is not available * initially, incrementing the delay until the maximum allowed delay is reached. * * @param delay The initial delay before attempting to initialize the handler. * @param intent The Intent to be sent to the deeplink callback handler once it is available. */ private fun initDeeplinkCallbackHandler(delay: Long, intent: Intent) { try { Handler(Looper.getMainLooper()).postDelayed({ if (deeplinkCallbackHandler == null) { Log.v( TAG, "SmartechBasePlugin - Deeplink handler delay is $delay handler instance is $deeplinkCallbackHandler" ) if (delay < maxDelayTime) { initDeeplinkCallbackHandler((delay + delayDiff), intent) } } else { Log.v( TAG, "SmartechBasePlugin - deeplink callback handler is available. Sending data to app." ) deeplinkCallbackHandler!!.onDeeplinkIntentReceived(intent) } }, delay) } catch (e: Exception) { e.printStackTrace() } } companion object { val TAG: String = SmartechBasePlugin::class.java.name @Volatile private var INSTANCE: SmartechBasePlugin? = null @JvmStatic val instance: SmartechBasePlugin get() = INSTANCE ?: synchronized(SmartechBasePlugin::class.java) { INSTANCE ?: buildInstance().also { INSTANCE = it } } private fun buildInstance(): SmartechBasePlugin { return SmartechBasePlugin() } } }