From c37da80f98e9230d0e66bff436d95bd81da52946 Mon Sep 17 00:00:00 2001 From: Jose Padilla Date: Tue, 14 Nov 2023 12:25:02 -0800 Subject: [PATCH] [HB-6725] Set user consent when HyprMX has initialized. (#10) * [HB-6725] Set user consent after HyprMX has initialized. The HyprMX SDK throws a an exception when the consent is attempted to be set before it has initialized. - Added HyprMX init check before using the consent API. - Updated CHANGELOG, README, and build gradle with adapter version bump. * Added comment. * Added HyprMX init state check. * CR feedback. * Adjust message. * CR feedback and modifications --- CHANGELOG.md | 3 ++ HyprMXAdapter/build.gradle.kts | 2 +- .../mediation/hyprmxadapter/HyprMXAdapter.kt | 51 +++++++++++++------ README.md | 2 +- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81d6089..b467e0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ Note the first digit of every adapter version corresponds to the major version of the Chartboost Mediation SDK compatible with that adapter. Adapters are compatible with any Chartboost Mediation SDK version within that major version. +### 4.6.2.0.3 +- Only set user consent for HyprMX if their SDK has initialized. This prevents a `java.lang.Exception: HyprEvalError ReferenceError` from happening. + ### 4.6.2.0.2 - Updated to handle recent AdFormat changes. diff --git a/HyprMXAdapter/build.gradle.kts b/HyprMXAdapter/build.gradle.kts index dbe1ad5..ca3f53c 100644 --- a/HyprMXAdapter/build.gradle.kts +++ b/HyprMXAdapter/build.gradle.kts @@ -35,7 +35,7 @@ android { minSdk = 21 targetSdk = 33 // If you touch the following line, don't forget to update scripts/get_rc_version.zsh - android.defaultConfig.versionName = System.getenv("VERSION_OVERRIDE") ?: "4.6.2.0.2" + android.defaultConfig.versionName = System.getenv("VERSION_OVERRIDE") ?: "4.6.2.0.3" buildConfigField("String", "CHARTBOOST_MEDIATION_HYPRMX_ADAPTER_VERSION", "\"${android.defaultConfig.versionName}\"") testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" diff --git a/HyprMXAdapter/src/main/java/com/chartboost/mediation/hyprmxadapter/HyprMXAdapter.kt b/HyprMXAdapter/src/main/java/com/chartboost/mediation/hyprmxadapter/HyprMXAdapter.kt index ba85e03..e5ae4dc 100644 --- a/HyprMXAdapter/src/main/java/com/chartboost/mediation/hyprmxadapter/HyprMXAdapter.kt +++ b/HyprMXAdapter/src/main/java/com/chartboost/mediation/hyprmxadapter/HyprMXAdapter.kt @@ -20,6 +20,7 @@ import com.hyprmx.android.sdk.consent.ConsentStatus import com.hyprmx.android.sdk.core.HyprMX import com.hyprmx.android.sdk.core.HyprMXErrors import com.hyprmx.android.sdk.core.HyprMXIf +import com.hyprmx.android.sdk.core.HyprMXState import com.hyprmx.android.sdk.placement.Placement import com.hyprmx.android.sdk.placement.PlacementListener import com.hyprmx.android.sdk.placement.RewardedPlacementListener @@ -213,20 +214,26 @@ class HyprMXAdapter : PartnerAdapter { ) when (gdprConsentStatus) { - GdprConsentStatus.GDPR_CONSENT_GRANTED -> setUserConsent( - context, - ConsentStatus.CONSENT_GIVEN - ) + GdprConsentStatus.GDPR_CONSENT_GRANTED -> checkHyprMxInitStateAndRun { + setUserConsentTask( + context, + ConsentStatus.CONSENT_GIVEN + ) + } - GdprConsentStatus.GDPR_CONSENT_DENIED -> setUserConsent( - context, - ConsentStatus.CONSENT_DECLINED - ) + GdprConsentStatus.GDPR_CONSENT_DENIED -> checkHyprMxInitStateAndRun { + setUserConsentTask( + context, + ConsentStatus.CONSENT_DECLINED + ) + } - GdprConsentStatus.GDPR_CONSENT_UNKNOWN -> setUserConsent( - context, - ConsentStatus.CONSENT_STATUS_UNKNOWN - ) + GdprConsentStatus.GDPR_CONSENT_UNKNOWN -> checkHyprMxInitStateAndRun { + setUserConsentTask( + context, + ConsentStatus.CONSENT_STATUS_UNKNOWN + ) + } } } @@ -261,7 +268,7 @@ class HyprMXAdapter : PartnerAdapter { * @param context a context that will be passed to the SharedPreferences to set the user consent. * @param consentStatus the consent status value to be stored. */ - private fun setUserConsent(context: Context, consentStatus: ConsentStatus) { + private fun setUserConsentTask(context: Context, consentStatus: ConsentStatus) { val prefsWriteSucceeded = context.getSharedPreferences(HYPRMX_PREFS_KEY, Context.MODE_PRIVATE) .edit() .putInt(HYPRMX_USER_CONSENT_KEY, consentStatus.ordinal) @@ -326,8 +333,8 @@ class HyprMXAdapter : PartnerAdapter { ) when (hasGrantedCcpaConsent) { - true -> setUserConsent(context, ConsentStatus.CONSENT_GIVEN) - false -> setUserConsent(context, ConsentStatus.CONSENT_DECLINED) + true -> checkHyprMxInitStateAndRun { setUserConsentTask(context, ConsentStatus.CONSENT_GIVEN) } + false -> checkHyprMxInitStateAndRun { setUserConsentTask(context, ConsentStatus.CONSENT_DECLINED) } } } @@ -770,4 +777,18 @@ class HyprMXAdapter : PartnerAdapter { HyprMXErrors.AD_SIZE_NOT_SET -> ChartboostMediationError.CM_LOAD_FAILURE_INVALID_BANNER_SIZE else -> ChartboostMediationError.CM_PARTNER_ERROR } + + /** + * Checks that the HyprMX initialization state has completed. If so, then run the function; + * otherwise, do nothing. + * + * @param function the function that will be run after the initialization state check was successful. + */ + private fun checkHyprMxInitStateAndRun(function: () -> Unit) { + if (HyprMX.getInitializationState() != HyprMXState.INITIALIZATION_COMPLETE) { + PartnerLogController.log(CUSTOM, "Cannot run $function. The HyprMX SDK has not initialized.") + return + } + function() + } } diff --git a/README.md b/README.md index 070c4ba..76ca98f 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ The Chartboost Mediation HyprMX adapter mediates HyprMX via the Chartboost Media In your `build.gradle`, add the following entry: ``` - implementation "com.chartboost:chartboost-mediation-adapter-hyprmx:4.6.2.0.2" + implementation "com.chartboost:chartboost-mediation-adapter-hyprmx:4.6.2.0.3" ``` ## Contributions