Commit d8ff67e0 authored by mengcuiguang's avatar mengcuiguang

代码优化

parent eb94b1c5
package com.mints.flowbox.ad.video;
import android.app.Activity;
import com.bytedance.msdk.api.AdError;
import com.bytedance.msdk.api.AdSlot;
import com.bytedance.msdk.api.TTAdConstant;
import com.bytedance.msdk.api.TTMediationAdSdk;
import com.bytedance.msdk.api.TTSettingConfigCallback;
import com.bytedance.msdk.api.TTVideoOption;
import com.bytedance.msdk.api.reward.RewardItem;
import com.bytedance.msdk.api.reward.TTRewardAd;
import com.bytedance.msdk.api.reward.TTRewardedAdListener;
import com.bytedance.msdk.api.reward.TTRewardedAdLoadCallback;
import com.mints.flowbox.BuildConfig;
import com.mints.flowbox.ad.express.TTPreLoadCarrierExpressManager;
import com.mints.flowbox.ad.express.TTPreLoadExpressManager;
import com.mints.flowbox.ad.video.base.BaseVideoAd;
import com.mints.flowbox.ad.video.base.VideoAdStatusListener;
import com.mints.flowbox.common.AppConfig;
import com.mints.flowbox.common.Constant;
import com.mints.flowbox.manager.CsjGroMoreManager;
import com.mints.flowbox.manager.TrackManager;
import com.mints.flowbox.manager.UserManager;
import com.mints.flowbox.utils.LogUtil;
import com.mints.flowbox.utils.ToastUtil;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
/**
* GroMore激励视频广告 预加载
*/
public class PreCsjGroMoreVideoAdManager extends BaseVideoAd {
private static final String TAG = PreCsjGroMoreVideoAdManager.class.getSimpleName();
private int curCoin;
private String extraId;
private String carrierType;
private VideoAdStatusListener videoAdStatusListener;
private static PreCsjGroMoreVideoAdManager _inst;
private TTRewardAd mttRewardAd;
private WeakReference<Activity> activity;
private boolean isClickScreen = true; // 是否点击屏幕跳转广告
private int isLoadSuccess = 0;//0-加载失败/未加载 1-加载中 2-加载成功
public static PreCsjGroMoreVideoAdManager getInstance() {
if (_inst == null) {
_inst = new PreCsjGroMoreVideoAdManager();
}
return _inst;
}
@Override
public void onDestroy() {
TTMediationAdSdk.unregisterConfigCallback(mSettingConfigCallback);
if (mttRewardAd != null) {
mttRewardAd.destroy();
}
activity = null;
}
/**
* 激励视频
*
* @param carrierType
*/
@Override
public void loadAd(Activity activity, int curCoin, String carrierType, String extraId) {
}
/**
* 预加载
* @param activity
*/
public void preLoadAd(Activity activity) {
// 只有加载失败/未加载时才预加载
if (isLoadSuccess == 0) {
isLoadSuccess = 1;
this.activity = new WeakReference<>(activity);
/**
* 判断当前是否存在config 配置 ,如果存在直接加载广告 ,如果不存在则注册config加载回调
*/
if (TTMediationAdSdk.configLoadSuccess()) {
loadAd(CsjGroMoreManager.INSTANCE.getAPP_IN_MAIN_AD_UNIT_VIDEO_ID(), TTAdConstant.VERTICAL);
} else {
TTMediationAdSdk.registerConfigCallback(mSettingConfigCallback); //不用使用内部类,否则在ondestory中无法移除该回调
}
}
}
public void showAd(Activity activity, int curCoin, String carrierType, String extraId) {
this.activity = new WeakReference<>(activity);
// 若上一个广告还在加载中则返回
if (isLoadSuccess == 1) {
ToastUtil.show(this.activity.get(), "加载太快了,请休息一下吧");
return;
}
if (BuildConfig.DEBUG) {
LogUtil.d(TAG, "showAd isLoadSuccess=" + isLoadSuccess + " isReady=" + mttRewardAd.isReady());
}
if (isLoadSuccess == 2 && mttRewardAd != null && mttRewardAd.isReady() && this.activity != null) {
isClickScreen = true;
this.curCoin = curCoin;
this.extraId = extraId;
this.carrierType = carrierType;
//在获取到广告后展示,强烈建议在onRewardVideoCached回调后,展示广告,提升播放体验
//该方法直接展示广告
//展示广告,并传入广告展示的场景
mttRewardAd.showRewardAd(this.activity.get(), mTTRewardedAdListener);
HashMap<String, Object> vo = new HashMap<>();
vo.put("adcode", mttRewardAd.getAdNetworkRitId());
vo.put("ecpm", mttRewardAd.getPreEcpm());
vo.put("adSource", mttRewardAd.getAdNetworkPlatformId());
vo.put("carrierType", carrierType);
TrackManager.getInstance().reporGromeEcpm(vo);
LogUtil.d(TAG, "adNetworkPlatformId: " + mttRewardAd.getAdNetworkPlatformId()); //获取展示广告对应的adn的值 具体值见NetworkPlatformConst类 -3: 无权限 -2: 暂无数据
LogUtil.d(TAG, "adNetworkRitId:" + mttRewardAd.getAdNetworkRitId()); //获取展示广告对应的代码位 具体值见NetworkPlatformConst类 "-3": 无权限 "-2"
LogUtil.d(TAG, "preEcpm: " + mttRewardAd.getPreEcpm()); //获取展示广告预估ecpm价格,单位是分 具体值见NetworkPlatformConst类 "-3": 无权限 "-2": 暂无数据 "-1":平台未有填写的预估ecpm价格
}
// 状态重置
isLoadSuccess = 0;
}
/**
* config回调
*/
private TTSettingConfigCallback mSettingConfigCallback = new TTSettingConfigCallback() {
@Override
public void configLoad() {
loadAd(CsjGroMoreManager.INSTANCE.getAPP_IN_MAIN_AD_UNIT_VIDEO_ID(), TTAdConstant.VERTICAL);
}
};
private void loadAd(final String adUnitId, int orientation) {
/**
* 注:每次加载激励视频广告的时候需要新建一个TTRewardAd,否则可能会出现广告填充问题
* ( 例如:mttRewardAd = new TTRewardAd(this, adUnitId);)
*/
mttRewardAd = new TTRewardAd(activity.get(), adUnitId);
TTVideoOption videoOption = new TTVideoOption.Builder()
.setMuted(true)//对所有SDK的激励广告生效,除需要在平台配置的SDK,如穿山甲SDK
.setAdmobAppVolume(0f)//配合Admob的声音大小设置[0-1]
.build();
//创建广告请求参数AdSlot,具体参数含义参考文档
AdSlot.Builder adSlotBuilder = new AdSlot.Builder()
.setTTVideoOption(videoOption)
.setUserID(UserManager.getInstance().getUserID())//用户id,必传参数
.setAdStyleType(AdSlot.TYPE_EXPRESS_AD) // 确保该聚合广告位下所有gdt代码位都是同一种类型(模版或非模版)。
.setOrientation(orientation);//必填参数,期望视频的播放方向:TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
//请求广告
mttRewardAd.loadRewardAd(adSlotBuilder.build(), new TTRewardedAdLoadCallback() {
@Override
public void onRewardVideoLoadFail(AdError adError) {
isLoadSuccess = 0;
TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_GROMORE, Constant.EVENT_TYPE_ONE, String.valueOf(adError.code), adError.message);
LogUtil.d(TAG, "onError " + String.valueOf(adError.code) + adError.message);
if (videoAdStatusListener != null) {
videoAdStatusListener.adFail();
}
}
@Override
public void onRewardVideoAdLoad() {
isLoadSuccess = 2;
}
@Override
public void onRewardVideoCached() {
isLoadSuccess = 2;
if (mttRewardAd != null) {
} else {
TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_GROMORE, Constant.EVENT_TYPE_ONE, "999999", "Cached Fail");
LogUtil.d(TAG, " onRewardVideoCached onError ");
if (videoAdStatusListener != null) {
videoAdStatusListener.adFail();
}
}
}
});
}
/**
* 激励视频交互回调
*/
private TTRewardedAdListener mTTRewardedAdListener = new TTRewardedAdListener() {
/**
* 广告的展示回调 每个广告仅回调一次
*/
public void onRewardedAdShow() {
LogUtil.d(TAG, "onRewardedAdShow");
TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_GROMORE, Constant.EVENT_TYPE_ZERO, "", "");
}
/**
* 注意Admob的激励视频不会回调该方法
*/
@Override
public void onRewardClick() {
LogUtil.d(TAG, "onRewardClick");
HashMap<String, Object> vo = new HashMap<>();
vo.put("ecpmId", AppConfig.gromoreId);
TrackManager.getInstance().reportGromoreAdClick(vo);
if (AppConfig.needReportClickAdEvent) {
TrackManager.getInstance().reportClickAdEvent();
}
TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_GROMORE, Constant.EVENT_TYPE_TWO, "", "");
if (isClickScreen) {
// 防止重复
TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_GROMORE, Constant.EVENT_TYPE_FOUR, "", "");
isClickScreen = false;
}
if (videoAdStatusListener != null) {
videoAdStatusListener.adDownload();
}
}
/**
* 广告关闭的回调
*/
public void onRewardedAdClosed() {
LogUtil.d(TAG, "onRewardedAdClosed");
TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_GROMORE, Constant.EVENT_TYPE_THREE, "", "");
HashMap<String, Object> voTemp = getAdMapVO(carrierType, Constant.AD_SOURCE_GROMORE, extraId, curCoin);
if (mttRewardAd != null) {
String groAdsouce = String.valueOf(mttRewardAd.getAdNetworkPlatformId());
switch (mttRewardAd.getAdNetworkPlatformId()) {
case 1:
groAdsouce = "CSJ";
break;
case 3:
groAdsouce = "YLH";
break;
case 7:
groAdsouce = "KS";
break;
}
voTemp.put("gromoreAdsource", groAdsouce);
voTemp.put("ecpmId", AppConfig.gromoreId);
}
TrackManager.getInstance().reportAddCoinMsg(activity.get(), voTemp);
if (mttRewardAd != null) {
/**
* SDK_NAME_NO_PERMISSION = -3; //无权限
* * SDK_NAME_NO_DATA = -2; //暂无数据
* * SDK_NAME_PANGLE = 1;
* * SDK_NAME_ADMOB = 2;
* * SDK_NAME_GDT = 3;
* * SDK_NAME_MT = 4;
* * SDK_NAME_UNITY = 5;
* * SDK_NAME_BAIDU = 6;
* * SDK_NAME_KS = 7;
* * SDK_NAME_SIGMOB = 8;
*/
HashMap<String, Object> vo = new HashMap<>();
vo.put("adcode", mttRewardAd.getAdNetworkRitId());
vo.put("ecpm", mttRewardAd.getPreEcpm());
vo.put("adSource", mttRewardAd.getAdNetworkPlatformId());
vo.put("adType", Constant.GRO_MORE_ADTYPE2);
vo.put("ecpmId", AppConfig.gromoreId);
vo.put("adid", CsjGroMoreManager.INSTANCE.getAPP_IN_MAIN_AD_UNIT_VIDEO_ID());
TrackManager.getInstance().cmtGroMoreInfo(vo);
}
if (videoAdStatusListener != null) {
videoAdStatusListener.adSuccess();
}
}
/**
* 视频播放完毕的回调 Admob广告不存在该回调
*/
public void onVideoComplete() {
LogUtil.d(TAG, "onVideoComplete");
}
/**
* 视频播放失败的回调 - Mintegral GDT Admob广告不存在该回调
*/
public void onVideoError() {
LogUtil.d(TAG, "onVideoError");
TrackManager.getInstance().addCallImp(Constant.AD_SOURCE_GROMORE, Constant.EVENT_TYPE_ONE, "999999", "VideoError");
if (videoAdStatusListener != null) {
videoAdStatusListener.adFail();
}
}
/**
* 激励视频播放完毕,验证是否有效发放奖励的回调
*/
public void onRewardVerify(RewardItem rewardItem) {
Map<String, Object> customData = rewardItem.getCustomData();
if (customData != null) {
String adnName = (String) customData.get(RewardItem.KEY_ADN_NAME);
switch (adnName) {
case RewardItem.KEY_GDT:
LogUtil.d(TAG, "rewardItem gdt: " + customData.get(RewardItem.KEY_GDT_TRANS_ID));
break;
}
}
}
/**
* - Mintegral GDT Admob广告不存在该回调
*/
@Override
public void onSkippedVideo() {
}
};
public void setVideoAdStatusListener(VideoAdStatusListener videoAdStatusListener) {
this.videoAdStatusListener = videoAdStatusListener;
}
}
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