package com.weikou.beibeivideo.ui.ad;
|
|
import android.content.Context;
|
import android.content.pm.ActivityInfo;
|
import android.os.Bundle;
|
|
import androidx.annotation.NonNull;
|
import androidx.annotation.Nullable;
|
import androidx.fragment.app.Fragment;
|
|
import android.os.Handler;
|
import android.os.Message;
|
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.CSJSplashAd;
|
import com.huawei.hms.ads.AdParam;
|
import com.huawei.hms.ads.AudioFocusType;
|
import com.huawei.hms.ads.splash.SplashView;
|
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.BeibeiConstant;
|
import com.weikou.beibeivideo.util.ad.AdUtil;
|
import com.weikou.beibeivideo.util.ad.HuaWeiAdConstant;
|
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 adPidInfo
|
* @param adPidInfoBy
|
* @param skip
|
* @param time
|
* @param first
|
* @param adLoadResultListener
|
*/
|
public static void loadAd(Context context, AdPidInfo adPidInfo, AdPidInfo adPidInfoBy, View skip, TextView time, boolean first, AdLoadResultListener adLoadResultListener) {
|
if (adPidInfo == null) {
|
if (adLoadResultListener != null)
|
adLoadResultListener.onNoAd();
|
return;
|
}
|
|
|
SplashAdUtil.loadAD(adPidInfo.getPid(), adPidInfo.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, adPidInfoBy, 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) {
|
CSJSplashAd ad = adLoadInfo.getCsjAd();
|
View view = ad.getSplashView();
|
fl_ad.removeAllViews();
|
//把SplashView 添加到ViewGroup中
|
fl_ad.addView(view);
|
|
ad.setSplashAdListener(new CSJSplashAd.SplashAdListener() {
|
@Override
|
public void onSplashAdShow(CSJSplashAd csjSplashAd) {
|
lastShowTime = System.currentTimeMillis();
|
}
|
|
@Override
|
public void onSplashAdClick(CSJSplashAd csjSplashAd) {
|
|
}
|
|
@Override
|
public void onSplashAdClose(CSJSplashAd csjSplashAd, int i) {
|
if (mAdShowListener != null)
|
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;
|
|
private Handler timeoutHandler ;
|
private static final int AD_TIMEOUT = 10000;
|
private static final int MSG_AD_TIMEOUT = 1001;
|
private void loadHWAd(SplashView splashView) {
|
AdParam adParam = new AdParam.Builder().build();
|
SplashView.SplashAdLoadListener splashAdLoadListener = new SplashView.SplashAdLoadListener() {
|
@Override
|
public void onAdLoaded() {
|
// 广告加载成功时调用
|
|
}
|
@Override
|
public void onAdFailedToLoad(int errorCode) {
|
// 广告加载失败时调用, 跳转至App主界面
|
if (mAdShowListener != null)
|
mAdShowListener.onClose();
|
}
|
@Override
|
public void onAdDismissed() {
|
// 广告展示完毕时调用, 跳转至App主界面
|
if (mAdShowListener != null)
|
mAdShowListener.onClose();
|
}
|
};
|
String slotId= HuaWeiAdConstant.HW_AD_SPLASH;
|
|
|
// 设置视频类开屏广告的音频焦点类型
|
splashView.setAudioFocusType(AudioFocusType.NOT_GAIN_AUDIO_FOCUS_WHEN_MUTE);
|
// 加载广告
|
splashView.load(slotId, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, adParam, splashAdLoadListener);
|
// 发送延时消息,保证广告显示超时后,APP首页可以正常显示
|
timeoutHandler.removeMessages(MSG_AD_TIMEOUT);
|
timeoutHandler.sendEmptyMessageDelayed(MSG_AD_TIMEOUT, AD_TIMEOUT);
|
}
|
|
|
private void init(){
|
timeoutHandler = new Handler(new Handler.Callback() {
|
@Override
|
public boolean handleMessage(@NonNull Message msg) {
|
if (getActivity()!=null&&getActivity().hasWindowFocus()) {
|
if (mAdShowListener != null)
|
mAdShowListener.onClose();
|
}
|
return 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);
|
init();
|
initView(root);
|
if (!showAd(Math.random() < 0.1f ? true : false)) {
|
Bundle arguments = getArguments();
|
AdPidInfo adPidInfo = null;
|
AdPidInfo adPidInfoBy = null;
|
if (arguments != null) {
|
adPidInfo = (AdPidInfo) arguments.getSerializable("pid1");
|
adPidInfoBy = (AdPidInfo) arguments.getSerializable("pid2");
|
}
|
if (adPidInfo.getAdType() == AdUtil.AD_TYPE.hw) {
|
// 华为广告单独处理
|
SplashView splashView=new SplashView(getContext());
|
FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
|
fl_ad.addView(splashView,params);
|
loadHWAd(splashView);
|
} else {
|
loadAd(getContext(), adPidInfo, adPidInfoBy, 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();
|
|
}
|
|
|
}
|