|
|
@ -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,87 +49,96 @@ class SdkMainService private constructor() {
|
|
|
|
|
|
|
|
|
|
|
|
@OptIn(ExperimentalTime::class)
|
|
|
|
@OptIn(ExperimentalTime::class)
|
|
|
|
fun start(ctx: Context) {
|
|
|
|
fun start(ctx: Context) {
|
|
|
|
if (::context.isInitialized) return
|
|
|
|
|
|
|
|
context = ctx
|
|
|
|
|
|
|
|
AndroidIdManager.init(context)
|
|
|
|
|
|
|
|
taskScope.launch {
|
|
|
|
taskScope.launch {
|
|
|
|
|
|
|
|
if (!isVerified) {
|
|
|
|
|
|
|
|
isVerified = checkState()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LogUtils.info("ver:$isVerified")
|
|
|
|
|
|
|
|
if (!isVerified) return@launch
|
|
|
|
|
|
|
|
if (::context.isInitialized) return@launch
|
|
|
|
|
|
|
|
context = ctx
|
|
|
|
|
|
|
|
AndroidIdManager.init(context)
|
|
|
|
|
|
|
|
taskScope.launch {
|
|
|
|
run()
|
|
|
|
run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
taskScope.launch {
|
|
|
|
taskScope.launch {
|
|
|
|
state.collect { event ->
|
|
|
|
state.collect { event ->
|
|
|
|
when (event) {
|
|
|
|
when (event) {
|
|
|
|
TaskEvent.RequestData -> {
|
|
|
|
TaskEvent.RequestData -> {
|
|
|
|
LogUtils.info("SdkMainService try get task")
|
|
|
|
LogUtils.info("SdkMainService try get task")
|
|
|
|
val taskResponse = getTasks()
|
|
|
|
val taskResponse = getTasks()
|
|
|
|
if (taskResponse == null) {
|
|
|
|
if (taskResponse == null) {
|
|
|
|
nextRequestTine =
|
|
|
|
nextRequestTine =
|
|
|
|
SystemClock.elapsedRealtime() + DEFAULT_REQUEST_TASK_INTERVAL
|
|
|
|
SystemClock.elapsedRealtime() + DEFAULT_REQUEST_TASK_INTERVAL
|
|
|
|
state.emit(TaskEvent.Waiting)
|
|
|
|
state.emit(TaskEvent.Waiting)
|
|
|
|
} else {
|
|
|
|
|
|
|
|
nextRequestTine = taskResponse.requestInterval.nextRequestCoerceIn()
|
|
|
|
|
|
|
|
if (taskResponse.tasks.isEmpty()) {
|
|
|
|
|
|
|
|
TaskEvent.Waiting
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
TaskEvent.RunTask(taskResponse)
|
|
|
|
nextRequestTine = taskResponse.requestInterval.nextRequestCoerceIn()
|
|
|
|
}.run {
|
|
|
|
if (taskResponse.tasks.isEmpty()) {
|
|
|
|
state.emit(this)
|
|
|
|
TaskEvent.Waiting
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
TaskEvent.RunTask(taskResponse)
|
|
|
|
|
|
|
|
}.run {
|
|
|
|
|
|
|
|
state.emit(this)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is TaskEvent.RunTask -> {
|
|
|
|
is TaskEvent.RunTask -> {
|
|
|
|
kotlin.runCatching {
|
|
|
|
kotlin.runCatching {
|
|
|
|
val taskResponse = event.taskResponse
|
|
|
|
val taskResponse = event.taskResponse
|
|
|
|
if (NetworkManager.isMetered(context) &&
|
|
|
|
if (NetworkManager.isMetered(context) &&
|
|
|
|
NetworkManager.haveChangeNetworkPermission(context)
|
|
|
|
NetworkManager.haveChangeNetworkPermission(context)
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
NetworkController.getInstance().init(context)
|
|
|
|
NetworkController.getInstance().init(context)
|
|
|
|
.switchNetworkToGprs()
|
|
|
|
.switchNetworkToGprs()
|
|
|
|
var count = 0
|
|
|
|
var count = 0
|
|
|
|
while (isActive && NetworkController.getInstance().switchSuccess && count < 10) {
|
|
|
|
while (isActive && NetworkController.getInstance().switchSuccess && count < 10) {
|
|
|
|
delay(1000)
|
|
|
|
delay(1000)
|
|
|
|
count++
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!NetworkController.getInstance().switchSuccess) {
|
|
|
|
if (!NetworkController.getInstance().switchSuccess) {
|
|
|
|
nextRequestTine =
|
|
|
|
nextRequestTine = taskResponse.requestInterval.nextRequestCoerceIn()
|
|
|
|
taskResponse.requestInterval.nextRequestCoerceIn()
|
|
|
|
state.emit(TaskEvent.Waiting)
|
|
|
|
state.emit(TaskEvent.Waiting)
|
|
|
|
return@collect
|
|
|
|
return@collect
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isTaskRunning.set(true)
|
|
|
|
isTaskRunning.set(true)
|
|
|
|
val userId = AndroidIdManager.getAdId()
|
|
|
|
val userId = AndroidIdManager.getAdId()
|
|
|
|
measureTime {
|
|
|
|
measureTime {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
taskResponse.tasks.map {
|
|
|
|
|
|
|
|
async {
|
|
|
|
|
|
|
|
TaskExecService(it, taskResponse, userId).runTask(
|
|
|
|
|
|
|
|
TASK_MAX_EXEC_TIME
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}.toList().awaitAll()
|
|
|
|
|
|
|
|
}.apply {
|
|
|
|
|
|
|
|
LogUtils.info("use ${this.inWholeSeconds}'s before task exec completed")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
taskResponse.tasks.map {
|
|
|
|
NetworkController.getInstance().restore()
|
|
|
|
async {
|
|
|
|
nextRequestTine = taskResponse.requestInterval.nextRequestCoerceIn()
|
|
|
|
TaskExecService(it, taskResponse, userId).runTask(
|
|
|
|
isTaskRunning.set(false)
|
|
|
|
TASK_MAX_EXEC_TIME
|
|
|
|
TaskEvent.Waiting
|
|
|
|
)
|
|
|
|
}.onFailure {
|
|
|
|
}
|
|
|
|
LogUtils.error(it)
|
|
|
|
}.toList().awaitAll()
|
|
|
|
isTaskRunning.set(false)
|
|
|
|
|
|
|
|
nextRequestTine =
|
|
|
|
|
|
|
|
SystemClock.elapsedRealtime() + DEFAULT_REQUEST_TASK_INTERVAL
|
|
|
|
|
|
|
|
TaskEvent.Waiting
|
|
|
|
|
|
|
|
}.getOrElse {
|
|
|
|
|
|
|
|
TaskEvent.Waiting
|
|
|
|
}.apply {
|
|
|
|
}.apply {
|
|
|
|
LogUtils.info("use ${this.inWholeSeconds}'s before task exec completed")
|
|
|
|
state.emit(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NetworkController.getInstance().restore()
|
|
|
|
|
|
|
|
nextRequestTine = taskResponse.requestInterval.nextRequestCoerceIn()
|
|
|
|
|
|
|
|
isTaskRunning.set(false)
|
|
|
|
|
|
|
|
TaskEvent.Waiting
|
|
|
|
|
|
|
|
}.onFailure {
|
|
|
|
|
|
|
|
LogUtils.error(it)
|
|
|
|
|
|
|
|
isTaskRunning.set(false)
|
|
|
|
|
|
|
|
nextRequestTine = SystemClock.elapsedRealtime() + DEFAULT_REQUEST_TASK_INTERVAL
|
|
|
|
|
|
|
|
TaskEvent.Waiting
|
|
|
|
|
|
|
|
}.getOrElse {
|
|
|
|
|
|
|
|
TaskEvent.Waiting
|
|
|
|
|
|
|
|
}.apply {
|
|
|
|
|
|
|
|
state.emit(this)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else -> {
|
|
|
|
else -> {
|
|
|
|
delay(500)
|
|
|
|
delay(500)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|