Skip to content

Commit

Permalink
AdsManager and ConsentManager are simple classes now
Browse files Browse the repository at this point in the history
  • Loading branch information
VolgoAK committed Aug 24, 2019
1 parent 28c2875 commit 2bb5380
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 56 deletions.
10 changes: 5 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 @@ -4,18 +4,15 @@ 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.db.Contract
import com.example.sergey.shlypa2.di.appModule
import com.example.sergey.shlypa2.game.Game
import com.example.sergey.shlypa2.utils.DbExporter
import com.example.sergey.shlypa2.utils.PreferenceHelper
import com.example.sergey.shlypa2.utils.PreferenceHelper.set
import com.example.sergey.shlypa2.utils.TimberDebugTree
import com.example.sergey.shlypa2.utils.TimberReleaseTree
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 @@ -25,6 +22,8 @@ import timber.log.Timber
*/
class App : MultiDexApplication() {

private val adsManager by inject<AdsManager>()

override fun onCreate() {
super.onCreate()

Expand All @@ -39,7 +38,8 @@ class App : MultiDexApplication() {
Timber.plant(TimberReleaseTree())
}

AdsManager.initAds(this)
adsManager.initAds()

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

import android.content.Context
import com.crashlytics.android.Crashlytics
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.MobileAds
import org.json.JSONObject
import java.io.BufferedReader
import java.io.InputStreamReader
import android.os.Bundle
import com.example.sergey.shlypa2.BuildConfig
import com.google.ads.mediation.admob.AdMobAdapter
import com.google.gson.JsonObject
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.MobileAds
import org.json.JSONObject
import timber.log.Timber


/**
* Created by alex on 5/8/18.
*/
object AdsManager {

private const val APP_ID_KEY = "app_id"
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 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"
class AdsManager(
private val context: Context,
private val consentManager: ConsentManager) {

companion object {
private const val APP_ID_KEY = "app_id"
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 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 var appId: String? = null
private var bannerId: String? = null
Expand All @@ -38,9 +38,10 @@ object AdsManager {

var initialized = false

fun initAds(context: Context) {
fun initAds() {
try {
val jsonObject = loadIds(context)
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)
Expand All @@ -50,10 +51,10 @@ object AdsManager {

MobileAds.initialize(context, appId)

ConsentManager.publisherId = publisherId
ConsentManager.testDeviceId = testDeviceId
ConsentManager.privacyLink = privacyLink
ConsentManager.initConsent(context)
consentManager.publisherId = publisherId
consentManager.testDeviceId = testDeviceId
consentManager.privacyLink = privacyLink
consentManager.initConsent(context)

initialized = true
} catch (ex: Exception) {
Expand All @@ -63,19 +64,19 @@ object AdsManager {
}

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

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

if(ConsentManager.nonPersonalizedOnly()) {
if(consentManager.nonPersonalizedOnly()) {
val extras = Bundle()
extras.putString("npa", "1")
adRequestBuilder.addNetworkExtrasBundle(AdMobAdapter::class.java, extras)
Expand All @@ -89,7 +90,7 @@ object AdsManager {
}

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

Expand All @@ -98,31 +99,21 @@ object AdsManager {
builder.addTestDevice(testDeviceId)
}

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

interstitialId?.let {
return Interstitial(context, it, builder.build())
} ?: kotlin.run {
return null
return interstitialId?.let {
Interstitial(context, it, builder.build())
}
}

private fun loadIds(context: Context): JSONObject {
val inputStream = context.assets.open("ads.json")

val builder = StringBuilder()
BufferedReader(InputStreamReader(inputStream)).use {
var line = it.readLine()
while (line != null) {
builder.append(line)
line = it.readLine()
}
private fun loadIds(context: Context): JSONObject? {
context.assets.open("ads.json").use {
return JSONObject(String(it.readBytes()))
}
return JSONObject(builder.toString())
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package com.example.sergey.shlypa2.ads
import android.content.Context
import android.preference.PreferenceManager

import java.net.MalformedURLException
import java.net.URL
class ConsentManager {

object ConsentManager {

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

var testDeviceId: String? = null
var publisherId: String? = null
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/example/sergey/shlypa2/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.example.sergey.shlypa2.di

import android.content.Context
import androidx.room.Room
import com.example.sergey.shlypa2.ads.AdsManager
import com.example.sergey.shlypa2.ads.ConsentManager
import com.example.sergey.shlypa2.data.PlayersRepository
import com.example.sergey.shlypa2.db.Contract
import com.example.sergey.shlypa2.db.DataBase
Expand Down Expand Up @@ -52,6 +54,8 @@ val appModule = module {
single { PlayersRepository(get())}
single { PreferencesProvider(get(), null)}
single { SoundManager(get(), get())}
single { ConsentManager()}
single { AdsManager(get(), get())}
factory { DbExporter(get()) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import com.example.sergey.shlypa2.extensions.observeSafe
import com.example.sergey.shlypa2.extensions.setThemeApi21
import com.example.sergey.shlypa2.screens.game_result.GameResultActivity
import com.google.android.gms.ads.AdListener
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel

typealias Command = RoundViewModel.Command

class RoundActivity : AppCompatActivity() {

private val viewModel: RoundViewModel by viewModel()
private val adsManager: AdsManager by inject()
private var interstitial: Interstitial? = null

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -31,8 +33,8 @@ class RoundActivity : AppCompatActivity() {

supportActionBar?.elevation = 0F

if (AdsManager.initialized) {
interstitial = AdsManager.getInterstitial(this)
if (adsManager.initialized) {
interstitial = adsManager.getInterstitial(this)
interstitial?.loadAd()
interstitial?.setAdListener(object : AdListener() {
override fun onAdClosed() {
Expand Down

0 comments on commit 2bb5380

Please sign in to comment.