admin
2021-03-31 5d475ef2f856a1415c5b2de255a1c1088d6a2ec2
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
package com.weikou.beibeivideo.ui.ad;
 
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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.AdPositionEnum;
import com.weikou.beibeivideo.entity.ad.SplashAdLoadInfo;
import com.weikou.beibeivideo.ui.SplashActivity;
import com.weikou.beibeivideo.util.BeibeiConstant;
import com.weikou.beibeivideo.util.ad.AdUtil;
import com.weikou.beibeivideo.util.ad.CSJConstant;
import com.weikou.beibeivideo.util.ad.GDTConstant;
import com.weikou.beibeivideo.util.ad.SplashAdUtil;
 
import java.util.Timer;
import java.util.TimerTask;
 
/**
 * 视频播放前贴广告
 */
public class SplashADFragment extends Fragment implements View.OnClickListener {
 
    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;
    }
 
    private 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();
                }
            }
        });
    }
 
    private Timer timer;
    private int leftTime = 5;
 
    public boolean showAd() {
        if (isAdLoaded()) {
            //穿山甲广告
            if (adLoadInfo.getCsjAd() != null) {
                TTSplashAd ad = adLoadInfo.getCsjAd();
                View view = ad.getSplashView();
                fl_ad.removeAllViews();
                //把SplashView 添加到ViewGroup中
                fl_ad.addView(view);
                //设置SplashView的交互监听器
                ad.setNotAllowSdkCountdown();
                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();
                    }
                });
                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 (ll_input != null)
                                            ll_input.setClickable(true);
                                    }
                                });
 
 
                            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 + "");
                                    }
                                });
                            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);
    }
 
    @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()) {
            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, ll_input, ad_close_time, true, new SplashADFragment.AdLoadResultListener() {
 
                @Override
                public void onAdLoad(SplashAdLoadInfo adLoadInfo) {
                    showAd();
                }
 
                @Override
                public void onNoAd() {
                    if (mAdShowListener != null)
                        mAdShowListener.onClose();
                }
 
                @Override
                public void onClose() {
                    if (mAdShowListener != null)
                        mAdShowListener.onClose();
                }
            });
        }
        return root;
    }
 
 
    @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 onNoAd();
 
        public void onClose();
 
    }
 
    public interface AdShowListener {
 
 
        public void onClose();
 
    }
 
 
}