Skip to content

Commit

Permalink
[HB-6725] Set user consent when HyprMX has initialized. (#10)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
cb-jpadilla authored Nov 14, 2023
1 parent de489a5 commit c37da80
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion HyprMXAdapter/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) }
}
}

Expand Down Expand Up @@ -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()
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c37da80

Please sign in to comment.