Skip to content

Commit

Permalink
init ads in the splash view model
Browse files Browse the repository at this point in the history
  • Loading branch information
VolgoAK committed Aug 24, 2019
1 parent 2bb5380 commit 336a84f
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 129 deletions.
11 changes: 6 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'

//firebase
implementation "com.google.firebase:firebase-core:16.0.7"
implementation 'com.google.android.gms:play-services-ads:17.2.0'
implementation "com.google.firebase:firebase-core:17.1.0"
implementation 'com.google.android.gms:play-services-ads:18.1.1'
//implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-gcm:16.1.0'
implementation "com.google.firebase:firebase-messaging:17.4.0"
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation "com.google.firebase:firebase-messaging:20.0.0"
implementation 'com.google.android.ads.consent:consent-library:1.0.7'
implementation 'com.flurry.android:analytics:11.6.0@aar'

implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'

//Dependency injection
implementation "org.koin:koin-android:$koinVersion"
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/java/com/example/sergey/shlypa2/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.example.sergey.shlypa2
import androidx.multidex.MultiDexApplication
import cat.ereza.customactivityoncrash.config.CaocConfig
import com.crashlytics.android.Crashlytics
import com.example.sergey.shlypa2.ads.AdsManager
import com.example.sergey.shlypa2.di.appModule
import com.example.sergey.shlypa2.game.Game
import com.example.sergey.shlypa2.utils.TimberDebugTree
Expand All @@ -12,7 +11,6 @@ import com.flurry.android.FlurryAgent
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.messaging.FirebaseMessaging
import io.fabric.sdk.android.Fabric
import org.koin.android.ext.android.inject
import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin
import timber.log.Timber
Expand All @@ -22,7 +20,6 @@ import timber.log.Timber
*/
class App : MultiDexApplication() {

private val adsManager by inject<AdsManager>()

override fun onCreate() {
super.onCreate()
Expand All @@ -38,8 +35,6 @@ class App : MultiDexApplication() {
Timber.plant(TimberReleaseTree())
}

adsManager.initAds()

//todo refactor this shit !!!
val namesArray = resources.getStringArray(R.array.teams)
Game.teamNames = namesArray.toMutableList()
Expand Down
88 changes: 39 additions & 49 deletions app/src/main/java/com/example/sergey/shlypa2/ads/AdsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.example.sergey.shlypa2.ads

import android.content.Context
import android.os.Bundle
import com.example.sergey.shlypa2.BuildConfig
import com.example.sergey.shlypa2.extensions.debug
import com.google.ads.mediation.admob.AdMobAdapter
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.MobileAds
Expand All @@ -22,16 +22,19 @@ class AdsManager(
private const val BANNER_ID_KEY = "banner_id"
private const val TEST_DEVICE_KEY = "test_device_id"
private const val INTERSTITIAL_ID_KEY = "interstitial_id"
private const val NATIVE_ID_KEY = "native_ads_id"
private const val PUBLISHER_ID_KEY = "publisher_id"
private const val PRIVACY_LINK = "privacy_link"

private const val BANNER_TEST_ID = "ca-app-pub-3940256099942544/6300978111"
private const val INTERSTITIAL_TEST_ID = "ca-app-pub-3940256099942544/1033173712"
private const val NATIVE_TEST_ID = "ca-app-pub-3940256099942544/2247696110"
}

private var appId: String? = null
private var bannerId: String? = null
private var interstitialId: String? = null
private var nativeAdsId: String? = null
private var testDeviceId: String? = null
private var publisherId: String? = null
private var privacyLink: String? = null
Expand All @@ -41,20 +44,20 @@ class AdsManager(
fun initAds() {
try {
val jsonObject = loadIds(context) ?: return
Timber.d("TESTING json object $jsonObject")
appId = jsonObject.optString(APP_ID_KEY)
bannerId = jsonObject.optString(BANNER_ID_KEY)
interstitialId = jsonObject.optString(INTERSTITIAL_ID_KEY)
testDeviceId = jsonObject.optString(TEST_DEVICE_KEY)
publisherId = jsonObject.optString(PUBLISHER_ID_KEY)
privacyLink = jsonObject.optString(PRIVACY_LINK)

with(jsonObject) {
appId = optString(APP_ID_KEY)
bannerId = optString(BANNER_ID_KEY)
interstitialId = optString(INTERSTITIAL_ID_KEY)
nativeAdsId = optString(NATIVE_ID_KEY)
testDeviceId = optString(TEST_DEVICE_KEY)
publisherId = optString(PUBLISHER_ID_KEY)
privacyLink = optString(PRIVACY_LINK)
}

MobileAds.initialize(context, appId)

consentManager.publisherId = publisherId
consentManager.testDeviceId = testDeviceId
consentManager.privacyLink = privacyLink
consentManager.initConsent(context)
consentManager.initConsent(publisherId ?: "", privacyLink ?: "", testDeviceId)

initialized = true
} catch (ex: Exception) {
Expand All @@ -63,50 +66,37 @@ class AdsManager(
}
}

fun checkConsent(context: Context) {
fun showConsentIfRequired(context: Context) {
consentManager.showConsentIfNeed(context)
}

fun createBannerRequest(): AdRequest? {
if(consentManager.canShowAds().not()) {
return null
}
val adRequestBuilder = AdRequest.Builder()
if (BuildConfig.DEBUG) {
adRequestBuilder.addTestDevice(testDeviceId)
}

if(consentManager.nonPersonalizedOnly()) {
val extras = Bundle()
extras.putString("npa", "1")
adRequestBuilder.addNetworkExtrasBundle(AdMobAdapter::class.java, extras)
}

return adRequestBuilder.build()
}

fun getBannerId(): String? {
return bannerId
}

fun getInterstitial(context: Context): Interstitial? {
if(consentManager.canShowAds().not()){
return null
}

val builder = AdRequest.Builder()
if(BuildConfig.DEBUG) {
builder.addTestDevice(testDeviceId)
}
val request = buildRequest() ?: return null

if(consentManager.nonPersonalizedOnly()) {
val extras = Bundle()
extras.putString("npa", "1")
builder.addNetworkExtrasBundle(AdMobAdapter::class.java, extras)
return interstitialId?.let {
Interstitial(context, it, request)
}
}

return interstitialId?.let {
Interstitial(context, it, builder.build())
/**
* Creates ad request if showing ads is allowed by the consentManager
* otherwise returns null
*/
private fun buildRequest(): AdRequest? {
if (consentManager.canShowAds().not()) return null

with(AdRequest.Builder()) {
debug {
addTestDevice(testDeviceId)
}

if (consentManager.nonPersonalizedOnly()) {
val extras = Bundle()
extras.putString("npa", "1")
addNetworkExtrasBundle(AdMobAdapter::class.java, extras)
}

return build()
}
}

Expand Down
126 changes: 69 additions & 57 deletions app/src/main/java/com/example/sergey/shlypa2/ads/ConsentManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,114 @@ package com.example.sergey.shlypa2.ads

import android.content.Context
import android.preference.PreferenceManager
import com.example.sergey.shlypa2.extensions.debug
import com.google.ads.consent.*
import timber.log.Timber
import java.net.MalformedURLException
import java.net.URL

class ConsentManager {
class ConsentManager(val context: Context) {

companion object {
private const val SAVED_STATUS = "saved_status"
}

var testDeviceId: String? = null
var publisherId: String? = null
var privacyLink: String? = null
var currentConsentStatus = InnerConsentStatus.NOT_SET
private var testDeviceId: String? = null
private var publisherId: String? = null
private var privacyLink: String? = null

private var currentConsentStatus = InnerConsentStatus.NOT_SET
private val consentInfo = ConsentInformation.getInstance(context)
private var consentForm: ConsentForm? = null

fun initConsent(context: Context) {
// ConsentInformation.getInstance(context).debugGeography = DebugGeography.DEBUG_GEOGRAPHY_EEA

/*if (BuildConfig.DEBUG) {
ConsentInformation.getInstance(context)
.addTestDevice(testDeviceId)
fun initConsent(publisherId: String, privacyLink: String, testDeviceId: String?) {
this.testDeviceId = testDeviceId
this.publisherId = publisherId
this.privacyLink = privacyLink

debug {
with(consentInfo) {
debugGeography = DebugGeography.DEBUG_GEOGRAPHY_EEA
addTestDevice(testDeviceId)
}
}

loadStatusFromPrefs(context)
loadStatusFromPrefs()

if(currentConsentStatus == InnerConsentStatus.NOT_SET || currentConsentStatus == InnerConsentStatus.UNKNOWN) {
checkConsent(context)
}*/
if (currentConsentStatus == InnerConsentStatus.NOT_SET || currentConsentStatus == InnerConsentStatus.UNKNOWN) {
checkConsent()
}
}

private fun checkConsent(context: Context, doOnUnknown: () -> Unit = {}) {
/*val consentInformation = ConsentInformation.getInstance(context)
fun showConsentIfNeed(context: Context, buyCallBack: () -> Unit = {}) {
when (currentConsentStatus) {
InnerConsentStatus.UNKNOWN -> if (consentForm?.isShowing != true) showConsent(context)
InnerConsentStatus.NOT_SET -> checkConsent { showConsentIfNeed(context, buyCallBack) }
else -> return
}
}

fun canShowAds(): Boolean =
currentConsentStatus == InnerConsentStatus.NO_NEED ||
currentConsentStatus == InnerConsentStatus.PERSONALIZED ||
currentConsentStatus == InnerConsentStatus.NON_PERSONALIZED

if (!consentInformation.isRequestLocationInEeaOrUnknown) {
fun nonPersonalizedOnly(): Boolean = currentConsentStatus == InnerConsentStatus.NON_PERSONALIZED

/**
* Check is consent is required
*/
private fun checkConsent(doOnUnknown: () -> Unit = {}) {
if (!consentInfo.isRequestLocationInEeaOrUnknown) {
currentConsentStatus = InnerConsentStatus.NO_NEED
saveConsentStatus(context, currentConsentStatus)
saveConsentStatus(currentConsentStatus)
return
}

val publisherIds = arrayOf(publisherId)
consentInformation.requestConsentInfoUpdate(publisherIds, object : ConsentInfoUpdateListener {
override fun onConsentInfoUpdated(consentStatus: com.google.ads.consent.ConsentStatus) {
consentInfo.requestConsentInfoUpdate(publisherIds, object : ConsentInfoUpdateListener {
override fun onConsentInfoUpdated(consentStatus: ConsentStatus) {
currentConsentStatus = when (consentStatus) {
ConsentStatus.UNKNOWN -> InnerConsentStatus.UNKNOWN
ConsentStatus.PERSONALIZED -> InnerConsentStatus.PERSONALIZED
ConsentStatus.NON_PERSONALIZED -> InnerConsentStatus.NON_PERSONALIZED
}
saveConsentStatus(context, currentConsentStatus)

saveConsentStatus(currentConsentStatus)
if (currentConsentStatus == InnerConsentStatus.UNKNOWN) doOnUnknown()
}

override fun onFailedToUpdateConsentInfo(errorDescription: String) {
}
})*/
override fun onFailedToUpdateConsentInfo(errorDescription: String) {}
})
}

fun showConsentIfNeed(context: Context, buyCallBack: () -> Unit = {}) {
when (currentConsentStatus) {
InnerConsentStatus.UNKNOWN -> showConsent(context)
InnerConsentStatus.NOT_SET -> checkConsent(context) { showConsentIfNeed(context, buyCallBack) }
else -> return
}
}

private fun saveConsentStatus(context: Context, status: InnerConsentStatus) {
/*PreferenceManager.getDefaultSharedPreferences(context)
private fun saveConsentStatus(status: InnerConsentStatus) {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putString(SAVED_STATUS, status.toString())
.apply()*/
.apply()
}

private fun loadStatusFromPrefs(context: Context) {
private fun loadStatusFromPrefs() {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
val statusString = prefs.getString(SAVED_STATUS, InnerConsentStatus.NOT_SET.toString())
currentConsentStatus = InnerConsentStatus.valueOf(statusString!!)
}

/*fun isUserInEurope(context: Context) = ConsentInformation.getInstance(context)
.isRequestLocationInEeaOrUnknown*/

fun showConsent(context: Context, buyCallBack: () -> Unit = {}) {
/*var privacyUrl: URL? = null
/**
* Shows consent dialog and saves user choice to shared preferences
* if user choice ad free version then buyCallBack function will be called
*/
private fun showConsent(context: Context, buyCallBack: () -> Unit = {}) {
var privacyUrl: URL? = null
try {
privacyUrl = URL(privacyLink)
} catch (e: MalformedURLException) {
e.printStackTrace()
// Handle error.
Timber.e(e)
}

var consentForm: ConsentForm? = null
val builder = ConsentForm.Builder(context, privacyUrl)
.withListener(object : ConsentFormListener() {
override fun onConsentFormLoaded() {
Expand All @@ -101,15 +120,17 @@ class ConsentManager {

override fun onConsentFormClosed(
consentStatus: ConsentStatus?, userPrefersAdFree: Boolean?) {
if(userPrefersAdFree == true) {
consentForm = null

if (userPrefersAdFree == true) {
buyCallBack()
} else {
currentConsentStatus = if (consentStatus == ConsentStatus.PERSONALIZED) {
InnerConsentStatus.PERSONALIZED
} else {
InnerConsentStatus.NON_PERSONALIZED
}
saveConsentStatus(context, currentConsentStatus)
saveConsentStatus(currentConsentStatus)
}
}

Expand All @@ -120,24 +141,15 @@ class ConsentManager {
.withAdFreeOption()

consentForm = builder.build()
consentForm?.load()*/
consentForm?.load()
}

fun canShowAds(): Boolean = true /*{
return currentConsentStatus == InnerConsentStatus.NO_NEED ||
currentConsentStatus == InnerConsentStatus.PERSONALIZED ||
currentConsentStatus == InnerConsentStatus.NON_PERSONALIZED
}*/

fun nonPersonalizedOnly(): Boolean = false /*{
return currentConsentStatus == InnerConsentStatus.NON_PERSONALIZED
}*/

enum class InnerConsentStatus {
NO_NEED,
PERSONALIZED,
NON_PERSONALIZED,
UNKNOWN,
NOT_SET,
PREMIUM
}
}
Loading

0 comments on commit 336a84f

Please sign in to comment.