diff --git a/java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/GoogleMobileAdsConsentManager.java b/java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/GoogleMobileAdsConsentManager.java index b649efa6..b6ac700b 100644 --- a/java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/GoogleMobileAdsConsentManager.java +++ b/java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/GoogleMobileAdsConsentManager.java @@ -59,15 +59,12 @@ public boolean canRequestAds() { return consentInformation.canRequestAds(); } - // [START is_privacy_options_required] /** Helper variable to determine if the privacy options form is required. */ public boolean isPrivacyOptionsRequired() { return consentInformation.getPrivacyOptionsRequirementStatus() == PrivacyOptionsRequirementStatus.REQUIRED; } - // [END is_privacy_options_required] - /** * Helper method to call the UMP SDK methods to request consent information and load/present a * consent form if necessary. @@ -84,21 +81,34 @@ public void gatherConsent( ConsentRequestParameters params = new ConsentRequestParameters.Builder().setConsentDebugSettings(debugSettings).build(); - // [START gather_consent] + // [START request_consent_info_update] // Requesting an update to consent information should be called on every app launch. consentInformation.requestConsentInfoUpdate( activity, params, - () -> - UserMessagingPlatform.loadAndShowConsentFormIfRequired( - activity, - formError -> { - // Consent has been gathered. - onConsentGatheringCompleteListener.consentGatheringComplete(formError); - }), - requestConsentError -> + () -> // Called when consent information is successfully updated. + // [START_EXCLUDE silent] + loadAndShowConsentFormIfRequired(activity, onConsentGatheringCompleteListener), + // [END_EXCLUDE] + requestConsentError -> // Called when there's an error updating consent information. + // [START_EXCLUDE silent] onConsentGatheringCompleteListener.consentGatheringComplete(requestConsentError)); - // [END gather_consent] + // [END_EXCLUDE] + // [END request_consent_info_update] + } + + private void loadAndShowConsentFormIfRequired( + Activity activity, OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) { + // [START load_and_show_consent_form] + UserMessagingPlatform.loadAndShowConsentFormIfRequired( + activity, + formError -> { + // Consent gathering process is complete. + // [START_EXCLUDE silent] + onConsentGatheringCompleteListener.consentGatheringComplete(formError); + // [END_EXCLUDE] + }); + // [END load_and_show_consent_form] } /** Helper method to call the UMP SDK method to present the privacy options form. */ 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 ea6d4898..dd609882 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 @@ -66,7 +66,6 @@ protected void onCreate(Bundle savedInstanceState) { googleMobileAdsConsentManager = GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); - // [START can_request_ads] googleMobileAdsConsentManager.gatherConsent( this, consentError -> { @@ -80,22 +79,17 @@ protected void onCreate(Bundle savedInstanceState) { if (googleMobileAdsConsentManager.canRequestAds()) { initializeMobileAdsSdk(); } - // [START_EXCLUDE] - // [START add_privacy_options] if (googleMobileAdsConsentManager.isPrivacyOptionsRequired()) { // Regenerate the options menu to include a privacy setting. invalidateOptionsMenu(); } - // [END add_privacy_options] - // [END_EXCLUDE] }); // This sample attempts to load ads using consent obtained in the previous session. if (googleMobileAdsConsentManager.canRequestAds()) { initializeMobileAdsSdk(); } - // [END can_request_ads] } @Override @@ -188,18 +182,17 @@ private void loadBanner() { // [END load_ad] } - // [START request_ads] private void initializeMobileAdsSdk() { if (isMobileAdsInitializeCalled.getAndSet(true)) { return; } - // [START_EXCLUDE silent] Hide from developer docs code snippet + // Set your test devices. MobileAds.setRequestConfiguration( new RequestConfiguration.Builder() .setTestDeviceIds(Arrays.asList(TEST_DEVICE_HASHED_ID)) .build()); - // [END_EXCLUDE] + new Thread( () -> { // Initialize the Google Mobile Ads SDK on a background thread. @@ -211,8 +204,6 @@ private void initializeMobileAdsSdk() { .start(); } - // [END request_ads] - // [START get_ad_size] // Get the ad size with screen width. public AdSize getAdSize() { diff --git a/kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/GoogleMobileAdsConsentManager.kt b/kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/GoogleMobileAdsConsentManager.kt index 2cd6fc94..e50d7d26 100644 --- a/kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/GoogleMobileAdsConsentManager.kt +++ b/kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/GoogleMobileAdsConsentManager.kt @@ -69,22 +69,39 @@ class GoogleMobileAdsConsentManager private constructor(context: Context) { val params = ConsentRequestParameters.Builder().setConsentDebugSettings(debugSettings).build() - // [START gather_consent] + // [START request_consent_info_update] // Requesting an update to consent information should be called on every app launch. consentInformation.requestConsentInfoUpdate( activity, params, { - UserMessagingPlatform.loadAndShowConsentFormIfRequired(activity) { formError -> - // Consent has been gathered. - onConsentGatheringCompleteListener.consentGatheringComplete(formError) - } + // Called when consent information is successfully updated. + // [START_EXCLUDE silent] + loadAndShowConsentFormIfRequired(activity, onConsentGatheringCompleteListener) + // [END_EXCLUDE] }, { requestConsentError -> + // Called when there's an error updating consent information. + // [START_EXCLUDE silent] onConsentGatheringCompleteListener.consentGatheringComplete(requestConsentError) + // [END_EXCLUDE] }, ) - // [END gather_consent] + // [END request_consent_info_update] + } + + private fun loadAndShowConsentFormIfRequired( + activity: Activity, + onConsentGatheringCompleteListener: OnConsentGatheringCompleteListener, + ) { + // [START load_and_show_consent_form] + UserMessagingPlatform.loadAndShowConsentFormIfRequired(activity) { formError -> + // Consent gathering process is complete. + // [START_EXCLUDE silent] + onConsentGatheringCompleteListener.consentGatheringComplete(formError) + // [END_EXCLUDE] + } + // [END load_and_show_consent_form] } /** Helper method to call the UMP SDK method to show the privacy options form. */ diff --git a/kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MainActivity.kt b/kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MainActivity.kt index f5aafc4c..987c777d 100644 --- a/kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MainActivity.kt +++ b/kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MainActivity.kt @@ -73,7 +73,6 @@ class MainActivity : AppCompatActivity() { Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion()) googleMobileAdsConsentManager = GoogleMobileAdsConsentManager.getInstance(applicationContext) - // [START can_request_ads] googleMobileAdsConsentManager.gatherConsent(this) { error -> if (error != null) { // Consent not obtained in current session. @@ -83,15 +82,11 @@ class MainActivity : AppCompatActivity() { if (googleMobileAdsConsentManager.canRequestAds) { initializeMobileAdsSdk() } - // [START_EXCLUDE] - // [START add_privacy_options] if (googleMobileAdsConsentManager.isPrivacyOptionsRequired) { // Regenerate the options menu to include a privacy setting. invalidateOptionsMenu() } - // [END add_privacy_options] - // [END_EXCLUDE] } // This sample attempts to load ads using consent obtained in the previous session. @@ -178,17 +173,16 @@ class MainActivity : AppCompatActivity() { // [END load_ad] } - // [START request_ads] private fun initializeMobileAdsSdk() { if (isMobileAdsInitializeCalled.getAndSet(true)) { return } - // [START_EXCLUDE silent] // [START_EXCLUDE silent] Hide from developer docs code snippet + // Set your test devices. MobileAds.setRequestConfiguration( RequestConfiguration.Builder().setTestDeviceIds(listOf(TEST_DEVICE_HASHED_ID)).build() ) - // [END_EXCLUDE] + CoroutineScope(Dispatchers.IO).launch { // Initialize the Google Mobile Ads SDK on a background thread. MobileAds.initialize(this@MainActivity) {} @@ -200,8 +194,6 @@ class MainActivity : AppCompatActivity() { } } - // [END request_ads] - companion object { // This is an ad unit ID for a test ad. Replace with your own banner ad unit ID. private const val AD_UNIT_ID = "ca-app-pub-3940256099942544/9214589741"