diff --git a/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/pageobjects/HomeScreen.kt b/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/pageobjects/HomeScreen.kt index b51b6e3..f8e18b3 100644 --- a/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/pageobjects/HomeScreen.kt +++ b/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/pageobjects/HomeScreen.kt @@ -29,10 +29,11 @@ internal class HomeScreen( NavigationItem.Rewarded::class.java -> 3 NavigationItem.NativeTemplate::class.java -> 4 NavigationItem.CustomNative::class.java -> 5 - NavigationItem.InstreamBinder::class.java -> 7 - NavigationItem.InstreamInRoll::class.java -> 8 - NavigationItem.Policies::class.java -> 9 - NavigationItem.AppOpenAd::class.java -> 10 + NavigationItem.AdfoxSlider::class.java -> 6 + NavigationItem.InstreamBinder::class.java -> 8 + NavigationItem.InstreamInRoll::class.java -> 9 + NavigationItem.Policies::class.java -> 10 + NavigationItem.AppOpenAd::class.java -> 11 else -> error("unsupported type") } } @@ -43,6 +44,7 @@ internal class HomeScreen( { itemType { NavigationItem.NativeTemplate(it) } itemType { NavigationItem.CustomNative(it) } + itemType { NavigationItem.AdfoxSlider(it) } itemType { NavigationItem.Policies(it) } itemType { NavigationItem.AppOpenAd(it) } itemType { NavigationItem.Rewarded(it) } @@ -85,6 +87,8 @@ internal class HomeScreen( class CustomNative(matcher: Matcher) : NavigationItem(matcher) + class AdfoxSlider(matcher: Matcher) : NavigationItem(matcher) + class Policies(matcher: Matcher) : NavigationItem(matcher) class AppOpenAd(matcher: Matcher) : NavigationItem(matcher) diff --git a/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/shared_steps/GoToSection.kt b/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/shared_steps/GoToSection.kt index a49f700..c90c6a1 100644 --- a/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/shared_steps/GoToSection.kt +++ b/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/shared_steps/GoToSection.kt @@ -22,6 +22,7 @@ internal class GoToSection( enum class NavigationItem { NATIVE_TEMPLATE, CUSTOM_NATIVE, + ADFOX_SLIDER, POLICIES, APP_OPEN_AD, REWARDED, @@ -39,6 +40,7 @@ internal class GoToSection( get() = when (this) { NavigationItem.NATIVE_TEMPLATE -> R.string.native_template_title NavigationItem.CUSTOM_NATIVE -> R.string.native_custom_title + NavigationItem.ADFOX_SLIDER -> R.string.adfox_slider_title NavigationItem.POLICIES -> R.string.policies NavigationItem.APP_OPEN_AD -> R.string.appopenad_title NavigationItem.REWARDED -> R.string.rewarded_title @@ -53,6 +55,7 @@ internal class GoToSection( get() = when (this) { NavigationItem.NATIVE_TEMPLATE -> HomeScreen.NavigationItem.NativeTemplate::class.java NavigationItem.CUSTOM_NATIVE -> HomeScreen.NavigationItem.CustomNative::class.java + NavigationItem.ADFOX_SLIDER -> HomeScreen.NavigationItem.AdfoxSlider::class.java NavigationItem.POLICIES -> HomeScreen.NavigationItem.Policies::class.java NavigationItem.APP_OPEN_AD -> HomeScreen.NavigationItem.AppOpenAd::class.java NavigationItem.REWARDED -> HomeScreen.NavigationItem.Rewarded::class.java diff --git a/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/InterstitialLoadTest.kt b/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/InterstitialLoadTest.kt index 1477171..44c4383 100644 --- a/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/InterstitialLoadTest.kt +++ b/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/InterstitialLoadTest.kt @@ -43,7 +43,9 @@ internal class InterstitialLoadTest : BaseUITest() { step("Нажать на кнопку \"Show ad\"") { onScreen { - clickShowAd() + flakySafely(10_000) { + clickShowAd() + } } step("Успешно отобразилась реклама") { diff --git a/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/RewardedLoadTest.kt b/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/RewardedLoadTest.kt index 61037b7..f8d2595 100644 --- a/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/RewardedLoadTest.kt +++ b/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/RewardedLoadTest.kt @@ -45,7 +45,9 @@ internal class RewardedLoadTest : BaseUITest() { step("Нажать на кнопку \"Show ad\"") { onScreen { - clickShowAd() + flakySafely(10_000) { + clickShowAd() + } } step("Успешно отобразилась реклама") { diff --git a/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/mediation/InterstitialLoadTest.kt b/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/mediation/InterstitialLoadTest.kt index c85a730..911bc78 100644 --- a/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/mediation/InterstitialLoadTest.kt +++ b/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/mediation/InterstitialLoadTest.kt @@ -49,7 +49,9 @@ internal class InterstitialLoadTest( step("Нажать на кнопку \"Show ad\"") { onScreen { - clickShowAd() + flakySafely(10_000) { + clickShowAd() + } } step("Реклама загрузилась. В случае подбора рекламы отобразилась на полный экран.") { diff --git a/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/mediation/RewardedLoadTest.kt b/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/mediation/RewardedLoadTest.kt index 15f5e8c..ff27214 100644 --- a/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/mediation/RewardedLoadTest.kt +++ b/YandexMobileAdsExample/app/src/androidTest/java/com/yandex/ads/sample/tests/mediation/RewardedLoadTest.kt @@ -49,7 +49,9 @@ internal class RewardedLoadTest( step("Нажать на кнопку \"Show ad\"") { onScreen { - clickShowAd() + flakySafely(10_000) { + clickShowAd() + } } step("Реклама загрузилась. В случае подбора рекламы отобразилась на полный экран.") { @@ -68,7 +70,6 @@ internal class RewardedLoadTest( } companion object { - private const val NO_ADS_AVAILABLE_MESSAGE = "Ad request completed successfully, but there are no ads available." diff --git a/YandexMobileAdsExample/app/src/main/AndroidManifest.xml b/YandexMobileAdsExample/app/src/main/AndroidManifest.xml index 0c9d20f..8b00dc4 100644 --- a/YandexMobileAdsExample/app/src/main/AndroidManifest.xml +++ b/YandexMobileAdsExample/app/src/main/AndroidManifest.xml @@ -29,6 +29,9 @@ + diff --git a/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/HomeActivity.kt b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/HomeActivity.kt index 48ef135..a325131 100644 --- a/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/HomeActivity.kt +++ b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/HomeActivity.kt @@ -15,6 +15,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.preference.PreferenceManager import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager +import com.yandex.ads.sample.adunits.AdfoxSliderAdActivity import com.yandex.ads.sample.adunits.AppOpenAdActivity import com.yandex.ads.sample.adunits.CustomNativeAdActivity import com.yandex.ads.sample.adunits.FeedActivity @@ -97,6 +98,11 @@ class HomeActivity : AppCompatActivity(R.layout.activity_home) { R.string.native_custom_title, ActivityNavigation(CustomNativeAdActivity::class.java), ), + NavigationItem( + R.drawable.ic_outline_carousel_24, + R.string.adfox_slider_title, + ActivityNavigation(AdfoxSliderAdActivity::class.java), + ), NavigationItem( R.drawable.ic_outline_movie_24, R.string.instream_simple_title, diff --git a/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxslider/AdfoxSliderAdapter.kt b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxslider/AdfoxSliderAdapter.kt new file mode 100644 index 0000000..db101a2 --- /dev/null +++ b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxslider/AdfoxSliderAdapter.kt @@ -0,0 +1,58 @@ +/* + * This file is a part of the Yandex Advertising Network + * + * Version for Android (C) 2022 YANDEX + * + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at https://legal.yandex.com/partner_ch/ + */ + +package com.yandex.ads.sample.adfoxslider + +import android.util.Log +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import com.yandex.ads.sample.databinding.AdfoxSliderItemBinding +import com.yandex.mobile.ads.nativeads.NativeAd +import com.yandex.mobile.ads.nativeads.NativeAdException +import com.yandex.mobile.ads.nativeads.NativeAdViewBinder + +class AdfoxSliderAdapter : RecyclerView.Adapter() { + + private var nativeAds: List = listOf() + + fun setData(nativeAds: List) { + this.nativeAds = nativeAds + notifyDataSetChanged() + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NativeAdViewBinderHolder { + val binding = AdfoxSliderItemBinding.inflate( + LayoutInflater.from(parent.context), parent, false + ) + return NativeAdViewBinderHolder(binding) + } + + override fun onBindViewHolder(holder: NativeAdViewBinderHolder, position: Int) { + val nativeAd = nativeAds[position] + bindNativeAd(nativeAd, holder.nativeAdViewBinder) + } + + override fun getItemCount(): Int { + return nativeAds.size + } + + private fun bindNativeAd(nativeAd: NativeAd, viewBinder: NativeAdViewBinder) { + try { + nativeAd.bindNativeAd(viewBinder) + } catch (exception: NativeAdException) { + Log.d(VIEW_PAGER_TAG, "${exception.message}") + } + } + + companion object { + + private const val VIEW_PAGER_TAG = "ViewPagerAdapter" + } +} diff --git a/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxslider/NativeAdViewBinderHolder.kt b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxslider/NativeAdViewBinderHolder.kt new file mode 100644 index 0000000..6c6eebe --- /dev/null +++ b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxslider/NativeAdViewBinderHolder.kt @@ -0,0 +1,40 @@ +/* + * This file is a part of the Yandex Advertising Network + * + * Version for Android (C) 2022 YANDEX + * + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at https://legal.yandex.com/partner_ch/ + */ + +package com.yandex.ads.sample.adfoxslider + +import androidx.recyclerview.widget.RecyclerView +import com.yandex.ads.sample.databinding.AdfoxSliderItemBinding +import com.yandex.mobile.ads.nativeads.NativeAdViewBinder + +class NativeAdViewBinderHolder(binding: AdfoxSliderItemBinding) : RecyclerView.ViewHolder(binding.root) { + + val nativeAdViewBinder: NativeAdViewBinder = createNativeAdViewBinder(binding) + + private fun createNativeAdViewBinder(binding: AdfoxSliderItemBinding): NativeAdViewBinder { + return binding.nativeAd.run { + NativeAdViewBinder.Builder(root) + .setAgeView(age) + .setBodyView(body) + .setCallToActionView(callToAction) + .setDomainView(domain) + .setFaviconView(favicon) + .setFeedbackView(feedback) + .setIconView(icon) + .setMediaView(media) + .setPriceView(price) + .setRatingView(rating) + .setReviewCountView(reviewCount) + .setSponsoredView(sponsored) + .setTitleView(title) + .setWarningView(warning) + .build() + } + } +} diff --git a/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxslider/SliderAutoscroller.kt b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxslider/SliderAutoscroller.kt new file mode 100644 index 0000000..aa4e430 --- /dev/null +++ b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxslider/SliderAutoscroller.kt @@ -0,0 +1,64 @@ +/* + * This file is a part of the Yandex Advertising Network + * + * Version for Android (C) 2022 YANDEX + * + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at https://legal.yandex.com/partner_ch/ + */ + +package com.yandex.ads.sample.adfoxslider + +import androidx.viewpager2.widget.ViewPager2 +import kotlinx.coroutines.Job +import kotlinx.coroutines.MainScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch + +class SliderAutoscroller(private val viewPager2: ViewPager2) { + + private val itemCount get() = viewPager2.adapter!!.itemCount + private val currentItem get() = viewPager2.currentItem + + private val scope = MainScope() + private var job: Job? = null + private var lastScroll = 0L + + fun startScrolling() { + stopScrolling() + updateLastScroll() + job = scope.launch { + while (true) { + delay(CHECK_INTERVAL_MILLIS) + if (System.currentTimeMillis() - lastScroll > SCROLL_INTERVAL_MILLIS) { + scroll() + } + } + } + } + + fun updateLastScroll() { + lastScroll = System.currentTimeMillis() + } + + fun stopScrolling() { + job?.cancel() + job = null + } + + private fun scroll() { + openNextPage() + updateLastScroll() + } + + private fun openNextPage() { + val nextItem = (currentItem + 1) % itemCount + viewPager2.setCurrentItem(nextItem, true) + } + + companion object { + + private const val SCROLL_INTERVAL_MILLIS = 5000L + private const val CHECK_INTERVAL_MILLIS = 1000L + } +} diff --git a/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxslider/SliderIndicatorController.kt b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxslider/SliderIndicatorController.kt new file mode 100644 index 0000000..c8dc872 --- /dev/null +++ b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adfoxslider/SliderIndicatorController.kt @@ -0,0 +1,68 @@ +/* + * This file is a part of the Yandex Advertising Network + * + * Version for Android (C) 2022 YANDEX + * + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at https://legal.yandex.com/partner_ch/ + */ + +package com.yandex.ads.sample.adfoxslider + +import android.widget.ImageView +import android.widget.LinearLayout +import androidx.annotation.DimenRes +import androidx.core.content.ContextCompat +import androidx.core.view.isVisible +import androidx.core.view.setMargins +import com.yandex.ads.sample.R +import kotlin.math.absoluteValue + +class SliderIndicatorController(private val indicatorLayout: LinearLayout) { + private val dotsImageViews = arrayListOf() + + fun setupIndicator(pagesCount: Int, selectedPage: Int) { + indicatorLayout.removeAllViews() + dotsImageViews.clear() + + for (i in 0 until pagesCount) { + val dotView = ImageView(indicatorLayout.context).apply { + setImageDrawable(ContextCompat.getDrawable(indicatorLayout.context, R.drawable.shape_adfox_slider_indicator_dot)) + } + dotsImageViews.add(dotView) + indicatorLayout.addView(dotsImageViews[i]) + } + changeSelectedPage(selectedPage) + } + + fun changeSelectedPage(selectedPage: Int) { + dotsImageViews.forEachIndexed { page, view -> + val distance = (selectedPage - page).absoluteValue + val size = when { + dotsImageViews.size <= 4 || distance < 2 -> R.dimen.adfox_slider_indicator_dot_size_large + distance == 2 -> R.dimen.adfox_slider_indicator_dot_size_medium + else -> R.dimen.adfox_slider_indicator_dot_size_small + } + view.layoutParams = getLayoutParams(getResource(size)) + view.isVisible = dotsImageViews.size <= 4 || distance <= 3 + view.alpha = if (page == selectedPage) DOT_ALPHA_SELECTED + else DOT_ALPHA_NOT_SELECTED + } + } + + private fun getLayoutParams(dotSize: Int): LinearLayout.LayoutParams { + val layoutParams = LinearLayout.LayoutParams(dotSize, dotSize) + layoutParams.setMargins(getResource(R.dimen.adfox_slider_indicator_dot_margin)) + return layoutParams + } + + private fun getResource(@DimenRes id: Int): Int { + return indicatorLayout.resources.getDimension(id).toInt() + } + + companion object { + + private const val DOT_ALPHA_SELECTED = 1f + private const val DOT_ALPHA_NOT_SELECTED = 0.6f + } +} diff --git a/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adunits/AdfoxSliderAdActivity.kt b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adunits/AdfoxSliderAdActivity.kt new file mode 100644 index 0000000..dab3a97 --- /dev/null +++ b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adunits/AdfoxSliderAdActivity.kt @@ -0,0 +1,202 @@ +/* + * This file is a part of the Yandex Advertising Network + * + * Version for Android (C) 2022 YANDEX + * + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at https://legal.yandex.com/partner_ch/ + */ + +package com.yandex.ads.sample.adunits + +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import androidx.core.view.isVisible +import androidx.fragment.app.commit +import androidx.recyclerview.widget.RecyclerView +import androidx.viewpager2.widget.ViewPager2 +import com.yandex.ads.sample.R +import com.yandex.ads.sample.adfoxslider.AdfoxSliderAdapter +import com.yandex.ads.sample.adfoxslider.SliderAutoscroller +import com.yandex.ads.sample.adfoxslider.SliderIndicatorController +import com.yandex.ads.sample.databinding.ActivityAdfoxSliderAdBinding +import com.yandex.ads.sample.network.Network +import com.yandex.mobile.ads.common.AdRequestError +import com.yandex.mobile.ads.common.ImpressionData +import com.yandex.mobile.ads.nativeads.NativeAd +import com.yandex.mobile.ads.nativeads.NativeAdEventListener +import com.yandex.mobile.ads.nativeads.NativeAdRequestConfiguration +import com.yandex.mobile.ads.nativeads.NativeAdViewBinder +import com.yandex.mobile.ads.nativeads.SliderAd +import com.yandex.mobile.ads.nativeads.SliderAdLoadListener +import com.yandex.mobile.ads.nativeads.SliderAdLoader + +class AdfoxSliderAdActivity : AppCompatActivity(R.layout.activity_adfox_slider_ad) { + + private val sliderAdView get() = binding.sliderNativeAdView + private val sliderAdViewPager2 get() = binding.sliderAdViewPager2 + private val sliderAdapter get() = sliderAdViewPager2.adapter as AdfoxSliderAdapter + private val leftButton get() = binding.sliderAdLeftButton + private val rightButton get() = binding.sliderAdRightButton + private val indicatorLayout get() = binding.sliderAdIndicatorLayout + private val adInfoFragment get() = requireNotNull(_adInfoFragment) + private val eventLogger = AdfoxSliderEventLogger() + + private var sliderAutoscroller: SliderAutoscroller? = null + private var sliderIndicatorController: SliderIndicatorController? = null + private var sliderAdLoader: SliderAdLoader? = null + private var _adInfoFragment: AdInfoFragment? = null + + private lateinit var binding: ActivityAdfoxSliderAdBinding + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + binding = ActivityAdfoxSliderAdBinding.inflate(layoutInflater) + binding.toolbar.setNavigationOnClickListener { onBackPressedDispatcher.onBackPressed() } + setContentView(binding.root) + + _adInfoFragment = AdInfoFragment.newInstance(networks) + adInfoFragment.onLoadClickListener = ::loadAds + supportFragmentManager.commit { + setReorderingAllowed(true) + replace(R.id.ad_info, adInfoFragment) + } + sliderAutoscroller = SliderAutoscroller(sliderAdViewPager2) + sliderIndicatorController = SliderIndicatorController(indicatorLayout) + + configureAdLoader() + configureViewPager() + } + + override fun onDestroy() { + sliderAutoscroller?.stopScrolling() + sliderAutoscroller = null + sliderIndicatorController = null + sliderAdLoader = null + _adInfoFragment = null + super.onDestroy() + } + + private fun loadAds() { + sliderAdLoader?.loadSlider(buildAdRequestConfiguration()) + } + + private fun configureAdLoader() { + sliderAdLoader = SliderAdLoader(this) + sliderAdLoader?.setSliderAdLoadListener(eventLogger) + } + + private fun configureViewPager() { + sliderAdViewPager2.adapter = AdfoxSliderAdapter() + sliderAdViewPager2.registerOnPageChangeCallback(ViewPagerCallback()) + configureViewPagerPadding() + configureViewPagerControls() + } + + private fun buildAdRequestConfiguration(): NativeAdRequestConfiguration { + val adUnitId = adInfoFragment.selectedNetwork.adUnitId + return NativeAdRequestConfiguration + .Builder(adUnitId) + .setShouldLoadImagesAutomatically(true) + .setParameters(adFoxParameters) + .build() + } + + private fun bindSliderAd(sliderAd: SliderAd) { + val sliderViewBinder = NativeAdViewBinder.Builder(sliderAdView).build() + sliderAd.bindSliderAd(sliderViewBinder) + bindNativeAds(sliderAd.nativeAds) + } + + private fun bindNativeAds(nativeAds: List) { + nativeAds.forEach { it.setNativeAdEventListener(eventLogger) } + sliderAdapter.setData(nativeAds) + sliderIndicatorController?.setupIndicator(nativeAds.size, sliderAdViewPager2.currentItem) + sliderAutoscroller?.startScrolling() + setViewPagerControlsVisibility() + } + + private fun configureViewPagerPadding() { + val padding = resources.getDimension(R.dimen.adfox_slider_ad_padding).toInt() + val recyclerView = sliderAdViewPager2.getChildAt(0) as RecyclerView + recyclerView.apply { + setPadding(padding, 0, padding, 0) + clipToPadding = false + } + } + + private fun configureViewPagerControls() { + leftButton.setOnClickListener { + val previousItem = (sliderAdViewPager2.currentItem + sliderAdapter.itemCount - 1) % sliderAdapter.itemCount + sliderAdViewPager2.setCurrentItem(previousItem, true) + } + rightButton.setOnClickListener { + val nextItem = (sliderAdViewPager2.currentItem + 1) % sliderAdapter.itemCount + sliderAdViewPager2.setCurrentItem(nextItem, true) + } + } + + private fun setViewPagerControlsVisibility() { + val shouldShow = sliderAdapter.itemCount > 1 + leftButton.isVisible = shouldShow + rightButton.isVisible = shouldShow + indicatorLayout.isVisible = shouldShow + } + + private inner class AdfoxSliderEventLogger : SliderAdLoadListener, NativeAdEventListener { + + override fun onSliderAdLoaded(sliderAd: SliderAd) { + adInfoFragment.log("Ads loaded") + adInfoFragment.hideLoading() + bindSliderAd(sliderAd) + } + + override fun onSliderAdFailedToLoad(error: AdRequestError) { + adInfoFragment.log( + "Ads failed to load with code ${error.code}: ${error.description}" + ) + adInfoFragment.hideLoading() + } + + override fun onAdClicked() { + adInfoFragment.log("Ad clicked") + } + + override fun onLeftApplication() { + adInfoFragment.log("Left application") + } + + override fun onReturnedToApplication() { + adInfoFragment.log("Returned to application") + } + + override fun onImpression(data: ImpressionData?) { + adInfoFragment.log("Impression: ${data?.rawData}") + } + } + + private inner class ViewPagerCallback : ViewPager2.OnPageChangeCallback() { + + override fun onPageSelected(position: Int) { + super.onPageSelected(position) + setViewPagerControlsVisibility() + sliderIndicatorController?.changeSelectedPage(position) + sliderAutoscroller?.updateLastScroll() + } + } + + companion object { + + private const val AD_FOX_AD_UNIT_ID = "R-M-243655-8" + + private val networks = arrayListOf( + Network(R.drawable.ic_adfox_icon, R.string.adfox_title, AD_FOX_AD_UNIT_ID), + ) + + private val adFoxParameters = mapOf( + "adf_ownerid" to "270901", + "adf_p1" to "ddflb", + "adf_p2" to "fksh" + ) + } +} diff --git a/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adunits/NativeTemplateAdActivity.kt b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adunits/NativeTemplateAdActivity.kt index c50dba3..d6e3e06 100644 --- a/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adunits/NativeTemplateAdActivity.kt +++ b/YandexMobileAdsExample/app/src/main/java/com/yandex/ads/sample/adunits/NativeTemplateAdActivity.kt @@ -9,8 +9,9 @@ package com.yandex.ads.sample.adunits -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import androidx.core.view.isVisible import androidx.fragment.app.commit import com.yandex.ads.sample.R import com.yandex.ads.sample.databinding.ActivityNativeTemplateAdBinding @@ -60,6 +61,7 @@ class NativeTemplateAdActivity : AppCompatActivity(R.layout.activity_native_temp private fun bindNative(nativeAd: NativeAd) { nativeAd.setNativeAdEventListener(eventLogger) + binding.nativeBanner.isVisible = true binding.nativeBanner.setAd(nativeAd) } diff --git a/YandexMobileAdsExample/app/src/main/res/drawable/ic_adfox_slider_arrow_left.xml b/YandexMobileAdsExample/app/src/main/res/drawable/ic_adfox_slider_arrow_left.xml new file mode 100644 index 0000000..f299915 --- /dev/null +++ b/YandexMobileAdsExample/app/src/main/res/drawable/ic_adfox_slider_arrow_left.xml @@ -0,0 +1,18 @@ + + + + diff --git a/YandexMobileAdsExample/app/src/main/res/drawable/ic_adfox_slider_arrow_right.xml b/YandexMobileAdsExample/app/src/main/res/drawable/ic_adfox_slider_arrow_right.xml new file mode 100644 index 0000000..b8cc0a5 --- /dev/null +++ b/YandexMobileAdsExample/app/src/main/res/drawable/ic_adfox_slider_arrow_right.xml @@ -0,0 +1,18 @@ + + + + diff --git a/YandexMobileAdsExample/app/src/main/res/drawable/ic_outline_carousel_24.xml b/YandexMobileAdsExample/app/src/main/res/drawable/ic_outline_carousel_24.xml new file mode 100644 index 0000000..2ab5ab6 --- /dev/null +++ b/YandexMobileAdsExample/app/src/main/res/drawable/ic_outline_carousel_24.xml @@ -0,0 +1,9 @@ + + + diff --git a/YandexMobileAdsExample/app/src/main/res/drawable/shape_adfox_slider_indicator_background.xml b/YandexMobileAdsExample/app/src/main/res/drawable/shape_adfox_slider_indicator_background.xml new file mode 100644 index 0000000..a8f9954 --- /dev/null +++ b/YandexMobileAdsExample/app/src/main/res/drawable/shape_adfox_slider_indicator_background.xml @@ -0,0 +1,16 @@ + + + + + + + + diff --git a/YandexMobileAdsExample/app/src/main/res/drawable/shape_adfox_slider_indicator_dot.xml b/YandexMobileAdsExample/app/src/main/res/drawable/shape_adfox_slider_indicator_dot.xml new file mode 100644 index 0000000..a379084 --- /dev/null +++ b/YandexMobileAdsExample/app/src/main/res/drawable/shape_adfox_slider_indicator_dot.xml @@ -0,0 +1,15 @@ + + + + + + + diff --git a/YandexMobileAdsExample/app/src/main/res/layout/activity_adfox_slider_ad.xml b/YandexMobileAdsExample/app/src/main/res/layout/activity_adfox_slider_ad.xml new file mode 100644 index 0000000..a6c141a --- /dev/null +++ b/YandexMobileAdsExample/app/src/main/res/layout/activity_adfox_slider_ad.xml @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/YandexMobileAdsExample/app/src/main/res/layout/activity_custom_native_ad.xml b/YandexMobileAdsExample/app/src/main/res/layout/activity_custom_native_ad.xml index bcfc7ef..28e652d 100644 --- a/YandexMobileAdsExample/app/src/main/res/layout/activity_custom_native_ad.xml +++ b/YandexMobileAdsExample/app/src/main/res/layout/activity_custom_native_ad.xml @@ -40,15 +40,18 @@ app:layout_constraintBottom_toTopOf="@id/native_ad" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintVertical_weight="1" tools:layout="@layout/fragment_ad_info" /> + android:layout_height="0dp" + android:visibility="gone" + app:layout_constraintVertical_weight="1" + app:layout_constraintBottom_toTopOf="@+id/additional_container" + app:layout_constraintTop_toBottomOf="@id/ad_info" /> diff --git a/YandexMobileAdsExample/app/src/main/res/layout/adfox_slider_item.xml b/YandexMobileAdsExample/app/src/main/res/layout/adfox_slider_item.xml new file mode 100644 index 0000000..c77caca --- /dev/null +++ b/YandexMobileAdsExample/app/src/main/res/layout/adfox_slider_item.xml @@ -0,0 +1,21 @@ + + + + + + + + diff --git a/YandexMobileAdsExample/app/src/main/res/layout/fragment_ad_info.xml b/YandexMobileAdsExample/app/src/main/res/layout/fragment_ad_info.xml index ac16ff0..783b7fa 100644 --- a/YandexMobileAdsExample/app/src/main/res/layout/fragment_ad_info.xml +++ b/YandexMobileAdsExample/app/src/main/res/layout/fragment_ad_info.xml @@ -78,6 +78,7 @@ diff --git a/YandexMobileAdsExample/app/src/main/res/layout/native_ad_view.xml b/YandexMobileAdsExample/app/src/main/res/layout/native_ad_view.xml index b04ded0..edb533d 100644 --- a/YandexMobileAdsExample/app/src/main/res/layout/native_ad_view.xml +++ b/YandexMobileAdsExample/app/src/main/res/layout/native_ad_view.xml @@ -27,7 +27,7 @@ android:id="@+id/components" android:layout_width="wrap_content" android:layout_height="wrap_content" - app:constraint_referenced_ids="age,feedback,feedback,icon,title" /> + app:constraint_referenced_ids="age,sponsored,feedback,title" /> 8dp 24dp + + 6dp + 4dp + 2dp + 2dp + 48dp + 32dp diff --git a/YandexMobileAdsExample/app/src/main/res/values/strings.xml b/YandexMobileAdsExample/app/src/main/res/values/strings.xml index bb562de..41f37ee 100644 --- a/YandexMobileAdsExample/app/src/main/res/values/strings.xml +++ b/YandexMobileAdsExample/app/src/main/res/values/strings.xml @@ -21,6 +21,7 @@ AppOpenAd Native template Custom native + Adfox slider Simple instream Instream: Pre / Mid / Post-Roll Instream: In-Roll @@ -62,6 +63,10 @@ Install Free + + Previous ad + Next ad + Policies This is sample of GDPR dialog