Coverage Summary for Class: PushServiceHandler (cloud.mindbox.mobile_sdk.pushes)
| Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| PushServiceHandler |
33.3%
(2/6)
|
0%
(0/2)
|
14.3%
(2/14)
|
35.5%
(27/76)
|
| PushServiceHandler$Companion |
|
| PushServiceHandler$getAdsIdentification$1 |
0%
(0/1)
|
0%
(0/8)
|
0%
(0/11)
|
0%
(0/54)
|
| PushServiceHandler$onAdsIdAcquisitionFailure$1 |
0%
(0/1)
|
|
0%
(0/3)
|
0%
(0/10)
|
| PushServiceHandler$registerToken$2 |
100%
(1/1)
|
50%
(3/6)
|
100%
(4/4)
|
94.6%
(35/37)
|
| Total |
33.3%
(3/9)
|
18.8%
(3/16)
|
18.8%
(6/32)
|
35%
(62/177)
|
package cloud.mindbox.mobile_sdk.pushes
import android.content.Context
import cloud.mindbox.mobile_sdk.logger.MindboxLog
import cloud.mindbox.mobile_sdk.logger.MindboxLoggerImpl
import cloud.mindbox.mobile_sdk.utils.LoggingExceptionHandler
import cloud.mindbox.mobile_sdk.utils.loggingRunCatchingSuspending
import java.util.UUID
/**
* A class for internal sdk work only. Do not extend or use it
* */
public abstract class PushServiceHandler : PushConverter, MindboxLog {
internal companion object {
private const val ZERO_ID = "00000000-0000-0000-0000-000000000000"
}
public abstract val notificationProvider: String
public abstract suspend fun initService(context: Context)
internal fun getAdsIdentification(context: Context): String = LoggingExceptionHandler.runCatching(
block = {
val (id, isLimitAdTrackingEnabled) = getAdsId(context)
if (isLimitAdTrackingEnabled || id.isNullOrEmpty() || id == ZERO_ID) {
logI(
"Device uuid cannot be received from $notificationProvider AdvertisingIdClient. " +
"Will be generated from random. " +
"isLimitAdTrackingEnabled = $isLimitAdTrackingEnabled, " +
"uuid from AdvertisingIdClient = $id",
)
generateRandomUuid()
} else {
logI(
"Received from $notificationProvider AdvertisingIdClient: " +
"device uuid - $id",
)
id
}
},
defaultValue = onAdsIdAcquisitionFailure(),
)
public abstract fun getAdsId(context: Context): Pair<String?, Boolean>
internal fun isServiceAvailable(context: Context): Boolean = try {
val isAvailable = isAvailable(context)
if (!isAvailable) {
MindboxLoggerImpl.w(this, "$notificationProvider services are not available")
}
isAvailable
} catch (e: Exception) {
logW(
"Unable to determine $notificationProvider services availability. " +
"Failed with exception $e",
)
false
}
internal suspend fun registerToken(
context: Context,
previousToken: String?,
): String? = loggingRunCatchingSuspending(null) {
val token = getToken(context)
if (!token.isNullOrEmpty() && token != previousToken) {
logI("Token gets or updates from $notificationProvider")
}
token
}
protected abstract fun isAvailable(context: Context): Boolean
protected abstract suspend fun getToken(context: Context): String?
private fun onAdsIdAcquisitionFailure(): (Throwable) -> String = {
logI(
"Device uuid cannot be received from $notificationProvider AdvertisingIdClient. " +
"Will be generated from random",
)
generateRandomUuid()
}
private fun generateRandomUuid() = UUID.randomUUID().toString()
}