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();
|
|
}
|
|
|
}
|