package com.zw.bobo.ad;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import com.kuaiyou.loader.AdViewBannerManager;
import com.kuaiyou.loader.AdViewInstlManager;
import com.kuaiyou.loader.AdViewNativeManager;
import com.kuaiyou.loader.AdViewSpreadManager;
import com.kuaiyou.loader.AdViewVideoManager;
import com.kuaiyou.loader.InitSDKManager;
import com.kuaiyou.loader.loaderInterface.AdViewInstlListener;
import com.kuaiyou.loader.loaderInterface.AdViewNativeListener;
import com.kuaiyou.loader.loaderInterface.AdViewVideoListener;
import com.zw.bobo.R;
import com.zw.bobo.util.Logger;
import java.util.HashMap;
import java.util.List;
/**
* AdView 广告的管理类
* 通过AdViewBusinessUtil调用
* author:Hzc
* Date: 2019/9/28
*/
public class AdViewAdManager {
public static final String ADVIEW_SDK_KEY = "SDK20192327110904r682sw3ghu0qdm5";
//广告位ID:
//开屏:
public static final String SPLASHVIEW_SDK_KEY = "POSIDmtasp31v7fg8";
//横幅:原生广告
public static final String BANNER_SDK_KEY = "POSIDz7og7e1nh0b9";
//原生广告 信息流:
public static final String WHEEL_PLANTING_SDK_KEY = "POSIDhp9snojpogqh";
//悬浮:
public static final String FLOAT_SDK_KEY = "POSIDvs5mt6hcyud4";
// 原生广告 测试用
public static final String DEBUG_BANNER_SDK_KEY = "NATIVEchzdqc0qachn";
// contentView 的id 用于销毁,数字随意写的
public static final int AdView_contentView_id = 11298;
public static final String sdkKey = AdConstants.ADVIEW_SDK_KEY;
//adView测试key
// public static final String sdkKey = "SDK20161629040641z7snyxkrbndasty";
public static String HTML = "<meta charset='utf-8'><style type='text/css'>* { padding: 0px; margin: 0px;}a:link { text-decoration: none;}</style><div style='width: 100%; height: 100%;'><img src=\"image_path\" width=\"100%\" height=\"100%\" ></div>";
private static AdViewAdManager manager;
private int NATIVEVIDEO_START = 1;//视频开始播放
private int NATIVEVIDEO_MEDIUM = 2;//视频播放1/2
private int NATIVEVIDEO_END = 3;//视频播放完成
private AdViewBannerManager adViewBIDView;
private AdViewNativeManager adViewNativeManager;
private AdViewAdManager() {
}
public static AdViewAdManager getInstance() {
if (manager == null) {
synchronized (AdViewAdManager.class) {
if (manager == null) {
manager = new AdViewAdManager();
}
}
}
return manager;
}
//针对api>19的机型,建议显示开屏时隐藏NavigationBar,代码如下:
public static void hideNavigationBar(Context context) {
if (Build.VERSION.SDK_INT >= 19) {
View decorView = ((Activity) context).getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
}
}
// //当成功接收到广告时 初始化插屏布局
// @Override
// public void onAdReceived () {
// //此时可以调用 展示插屏方法,3.8.8版本以后请使用onAdReady方法标识加载完成
// adInstlBIDView.showInstl(context)
// //如果使用自定义插屏,可使用下面的方法,获取广告view,自定义展示
// adInstlBIDView.getDialogView();
// }
//5.视频广告创建
public static void videoAdItem(
Context context, String appId, String posId,
AdViewVideoListener adViewVideoListener) {
//初始化视频广告(传入参数依次为Context、sdkKey、广告位id、监听接口、是否贴片)
AdViewVideoManager videoManager = new AdViewVideoManager(
context, appId, posId,
adViewVideoListener, false);
//当视频回调onVideoReady接口调用时,表示视频加载完毕 可以播放
videoManager.playVideo(context);
//设置视频播放时的背景颜色
videoManager.setVideoBackgroungColor("#000000");
//设置视频播放视频的横竖屏方向
videoManager.setVideoOrientation(0);
//获取当前视频状态,判断是否准备完毕
videoManager.isReady();
}
// 1.广告条代码创建(单次请求模式) // 横幅
public AdViewBannerManager adBannerBuild(Context context, int adSize,
com.kuaiyou.loader.loaderInterface.AdViewBannerListener adViewBannerListener) {
//初始化banner(传入的参数依次为 context,sdkKey,尺寸,是否刷新)
adViewBIDView = new AdViewBannerManager(context, sdkKey, adSize, true);
//设置下载类广告非wifi状态下是否显示二次确认窗口。请在广告初始化后调用该方法
InitSDKManager.setDownloadNotificationEnable(true);
//设置banner的关闭按钮
adViewBIDView.setShowCloseBtn(false);
//设置广告切换时间(秒),如果不设置默认不自动切换广告,
adViewBIDView.setRefreshTime(15);
//设置广告自动切换时动画效果
adViewBIDView.setOpenAnim(true);
//设置监听回调
adViewBIDView.setOnAdViewListener(adViewBannerListener);
return adViewBIDView;
}
public void adViewDestory() {
//当不再需要展示广告时,调用以下方法释放资源
if (adViewBIDView != null) {
adViewBIDView.destroy();
}
}
//2.插屏代码创建
public AdViewInstlManager adInsterItem(Context context, AdViewInstlListener adViewInstlListener) {
//初始化Instl(传入的参数依次为 Context,sdkKey,是否可关闭)
AdViewInstlManager adInstlBIDView = new AdViewInstlManager(context, sdkKey, true);
//设置监听回调
adInstlBIDView.setOnAdViewListener(adViewInstlListener);
return adInstlBIDView;
}
//3. 开屏代码创建
public AdViewSpreadManager splashViewAd(
Context context, ViewGroup parentView,
int logoDrawable, int backgroundColor,
com.kuaiyou.loader.loaderInterface.AdViewSpreadListener adViewSpreadListener) {
//初始化Instl(传入的参数依次为 Context,sdkKey,需要展示开屏广告的外层布局)
AdViewSpreadManager adViewBIDSpread = new AdViewSpreadManager(context, sdkKey, parentView);
//设置顶部倒计时通知方式,默认不通知
adViewBIDSpread.setSpreadNotifyType(AdViewSpreadManager.NOTIFY_COUNTER_TEXT);
//设置开屏广告logo
// adViewBIDSpread.setLogo(logoDrawable);
// 设置开屏广告背景颜色
// adViewBIDSpread.setBackgroundColor(R.mipmap.launcher);
//设置开屏广告监听回调
adViewBIDSpread.setOnAdViewListener(adViewSpreadListener);
// 3.1 开屏顶部通知设置
// 枚举名称 描述 常量值
// NOTIFY_COUNTER_NULL 不显示任何倒计时提示 0
// NOTIFY_COUNTER_NUM 设置后顶部显示倒计时 1
// NOTIFY_COUNTER_TEXT 设置后顶部显示为跳过按钮
// (在规定展示时间之后才会出现) 2
// NOTIFY_COUNTER_CUSTOM 设置后
// 将调用onAdNotifyCustomCallback(),可在其中自定义通知样式 3
return adViewBIDSpread;
}
//4.2原生广告字段描述
// 字段 类型 说明
// 1、原生图文广告
// adIcon String Icon图片链接
// adImage String 大图数据的图片链接
// adFlagLogo String Logo
// adFlagIcon String Icon
// sec_description String 补充广告描述文本
// imageList List 原生图片数组(返回多个图片)
// nativeView View 已渲染的原生 View(可选)
// imageWidth int 大图宽
// imageHeight int 大图高
// iconWidth int Icon 宽
// iconHeight int Icon 高
// 2、原生视频广告
// videoUrl String 视频物料下载地址
// iconUrl String 视频 icon 图片地址
// duration String 视频播放时长
// preImage String 视频前贴片图片地址
// endHtml String 播放完整页面 html
// endImgUrl String 播放完成页面图片地址
// 3、共用字段
// adId String 广告 Id,用于区分广告内容
// title String 广告标题
// description String 广告描述内容
// 注:
// 1.原生物料广告(除 nativeView 字段外)需要开发者自行组合,并且手动调用展
// 示上报以及点击上报;
// 2.当返回 nativeView 字段时,将不会返回其他原生物料字段,开发者只需将
// nativeView 添加到界面中进行展示即可;开发者可根据 nativeView 的返回值判断由哪
// 种方式进行展示。
// 3.原生视频广告,需要手动添加汇报方法
// 原生广告创建 (大图数据的图片链接 or 视频)
public AdViewNativeManager nativeAdBuild(
Context context, String appId, String posId, int width, int height, AdViewNativeListener adViewNativeListener) {
//初始化原生广告(传入的参数依次为 Context,sdkKey,广告位id,监听接口)
AdViewNativeManager adViewNative = new AdViewNativeManager(
context, appId, posId, adViewNativeListener);
//头条广告打底,需要设置广告尺寸
adViewNative.setAdSize(width, height);
//请求广告 (可选参数 :广告条数)
adViewNative.requestAd();
return adViewNative;
}
// 原生广告展示处理(视频or图片)
public void nativeShowAdListener(
Context context, ViewGroup natAd,
AdViewNativeManager adViewNative, List nativeAdList) {
// 汇报展示
View view = null;
VideoView videoView = null;
View contentView = null;
if (null != natAd) {
//natAd.removeAllViews();
for (int i = 0; i < natAd.getChildCount(); i++) {
View viewchild = natAd.getChildAt(i);
if (viewchild == null) {
continue;
}
Logger.e(Logger.AD_TAG, "viewchild.getId():" + viewchild.getId());
if (viewchild.getId() == AdViewAdManager.AdView_contentView_id ) {
natAd.removeView(viewchild);
}
}
}
if (null != nativeAdList && !nativeAdList.isEmpty()) {
LayoutInflater inflater = LayoutInflater.from(context);
HashMap nativeAd = (HashMap) nativeAdList.get(0);
if (null != nativeAd.get("videoUrl")) {
Logger.e(Logger.AD_TAG, "原生视频");
//Log.i("原生视频信息:", "videoUrl=" + nativeAd.get("videoUrl") + "\ntitle=" + nativeAd.get("title") + "\niconUrl=" + nativeAd.get("adIcon") + "\ndesc=" + nativeAd.get("description") + "\nendImgUrl=" + nativeAd.get("endImgUrl"));
String videoUrl2 = (String) nativeAd.get("videoUrl");
Uri uri = Uri.parse(videoUrl2);
//Toast.makeText(context, "视频广告获取成功", Toast.LENGTH_SHORT).show();
contentView = inflater.inflate(R.layout.native_video, null);
contentView.setId(AdView_contentView_id);
TextView natVideoTtl = contentView.findViewById(R.id.natVideoTtl);
WebView iconWV = contentView.findViewById(R.id.natVideoIcon);
TextView descTV = contentView.findViewById(R.id.natVideoDesc);
natVideoTtl.setText((CharSequence) nativeAd.get("title"));
descTV.setText((CharSequence) nativeAd.get("description"));
videoView = contentView.findViewById(R.id.videoView);
//设置视频控制器
videoView.setMediaController(new MediaController(context));
videoView.setZOrderOnTop(true);
//播放完成回调
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Toast.makeText(context, "播放完成了", Toast.LENGTH_SHORT).show();
}
});
if (!TextUtils.isEmpty((CharSequence) nativeAd.get("adIcon"))
&& null != iconWV) {
iconWV.loadData((HTML).replace("image_path", (CharSequence) nativeAd.get("adIcon")),
"text/html; charset=UTF-8", null);
} else {
iconWV.loadData((HTML).replace("image_path",
"http://www.adview.com/ssp/image/loff6ty1_main_20181214144226632006_1280_720.jpg"),
"text/html; charset=UTF-8", null);
}
natAd.addView(contentView);
//设置视频路径
videoView.setVideoURI(uri);
//开始播放视频
videoView.start();
//Toast.makeText(context, "视频播放了", Toast.LENGTH_SHORT).show();
} else if ((view = (View) nativeAd.get("nativeView")) != null) {
Logger.e(Logger.AD_TAG, "模板广告获取成功");
//Toast.makeText(context, "模板广告获取成功", Toast.LENGTH_SHORT).show();
contentView = inflater.inflate(R.layout.native_mod, null);
contentView.setId(AdView_contentView_id);
LinearLayout natAdMod = contentView.findViewById(R.id.natAdMod);
natAdMod.addView(view);
natAd.addView(contentView, new FrameLayout.LayoutParams(-2, -2));
} else {
Logger.e(Logger.AD_TAG, "物料广告获取成功");
//Log.i("原生物料信息:", "title=" + nativeAd.get("title") + "\niconUrl=" + nativeAd.get("adIcon") + "\ndescription=" + nativeAd.get("description") + "\nimageUrl=" + nativeAd.get("adImage"));
//Toast.makeText(context, "物料广告获取成功", Toast.LENGTH_SHORT).show();
contentView = inflater.inflate(R.layout.adview_four, null);
contentView.setId(AdView_contentView_id);
WebView icon = contentView.findViewById(R.id.icon);
WebView image = contentView.findViewById(R.id.image);
TextView title = contentView.findViewById(R.id.title);
TextView desc = contentView.findViewById(R.id.desc);
TextView desc2 = contentView.findViewById(R.id.desc2);
natAd.addView(contentView);
if (null != nativeAd) {
desc.setText((CharSequence) nativeAd.get("description"));
desc2.setText((CharSequence) nativeAd.get("sec_description"));
title.setText((CharSequence) nativeAd.get("title"));
if (!TextUtils
.isEmpty((CharSequence) nativeAd.get("adImage"))
&& null != image) {
image.loadData((HTML).replace("image_path",
(CharSequence) nativeAd.get("adImage")),
"text/html; charset=UTF-8", null);
}
// else {
// image.loadData((new String(HTML)).replace("image_path",
// (CharSequence) "http://www.adview.com/ssp/image/loff6ty1_main_20181214144226632006_1280_720.jpg"),
// "text/html; charset=UTF-8", null);
// }
if (!TextUtils.isEmpty((CharSequence) nativeAd.get("adIcon"))
&& null != icon)
icon.loadData((HTML).replace("image_path",
(CharSequence) nativeAd.get("adIcon")),
"text/html; charset=UTF-8", null);
}
}
// 汇报展示
adViewNative.reportImpression((String) nativeAd.get("adId"));
adViewNative.reportVideoStatus(context, (String) nativeAd.get("adId"), NATIVEVIDEO_START);//视频开始播放时上报
adViewNative.reportVideoStatus(context, (String) nativeAd.get("adId"), NATIVEVIDEO_MEDIUM);//视频播放1/2 时上报
adViewNative.reportVideoStatus(context, (String) nativeAd.get("adId"), NATIVEVIDEO_END);//视频播放完成时上报
/***************************************************************/
// 触发点击汇报
if (null != natAd) {
VideoView finalVideoView = videoView;
natAd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != finalVideoView) {
finalVideoView.pause();
}
// natAd.performClick();
adViewNative.reportClick(
(String) nativeAd.get("adId"));
}
});
}
/***************************************************************/
}
}
// 原生视频广告,需要手动添加汇报方法如下
public void repostVideoSatts(
AdViewNativeManager adViewNative, Context context, HashMap<String, Object> nativeAd) {
//视频开始播放时上报
adViewNative.reportVideoStatus(context, (String) nativeAd.get("adId"), 1);
//视频播放 1/2 时上报
adViewNative.reportVideoStatus(context, (String) nativeAd.get("adId"), 2);
//视频播放完成时上报
adViewNative.reportVideoStatus(context, (String) nativeAd.get("adId"), 3);
}
}
网友评论