Skip to content

Commit

Permalink
Finished Google UMP integration for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Jul 20, 2023
1 parent 9a30813 commit 9a590e0
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 105 deletions.
10 changes: 1 addition & 9 deletions engine/core/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,10 @@ void System::removeKey(const char *key){
XMLUtils::removeKey(USERSETTINGS_XML_FILE, USERSETTINGS_ROOT, key);
}

void System::initializeAdMob(){
void System::initializeAdMob(bool tagForChildDirectedTreatment, bool tagForUnderAgeOfConsent){
Log::error("Cannot initialize AdMob in this system");
}

void System::tagForChildDirectedTreatmentAdMob(bool enable){
Log::error("Cannot tagForChildDirectedTreatment AdMob in this system");
}

void System::tagForUnderAgeOfConsentAdMob(bool enable){
Log::error("Cannot tagForUnderAgeOfConsent AdMob in this system");
}

void System::loadInterstitialAd(std::string adUnitID){
Log::error("Cannot load InterstitialAd in this system");
}
Expand Down
4 changes: 1 addition & 3 deletions engine/core/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ namespace Supernova {
virtual void removeKey(const char *key);

// Google AdMob SDK
virtual void initializeAdMob();
virtual void tagForChildDirectedTreatmentAdMob(bool enable);
virtual void tagForUnderAgeOfConsentAdMob(bool enable);
virtual void initializeAdMob(bool tagForChildDirectedTreatment = false, bool tagForUnderAgeOfConsent = false);
virtual void loadInterstitialAd(std::string adUnitID);
virtual bool isInterstitialAdLoaded();
virtual void showInterstitialAd();
Expand Down
4 changes: 1 addition & 3 deletions platform/android/NativeEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,7 @@ void NativeEngine::setupJNI(){

mJniData.removeKeyRef = env->GetMethodID(mJniData.userSettingsClsRef, "removeKey", "(Ljava/lang/String;)V");

mJniData.initializeAdMob = env->GetMethodID(mJniData.adMobWrapperClsRef, "initialize","()V");
mJniData.tagForChildDirectedTreatmentAdMob = env->GetMethodID(mJniData.adMobWrapperClsRef, "tagForChildDirectedTreatment","(Z)V");
mJniData.tagForUnderAgeOfConsentAdMob = env->GetMethodID(mJniData.adMobWrapperClsRef, "tagForUnderAgeOfConsent","(Z)V");
mJniData.initializeAdMob = env->GetMethodID(mJniData.adMobWrapperClsRef, "initialize","(ZZ)V");
mJniData.loadInterstitialAd = env->GetMethodID(mJniData.adMobWrapperClsRef, "loadInterstitialAd","(Ljava/lang/String;)V");
mJniData.isInterstitialAdLoaded = env->GetMethodID(mJniData.adMobWrapperClsRef, "isInterstitialAdLoaded","()Z");
mJniData.showInterstitialAd = env->GetMethodID(mJniData.adMobWrapperClsRef, "showInterstitialAd","()V");
Expand Down
2 changes: 0 additions & 2 deletions platform/android/NativeEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ struct JniData{
jmethodID removeKeyRef;

jmethodID initializeAdMob;
jmethodID tagForChildDirectedTreatmentAdMob;
jmethodID tagForUnderAgeOfConsentAdMob;
jmethodID loadInterstitialAd;
jmethodID isInterstitialAdLoaded;
jmethodID showInterstitialAd;
Expand Down
18 changes: 2 additions & 16 deletions platform/android/SupernovaAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,25 +210,11 @@ void SupernovaAndroid::removeKey(const char* key){
env->DeleteLocalRef(strKey);
}

void SupernovaAndroid::initializeAdMob(){
void SupernovaAndroid::initializeAdMob(bool tagForChildDirectedTreatment, bool tagForUnderAgeOfConsent){
JniData& jniData = NativeEngine::getInstance()->getJniData();
JNIEnv* env = NativeEngine::getInstance()->getJniEnv();

env->CallVoidMethod(jniData.adMobWrapperObjRef, jniData.initializeAdMob);
}

void SupernovaAndroid::tagForChildDirectedTreatmentAdMob(bool enable){
JniData& jniData = NativeEngine::getInstance()->getJniData();
JNIEnv* env = NativeEngine::getInstance()->getJniEnv();

env->CallVoidMethod(jniData.adMobWrapperObjRef, jniData.tagForChildDirectedTreatmentAdMob, enable);
}

void SupernovaAndroid::tagForUnderAgeOfConsentAdMob(bool enable){
JniData& jniData = NativeEngine::getInstance()->getJniData();
JNIEnv* env = NativeEngine::getInstance()->getJniEnv();

env->CallVoidMethod(jniData.adMobWrapperObjRef, jniData.tagForUnderAgeOfConsentAdMob, enable);
env->CallVoidMethod(jniData.adMobWrapperObjRef, jniData.initializeAdMob, tagForChildDirectedTreatment, tagForUnderAgeOfConsent);
}

void SupernovaAndroid::loadInterstitialAd(std::string adUnitID){
Expand Down
4 changes: 1 addition & 3 deletions platform/android/SupernovaAndroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ class SupernovaAndroid: public Supernova::System {

virtual void removeKey(const char* key);

virtual void initializeAdMob();
virtual void tagForChildDirectedTreatmentAdMob(bool enable);
virtual void tagForUnderAgeOfConsentAdMob(bool enable);
virtual void initializeAdMob(bool tagForChildDirectedTreatment, bool tagForUnderAgeOfConsent);
virtual void loadInterstitialAd(std::string adUnitID);
virtual bool isInterstitialAdLoaded();
virtual void showInterstitialAd();
Expand Down
135 changes: 66 additions & 69 deletions platform/android/java/org/supernovaengine/supernova/AdMobWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,24 @@ public AdMobWrapper(final Activity activity) {
this.activity = activity;
}

public void initialize(){
public void initialize(boolean tagForChildDirectedTreatment, boolean tagForUnderAgeOfConsent){
activity.runOnUiThread(new Runnable() {
@Override public void run() {
// Initialize the Mobile Ads SDK.
MobileAds.initialize(activity, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});

setTagForChildDirectedTreatment(tagForChildDirectedTreatment);
setTagForUnderAgeOfConsent(tagForUnderAgeOfConsent);

requestConsent(tagForChildDirectedTreatment || tagForUnderAgeOfConsent);
}
});
}

public void tagForChildDirectedTreatment(boolean enable){
private void setTagForChildDirectedTreatment(boolean enable){
activity.runOnUiThread(new Runnable() {
@Override public void run() {
int tagValue;
Expand All @@ -67,7 +72,7 @@ public void tagForChildDirectedTreatment(boolean enable){
});
}

public void tagForUnderAgeOfConsent(boolean enable){
private void setTagForUnderAgeOfConsent(boolean enable){
activity.runOnUiThread(new Runnable() {
@Override public void run() {
int tagValue;
Expand All @@ -86,30 +91,24 @@ public void tagForUnderAgeOfConsent(boolean enable){
});
}

public void requestConsent(){
boolean tagUnderAgeCOnsent = false;
if (MobileAds.getRequestConfiguration().getTagForChildDirectedTreatment() == RequestConfiguration.TAG_FOR_CHILD_DIRECTED_TREATMENT_TRUE){
tagUnderAgeCOnsent = true;
}
if (MobileAds.getRequestConfiguration().getTagForUnderAgeOfConsent() == RequestConfiguration.TAG_FOR_UNDER_AGE_OF_CONSENT_TRUE){
tagUnderAgeCOnsent = true;
}

ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(activity)
.setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
.addTestDeviceHashedId("TEST-DEVICE-HASHED-ID")
.build();
private void requestConsent(boolean tagForUnderAgeConsent){
//ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(activity)
// .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
// .addTestDeviceHashedId("TEST-DEVICE-HASHED-ID")
// .build();

// Set tag for under age of consent. false means users are not under
// age.
ConsentRequestParameters params = new ConsentRequestParameters
.Builder()
// call requestConsentInfoUpdate() without setting this value, your app logs the required ID hash when run
//.setConsentDebugSettings(debugSettings)
.setTagForUnderAgeOfConsent(tagUnderAgeCOnsent)
.setTagForUnderAgeOfConsent(tagForUnderAgeConsent)
.build();

consentInformation = UserMessagingPlatform.getConsentInformation(activity);
//consentInformation.reset();

consentInformation.requestConsentInfoUpdate(
activity,
params,
Expand All @@ -118,9 +117,6 @@ public void requestConsent(){
public void onConsentInfoUpdateSuccess() {
// The consent information state was updated.
// You are now ready to check if a form is available.
if (consentInformation.isConsentFormAvailable()) {
loadForm();
}
}
},
new ConsentInformation.OnConsentInfoUpdateFailureListener() {
Expand All @@ -145,10 +141,6 @@ public void onConsentFormLoadSuccess(ConsentForm consentForm) {
new ConsentForm.OnConsentFormDismissedListener() {
@Override
public void onConsentFormDismissed(FormError formError) {
if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.OBTAINED) {
// App can start requesting ads.
}

// Handle dismissal by reloading form.
loadForm();
}
Expand All @@ -169,52 +161,57 @@ public void loadInterstitialAd(String adUnitID) {
activity.runOnUiThread(new Runnable() {
@Override public void run() {

AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(
activity,
adUnitID,
adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
AdMobWrapper.this.interstitialAd = interstitialAd;

interstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
// Make sure to set your reference to null so you don't
// show it a second time.
AdMobWrapper.this.interstitialAd = null;
}

@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
// Make sure to set your reference to null so you don't
// show it a second time.
AdMobWrapper.this.interstitialAd = null;
Log.e("Supernova", "The ad failed to show.");
}

@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
}
});
}

@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.e("Supernova", loadAdError.getMessage());
interstitialAd = null;
}
});
if (consentInformation.isConsentFormAvailable()) {
loadForm();
}

if (consentInformation.getConsentStatus() == ConsentInformation.ConsentStatus.OBTAINED) {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(
activity,
adUnitID,
adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
AdMobWrapper.this.interstitialAd = interstitialAd;

interstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
// Make sure to set your reference to null so you don't
// show it a second time.
AdMobWrapper.this.interstitialAd = null;
}

@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
// Make sure to set your reference to null so you don't
// show it a second time.
AdMobWrapper.this.interstitialAd = null;
Log.e("Supernova", "The ad failed to show.");
}

@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
}
});
}

@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.e("Supernova", loadAdError.getMessage());
interstitialAd = null;
}
});
}
}
});

Expand Down

0 comments on commit 9a590e0

Please sign in to comment.