Prototype ad implementation

This commit is contained in:
Phillip Thelen 2022-04-05 14:50:34 +02:00
parent 80bd8577e9
commit 4cb2c7212f
5 changed files with 135 additions and 0 deletions

View file

@ -30,6 +30,9 @@
<meta-data
android:name="firebase_performance_logcat_enabled"
android:value="true" />
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<activity
android:name=".ui.activities.MainActivity"
android:label="@string/app_name"

View file

@ -121,6 +121,7 @@ dependencies {
implementation 'com.google.firebase:firebase-messaging-ktx'
implementation 'com.google.firebase:firebase-config-ktx'
implementation 'com.google.firebase:firebase-perf-ktx'
implementation 'com.google.android.gms:play-services-ads:20.6.0'
implementation 'com.google.android.gms:play-services-auth:20.1.0'
implementation 'com.nex3z:flow-layout:1.2.2'

View file

@ -1225,4 +1225,5 @@
<string name="compact">Compact</string>
<string name="minimal">Minimal</string>
<string name="watch_ad">Watch Ad</string>
</resources>

View file

@ -0,0 +1,119 @@
package com.habitrpg.android.habitica.helpers
import android.app.Activity
import android.content.Context
import android.util.Log
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.OnUserEarnedRewardListener
import com.google.android.gms.ads.rewarded.RewardItem
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
class AdHandler(var activity: Activity, var rewardAction: (Boolean) -> Unit): OnUserEarnedRewardListener {
private var rewardedAd: RewardedAd? = null
companion object {
private enum class AdStatus {
UNINITIALIZED,
INITIALIZING,
READY,
DISABLED
}
const val TAG = "AdHandler"
const val adUnitID = "ca-app-pub-3940256099942544/5224354917"
private var currentAdStatus = AdStatus.UNINITIALIZED
fun initialize(context: Context, onComplete: () -> Unit) {
if (currentAdStatus != AdStatus.UNINITIALIZED) return
currentAdStatus = AdStatus.INITIALIZING
MobileAds.initialize(context) {
currentAdStatus = AdStatus.READY
onComplete()
}
}
fun whenAdsInitialized(context: Context, onComplete: () -> Unit) {
when (currentAdStatus) {
AdStatus.READY -> {
onComplete()
}
AdStatus.DISABLED -> {
return
}
AdStatus.UNINITIALIZED -> {
initialize(context) {
onComplete()
}
}
AdStatus.INITIALIZING -> {
return
}
}
}
}
fun prepare() {
whenAdsInitialized(activity) {
val adRequest = AdRequest.Builder().build()
RewardedAd.load(activity, adUnitID, adRequest, object : RewardedAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
rewardAction(false)
}
override fun onAdLoaded(rewardedAd: RewardedAd) {
this@AdHandler.rewardedAd = rewardedAd
configureReward()
}
})
}
}
fun show() {
when (currentAdStatus) {
AdStatus.READY -> {
showRewardedAd()
}
AdStatus.DISABLED -> {
rewardAction(false)
return
}
AdStatus.UNINITIALIZED -> {
initialize(activity) {
showRewardedAd()
}
}
AdStatus.INITIALIZING -> {
return
}
}
}
private fun configureReward() {
rewardedAd?.run {
fullScreenContentCallback = object : FullScreenContentCallback() {
}
}
}
private fun showRewardedAd() {
if (rewardedAd != null) {
rewardedAd?.show(activity, this)
} else {
Log.d(TAG, "The rewarded ad wasn't ready yet.")
}
}
override fun onUserEarnedReward(rewardItem: RewardItem) {
val rewardAmount = rewardItem.amount
val rewardType = rewardItem.type
Log.d(TAG, "User earned the reward. ${rewardAmount}, ${rewardType}")
rewardAction(true)
}
}

View file

@ -9,6 +9,7 @@ import android.content.pm.PackageManager
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.MenuItem
import android.view.View
@ -35,6 +36,7 @@ import com.habitrpg.android.habitica.extensions.hideKeyboard
import com.habitrpg.android.habitica.extensions.isUsingNightModeResources
import com.habitrpg.android.habitica.extensions.subscribeWithErrorHandler
import com.habitrpg.android.habitica.extensions.updateStatusBarColor
import com.habitrpg.android.habitica.helpers.AdHandler
import com.habitrpg.android.habitica.helpers.AmplitudeManager
import com.habitrpg.android.habitica.helpers.AppConfigManager
import com.habitrpg.android.habitica.helpers.MainNavigationController
@ -445,6 +447,11 @@ open class MainActivity : BaseActivity(), SnackbarActivity {
}
if (this.faintDialog == null && !this.isFinishing) {
val handler = AdHandler(this) {
Log.d("AdHandler", "Reviving user")
compositeSubscription.add(userRepository.updateUser("stats.hp", 50).subscribe({}, RxErrorHandler.handleEmptyError()))
}
handler.prepare()
val binding = DialogFaintBinding.inflate(this.layoutInflater)
binding.hpBar.setLightBackground(true)
binding.hpBar.setIcon(HabiticaIconsHelper.imageOfHeartLightBg())
@ -457,6 +464,10 @@ open class MainActivity : BaseActivity(), SnackbarActivity {
faintDialog = null
userRepository.revive().subscribe({ }, RxErrorHandler.handleEmptyError())
}
faintDialog?.addButton(R.string.watch_ad, true) { _, _ ->
faintDialog = null
handler.show()
}
soundManager.loadAndPlayAudio(SoundManager.SoundDeath)
this.faintDialog?.enqueue()
}