Skip to content

Commit

Permalink
Updated Ads logic (for trying to avoid resource not found errors)
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed Jun 28, 2020
1 parent 66c93f7 commit 6d20163
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions ads/src/main/AndroidManifest.xml
Expand Up @@ -11,6 +11,7 @@
<dist:fusing dist:include="true" />
</dist:module>

<uses-permission android:name="android.permission.INTERNET" />
<application>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
Expand Down
Expand Up @@ -33,6 +33,7 @@ import com.google.android.gms.ads.*
import com.google.android.gms.ads.formats.NativeAdOptions
import com.google.android.gms.ads.formats.UnifiedNativeAd
import com.google.android.gms.ads.formats.UnifiedNativeAdView
import com.google.android.play.core.splitcompat.SplitCompat
import com.javinator9889.handwashingreminder.gms.ads.AdLoader
import com.javinator9889.handwashingreminder.utils.isConnected
import com.javinator9889.handwashingreminder.utils.isDebuggable
Expand All @@ -46,19 +47,23 @@ val ADMOB_APP_NATIVE_ID =
if (isDebuggable()) "ca-app-pub-3940256099942544/2247696110"
else "ca-app-pub-5517327035817913/5656089851"

@SuppressLint("MissingPermission")
@Keep
class AdLoaderImpl private constructor(context: Context?) : AdLoader {
private val adOptions: NativeAdOptions
private var currentNativeAd: UnifiedNativeAd? = null
private var isVideoEnded = true
private val moduleContext: Context

init {
if (context == null)
throw NullPointerException(
"Context cannot be null when creating " +
"the first instance"
)
MobileAds.initialize(context, ADMOB_APP_ID)
moduleContext = context
SplitCompat.install(moduleContext)
MobileAds.initialize(moduleContext)
val videoOptions = VideoOptions.Builder()
.setStartMuted(true)
.build()
Expand All @@ -72,20 +77,22 @@ class AdLoaderImpl private constructor(context: Context?) : AdLoader {
private var instance = WeakReference<AdLoader?>(null)

override fun instance(context: Context?): AdLoader {
val appInstance = instance.get() ?: AdLoaderImpl(context)
if (instance.get() == null)
instance = WeakReference(appInstance)
return appInstance
instance.get()?.let { return it }
synchronized(this) {
val instance = AdLoaderImpl(context?.applicationContext)
this.instance = WeakReference(instance)
return instance
}
}
}

@SuppressLint("InflateParams")
override fun loadAdForViewGroup(view: ViewGroup, removeAllViews: Boolean) {
if (!isVideoEnded || !isConnected())
return
val adLoader = AdBase.Builder(view.context, ADMOB_APP_NATIVE_ID)
val adLoader = AdBase.Builder(moduleContext, ADMOB_APP_NATIVE_ID)
.forUnifiedNativeAd { ad: UnifiedNativeAd ->
val adView = LayoutInflater.from(view.context)
val adView = LayoutInflater.from(moduleContext)
.inflate(R.layout.native_ad_view, null) as CardView
populateUnifiedNativeAdView(ad, adView)
if (removeAllViews)
Expand Down
Expand Up @@ -19,6 +19,7 @@
package com.javinator9889.handwashingreminder.activities

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.animation.Animation
Expand Down Expand Up @@ -149,7 +150,13 @@ class LauncherActivity : AppCompatActivity() {
if (Ads.MODULE_NAME in splitInstallManager.installedModules &&
sharedPreferences.getBoolean(ADS_ENABLED, true)) {
when (resultCode) {
Activity.RESULT_OK -> initAds()
Activity.RESULT_OK -> {
createPackageContext(packageName, 0).also {
SplitCompat.install(it)
}.also {
initAds(it)
}
}
Activity.RESULT_CANCELED -> app.adLoader = null
}
}
Expand Down Expand Up @@ -230,7 +237,7 @@ class LauncherActivity : AppCompatActivity() {
splitInstallManager.installedModules
}

private fun initAds() {
private fun initAds(context: Context) {
val className = "${Ads.PACKAGE_NAME}.${Ads
.CLASS_NAME}\$${Ads.PROVIDER_NAME}"
val adProvider = Class.forName(className).kotlin
Expand Down

0 comments on commit 6d20163

Please sign in to comment.