Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance AdRequestOptions Extension Functions #161

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/src/main/kotlin/dev/teogor/ceres/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import dev.teogor.ceres.ads.ApplicationOpenAd
import dev.teogor.ceres.framework.core.Application
import dev.teogor.ceres.framework.core.model.ThemeBuilder
import dev.teogor.ceres.monetisation.admob.AdMob
import dev.teogor.ceres.monetisation.ads.extensions.setForChildrenApp
import dev.teogor.ceres.monetisation.ads.extensions.setForEveryoneApp
import dev.teogor.ceres.monetisation.ads.extensions.setForFamilies
import dev.teogor.ceres.monetisation.ads.model.AdContentRating
import dev.teogor.ceres.monetisation.ads.model.TagForChildDirectedTreatment
import dev.teogor.ceres.monetisation.ads.model.TagForUnderAgeOfConsent
import dev.teogor.ceres.theme.configureTheme
import javax.inject.Inject

Expand All @@ -45,6 +51,16 @@ class Application : Application() {
override fun onCreate() {
super.onCreate()

AdMob.configureAdRequest {
maxAdContentRating = AdContentRating.UNSPECIFIED
tagForChildDirectedTreatment = TagForChildDirectedTreatment.UNSPECIFIED
tagForUnderAgeOfConsent = TagForUnderAgeOfConsent.UNSPECIFIED

setForFamilies()
setForChildrenApp()
setForEveryoneApp()
}

AdMob.setApplicationOpenAd(applicationOpenAd)
}
}
8 changes: 7 additions & 1 deletion monetisation/admob/api/admob.api
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
public final class dev/teogor/ceres/monetisation/admob/AdMob {
public static final field $stable I
public static final field INSTANCE Ldev/teogor/ceres/monetisation/admob/AdMob;
public final fun configureAdRequest (Lkotlin/jvm/functions/Function1;)V
public final fun getAppOpenAd ()Ldev/teogor/ceres/monetisation/admob/formats/AppOpenAd;
public final fun setApplicationOpenAd (Ldev/teogor/ceres/monetisation/admob/formats/AppOpenAd;)V
}
Expand All @@ -9,10 +10,15 @@ public final class dev/teogor/ceres/monetisation/admob/AdMobInitializer {
public static final field $stable I
public static final field INSTANCE Ldev/teogor/ceres/monetisation/admob/AdMobInitializer;
public final fun configureAdsControl (Ldev/teogor/ceres/monetisation/ads/AdsControl;)V
public final fun getHashedAdvertisingId (Landroid/content/Context;)Ljava/lang/String;
public final fun initialize (Landroid/content/Context;)V
}

