Skip to content

Commit 4befed4

Browse files
malandr2copybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 699162284
1 parent eb8303a commit 4befed4

File tree

4 files changed

+50
-40
lines changed

4 files changed

+50
-40
lines changed

java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/GoogleMobileAdsConsentManager.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,12 @@ public boolean canRequestAds() {
5959
return consentInformation.canRequestAds();
6060
}
6161

62-
// [START is_privacy_options_required]
6362
/** Helper variable to determine if the privacy options form is required. */
6463
public boolean isPrivacyOptionsRequired() {
6564
return consentInformation.getPrivacyOptionsRequirementStatus()
6665
== PrivacyOptionsRequirementStatus.REQUIRED;
6766
}
6867

69-
// [END is_privacy_options_required]
70-
7168
/**
7269
* Helper method to call the UMP SDK methods to request consent information and load/present a
7370
* consent form if necessary.
@@ -84,21 +81,34 @@ public void gatherConsent(
8481
ConsentRequestParameters params =
8582
new ConsentRequestParameters.Builder().setConsentDebugSettings(debugSettings).build();
8683

87-
// [START gather_consent]
84+
// [START request_consent_info_update]
8885
// Requesting an update to consent information should be called on every app launch.
8986
consentInformation.requestConsentInfoUpdate(
9087
activity,
9188
params,
92-
() ->
93-
UserMessagingPlatform.loadAndShowConsentFormIfRequired(
94-
activity,
95-
formError -> {
96-
// Consent has been gathered.
97-
onConsentGatheringCompleteListener.consentGatheringComplete(formError);
98-
}),
99-
requestConsentError ->
89+
() -> // Called when consent information is successfully updated.
90+
// [START_EXCLUDE silent]
91+
loadAndShowConsentFormIfRequired(activity, onConsentGatheringCompleteListener),
92+
// [END_EXCLUDE]
93+
requestConsentError -> // Called when there's an error updating consent information.
94+
// [START_EXCLUDE silent]
10095
onConsentGatheringCompleteListener.consentGatheringComplete(requestConsentError));
101-
// [END gather_consent]
96+
// [END_EXCLUDE]
97+
// [END request_consent_info_update]
98+
}
99+
100+
private void loadAndShowConsentFormIfRequired(
101+
Activity activity, OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) {
102+
// [START load_and_show_consent_form]
103+
UserMessagingPlatform.loadAndShowConsentFormIfRequired(
104+
activity,
105+
formError -> {
106+
// Consent gathering process is complete.
107+
// [START_EXCLUDE silent]
108+
onConsentGatheringCompleteListener.consentGatheringComplete(formError);
109+
// [END_EXCLUDE]
110+
});
111+
// [END load_and_show_consent_form]
102112
}
103113

104114
/** Helper method to call the UMP SDK method to present the privacy options form. */

java/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MyActivity.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ protected void onCreate(Bundle savedInstanceState) {
6666

6767
googleMobileAdsConsentManager =
6868
GoogleMobileAdsConsentManager.getInstance(getApplicationContext());
69-
// [START can_request_ads]
7069
googleMobileAdsConsentManager.gatherConsent(
7170
this,
7271
consentError -> {
@@ -80,22 +79,17 @@ protected void onCreate(Bundle savedInstanceState) {
8079
if (googleMobileAdsConsentManager.canRequestAds()) {
8180
initializeMobileAdsSdk();
8281
}
83-
// [START_EXCLUDE]
8482

85-
// [START add_privacy_options]
8683
if (googleMobileAdsConsentManager.isPrivacyOptionsRequired()) {
8784
// Regenerate the options menu to include a privacy setting.
8885
invalidateOptionsMenu();
8986
}
90-
// [END add_privacy_options]
91-
// [END_EXCLUDE]
9287
});
9388

9489
// This sample attempts to load ads using consent obtained in the previous session.
9590
if (googleMobileAdsConsentManager.canRequestAds()) {
9691
initializeMobileAdsSdk();
9792
}
98-
// [END can_request_ads]
9993
}
10094

10195
@Override
@@ -188,18 +182,17 @@ private void loadBanner() {
188182
// [END load_ad]
189183
}
190184

191-
// [START request_ads]
192185
private void initializeMobileAdsSdk() {
193186
if (isMobileAdsInitializeCalled.getAndSet(true)) {
194187
return;
195188
}
196-
// [START_EXCLUDE silent] Hide from developer docs code snippet
189+
197190
// Set your test devices.
198191
MobileAds.setRequestConfiguration(
199192
new RequestConfiguration.Builder()
200193
.setTestDeviceIds(Arrays.asList(TEST_DEVICE_HASHED_ID))
201194
.build());
202-
// [END_EXCLUDE]
195+
203196
new Thread(
204197
() -> {
205198
// Initialize the Google Mobile Ads SDK on a background thread.
@@ -211,8 +204,6 @@ private void initializeMobileAdsSdk() {
211204
.start();
212205
}
213206

214-
// [END request_ads]
215-
216207
// [START get_ad_size]
217208
// Get the ad size with screen width.
218209
public AdSize getAdSize() {

kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/GoogleMobileAdsConsentManager.kt

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,39 @@ class GoogleMobileAdsConsentManager private constructor(context: Context) {
6969

7070
val params = ConsentRequestParameters.Builder().setConsentDebugSettings(debugSettings).build()
7171

72-
// [START gather_consent]
72+
// [START request_consent_info_update]
7373
// Requesting an update to consent information should be called on every app launch.
7474
consentInformation.requestConsentInfoUpdate(
7575
activity,
7676
params,
7777
{
78-
UserMessagingPlatform.loadAndShowConsentFormIfRequired(activity) { formError ->
79-
// Consent has been gathered.
80-
onConsentGatheringCompleteListener.consentGatheringComplete(formError)
81-
}
78+
// Called when consent information is successfully updated.
79+
// [START_EXCLUDE silent]
80+
loadAndShowConsentFormIfRequired(activity, onConsentGatheringCompleteListener)
81+
// [END_EXCLUDE]
8282
},
8383
{ requestConsentError ->
84+
// Called when there's an error updating consent information.
85+
// [START_EXCLUDE silent]
8486
onConsentGatheringCompleteListener.consentGatheringComplete(requestConsentError)
87+
// [END_EXCLUDE]
8588
},
8689
)
87-
// [END gather_consent]
90+
// [END request_consent_info_update]
91+
}
92+
93+
private fun loadAndShowConsentFormIfRequired(
94+
activity: Activity,
95+
onConsentGatheringCompleteListener: OnConsentGatheringCompleteListener,
96+
) {
97+
// [START load_and_show_consent_form]
98+
UserMessagingPlatform.loadAndShowConsentFormIfRequired(activity) { formError ->
99+
// Consent gathering process is complete.
100+
// [START_EXCLUDE silent]
101+
onConsentGatheringCompleteListener.consentGatheringComplete(formError)
102+
// [END_EXCLUDE]
103+
}
104+
// [END load_and_show_consent_form]
88105
}
89106

90107
/** Helper method to call the UMP SDK method to show the privacy options form. */

kotlin/admob/BannerExample/app/src/main/java/com/google/android/gms/example/bannerexample/MainActivity.kt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ class MainActivity : AppCompatActivity() {
7373
Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion())
7474

7575
googleMobileAdsConsentManager = GoogleMobileAdsConsentManager.getInstance(applicationContext)
76-
// [START can_request_ads]
7776
googleMobileAdsConsentManager.gatherConsent(this) { error ->
7877
if (error != null) {
7978
// Consent not obtained in current session.
@@ -83,15 +82,11 @@ class MainActivity : AppCompatActivity() {
8382
if (googleMobileAdsConsentManager.canRequestAds) {
8483
initializeMobileAdsSdk()
8584
}
86-
// [START_EXCLUDE]
8785

88-
// [START add_privacy_options]
8986
if (googleMobileAdsConsentManager.isPrivacyOptionsRequired) {
9087
// Regenerate the options menu to include a privacy setting.
9188
invalidateOptionsMenu()
9289
}
93-
// [END add_privacy_options]
94-
// [END_EXCLUDE]
9590
}
9691

9792
// This sample attempts to load ads using consent obtained in the previous session.
@@ -178,17 +173,16 @@ class MainActivity : AppCompatActivity() {
178173
// [END load_ad]
179174
}
180175

181-
// [START request_ads]
182176
private fun initializeMobileAdsSdk() {
183177
if (isMobileAdsInitializeCalled.getAndSet(true)) {
184178
return
185179
}
186-
// [START_EXCLUDE silent] // [START_EXCLUDE silent] Hide from developer docs code snippet
180+
187181
// Set your test devices.
188182
MobileAds.setRequestConfiguration(
189183
RequestConfiguration.Builder().setTestDeviceIds(listOf(TEST_DEVICE_HASHED_ID)).build()
190184
)
191-
// [END_EXCLUDE]
185+
192186
CoroutineScope(Dispatchers.IO).launch {
193187
// Initialize the Google Mobile Ads SDK on a background thread.
194188
MobileAds.initialize(this@MainActivity) {}
@@ -200,8 +194,6 @@ class MainActivity : AppCompatActivity() {
200194
}
201195
}
202196

203-
// [END request_ads]
204-
205197
companion object {
206198
// This is an ad unit ID for a test ad. Replace with your own banner ad unit ID.
207199
private const val AD_UNIT_ID = "ca-app-pub-3940256099942544/9214589741"

0 commit comments

Comments
 (0)