admin
2021-12-21 fd0097a37e24c771d5ad271135e48e8f52e325e0
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
package com.weikou.beibeivideo.ui.ad;
 
import android.content.Context;
import android.os.Bundle;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
 
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import com.bytedance.sdk.openadsdk.TTSplashAd;
import com.qq.e.ads.splash.SplashAD;
import com.weikou.beibeivideo.R;
import com.weikou.beibeivideo.entity.ad.AdPidInfo;
import com.weikou.beibeivideo.entity.ad.SplashAdLoadInfo;
import com.weikou.beibeivideo.util.ad.SplashAdUtil;
 
import java.util.Timer;
import java.util.TimerTask;
 
/**
 * 视频播放前贴广告
 */
public class SplashADFragment extends Fragment implements View.OnClickListener {
 
    private static final String TAG = "SplashADFragment";
    private LinearLayout ll_input;
    private TextView ad_close_time;
    private FrameLayout fl_ad;
 
    private static SplashAdLoadInfo adLoadInfo;
    public static long lastShowTime = 0L;
 
    private AdShowListener mAdShowListener;
 
    public void setAdShowListener(AdShowListener mAdShowListener) {
        this.mAdShowListener = mAdShowListener;
    }
 
    public SplashADFragment() {
 
    }
 
    public static SplashADFragment newInstance(AdPidInfo pid1, AdPidInfo pid2) {
        SplashADFragment fragment = new SplashADFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("pid1", pid1);
        bundle.putSerializable("pid2", pid2);
        fragment.setArguments(bundle);
        return fragment;
    }
 
 
    /**
     * 加载开屏广告
     *
     * @param context
     * @param adPidInfo1
     * @param adPidInfo2
     * @param skip
     * @param time
     * @param first
     * @param adLoadResultListener
     */
    public static void loadAd(Context context, AdPidInfo adPidInfo1, AdPidInfo adPidInfo2, View skip, TextView time, boolean first, AdLoadResultListener adLoadResultListener) {
        if (adPidInfo1 == null) {
            if (adLoadResultListener != null)
                adLoadResultListener.onNoAd();
            return;
        }
 
        SplashAdUtil.loadAD(adPidInfo1.getPid(), adPidInfo1.getAdType(), context, skip, time, new SplashAdUtil.SplashAdListener() {
            @Override
            public void onAdLoad(SplashAdLoadInfo adLoadInfo) {
                SplashADFragment.adLoadInfo = adLoadInfo;
                if (adLoadResultListener != null) {
                    adLoadResultListener.onAdLoad(adLoadInfo);
                }
            }
 
            @Override
            public void close() {
                if (adLoadResultListener != null)
                    adLoadResultListener.onClose();
            }
 
            @Override
            public void noAd() {//初次加载未加载出广告
 
                if (first) {
                    loadAd(context, adPidInfo2, null, skip, time, false, adLoadResultListener);
                } else {
                    if (adLoadResultListener != null)
                        adLoadResultListener.onNoAd();
                }
            }
 
            @Override
            public void onAdClick() {
                if (adLoadResultListener != null)
                    adLoadResultListener.onAdClick();
            }
        });
    }
 
    private Timer timer;
    private int leftTime = 5;
 
