class SDKInitCoordinator(private val context: Context) : VelocityAdsInitListener {
private val handler = Handler(Looper.getMainLooper())
private var retryAttempt = 0
private val maxRetries = 3
private val baseDelayMs = 1_000L
private val maxDelayMs = 30_000L
fun start() { initSDK() }
private fun initSDK() {
val request = VelocityAdsInitRequest.Builder("YOUR_APPLICATION_KEY").build()
VelocityAds.initSDK(context, request, this)
}
override fun onInitSuccess() { retryAttempt = 0 }
override fun onInitFailure(error: VelocityAdsError) {
if (retryAttempt >= maxRetries) return
if (!isOnline()) return
val exponential = min(maxDelayMs, baseDelayMs * 2.0.pow(retryAttempt).toLong())
val delay = exponential + Random.nextLong(0, 500)
retryAttempt++
handler.postDelayed({ initSDK() }, delay)
}
private fun isOnline(): Boolean {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val caps = cm.getNetworkCapabilities(cm.activeNetwork ?: return false) ?: return false
return caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
}
}