From 88d752a7f5d4c1151104928dd4eca9fff8803361 Mon Sep 17 00:00:00 2001 From: Justin Malandruccolo Date: Thu, 17 Oct 2024 11:35:03 -0700 Subject: [PATCH] Add includecode comments for UMP SDK integration PiperOrigin-RevId: 686987300 --- .../GoogleMobileAdsConsentManager.java | 7 +++++++ .../gms/example/bannerexample/MyActivity.java | 17 ++++++++++++++--- .../GoogleMobileAdsConsentManager.kt | 7 +++++++ .../gms/example/bannerexample/MainActivity.kt | 13 +++++++++++-- 4 files changed, 39 insertions(+), 5 deletions(-) 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 b32f81d31..b649efa61 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,12 +59,15 @@ 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. @@ -81,6 +84,7 @@ public void gatherConsent( ConsentRequestParameters params = new ConsentRequestParameters.Builder().setConsentDebugSettings(debugSettings).build(); + // [START gather_consent] // Requesting an update to consent information should be called on every app launch. consentInformation.requestConsentInfoUpdate( activity, @@ -94,11 +98,14 @@ public void gatherConsent( }), requestConsentError -> onConsentGatheringCompleteListener.consentGatheringComplete(requestConsentError)); + // [END gather_consent] } /** Helper method to call the UMP SDK method to present the privacy options form. */ public void showPrivacyOptionsForm( Activity activity, OnConsentFormDismissedListener onConsentFormDismissedListener) { + // [START present_privacy_options_form] UserMessagingPlatform.showPrivacyOptionsForm(activity, onConsentFormDismissedListener); + // [END present_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 f303068bd..100b401c8 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,6 +66,7 @@ protected void onCreate(Bundle savedInstanceState) { googleMobileAdsConsentManager = GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); + // [START can_request_ads] googleMobileAdsConsentManager.gatherConsent( this, consentError -> { @@ -79,17 +80,22 @@ 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 @@ -182,15 +188,18 @@ 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()); - + 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. @@ -202,6 +211,8 @@ private void initializeMobileAdsSdk() { .start(); } + // [END request_ads] + // Get the ad size with screen width. public AdSize getAdSize() { DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); 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 cacc8c5e4..2cd6fc945 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 @@ -43,12 +43,15 @@ class GoogleMobileAdsConsentManager private constructor(context: Context) { val canRequestAds: Boolean get() = consentInformation.canRequestAds() + // [START is_privacy_options_required] /** Helper variable to determine if the privacy options form is required. */ val isPrivacyOptionsRequired: Boolean get() = consentInformation.privacyOptionsRequirementStatus == ConsentInformation.PrivacyOptionsRequirementStatus.REQUIRED + // [END is_privacy_options_required] + /** * Helper method to call the UMP SDK methods to request consent information and load/show a * consent form if necessary. @@ -66,6 +69,7 @@ class GoogleMobileAdsConsentManager private constructor(context: Context) { val params = ConsentRequestParameters.Builder().setConsentDebugSettings(debugSettings).build() + // [START gather_consent] // Requesting an update to consent information should be called on every app launch. consentInformation.requestConsentInfoUpdate( activity, @@ -80,6 +84,7 @@ class GoogleMobileAdsConsentManager private constructor(context: Context) { onConsentGatheringCompleteListener.consentGatheringComplete(requestConsentError) }, ) + // [END gather_consent] } /** Helper method to call the UMP SDK method to show the privacy options form. */ @@ -87,7 +92,9 @@ class GoogleMobileAdsConsentManager private constructor(context: Context) { activity: MainActivity, onConsentFormDismissedListener: OnConsentFormDismissedListener, ) { + // [START present_privacy_options_form] UserMessagingPlatform.showPrivacyOptionsForm(activity, onConsentFormDismissedListener) + // [END present_privacy_options_form] } companion object { 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 cef0c7680..3b7f4f811 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 @@ -70,6 +70,7 @@ 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. @@ -79,17 +80,22 @@ 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. if (googleMobileAdsConsentManager.canRequestAds) { initializeMobileAdsSdk() } + // [END can_request_ads] } /** Called when leaving the activity. */ @@ -169,16 +175,17 @@ 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) {} @@ -190,6 +197,8 @@ 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"