    public boolean showAd(boolean vclick) {
        if (isAdLoaded()) {
            //穿山甲广告
            if (adLoadInfo.getCsjAd() != null) {
                TTSplashAd ad = adLoadInfo.getCsjAd();
                View view = ad.getSplashView();
                fl_ad.removeAllViews();
                //把SplashView 添加到ViewGroup中
                fl_ad.addView(view);
 
                ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                    @Override
                    public void onAdClicked(View view, int type) {
 
                    }
 
                    @Override
                    public void onAdShow(View view, int type) {
                        lastShowTime = System.currentTimeMillis();
                    }
 
                    @Override
                    public void onAdSkip() {
                        if (mAdShowListener != null)
                            mAdShowListener.onClose();
                    }
 
                    @Override
                    public void onAdTimeOver() {
                        if (mAdShowListener != null)
                            mAdShowListener.onClose();
                    }
                });
                //设置SplashView的交互监听器
//                ad.setNotAllowSdkCountdown();
                //自定义跳过按钮
//                if (timer != null)
//                    timer.cancel();
//                timer = new Timer();
//                ll_input.setVisibility(View.VISIBLE);
//                timer.schedule(new TimerTask() {
//                    @Override
//                    public void run() {
//                        leftTime--;
//                        if (leftTime < 0) {
//                            timer.cancel();
//                            if (ad_close_time != null)
//                                ad_close_time.post(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        if (ad_close_time != null)
//                                            ad_close_time.setVisibility(View.GONE);
//                                        if (vclick) {
//                                            if (ll_input != null)
//                                                ll_input.setClickable(true);
//                                        } else {
//                                            if (mAdShowListener != null)
//                                                mAdShowListener.onClose();
//                                        }
//
//                                    }
//                                });
//
//
//                            return;
//                        } else {
//                            //倒计时
//                            if (ad_close_time != null)
//                                ad_close_time.post(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        if (ad_close_time != null)
//                                            ad_close_time.setText(leftTime + "");
//                                    }
//                                });
//                            if (vclick) {
//                                ll_input.setClickable(false);
//                            }
//                        }
//                    }
//                }, 1000, 1000);
//
//
//                ll_input.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        mAdShowListener.onClose();
//                    }
//                });
 
                adLoadInfo = null;
            } else if (adLoadInfo.getGdtSplashAd() != null) {
                SplashAD splashAD = adLoadInfo.getGdtSplashAd();
                splashAD.showAd(fl_ad);
                lastShowTime = System.currentTimeMillis();
                adLoadInfo = null;
            }
 
            return true;
        }
 
        return false;
    }
 
    /**
     * 广告是否加载
     *
     * @return
     */
    public static boolean isAdLoaded() {
        if (adLoadInfo != null && adLoadInfo.getExpireTime() > System.currentTimeMillis()) {
            return true;
        }
        return false;
    }
 
    private void initView(View root) {
        ll_input = root.findViewById(R.id.ll_input);
        ad_close_time = root.findViewById(R.id.ad_close_time);
        fl_ad = root.findViewById(R.id.fl_ad);
 
        ll_input.setVisibility(View.GONE);
        ad_close_time.setVisibility(View.GONE);
    }
 
 
    boolean adClick = false;
 
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = LayoutInflater.from(getContext()).inflate(R.layout.fragment_splash_ad, null);
        initView(root);
        if (!showAd(Math.random() < 0.1f ? true : false)) {
            Bundle arguments = getArguments();
            AdPidInfo adPidInfo1 = null;
            AdPidInfo adPidInfo2 = null;
            if (arguments != null) {
                adPidInfo1 = (AdPidInfo) arguments.getSerializable("pid1");
                adPidInfo2 = (AdPidInfo) arguments.getSerializable("pid2");
            }
 
            loadAd(getContext(), adPidInfo1, adPidInfo2, null, null, true, new SplashADFragment.AdLoadResultListener() {
 
                @Override
                public void onAdLoad(SplashAdLoadInfo adLoadInfo) {
                    showAd(false);
                }
 
                @Override
                public void onNoAd() {
                    if (mAdShowListener != null)
                        mAdShowListener.onClose();
                }
 
                @Override
                public void onClose() {
                    //广告关闭点击之后先不忙调用关闭方法
                    if (mAdShowListener != null && !adClick) {
                        mAdShowListener.onClose();
                        Log.i(TAG, "广告关闭");
                    }
                }
 
                @Override
                public void onAdClick() {
                    adClick = true;
                    Log.i(TAG, "广告点击");
                }
            });
        }
        return root;
    }
 
 
    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
        if (adClick) {
            //广告关闭点击之后,回来时再跳转
            adClick = false;
            if (mAdShowListener != null)
                mAdShowListener.onClose();
        }
    }
 
    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");
    }
 
    @Override
    public void onClick(View v) {
 
        switch (v.getId()) {
            case R.id.tv_skip:
 
 
                break;
        }
 
    }
 
    public interface AdLoadResultListener {
 
        public void onAdLoad(SplashAdLoadInfo adLoadInfo);
 
        public void onAdClick();
 
        public void onNoAd();
 
        public void onClose();
 
    }
 
    public interface AdShowListener {
 
 
        public void onClose();
 
    }
 
 
}