diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 925a29a..1a19580 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -4,10 +4,8 @@ diff --git a/easyadv/build.gradle b/easyadv/build.gradle index 370b118..52822e8 100644 --- a/easyadv/build.gradle +++ b/easyadv/build.gradle @@ -29,5 +29,7 @@ dependencies { implementation("androidx.appcompat:appcompat:1.2.0") implementation("androidx.core:core-ktx:1.5.0") + implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4" + implementation 'androidx.lifecycle:lifecycle-process:2.4.1' compileOnly "com.pangle.cn:mediation-sdk:5.7.0.5" //融合SDK } \ No newline at end of file diff --git a/easyadv/src/main/java/com/espoir/easyadv/AdvControl.kt b/easyadv/src/main/java/com/espoir/easyadv/AdvControl.kt index bbdc1ec..fa36d21 100644 --- a/easyadv/src/main/java/com/espoir/easyadv/AdvControl.kt +++ b/easyadv/src/main/java/com/espoir/easyadv/AdvControl.kt @@ -3,6 +3,11 @@ package com.espoir.easyadv import android.app.Activity import android.app.Application import android.os.Bundle +import android.util.Log +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleObserver +import androidx.lifecycle.OnLifecycleEvent +import androidx.lifecycle.ProcessLifecycleOwner import com.bytedance.sdk.openadsdk.stub.activity.Stub_Standard_Portrait_Activity import com.espoir.easyadv.config.BannerAdvConfig import com.espoir.easyadv.config.FeedAdConfig @@ -12,18 +17,23 @@ import com.espoir.easyadv.config.SplashAdvConfig import com.espoir.easyadv.config.listener import com.espoir.easyadv.interceptor.AdvInterceptCallback import com.espoir.easyadv.interceptor.AdvInterceptorManager +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import java.util.concurrent.atomic.AtomicBoolean -class AdvControl { +class AdvControl : LifecycleObserver { private var interceptorManager = AdvInterceptorManager() - private var activityCount: Int = 0 private var showHotSplashAdv = false @Volatile - private var splashAdvFlag = false + private var splashAdvFlag = AtomicBoolean(false) @Volatile private var showSplashAdvTime = 0L + private var isForeground = false + private var currAct: Activity? = null fun showSplashAdv(config: SplashAdvConfig, splashAdvEngine: ISplashAdvEngine?) { @@ -40,20 +50,27 @@ class AdvControl { } showSplashAdvTime = System.currentTimeMillis() //等待sdk初始化完成 - while (!splashAdvFlag) { - if (EasyAdv.isFinishSdkInit) { - splashAdvFlag = if (EasyAdv.isInitSdkSuccess) { - splashAdvEngine?.showSplashAdv(config) - true + config.scope?.launch(Dispatchers.IO) { + while (!splashAdvFlag.get()) { + if (EasyAdv.isFinishSdkInit) { + withContext(Dispatchers.Main) { + if (EasyAdv.isInitSdkSuccess) { + splashAdvEngine?.showSplashAdv(config) + } else { + config.listener(CallbackType.ERROR, EasyAdv.ERROR_SDK, "sdk init fail") + } + } + splashAdvFlag.set(true) } else { - config.listener(CallbackType.ERROR, EasyAdv.ERROR_SDK, "sdk init fail") - true - } - } else { - if (EasyAdv.sdkTimeOutMillis > 0) { - if (System.currentTimeMillis() - showSplashAdvTime > EasyAdv.sdkTimeOutMillis) { - config.listener(CallbackType.ERROR, EasyAdv.ERROR_SDK, "sdk init time out") - splashAdvFlag = true + if (EasyAdv.sdkTimeOutMillis > 0) { + if (System.currentTimeMillis() - showSplashAdvTime > EasyAdv.sdkTimeOutMillis) { + withContext(Dispatchers.Main) { + config.listener(CallbackType.ERROR, EasyAdv.ERROR_SDK, "sdk init time out") + } + splashAdvFlag.set(true) + } + } else { + splashAdvFlag.set(true) } } } @@ -101,43 +118,65 @@ class AdvControl { } } - fun setUpHotSplashAdvStrategy(application: Application) { - application.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks { - override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { - } + //在前台 + @OnLifecycleEvent(Lifecycle.Event.ON_START) + private fun onForeground() { + if (!isForeground) { + isForeground = true - override fun onActivityStarted(activity: Activity) { - activityCount++ - if (activity is Stub_Standard_Portrait_Activity) { + Log.i("AdvControl", "App on onForeground") + currAct?.let { act -> + if (act is Stub_Standard_Portrait_Activity) { showHotSplashAdv = false } - } - - override fun onActivityResumed(activity: Activity) { if (EasyAdv.isFinishSdkInit && EasyAdv.isInitSdkSuccess) { EasyAdv.globalConfig()?.hotSplashAdvStrategy?.let { - if (it.showRequirement(activity) && showHotSplashAdv) { + if (it.showRequirement(act) && showHotSplashAdv) { showHotSplashAdv = false - it.showHotSplashAdv(activity, it.getCodeId()) + Log.i("AdvControl", "==热启动显示广告==") + it.showHotSplashAdv(act, it.getCodeId()) } } } } + } + } + + //在后台 + @OnLifecycleEvent(Lifecycle.Event.ON_STOP) + private fun onBackground() { + if (isForeground) { + isForeground = false + } + showHotSplashAdv = true + Log.i("AdvControl", "App on onBackground") + } + + fun setUpHotSplashAdvStrategy(application: Application) { + ProcessLifecycleOwner.get().lifecycle.addObserver(this) + application.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks { + override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { + currAct = activity + } + + override fun onActivityStarted(activity: Activity) { + currAct = activity + } + + override fun onActivityResumed(activity: Activity) { + } override fun onActivityPaused(activity: Activity) { } override fun onActivityStopped(activity: Activity) { - activityCount-- - if (activityCount == 0) {// 切换到了后台 - showHotSplashAdv = true - } } override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) { } override fun onActivityDestroyed(activity: Activity) { + currAct = null } }) } diff --git a/easyadv/src/main/java/com/espoir/easyadv/config/SplashAdvConfig.kt b/easyadv/src/main/java/com/espoir/easyadv/config/SplashAdvConfig.kt index 39cc6d8..1348c3c 100644 --- a/easyadv/src/main/java/com/espoir/easyadv/config/SplashAdvConfig.kt +++ b/easyadv/src/main/java/com/espoir/easyadv/config/SplashAdvConfig.kt @@ -9,6 +9,7 @@ import com.bytedance.sdk.openadsdk.TTSplashAd import com.espoir.easyadv.CallbackType import com.espoir.easyadv.EasyAdv import com.espoir.easyadv.SplashAdvListener +import kotlinx.coroutines.CoroutineScope import java.lang.ref.WeakReference class SplashAdvConfig : BaseAdvConfig() { @@ -17,6 +18,7 @@ class SplashAdvConfig : BaseAdvConfig() { internal var isHotSplash: Boolean = false internal var splashAdvListener: SplashAdvListener? = null internal var lifecycleOwner: LifecycleOwner? = null + internal var scope: CoroutineScope? = null internal var lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY fun setCodeId(codeId: String) = apply { @@ -40,6 +42,10 @@ class SplashAdvConfig : BaseAdvConfig() { this.lifeEvent = lifeEvent } + fun scope(scope: CoroutineScope?) = apply { + this.scope = scope + } + fun isHotSplash(isHotSplash: Boolean) = apply { this.isHotSplash = isHotSplash }