admin
2021-04-07 cbb88109494ffc7916f6639c20ce05c0cec941a9
BuWanVideo/src/com/weikou/beibeivideo/util/ad/ExpressAdManager.java
@@ -12,6 +12,7 @@
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.google.gson.Gson;
import com.qq.e.ads.cfg.VideoOption;
import com.qq.e.ads.nativ.ADSize;
import com.qq.e.ads.nativ.NativeExpressAD;
@@ -22,6 +23,7 @@
import com.qq.e.ads.nativ.express2.VideoOption2;
import com.qq.e.comm.util.AdError;
import com.weikou.beibeivideo.entity.ad.ExpressAdContainer;
import com.weikou.beibeivideo.ui.recommend.SearchActivity;
import com.weikou.beibeivideo.util.BeibeiConstant;
import com.weikou.beibeivideo.util.DimenUtils;
@@ -32,22 +34,20 @@
 * 信息流广告管理
 */
public class ExpressAdManager {
    public final static int SOURCE_GDT = 1;//广点通
    public final static int SOURCE_GDT2 = 11;//广点通2.0
    public final static int SOURCE_CSJ = 2;//穿山甲
    final static String TAG = "ExpressAdManager";
    int sourceType = SOURCE_GDT;
    AdUtil.AD_TYPE sourceType = AdUtil.AD_TYPE.gdt;
    private TTAdNative mTTAdNative;
    private Context mContext;
    public ExpressAdManager(int adSource, Context context) {
    public ExpressAdManager(AdUtil.AD_TYPE adSource, Context context) {
        sourceType = adSource;
        this.mContext = context;
        if (sourceType == SOURCE_CSJ) {
        if (sourceType == AdUtil.AD_TYPE.csj) {
            TTAdManager ttAdManager = TTAdManagerHolder.get();
            mTTAdNative = ttAdManager.createAdNative(context);
//            TTAdManagerHolder.get().requestPermissionIfNecessary(context);
@@ -66,7 +66,7 @@
     * @param adLoadListener
     */
    public void loadAd(int widthDP, int heightDP, String pid, int count, final IAdLoadListener adLoadListener) {
        if (sourceType == SOURCE_GDT) {
        if (sourceType == AdUtil.AD_TYPE.gdt) {
            loadGDT(pid, widthDP, heightDP, count, new IGDTAdLoadListener() {
                @Override
                public void onSuccess(List<NativeExpressADView> adList) {
@@ -83,7 +83,7 @@
                }
            }, mContext);
        } else if (sourceType == SOURCE_GDT2) {
        } else if (sourceType == AdUtil.AD_TYPE.gdt2) {
            loadGDT2(pid, widthDP, heightDP, count, new IGDTAdLoadListener() {
                @Override
                public void onSuccess(List<NativeExpressADView> adList) {
@@ -101,7 +101,7 @@
                }
            }, mContext);
        } else if (sourceType == SOURCE_CSJ) {
        } else if (sourceType == AdUtil.AD_TYPE.csj) {
            loadCSJ(pid, widthDP, heightDP, count, new ICSJAdLoadListener() {
                @Override
                public void onSuccess(List<TTNativeExpressAd> adList) {
@@ -127,15 +127,15 @@
        //加载大图
        int deviceWidth = DimenUtils.getScreenWidth(mContext);
        deviceWidth = DimenUtils.px2dip(mContext, deviceWidth);
        int width = deviceWidth - 6;
        int width = deviceWidth -9 * 2;
        int height = (int) (width * 0.8);
        String pid = null;
        if (sourceType == SOURCE_CSJ) {
        if (sourceType == AdUtil.AD_TYPE.csj) {
            pid = CSJConstant.RECOMMEND_BIG_IMG_AD;
            height = (int) (width * 0.56);
        } else if (sourceType == SOURCE_GDT) {
            height = (int) (width * 0.78);
        } else if (sourceType == AdUtil.AD_TYPE.gdt) {
            pid = BeibeiConstant.GDT_RECOMMAND_NATIVE;
        } else if (sourceType == SOURCE_GDT2) {
        } else if (sourceType == AdUtil.AD_TYPE.gdt2) {
            pid = GDTConstant.PID_HOME_RECOMMEND_BIG_IMG;
        }
        //获取屏幕的宽
@@ -152,9 +152,9 @@
    public void loadRecommendSmallAd(final IAdLoadListener adLoadListener) {
        //加载大图
        String pid = null;
        if (sourceType == SOURCE_CSJ) {
        if (sourceType == AdUtil.AD_TYPE.csj) {
            pid = "945375047";
        } else if (sourceType == SOURCE_GDT) {
        } else if (sourceType == AdUtil.AD_TYPE.gdt) {
            pid = BeibeiConstant.GDT_RECOMMAND_MIN_NATIVE;
        }
        //获取屏幕的宽
@@ -172,18 +172,21 @@
     * @param adLoadListener
     */
    public void loadVideoDetailPlayerBottomAd(final IAdLoadListener adLoadListener) {
        Log.i(TAG, "loadVideoDetailPlayerBottomAd");
        //加载大图
        String pid = null;
        if (sourceType == SOURCE_CSJ) {
            pid = "945375047";
        } else if (sourceType == SOURCE_GDT2) {
        if (sourceType == AdUtil.AD_TYPE.csj) {
            pid = CSJConstant.VIDEO_DETAIL_PLAYER_BOTTOM;
        } else if (sourceType == AdUtil.AD_TYPE.gdt2) {
            pid = GDTConstant.PID_2_VIDEO_DETAIL_PLAY_EXPRESS1;
        }
        if (mContext == null)
            return;
        //获取屏幕的宽
        int deviceWidth = DimenUtils.getScreenWidth(mContext);
        deviceWidth = DimenUtils.px2dip(mContext, deviceWidth);
        int width = (deviceWidth - 20);
        int height = 80;
        int height = 100;
        loadAd(width, height, pid, 1, adLoadListener);
    }
@@ -196,9 +199,9 @@
    public void loadVideoDetailRelativeVideoBottomAd(final IAdLoadListener adLoadListener) {
        //加载大图
        String pid = null;
        if (sourceType == SOURCE_CSJ) {
            pid = "945375047";
        } else if (sourceType == SOURCE_GDT2) {
        if (sourceType == AdUtil.AD_TYPE.csj) {
            pid = CSJConstant.VIDEO_DETAIL_GUESSLIKE_TOP;
        } else if (sourceType == AdUtil.AD_TYPE.gdt2) {
            pid = GDTConstant.PID_2_VIDEO_DETAIL_PLAY_EXPRESS2;
        }
        if (mContext == null)
@@ -220,9 +223,9 @@
    public void loadRecommendCategorySmallAd(final IAdLoadListener adLoadListener) {
        //加载大图
        String pid = null;
        if (sourceType == SOURCE_CSJ) {
        if (sourceType == AdUtil.AD_TYPE.csj) {
            pid = "945375047";
        } else if (sourceType == SOURCE_GDT) {
        } else if (sourceType == AdUtil.AD_TYPE.gdt) {
            pid = BeibeiConstant.GDT_RECOMMEND_CATEGORY_MIN_NATIVE;
        }
        //获取屏幕的宽
@@ -242,9 +245,9 @@
    public void loadSearchResultSmallAd(int count, final IAdLoadListener adLoadListener) {
        //加载大图
        String pid = null;
        if (sourceType == SOURCE_CSJ) {
        if (sourceType == AdUtil.AD_TYPE.csj) {
            pid = "945379581";
        } else if (sourceType == SOURCE_GDT) {
        } else if (sourceType == AdUtil.AD_TYPE.gdt2) {
            pid = BeibeiConstant.GDT_SEARCH_RESULT_MIN_NATIVE;
        }
        //获取屏幕的宽
@@ -267,9 +270,9 @@
        //加载大图
        String pid = null;
        if (sourceType == SOURCE_CSJ) {
        if (sourceType == AdUtil.AD_TYPE.csj) {
            pid = "945406595";
        } else if (sourceType == SOURCE_GDT2) {
        } else if (sourceType == AdUtil.AD_TYPE.gdt2) {
            if (columns == 3)
                pid = GDTConstant.PID_2_VIDEO_LIST_SMALL_AD_COLUMN3;
            else if (columns == 2)
@@ -294,6 +297,48 @@
    /**
     * 加载APP退出广告
     *
     * @param adLoadListener
     */
    public void loadAppExitAd(final IAdLoadListener adLoadListener) {
        //加载大图
        String pid = null;
        if (sourceType == AdUtil.AD_TYPE.csj) {
            pid = CSJConstant.APP_EXIT;
        } else if (sourceType == AdUtil.AD_TYPE.gdt) {
            pid = BeibeiConstant.GDT_EXIT_DIALOG;
        }
        //获取屏幕的宽
        int deviceWidth = DimenUtils.getScreenWidth(mContext);
        deviceWidth = DimenUtils.px2dip(mContext, deviceWidth);
        int width = deviceWidth * 3 / 4;
        int height = (int) (width / 1.78f);
        loadAd(width, height, pid, 1, adLoadListener);
    }
    /**
     * 加载视频搜索广告
     *
     * @param adLoadListener
     */
    public void loadSearchAd(int widthDP, final IAdLoadListener adLoadListener) {
        //加载大图
        String pid = null;
        if (sourceType == AdUtil.AD_TYPE.csj) {
            pid = CSJConstant.VIDEO_SEARCH;
        } else if (sourceType == AdUtil.AD_TYPE.gdt) {
            pid = BeibeiConstant.GDT_SEARCH_NATIVE;
        }
        int height = (int) (widthDP / 1.78f);
        loadAd(widthDP, height, pid, 1, adLoadListener);
    }
    /**
     * 加载广点通广告
     *
     * @param positionId
@@ -302,6 +347,8 @@
     * @param context
     */
    private void loadGDT(String positionId, int widthDP, int heightDP, int count, final IGDTAdLoadListener adLoadListener, Context context) {
        NativeExpressAD nativeExpressAD = new NativeExpressAD(context, new ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT),
                positionId, new NativeExpressAD.NativeExpressADListener() {
@@ -376,6 +423,9 @@
     * @param context
     */
    private void loadGDT2(String positionId, int widthDP, int heightDP, int count, final IGDTAdLoadListener adLoadListener, Context context) {
        Log.i(TAG, String.format("loadGDT2:positionId-%s  widthDP-%s heightDP-%s", positionId, widthDP, heightDP));
        NativeExpressAD2 nativeExpressAD2 = new NativeExpressAD2(context, positionId, new NativeExpressAD2.AdLoadListener() {
            @Override
            public void onLoadSuccess(List<NativeExpressADData2> list) {