Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 699162284
  • Loading branch information
Justin Malandruccolo authored and copybara-github committed Nov 22, 2024
1 parent eb8303a commit 4befed4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ protected void onCreate(Bundle savedInstanceState) {

googleMobileAdsConsentManager =
GoogleMobileAdsConsentManager.getInstance(getApplicationContext());
// [START can_request_ads]
googleMobileAdsConsentManager.gatherConsent(
this,
consentError -> {
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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) {}
Expand All @@ -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"
Expand Down

0 comments on commit 4befed4

Please sign in to comment.