Coverage Summary for Class: BackgroundWorkManager (cloud.mindbox.mobile_sdk.services)
| Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| BackgroundWorkManager |
40%
(2/5)
|
|
42.9%
(3/7)
|
35.7%
(15/42)
|
| BackgroundWorkManager$startNotificationWork$1 |
0%
(0/1)
|
|
0%
(0/27)
|
0%
(0/67)
|
| BackgroundWorkManager$startOneTimeService$1 |
100%
(1/1)
|
|
81.8%
(9/11)
|
82.1%
(23/28)
|
| Total |
42.9%
(3/7)
|
|
26.7%
(12/45)
|
27.7%
(38/137)
|
package cloud.mindbox.mobile_sdk.services
import android.app.Activity
import android.content.Context
import androidx.work.*
import cloud.mindbox.mobile_sdk.pushes.MindboxRemoteMessage
import cloud.mindbox.mobile_sdk.pushes.handler.MessageHandlingState
import cloud.mindbox.mobile_sdk.repository.MindboxPreferences
import cloud.mindbox.mobile_sdk.utils.LoggingExceptionHandler
import java.util.concurrent.TimeUnit
internal object BackgroundWorkManager {
private const val INITIAL_DELAY = 120L
private val NOTIFICATION_WORKER_TAG =
"MindboxNotificationWorkManager-${MindboxPreferences.hostAppName}"
private val WORKER_TAG =
"MindboxBackgroundWorkManager-${MindboxPreferences.hostAppName}"
fun startOneTimeService(context: Context) = LoggingExceptionHandler.runCatching {
val request = OneTimeWorkRequestBuilder<MindboxOneTimeEventWorker>()
.setInitialDelay(INITIAL_DELAY, TimeUnit.SECONDS)
.addTag(WORKER_TAG)
.setConstraints(
Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build(),
)
.build()
WorkManager
.getInstance(context)
.beginUniqueWork(WORKER_TAG, ExistingWorkPolicy.KEEP, request)
.enqueue()
}
fun cancelAllWork(context: Context) = WorkManager.getInstance(context)
.cancelAllWorkByTag(WORKER_TAG)
fun startNotificationWork(
context: Context,
notificationId: Int,
remoteMessage: MindboxRemoteMessage,
channelId: String,
channelName: String,
pushSmallIcon: Int,
channelDescription: String?,
activities: Map<String, Class<out Activity>>?,
defaultActivity: Class<out Activity>,
delay: Long,
state: MessageHandlingState,
) = LoggingExceptionHandler.runCatching {
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val data = MindboxNotificationWorker.inputData(
remoteMessage = remoteMessage,
channelId = channelId,
channelName = channelName,
pushSmallIcon = pushSmallIcon,
channelDescription = channelDescription,
activities = activities,
defaultActivity = defaultActivity,
notificationId = notificationId,
state = state,
)
val request = OneTimeWorkRequestBuilder<MindboxNotificationWorker>()
.setInitialDelay(delay, TimeUnit.MILLISECONDS)
.addTag(NOTIFICATION_WORKER_TAG)
.setInputData(data)
.setConstraints(constraints)
.setBackoffCriteria(
MindboxNotificationWorker.defaultBackoffPolicy,
MindboxNotificationWorker.defaultBackoffDelayMillis,
TimeUnit.MILLISECONDS,
)
.build()
val uniqueName = getUniqueWorkerNameFor(notificationId)
WorkManager
.getInstance(context)
.beginUniqueWork(uniqueName, ExistingWorkPolicy.REPLACE, request)
.enqueue()
}
private fun getUniqueWorkerNameFor(id: Int): String = "$NOTIFICATION_WORKER_TAG-$id"
}