Commit 510351f4 authored by jyx's avatar jyx

添加饭补页面信息流

parent f4929c61
package com.mints.goodmoney.ad.express
import android.os.Handler
import android.os.HandlerThread
import android.view.Gravity
import android.widget.FrameLayout
import com.bytedance.msdk.api.*
import com.bytedance.msdk.api.nativeAd.*
import com.mints.goodmoney.MintsApplication
import com.mints.goodmoney.manager.CsjGroMoreManager
import com.mints.goodmoney.ui.widgets.seekbar.BubbleUtils
import com.mints.goodmoney.utils.LogUtil
import com.mints.goodmoney.utils.UIUtils
import com.mints.goodmoney.utils.VideoOptionUtil
private val TAG = GroMoreCarrierExpressManager::class.java.simpleName
class GroMoreCarrierExpressManager : TTSettingConfigCallback {
companion object {
val instance: GroMoreCarrierExpressManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
GroMoreCarrierExpressManager()
}
}
private var mAdStyle = 0
private var mExpressType = 0
private val frameLayout: FrameLayout? = null
private var mHandlerThread: HandlerThread? = null
private var mWorkHandler: Handler? = null
private var mTTAdNative: TTUnifiedNativeAd? = null
init {
mAdStyle = AdSlot.TYPE_EXPRESS_AD
}
fun loadADFrameLayout() {
// 销毁及移除事件
destroy()
frameLayout?.removeAllViews()
if (TTMediationAdSdk.configLoadSuccess()) {
loadAd()
} else {
TTMediationAdSdk.registerConfigCallback(this)
}
}
/**
* config成功,加载信息流
*/
override fun configLoad() {
loadAd()
}
private fun loadAd() {
mTTAdNative = TTUnifiedNativeAd(MintsApplication.getContext(), CsjGroMoreManager.AD_UNIT_ID)
//视频声音控制设置
var videoOption: TTVideoOption = VideoOptionUtil.getTTVideoOption()
//判断是否需要使用GDT Feed 模板渲染2.0;注意!!!!需要向广点通商务申请模渲染2.0权限
// 注意不设置默认使用 GDT Feed 模板渲染1.0,默认情况下无需执行下方代码
if (mAdStyle == AdSlot.TYPE_EXPRESS_AD && mExpressType == GDTExtraOption.FeedExpressType.FEED_EXPRESS_TYPE_2) {
videoOption = VideoOptionUtil.getTTVideoOption2()
}
//针对Admob Native的特殊配置项
val admobNativeAdOptions = AdmobNativeAdOptions()
admobNativeAdOptions.setAdChoicesPlacement(AdmobNativeAdOptions.ADCHOICES_TOP_RIGHT) //设置广告小标默认情况下,广告选择叠加层会显示在右上角。
.setRequestMultipleImages(true).isReturnUrlsForImageAssets = true //设置为true,SDK会仅提供Uri字段的值,允许自行决定是否下载实际图片,同时不会提供Drawable字段的值
// 针对Gdt Native自渲染广告,可以自定义gdt logo的布局参数。该参数可选,非必须。
val gdtNativeAdLogUtiloParams = FrameLayout.LayoutParams(
BubbleUtils.dp2px(40),
BubbleUtils.dp2px(14),
Gravity.END or Gravity.TOP) // 例如,放在右上角
/**
* 创建feed广告请求类型参数AdSlot,具体参数含义参考文档
* 备注
* 1: 如果是信息流自渲染广告,设置广告图片期望的图片宽高 ,不能为0
* 2:如果是信息流模板广告,宽度设置为希望的宽度,高度设置为0(0为高度选择自适应参数)
*/
/**
* 创建feed广告请求类型参数AdSlot,具体参数含义参考文档
* 备注
* 1: 如果是信息流自渲染广告,设置广告图片期望的图片宽高 ,不能为0
* 2:如果是信息流模板广告,宽度设置为希望的宽度,高度设置为0(0为高度选择自适应参数)
*/
val adSlot = AdSlot.Builder()
.setTTVideoOption(videoOption) //视频声音相关的配置
.setAdmobNativeAdOptions(admobNativeAdOptions)
.setAdStyleType(mAdStyle) //必传,表示请求的模板广告还是原生广告,AdSlot.TYPE_EXPRESS_AD:模板广告 ; AdSlot.TYPE_NATIVE_AD:原生广告
.setImageAdSize(UIUtils.getScreenWidthDp(MintsApplication.getContext()).toInt(), 320) // 必选参数 单位dp ,详情见上面备注解释
.setAdCount(1) //请求广告数量为1到3条
.setGdtNativeAdLogoParams(gdtNativeAdLogUtiloParams) // 设置gdt logo布局参数。
.build()
//请求广告,调用feed广告异步请求接口,加载到广告后,拿到广告素材自定义渲染
/**
* 注:每次加载信息流广告的时候需要新建一个TTUnifiedNativeAd,否则可能会出现广告填充问题
* (例如:mTTAdNative = new TTUnifiedNativeAd(this, mAdUnitId);)
*/
mTTAdNative?.loadAd(adSlot, object : TTNativeAdLoadCallback {
override fun onAdLoadedFial(adError: AdError?) {
LogUtil.e(TAG, "load feed ad error : " + adError?.code + ", " + adError?.message)
}
override fun onAdLoaded(ads: MutableList<TTNativeAd>?) {
if (ads == null || ads.isEmpty()) {
LogUtil.e(TAG, "on FeedAdLoaded: ad is null!")
return
}
for (ttNativeAd in ads) {
LogUtil.e(TAG, " ")
LogUtil.e(TAG, "adNetworkPlatformId: " + ttNativeAd.adNetworkPlatformId + " adNetworkRitId:" + ttNativeAd.adNetworkRitId + " preEcpm: " + ttNativeAd.preEcpm)
}
if (ads.size > 0) {
frameLayout?.addView(ads[0].expressView)
}
}
})
}
fun getADFrameLayout(): FrameLayout? {
return frameLayout
}
/**
* 销毁
*/
private fun destroy() {
mHandlerThread?.quit()
mHandlerThread = null
mWorkHandler?.removeCallbacksAndMessages(null)
mWorkHandler = null
}
//渲染模板广告
// private fun getExpressAdView(ad: TTNativeAd): View? {
// val adViewHolder: GroMoreExpressManager.ExpressAdViewHolder
// val convertView = LayoutInflater.from(MintsApplication.getContext()).inflate(R.layout.listitem_ad_native_express, null, false)
// try {
// adViewHolder = GroMoreExpressManager.ExpressAdViewHolder()
// adViewHolder.mAdContainerView = convertView.findViewById(R.id.iv_listitem_express) as FrameLayout
// convertView.tag = adViewHolder
//
// //判断是否存在dislike按钮
//// if (ad.hasDislike()) {
//// ad.setDislikeCallback(MintsApplication.getContext() as Activity?, object : TTDislikeCallback {
//// override fun onSelected(position: Int, value: String) {
//// ToastUtil.show(MintsApplication.getContext(), "点击 $value")
//// //用户选择不喜欢原因后,移除广告展示
//// }
////
//// override fun onCancel() {
//// ToastUtil.show(MintsApplication.getContext(), "dislike 点击了取消")
//// LogUtil.d(TAG, "dislike 点击了取消")
//// }
////
//// /**
//// * 拒绝再次提交
//// */
//// override fun onRefuse() {}
//// override fun onShow() {}
//// })
//// }
//
// //设置点击展示回调监听
// ad.setTTNativeAdListener(object : TTNativeExpressAdListener {
// override fun onAdClick() {
// LogUtil.d(TAG, "onAdClick")
// ToastUtil.show(MintsApplication.getContext(), "模板广告被点击")
// }
//
// override fun onAdShow() {
// LogUtil.d(TAG, "onAdShow")
// ToastUtil.show(MintsApplication.getContext(), "模板广告show")
// }
//
// override fun onRenderFail(view: View, msg: String, code: Int) {
// ToastUtil.show(MintsApplication.getContext(), "模板广告渲染失败code=$code,msg=$msg")
// LogUtil.d(TAG, "onRenderFail code=$code,msg=$msg")
// }
//
// // ** 注意点 ** 不要在广告加载成功回调里进行广告view展示,要在onRenderSucces进行广告view展示,否则会导致广告无法展示。
// // 另外,不要直接使用参数view,而是通过ad.getExpressView()来获取广告view。
// override fun onRenderSuccess(view: View, width: Float, height: Float) {
// LogUtil.d(TAG, "onRenderSuccess")
// ToastUtil.show(MintsApplication.getContext(), "模板广告渲染成功:width=$width,height=$height")
// //回调渲染成功后将模板布局添加的父View中
// if (adViewHolder.mAdContainerView != null) {
// //获取视频播放view,该view SDK内部渲染,在媒体平台可配置视频是否自动播放等设置。
// val sWidth: Int
// val sHeight: Int
// val video = ad.expressView // 使用该方式获取广告view,而不是直接使用onRenderSuccess的参数view。
// if (width == TTAdSize.FULL_WIDTH.toFloat() && height == TTAdSize.AUTO_HEIGHT.toFloat()) {
// sWidth = FrameLayout.LayoutParams.MATCH_PARENT
// sHeight = FrameLayout.LayoutParams.WRAP_CONTENT
// } else {
// sWidth = UIUtils.getScreenWidthDp(MintsApplication.getContext()).toInt()
// sHeight = (sWidth * height / width).toInt()
// }
// if (video != null) {
// if (video.parent == null) {
// val layoutParams = FrameLayout.LayoutParams(sWidth, sHeight)
// adViewHolder.mAdContainerView!!.removeAllViews()
// adViewHolder.mAdContainerView!!.addView(video, layoutParams)
// }
// }
// }
// }
// })
//
//
// //视频广告设置播放状态回调(可选)
// ad.setTTVideoListener(object : TTVideoListener {
// override fun onVideoStart() {
// ToastUtil.show(MintsApplication.getContext(), "模板广告视频开始播放")
// LogUtil.d(TAG, "onVideoStart")
// }
//
// override fun onVideoPause() {
// ToastUtil.show(MintsApplication.getContext(), "模板广告视频暂停")
// LogUtil.d(TAG, "onVideoPause")
// }
//
// override fun onVideoResume() {
// ToastUtil.show(MintsApplication.getContext(), "模板广告视频继续播放")
// LogUtil.d(TAG, "onVideoResume")
// }
//
// override fun onVideoCompleted() {
// ToastUtil.show(MintsApplication.getContext(), "模板播放完成")
// LogUtil.d(TAG, "onVideoCompleted")
// }
//
// override fun onVideoError(adError: AdError) {
// ToastUtil.show(MintsApplication.getContext(), "模板广告视频播放出错")
// LogUtil.d(TAG, "onVideoError")
// }
// })
// ad.render()
// } catch (e: Exception) {
// e.printStackTrace()
// }
// return convertView
// }
}
\ No newline at end of file
......@@ -301,7 +301,6 @@ class GroMoreExpressManager : TTSettingConfigCallback {
return convertView
}
/**
* @param ad
* @return
......
......@@ -3,10 +3,8 @@ package com.mints.goodmoney.ui.activitys
import android.os.Bundle
import android.view.View
import com.mints.goodmoney.R
import com.mints.goodmoney.ad.express.GroMoreExpressManager
import com.mints.goodmoney.ad.express.GroMoreCarrierExpressManager
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.ad.express.TTPreLoadCarrierExpressManager
import com.mints.goodmoney.manager.TtCsjAdManager
import com.mints.goodmoney.mvp.model.MealBean
import com.mints.goodmoney.mvp.presenters.FoodSubsidyPresenter
import com.mints.goodmoney.mvp.views.FoodSubsidyView
......@@ -51,9 +49,6 @@ class FoodSubsidyActivity : BaseActivity(), View.OnClickListener, FoodSubsidyVie
svWalk?.destoryAnim()
flSubsidykAd?.removeAllViews()
GroMoreExpressManager.instance.loadADFrameLayout()
foodSubsidyPresenter.detachView()
}
......@@ -92,7 +87,8 @@ class FoodSubsidyActivity : BaseActivity(), View.OnClickListener, FoodSubsidyVie
}
// 预加载信息流
TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_EAT)
// TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_EAT)
GroMoreCarrierExpressManager.instance.loadADFrameLayout()
}
......@@ -101,11 +97,11 @@ class FoodSubsidyActivity : BaseActivity(), View.OnClickListener, FoodSubsidyVie
*/
private fun initExpress() {
try {
if (TTPreLoadCarrierExpressManager.getInstance().ttFrameLayout != null) {
if (GroMoreCarrierExpressManager.instance.getADFrameLayout() != null) {
if (flSubsidykAd != null) {
flSubsidykAd.removeAllViews()
// flSubsidykAd.addView(TTPreLoadCarrierExpressManager.getInstance().ttFrameLayout)
flSubsidykAd.addView(GroMoreExpressManager.instance.getADFrameLayout())
flSubsidykAd.addView(GroMoreCarrierExpressManager.instance.getADFrameLayout())
}
}
} catch (e: Exception) {
......
......@@ -5,7 +5,7 @@ import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.mints.goodmoney.R
import com.mints.goodmoney.ad.express.GroMoreExpressManager
import com.mints.goodmoney.ad.express.GroMoreCarrierExpressManager
import com.mints.goodmoney.ad.video.VideoAdingManager
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.ad.express.TTPreLoadCarrierExpressManager
......@@ -55,8 +55,6 @@ class MorningClockActivity : BaseActivity(), View.OnClickListener, MorningClockV
override fun onDestroy() {
super.onDestroy()
GroMoreExpressManager.instance.loadADFrameLayout()
flMorningClockAd?.removeAllViews()
morningClockPresenter.detachView()
}
......@@ -186,10 +184,12 @@ class MorningClockActivity : BaseActivity(), View.OnClickListener, MorningClockV
*/
private fun initExpress() {
try {
if (flMorningClockAd != null) {
flMorningClockAd.removeAllViews()
if (GroMoreCarrierExpressManager.instance.getADFrameLayout() != null) {
if (flMorningClockAd != null) {
flMorningClockAd.removeAllViews()
// flMorningClockAd.addView(TTPreLoadCarrierExpressManager.getInstance().ttFrameLayout)
flMorningClockAd.addView(GroMoreExpressManager.instance.getADFrameLayout())
flMorningClockAd.addView(GroMoreCarrierExpressManager.instance.getADFrameLayout())
}
}
} catch (e: Exception) {
e.printStackTrace()
......
......@@ -3,10 +3,8 @@ package com.mints.goodmoney.ui.activitys
import android.os.Bundle
import android.view.View
import com.mints.goodmoney.R
import com.mints.goodmoney.ad.express.GroMoreExpressManager
import com.mints.goodmoney.ad.express.GroMoreCarrierExpressManager
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.ad.express.TTPreLoadCarrierExpressManager
import com.mints.goodmoney.manager.TtCsjAdManager
import com.mints.goodmoney.mvp.model.WalkBean
import com.mints.goodmoney.mvp.presenters.WalkPresenter
import com.mints.goodmoney.mvp.views.WalkView
......@@ -45,8 +43,6 @@ class WalkActivity : BaseActivity(), View.OnClickListener, WalkView, StepView.St
override fun onDestroy() {
super.onDestroy()
GroMoreExpressManager.instance.loadADFrameLayout()
svWalk?.destoryAnim()
flWalkAd?.removeAllViews()
walkPresenter.detachView()
......@@ -102,7 +98,8 @@ class WalkActivity : BaseActivity(), View.OnClickListener, WalkView, StepView.St
}
// 预加载信息流
TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_WALK)
// TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_WALK)
GroMoreCarrierExpressManager.instance.loadADFrameLayout()
}
/**
......@@ -110,11 +107,11 @@ class WalkActivity : BaseActivity(), View.OnClickListener, WalkView, StepView.St
*/
private fun initExpress() {
try {
if (TTPreLoadCarrierExpressManager.getInstance().ttFrameLayout != null) {
if (GroMoreCarrierExpressManager.instance.getADFrameLayout() != null) {
if (flWalkAd != null) {
flWalkAd.removeAllViews()
// flWalkAd.addView(TTPreLoadCarrierExpressManager.getInstance().ttFrameLayout)
flWalkAd.addView(GroMoreExpressManager.instance.getADFrameLayout())
flWalkAd.addView(GroMoreCarrierExpressManager.instance.getADFrameLayout())
}
}
} catch (e: Exception) {
......
......@@ -3,10 +3,8 @@ package com.mints.goodmoney.ui.activitys
import android.os.Bundle
import android.view.View
import com.mints.goodmoney.R
import com.mints.goodmoney.ad.express.GroMoreExpressManager
import com.mints.goodmoney.ad.express.GroMoreCarrierExpressManager
import com.mints.goodmoney.common.Constant
import com.mints.goodmoney.ad.express.TTPreLoadCarrierExpressManager
import com.mints.goodmoney.manager.TtCsjAdManager
import com.mints.goodmoney.mvp.model.WaterBean
import com.mints.goodmoney.mvp.presenters.WaterPresenter
import com.mints.goodmoney.ui.activitys.base.BaseActivity
......@@ -48,8 +46,6 @@ class WaterActivity : BaseActivity(), View.OnClickListener, WaterView.WaterViewL
override fun onDestroy() {
super.onDestroy()
GroMoreExpressManager.instance.loadADFrameLayout()
// 取消水滴动画
wvWater?.destoryAnim()
wvWater?.closeWaterViewTime()
......@@ -72,7 +68,8 @@ class WaterActivity : BaseActivity(), View.OnClickListener, WaterView.WaterViewL
}
// 预加载信息流
TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_WATER)
// TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_WATER)
GroMoreCarrierExpressManager.instance.loadADFrameLayout()
}
override fun clickForWaterSuc(waterCoin: Int) {
......@@ -114,10 +111,12 @@ class WaterActivity : BaseActivity(), View.OnClickListener, WaterView.WaterViewL
*/
private fun initExpress() {
try {
if (flWaterAd != null) {
flWaterAd.removeAllViews()
if (GroMoreCarrierExpressManager.instance.getADFrameLayout() != null) {
if (flWaterAd != null) {
flWaterAd.removeAllViews()
// flWaterAd.addView(TTPreLoadCarrierExpressManager.getInstance().ttFrameLayout)
flWaterAd.addView(GroMoreExpressManager.instance.getADFrameLayout())
flWaterAd.addView(GroMoreCarrierExpressManager.instance.getADFrameLayout())
}
}
} catch (e: Exception) {
e.printStackTrace()
......
......@@ -19,6 +19,7 @@ import com.mints.goodmoney.BuildConfig
import com.mints.goodmoney.MintsApplication
import com.mints.goodmoney.R
import com.mints.goodmoney.ad.banner.BannerManager
import com.mints.goodmoney.ad.express.GroMoreCarrierExpressManager
import com.mints.goodmoney.ad.express.PreLoadExpressManager
import com.mints.goodmoney.ad.express.TTPreLoadCarrierExpressManager
import com.mints.goodmoney.ad.express.TTPreLoadExpressXmlyManager
......@@ -1012,11 +1013,13 @@ class MyFragment : BaseFragment(),
readyGo(XiaomanActivity::class.java, bundle)
}
Constant.HOT_ACTIVITY_WATER -> {
TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_WATER)
GroMoreCarrierExpressManager.instance.loadADFrameLayout()
// TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_WATER)
readyGo(WaterActivity::class.java)
}
Constant.HOT_ACTIVITY_WALK -> {
TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_WALK)
GroMoreCarrierExpressManager.instance.loadADFrameLayout()
// TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_WALK)
readyGo(WalkActivity::class.java)
}
Constant.HOT_ACTIVITY_CARD -> {
......@@ -1026,7 +1029,8 @@ class MyFragment : BaseFragment(),
readyGo(LiebaoGameActivity::class.java)
}
Constant.HOT_ACTIVITY_MORNINGCLOCK -> {
TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_MORNINGCLOCK)
GroMoreCarrierExpressManager.instance.loadADFrameLayout()
// TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_MORNINGCLOCK)
readyGo(MorningClockActivity::class.java)
}
Constant.HOT_ACTIVITY_BXM -> {
......@@ -1106,7 +1110,8 @@ class MyFragment : BaseFragment(),
readyGo(WrapperActivity::class.java, bundle)
}
Constant.HOT_ACTIVITY_EAT -> {
TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_EAT)
GroMoreCarrierExpressManager.instance.loadADFrameLayout()
// TTPreLoadCarrierExpressManager.getInstance().loadTtFrameLayout(TtCsjAdManager.TT_AD_NATIVEEXPRESS_EAT)
readyGo(FoodSubsidyActivity::class.java)
}
Constant.HOT_ACTIVITY_SHARE_NEWS -> {
......
<?xml version="1.0" encoding="utf-8"?>
<com.bytedance.msdk.api.format.TTNativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment