diff --git a/engine/core/System.cpp b/engine/core/System.cpp index faa9837d..13a28851 100644 --- a/engine/core/System.cpp +++ b/engine/core/System.cpp @@ -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"); } diff --git a/engine/core/System.h b/engine/core/System.h index 7eea6dd2..f574749b 100644 --- a/engine/core/System.h +++ b/engine/core/System.h @@ -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(); diff --git a/platform/android/NativeEngine.cpp b/platform/android/NativeEngine.cpp index b10e4833..b463578c 100644 --- a/platform/android/NativeEngine.cpp +++ b/platform/android/NativeEngine.cpp @@ -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"); diff --git a/platform/android/NativeEngine.h b/platform/android/NativeEngine.h index d9e2be55..207d5dce 100644 --- a/platform/android/NativeEngine.h +++ b/platform/android/NativeEngine.h @@ -42,8 +42,6 @@ struct JniData{ jmethodID removeKeyRef; jmethodID initializeAdMob; - jmethodID tagForChildDirectedTreatmentAdMob; - jmethodID tagForUnderAgeOfConsentAdMob; jmethodID loadInterstitialAd; jmethodID isInterstitialAdLoaded; jmethodID showInterstitialAd; diff --git a/platform/android/SupernovaAndroid.cpp b/platform/android/SupernovaAndroid.cpp index 3a79c2db..2414b0f4 100644 --- a/platform/android/SupernovaAndroid.cpp +++ b/platform/android/SupernovaAndroid.cpp @@ -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){ diff --git a/platform/android/SupernovaAndroid.h b/platform/android/SupernovaAndroid.h index ab2ff807..f6f01f72 100644 --- a/platform/android/SupernovaAndroid.h +++ b/platform/android/SupernovaAndroid.h @@ -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(); diff --git a/platform/android/java/org/supernovaengine/supernova/AdMobWrapper.java b/platform/android/java/org/supernovaengine/supernova/AdMobWrapper.java index dc1be704..b6752bae 100644 --- a/platform/android/java/org/supernovaengine/supernova/AdMobWrapper.java +++ b/platform/android/java/org/supernovaengine/supernova/AdMobWrapper.java @@ -36,7 +36,7 @@ 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. @@ -44,11 +44,16 @@ public void initialize(){ @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; @@ -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; @@ -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, @@ -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() { @@ -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(); } @@ -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; + } + }); + } } });