public final class dev/teogor/ceres/monetisation/admob/AdMobInitializerKt {
public static final fun getHashedAdvertisingId (Landroid/content/Context;)Ljava/lang/String;
public static final fun getRequestConfiguration (Ldev/teogor/ceres/monetisation/ads/model/AdRequestOptions;Lkotlin/jvm/functions/Function1;)Lcom/google/android/gms/ads/RequestConfiguration;
public static synthetic fun getRequestConfiguration$default (Ldev/teogor/ceres/monetisation/ads/model/AdRequestOptions;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lcom/google/android/gms/ads/RequestConfiguration;
}

public final class dev/teogor/ceres/monetisation/admob/AdmobManagerInitializer : androidx/startup/Initializer {
public static final field $stable I
public fun <init> ()V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package dev.teogor.ceres.monetisation.admob

import com.google.android.gms.ads.MobileAds
import dev.teogor.ceres.monetisation.admob.formats.AppOpenAd
import dev.teogor.ceres.monetisation.ads.ExperimentalAdsControlApi
import dev.teogor.ceres.monetisation.ads.model.AdRequestOptions
import java.lang.ref.WeakReference

object AdMob {
Expand All @@ -29,4 +32,12 @@ object AdMob {
}

fun getAppOpenAd() = weakRefAppOpenAd.get()

@OptIn(ExperimentalAdsControlApi::class)
fun configureAdRequest(
block: AdRequestOptions.() -> Unit,
) {
val adRequestOptions = AdMobInitializer.adsControl.adRequestOptions.value.apply(block)
MobileAds.setRequestConfiguration(getRequestConfiguration(adRequestOptions))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import dev.teogor.ceres.core.runtime.AppMetadataManager
import dev.teogor.ceres.monetisation.ads.AdsControl
import dev.teogor.ceres.monetisation.ads.AdsControlProvider
import dev.teogor.ceres.monetisation.ads.ExperimentalAdsControlApi
import dev.teogor.ceres.monetisation.ads.model.AdRequestOptions
import java.io.UnsupportedEncodingException
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
Expand All @@ -36,22 +37,11 @@ object AdMobInitializer {

private var connectivityManager: ConnectivityManagerNetworkMonitor? = null

@SuppressLint("HardwareIds")
fun getHashedAdvertisingId(
context: Context,
): String {
val androidId: String = Settings.Secure.getString(
context.contentResolver,
Settings.Secure.ANDROID_ID,
)
return md5(androidId)?.uppercase() ?: ""
}

fun configureAdsControl(adsControl: AdsControl) {
AdsControlProvider.initialize(adsControl)
}

private val adsControl: AdsControl
internal val adsControl: AdsControl
get() = AdsControlProvider.adsControl

fun initialize(context: Context) {
Expand All @@ -65,19 +55,51 @@ object AdMobInitializer {
MobileAds.setRequestConfiguration(configuration)
}
}
}

inline fun getRequestConfiguration(
adRequestOptions: AdRequestOptions,
crossinline block: RequestConfiguration.Builder.() -> Unit = {},
): RequestConfiguration {
return MobileAds.getRequestConfiguration()
.toBuilder()
.apply {
setTagForChildDirectedTreatment(
adRequestOptions.tagForChildDirectedTreatment.intValue,
)
setMaxAdContentRating(
adRequestOptions.maxAdContentRating.stringValue,
)
setTagForUnderAgeOfConsent(
adRequestOptions.tagForUnderAgeOfConsent.intValue,
)
block()
}
.build()
}

@SuppressLint("HardwareIds")
fun getHashedAdvertisingId(
context: Context,
): String {
val androidId: String = Settings.Secure.getString(
context.contentResolver,
Settings.Secure.ANDROID_ID,
)
return md5(androidId)?.uppercase() ?: ""
}

private fun md5(md5: String): String? {
try {
val md = MessageDigest.getInstance("MD5")
val array = md.digest(md5.toByteArray(charset("UTF-8")))
val sb = StringBuffer()
for (i in array.indices) {
sb.append(Integer.toHexString(array[i].toInt() and 0xFF or 0x100).substring(1, 3))
}
return sb.toString()
} catch (_: NoSuchAlgorithmException) {
} catch (_: UnsupportedEncodingException) {
private fun md5(md5: String): String? {
try {
val md = MessageDigest.getInstance("MD5")
val array = md.digest(md5.toByteArray(charset("UTF-8")))
val sb = StringBuffer()
for (i in array.indices) {
sb.append(Integer.toHexString(array[i].toInt() and 0xFF or 0x100).substring(1, 3))
}
return null
return sb.toString()
} catch (_: NoSuchAlgorithmException) {
} catch (_: UnsupportedEncodingException) {
}
return null
}
75 changes: 70 additions & 5 deletions monetisation/ads/api/ads.api
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public abstract interface class dev/teogor/ceres/monetisation/ads/AdsControl {
public abstract fun getAdRequestOptions ()Landroidx/compose/runtime/MutableState;
public abstract fun getCanRequestAds ()Landroidx/compose/runtime/MutableState;
public abstract fun getConsentRequirementStatus ()Landroidx/compose/runtime/MutableState;
public abstract fun getConsentStatus ()Landroidx/compose/runtime/MutableState;
Expand Down Expand Up @@ -26,16 +27,18 @@ public final class dev/teogor/ceres/monetisation/ads/AdsControlProvider {
public final class dev/teogor/ceres/monetisation/ads/AndroidAdsControl : dev/teogor/ceres/monetisation/ads/AdsControl {
public static final field $stable I
public fun <init> ()V
public fun <init> (Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;)V
public synthetic fun <init> (Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;)V
public synthetic fun <init> (Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Landroidx/compose/runtime/MutableState;
public final fun component2 ()Landroidx/compose/runtime/MutableState;
public final fun component3 ()Landroidx/compose/runtime/MutableState;
public final fun component4 ()Lkotlin/jvm/functions/Function0;
public final fun component4 ()Landroidx/compose/runtime/MutableState;
public final fun component5 ()Lkotlin/jvm/functions/Function0;
public final fun copy (Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;)Ldev/teogor/ceres/monetisation/ads/AndroidAdsControl;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/ads/AndroidAdsControl;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Ldev/teogor/ceres/monetisation/ads/AndroidAdsControl;
public final fun component6 ()Lkotlin/jvm/functions/Function0;
public final fun copy (Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;)Ldev/teogor/ceres/monetisation/ads/AndroidAdsControl;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/ads/AndroidAdsControl;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Landroidx/compose/runtime/MutableState;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Ldev/teogor/ceres/monetisation/ads/AndroidAdsControl;
public fun equals (Ljava/lang/Object;)Z
public fun getAdRequestOptions ()Landroidx/compose/runtime/MutableState;
public fun getCanRequestAds ()Landroidx/compose/runtime/MutableState;
public fun getConsentRequirementStatus ()Landroidx/compose/runtime/MutableState;
public fun getConsentStatus ()Landroidx/compose/runtime/MutableState;
Expand All @@ -50,6 +53,48 @@ public final class dev/teogor/ceres/monetisation/ads/AndroidAdsControl : dev/teo
public abstract interface annotation class dev/teogor/ceres/monetisation/ads/ExperimentalAdsControlApi : java/lang/annotation/Annotation {
}

public final class dev/teogor/ceres/monetisation/ads/extensions/AdRequestOptionsExtensionsKt {
public static final fun setForAdults (Ldev/teogor/ceres/monetisation/ads/model/AdRequestOptions;)V
public static final fun setForChildrenApp (Ldev/teogor/ceres/monetisation/ads/model/AdRequestOptions;)V
public static final fun setForEveryoneApp (Ldev/teogor/ceres/monetisation/ads/model/AdRequestOptions;)V
public static final fun setForFamilies (Ldev/teogor/ceres/monetisation/ads/model/AdRequestOptions;ZZ)V
public static synthetic fun setForFamilies$default (Ldev/teogor/ceres/monetisation/ads/model/AdRequestOptions;ZZILjava/lang/Object;)V
public static final fun setForTeens (Ldev/teogor/ceres/monetisation/ads/model/AdRequestOptions;)V
}

public final class dev/teogor/ceres/monetisation/ads/model/AdContentRating : java/lang/Enum {
public static final field G Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;
public static final field MA Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;
public static final field PG Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;
public static final field T Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;
public static final field UNSPECIFIED Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public final fun getStringValue ()Ljava/lang/String;
public static fun valueOf (Ljava/lang/String;)Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;
public static fun values ()[Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;
}

public final class dev/teogor/ceres/monetisation/ads/model/AdRequestOptions {
public static final field $stable I
public fun <init> ()V
public fun <init> (Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;Ldev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment;Ldev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent;)V
public synthetic fun <init> (Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;Ldev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment;Ldev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;
public final fun component2 ()Ldev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment;
public final fun component3 ()Ldev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent;
public final fun copy (Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;Ldev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment;Ldev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent;)Ldev/teogor/ceres/monetisation/ads/model/AdRequestOptions;
public static synthetic fun copy$default (Ldev/teogor/ceres/monetisation/ads/model/AdRequestOptions;Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;Ldev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment;Ldev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent;ILjava/lang/Object;)Ldev/teogor/ceres/monetisation/ads/model/AdRequestOptions;
public fun equals (Ljava/lang/Object;)Z
public final fun getMaxAdContentRating ()Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;
public final fun getTagForChildDirectedTreatment ()Ldev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment;
public final fun getTagForUnderAgeOfConsent ()Ldev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent;
public fun hashCode ()I
public final fun setMaxAdContentRating (Ldev/teogor/ceres/monetisation/ads/model/AdContentRating;)V
public final fun setTagForChildDirectedTreatment (Ldev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment;)V
public final fun setTagForUnderAgeOfConsent (Ldev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent;)V
public fun toString ()Ljava/lang/String;
}

public final class dev/teogor/ceres/monetisation/ads/model/ConsentRequirementStatus : java/lang/Enum {
public static final field NOT_REQUIRED Ldev/teogor/ceres/monetisation/ads/model/ConsentRequirementStatus;
public static final field REQUIRED Ldev/teogor/ceres/monetisation/ads/model/ConsentRequirementStatus;
Expand All @@ -71,3 +116,23 @@ public final class dev/teogor/ceres/monetisation/ads/model/ConsentStatus : java/
public static fun values ()[Ldev/teogor/ceres/monetisation/ads/model/ConsentStatus;
}

public final class dev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment : java/lang/Enum {
public static final field FALSE Ldev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment;
public static final field TRUE Ldev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment;
public static final field UNSPECIFIED Ldev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public final fun getIntValue ()I
public static fun valueOf (Ljava/lang/String;)Ldev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment;
public static fun values ()[Ldev/teogor/ceres/monetisation/ads/model/TagForChildDirectedTreatment;
}

public final class dev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent : java/lang/Enum {
public static final field FALSE Ldev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent;
public static final field TRUE Ldev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent;
public static final field UNSPECIFIED Ldev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public final fun getIntValue ()I
public static fun valueOf (Ljava/lang/String;)Ldev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent;
public static fun values ()[Ldev/teogor/ceres/monetisation/ads/model/TagForUnderAgeOfConsent;
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import dev.teogor.ceres.monetisation.ads.model.AdRequestOptions
import dev.teogor.ceres.monetisation.ads.model.ConsentRequirementStatus
import dev.teogor.ceres.monetisation.ads.model.ConsentStatus

Expand All @@ -40,6 +41,10 @@ data class AndroidAdsControl(
override val consentRequirementStatus: MutableState<ConsentRequirementStatus> = mutableStateOf(
ConsentRequirementStatus.UNKNOWN,
),
override val adRequestOptions: MutableState<AdRequestOptions> = mutableStateOf(
AdRequestOptions(),
),

val showConsent: (() -> Unit)? = null,
val resetConsent: (() -> Unit)? = null,
) : AdsControl {
Expand Down Expand Up @@ -69,6 +74,7 @@ interface AdsControl {
val canRequestAds: MutableState<Boolean>
val consentStatus: MutableState<ConsentStatus>
val consentRequirementStatus: MutableState<ConsentRequirementStatus>
val adRequestOptions: MutableState<AdRequestOptions>

fun showConsent()

Expand Down
Loading