Skip to content

Commit

Permalink
Dependency updates
Browse files Browse the repository at this point in the history
Ad fixes
  • Loading branch information
dkrivoruchko committed Jul 26, 2021
1 parent 88dbe9f commit 17d7519
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 22 deletions.
13 changes: 8 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ android {
flavorDimensions("default")
productFlavors {
create("firebase") {
buildConfigField "String", "AD_UNIT_ID", localProps.getProperty("ad.unitId", "ca-app-pub-3940256099942544/6300978111")
manifestPlaceholders = ["adMobPubId": "${localProps.getProperty("ad.pubId")}"]
buildConfigField "String", "AD_UNIT_ID", localProps.getProperty("ad.unitId")
}
create("firebasefree") {
manifestPlaceholders = ["adMobPubId": ""]
// buildConfigField "String", "AD_UNIT_ID", "ca-app-pub-3940256099942544/6300978111"
firebaseCrashlytics.mappingFileUploadEnabled = false
}
}
Expand Down Expand Up @@ -90,14 +93,14 @@ dependencies {

implementation("androidx.core:core-ktx:1.6.0")
implementation("androidx.activity:activity-ktx:1.2.4")
implementation("androidx.fragment:fragment-ktx:1.3.6")
implementation("androidx.fragment:fragment-ktx:1.4.0-alpha05")
implementation("androidx.appcompat:appcompat:1.3.1")
implementation("androidx.constraintlayout:constraintlayout:2.0.4")
implementation("androidx.recyclerview:recyclerview:1.2.1")
implementation("com.google.android.material:material:1.4.0")

implementation("androidx.navigation:navigation-fragment-ktx:2.3.5")
implementation("androidx.navigation:navigation-ui-ktx:2.3.5")
implementation("androidx.navigation:navigation-fragment-ktx:2.4.0-alpha05")
implementation("androidx.navigation:navigation-ui-ktx:2.4.0-alpha05")
implementation("androidx.lifecycle:lifecycle-livedata:2.3.1")
implementation("androidx.lifecycle:lifecycle-common-java8:2.3.1")

Expand All @@ -122,7 +125,7 @@ dependencies {
}
firebaseImplementation("com.google.firebase:firebase-analytics:19.0.0")
firebaseImplementation("com.google.firebase:firebase-crashlytics:18.2.0")
firebaseImplementation("com.google.android.gms:play-services-ads-lite:20.2.0")
firebaseImplementation("com.google.android.gms:play-services-ads:20.2.0")

debugImplementation("com.squareup.leakcanary:leakcanary-android:2.7")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,58 @@ import android.view.ViewTreeObserver
import android.widget.FrameLayout
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.MobileAds
import info.dvkr.screenstream.BaseApp
import info.dvkr.screenstream.BuildConfig
import kotlinx.coroutines.delay

abstract class AdFragment(@LayoutRes contentLayoutId: Int) : Fragment(contentLayoutId) {

private companion object {
private const val TEST_AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111"
}

private var adView: AdView? = null
private lateinit var adSize: AdSize

fun loadAd(adViewContainer: FrameLayout) {
MobileAds.initialize(requireContext()) {}
adView = AdView(requireContext())
adViewContainer.addView(adView)
adViewContainer.viewTreeObserver.addOnGlobalLayoutListener(
fun loadAdOnViewCreated(adViewContainer: FrameLayout) {
if (::adSize.isInitialized) loadAd(adViewContainer)
else adViewContainer.viewTreeObserver.addOnGlobalLayoutListener(
object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val outMetrics =
DisplayMetrics().also { requireActivity().windowManager.defaultDisplay.getMetrics(it) }
var adWidthPixels = adViewContainer.width.toFloat()
if (adWidthPixels == 0f) adWidthPixels = outMetrics.widthPixels.toFloat()
val adWidth = (adWidthPixels / outMetrics.density).toInt()
val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(requireContext(), adWidth)

adView!!.adUnitId =
if (BuildConfig.DEBUG) "ca-app-pub-3940256099942544/6300978111" else BuildConfig.AD_UNIT_ID
adView!!.adSize = adSize
adViewContainer.minimumHeight = adSize.getHeightInPixels(requireContext())
adView!!.loadAd(AdRequest.Builder().build())
adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(requireContext(), adWidth)

adViewContainer.viewTreeObserver.removeOnGlobalLayoutListener(this)
loadAd(adViewContainer)
}
}
)
}

private fun loadAd(adViewContainer: FrameLayout) {
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
delay((requireActivity().application as BaseApp).lastAdLoadTime + 61_000 - System.currentTimeMillis())
MobileAds.initialize(requireActivity()) {}
adView = AdView(requireActivity()).also { adView ->
adViewContainer.addView(adView)
adViewContainer.minimumHeight = adSize.getHeightInPixels(requireContext())
adView.adUnitId = if (BuildConfig.DEBUG) TEST_AD_UNIT_ID else BuildConfig.AD_UNIT_ID;
adView.adSize = adSize
adView.loadAd(AdRequest.Builder().build())
(requireActivity().application as BaseApp).lastAdLoadTime = System.currentTimeMillis()
}
}
}

override fun onPause() {
adView?.pause()
super.onPause()
Expand All @@ -53,6 +69,7 @@ abstract class AdFragment(@LayoutRes contentLayoutId: Int) : Fragment(contentLay

override fun onDestroyView() {
adView?.destroy()
adView = null
super.onDestroyView()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import androidx.fragment.app.Fragment

abstract class AdFragment(@LayoutRes contentLayoutId: Int) : Fragment(contentLayoutId) {

fun loadAd(adViewContainer: FrameLayout) {
fun loadAdOnViewCreated(adViewContainer: FrameLayout) {
adViewContainer.visibility = View.GONE
}
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3406399667931208~2862159727"/>
android:value="${adMobPubId}" />

<activity
android:name=".ui.activity.AppActivity"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/kotlin/info/dvkr/screenstream/BaseApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ abstract class BaseApp : Application() {
.build()
}

var lastAdLoadTime: Long = 0

abstract fun initLogger()

override fun onCreate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DiffUtil
Expand Down Expand Up @@ -54,7 +53,7 @@ class StreamFragment : AdFragment(R.layout.fragment_stream) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

loadAd(binding.flFragmentStreamAdViewContainer)
loadAdOnViewCreated(binding.flFragmentStreamAdViewContainer)

binding.tvFragmentStreamTrafficHeader.text = getString(R.string.stream_fragment_current_traffic).run {
format(0.0).setColorSpan(colorAccent, indexOf('%'))
Expand Down Expand Up @@ -133,7 +132,8 @@ class StreamFragment : AdFragment(R.layout.fragment_stream) {
else
getString(R.string.stream_fragment_pin, settingsReadOnly.pin)

binding.tvFragmentStreamPin.text = pinText.setColorSpan(colorAccent, pinText.length - settingsReadOnly.pin.length)
binding.tvFragmentStreamPin.text =
pinText.setColorSpan(colorAccent, pinText.length - settingsReadOnly.pin.length)
} else {
binding.tvFragmentStreamPin.setText(R.string.stream_fragment_pin_disabled)
}
Expand Down

0 comments on commit 17d7519

Please sign in to comment.