package com.weikou.beibeivideo.util.ad;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.os.Bundle;
|
import android.util.Log;
|
|
import com.bytedance.sdk.openadsdk.AdSlot;
|
import com.bytedance.sdk.openadsdk.TTAdConstant;
|
import com.bytedance.sdk.openadsdk.TTAdManager;
|
import com.bytedance.sdk.openadsdk.TTAdNative;
|
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
|
import com.qq.e.ads.nativ.NativeExpressADView;
|
import com.ut.device.UTDevice;
|
import com.weikou.beibeivideo.entity.ad.ExpressAdContainer;
|
import com.weikou.beibeivideo.entity.ad.RewardVideoAdContainer;
|
|
import java.util.List;
|
|
/**
|
* 激励视频广告管理
|
*/
|
public class RewardVideoAdManager {
|
|
|
final static String TAG = RewardVideoAdManager.class.getSimpleName();
|
|
AdUtil.AD_TYPE sourceType = AdUtil.AD_TYPE.gdt;
|
|
private TTAdNative mTTAdNative;
|
private Context mContext;
|
|
|
public RewardVideoAdManager(AdUtil.AD_TYPE adSource, Context context) {
|
sourceType = adSource;
|
this.mContext = context;
|
if (sourceType == AdUtil.AD_TYPE.csj) {
|
TTAdManager ttAdManager = null;
|
try {
|
ttAdManager = TTAdManagerHolder.get();
|
mTTAdNative = ttAdManager.createAdNative(context);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
}
|
}
|
|
|
//加载小广告
|
|
/**
|
* 加载广告
|
*
|
* @param widthDP
|
* @param heightDP
|
* @param hrizontal
|
* @param adLoadListener
|
*/
|
public void loadAd(int widthDP, int heightDP, String pid, boolean hrizontal, final IAdLoadListener adLoadListener, IAdShowListener showListener) {
|
if (sourceType == AdUtil.AD_TYPE.csj) {
|
loadCSJ(pid, widthDP, heightDP, hrizontal, new ICSJAdLoadListener() {
|
@Override
|
public void onSuccess(TTRewardVideoAd ad) {
|
adLoadListener.onSuccess(new RewardVideoAdContainer(ad));
|
}
|
|
@Override
|
public void onError(String msg) {
|
adLoadListener.onError(msg);
|
}
|
});
|
}
|
}
|
|
|
/**
|
* 穿山甲广告
|
*
|
* @param codeId
|
* @param width
|
* @param height
|
* @param count
|
* @param adLoadListener
|
*/
|
|
private TTRewardVideoAd mTTRewardVideoAd;
|
|
private void loadCSJ(String codeId, int width, int height, boolean hrizontal, final ICSJAdLoadListener adLoadListener) {
|
AdSlot adSlot = new AdSlot.Builder()
|
.setCodeId(codeId)
|
.setSupportDeepLink(true)
|
.setUserID(UTDevice.getUtdid(mContext))
|
.setExpressViewAcceptedSize(width, height)
|
.setOrientation(hrizontal ? TTAdConstant.HORIZONTAL : TTAdConstant.VERTICAL)
|
.build();
|
if (mTTAdNative == null) {
|
if (adLoadListener != null)
|
adLoadListener.onError("mTTAdNative为空");
|
return;
|
}
|
mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
|
//请求广告失败
|
@Override
|
public void onError(int code, String message) {
|
adLoadListener.onError(code + ":" + message);
|
}
|
|
//视频广告加载后,视频资源缓存到本地的回调,在此回调后,播放本地视频,流畅不阻塞。
|
@Override
|
public void onRewardVideoCached() {
|
adLoadListener.onSuccess(mTTRewardVideoAd);
|
}
|
|
@Override
|
public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
|
adLoadListener.onSuccess(mTTRewardVideoAd);
|
}
|
|
//视频广告的素材加载完毕,比如视频url等,在此回调后,可以播放在线视频,网络不好可能出现加载缓冲,影响体验。
|
@Override
|
public void onRewardVideoAdLoad(TTRewardVideoAd ad) {
|
mTTRewardVideoAd = ad;
|
}
|
});
|
}
|
|
/**
|
* 渲染模板和填充广告
|
*
|
* @param activity
|
* @param ad
|
* @param showListener
|
*/
|
public static void showAd(Activity activity, final RewardVideoAdContainer ad, RewardVideoAdManager.IAdShowListener showListener) {
|
if (activity == null)
|
return;
|
|
if (ad == null) {//广告为空
|
return;
|
}
|
if (ad.getCsj() != null) {
|
ad.getCsj().setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
|
//广告展示回调
|
@Override
|
public void onAdShow() {
|
Log.i(TAG, "onAdShow");
|
showListener.onShow();
|
}
|
|
//下载回调
|
@Override
|
public void onAdVideoBarClick() {
|
Log.i(TAG, "onAdVideoBarClick");
|
}
|
|
//广告关闭
|
@Override
|
public void onAdClose() {
|
Log.i(TAG, "onAdClose");
|
showListener.onClose();
|
}
|
|
//视频播放完成回调
|
@Override
|
public void onVideoComplete() {
|
Log.i(TAG, "onVideoComplete");
|
}
|
|
@Override
|
public void onVideoError() {
|
Log.i(TAG, "onVideoError");
|
showListener.onError("onVideoError");
|
}
|
|
//奖励验证回调
|
//rewardVerify:是否有效,rewardAmount:奖励梳理,rewardName:奖励名称,code:错误码,msg:错误信息
|
@Override
|
public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName, int code, String msg) {
|
Log.i(TAG, "onRewardVerify");
|
Log.i(TAG, "rewardVerify:" + rewardVerify);
|
Log.i(TAG, "rewardAmount:" + rewardAmount);
|
Log.i(TAG, "rewardName:" + rewardName);
|
Log.i(TAG, "code:" + code);
|
Log.i(TAG, "msg:" + msg);
|
|
if (rewardVerify) {
|
showListener.onReward();
|
}
|
}
|
|
@Override
|
public void onRewardArrived(boolean b, int i, Bundle bundle) {
|
|
}
|
|
//跳过视频播放回调
|
@Override
|
public void onSkippedVideo() {
|
Log.i(TAG, "onSkippedVideo");
|
}
|
});
|
ad.getCsj().showRewardVideoAd(activity, TTAdConstant.RitScenes.CUSTOMIZE_SCENES, "scenes_test");
|
ad.setCsj(null);
|
}
|
|
}
|
|
|
interface IGDTAdLoadListener {
|
public void onSuccess(List<NativeExpressADView> adList);
|
}
|
|
interface ICSJAdLoadListener {
|
public void onSuccess(TTRewardVideoAd ad);
|
|
public void onError(String msg);
|
}
|
|
|
public interface IAdLoadListener {
|
public void onSuccess(RewardVideoAdContainer ad);
|
|
public void onError(String msg);
|
}
|
|
public interface IAdShowListener {
|
//展示
|
public void onShow();
|
|
//奖励发放
|
public void onReward();
|
|
public void onError(String msg);
|
|
public void onClose();
|
}
|
|
public interface IAdEventListener {
|
public void closeAd(ExpressAdContainer ad);
|
}
|
|
|
}
|