diff --git a/android/core/ad/src/google/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt b/android/core/ad/src/google/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt index df67a783e8..fd94ff1ff9 100644 --- a/android/core/ad/src/google/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt +++ b/android/core/ad/src/google/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt @@ -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 @@ -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() } } diff --git a/android/core/ad/src/huawei/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt b/android/core/ad/src/huawei/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt index 51f85bc32b..d5d7a35225 100644 --- a/android/core/ad/src/huawei/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt +++ b/android/core/ad/src/huawei/kotlin/com/oztechan/ccc/android/core/ad/AdManagerImpl.kt @@ -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 { @@ -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) } diff --git a/android/core/ad/src/main/kotlin/com/oztechan/ccc/android/core/ad/AdManager.kt b/android/core/ad/src/main/kotlin/com/oztechan/ccc/android/core/ad/AdManager.kt index 074490f01c..12fc1ad670 100644 --- a/android/core/ad/src/main/kotlin/com/oztechan/ccc/android/core/ad/AdManager.kt +++ b/android/core/ad/src/main/kotlin/com/oztechan/ccc/android/core/ad/AdManager.kt @@ -13,9 +13,8 @@ interface AdManager { fun getBannerAd( context: Context, - width: Int, adId: String, - onAdLoaded: (Int?) -> Unit + maxHeight: Int ): BannerAdView fun showInterstitialAd( diff --git a/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/calculator/CalculatorFragment.kt b/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/calculator/CalculatorFragment.kt index 5d0b1fa8b5..d779a0527d 100755 --- a/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/calculator/CalculatorFragment.kt +++ b/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/calculator/CalculatorFragment.kt @@ -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 @@ -76,7 +76,7 @@ class CalculatorFragment : BaseVBFragment() { } private fun FragmentCalculatorBinding.initViews() { - adViewContainer.setBannerAd( + adViewContainer.buildBanner( adManager = adManager, adId = if (BuildConfig.DEBUG) { getString(R.string.banner_ad_unit_id_calculator_debug) diff --git a/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/currencies/CurrenciesFragment.kt b/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/currencies/CurrenciesFragment.kt index 43470dd5c1..4add409ed4 100755 --- a/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/currencies/CurrenciesFragment.kt +++ b/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/currencies/CurrenciesFragment.kt @@ -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 @@ -62,7 +62,7 @@ class CurrenciesFragment : BaseVBFragment() { } private fun FragmentCurrenciesBinding.initViews() { - adViewContainer.setBannerAd( + adViewContainer.buildBanner( adManager = adManager, adId = if (BuildConfig.DEBUG) { getString(R.string.banner_ad_unit_id_currencies_debug) diff --git a/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/settings/SettingsFragment.kt b/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/settings/SettingsFragment.kt index 4c59d85078..d7c97f279f 100644 --- a/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/settings/SettingsFragment.kt +++ b/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/settings/SettingsFragment.kt @@ -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 @@ -66,7 +66,7 @@ class SettingsFragment : BaseVBFragment() { @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) diff --git a/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/util/ViewExtensions.kt b/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/util/ViewExtensions.kt index 077759904a..9deafcbe70 100644 --- a/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/util/ViewExtensions.kt +++ b/android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/util/ViewExtensions.kt @@ -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 @@ -26,27 +24,21 @@ 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() ?.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() { @@ -54,22 +46,6 @@ fun FrameLayout.destroyBanner() { 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