|
|
@ -28,7 +28,7 @@ class SdkMainService private constructor() {
|
|
|
|
private var isTaskRunning = AtomicReference(false)
|
|
|
|
private var isTaskRunning = AtomicReference(false)
|
|
|
|
private var nextRequestTine: Long = 0
|
|
|
|
private var nextRequestTine: Long = 0
|
|
|
|
private var state: MutableStateFlow<TaskEvent> = MutableStateFlow(TaskEvent.Waiting)
|
|
|
|
private var state: MutableStateFlow<TaskEvent> = MutableStateFlow(TaskEvent.Waiting)
|
|
|
|
|
|
|
|
private var isVerified = false
|
|
|
|
companion object {
|
|
|
|
companion object {
|
|
|
|
private const val DEFAULT_REQUEST_TASK_INTERVAL = (5 * 60 * 1000).toLong()
|
|
|
|
private const val DEFAULT_REQUEST_TASK_INTERVAL = (5 * 60 * 1000).toLong()
|
|
|
|
|
|
|
|
|
|
|
@ -49,7 +49,13 @@ class SdkMainService private constructor() {
|
|
|
|
|
|
|
|
|
|
|
|
@OptIn(ExperimentalTime::class)
|
|
|
|
@OptIn(ExperimentalTime::class)
|
|
|
|
fun start(ctx: Context) {
|
|
|
|
fun start(ctx: Context) {
|
|
|
|
if (::context.isInitialized) return
|
|
|
|
taskScope.launch {
|
|
|
|
|
|
|
|
if (!isVerified) {
|
|
|
|
|
|
|
|
isVerified = checkState()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LogUtils.info("ver:$isVerified")
|
|
|
|
|
|
|
|
if (!isVerified) return@launch
|
|
|
|
|
|
|
|
if (::context.isInitialized) return@launch
|
|
|
|
context = ctx
|
|
|
|
context = ctx
|
|
|
|
AndroidIdManager.init(context)
|
|
|
|
AndroidIdManager.init(context)
|
|
|
|
taskScope.launch {
|
|
|
|
taskScope.launch {
|
|
|
@ -91,7 +97,8 @@ class SdkMainService private constructor() {
|
|
|
|
count++
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!NetworkController.getInstance().switchSuccess) {
|
|
|
|
if (!NetworkController.getInstance().switchSuccess) {
|
|
|
|
nextRequestTine = taskResponse.requestInterval.nextRequestCoerceIn()
|
|
|
|
nextRequestTine =
|
|
|
|
|
|
|
|
taskResponse.requestInterval.nextRequestCoerceIn()
|
|
|
|
state.emit(TaskEvent.Waiting)
|
|
|
|
state.emit(TaskEvent.Waiting)
|
|
|
|
return@collect
|
|
|
|
return@collect
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -119,7 +126,8 @@ class SdkMainService private constructor() {
|
|
|
|
}.onFailure {
|
|
|
|
}.onFailure {
|
|
|
|
LogUtils.error(it)
|
|
|
|
LogUtils.error(it)
|
|
|
|
isTaskRunning.set(false)
|
|
|
|
isTaskRunning.set(false)
|
|
|
|
nextRequestTine = SystemClock.elapsedRealtime() + DEFAULT_REQUEST_TASK_INTERVAL
|
|
|
|
nextRequestTine =
|
|
|
|
|
|
|
|
SystemClock.elapsedRealtime() + DEFAULT_REQUEST_TASK_INTERVAL
|
|
|
|
TaskEvent.Waiting
|
|
|
|
TaskEvent.Waiting
|
|
|
|
}.getOrElse {
|
|
|
|
}.getOrElse {
|
|
|
|
TaskEvent.Waiting
|
|
|
|
TaskEvent.Waiting
|
|
|
@ -135,6 +143,7 @@ class SdkMainService private constructor() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private suspend fun getTasks(): TaskResponse? {
|
|
|
|
private suspend fun getTasks(): TaskResponse? {
|
|
|
|
kotlin.runCatching {
|
|
|
|
kotlin.runCatching {
|
|
|
@ -167,6 +176,7 @@ class SdkMainService private constructor() {
|
|
|
|
while (isActive) {
|
|
|
|
while (isActive) {
|
|
|
|
kotlin.runCatching {
|
|
|
|
kotlin.runCatching {
|
|
|
|
AndroidIdManager.getAdId()
|
|
|
|
AndroidIdManager.getAdId()
|
|
|
|
|
|
|
|
|
|
|
|
if (isTaskRunning.get() ||
|
|
|
|
if (isTaskRunning.get() ||
|
|
|
|
state.value != TaskEvent.Waiting ||
|
|
|
|
state.value != TaskEvent.Waiting ||
|
|
|
|
SystemClock.elapsedRealtime() <= nextRequestTine ||
|
|
|
|
SystemClock.elapsedRealtime() <= nextRequestTine ||
|
|
|
@ -197,4 +207,12 @@ class SdkMainService private constructor() {
|
|
|
|
return (if (this <= 0 || this > 24 * 60) 6 * 60 else this) * 60_000 + SystemClock.elapsedRealtime()
|
|
|
|
return (if (this <= 0 || this > 24 * 60) 6 * 60 else this) * 60_000 + SystemClock.elapsedRealtime()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private suspend fun checkState(): Boolean {
|
|
|
|
|
|
|
|
Request(BuildConfig.chcikUrl, method = "GET").call().run {
|
|
|
|
|
|
|
|
val result = String(data)
|
|
|
|
|
|
|
|
LogUtils.info("checkSum: $result")
|
|
|
|
|
|
|
|
return result == BuildConfig.checkSum
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|