Skip to content

Commit

Permalink
Add includecode comments for UMP SDK integration
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 686987300
  • Loading branch information
Justin Malandruccolo authored and copybara-github committed Oct 17, 2024
1 parent 6ac1e9a commit 88d752a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -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]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ protected void onCreate(Bundle savedInstanceState) {

googleMobileAdsConsentManager =
GoogleMobileAdsConsentManager.getInstance(getApplicationContext());
// [START can_request_ads]
googleMobileAdsConsentManager.gatherConsent(
this,
consentError -> {
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand All @@ -80,14 +84,17 @@ 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. */
fun showPrivacyOptionsForm(
activity: MainActivity,
onConsentFormDismissedListener: OnConsentFormDismissedListener,
) {
// [START present_privacy_options_form]
UserMessagingPlatform.showPrivacyOptionsForm(activity, onConsentFormDismissedListener)
// [END present_privacy_options_form]
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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. */
Expand Down Expand Up @@ -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) {}
Expand All @@ -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"
Expand Down

0 comments on commit 88d752a

Please sign in to comment.