Skip to content

Commit

Permalink
完善热启动判断,开屏广告等待过程放协程,避免anr
Browse files Browse the repository at this point in the history
  • Loading branch information
Espoir committed Dec 25, 2023
1 parent 9419a0c commit 862751c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 35 deletions.
5 changes: 2 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions easyadv/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
103 changes: 71 additions & 32 deletions easyadv/src/main/java/com/espoir/easyadv/AdvControl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?) {
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 862751c

Please sign in to comment.