Skip to content

Commit

Permalink
[#4083] Remove onAdLoaded listener and use predefined fixed height (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaozhan authored Nov 24, 2024
1 parent 8b2e1df commit 266e0f6
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package com.oztechan.ccc.android.core.ad

import android.app.Activity
import android.content.Context
import android.os.Build
import android.view.WindowManager
import co.touchlab.kermit.Logger
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
Expand Down Expand Up @@ -83,34 +84,35 @@ internal class AdManagerImpl(context: Context) : AdManager {

override fun getBannerAd(
context: Context,
width: Int,
adId: String,
onAdLoaded: (Int?) -> Unit
maxHeight: Int
): BannerAdView {
Logger.v { "AdManagerImpl getBannerAd" }

val adView = AdView(context).apply {
val adWidthPixels = if (width == 0) {
context.resources.displayMetrics.widthPixels.toFloat()
} else {
width.toFloat()
}
val adWidthPixels = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.currentWindowMetrics.bounds.width()
} else {
context.resources.displayMetrics.widthPixels
}

setAdSize(
AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
context,
(adWidthPixels / resources.displayMetrics.density).toInt()
)
)
val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
context,
(adWidthPixels / context.resources.displayMetrics.density).toInt()
)

val finalAdSize = if (adSize.height > maxHeight) {
AdSize(adSize.width, maxHeight)
} else {
adSize
}

val adView = AdView(context).apply {
setAdSize(finalAdSize)
adUnitId = adId
adListener = object : AdListener() {
override fun onAdLoaded() {
super.onAdLoaded()
onAdLoaded(adSize?.height?.times(resources.displayMetrics.density)?.toInt())
}
}
loadAd(adRequest)
}

return BannerAdView(context, banner = adView) { adView.destroy() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.huawei.hms.ads.reward.Reward
import com.huawei.hms.ads.reward.RewardAd
import com.huawei.hms.ads.reward.RewardAdLoadListener
import com.huawei.hms.ads.reward.RewardAdStatusListener
import java.lang.Exception

internal class AdManagerImpl : AdManager {

Expand All @@ -38,21 +37,14 @@ internal class AdManagerImpl : AdManager {

override fun getBannerAd(
context: Context,
width: Int,
adId: String,
onAdLoaded: (Int?) -> Unit
maxHeight: Int
): BannerAdView {
Logger.v { "AdManagerImpl getBannerAd" }

val adView = BannerView(context).apply {
this.adId = adId
bannerAdSize = BannerAdSize.BANNER_SIZE_SMART
adListener = object : AdListener() {
override fun onAdLoaded() {
super.onAdImpression()
onAdLoaded(bannerAdSize.getHeightPx(context))
}
}
loadAd(adParam)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ interface AdManager {

fun getBannerAd(
context: Context,
width: Int,
adId: String,
onAdLoaded: (Int?) -> Unit
maxHeight: Int
): BannerAdView

fun showInterstitialAd(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import com.oztechan.ccc.android.core.ad.AdManager
import com.oztechan.ccc.android.ui.mobile.BuildConfig
import com.oztechan.ccc.android.ui.mobile.R
import com.oztechan.ccc.android.ui.mobile.databinding.FragmentCalculatorBinding
import com.oztechan.ccc.android.ui.mobile.util.buildBanner
import com.oztechan.ccc.android.ui.mobile.util.copyToClipBoard
import com.oztechan.ccc.android.ui.mobile.util.dataState
import com.oztechan.ccc.android.ui.mobile.util.destroyBanner
import com.oztechan.ccc.android.ui.mobile.util.getFromClipBoard
import com.oztechan.ccc.android.ui.mobile.util.setBackgroundByName
import com.oztechan.ccc.android.ui.mobile.util.setBannerAd
import com.oztechan.ccc.android.ui.mobile.util.showSnack
import com.oztechan.ccc.android.ui.mobile.util.visibleIf
import com.oztechan.ccc.client.core.analytics.AnalyticsManager
Expand Down Expand Up @@ -76,7 +76,7 @@ class CalculatorFragment : BaseVBFragment<FragmentCalculatorBinding>() {
}

private fun FragmentCalculatorBinding.initViews() {
adViewContainer.setBannerAd(
adViewContainer.buildBanner(
adManager = adManager,
adId = if (BuildConfig.DEBUG) {
getString(R.string.banner_ad_unit_id_calculator_debug)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import com.oztechan.ccc.android.core.ad.AdManager
import com.oztechan.ccc.android.ui.mobile.BuildConfig
import com.oztechan.ccc.android.ui.mobile.R
import com.oztechan.ccc.android.ui.mobile.databinding.FragmentCurrenciesBinding
import com.oztechan.ccc.android.ui.mobile.util.buildBanner
import com.oztechan.ccc.android.ui.mobile.util.destroyBanner
import com.oztechan.ccc.android.ui.mobile.util.hideKeyboard
import com.oztechan.ccc.android.ui.mobile.util.setBannerAd
import com.oztechan.ccc.android.ui.mobile.util.showSnack
import com.oztechan.ccc.android.ui.mobile.util.visibleIf
import com.oztechan.ccc.client.core.analytics.AnalyticsManager
Expand Down Expand Up @@ -62,7 +62,7 @@ class CurrenciesFragment : BaseVBFragment<FragmentCurrenciesBinding>() {
}

private fun FragmentCurrenciesBinding.initViews() {
adViewContainer.setBannerAd(
adViewContainer.buildBanner(
adManager = adManager,
adId = if (BuildConfig.DEBUG) {
getString(R.string.banner_ad_unit_id_currencies_debug)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import com.oztechan.ccc.android.ui.mobile.BuildConfig
import com.oztechan.ccc.android.ui.mobile.R
import com.oztechan.ccc.android.ui.mobile.content.main.ComposeMainActivity
import com.oztechan.ccc.android.ui.mobile.databinding.FragmentSettingsBinding
import com.oztechan.ccc.android.ui.mobile.util.buildBanner
import com.oztechan.ccc.android.ui.mobile.util.destroyBanner
import com.oztechan.ccc.android.ui.mobile.util.getThemeMode
import com.oztechan.ccc.android.ui.mobile.util.resolveAndStartIntent
import com.oztechan.ccc.android.ui.mobile.util.setBannerAd
import com.oztechan.ccc.android.ui.mobile.util.showDialog
import com.oztechan.ccc.android.ui.mobile.util.showSingleChoiceDialog
import com.oztechan.ccc.android.ui.mobile.util.showSnack
Expand Down Expand Up @@ -66,7 +66,7 @@ class SettingsFragment : BaseVBFragment<FragmentSettingsBinding>() {

@Suppress("LongMethod")
private fun FragmentSettingsBinding.initViews() {
adViewContainer.setBannerAd(
adViewContainer.buildBanner(
adManager = adManager,
adId = if (BuildConfig.DEBUG) {
getString(R.string.banner_ad_unit_id_settings_debug)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.view.View
import android.view.animation.Animation
import android.view.animation.Transformation
import android.view.inputmethod.InputMethodManager
import android.widget.FrameLayout
import android.widget.ImageView
Expand All @@ -26,50 +24,28 @@ import com.oztechan.ccc.android.ui.mobile.R
import com.oztechan.ccc.client.core.res.getImageIdByName
import com.oztechan.ccc.client.viewmodel.calculator.model.ConversionState

private const val ANIMATION_DURATION = 500L

fun View.hideKeyboard() = context?.getSystemService(Context.INPUT_METHOD_SERVICE)
?.castTo<InputMethodManager>()
?.hideSoftInputFromWindow(windowToken, 0)

fun FrameLayout.setBannerAd(
fun FrameLayout.buildBanner(
adManager: AdManager,
adId: String,
shouldShowAd: Boolean
) = if (shouldShowAd) {
removeAllViews()

addView(
adManager.getBannerAd(context, width, adId) { height ->
if (height != null) animateHeight(0, height)
isVisible = true
}
)
} else {
isGone = true
) {
if (shouldShowAd) {
val maxAdHeight =
(context.resources.getDimension(R.dimen.ads_banner_height) / resources.displayMetrics.density).toInt()
removeAllViews()
addView(adManager.getBannerAd(context, adId, maxAdHeight))
}
}

fun FrameLayout.destroyBanner() {
children.firstOrNull()?.castTo<BannerAdView>()?.onDestroy?.invoke()
removeAllViews()
}

fun View.animateHeight(startHeight: Int, endHeight: Int) {
layoutParams.height = 0

val delta = endHeight - startHeight
var newHeight: Int
val animation = object : Animation() {
override fun applyTransformation(interpolatedTime: Float, t: Transformation) {
newHeight = (startHeight + delta * interpolatedTime).toInt()
layoutParams.height = newHeight
requestLayout()
}
}
animation.duration = ANIMATION_DURATION
startAnimation(animation)
}

fun View?.visibleIf(visible: Boolean, bringFront: Boolean = false) = this?.apply {
if (visible) {
isVisible = true
Expand Down

0 comments on commit 266e0f6

Please sign in to comment.