package com.weikou.beibeivideo.ui.ad;
|
|
import android.content.Context;
|
import android.graphics.Bitmap;
|
import android.net.ConnectivityManager;
|
import android.net.NetworkInfo;
|
import android.os.Bundle;
|
import android.support.annotation.NonNull;
|
import android.support.annotation.Nullable;
|
import android.support.v4.app.Fragment;
|
import android.text.TextUtils;
|
import android.util.Log;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.widget.Button;
|
import android.widget.ImageView;
|
import android.widget.ProgressBar;
|
import android.widget.TextView;
|
|
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.request.animation.GlideAnimation;
|
import com.bumptech.glide.request.target.SimpleTarget;
|
import com.qq.e.ads.cfg.VideoOption;
|
import com.qq.e.ads.nativ.MediaView;
|
import com.qq.e.ads.nativ.NativeADEventListener;
|
import com.qq.e.ads.nativ.NativeADMediaListener;
|
import com.qq.e.ads.nativ.NativeADUnifiedListener;
|
import com.qq.e.ads.nativ.NativeUnifiedAD;
|
import com.qq.e.ads.nativ.NativeUnifiedADData;
|
import com.qq.e.ads.nativ.VideoPreloadListener;
|
import com.qq.e.ads.nativ.widget.NativeAdContainer;
|
import com.qq.e.comm.constants.AdPatternType;
|
import com.qq.e.comm.util.AdError;
|
import com.yeshi.buwanshequ.R;
|
|
import java.util.ArrayList;
|
import java.util.Calendar;
|
import java.util.List;
|
|
public class VideoDetailVideoAdFragment extends Fragment implements View.OnClickListener {
|
|
private MediaView mediaView;
|
|
private NativeAdContainer mContainer;
|
|
private NativeUnifiedADData mAdData;
|
|
private TextView tv_skip;
|
|
private ProgressBar pb_progress;
|
|
@Override
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
}
|
|
private ImageView iv_logo, img_poster;
|
private TextView text_title;
|
private TextView text_desc;
|
private Button mCTAButton, mDownloadButton;
|
|
private void initView(View root) {
|
mediaView = root.findViewById(R.id.gdt_media_view);
|
mContainer = root.findViewById(R.id.native_ad_container);
|
iv_logo = root.findViewById(R.id.img_logo);
|
img_poster = root.findViewById(R.id.img_poster);
|
text_title = root.findViewById(R.id.text_title);
|
text_desc = root.findViewById(R.id.text_desc);
|
mCTAButton = root.findViewById(R.id.btn_cta);
|
mDownloadButton = root.findViewById(R.id.btn_download);
|
tv_skip = root.findViewById(R.id.tv_skip);
|
pb_progress = root.findViewById(R.id.pb_progress);
|
}
|
|
@Nullable
|
@Override
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
View root = LayoutInflater.from(getContext()).inflate(R.layout.item_video_detail_ad, null);
|
initView(root);
|
// loadVideoAD();
|
tv_skip.setOnClickListener(this);
|
return root;
|
}
|
|
|
private Runnable timer = null;
|
|
private synchronized void autoTimer(final View view) {
|
|
if (timer == null) {
|
timer = new Runnable() {
|
@Override
|
public void run() {
|
if (remainTime - (mAdData.getVideoCurrentPosition() / 1000) > 0) {
|
tv_skip.setText((remainTime - (mAdData.getVideoCurrentPosition() / 1000)) + "s后可跳过");
|
tv_skip.setClickable(false);
|
} else {
|
tv_skip.setText("跳过");
|
tv_skip.setClickable(true);
|
}
|
autoTimer(view);
|
}
|
};
|
view.postDelayed(timer, 1000);
|
} else {
|
view.removeCallbacks(timer);
|
view.postDelayed(timer, 1000);
|
}
|
}
|
|
|
int remainTime = 0;
|
|
public void loadVideoAD(final IVideoAdListener videoAdListener) {
|
tv_skip.setVisibility(View.GONE);
|
if (mAdData != null)
|
mAdData.destroy();
|
NativeUnifiedAD mAdManager = new NativeUnifiedAD(getContext(), "5061928359494942", new NativeADUnifiedListener() {
|
|
@Override
|
public void onNoAD(AdError adError) {
|
System.out.println(adError);
|
}
|
|
@Override
|
public void onADLoaded(List<NativeUnifiedADData> list) {
|
System.out.println(list);
|
final NativeUnifiedADData ad = list.get(0);
|
//填充内容
|
mAdData = ad;
|
renderAdUi(ad);
|
List<View> clickableViews = new ArrayList<>();
|
clickableViews.add(mDownloadButton);
|
|
ad.bindAdToView(getContext(), mContainer, null, clickableViews);
|
if (ad.getAdPatternType() == AdPatternType.NATIVE_VIDEO) {
|
final String TAG = "NativeUnifiedADData";
|
// 如果是视频广告,可以调用preloadVideo预加载视频素材
|
ad.preloadVideo(new VideoPreloadListener() {
|
@Override
|
public void onVideoCached() {
|
Log.d(TAG, "onVideoCached");
|
//展示广告
|
VideoOption videoOption = getVideoOption();
|
ad.bindMediaView(mediaView, videoOption, new NativeADMediaListener() {
|
@Override
|
public void onVideoInit() {
|
Log.d(TAG, "onVideoInit: ");
|
pb_progress.setVisibility(View.VISIBLE);
|
}
|
|
@Override
|
public void onVideoLoading() {
|
Log.d(TAG, "onVideoLoading: ");
|
}
|
|
@Override
|
public void onVideoReady() {
|
Log.d(TAG, "onVideoReady");
|
img_poster.setVisibility(View.GONE);
|
pb_progress.setVisibility(View.GONE);
|
}
|
|
@Override
|
public void onVideoLoaded(int videoDuration) {
|
Log.d(TAG, "onVideoLoaded: ");
|
|
}
|
|
@Override
|
public void onVideoStart() {
|
Log.d(TAG, "onVideoStart");
|
if (ad.getVideoDuration() / 1000 > 10)
|
remainTime = 10;
|
else
|
remainTime = ad.getVideoDuration() / 1000;
|
|
videoAdListener.startPlay();
|
autoTimer(img_poster);
|
img_poster.post(new Runnable() {
|
@Override
|
public void run() {
|
tv_skip.setVisibility(View.VISIBLE);
|
}
|
});
|
|
}
|
|
@Override
|
public void onVideoPause() {
|
Log.d(TAG, "onVideoPause: ");
|
}
|
|
@Override
|
public void onVideoResume() {
|
Log.d(TAG, "onVideoResume: ");
|
}
|
|
@Override
|
public void onVideoCompleted() {
|
Log.d(TAG, "onVideoCompleted: ");
|
tv_skip.setVisibility(View.GONE);
|
if (videoAdListener != null) {
|
videoAdListener.finishPlay();
|
}
|
}
|
|
@Override
|
public void onVideoError(AdError error) {
|
Log.d(TAG, "onVideoError: ");
|
if (videoAdListener != null) {
|
videoAdListener.finishPlay();
|
}
|
}
|
|
@Override
|
public void onVideoStop() {
|
Log.d(TAG, "onVideoStop");
|
}
|
|
@Override
|
public void onVideoClicked() {
|
Log.d(TAG, "onVideoClicked");
|
}
|
});
|
ad.setNativeAdEventListener(new NativeADEventListener() {
|
@Override
|
public void onADExposed() {
|
Log.d(TAG, "onADExposed");
|
}
|
|
@Override
|
public void onADClicked() {
|
Log.d(TAG, "onADClicked");
|
}
|
|
@Override
|
public void onADError(AdError adError) {
|
Log.d(TAG, "onADError");
|
}
|
|
@Override
|
public void onADStatusChanged() {
|
Log.d(TAG, "onADStatusChanged");
|
updateAdAction(mDownloadButton, ad);
|
}
|
});
|
|
updateAdAction(mDownloadButton, ad);
|
|
List<View> CTAViews = new ArrayList<>();
|
CTAViews.add(mCTAButton);
|
ad.bindCTAViews(CTAViews);
|
String ctaText = ad.getCTAText(); //获取组件文案
|
if (!TextUtils.isEmpty(ctaText)) {
|
//如果拉取广告包含CTA组件,则渲染该组件
|
//当广告中有营销组件时,隐藏下载按钮,仅为demo示例所用,开发者可自行决定mDownloadButton按钮是否显示
|
mCTAButton.setText(ctaText);
|
mCTAButton.setVisibility(View.VISIBLE);
|
mDownloadButton.setVisibility(View.INVISIBLE);
|
}
|
ad.startVideo();
|
}
|
|
@Override
|
public void onVideoCacheFailed(int errorNo, String msg) {
|
Log.d(TAG, "onVideoCacheFailed:" + msg);
|
}
|
});
|
}
|
}
|
});
|
mAdManager.setVastClassName("com.qq.e.union.demo.adapter.vast.unified.ImaNativeDataAdapter");
|
mAdManager.setVideoPlayPolicy(getVideoPlayPolicy(getContext()));
|
mAdManager.setVideoADContainerRender(VideoOption.VideoADContainerRender.SDK);
|
mAdManager.loadData(1);
|
|
tv_skip.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (mAdData != null)
|
mAdData.stopVideo();
|
if (videoAdListener != null)
|
videoAdListener.finishPlay();
|
tv_skip.setVisibility(View.GONE);
|
}
|
});
|
|
}
|
|
public static void updateAdAction(Button button, NativeUnifiedADData ad) {
|
if (!ad.isAppAd()) {
|
button.setText("浏览");
|
return;
|
}
|
switch (ad.getAppStatus()) {
|
case 0:
|
button.setText("下载");
|
break;
|
case 1:
|
button.setText("启动");
|
break;
|
case 2:
|
button.setText("更新");
|
break;
|
case 4:
|
button.setText(ad.getProgress() + "%");
|
break;
|
case 8:
|
button.setText("安装");
|
break;
|
case 16:
|
button.setText("下载失败,重新下载");
|
break;
|
default:
|
button.setText("浏览");
|
break;
|
}
|
}
|
|
|
private void renderAdUi(final NativeUnifiedADData ad) {
|
int patternType = ad.getAdPatternType();
|
if (patternType == AdPatternType.NATIVE_VIDEO) {
|
Glide.with(getContext()).load(ad.getIconUrl()).into(iv_logo);
|
Glide.with(getContext()).load(ad.getImgUrl())
|
.asBitmap().into(new SimpleTarget<Bitmap>() {
|
@Override
|
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
|
if (img_poster.getVisibility() == View.VISIBLE) {
|
img_poster.setImageBitmap(resource);
|
}
|
}
|
});
|
text_title.setText(ad.getTitle());
|
text_desc.setText(ad.getDesc());
|
}
|
}
|
|
@Nullable
|
public static VideoOption getVideoOption() {
|
VideoOption.Builder builder = new VideoOption.Builder();
|
builder.setAutoPlayPolicy(VideoOption.AutoPlayPolicy.NEVER);
|
builder.setAutoPlayMuted(true);
|
builder.setDetailPageMuted(false);
|
builder.setNeedCoverImage(true);
|
builder.setNeedProgressBar(false);
|
builder.setEnableDetailPage(true);
|
builder.setEnableUserControl(false);
|
return builder.build();
|
}
|
|
|
public static int getVideoPlayPolicy(Context context) {
|
|
if (1 > 0)
|
return VideoOption.VideoPlayPolicy.MANUAL;
|
|
VideoOption option = getVideoOption();
|
if (option == null) {
|
return VideoOption.VideoPlayPolicy.AUTO;
|
}
|
|
int autoPlayPolicy = option.getAutoPlayPolicy();
|
|
if (autoPlayPolicy == VideoOption.AutoPlayPolicy.ALWAYS) {
|
return VideoOption.VideoPlayPolicy.AUTO;
|
} else if (autoPlayPolicy == VideoOption.AutoPlayPolicy.WIFI) {
|
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
NetworkInfo wifiNetworkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
return wifiNetworkInfo != null && wifiNetworkInfo.isConnected() ? VideoOption.VideoPlayPolicy.AUTO
|
: VideoOption.VideoPlayPolicy.MANUAL;
|
} else if (autoPlayPolicy == VideoOption.AutoPlayPolicy.NEVER) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTimeInMillis(System.currentTimeMillis());
|
int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
|
// 简单的播放策略示例,虽然VideoOption设置了从不自动播放,SDK不会主动触发自动播放
|
// 但晚上10点以后,开发者自行调用了startVideoPlay进行了视频播放
|
// 这在用户看来仍然是"自动播放"的,因为用户没有进行任何操作,视频就开始播放了
|
return hour >= 22 ? VideoOption.VideoPlayPolicy.AUTO : VideoOption.VideoPlayPolicy.MANUAL;
|
}
|
|
return VideoOption.VideoPlayPolicy.UNKNOWN;
|
}
|
|
|
@Override
|
public void onResume() {
|
super.onResume();
|
if (mAdData != null) {
|
// 必须要在Activity.onResume()时通知到广告数据,以便重置广告恢复状态
|
mAdData.resume();
|
}
|
}
|
|
@Override
|
public void onDestroy() {
|
super.onDestroy();
|
if (mAdData != null) {
|
// 必须要在Actiivty.destroy()时通知到广告数据,以便释放内存
|
mAdData.destroy();
|
}
|
}
|
|
@Override
|
public void onClick(View v) {
|
|
switch (v.getId()) {
|
case R.id.tv_skip:
|
|
|
break;
|
}
|
|
}
|
|
|
public interface IVideoAdListener {
|
|
//结束播放
|
public void finishPlay();
|
|
//开始播放
|
public void startPlay();
|
|
}
|
}
|