Commit 868b20b8 authored by wangxuewei's avatar wangxuewei

[提交人]:王雪伟

[提交简述] :多多菜园
[实现方案] :1.0.3_广告预加载
parent e6864a2b
......@@ -86,6 +86,7 @@ class GameActivity : UnityPlayerActivity(), IDialogViewCloseCallback {
Constant.isReportXiaoman = false
}
openNotif()
AdManager.initCacheAdList(this)
}
override fun onDestroy() {
......
......@@ -2,6 +2,7 @@ package com.ym.library.config;
import com.ym.library.module.NewsEntity;
import com.zxhl.modulecommon.module.CacheAdEntity;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -14,4 +15,9 @@ import java.util.List;
public class AdCache {
public static HashMap<String,List<NewsEntity>> rewardList = new HashMap<>();
public static HashMap<String,List<NewsEntity>> nativeList = new HashMap<>();
public static HashMap<String, CacheAdEntity> cacheList = new HashMap<>();
// public static CacheAdEntity cacheRewardEntity = new CacheAdEntity();
public static int AdCacheNum=1;
public static long AdCacheTimeOut=0;
}
......@@ -23,6 +23,7 @@ import com.wannuosili.sdk.WNAdSdk
import com.ym.admodule.listener.ZXADDrawListener
import com.ym.admodule.rs.RSADUtils
import com.ym.library.bd.BDADUtils
import com.ym.library.config.AdCache.AdCacheTimeOut
import com.ym.library.csj.TTADUtils
import com.ym.library.gdt.GDTAdUtils
import com.ym.library.ks.KSADUtils
......@@ -38,6 +39,7 @@ import com.ym.library.sh.SHADUtils
import com.ym.library.sm.SMADUtils
import com.ym.library.utils.Utils
import com.ym.library.ylb.WNManager
import com.zxhl.modulecommon.module.CacheAdEntity
/**
......@@ -557,17 +559,27 @@ object AdManager {
AdCache.rewardList.put(slotName, totalEntity)
if (totalEntity.size > 0) {
var newsEntitys = totalEntity[0]
loadVideoAd(mActivity, newsEntitys, listener, this)
totalEntity.removeAt(0)
// AdCache.rewardList.put(slotName,result)
totalEntity.removeAt(0)
if (initTimeOut(mActivity)) {//超时了 重新缓存的过程中走加载直接播放流程
loadVideoAd(mActivity, newsEntitys, listener, this)
} else {//未超时 走缓存
playCacheRewardAd(mActivity, newsEntitys, listener, this)
}
}
}
}
loadVideoAd(mActivity, totalEntity[0], listener, callback)
totalEntity.removeAt(0)
AdCache.rewardList.put(slotName, totalEntity)
listener.onSuccess()
if (totalEntity.isNotEmpty()) {
var newsEntitys = totalEntity[0]
totalEntity.removeAt(0)
if (initTimeOut(mActivity)) {//超时了 重新缓存的过程中走加载直接播放流程
loadVideoAd(mActivity, newsEntitys, listener, callback)
} else {//未超时 走缓存
playCacheRewardAd(mActivity, newsEntitys, listener, callback)
}
listener.onSuccess()
}
}
private fun loadVideoAd(
......@@ -1031,4 +1043,147 @@ object AdManager {
SHADUtils.loadSHFeedSpecialVideoAd(activity, i)
}
///===================================缓存广告=========================================================================
fun initCacheAdList(activity: Activity) {
var mCacheList = AdCache.cacheList.get("CacheReward")
if (mCacheList != null) {
if (mCacheList.codeCfg?.video != null) {
for (index in mCacheList.codeCfg?.video!!.indices) {
initCacheReward(activity, mCacheList.codeCfg?.video!![index])
}
return
}
}
ZXADClient.adApi.getCacheAd().compose(RxSchedulers.observableIO2Main())
.subscribe(object : BaseObserver<CacheAdEntity>() {
override fun onSuccess(result: CacheAdEntity) {
if (result.adCacheCfg?.cacheNum != null) {
if (result.adCacheCfg?.cacheNum!! > 0) {
AdCache.AdCacheNum = result.adCacheCfg?.cacheNum!!
}
}
Log.e(TAG, "initCacheAdList成功 返回" + result.adCacheCfg?.cacheNum + "条广告")
Log.e(TAG, "initCacheAdList成功 返回" + result.adCacheCfg?.pkg + "条广告")
Log.e(TAG, "initCacheAdList成功 返回" + result.codeCfg?.video?.size + "条广告")
if (result != null) {
if (result.codeCfg?.video != null) {
AdCache.AdCacheTimeOut = System.currentTimeMillis()
AdCache.cacheList.put("CacheReward", result)
for (index in result.codeCfg?.video!!.indices) {
initCacheReward(activity, result.codeCfg?.video!![index])
}
}
}
}
override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
Log.e(TAG, "initCacheAdList失败 返回" + errorMsg)
//不做处理
}
})
}
private fun initCacheReward(activity: Activity, entity: CacheAdEntity.CodeConfig) {
if (entity.adType != 7) {
return
}
when (entity.adPlatform) {
ZXADType.PLANTFORM_AD_GDT -> {
GDTAdUtils.loadCacheRewardAd(activity, entity)
}
ZXADType.PLANTFORM_AD_TT -> {
TTADUtils.loadCacheRewardAd(activity, entity)
}
ZXADType.PLANTFORM_AD_SH -> {
// SHADUtils
}
ZXADType.PLANTFORM_AD_SIGNMOB -> {
SMADUtils.loadCacheRewardAd(activity, entity);
}
ZXADType.PLANTFORM_AD_BD -> {
// BDADUtils.loadCacheRewardAd(activity, entity);
}
//优量宝
ZXADType.PLANTFORM_SH_YLB -> {
WNManager.loadCacheRewardAd(activity, entity)
}
//快手
ZXADType.PLANTFORM_AD_KUS -> {
KSADUtils.loadCacheRewardAd(activity, entity)
}
//瑞狮
ZXADType.PLANTFORM_AD_RS -> {
RSADUtils.loadCacheRewardAd(activity, entity)
}
}
}
fun playCacheRewardAd(
activity: Activity,
newsEntity: NewsEntity,
listener: ZXADVideoListener,
callback: ZXADCallback
) {
when (newsEntity.adPlatform) {
ZXADType.PLANTFORM_AD_GDT -> {
GDTAdUtils.playCacheRewardAd(activity, newsEntity, listener, callback)
}
ZXADType.PLANTFORM_AD_TT -> {
TTADUtils.playCacheRewardAd(activity, newsEntity, listener, callback)
}
ZXADType.PLANTFORM_AD_SH -> {
callback.onAdFail()
// SHADUtils
}
ZXADType.PLANTFORM_AD_SIGNMOB -> {
SMADUtils.playCacheRewardAd(activity, listener, callback)
}
ZXADType.PLANTFORM_AD_BD -> {
callback.onAdFail()
// BDADUtils.playCacheRewardAd(activity, listener, callback)//没有配置
}
//优量宝
ZXADType.PLANTFORM_SH_YLB -> {
//code:60008 -- message:激励视频请求合法,广告暂时无填充
WNManager.playCacheRewardAd(activity, newsEntity, listener, callback)
}
//快手
ZXADType.PLANTFORM_AD_KUS -> {
KSADUtils.playCacheRewardAd(activity, newsEntity, listener, callback)
}
//瑞狮
ZXADType.PLANTFORM_AD_RS -> {
RSADUtils.playCacheRewardAd(activity, newsEntity, listener, callback)
}
}
}
private fun initTimeOut(activity: Activity): Boolean {
var mCacheList = AdCache.cacheList.get("CacheReward")
var time = mCacheList?.adCacheCfg?.timeout
if (time != null && time > 0) {
//最新刷新的时间+时间差 超过当前时间 说明未超时
if ((AdCacheTimeOut + (time * 1000)) > System.currentTimeMillis()) {
return false
}
//重置时间
AdCacheTimeOut = System.currentTimeMillis()
//清除缓存
cleanCacheRewardAd()
//初始化
initCacheAdList(activity)
}
return true
}
fun cleanCacheRewardAd() {
TTADUtils.cleanRewardCache()
GDTAdUtils.cleanRewardCache()
KSADUtils.cleanRewardCache()
SMADUtils.cleanRewardCache()
WNManager.cleanRewardCache()
}
}
\ No newline at end of file
......@@ -31,6 +31,8 @@ import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.KsSplashScreenAd;
import com.kwad.sdk.api.KsVideoPlayConfig;
import com.ym.library.R;
import com.ym.library.config.AdCache;
import com.ym.library.config.AdManager;
import com.ym.library.config.ZXADCallback;
import com.ym.library.config.ZXADSizeConfig;
import com.ym.library.listener.ZXADExpressListener;
......@@ -40,9 +42,13 @@ import com.ym.library.module.NewsEntity;
import com.ym.library.net.BaseActivity;
import com.ym.library.utils.EventUtils;
import com.ym.library.utils.LogUtils;
import com.zxhl.modulecommon.module.CacheAdEntity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* Craeted by ${junqi.li}
......@@ -606,4 +612,115 @@ public class KSADUtils {
}
}
//=============================================预加载===========================================
// private static final ConcurrentLinkedQueue<KsRewardVideoAd> mKsAdCache = new ConcurrentLinkedQueue<KsRewardVideoAd>();
//快手广告经常提示 检测异常退出
public static void loadCacheRewardAd(Activity activity, CacheAdEntity.CodeConfig entity) {
if (mKsAdCacheMap.get(entity.getCodeId()) != null) {
if (mKsAdCacheMap.get(entity.getCodeId()).size() >= AdCache.AdCacheNum) {
return;
}
}
KsScene scene = new KsScene.Builder(Long.parseLong(Objects.requireNonNull(entity.getCodeId()))).build(); // 此为测试posId,请联系快手平台申请正式posId
KsAdSDK.getLoadManager().loadRewardVideoAd(scene, new KsLoadManager.RewardVideoAdListener() {
@Override
public void onError(int code, String msg) {
Log.e(TAG, "快手 code:" + code + "msg " + msg);
}
@Override
public void onRewardVideoAdLoad(@Nullable List<KsRewardVideoAd> adList) {
Log.e(TAG, "快手 adList.size" + adList.size() + "entity CodeID" + entity.getCodeId());
if (adList != null && adList.size() > 0) {
// mKsAdCache.offer(adList.get(0));
setRewardVideo(entity.getCodeId(), adList.get(0));
}
}
});
}
public static void playCacheRewardAd(Activity activity, NewsEntity newsEntity, ZXADVideoListener listener,
ZXADCallback callback) {
// KsRewardVideoAd ksCacheRewardAd = mKsAdCache.poll();
KsRewardVideoAd ksCacheRewardAd = getRewardVideo(activity, newsEntity.getCodeId());
if (ksCacheRewardAd != null && ksCacheRewardAd.isAdEnable()) {
ksCacheRewardAd.setRewardAdInteractionListener(new KsRewardVideoAd.RewardAdInteractionListener() {
@Override
public void onAdClicked() {
listener.onAdClose();
}
@Override
public void onPageDismiss() {
}
@Override
public void onVideoPlayError(int i, int i1) {
listener.onError();
callback.onAdFail();
}
@Override
public void onVideoPlayEnd() {
}
@Override
public void onVideoPlayStart() {
listener.onAdShow();
callback.onAdSuccess();
}
@Override
public void onRewardVerify() {
listener.onSuccess();
}
});
ksCacheRewardAd.showRewardVideoAd(activity, null);
} else {
// playCacheRewardAd(activity, newsEntity, listener, callback);
callback.onAdFail();
}
}
private static final HashMap<String, ConcurrentLinkedQueue<KsRewardVideoAd>> mKsAdCacheMap = new HashMap<>();
private static void setRewardVideo(String codeId, KsRewardVideoAd ksRewardAD) {
if (mKsAdCacheMap.get(codeId) != null) {
ConcurrentLinkedQueue<KsRewardVideoAd> ksRewardVideoAds = mKsAdCacheMap.get(codeId);
if (ksRewardVideoAds == null) {
ksRewardVideoAds = new ConcurrentLinkedQueue<>();
}
ksRewardVideoAds.offer(ksRewardAD);
mKsAdCacheMap.put(codeId, ksRewardVideoAds);
} else {
ConcurrentLinkedQueue<KsRewardVideoAd> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
concurrentLinkedQueue.offer(ksRewardAD);
mKsAdCacheMap.put(codeId, concurrentLinkedQueue);
}
}
private static KsRewardVideoAd getRewardVideo(Activity activity, String codeId) {
if (mKsAdCacheMap.get(codeId) != null) {
ConcurrentLinkedQueue<KsRewardVideoAd> ksRewardVideoAds = mKsAdCacheMap.get(codeId);
if (ksRewardVideoAds != null && ksRewardVideoAds.size() > 0) {
KsRewardVideoAd ad = ksRewardVideoAds.poll();
if (ksRewardVideoAds.size() < AdCache.AdCacheNum) {
//再次预加载
AdManager.INSTANCE.initCacheAdList(activity);
}
return ad;
}
//再次预加载
AdManager.INSTANCE.initCacheAdList(activity);
Log.d(TAG, "Num KS codeID" + mKsAdCacheMap.get(codeId).size());
}
return null;
}
public static void cleanRewardCache(){
mKsAdCacheMap.clear();
}
}
package com.zxhl.modulecommon.module
import java.io.Serializable
/**
* @author (wangXuewei)
* @datetime 2020-11-07 17:03 GMT+8
* @detail :
*/
class CacheAdEntity : Serializable {
var adCacheCfg: AdCacheConfig? = null
// var codeConfigType: List<CodeConfig> = ArrayList()
var codeCfg: CodeConfigType? = null
class AdCacheConfig : Serializable {
var id: Int = 0
var pkg: String? = ""
var cacheNum: Int = 0
var timeout: Int = 0
var startTime: Int = 0
var cfgSwitch: Int = 0
var status: Int = 0
var createdAt: String? = ""
var updateAt: String? = ""
}
class CodeConfigType : Serializable {
var bigImg: List<CodeConfig> = ArrayList()
var splash: List<CodeConfig> = ArrayList()
var video: List<CodeConfig> = ArrayList()
}
class CodeConfig : Serializable {
var id: Int = 0
var adPlatform: String? = ""
var adPlatformId: Int = 0
var source: String? = ""
var pkg: String? = ""
var slotId: Int = 0
var slotName: String? = ""
var slotCode: String? = ""
var codeAppId: String? = ""
var codeId: String? = ""
var adType: Int = 0
var codeTitle: String? = ""
var sdkRenderType: Int = 0
var createAd: Int = 0
var updateAt: Int = 0
var sort: Int = 0
}
}
\ No newline at end of file
......@@ -210,10 +210,11 @@ public class NetConfig {
public static final String URL_AD_GET_STATUS = "app/v2/novel/getAdStatusNew";
public static final String URL_AD_NEW_GET_STATUS = "/app/v2/task/kkz/storeCoin";
public static final String URL_GET_AD_CACHE_V1 = "v1/ad/init";
}
public static final String URL_GET_ADS_V3 = "v4/ads";
public static class Game {
public static final String URL_GAME_AD_VIDEO_REPORT = "app/v1/game/report";//看视频上报
......
......@@ -2,6 +2,7 @@ package com.ym.library.net
import com.ym.library.module.AdIdConfig
import com.ym.library.module.NewsEntity
import com.zxhl.modulecommon.module.CacheAdEntity
import io.reactivex.Observable
import okhttp3.ResponseBody
import retrofit2.http.GET
......@@ -18,6 +19,13 @@ interface ZXAdApi {
@GET(NetConfig.URL_GET_ADS_V3)
fun getAd(@Query("slotName") slotId: String): Observable<Response<List<NewsEntity>>>
/**
* 获取缓存配置
*
* @return
*/
@GET(NetConfig.Ad.URL_GET_AD_CACHE_V1)
fun getCacheAd(): Observable<Response<CacheAdEntity>>
@GET
fun showAd(@Url url: String): Observable<ResponseBody>
......
......@@ -13,6 +13,7 @@ import cn.vlion.ad.moudle.natives.NativeManager
import cn.vlion.ad.moudle.natives.model.NativeFeedsData
import cn.vlion.ad.moudle.splash.SplashManager
import cn.vlion.ad.moudle.video.VideoManager
import com.ym.library.config.AdManager
import com.ym.library.config.ZXADCallback
import com.ym.library.config.ZXADSizeConfig
import com.ym.library.listener.ZXADExpressListener
......@@ -26,6 +27,7 @@ import com.ym.library.utils.EventUtils.onEventPullSuccess
import com.ym.library.utils.EventUtils.onEventShow
import com.ym.library.utils.EventUtils.onEventShowFail
import com.ym.library.utils.EventUtils.onEventUM
import com.zxhl.modulecommon.module.CacheAdEntity
import show.vion.cn.vlion_ad_inter.splash.SplashViewListener
import show.vion.cn.vlion_ad_inter.video.VideoViewListener
import show.vion.cn.vlion_ad_inter.vlionnative.NativeListener
......@@ -280,4 +282,99 @@ object RSADUtils {
handler.sendEmptyMessageDelayed(1, 20)
}
//=============================================预加载===========================================
// private val mRsAdCache: ConcurrentLinkedQueue<VideoManager> =
// ConcurrentLinkedQueue<VideoManager>()
private var zxVideoListener: ZXADVideoListener? = null
//预加载激励视频
fun loadCacheRewardAd(
activity: Activity,
entity: CacheAdEntity.CodeConfig
) {
// if (VideoManager.getInstance().isReady) {
// return
// }
//视频的填充适配类型
VideoManager.getInstance()
.setAdScalingModel(Config.AD_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
VideoManager.getInstance().setVideoOrientation(Config.AD_VERTIVAL_SCREEN_DISPLAY)
//视频的尺寸
VideoManager.getInstance().setImageAcceptedSize(1080, 1920)
//传入期望个性化模板广告的大小,单位dp。激励视频场景只需设置大于0即可 (必传的)
VideoManager.getInstance().setExpressViewAcceptedSize(6, 6)
VideoManager.getInstance()
.getVLionVideoView(activity, entity.codeId, object : VideoViewListener {
override fun onLoadVideo(adId: String) {
// mRsAdCache.offer(VideoManager.getInstance())
Log.e(TAG, "瑞狮 onLoadVideo:缓存成功,可以展示 adId=$adId")
// if (VideoManager.getInstance().isReady) {
// //展示广告
// adLoaded = true
// VideoManager.getInstance().showVideo();
// }
zxVideoListener?.onSuccess()
}
override fun onVideoPlayStart(adId: String) {
Log.e(TAG, "2瑞狮 onVideoPlayStart:adId=$adId")
zxVideoListener?.onAdShow()
}
override fun onVideoPlayFailed(adId: String, code: Int, msg: String) {
Log.e(TAG, "瑞狮 onVideoPlayFailed:adId=" + adId + "code=" + code + "msg=" + msg)
AdManager.initCacheAdList(activity)
zxVideoListener?.onError()
}
override fun onVideoClosed(adId: String) {
Log.e(TAG, "瑞狮 onVideoClosed:adId=$adId")
zxVideoListener?.onAdClose()
}
override fun onVideoClicked(adId: String) {
Log.e(TAG, "瑞狮 onVideoClicked:adId=$adId")
AdManager.initCacheAdList(activity)
// zxVideoListener?.onAdVideoBarClick()
}
override fun onVideoFinish(adId: String) {
Log.e(TAG, "onVideoFinish:adId=$adId")
AdManager.initCacheAdList(activity)
}
override fun onRewardVerify(adId: String) {
Log.e(TAG, "onRewardVerify:adId=$adId")
}
//广告请求失败
override fun onRequestFailed(adId: String, code: Int, errorMsg: String) {
AdManager.initCacheAdList(activity)
Log.e(
TAG,
"onRequestFailed:adId=" + adId + "code=" + code + "errorMsg=" + errorMsg
)
}
})
}
fun playCacheRewardAd(
activity: Activity,
newsEntity: NewsEntity,
listener: ZXADVideoListener, callback: ZXADCallback
) {
zxVideoListener = listener
if (VideoManager.getInstance().isReady) {
callback.onAdSuccess()
VideoManager.getInstance().showVideo();
} else {
callback.onAdFail()
}
}
}
\ No newline at end of file
......@@ -18,6 +18,8 @@ import com.sigmob.windad.rewardedVideo.WindRewardAdRequest
import com.sigmob.windad.rewardedVideo.WindRewardInfo
import com.sigmob.windad.rewardedVideo.WindRewardedVideoAd
import com.sigmob.windad.rewardedVideo.WindRewardedVideoAdListener
import com.ym.library.config.AdCache
import com.ym.library.config.AdManager
import com.ym.library.config.ZXADCallback
import com.ym.library.listener.ZXADSplashListener
import com.ym.library.listener.ZXADVideoListener
......@@ -31,6 +33,8 @@ import com.ym.library.utils.EventUtils.onEventShow
import com.ym.library.utils.EventUtils.onEventShowFail
import com.ym.library.utils.EventUtils.onEventUM
import com.ym.library.utils.SettingPreference
import com.zxhl.modulecommon.module.CacheAdEntity
import java.util.concurrent.ConcurrentLinkedQueue
object SMADUtils {
......@@ -272,4 +276,105 @@ object SMADUtils {
}
//=============================================预加载===========================================
private val mSgmAdCache: ConcurrentLinkedQueue<Map<WindRewardAdRequest, WindRewardedVideoAd>> =
ConcurrentLinkedQueue()
private var zxVideoListener: ZXADVideoListener? = null
//只能在播放完之后缓存
fun loadCacheRewardAd(
activity: Activity,
entity: CacheAdEntity.CodeConfig
) {
if (mSgmAdCache.size >= AdCache.AdCacheNum) {
return
}
var windRewardedVideoAd = WindRewardedVideoAd.sharedInstance()
//placementId 必填,USER_ID,OPTIONS可不填, "e859a9a8882"
var request = WindRewardAdRequest(entity.codeId, SettingPreference.getUid() ?: "", null)
windRewardedVideoAd.loadAd(request)
windRewardedVideoAd.setWindRewardedVideoAdListener(object : WindRewardedVideoAdListener {
override fun onVideoAdPlayError(p0: WindAdError?, p1: String?) {
Log.d(TAG, "Sigmob 激励 onVideoAdPlayError" + p0?.message)
AdManager.initCacheAdList(activity)
zxVideoListener?.onError()
}
override fun onVideoAdPreLoadFail(p0: String?) {
Log.d(TAG, "Sigmob 激励 onVideoAdPreLoadFail" + p0)
}
override fun onVideoAdPlayStart(p0: String?) {
Log.d(TAG, "Sigmob 激励 onVideoAdPlayStart")
zxVideoListener?.onAdShow()
}
override fun onVideoAdClosed(p0: WindRewardInfo?, p1: String?) {
Log.d(TAG, "Sigmob 激励 onVideoAdClosed")
zxVideoListener?.onAdClose()
AdManager.initCacheAdList(activity)
}
override fun onVideoAdPlayEnd(p0: String?) {
Log.d(TAG, "Sigmob 激励 onVideoAdPlayEnd" + p0)
}
override fun onVideoAdLoadSuccess(p0: String?) {
Log.d(TAG, "Sigmob 激励 onVideoAdLoadSuccess")
zxVideoListener?.onSuccess()
}
override fun onVideoAdPreLoadSuccess(p0: String?) {
Log.d(TAG, "Sigmob 激励 onVideoAdPreLoadSuccess" + p0)
}
override fun onVideoAdClicked(p0: String?) {
Log.d(TAG, "Sigmob 激励 onVideoAdClicked")
// zxVideoListener?.onAdVideoBarClick()
}
override fun onVideoAdLoadError(p0: WindAdError?, p1: String?) {
Log.d(TAG, "Sigmob 激励 onVideoAdLoadError" + p1)
AdManager.initCacheAdList(activity)
}
})
var map: Map<WindRewardAdRequest, WindRewardedVideoAd> =
mapOf(request to windRewardedVideoAd)
mSgmAdCache.offer(map)
}
fun playCacheRewardAd(
activity: Activity,
listener: ZXADVideoListener,
callback: ZXADCallback
) {
zxVideoListener = listener
if (mSgmAdCache.size > 0) {
var rewardVideoADMap = mSgmAdCache.poll()
rewardVideoADMap.forEach {
it.key
it.value
if (it.value.isReady(it.key.placementId)) {
//广告播放
callback.onAdSuccess()
it.value.show(activity, it.key)
} else {
callback.onAdFail()
}
}
} else {
callback.onAdFail()
}
// if (mSgmAdCache.size <= 0){
// AdManager.initCacheAdList(activity)
// }
}
fun cleanRewardCache() {
mSgmAdCache.clear()
}
}
\ No newline at end of file
......@@ -3,16 +3,22 @@ package com.ym.library.ylb
import android.app.Activity
import android.util.Log
import com.wannuosili.sdk.*
import com.ym.library.config.AdCache
import com.ym.library.config.AdManager
import com.ym.library.config.ZXADCallback
import com.ym.library.listener.ZXADVideoListener
import com.ym.library.module.NewsEntity
import com.ym.library.utils.EventUtils
import com.zxhl.modulecommon.module.CacheAdEntity
import java.util.concurrent.ConcurrentLinkedQueue
/**
*Craeted by ${junqi.li}
*Craeted by 2020/9/21
*/
object WNManager {
private const val TAG = "AdManager"
/**
* 显示激励视频广告
* @param slotId 代码位ID
......@@ -108,4 +114,120 @@ object WNManager {
}
})
}
//=============================================预加载===========================================
//预加载
fun loadCacheRewardAd(
activity: Activity,
entity: CacheAdEntity.CodeConfig
) {
if (mRewardCacheMap[entity.codeId] != null) {
if (mRewardCacheMap[entity.codeId]?.size!! >= AdCache.AdCacheNum) {
return
}
}
val slot = WNAdSlot.Builder()
.setSlotId(entity.codeId)
.setOrientation(WNAdConstant.REWARD_VIDEO_AD_PORTRAIT)
.build()
WNAdSdk.getAdManager()
.loadRewardVideoAd(slot, object : WNRewardVideoAd.RewardVideoAdListener {
override fun onError(code: Int, message: String) {
Log.e(TAG, "优量宝 code:${code} -- message:${message}")
}
override fun onLoad(ad: WNRewardVideoAd) {
setRewardVideo(entity.codeId, ad)
}
})
}
//播放缓存
fun playCacheRewardAd(
activity: Activity,
newsEntity: NewsEntity,
listener: ZXADVideoListener,
callback: ZXADCallback
) {
// var ad = mYLBAdCache.poll()
var ad = getRewardVideo(activity, newsEntity.codeId)
if (ad != null) {
ad.interactionListener = object : WNRewardVideoAd.InteractionListener {
override fun onAdShow() {
Log.e(TAG, "优量宝 激励视频展示")
callback.onAdSuccess()
}
override fun onAdClick() {
// listener.onAdVideoBarClick()
}
override fun onAdClose() {
Log.d(TAG, "优量宝 onAdClose")
listener.onAdClose()
}
override fun onVideoComplete() {
Log.d(TAG, "优量宝 onVideoComplete")
}
override fun onRewardVerify(
rewardVerify: Boolean,
rewardAmount: Int,
rewardName: String
) {
Log.d(TAG, "优量宝 onRewardVerify")
}
}
ad.showRewardVideoAd(activity)
} else {
callback.onAdFail()
}
}
// private val mYLBAdCache: ConcurrentLinkedQueue<WNRewardVideoAd> =
// ConcurrentLinkedQueue<WNRewardVideoAd>()
private val mRewardCacheMap = HashMap<String, ConcurrentLinkedQueue<WNRewardVideoAd>>()
private fun setRewardVideo(codeId: String?, ksRewardAD: WNRewardVideoAd) {
if (codeId == null)
return
if (mRewardCacheMap[codeId] != null) {
var ksRewardVideoAds = mRewardCacheMap[codeId]
if (ksRewardVideoAds == null) {
ksRewardVideoAds = ConcurrentLinkedQueue()
}
ksRewardVideoAds.offer(ksRewardAD)
mRewardCacheMap[codeId] = ksRewardVideoAds
} else {
val concurrentLinkedQueue = ConcurrentLinkedQueue<WNRewardVideoAd>()
concurrentLinkedQueue.offer(ksRewardAD)
mRewardCacheMap[codeId] = concurrentLinkedQueue
}
Log.d(TAG, "CodeID :${codeId}Num YLB codeID" + mRewardCacheMap[codeId]?.size)
}
private fun getRewardVideo(activity: Activity, codeId: String?): WNRewardVideoAd? {
if (mRewardCacheMap[codeId] != null) {
val ksRewardVideoAds = mRewardCacheMap[codeId]
if (ksRewardVideoAds != null && ksRewardVideoAds.size > 0) {
var ad = ksRewardVideoAds.poll()
if (ksRewardVideoAds.size < AdCache.AdCacheNum) {
//再次预加载
AdManager.initCacheAdList(activity)
}
return ad
}
//再次预加载
AdManager.initCacheAdList(activity)
Log.d(TAG, "Num WNM codeID" + mRewardCacheMap[codeId]?.size)
}
return null
}
fun cleanRewardCache() {
mRewardCacheMap.clear()
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment