diff --git a/.github/ISSUE_TEMPLATE/----.md b/.github/ISSUE_TEMPLATE/----.md new file mode 100644 index 0000000..56b3c6a --- /dev/null +++ b/.github/ISSUE_TEMPLATE/----.md @@ -0,0 +1,13 @@ +--- +name: 功能建议 +about: 说出你对项目的建议 +title: '' +labels: enhancement +assignees: ifmvo + +--- + +**你的建议基于你遇到什么问题?** + + +**你希望怎样改进?** diff --git a/.github/ISSUE_TEMPLATE/bug--.md b/.github/ISSUE_TEMPLATE/bug--.md new file mode 100644 index 0000000..0bd02d4 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug--.md @@ -0,0 +1,30 @@ +--- +name: Bug反馈 +about: 说出你遇到的问题 +title: '例: 开屏广告倒计时结束后发生崩溃' +labels: bug +assignees: ifmvo + +--- + +**Bug 描述和日志** + + +**Bug 复现的步骤** +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**截图** +如果Bug展示较为明显,最好附上截图 + +**系统版本以及设备型号** + - 设备型号:华为Mate40Pro] + - 系统版本:Android11 + - TogetherAd版本: + implementation 'com.matthewchen.togetherad:csj:x.x.x-x.x.x.x' + implementation 'com.matthewchen.togetherad:gdt:x.x.x-x.xxx.xxxx' + implementation 'com.matthewchen.togetherad:baidu:x.x.x-x.xx' + +**附加信息** diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e80e45a --- /dev/null +++ b/.gitignore @@ -0,0 +1,54 @@ +# Copy from Alibaba open source + +# Built application files + +# Files for the Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +# local.properties +.classpath +.project +.settings/ + +# Proguard folder generated by Eclipse +proguard/ + +#Log Files +*.log + +# OS X +.DS_Store + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.war +*.ear +*.iml + +# IDEA Files +.idea/ +.svn/ +out/ + +# MAVEN COMPILE Files +target/ +lint.xml +local.properties + +deploy.gradle +jcenterDeploy.gradle +jcenterInstall.gradle \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3a8ae37 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 陈铭卓 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a8a920d --- /dev/null +++ b/README.md @@ -0,0 +1,132 @@ +# TogetherAd + +TogetherAd 是由 Kotlin 编写的 Android 广告聚合开源项目。 + +能够帮助 Android 开发者``快速``、``便捷``、``灵活``的 ``接入并使用`` 国内多家主流广告 SDK。 + +TogetherAd 也是一种将各个广告提供商``组件化处理``的解决方案。 + +默认提供了三家广告提供商:``字节的穿山甲``、``腾讯的优量汇``、``百度的百青藤``。 + +如果这三家满足不了你的需求,还需要其他家广告提供商,可参考[扩展文档](doc/extend.md)。 + +TogetherAd 也提供了很多自定义功能,比如:``按权重分发广告``、``失败切换``、``超时时间``、``自定义图片加载器``、``热启动开屏广告解决方案``等... + +### 安装 Demo + +微信扫描底部二维码,回复 ``apk`` 可下载 Demo Apk 尝鲜 + +### Gradle 添加依赖 + +项目根目录下的 build.gradle 文件中添加 ``JitPack`` 仓库 + +```gradle +allprojects { + repositories { + ... + maven { url 'https://jitpack.io' } + } +} +``` + +``core``是必选,其他3个根据自身需求``任选``1 ~ 3个组合搭配 + +```gradle +dependencies { + + //核心库( 必要 ) + implementation 'com.github.ifmvo.TogetherAd:core:5.0.3' + + //优量汇( 腾讯广点通 )可选 + implementation 'com.github.ifmvo.TogetherAd:gdt:5.0.3' + + //穿山甲( 头条 )可选 + implementation 'com.github.ifmvo.TogetherAd:csj:5.0.3' + + //百青藤 ( 百度 Mob ) 可选 + implementation 'com.github.ifmvo.TogetherAd:baidu:5.0.3' + +} +``` + +> 对应版本:穿山甲3.6.1.1;优量汇4.351.1221;百度5.91 + +### 使用方法 + +- [特色功能](doc/feature.md) + +- [准备工作及初始化](doc/prepare.md) + +- [开屏广告](doc/splash.md) + +- [Banner横幅广告](doc/banner.md) + +- [Interstitial插屏广告](doc/inter.md) + +- [原生模板](doc/express.md) + +- [原生自渲染](doc/native.md) + +- [激励广告](doc/reward.md) + +- [全屏视频广告](doc/full_video.md) + +- [混合使用](doc/hybrid.md) + +- [扩展](doc/extend.md) + +- [常见问题](doc/question.md) + +- [版本更新日志](doc/update_log.md) + +### 混淆 proguard-rules.pro + +资源库中已自带混淆规则,通常情况下无需手动配置。 + +当然你也可以点击这里查看每个资源库的混淆规则: [core](core/proguard-rules.pro)、[gdt](gdt/proguard-rules.pro)、[csj](csj/proguard-rules.pro)、[baidu](baidu/proguard-rules.pro) + +### 相关文档收集 + +- [优量汇接入文档](https://developers.adnet.qq.com/doc/android/access_doc) + +- [优量汇常见问题](https://e.qq.com/dev/help_detail.html?cid=668&pid=2208) + +- [优量汇SDK修订历史](https://developers.adnet.qq.com/doc/android/union/union_version) + +- [优量汇错误码对照](https://developers.adnet.qq.com/backend/error_code.html) + +- [穿山甲文档](http://partner.toutiao.com/doc?id=5dd0fe756b181e00112e3ec5) + +- [百青藤v5.88接入文档](https://baidu-ssp.gz.bcebos.com/mssp/sdk/BaiduMobAds_MSSP_bd_SDK_android_v5.88.pdf) + +### 有疑问? + +微信扫描下面二维码, **关注后点击联系我** 可邀请进微信交流群,更多大佬为你答疑。 + + + +### License + +``` +MIT License + +Copyright (c) 2021 陈铭卓 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +``` \ No newline at end of file diff --git a/baidu/.gitignore b/baidu/.gitignore new file mode 100644 index 0000000..107ef7c --- /dev/null +++ b/baidu/.gitignore @@ -0,0 +1,53 @@ +# Copy from Alibaba open source + +# Built application files + +# Files for the Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +# local.properties +.classpath +.project +.settings/ + +# Proguard folder generated by Eclipse +proguard/ + +#Log Files +*.log + +# OS X +.DS_Store + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.war +*.ear +*.iml + +# IDEA Files +.idea/ +.svn/ +out/ + +# MAVEN COMPILE Files +target/ +lint.xml + +deploy.gradle +jcenterDeploy.gradle +jcenterInstall.gradle \ No newline at end of file diff --git a/baidu/build.gradle b/baidu/build.gradle new file mode 100644 index 0000000..a97d647 --- /dev/null +++ b/baidu/build.gradle @@ -0,0 +1,40 @@ +apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' +apply plugin: 'com.kezong.fat-aar' + +// JitPack Maven +apply plugin: 'com.github.dcendents.android-maven' +// Your Group +group = 'com.github.ifmvo' + +android { + compileSdkVersion 28 + + defaultConfig { + minSdkVersion 16 + targetSdkVersion 28 + versionCode 1 + versionName "5.0.3" + } + + lintOptions { + abortOnError false + } + + buildTypes { + release { + consumerProguardFiles 'proguard-rules.pro' + } + } + + repositories { flatDir { dirs 'libs' } } +} + +dependencies { + api fileTree(dir: 'libs', include: ['*.jar', '*.aar']) +// embed(name: 'Baidu_MobAds_SDK-release', ext: 'aar') + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + + api project(':core') +} \ No newline at end of file diff --git a/baidu/libs/Baidu_MobAds_SDK-release.aar b/baidu/libs/Baidu_MobAds_SDK-release.aar new file mode 100644 index 0000000..1a3af7f Binary files /dev/null and b/baidu/libs/Baidu_MobAds_SDK-release.aar differ diff --git a/baidu/proguard-rules.pro b/baidu/proguard-rules.pro new file mode 100644 index 0000000..abd659a --- /dev/null +++ b/baidu/proguard-rules.pro @@ -0,0 +1,34 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile + +#------------------------百度的混淆---------------------------# +-keepclassmembers class * extends android.app.Activity { + public void *(android.view.View); +} +-keepclassmembers enum * { + public static **[] values(); + public static ** valueOf(java.lang.String); +} +-keep class com.baidu.mobads.** { *; } +-keep class com.baidu.mobad.** { *; } + +-keep class com.ifmvo.togetherad.baidu.** { *; } \ No newline at end of file diff --git a/baidu/src/main/AndroidManifest.xml b/baidu/src/main/AndroidManifest.xml new file mode 100644 index 0000000..220d2ae --- /dev/null +++ b/baidu/src/main/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + diff --git a/baidu/src/main/java/com/ifmvo/togetherad/baidu/TogetherAdBaidu.kt b/baidu/src/main/java/com/ifmvo/togetherad/baidu/TogetherAdBaidu.kt new file mode 100644 index 0000000..f0ee7c8 --- /dev/null +++ b/baidu/src/main/java/com/ifmvo/togetherad/baidu/TogetherAdBaidu.kt @@ -0,0 +1,36 @@ +package com.ifmvo.togetherad.baidu + +import android.content.Context +import org.jetbrains.annotations.NotNull +import com.baidu.mobads.AdView +import com.ifmvo.togetherad.baidu.provider.BaiduProvider +import com.ifmvo.togetherad.core.TogetherAd +import com.ifmvo.togetherad.core.entity.AdProviderEntity + +/** + * 初始化百青藤 + * + * Created by Matthew Chen on 2020-04-04. + */ +object TogetherAdBaidu { + + var idMapBaidu = mutableMapOf() + + fun init(@NotNull context: Context, @NotNull adProviderType: String, @NotNull baiduAdAppId: String) { + init(context, adProviderType, baiduAdAppId, null, null) + } + + fun init(@NotNull context: Context, @NotNull adProviderType: String, @NotNull baiduAdAppId: String, providerClassPath: String? = null) { + init(context, adProviderType, baiduAdAppId, null, providerClassPath) + } + + fun init(@NotNull context: Context, @NotNull adProviderType: String, @NotNull baiduAdAppId: String, baiduIdMap: Map? = null) { + init(context, adProviderType, baiduAdAppId, baiduIdMap, null) + } + + fun init(@NotNull context: Context, @NotNull adProviderType: String, @NotNull baiduAdAppId: String, baiduIdMap: Map? = null, providerClassPath: String? = null) { + TogetherAd.addProvider(AdProviderEntity(adProviderType, if (providerClassPath?.isEmpty() != false) BaiduProvider::class.java.name else providerClassPath)) + baiduIdMap?.let { idMapBaidu.putAll(it) } + AdView.setAppSid(context, baiduAdAppId) + } +} \ No newline at end of file diff --git a/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/BaseNativeViewBaidu.kt b/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/BaseNativeViewBaidu.kt new file mode 100644 index 0000000..712e4af --- /dev/null +++ b/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/BaseNativeViewBaidu.kt @@ -0,0 +1,93 @@ +package com.ifmvo.togetherad.baidu.native_.view + +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.TextView +import com.baidu.mobad.feeds.NativeResponse +import com.ifmvo.togetherad.baidu.R +import com.ifmvo.togetherad.core.TogetherAd +import com.ifmvo.togetherad.core.custom.native_.BaseNativeView +import com.ifmvo.togetherad.core.listener.NativeViewListener + +/** + * + * Created by Matthew Chen on 2020/9/14. + */ +abstract class BaseNativeViewBaidu(onClose: ((adProviderType: String) -> Unit)? = null) : BaseNativeView() { + + private var mOnClose = onClose + + var rootView: View? = null + + open fun getLayoutRes(): Int { + return R.layout.layout_native_view_baidu + } + + open fun getMainImageView(): ImageView? { + return rootView?.findViewById(R.id.native_main_image) + } + + open fun getIconImageView(): ImageView? { + return rootView?.findViewById(R.id.native_icon_image) + } + + open fun getBaiduLogoImageView(): ImageView? { + return rootView?.findViewById(R.id.native_baidulogo) + } + + open fun getAdLogoImageView(): ImageView? { + return rootView?.findViewById(R.id.native_adlogo) + } + + open fun getTitleTextView(): TextView? { + return rootView?.findViewById(R.id.native_title) + } + + open fun getDescTextView(): TextView? { + return rootView?.findViewById(R.id.native_text) + } + + open fun getBrandNameTextView(): TextView? { + return rootView?.findViewById(R.id.native_brand_name) + } + + //关闭按钮,可以重写为任意类型的View + open fun getCloseButton(): View? { + return rootView?.findViewById(R.id.btn_close) + } + + override fun showNative(adProviderType: String, adObject: Any, container: ViewGroup, listener: NativeViewListener?) { + if (adObject !is NativeResponse) { + return + } + + //findView + rootView = View.inflate(container.context, getLayoutRes(), container) + + //Image + getMainImageView()?.let { TogetherAd.mImageLoader?.loadImage(container.context, it, adObject.imageUrl) } + getIconImageView()?.let { TogetherAd.mImageLoader?.loadImage(container.context, it, adObject.iconUrl) } + getBaiduLogoImageView()?.let { TogetherAd.mImageLoader?.loadImage(container.context, it, adObject.baiduLogoUrl) } + getAdLogoImageView()?.let { TogetherAd.mImageLoader?.loadImage(container.context, it, adObject.adLogoUrl) } + + //Text + getTitleTextView()?.let { it.text = adObject.title } + getDescTextView()?.let { it.text = adObject.desc } + getBrandNameTextView()?.let { it.text = adObject.brandName } + + //CloseBtn + getCloseButton()?.visibility = if (mOnClose == null) View.GONE else View.VISIBLE + getCloseButton()?.setOnClickListener { + mOnClose?.invoke(adProviderType) + } + + //Handle + adObject.recordImpression(rootView) + listener?.onAdExposed(adProviderType) + rootView?.setOnClickListener { + adObject.handleClick(it) + listener?.onAdClicked(adProviderType) + } + } +} \ No newline at end of file diff --git a/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple1.kt b/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple1.kt new file mode 100644 index 0000000..3769923 --- /dev/null +++ b/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple1.kt @@ -0,0 +1,22 @@ +package com.ifmvo.togetherad.baidu.native_.view + +import android.view.ViewGroup +import com.ifmvo.togetherad.baidu.R +import com.ifmvo.togetherad.core.listener.NativeViewListener +import com.ifmvo.togetherad.core.utils.ScreenUtil + +/** + * Created by Matthew Chen on 2020-04-21. + */ +class NativeViewBaiduSimple1 : BaseNativeViewBaidu() { + + override fun getLayoutRes(): Int { + return R.layout.layout_native_view_baidu_simple_1 + } + + override fun showNative(adProviderType: String, adObject: Any, container: ViewGroup, listener: NativeViewListener?) { + super.showNative(adProviderType, adObject, container, listener) + //主图 16:9 + getMainImageView()?.layoutParams?.height = (ScreenUtil.getDisplayMetricsWidth(container.context) * 9 / 16) + } +} \ No newline at end of file diff --git a/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple2.kt b/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple2.kt new file mode 100644 index 0000000..afecc7a --- /dev/null +++ b/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple2.kt @@ -0,0 +1,22 @@ +package com.ifmvo.togetherad.baidu.native_.view + +import android.view.ViewGroup +import com.ifmvo.togetherad.baidu.R +import com.ifmvo.togetherad.core.listener.NativeViewListener +import com.ifmvo.togetherad.core.utils.ScreenUtil + +/** + * Created by Matthew Chen on 2020-04-21. + */ +class NativeViewBaiduSimple2 : BaseNativeViewBaidu() { + + override fun getLayoutRes(): Int { + return R.layout.layout_native_view_baidu_simple_2 + } + + override fun showNative(adProviderType: String, adObject: Any, container: ViewGroup, listener: NativeViewListener?) { + super.showNative(adProviderType, adObject, container, listener) + //主图 16:9 + getMainImageView()?.layoutParams?.height = (ScreenUtil.getDisplayMetricsWidth(container.context) / 3 * 9 / 16) + } +} \ No newline at end of file diff --git a/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple3.kt b/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple3.kt new file mode 100644 index 0000000..542feaa --- /dev/null +++ b/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple3.kt @@ -0,0 +1,49 @@ +package com.ifmvo.togetherad.baidu.native_.view + +import android.os.CountDownTimer +import android.view.ViewGroup +import com.ifmvo.togetherad.baidu.R +import com.ifmvo.togetherad.core.custom.splashSkip.SplashSkipViewSimple2 +import com.ifmvo.togetherad.core.listener.NativeViewListener +import kotlin.math.roundToInt + +/** + * Created by Matthew Chen on 2020-04-21. + */ +class NativeViewBaiduSimple3(onClose: ((adProviderType: String) -> Unit)? = null) : BaseNativeViewBaidu() { + + private var mOnClose = onClose + private var mTimer: CountDownTimer? = null + + override fun getLayoutRes(): Int { + return R.layout.layout_native_view_baidu_simple_3 + } + + override fun showNative(adProviderType: String, adObject: Any, container: ViewGroup, listener: NativeViewListener?) { + super.showNative(adProviderType, adObject, container, listener) + //添加跳过按钮 + val customSkipView = SplashSkipViewSimple2() + val skipView = customSkipView.onCreateSkipView(container.context) + skipView.run { + container.addView(this, customSkipView.getLayoutParams()) + setOnClickListener { + mTimer?.cancel() + mOnClose?.invoke(adProviderType) + } + } + + //开始倒计时 + mTimer?.cancel() + mTimer = object : CountDownTimer(5000, 1000) { + override fun onFinish() { + mOnClose?.invoke(adProviderType) + } + + override fun onTick(millisUntilFinished: Long) { + val second = (millisUntilFinished / 1000f).roundToInt() + customSkipView.handleTime(second) + } + } + mTimer?.start() + } +} \ No newline at end of file diff --git a/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple4.kt b/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple4.kt new file mode 100644 index 0000000..1f9f014 --- /dev/null +++ b/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple4.kt @@ -0,0 +1,53 @@ +package com.ifmvo.togetherad.baidu.native_.view + +import android.os.CountDownTimer +import android.view.ViewGroup +import com.ifmvo.togetherad.baidu.R +import com.ifmvo.togetherad.core.custom.splashSkip.SplashSkipViewSimple3 +import com.ifmvo.togetherad.core.listener.NativeViewListener +import com.ifmvo.togetherad.core.utils.ScreenUtil +import kotlin.math.roundToInt + +/** + * Created by Matthew Chen on 2020-04-21. + */ +class NativeViewBaiduSimple4(onClose: ((adProviderType: String) -> Unit)? = null) : BaseNativeViewBaidu() { + + private var mOnClose = onClose + private var mTimer: CountDownTimer? = null + + override fun getLayoutRes(): Int { + return R.layout.layout_native_view_baidu_simple_4 + } + + override fun showNative(adProviderType: String, adObject: Any, container: ViewGroup, listener: NativeViewListener?) { + super.showNative(adProviderType, adObject, container, listener) + + getMainImageView()?.layoutParams?.height = (ScreenUtil.getDisplayMetricsWidth(container.context) * 9 / 16) + + //添加跳过按钮 + val customSkipView = SplashSkipViewSimple3() + val skipView = customSkipView.onCreateSkipView(container.context) + skipView.run { + container.addView(this, customSkipView.getLayoutParams()) + setOnClickListener { + mTimer?.cancel() + mOnClose?.invoke(adProviderType) + } + } + + //开始倒计时 + mTimer?.cancel() + mTimer = object : CountDownTimer(5000, 1000) { + override fun onFinish() { + mOnClose?.invoke(adProviderType) + } + + override fun onTick(millisUntilFinished: Long) { + val second = (millisUntilFinished / 1000f).roundToInt() + customSkipView.handleTime(second) + } + } + mTimer?.start() + } +} \ No newline at end of file diff --git a/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple5.kt b/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple5.kt new file mode 100644 index 0000000..95e8820 --- /dev/null +++ b/baidu/src/main/java/com/ifmvo/togetherad/baidu/native_/view/NativeViewBaiduSimple5.kt @@ -0,0 +1,7 @@ +package com.ifmvo.togetherad.baidu.native_.view + + +/** + * Created by Matthew Chen on 2020-04-21. + */ +class NativeViewBaiduSimple5(onClose: ((adProviderType: String) -> Unit)? = null) : BaseNativeViewBaidu(onClose) \ No newline at end of file diff --git a/baidu/src/main/java/com/ifmvo/togetherad/baidu/provider/BaiduProvider.kt b/baidu/src/main/java/com/ifmvo/togetherad/baidu/provider/BaiduProvider.kt new file mode 100644 index 0000000..8019ef1 --- /dev/null +++ b/baidu/src/main/java/com/ifmvo/togetherad/baidu/provider/BaiduProvider.kt @@ -0,0 +1,334 @@ +package com.ifmvo.togetherad.baidu.provider + +import android.app.Activity +import android.view.ViewGroup +import com.baidu.mobad.feeds.BaiduNative +import com.baidu.mobad.feeds.NativeErrorCode +import com.baidu.mobad.feeds.NativeResponse +import com.baidu.mobad.feeds.RequestParameters +import com.baidu.mobads.* +import com.baidu.mobads.rewardvideo.RewardVideoAd +import com.ifmvo.togetherad.baidu.TogetherAdBaidu +import com.ifmvo.togetherad.core.listener.* +import com.ifmvo.togetherad.core.provider.BaseAdProvider +import com.ifmvo.togetherad.core.utils.logd +import com.ifmvo.togetherad.core.utils.loge +import com.ifmvo.togetherad.core.utils.logi +import org.json.JSONObject + + +/** + * 广告提供商:百青藤 + * + * Created by Matthew Chen on 2020-04-03. + */ +open class BaiduProvider : BaseAdProvider() { + + object Splash { + + //超时时间 + var maxFetchDelay = 4000 + + } + + override fun loadOnlySplashAd(activity: Activity, adProviderType: String, alias: String, listener: SplashListener) { + callbackSplashStartRequest(adProviderType, alias, listener) + callbackSplashFailed(adProviderType, alias, listener, null, "百度开屏不支持加载和展示分开") + } + + override fun showSplashAd(container: ViewGroup): Boolean { + return false + } + + /** + * --------------------------- 开屏 --------------------------- + */ + override fun loadAndShowSplashAd(activity: Activity, adProviderType: String, alias: String, container: ViewGroup, listener: SplashListener) { + callbackSplashStartRequest(adProviderType, alias, listener) + + SplashAd(activity, container, object : SplashAdListener { + override fun onAdPresent() { + "onADLoaded".logd(tag) + callbackSplashLoaded(adProviderType, alias, listener) + } + + override fun onAdDismissed() { + "onADLoaded".logd(tag) + + callbackSplashDismiss(adProviderType, listener) + } + + override fun onADLoaded() { + "onADLoaded".logd(tag) + } + + override fun onAdFailed(errorMsg: String) { + "onADLoaded".logd(tag) + callbackSplashFailed(adProviderType, alias, listener, null, errorMsg) + } + + override fun onAdClick() { + "onADLoaded".logd(tag) + callbackSplashClicked(adProviderType, listener) + } + + }, TogetherAdBaidu.idMapBaidu[alias], true, Splash.maxFetchDelay) + } + + /** + * --------------------------- Banner横幅 --------------------------- + */ + private var adView: AdView? = null + override fun showBannerAd(activity: Activity, adProviderType: String, alias: String, container: ViewGroup, listener: BannerListener) { + callbackBannerStartRequest(adProviderType, alias, listener) + + adView = AdView(activity, TogetherAdBaidu.idMapBaidu[alias]) + adView?.setListener(object : AdViewListener { + override fun onAdFailed(errorMsg: String?) { + callbackBannerFailed(adProviderType, alias, listener, null, errorMsg) + } + + override fun onAdShow(p0: JSONObject?) { + callbackBannerExpose(adProviderType, listener) + } + + override fun onAdClick(p0: JSONObject?) { + callbackBannerClicked(adProviderType, listener) + } + + override fun onAdReady(p0: AdView?) { + callbackBannerLoaded(adProviderType, alias, listener) + } + + override fun onAdSwitch() { + "onAdSwitch".logi(tag) + } + + override fun onAdClose(p0: JSONObject?) { + callbackBannerClosed(adProviderType, listener) + } + }) + container.addView(adView) + } + + override fun destroyBannerAd() { + adView?.destroy() + } + + /** + * --------------------------- Inter插屏 --------------------------- + */ + private var mInterAd: InterstitialAd? = null + override fun requestInterAd(activity: Activity, adProviderType: String, alias: String, listener: InterListener) { + + callbackInterStartRequest(adProviderType, alias, listener) + "onStartRequest".logd(tag) + destroyInterAd() + + mInterAd = InterstitialAd(activity, TogetherAdBaidu.idMapBaidu[alias]) + mInterAd?.setListener(object : InterstitialAdListener { + override fun onAdFailed(errorMsg: String?) { + "onAdFailed".logd(tag) + callbackInterFailed(adProviderType, alias, listener, null, errorMsg) + } + + override fun onAdDismissed() { + "onAdDismissed".logd(tag) + callbackInterClosed(adProviderType, listener) + } + + override fun onAdPresent() { + "onAdPresent".logd(tag) + callbackInterExpose(adProviderType, listener) + } + + override fun onAdClick(inter: InterstitialAd?) { + "onAdClick".logd(tag) + callbackInterClicked(adProviderType, listener) + } + + override fun onAdReady() { + "onAdReady".logd(tag) + callbackInterLoaded(adProviderType, alias, listener) + } + }) + mInterAd?.loadAd() + } + + override fun showInterAd(activity: Activity) { + mInterAd?.showAd(activity) + } + + override fun destroyInterAd() { + mInterAd?.destroy() + mInterAd = null + } + + /** + * --------------------------- 原生自渲染 --------------------------- + */ + override fun getNativeAdList(activity: Activity, adProviderType: String, alias: String, maxCount: Int, listener: NativeListener) { + + callbackNativeStartRequest(adProviderType, alias, listener) + + val baidu = BaiduNative(activity, TogetherAdBaidu.idMapBaidu[alias], object : BaiduNative.BaiduNativeNetworkListener { + + override fun onNativeLoad(list: List) { + val subList = if (list.size > maxCount) { + list.subList(0, maxCount) + } else { + list + } + callbackNativeLoaded(adProviderType, alias, listener, subList) + } + + override fun onNativeFail(nativeErrorCode: NativeErrorCode) { + val errorMsg = when (nativeErrorCode) { + NativeErrorCode.LOAD_AD_FAILED -> { + "请求失败" + } + NativeErrorCode.CONFIG_ERROR -> { + "配置错误" + } + NativeErrorCode.INTERNAL_ERROR -> { + "内部错误" + } + NativeErrorCode.UNKNOWN -> { + "未知错误" + } + } + callbackNativeFailed(adProviderType, alias, listener, null, errorMsg) + } + }) + /* + * Step 2. 创建requestParameters对象,并将其传给baidu.makeRequest来请求广告 + */ + // 用户点击下载类广告时,是否弹出提示框让用户选择下载与否 + val requestParameters = RequestParameters.Builder().build() + + baidu.makeRequest(requestParameters) + } + + override fun nativeAdIsBelongTheProvider(adObject: Any): Boolean { + return adObject is NativeResponse + } + + override fun resumeNativeAd(adObject: Any) { + when (adObject) { + is NativeResponse -> { + + } + } + } + + override fun pauseNativeAd(adObject: Any) { + when (adObject) { + is NativeResponse -> { + + } + } + } + + override fun destroyNativeAd(adObject: Any) { + when (adObject) { + is NativeResponse -> { + + } + } + } + + override fun getNativeExpressAdList(activity: Activity, adProviderType: String, alias: String, adCount: Int, listener: NativeExpressListener) { + callbackNativeExpressStartRequest(adProviderType, alias, listener) + callbackNativeExpressFailed(adProviderType, alias, listener, null, "百度不支持原生模板类型广告") + } + + override fun destroyNativeExpressAd(adObject: Any) { + + } + + override fun nativeExpressAdIsBelongTheProvider(adObject: Any): Boolean { + return false + } + + override fun getNativeExpress2AdList(activity: Activity, adProviderType: String, alias: String, adCount: Int, listener: NativeExpress2Listener) { + callbackNativeExpressStartRequest(adProviderType, alias, listener) + callbackNativeExpressFailed(adProviderType, alias, listener, null, "百度不支持原生模板2类型广告") + } + + override fun destroyNativeExpress2Ad(adObject: Any) { + } + + override fun nativeExpress2AdIsBelongTheProvider(adObject: Any): Boolean { + return false + } + + /** + * --------------------------- 激励 --------------------------- + */ + private var mRewardVideoAd: RewardVideoAd? = null + override fun requestRewardAd(activity: Activity, adProviderType: String, alias: String, listener: RewardListener) { + + callbackRewardStartRequest(adProviderType, alias, listener) + + mRewardVideoAd = RewardVideoAd(activity, TogetherAdBaidu.idMapBaidu[alias], object : RewardVideoAd.RewardVideoAdListener { + override fun onAdFailed(errorMsg: String?) { + "onAdFailed".loge(tag) + callbackRewardFailed(adProviderType, alias, listener, null, errorMsg) + mRewardVideoAd = null + } + + override fun playCompletion() { + "playCompletion".logi(tag) + callbackRewardVideoComplete(adProviderType, listener) + callbackRewardVerify(adProviderType, listener) + } + + override fun onAdShow() { + "onAdShow".logi(tag) + callbackRewardShow(adProviderType, listener) + callbackRewardExpose(adProviderType, listener) + } + + override fun onAdClick() { + "onAdClick".logi(tag) + callbackRewardClicked(adProviderType, listener) + } + + override fun onAdClose(playScale: Float) { + "onAdClose".logi(tag) + callbackRewardClosed(adProviderType, listener) + mRewardVideoAd = null + } + + override fun onVideoDownloadSuccess() { + "onVideoDownloadSuccess".logi(tag) + callbackRewardLoaded(adProviderType, alias, listener) + callbackRewardVideoCached(adProviderType, listener) + } + + override fun onVideoDownloadFailed() { + "onVideoDownloadFailed".loge(tag) + callbackRewardFailed(adProviderType, alias, listener, null, "视频缓存失败") + } + }, false) + + mRewardVideoAd?.load() + } + + override fun showRewardAd(activity: Activity): Boolean { + if (mRewardVideoAd?.isReady == true) { + mRewardVideoAd?.show() + return true + } + return false + } + + override fun requestFullVideoAd(activity: Activity, adProviderType: String, alias: String, listener: FullVideoListener) { + callbackFullVideoStartRequest(adProviderType, alias, listener) + callbackFullVideoFailed(adProviderType, alias, listener, null, "百度不支持全屏视频广告") + } + + override fun showFullVideoAd(activity: Activity): Boolean { + return false + } +} \ No newline at end of file diff --git a/baidu/src/main/res/layout/layout_native_view_baidu.xml b/baidu/src/main/res/layout/layout_native_view_baidu.xml new file mode 100644 index 0000000..e17f04f --- /dev/null +++ b/baidu/src/main/res/layout/layout_native_view_baidu.xml @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + +