175 lines
4.6 KiB
Kotlin
175 lines
4.6 KiB
Kotlin
package com.keyboard.journey.topon
|
|
|
|
import android.app.Activity
|
|
import android.os.CountDownTimer
|
|
import android.util.Log
|
|
import com.anythink.core.api.ATAdInfo
|
|
import com.anythink.core.api.AdError
|
|
import com.anythink.interstitial.api.ATInterstitial
|
|
import com.anythink.interstitial.api.ATInterstitialListener
|
|
import com.keyboard.journey.JourneyApp
|
|
|
|
|
|
object AdManager {
|
|
|
|
|
|
const val type_no_cache = 0
|
|
const val type_has_cache = 1
|
|
const val type_show_success = 2
|
|
const val type_show_close = 3
|
|
const val type_show_fail = 4
|
|
|
|
|
|
const val place1Id = "n669781bc9cd1e"
|
|
const val place2Id = "n669781bc3452e"
|
|
const val place3Id = "n669781bba00d8"
|
|
|
|
val list = mutableListOf<ATInterstitial>()
|
|
|
|
|
|
@JvmStatic
|
|
fun loadAllAd() {
|
|
if (list.size <= 0) {
|
|
val mInterstitialAd1 = ATInterstitial(JourneyApp.app, place1Id)
|
|
val mInterstitialAd2 = ATInterstitial(JourneyApp.app, place2Id)
|
|
val mInterstitialAd3 = ATInterstitial(JourneyApp.app, place3Id)
|
|
list.add(mInterstitialAd1)
|
|
list.add(mInterstitialAd2)
|
|
list.add(mInterstitialAd3)
|
|
}
|
|
for (ad in list) {
|
|
if (!ad.isAdReady) {
|
|
setCallBack(ad,object : AdListener {
|
|
override fun showSuccess() {
|
|
|
|
}
|
|
|
|
override fun showFail() {
|
|
|
|
}
|
|
|
|
override fun showClose() {
|
|
|
|
}
|
|
|
|
})
|
|
ad.load()
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
@JvmStatic
|
|
fun getReadyAd(): ATInterstitial? {
|
|
list.shuffle()
|
|
for (ad in list) {
|
|
if (ad.isAdReady) {
|
|
return ad
|
|
}
|
|
}
|
|
return null
|
|
}
|
|
|
|
|
|
@JvmStatic
|
|
fun showWelcomeAd(activity: Activity,totalTim: Long, goMain: () -> Unit): CountDownTimer {
|
|
var alreadyShow = false
|
|
var timer = object : CountDownTimer(totalTim, 100) {
|
|
override fun onTick(millisUntilFinished: Long) {
|
|
if (!alreadyShow) {
|
|
showAD(activity) {
|
|
if (it == type_has_cache) {
|
|
alreadyShow = true
|
|
}
|
|
if (it == type_show_close || it == type_show_fail) {
|
|
goMain.invoke()
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
override fun onFinish() {
|
|
if (!alreadyShow) {
|
|
showAD(activity) {
|
|
if (it == type_show_close || it == type_show_fail || it == type_no_cache) {
|
|
goMain.invoke()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
return timer
|
|
}
|
|
|
|
private fun setCallBack(ad: ATInterstitial, listener: AdListener) {
|
|
ad.setAdListener(object : ATInterstitialListener {
|
|
override fun onInterstitialAdLoaded() {
|
|
Log.d(JourneyApp.TAG, "LoadLoaded ${ad.mPlacementId}")
|
|
}
|
|
|
|
override fun onInterstitialAdLoadFail(p0: AdError?) {
|
|
Log.d(JourneyApp.TAG, "LoadFail:${p0?.code} ${p0?.desc}")
|
|
}
|
|
|
|
override fun onInterstitialAdClicked(p0: ATAdInfo?) {
|
|
|
|
}
|
|
|
|
override fun onInterstitialAdShow(p0: ATAdInfo?) {
|
|
Log.d(JourneyApp.TAG, "AdShow ${p0?.showId} ")
|
|
listener.showSuccess()
|
|
ad.load()
|
|
}
|
|
|
|
override fun onInterstitialAdClose(p0: ATAdInfo?) {
|
|
listener.showClose()
|
|
}
|
|
|
|
override fun onInterstitialAdVideoStart(p0: ATAdInfo?) {
|
|
|
|
}
|
|
|
|
override fun onInterstitialAdVideoEnd(p0: ATAdInfo?) {
|
|
|
|
}
|
|
|
|
override fun onInterstitialAdVideoError(p0: AdError?) {
|
|
listener.showFail()
|
|
}
|
|
|
|
})
|
|
}
|
|
|
|
|
|
@JvmStatic
|
|
fun showAD(activity: Activity, action: (type: Int) -> Unit) {
|
|
val readyAd = getReadyAd()
|
|
if (readyAd!= null) {
|
|
Log.d(JourneyApp.TAG, "readyAd ${readyAd.mPlacementId} ")
|
|
action.invoke(type_has_cache)
|
|
setCallBack(readyAd,object : AdListener {
|
|
override fun showSuccess() {
|
|
action.invoke(type_show_success)
|
|
}
|
|
|
|
override fun showFail() {
|
|
action.invoke(type_show_fail)
|
|
}
|
|
|
|
override fun showClose() {
|
|
action.invoke(type_show_close)
|
|
}
|
|
|
|
})
|
|
readyAd.show(activity)
|
|
} else {
|
|
action.invoke(type_no_cache)
|
|
|
|
}
|
|
}
|
|
|
|
|
|
} |