完成视频流广告
This commit is contained in:
@ -5,8 +5,6 @@ import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.gh.common.util.GsonUtils;
|
||||
import com.gh.common.util.PackageHelper;
|
||||
import com.gh.common.util.PackageUtils;
|
||||
@ -25,6 +23,7 @@ import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
|
||||
@ -12,6 +12,7 @@ import com.gh.common.util.DisplayUtils
|
||||
import com.gh.common.util.ImageUtils
|
||||
import com.gh.common.util.rxTimer
|
||||
import com.gh.gamecenter.R
|
||||
import com.gh.gamecenter.entity.SettingsEntity
|
||||
import io.reactivex.disposables.Disposable
|
||||
import kotlin.math.abs
|
||||
|
||||
@ -22,7 +23,7 @@ import kotlin.math.abs
|
||||
class AdBannerView : LinearLayout {
|
||||
|
||||
private lateinit var mViewPager2: ViewPager2
|
||||
private var mDatas = ArrayList<String>()
|
||||
private var mDatas = ArrayList<SettingsEntity.Advertisement>()
|
||||
var isAutoPlay = true
|
||||
private var currentPage = 0
|
||||
private var autoDurationTime = 6000L
|
||||
@ -70,7 +71,8 @@ class AdBannerView : LinearLayout {
|
||||
}
|
||||
|
||||
|
||||
fun start(datas: ArrayList<String>) {
|
||||
fun start(datas: ArrayList<SettingsEntity.Advertisement>) {
|
||||
if (datas.isNullOrEmpty()) throw IllegalArgumentException("广告列表不能为空")
|
||||
val adapter = mViewPager2.adapter
|
||||
mDatas = datas
|
||||
if (adapter == null) {
|
||||
@ -81,27 +83,29 @@ class AdBannerView : LinearLayout {
|
||||
}
|
||||
currentPage = (mViewPager2.adapter as AdBannerAdapter).getActualFirstPositionInCenter()
|
||||
mViewPager2.setCurrentItem(currentPage, false)
|
||||
addIndicator()
|
||||
slideIndicator(currentPage % mDatas.size)
|
||||
autoPlay()
|
||||
if (mDatas.size > 1) {
|
||||
addIndicator()
|
||||
slideIndicator(currentPage % mDatas.size)
|
||||
autoPlay()
|
||||
}
|
||||
}
|
||||
|
||||
private fun autoPlay() {
|
||||
mRxTimer = rxTimer(autoDurationTime) {
|
||||
slidePage()
|
||||
if (isAutoPlay && mDatas.size > 1) {
|
||||
mRxTimer = rxTimer(autoDurationTime) {
|
||||
slidePage()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun slidePage() {
|
||||
if (isAutoPlay && mDatas.size > 1) {
|
||||
currentPage++
|
||||
mViewPager2.setCurrentItem(currentPage, true)
|
||||
}
|
||||
currentPage++
|
||||
mViewPager2.setCurrentItem(currentPage, true)
|
||||
}
|
||||
|
||||
fun resumePage() {
|
||||
slidePage()
|
||||
autoPlay()
|
||||
}
|
||||
|
||||
fun pausePage() {
|
||||
@ -170,18 +174,19 @@ class AdBannerView : LinearLayout {
|
||||
return object : RecyclerView.ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_ad_banner, parent, false)) {}
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = Int.MAX_VALUE
|
||||
override fun getItemCount(): Int = if (mDatas.size == 1) mDatas.size else Int.MAX_VALUE
|
||||
|
||||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
||||
val imageUrl = mDatas[position % mDatas.size]
|
||||
val ad = mDatas[position % mDatas.size]
|
||||
val view = holder.itemView as SimpleDraweeView
|
||||
ImageUtils.display(view, imageUrl)
|
||||
ImageUtils.display(view, ad.image)
|
||||
holder.itemView.setOnClickListener {
|
||||
onItemClick?.invoke(position % mDatas.size)
|
||||
}
|
||||
}
|
||||
|
||||
fun getActualFirstPositionInCenter(): Int {
|
||||
if (mDatas.size == 1) return 0
|
||||
var index = itemCount / 2
|
||||
if (index % mDatas.size != 0) {
|
||||
index -= (index % mDatas.size)
|
||||
|
||||
@ -27,7 +27,7 @@ data class SettingsEntity(
|
||||
@SerializedName("article_entrance")
|
||||
private var articleEntrance: String = "",
|
||||
@SerializedName("video_advertisement")
|
||||
var videoAdvertisement: Advertisement? = null //新增字段video_advertisement,可能为空,代表广告关闭
|
||||
var videoAdvertisement: VideoAds? = null //新增字段video_advertisement,可能为空,代表广告关闭
|
||||
) {
|
||||
|
||||
fun setCommunityEntrance(communityEntrance: String) {
|
||||
@ -165,6 +165,12 @@ data class SettingsEntity(
|
||||
}
|
||||
}
|
||||
|
||||
data class VideoAds(
|
||||
var center: ArrayList<Advertisement>? = null,
|
||||
var left: ArrayList<Advertisement>? = null,
|
||||
var right: ArrayList<Advertisement>? = null
|
||||
)
|
||||
|
||||
data class Advertisement(
|
||||
var duration: Int = -1, //广告总显示秒数
|
||||
var frequency: Int = -1,//广告出现频率
|
||||
|
||||
@ -19,6 +19,7 @@ import com.gh.common.constant.Config
|
||||
import com.gh.common.constant.Constants
|
||||
import com.gh.common.exposure.ExposureListener
|
||||
import com.gh.common.util.*
|
||||
import com.gh.common.view.AdBannerView
|
||||
import com.gh.common.view.vertical_recycler.OnPagerListener
|
||||
import com.gh.common.view.vertical_recycler.PagerLayoutManager
|
||||
import com.gh.download.DownloadManager
|
||||
@ -26,6 +27,7 @@ import com.gh.download.cache.ExoCacheManager
|
||||
import com.gh.gamecenter.R
|
||||
import com.gh.gamecenter.WebActivity
|
||||
import com.gh.gamecenter.entity.LinkEntity
|
||||
import com.gh.gamecenter.entity.SettingsEntity
|
||||
import com.gh.gamecenter.entity.VideoEntity
|
||||
import com.gh.gamecenter.eventbus.EBDownloadStatus
|
||||
import com.gh.gamecenter.eventbus.EBPackage
|
||||
@ -186,14 +188,6 @@ class VideoDetailContainerFragment : BaseLazyFragment(), OnBackPressedListener {
|
||||
}
|
||||
|
||||
mViewModel.getVideoDetailList(mInitialVideoId, mLocation, isLoadNext = true)
|
||||
|
||||
//TODO:test
|
||||
adBannerLeft.start(arrayListOf("https://resource.ghzs.com/image/halo/addon/icon/5e13ed72ae56a30122271ea8.png",
|
||||
"https://resource.ghzs.com/image/halo/addon/icon/5e5f58dbdde09c2940343d87.gif",
|
||||
"https://resource.ghzs.com/image/halo/addon/icon/5e6061d0ed2d1d33c41d62cc.png"))
|
||||
adBannerLeft.onItemClick = {
|
||||
ToastUtils.showToast(it.toString())
|
||||
}
|
||||
}
|
||||
|
||||
private fun initListener() {
|
||||
@ -204,7 +198,8 @@ class VideoDetailContainerFragment : BaseLazyFragment(), OnBackPressedListener {
|
||||
val offsetY = distance % itemHeight
|
||||
val alpha = 1 - offsetY.toFloat() / itemHeight
|
||||
marquee_ad.alpha = alpha
|
||||
icon_ad.alpha = alpha
|
||||
adBannerLeft.alpha = alpha
|
||||
adBannerRight.alpha = alpha
|
||||
}
|
||||
})
|
||||
|
||||
@ -410,52 +405,67 @@ class VideoDetailContainerFragment : BaseLazyFragment(), OnBackPressedListener {
|
||||
}, 500)
|
||||
}
|
||||
|
||||
//设置广告
|
||||
private fun setAdvertisement(position: Int) {
|
||||
if (!mIsHomeVideo) return
|
||||
val videoAdvertisement = Config.getSettings()?.videoAdvertisement
|
||||
videoAdvertisement?.run {
|
||||
if (mIsHomeVideo && position == frequency) {
|
||||
if (type == "image") {//图片
|
||||
icon_ad.visibility = View.VISIBLE
|
||||
ImageUtils.display(icon_ad, image)
|
||||
} else {//跑马灯
|
||||
if (!center.isNullOrEmpty()) {
|
||||
if (position == (center!![0].frequency)) {
|
||||
marquee_ad.visibility = View.VISIBLE
|
||||
marquee_ad_title.text = title
|
||||
marquee_ad_title.text = center!![0].title
|
||||
marquee_ad_title.isSelected = true
|
||||
|
||||
marquee_ad.setOnClickListener {
|
||||
val pos = mViewPagerLayoutManager.findFirstCompletelyVisibleItemPosition()
|
||||
if (pos < 0) return@setOnClickListener
|
||||
val videoEntity = mAdapter.videoList[pos]
|
||||
MtaHelper.onEvent("视频详情", "视频广告", "${videoEntity.title}(${videoEntity.id})")
|
||||
findVisibleVideoViewByPosition()?.let {
|
||||
it.recordMta("视频广告")
|
||||
it.uploadVideoStreamingPlaying("点击广告")
|
||||
}
|
||||
|
||||
DirectUtils.directToLinkPage(requireContext(), LinkEntity(link = center!![0].link, type = center!![0].linkType, community = center!![0].linkCommunity), "", "视频详情")
|
||||
}
|
||||
if (center!![0].duration > 0 && mAdCountDownTimer == null) {
|
||||
mAdCountDownTimer = startTimer(center!![0].duration.toLong())
|
||||
}
|
||||
}
|
||||
marquee_ad.setOnClickListener {
|
||||
val pos = mViewPagerLayoutManager.findFirstCompletelyVisibleItemPosition()
|
||||
if (pos < 0) return@setOnClickListener
|
||||
val videoEntity = mAdapter.videoList[pos]
|
||||
}
|
||||
if (!left.isNullOrEmpty()) {
|
||||
setAdBanner(adBannerLeft, left!!, position)
|
||||
}
|
||||
if (!right.isNullOrEmpty()) {
|
||||
setAdBanner(adBannerRight, right!!, position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setAdBanner(banner: AdBannerView, ads: ArrayList<SettingsEntity.Advertisement>, position: Int) {
|
||||
if (position == ads[0].frequency) {
|
||||
banner.visibility = View.VISIBLE
|
||||
banner.start(ads)
|
||||
banner.onItemClick = { pos ->
|
||||
val videoPos = mViewPagerLayoutManager.findFirstCompletelyVisibleItemPosition()
|
||||
if (videoPos >= 0) {
|
||||
val videoEntity = mAdapter.videoList[videoPos]
|
||||
MtaHelper.onEvent("视频详情", "视频广告", "${videoEntity.title}(${videoEntity.id})")
|
||||
findVisibleVideoViewByPosition()?.let {
|
||||
it.recordMta("视频广告")
|
||||
it.uploadVideoStreamingPlaying("点击广告")
|
||||
}
|
||||
|
||||
DirectUtils.directToLinkPage(requireContext(), LinkEntity(link = link, type = linkType, community = linkCommunity), "", "视频详情")
|
||||
}
|
||||
icon_ad.setOnClickListener {
|
||||
val pos = mViewPagerLayoutManager.findFirstCompletelyVisibleItemPosition()
|
||||
if (pos < 0) return@setOnClickListener
|
||||
val videoEntity = mAdapter.videoList[pos]
|
||||
MtaHelper.onEvent("视频详情", "视频广告", "${videoEntity.title}(${videoEntity.id})")
|
||||
findVisibleVideoViewByPosition()?.let{
|
||||
it.recordMta("视频广告")
|
||||
it.uploadVideoStreamingPlaying("点击广告")
|
||||
}
|
||||
|
||||
if (linkType == "web") {
|
||||
if (ads[pos].linkType == "web") {
|
||||
// 当前入口打开Web页面要显示分享按钮
|
||||
val webIntentByShare = WebActivity.getWebIntentByShare(context, link, true)
|
||||
val webIntentByShare = WebActivity.getWebIntentByShare(context, ads[pos].link, true)
|
||||
startActivity(webIntentByShare)
|
||||
} else {
|
||||
marquee_ad.performClick()
|
||||
}
|
||||
}
|
||||
if (duration > 0) {
|
||||
mAdCountDownTimer = startTimer(duration.toLong())
|
||||
}
|
||||
}
|
||||
if (ads[0].duration > 0 && mAdCountDownTimer == null) {
|
||||
mAdCountDownTimer = startTimer(ads[0].duration.toLong())
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -465,7 +475,8 @@ class VideoDetailContainerFragment : BaseLazyFragment(), OnBackPressedListener {
|
||||
return countDownTimer(countDownTime) { finish, _ ->
|
||||
if (finish) {
|
||||
marquee_ad.visibility = View.GONE
|
||||
icon_ad.visibility = View.GONE
|
||||
adBannerLeft.visibility = View.GONE
|
||||
adBannerRight.visibility = View.GONE
|
||||
findVisibleVideoViewByPosition()?.uploadVideoStreamingPlaying("关闭广告")
|
||||
}
|
||||
}
|
||||
@ -503,6 +514,8 @@ class VideoDetailContainerFragment : BaseLazyFragment(), OnBackPressedListener {
|
||||
DisplayUtils.setLightStatusBar(requireActivity(), false, false)
|
||||
}, 100)
|
||||
}
|
||||
adBannerLeft.resumePage()
|
||||
adBannerRight.resumePage()
|
||||
super.onFragmentResume()
|
||||
}
|
||||
|
||||
@ -527,7 +540,8 @@ class VideoDetailContainerFragment : BaseLazyFragment(), OnBackPressedListener {
|
||||
slideGuideAnimation.cancelAnimation()
|
||||
guideRl.visibility = View.GONE
|
||||
}
|
||||
SPUtils.setBoolean(Constants.SP_SHOW_SLIDE_GUIDE, true)
|
||||
adBannerLeft.pausePage()
|
||||
adBannerRight.pausePage()
|
||||
super.onFragmentPause()
|
||||
ExoCacheManager.cancelAll()
|
||||
}
|
||||
@ -540,6 +554,7 @@ class VideoDetailContainerFragment : BaseLazyFragment(), OnBackPressedListener {
|
||||
}, 500)
|
||||
if (mAdCountDownTimer != null && !mAdCountDownTimer!!.isDisposed) {
|
||||
mAdCountDownTimer?.dispose()
|
||||
mAdCountDownTimer = null
|
||||
}
|
||||
super.onDestroyView()
|
||||
ExoCacheManager.cancelAll()
|
||||
|
||||
Reference in New Issue
Block a user