Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Oztechan/CCC#4083] Remove onAdLoaded listener and use predefined fixed height #4084

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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 @@
}

private fun FragmentCalculatorBinding.initViews() {
adViewContainer.setBannerAd(
adViewContainer.buildBanner(

Check warning on line 79 in android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/calculator/CalculatorFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/calculator/CalculatorFragment.kt#L79

Added line #L79 was not covered by tests
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.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 @@
}

private fun FragmentCurrenciesBinding.initViews() {
adViewContainer.setBannerAd(
adViewContainer.buildBanner(

Check warning on line 65 in android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/currencies/CurrenciesFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/currencies/CurrenciesFragment.kt#L65

Added line #L65 was not covered by tests
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.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 @@

@Suppress("LongMethod")
private fun FragmentSettingsBinding.initViews() {
adViewContainer.setBannerAd(
adViewContainer.buildBanner(

Check warning on line 69 in android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/settings/SettingsFragment.kt

View check run for this annotation

Codecov / codecov/patch

android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/content/settings/SettingsFragment.kt#L69

Added line #L69 was not covered by tests
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.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.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))

Check warning on line 40 in android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/util/ViewExtensions.kt

View check run for this annotation

Codecov / codecov/patch

android/ui/mobile/src/main/kotlin/com/oztechan/ccc/android/ui/mobile/util/ViewExtensions.kt#L37-L40

Added lines #L37 - L40 were not covered by tests
}
}

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