admin
2021-09-30 2f1ab8af88cae4e723126ea5cf2f7d42dff7dbdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package com.weikou.beibeivideo.util.ad;
 
import android.app.Activity;
import android.content.Context;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;
 
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
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.bytedance.sdk.openadsdk.TTRewardVideoAd;
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.ads.nativ.express2.AdEventListener;
import com.qq.e.ads.nativ.express2.NativeExpressAD2;
import com.qq.e.ads.nativ.express2.NativeExpressADData2;
import com.qq.e.ads.nativ.express2.VideoOption2;
import com.qq.e.ads.rewardvideo.RewardVideoAD;
import com.qq.e.ads.rewardvideo.RewardVideoADListener;
import com.qq.e.ads.rewardvideo2.ExpressRewardVideoAD;
import com.qq.e.ads.rewardvideo2.ExpressRewardVideoAdListener;
import com.qq.e.comm.util.AdError;
import com.qq.e.comm.util.VideoAdValidity;
import com.ut.device.UTDevice;
import com.weikou.beibeivideo.entity.ad.ExpressAdContainer;
import com.weikou.beibeivideo.entity.ad.RewardVideoAdContainer;
import com.weikou.beibeivideo.util.BeibeiConstant;
import com.weikou.beibeivideo.util.DimenUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * 激励视频广告管理
 */
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);
                }
            });
        } else if (sourceType == AdUtil.AD_TYPE.gdt2) {
            loadGdt2(pid, new IGDT2AdLoadListener() {
 
                @Override
                public void onSuccess(ExpressRewardVideoAD rewardVideoAD) {
                    adLoadListener.onSuccess(new RewardVideoAdContainer(rewardVideoAD));
                }
 
                @Override
                public void onError(String msg) {
                    adLoadListener.onError(msg);
                }
            }, showListener);
        }
    }
 
 
    /**
     * 穿山甲广告
     *
     * @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;
            }
        });
    }
 
    private ExpressRewardVideoAD rewardVideoAD = null;
 
    private void loadGdt2(String pid, IGDT2AdLoadListener adLoadListener, IAdShowListener adShowListener) {
        //广点通
        rewardVideoAD = new ExpressRewardVideoAD(mContext, pid, new ExpressRewardVideoAdListener() {
 
            @Override
            public void onAdLoaded() {
                Log.i(TAG, "onAdLoaded");
            }
 
            @Override
            public void onVideoCached() {
                Log.i(TAG, "onVideoCached");
                adLoadListener.onSuccess(rewardVideoAD);
            }
 
            @Override
            public void onShow() {
                Log.i(TAG, "onShow");
                adShowListener.onShow();
            }
 
            @Override
            public void onExpose() {
                Log.i(TAG, "onExpose");
            }
 
            @Override
            public void onReward(Map<String, Object> map) {
                Log.i(TAG, "onReward");
                adShowListener.onReward();
            }
 
            @Override
            public void onClick() {
                Log.i(TAG, "onClick");
            }
 
            @Override
            public void onVideoComplete() {
                Log.i(TAG, "onVideoComplete");
            }
 
            @Override
            public void onClose() {
                Log.i(TAG, "onClose");
                adShowListener.onClose();
            }
 
            @Override
            public void onError(AdError adError) {
                Log.i(TAG, "onError:" + adError.getErrorMsg());
                adLoadListener.onError(adError.getErrorMsg());
            }
        });
        rewardVideoAD.loadAD();
    }
 
 
    /**
     * 渲染模板和填充广告
     *
     * @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 onSkippedVideo() {
                    Log.i(TAG, "onSkippedVideo");
                }
            });
            ad.getCsj().showRewardVideoAd(activity, TTAdConstant.RitScenes.CUSTOMIZE_SCENES, "scenes_test");
            ad.setCsj(null);
        } else if (ad.getGdt2() != null) {
            // 展示广告
            VideoAdValidity validity = ad.getGdt2().checkValidity();
            switch (validity) {
                case SHOWED:
                    if (showListener != null)
                        showListener.onError("广告已经展示过!");
                    return;
                // 在视频缓存成功后展示,以省去用户的等待时间,提升用户体验
                case NONE_CACHE:
                    if (showListener != null)
                        showListener.onError("广告素材未缓存成功!");
                    return;
                case OVERDUE:
                    if (showListener != null)
                        showListener.onError("广告已经过期!");
                    return;
                case VALID:
                    // 有效广告
                    break;
            }
            // 在视频缓存成功后展示,以省去用户的等待时间,提升用户体验
            ad.getGdt2().showAD(null);
        }
 
    }
 
 
    interface IGDTAdLoadListener {
        public void onSuccess(List<NativeExpressADView> adList);
 
        public void onSuccess2(List<NativeExpressADData2> adList);
    }
 
    interface ICSJAdLoadListener {
        public void onSuccess(TTRewardVideoAd ad);
 
        public void onError(String msg);
    }
 
    interface IGDT2AdLoadListener {
        public void onSuccess(ExpressRewardVideoAD rewardVideoAD);
 
        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);
    }
 
 
}