From 54244d8d94b66cd8449259063b3b2bd79172c346 Mon Sep 17 00:00:00 2001 From: Justin Malandruccolo Date: Tue, 17 Oct 2023 13:04:36 -0700 Subject: [PATCH] Updated GoogleMobileAdsConsentManager to be a singleton for AdMob Java samples PiperOrigin-RevId: 574241621 --- .../GoogleMobileAdsConsentManager.java | 33 ++++++++----- .../adaptivebannerexample/MyActivity.java | 4 +- .../example/appopenexample/MainActivity.java | 22 +++++---- .../example/appopenexample/MyApplication.java | 8 ++-- .../appopenexample/SplashActivity.java | 47 ++++++++++--------- .../gms/example/bannerexample/MyActivity.java | 6 ++- .../GoogleMobileAdsConsentManager.java | 33 ++++++++----- .../interstitialexample/MyActivity.java | 4 +- .../GoogleMobileAdsConsentManager.java | 33 ++++++++----- .../nativeadvancedexample/MainActivity.java | 4 +- .../GoogleMobileAdsConsentManager.java | 33 ++++++++----- .../MainActivity.java | 4 +- .../GoogleMobileAdsConsentManager.java | 33 ++++++++----- .../rewardedvideoexample/MainActivity.java | 5 +- 14 files changed, 171 insertions(+), 98 deletions(-) diff --git a/java/admob/AdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/GoogleMobileAdsConsentManager.java b/java/admob/AdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/GoogleMobileAdsConsentManager.java index fd3ea84d1..9bfbb9403 100644 --- a/java/admob/AdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/GoogleMobileAdsConsentManager.java +++ b/java/admob/AdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/GoogleMobileAdsConsentManager.java @@ -1,7 +1,7 @@ package com.google.android.gms.example.adaptivebannerexample; import android.app.Activity; -import androidx.annotation.NonNull; +import android.content.Context; import com.google.android.ump.ConsentDebugSettings; import com.google.android.ump.ConsentForm.OnConsentFormDismissedListener; import com.google.android.ump.ConsentInformation; @@ -16,21 +16,30 @@ * consent for users in GDPR impacted countries. This is an example and * you can choose another consent management platform to capture consent. */ +@SuppressWarnings("NonFinalStaticField") public class GoogleMobileAdsConsentManager { - private final Activity activity; + private static GoogleMobileAdsConsentManager instance; private final ConsentInformation consentInformation; + /** Private constructor. */ + private GoogleMobileAdsConsentManager(Context context) { + this.consentInformation = UserMessagingPlatform.getConsentInformation(context); + } + + /** Public constructor. */ + public static GoogleMobileAdsConsentManager getInstance(Context context) { + if (instance == null) { + instance = new GoogleMobileAdsConsentManager(context); + } + + return instance; + } + /** Interface definition for a callback to be invoked when consent gathering is complete. */ public interface OnConsentGatheringCompleteListener { void consentGatheringComplete(FormError error); } - /** Constructor */ - public GoogleMobileAdsConsentManager(@NonNull Activity activity) { - this.activity = activity; - this.consentInformation = UserMessagingPlatform.getConsentInformation(activity); - } - /** Helper variable to determine if the app can request ads. */ public boolean canRequestAds() { return consentInformation.canRequestAds(); @@ -42,10 +51,12 @@ public boolean isPrivacyOptionsRequired() { == PrivacyOptionsRequirementStatus.REQUIRED; } - /** Helper method to call the UMP SDK methods to request consent information and load/present a - * consent form if necessary. */ + /** + * Helper method to call the UMP SDK methods to request consent information and load/present a + * consent form if necessary. + */ public void gatherConsent( - OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) { + Activity activity, OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) { // For testing purposes, you can force a DebugGeography of EEA or NOT_EEA. ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(activity) // Uncomment the line below to set DebugGeography to EEA. diff --git a/java/admob/AdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/MyActivity.java b/java/admob/AdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/MyActivity.java index 2eba2de09..6a68855b7 100644 --- a/java/admob/AdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/MyActivity.java +++ b/java/admob/AdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/MyActivity.java @@ -57,8 +57,10 @@ protected void onCreate(Bundle savedInstanceState) { // Log the Mobile Ads SDK version. Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion()); - googleMobileAdsConsentManager = new GoogleMobileAdsConsentManager(this); + googleMobileAdsConsentManager = + GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); googleMobileAdsConsentManager.gatherConsent( + this, consentError -> { if (consentError != null) { // Consent not obtained in current session. diff --git a/java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/MainActivity.java b/java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/MainActivity.java index 0ee332099..0975b4c41 100644 --- a/java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/MainActivity.java +++ b/java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/MainActivity.java @@ -26,17 +26,22 @@ /** Main activity in the app. */ public class MainActivity extends AppCompatActivity { + private GoogleMobileAdsConsentManager googleMobileAdsConsentManager; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + googleMobileAdsConsentManager = + GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.action_menu, menu); MenuItem moreMenu = menu.findItem(R.id.action_more); - moreMenu.setVisible(GoogleMobileAdsConsentManager.getInstance(this).isPrivacyOptionsRequired()); + moreMenu.setVisible(googleMobileAdsConsentManager.isPrivacyOptionsRequired()); return true; } @@ -50,14 +55,13 @@ public boolean onOptionsItemSelected(MenuItem item) { popupMenuItem -> { if (popupMenuItem.getItemId() == R.id.privacy_settings) { // Handle changes to user consent. - GoogleMobileAdsConsentManager.getInstance(this) - .showPrivacyOptionsForm( - this, - formError -> { - if (formError != null) { - Toast.makeText(this, formError.getMessage(), Toast.LENGTH_SHORT).show(); - } - }); + googleMobileAdsConsentManager.showPrivacyOptionsForm( + this, + formError -> { + if (formError != null) { + Toast.makeText(this, formError.getMessage(), Toast.LENGTH_SHORT).show(); + } + }); return true; } return false; diff --git a/java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/MyApplication.java b/java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/MyApplication.java index 8cb822aff..33d5bdad1 100644 --- a/java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/MyApplication.java +++ b/java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/MyApplication.java @@ -130,6 +130,8 @@ private class AppOpenAdManager { private static final String LOG_TAG = "AppOpenAdManager"; private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/3419835294"; + private final GoogleMobileAdsConsentManager googleMobileAdsConsentManager = + GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); private AppOpenAd appOpenAd = null; private boolean isLoadingAd = false; private boolean isShowingAd = false; @@ -237,7 +239,7 @@ private void showAdIfAvailable( if (!isAdAvailable()) { Log.d(LOG_TAG, "The app open ad is not ready yet."); onShowAdCompleteListener.onShowAdComplete(); - if (GoogleMobileAdsConsentManager.getInstance(activity).canRequestAds()) { + if (googleMobileAdsConsentManager.canRequestAds()) { loadAd(currentActivity); } return; @@ -258,7 +260,7 @@ public void onAdDismissedFullScreenContent() { Toast.makeText(activity, "onAdDismissedFullScreenContent", Toast.LENGTH_SHORT).show(); onShowAdCompleteListener.onShowAdComplete(); - if (GoogleMobileAdsConsentManager.getInstance(activity).canRequestAds()) { + if (googleMobileAdsConsentManager.canRequestAds()) { loadAd(activity); } } @@ -274,7 +276,7 @@ public void onAdFailedToShowFullScreenContent(AdError adError) { .show(); onShowAdCompleteListener.onShowAdComplete(); - if (GoogleMobileAdsConsentManager.getInstance(activity).canRequestAds()) { + if (googleMobileAdsConsentManager.canRequestAds()) { loadAd(activity); } } diff --git a/java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/SplashActivity.java b/java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/SplashActivity.java index f8445e443..d8d09a79c 100644 --- a/java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/SplashActivity.java +++ b/java/admob/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/SplashActivity.java @@ -31,6 +31,7 @@ public class SplashActivity extends AppCompatActivity { private static final String LOG_TAG = "SplashActivity"; private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false); + private GoogleMobileAdsConsentManager googleMobileAdsConsentManager; /** * Number of milliseconds to count down before showing the app open ad. This simulates the time @@ -48,29 +49,30 @@ protected void onCreate(Bundle savedInstanceState) { // Create a timer so the SplashActivity will be displayed for a fixed amount of time. createTimer(COUNTER_TIME_MILLISECONDS); - GoogleMobileAdsConsentManager.getInstance(this) - .gatherConsent( - this, - consentError -> { - if (consentError != null) { - // Consent not obtained in current session. - Log.w( - LOG_TAG, - String.format( - "%s: %s", consentError.getErrorCode(), consentError.getMessage())); - } - - if (GoogleMobileAdsConsentManager.getInstance(this).canRequestAds()) { - initializeMobileAdsSdk(); - } - - if (secondsRemaining <= 0) { - startMainActivity(); - } - }); + googleMobileAdsConsentManager = + GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); + googleMobileAdsConsentManager.gatherConsent( + this, + consentError -> { + if (consentError != null) { + // Consent not obtained in current session. + Log.w( + LOG_TAG, + String.format( + "%s: %s", consentError.getErrorCode(), consentError.getMessage())); + } + + if (googleMobileAdsConsentManager.canRequestAds()) { + initializeMobileAdsSdk(); + } + + if (secondsRemaining <= 0) { + startMainActivity(); + } + }); // This sample attempts to load ads using consent obtained in the previous session. - if (GoogleMobileAdsConsentManager.getInstance(this).canRequestAds()) { + if (googleMobileAdsConsentManager.canRequestAds()) { initializeMobileAdsSdk(); } } @@ -106,8 +108,7 @@ public void onShowAdComplete() { // Check if the consent form is currently on screen before moving to the // main // activity. - if (GoogleMobileAdsConsentManager.getInstance(SplashActivity.this) - .canRequestAds()) { + if (googleMobileAdsConsentManager.canRequestAds()) { startMainActivity(); } } diff --git a/java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MyActivity.java b/java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MyActivity.java index 0e36c65f7..afe64a493 100644 --- a/java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MyActivity.java +++ b/java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MyActivity.java @@ -37,9 +37,9 @@ public class MyActivity extends AppCompatActivity { private static final String TAG = "MyActivity"; private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false); - private final GoogleMobileAdsConsentManager googleMobileAdsConsentManager = - GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); + private GoogleMobileAdsConsentManager googleMobileAdsConsentManager; private AdView adView; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -60,6 +60,8 @@ protected void onCreate(Bundle savedInstanceState) { // values/strings.xml. adView = findViewById(R.id.ad_view); + googleMobileAdsConsentManager = + GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); googleMobileAdsConsentManager.gatherConsent( this, consentError -> { diff --git a/java/admob/InterstitialExample/app/src/main/java/com/google/android/gms/example/interstitialexample/GoogleMobileAdsConsentManager.java b/java/admob/InterstitialExample/app/src/main/java/com/google/android/gms/example/interstitialexample/GoogleMobileAdsConsentManager.java index 62d71174a..915c3c07c 100644 --- a/java/admob/InterstitialExample/app/src/main/java/com/google/android/gms/example/interstitialexample/GoogleMobileAdsConsentManager.java +++ b/java/admob/InterstitialExample/app/src/main/java/com/google/android/gms/example/interstitialexample/GoogleMobileAdsConsentManager.java @@ -1,7 +1,7 @@ package com.google.android.gms.example.interstitialexample; import android.app.Activity; -import androidx.annotation.NonNull; +import android.content.Context; import com.google.android.ump.ConsentDebugSettings; import com.google.android.ump.ConsentForm.OnConsentFormDismissedListener; import com.google.android.ump.ConsentInformation; @@ -16,21 +16,30 @@ * consent for users in GDPR impacted countries. This is an example and * you can choose another consent management platform to capture consent. */ +@SuppressWarnings("NonFinalStaticField") public class GoogleMobileAdsConsentManager { - private final Activity activity; + private static GoogleMobileAdsConsentManager instance; private final ConsentInformation consentInformation; + /** Private constructor. */ + private GoogleMobileAdsConsentManager(Context context) { + this.consentInformation = UserMessagingPlatform.getConsentInformation(context); + } + + /** Public constructor. */ + public static GoogleMobileAdsConsentManager getInstance(Context context) { + if (instance == null) { + instance = new GoogleMobileAdsConsentManager(context); + } + + return instance; + } + /** Interface definition for a callback to be invoked when consent gathering is complete. */ public interface OnConsentGatheringCompleteListener { void consentGatheringComplete(FormError error); } - /** Constructor */ - public GoogleMobileAdsConsentManager(@NonNull Activity activity) { - this.activity = activity; - this.consentInformation = UserMessagingPlatform.getConsentInformation(activity); - } - /** Helper variable to determine if the app can request ads. */ public boolean canRequestAds() { return consentInformation.canRequestAds(); @@ -42,10 +51,12 @@ public boolean isPrivacyOptionsRequired() { == PrivacyOptionsRequirementStatus.REQUIRED; } - /** Helper method to call the UMP SDK methods to request consent information and load/present a - * consent form if necessary. */ + /** + * Helper method to call the UMP SDK methods to request consent information and load/present a + * consent form if necessary. + */ public void gatherConsent( - OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) { + Activity activity, OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) { // For testing purposes, you can force a DebugGeography of EEA or NOT_EEA. ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(activity) // .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA) diff --git a/java/admob/InterstitialExample/app/src/main/java/com/google/android/gms/example/interstitialexample/MyActivity.java b/java/admob/InterstitialExample/app/src/main/java/com/google/android/gms/example/interstitialexample/MyActivity.java index f456c6613..c19f58a3f 100644 --- a/java/admob/InterstitialExample/app/src/main/java/com/google/android/gms/example/interstitialexample/MyActivity.java +++ b/java/admob/InterstitialExample/app/src/main/java/com/google/android/gms/example/interstitialexample/MyActivity.java @@ -73,8 +73,10 @@ protected void onCreate(Bundle savedInstanceState) { public void onInitializationComplete(InitializationStatus initializationStatus) {} }); - googleMobileAdsConsentManager = new GoogleMobileAdsConsentManager(this); + googleMobileAdsConsentManager = + GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); googleMobileAdsConsentManager.gatherConsent( + this, consentError -> { if (consentError != null) { // Consent not obtained in current session. diff --git a/java/admob/NativeAdvancedExample/app/src/main/java/com/google/example/gms/nativeadvancedexample/GoogleMobileAdsConsentManager.java b/java/admob/NativeAdvancedExample/app/src/main/java/com/google/example/gms/nativeadvancedexample/GoogleMobileAdsConsentManager.java index 0b9c59834..3b3b27dff 100644 --- a/java/admob/NativeAdvancedExample/app/src/main/java/com/google/example/gms/nativeadvancedexample/GoogleMobileAdsConsentManager.java +++ b/java/admob/NativeAdvancedExample/app/src/main/java/com/google/example/gms/nativeadvancedexample/GoogleMobileAdsConsentManager.java @@ -1,7 +1,7 @@ package com.google.example.gms.nativeadvancedexample; import android.app.Activity; -import androidx.annotation.NonNull; +import android.content.Context; import com.google.android.ump.ConsentDebugSettings; import com.google.android.ump.ConsentForm.OnConsentFormDismissedListener; import com.google.android.ump.ConsentInformation; @@ -16,21 +16,30 @@ * consent for users in GDPR impacted countries. This is an example and * you can choose another consent management platform to capture consent. */ +@SuppressWarnings("NonFinalStaticField") public class GoogleMobileAdsConsentManager { - private final Activity activity; + private static GoogleMobileAdsConsentManager instance; private final ConsentInformation consentInformation; + /** Private constructor. */ + private GoogleMobileAdsConsentManager(Context context) { + this.consentInformation = UserMessagingPlatform.getConsentInformation(context); + } + + /** Public constructor. */ + public static GoogleMobileAdsConsentManager getInstance(Context context) { + if (instance == null) { + instance = new GoogleMobileAdsConsentManager(context); + } + + return instance; + } + /** Interface definition for a callback to be invoked when consent gathering is complete. */ public interface OnConsentGatheringCompleteListener { void consentGatheringComplete(FormError error); } - /** Constructor */ - public GoogleMobileAdsConsentManager(@NonNull Activity activity) { - this.activity = activity; - this.consentInformation = UserMessagingPlatform.getConsentInformation(activity); - } - /** Helper variable to determine if the app can request ads. */ public boolean canRequestAds() { return consentInformation.canRequestAds(); @@ -42,10 +51,12 @@ public boolean isPrivacyOptionsRequired() { == PrivacyOptionsRequirementStatus.REQUIRED; } - /** Helper method to call the UMP SDK methods to request consent information and load/present a - * consent form if necessary. */ + /** + * Helper method to call the UMP SDK methods to request consent information and load/present a + * consent form if necessary. + */ public void gatherConsent( - OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) { + Activity activity, OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) { // For testing purposes, you can force a DebugGeography of EEA or NOT_EEA. ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(activity) // .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA) diff --git a/java/admob/NativeAdvancedExample/app/src/main/java/com/google/example/gms/nativeadvancedexample/MainActivity.java b/java/admob/NativeAdvancedExample/app/src/main/java/com/google/example/gms/nativeadvancedexample/MainActivity.java index 546abcfb5..25101690a 100644 --- a/java/admob/NativeAdvancedExample/app/src/main/java/com/google/example/gms/nativeadvancedexample/MainActivity.java +++ b/java/admob/NativeAdvancedExample/app/src/main/java/com/google/example/gms/nativeadvancedexample/MainActivity.java @@ -76,8 +76,10 @@ protected void onCreate(Bundle savedInstanceState) { // Log the Mobile Ads SDK version. Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion()); - googleMobileAdsConsentManager = new GoogleMobileAdsConsentManager(this); + googleMobileAdsConsentManager = + GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); googleMobileAdsConsentManager.gatherConsent( + this, consentError -> { if (consentError != null) { // Consent not obtained in current session. diff --git a/java/admob/RewardedInterstitialExample/app/src/main/java/com/google/ads/rewardedinterstitialexample/GoogleMobileAdsConsentManager.java b/java/admob/RewardedInterstitialExample/app/src/main/java/com/google/ads/rewardedinterstitialexample/GoogleMobileAdsConsentManager.java index aedb79a17..2f1665969 100644 --- a/java/admob/RewardedInterstitialExample/app/src/main/java/com/google/ads/rewardedinterstitialexample/GoogleMobileAdsConsentManager.java +++ b/java/admob/RewardedInterstitialExample/app/src/main/java/com/google/ads/rewardedinterstitialexample/GoogleMobileAdsConsentManager.java @@ -1,7 +1,7 @@ package com.google.ads.rewardedinterstitialexample; import android.app.Activity; -import androidx.annotation.NonNull; +import android.content.Context; import com.google.android.ump.ConsentDebugSettings; import com.google.android.ump.ConsentForm.OnConsentFormDismissedListener; import com.google.android.ump.ConsentInformation; @@ -16,21 +16,30 @@ * consent for users in GDPR impacted countries. This is an example and * you can choose another consent management platform to capture consent. */ +@SuppressWarnings("NonFinalStaticField") public class GoogleMobileAdsConsentManager { - private final Activity activity; + private static GoogleMobileAdsConsentManager instance; private final ConsentInformation consentInformation; + /** Private constructor. */ + private GoogleMobileAdsConsentManager(Context context) { + this.consentInformation = UserMessagingPlatform.getConsentInformation(context); + } + + /** Public constructor. */ + public static GoogleMobileAdsConsentManager getInstance(Context context) { + if (instance == null) { + instance = new GoogleMobileAdsConsentManager(context); + } + + return instance; + } + /** Interface definition for a callback to be invoked when consent gathering is complete. */ public interface OnConsentGatheringCompleteListener { void consentGatheringComplete(FormError error); } - /** Constructor */ - public GoogleMobileAdsConsentManager(@NonNull Activity activity) { - this.activity = activity; - this.consentInformation = UserMessagingPlatform.getConsentInformation(activity); - } - /** Helper variable to determine if the app can request ads. */ public boolean canRequestAds() { return consentInformation.canRequestAds(); @@ -42,10 +51,12 @@ public boolean isPrivacyOptionsRequired() { == PrivacyOptionsRequirementStatus.REQUIRED; } - /** Helper method to call the UMP SDK methods to request consent information and load/present a - * consent form if necessary. */ + /** + * Helper method to call the UMP SDK methods to request consent information and load/present a + * consent form if necessary. + */ public void gatherConsent( - OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) { + Activity activity, OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) { // For testing purposes, you can force a DebugGeography of EEA or NOT_EEA. ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(activity) // .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA) diff --git a/java/admob/RewardedInterstitialExample/app/src/main/java/com/google/ads/rewardedinterstitialexample/MainActivity.java b/java/admob/RewardedInterstitialExample/app/src/main/java/com/google/ads/rewardedinterstitialexample/MainActivity.java index 83636016a..d30a9a616 100644 --- a/java/admob/RewardedInterstitialExample/app/src/main/java/com/google/ads/rewardedinterstitialexample/MainActivity.java +++ b/java/admob/RewardedInterstitialExample/app/src/main/java/com/google/ads/rewardedinterstitialexample/MainActivity.java @@ -57,9 +57,11 @@ protected void onCreate(Bundle savedInstanceState) { // Log the Mobile Ads SDK version. Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion()); - googleMobileAdsConsentManager = new GoogleMobileAdsConsentManager(this); + googleMobileAdsConsentManager = + GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); googleMobileAdsConsentManager.gatherConsent( + this, consentError -> { if (consentError != null) { // Consent not obtained in current session. diff --git a/java/admob/RewardedVideoExample/app/src/main/java/com/google/ads/rewardedvideoexample/GoogleMobileAdsConsentManager.java b/java/admob/RewardedVideoExample/app/src/main/java/com/google/ads/rewardedvideoexample/GoogleMobileAdsConsentManager.java index 36b7cc9ce..55ee48c68 100644 --- a/java/admob/RewardedVideoExample/app/src/main/java/com/google/ads/rewardedvideoexample/GoogleMobileAdsConsentManager.java +++ b/java/admob/RewardedVideoExample/app/src/main/java/com/google/ads/rewardedvideoexample/GoogleMobileAdsConsentManager.java @@ -1,7 +1,7 @@ package com.google.ads.rewardedvideoexample; import android.app.Activity; -import androidx.annotation.NonNull; +import android.content.Context; import com.google.android.ump.ConsentDebugSettings; import com.google.android.ump.ConsentForm.OnConsentFormDismissedListener; import com.google.android.ump.ConsentInformation; @@ -16,21 +16,30 @@ * consent for users in GDPR impacted countries. This is an example and * you can choose another consent management platform to capture consent. */ +@SuppressWarnings("NonFinalStaticField") public class GoogleMobileAdsConsentManager { - private final Activity activity; + private static GoogleMobileAdsConsentManager instance; private final ConsentInformation consentInformation; + /** Private constructor. */ + private GoogleMobileAdsConsentManager(Context context) { + this.consentInformation = UserMessagingPlatform.getConsentInformation(context); + } + + /** Public constructor. */ + public static GoogleMobileAdsConsentManager getInstance(Context context) { + if (instance == null) { + instance = new GoogleMobileAdsConsentManager(context); + } + + return instance; + } + /** Interface definition for a callback to be invoked when consent gathering is complete. */ public interface OnConsentGatheringCompleteListener { void consentGatheringComplete(FormError error); } - /** Constructor */ - public GoogleMobileAdsConsentManager(@NonNull Activity activity) { - this.activity = activity; - this.consentInformation = UserMessagingPlatform.getConsentInformation(activity); - } - /** Helper variable to determine if the app can request ads. */ public boolean canRequestAds() { return consentInformation.canRequestAds(); @@ -42,10 +51,12 @@ public boolean isPrivacyOptionsRequired() { == PrivacyOptionsRequirementStatus.REQUIRED; } - /** Helper method to call the UMP SDK methods to request consent information and load/present a - * consent form if necessary. */ + /** + * Helper method to call the UMP SDK methods to request consent information and load/present a + * consent form if necessary. + */ public void gatherConsent( - OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) { + Activity activity, OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) { // For testing purposes, you can force a DebugGeography of EEA or NOT_EEA. ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(activity) // .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA) diff --git a/java/admob/RewardedVideoExample/app/src/main/java/com/google/ads/rewardedvideoexample/MainActivity.java b/java/admob/RewardedVideoExample/app/src/main/java/com/google/ads/rewardedvideoexample/MainActivity.java index b9282a276..b05f9555f 100644 --- a/java/admob/RewardedVideoExample/app/src/main/java/com/google/ads/rewardedvideoexample/MainActivity.java +++ b/java/admob/RewardedVideoExample/app/src/main/java/com/google/ads/rewardedvideoexample/MainActivity.java @@ -57,9 +57,10 @@ protected void onCreate(Bundle savedInstanceState) { // Log the Mobile Ads SDK version. Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion()); - googleMobileAdsConsentManager = new GoogleMobileAdsConsentManager(this); - + googleMobileAdsConsentManager = + GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); googleMobileAdsConsentManager.gatherConsent( + this, consentError -> { if (consentError != null) { // Consent not obtained in current session.