package com.ysvideo.zhibo.app.util.ad;
|
|
import android.app.Activity;
|
import android.content.Context;
|
import android.util.Log;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.widget.FrameLayout;
|
|
import com.bytedance.sdk.openadsdk.AdSlot;
|
import com.bytedance.sdk.openadsdk.TTAdDislike;
|
import com.bytedance.sdk.openadsdk.TTAdManager;
|
import com.bytedance.sdk.openadsdk.TTAdNative;
|
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
|
import com.qq.e.ads.cfg.VideoOption;
|
import com.qq.e.ads.nativ.ADSize;
|
import com.qq.e.ads.nativ.NativeExpressAD;
|
import com.qq.e.ads.nativ.NativeExpressADView;
|
import com.qq.e.comm.util.AdError;
|
import com.ysvideo.zhibo.app.entity.ad.ExpressAdContainer;
|
import com.ysvideo.zhibo.lib.common.util.common.DimenUtils;
|
import com.ysvideo.zhibo.library_ad.AdUtil;
|
import com.ysvideo.zhibo.library_ad.TTAdManagerHolder;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
/**
|
* 信息流广告管理
|
*/
|
public class ExpressAdManager {
|
|
|
final static String TAG = "ExpressAdManager";
|
|
private TTAdNative mTTAdNative;
|
private Context mContext;
|
private static ExpressAdManager instance;
|
|
public static ExpressAdManager getInstance(Context context) {
|
if (instance == null) {
|
instance = new ExpressAdManager(context);
|
}
|
return instance;
|
}
|
|
|
public ExpressAdManager(Context context) {
|
this.mContext = context;
|
if (AdUtil.isCanInitCSJ()) {
|
TTAdManager ttAdManager = null;
|
try {
|
ttAdManager = TTAdManagerHolder.get();
|
mTTAdNative = ttAdManager.createAdNative(context);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
|
/**
|
* 加载并渲染广告
|
*
|
* @param activity
|
* @param adInfo
|
* @param container
|
* @param eventListener
|
*/
|
public void loadAndRender(Activity activity, ExpressAdInfo adInfo, final ViewGroup container, final IAdEventListener eventListener) {
|
if (adInfo == null) {
|
return;
|
}
|
if (adInfo.getAdType() == null) {
|
return;
|
}
|
|
if (adInfo.getAdType() == AdUtil.AD_TYPE.gdt) {
|
loadGDT(adInfo.getPid(), adInfo.getWidth(), adInfo.getHeight(), 1, new IGDTAdLoadListener() {
|
@Override
|
public void onSuccess(List<NativeExpressADView> adList) {
|
if (adList != null && adList.size() > 0) {
|
NativeExpressADView adView = adList.get(0);
|
if (adView.getParent() != null) {
|
((ViewGroup) adView.getParent()).removeAllViews();
|
}
|
|
container.removeAllViews();
|
container.addView(adView);
|
adView.render();
|
}
|
}
|
}, new IAdEventListener() {
|
@Override
|
public void closeAd(ExpressAdContainer ad) {
|
container.removeAllViews();
|
if (eventListener != null) {
|
eventListener.closeAd(ad);
|
}
|
}
|
}, mContext);
|
} else if (adInfo.getAdType() == AdUtil.AD_TYPE.csj) {
|
loadCSJ(adInfo, 1, new ICSJAdLoadListener() {
|
@Override
|
public void onSuccess(List<TTNativeExpressAd> adList) {
|
if (adList != null && adList.size() > 0) {
|
final TTNativeExpressAd ad = adList.get(0);
|
ad.setDislikeCallback(activity, new TTAdDislike.DislikeInteractionCallback() {
|
|
|
@Override
|
public void onShow() {
|
|
}
|
|
@Override
|
public void onSelected(int i, String s, boolean b) {
|
container.removeAllViews();
|
if (eventListener != null) {
|
eventListener.closeAd(new ExpressAdContainer(ad));
|
}
|
}
|
|
@Override
|
public void onCancel() {
|
|
}
|
|
});
|
ad.render();
|
View adView = ad.getExpressAdView();
|
if (adView.getParent() != null) {
|
((ViewGroup) adView.getParent()).removeAllViews();
|
}
|
container.removeAllViews();
|
container.addView(adView);
|
}
|
}
|
});
|
}
|
}
|
|
|
/**
|
* 视频播放器底部小广告
|
*
|
* @param adLoadListener
|
*/
|
public void loadVideoDetailPlayerBottomAd(final IAdLoadListener adLoadListener) {
|
//加载大图
|
// String pid = null;
|
// if (sourceType == AdUtil.AD_TYPE.csj) {
|
// pid = "945375047";
|
// }
|
// //获取屏幕的宽
|
// int deviceWidth = DimenUtils.getScreenWidth(mContext);
|
// deviceWidth = DimenUtils.px2dip(mContext, deviceWidth);
|
// int width = (deviceWidth - 20);
|
// int height = 80;
|
// loadAd(width, height, pid, 1, adLoadListener);
|
}
|
|
|
/**
|
* 相关推荐底部小广告
|
*
|
* @param adLoadListener
|
*/
|
public void loadVideoDetailRelativeVideoBottomAd(final IAdLoadListener adLoadListener) {
|
//加载大图
|
// String pid = null;
|
// if (sourceType == AdUtil.AD_TYPE.csj) {
|
// pid = "945375047";
|
// }
|
// if (mContext == null)
|
// return;
|
// //获取屏幕的宽
|
// int deviceWidth = DimenUtils.getScreenWidth(mContext);
|
// deviceWidth = DimenUtils.px2dip(mContext, deviceWidth);
|
// int width = (deviceWidth - 20);
|
// int height = (int) (width * 0.5625);
|
// loadAd(width, height, pid, 1, adLoadListener);
|
}
|
|
public void loadExpressAd(AdUtil.AD_TYPE adType, String pid, Integer width, Integer height, ViewGroup container, final IAdLoadListener adLoadListener) {
|
|
|
}
|
|
|
/**
|
* 加载广点通广告
|
*
|
* @param positionId
|
* @param count
|
* @param adLoadListener
|
* @param context
|
*/
|
private void loadGDT(String positionId, int widthDP, int heightDP, int count, final IGDTAdLoadListener adLoadListener, final IAdEventListener adEventListener, Context context) {
|
NativeExpressAD nativeExpressAD = new NativeExpressAD(context, new ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT),
|
positionId, new NativeExpressAD.NativeExpressADListener() {
|
|
@Override
|
public void onADLoaded(List<NativeExpressADView> list) {
|
if (adLoadListener != null)
|
adLoadListener.onSuccess(list);
|
}
|
|
@Override
|
public void onRenderFail(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onRenderSuccess(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADExposure(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADClicked(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
@Override
|
public void onADClosed(NativeExpressADView nativeExpressADView) {
|
if (adEventListener != null) {
|
adEventListener.closeAd(new ExpressAdContainer(nativeExpressADView));
|
}
|
}
|
|
@Override
|
public void onADLeftApplication(NativeExpressADView nativeExpressADView) {
|
|
}
|
|
|
@Override
|
public void onNoAD(AdError adError) {
|
if (adLoadListener != null)
|
adLoadListener.onSuccess(null);
|
}
|
}); // 传入Activity
|
nativeExpressAD.setVideoOption(new VideoOption.Builder()
|
.setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI) // WIFI 环境下可以自动播放视频
|
.setAutoPlayMuted(true) // 自动播放时为静音
|
.build()); //
|
nativeExpressAD.loadAD(count);
|
}
|
|
/**
|
* 穿山甲广告
|
*
|
* @param adInfo
|
* @param count
|
* @param adLoadListener
|
*/
|
private void loadCSJ(ExpressAdInfo adInfo, int count, final ICSJAdLoadListener adLoadListener) {
|
count = count > 3 ? 3 : count;
|
AdSlot adSlot = new AdSlot.Builder()
|
.setCodeId(adInfo.getPid())
|
.setSupportDeepLink(true)
|
.setExpressViewAcceptedSize(adInfo.getWidth(), adInfo.getHeight())
|
.setAdCount(count) //请求广告数量为1到3条
|
.build();
|
mTTAdNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
|
@Override
|
public void onError(int i, String s) {
|
adLoadListener.onSuccess(null);
|
}
|
|
@Override
|
public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
|
adLoadListener.onSuccess(list);
|
}
|
});
|
}
|
|
|
/**
|
* 渲染模板和填充广告
|
*
|
* @param activity
|
* @param ad
|
* @param fl_ad
|
* @param adEventListener
|
*/
|
public static void renderAndFillAd(Activity activity, final ExpressAdContainer ad, final FrameLayout fl_ad, final IAdEventListener adEventListener) {
|
|
if (ad == null) {//广告为空
|
fl_ad.setVisibility(View.GONE);
|
} else {
|
fl_ad.setVisibility(View.VISIBLE);
|
if (ad.getGdt() != null) {
|
if (fl_ad.getChildCount() > 0 && fl_ad.getChildAt(0) == ad.getGdt()) {
|
return;
|
}
|
fl_ad.removeAllViews();
|
ad.getGdt().render();
|
if (ad.getGdt().getParent() != null) {
|
((ViewGroup) ad.getGdt().getParent()).removeAllViews();
|
}
|
fl_ad.addView(ad.getGdt());
|
} else if (ad.getCsj() != null) {
|
if (fl_ad.getChildCount() > 0 && fl_ad.getChildAt(0) == ad.getCsj().getExpressAdView()) {
|
return;
|
}
|
|
ad.getCsj().setDislikeCallback(activity, new TTAdDislike.DislikeInteractionCallback() {
|
|
|
@Override
|
public void onShow() {
|
|
}
|
|
@Override
|
public void onSelected(int i, String s, boolean b) {
|
if (adEventListener != null) {
|
adEventListener.closeAd(ad);
|
}
|
}
|
|
@Override
|
public void onCancel() {
|
|
}
|
|
});
|
ad.getCsj().render();
|
if (ad.getCsj().getExpressAdView().getParent() != null) {
|
((ViewGroup) ad.getCsj().getExpressAdView().getParent()).removeAllViews();
|
}
|
fl_ad.addView(ad.getCsj().getExpressAdView());
|
}
|
}
|
|
}
|
|
|
/**
|
* 渲染广告
|
*
|
* @param activity
|
* @param ad
|
* @param adRenderListener
|
* @param adEventListener
|
*/
|
public static void renderAd(Activity activity, final ExpressAdContainer ad, final IAdRenderListener adRenderListener, final IAdEventListener adEventListener) {
|
|
if (ad == null) {//广告为空
|
return;
|
}
|
|
if (ad.getGdt() != null) {
|
ad.getGdt().render();
|
List<ExpressAdContainer> list = new ArrayList<>();
|
list.add(ad);
|
if (adRenderListener != null)
|
adRenderListener.onRenderSuccess(list);
|
} else if (ad.getCsj() != null) {
|
ad.getCsj().setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
|
@Override
|
public void onAdClicked(View view, int i) {
|
|
}
|
|
@Override
|
public void onAdShow(View view, int i) {
|
|
}
|
|
@Override
|
public void onRenderFail(View view, String s, int i) {
|
List<ExpressAdContainer> list = new ArrayList<>();
|
list.add(ad);
|
if (adRenderListener != null)
|
adRenderListener.onRenderFail(list);
|
}
|
|
@Override
|
public void onRenderSuccess(View view, float v, float v1) {
|
Log.d(TAG, "onRenderSuccess");
|
List<ExpressAdContainer> list = new ArrayList<>();
|
list.add(ad);
|
if (adRenderListener != null)
|
adRenderListener.onRenderSuccess(list);
|
}
|
});
|
if (activity != null)
|
ad.getCsj().setDislikeCallback(activity, new TTAdDislike.DislikeInteractionCallback() {
|
|
@Override
|
public void onShow() {
|
|
}
|
|
@Override
|
public void onSelected(int i, String s, boolean b) {
|
if (adEventListener != null) {
|
adEventListener.closeAd(ad);
|
}
|
}
|
|
@Override
|
public void onCancel() {
|
|
}
|
|
|
});
|
ad.getCsj().render();
|
}
|
|
|
}
|
|
/**
|
* 为广告绑定关闭事件
|
*
|
* @param activity
|
* @param ad
|
* @param adEventListener
|
*/
|
public static void bindCloseListener(Activity activity, final ExpressAdContainer ad, final IAdEventListener adEventListener) {
|
|
if (ad == null) {//广告为空
|
return;
|
}
|
|
if (ad.getGdt() != null) {
|
return;
|
} else if (ad.getCsj() != null) {
|
ad.getCsj().setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
|
@Override
|
public void onAdClicked(View view, int i) {
|
|
}
|
|
@Override
|
public void onAdShow(View view, int i) {
|
|
}
|
|
@Override
|
public void onRenderFail(View view, String s, int i) {
|
|
}
|
|
@Override
|
public void onRenderSuccess(View view, float v, float v1) {
|
|
}
|
});
|
ad.getCsj().setDislikeCallback(activity, new TTAdDislike.DislikeInteractionCallback() {
|
|
@Override
|
public void onShow() {
|
|
}
|
|
@Override
|
public void onSelected(int i, String s, boolean b) {
|
if (adEventListener != null) {
|
adEventListener.closeAd(ad);
|
}
|
}
|
|
@Override
|
public void onCancel() {
|
|
}
|
|
});
|
}
|
|
|
}
|
|
|
/**
|
* 填充广告
|
*
|
* @param ad
|
* @param fl_ad
|
*/
|
public static void fillAd(final ExpressAdContainer ad, final FrameLayout fl_ad) {
|
|
fl_ad.post(new Runnable() {
|
@Override
|
public void run() {
|
if (ad == null) {//广告为空
|
fl_ad.setVisibility(View.GONE);
|
} else {
|
fl_ad.setVisibility(View.VISIBLE);
|
if (ad.getGdt() != null) {
|
if (fl_ad.getChildCount() > 0 && fl_ad.getChildAt(0) == ad.getGdt()) {
|
return;
|
}
|
if (ad.getGdt().getParent() != null) {
|
((ViewGroup) ad.getGdt().getParent()).removeAllViews();
|
}
|
fl_ad.removeAllViews();
|
fl_ad.addView(ad.getGdt());
|
} else if (ad.getCsj() != null) {
|
if (fl_ad.getChildCount() > 0 && fl_ad.getChildAt(0) == ad.getCsj().getExpressAdView()) {
|
return;
|
}
|
if (ad.getCsj().getExpressAdView().getParent() != null) {
|
((ViewGroup) ad.getCsj().getExpressAdView().getParent()).removeAllViews();
|
}
|
fl_ad.removeAllViews();
|
fl_ad.addView(ad.getCsj().getExpressAdView());
|
}
|
}
|
if (fl_ad.getChildCount() > 0) {
|
fl_ad.getChildAt(0).setVisibility(View.VISIBLE);
|
fl_ad.getChildAt(0).post(new Runnable() {
|
@Override
|
public void run() {
|
|
}
|
});
|
}
|
}
|
});
|
|
|
}
|
|
|
interface IGDTAdLoadListener {
|
void onSuccess(List<NativeExpressADView> adList);
|
}
|
|
interface ICSJAdLoadListener {
|
void onSuccess(List<TTNativeExpressAd> adList);
|
}
|
|
|
public interface IAdLoadListener {
|
void onSuccess(List<ExpressAdContainer> adList);
|
}
|
|
public interface IAdRenderListener {
|
void onRenderSuccess(List<ExpressAdContainer> adList);
|
|
void onRenderFail(List<ExpressAdContainer> adList);
|
}
|
|
public interface IAdEventListener {
|
void closeAd(ExpressAdContainer ad);
|
}
|
|
|
public interface IAdShowListener {
|
void show();
|
}
|
|
public static class ExpressAdInfo {
|
|
private AdUtil.AD_TYPE adType;
|
private String pid;
|
private Integer width;
|
private Integer height;
|
|
public AdUtil.AD_TYPE getAdType() {
|
return adType;
|
}
|
|
public void setAdType(AdUtil.AD_TYPE adType) {
|
this.adType = adType;
|
}
|
|
public String getPid() {
|
return pid;
|
}
|
|
public void setPid(String pid) {
|
this.pid = pid;
|
}
|
|
public Integer getWidth() {
|
return width;
|
}
|
|
public void setWidth(Integer width) {
|
this.width = width;
|
}
|
|
public Integer getHeight() {
|
return height;
|
}
|
|
public void setHeight(Integer height) {
|
this.height = height;
|
}
|
|
public static class Builder {
|
|
private AdUtil.AD_TYPE adType;
|
private String pid;
|
private Integer width;
|
private Integer height;
|
|
public Builder setWidth(Integer width) {
|
this.width = width;
|
return this;
|
}
|
|
public Builder setHeight(Integer height) {
|
this.height = height;
|
return this;
|
}
|
|
public Builder setAdType(AdUtil.AD_TYPE adType) {
|
this.adType = adType;
|
return this;
|
}
|
|
public Builder setPid(String pid) {
|
this.pid = pid;
|
return this;
|
}
|
|
public ExpressAdInfo build() {
|
ExpressAdInfo adInfo = new ExpressAdInfo();
|
adInfo.setHeight(height);
|
adInfo.setWidth(width);
|
adInfo.setAdType(adType);
|
adInfo.setPid(pid);
|
return adInfo;
|
}
|
|
}
|
}
|
|
|
}
|