Skip to content

Commit

Permalink
Updated GoogleMobileAdsConsentManager to be a singleton for AdMob Jav…
Browse files Browse the repository at this point in the history
…a samples

PiperOrigin-RevId: 574241621
  • Loading branch information
Justin Malandruccolo authored and maddevrelgithubbot committed Oct 17, 2023
1 parent 62f557d commit 54244d8
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.google.android.gms.example.adaptivebannerexample;

import android.app.Activity;
import androidx.annotation.NonNull;
import android.content.Context;
import com.google.android.ump.ConsentDebugSettings;
import com.google.android.ump.ConsentForm.OnConsentFormDismissedListener;
import com.google.android.ump.ConsentInformation;
Expand All @@ -16,21 +16,30 @@
* consent for users in GDPR impacted countries. This is an example and
* you can choose another consent management platform to capture consent.
*/
@SuppressWarnings("NonFinalStaticField")
public class GoogleMobileAdsConsentManager {
private final Activity activity;
private static GoogleMobileAdsConsentManager instance;
private final ConsentInformation consentInformation;

/** Private constructor. */
private GoogleMobileAdsConsentManager(Context context) {
this.consentInformation = UserMessagingPlatform.getConsentInformation(context);
}

/** Public constructor. */
public static GoogleMobileAdsConsentManager getInstance(Context context) {
if (instance == null) {
instance = new GoogleMobileAdsConsentManager(context);
}

return instance;
}

/** Interface definition for a callback to be invoked when consent gathering is complete. */
public interface OnConsentGatheringCompleteListener {
void consentGatheringComplete(FormError error);
}

/** Constructor */
public GoogleMobileAdsConsentManager(@NonNull Activity activity) {
this.activity = activity;
this.consentInformation = UserMessagingPlatform.getConsentInformation(activity);
}

/** Helper variable to determine if the app can request ads. */
public boolean canRequestAds() {
return consentInformation.canRequestAds();
Expand All @@ -42,10 +51,12 @@ public boolean isPrivacyOptionsRequired() {
== PrivacyOptionsRequirementStatus.REQUIRED;
}

/** Helper method to call the UMP SDK methods to request consent information and load/present a
* consent form if necessary. */
/**
* Helper method to call the UMP SDK methods to request consent information and load/present a
* consent form if necessary.
*/
public void gatherConsent(
OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) {
Activity activity, OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) {
// For testing purposes, you can force a DebugGeography of EEA or NOT_EEA.
ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(activity)
// Uncomment the line below to set DebugGeography to EEA.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ protected void onCreate(Bundle savedInstanceState) {
// Log the Mobile Ads SDK version.
Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion());

googleMobileAdsConsentManager = new GoogleMobileAdsConsentManager(this);
googleMobileAdsConsentManager =
GoogleMobileAdsConsentManager.getInstance(getApplicationContext());
googleMobileAdsConsentManager.gatherConsent(
this,
consentError -> {
if (consentError != null) {
// Consent not obtained in current session.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,22 @@
/** Main activity in the app. */
public class MainActivity extends AppCompatActivity {

private GoogleMobileAdsConsentManager googleMobileAdsConsentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

googleMobileAdsConsentManager =
GoogleMobileAdsConsentManager.getInstance(getApplicationContext());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_menu, menu);
MenuItem moreMenu = menu.findItem(R.id.action_more);
moreMenu.setVisible(GoogleMobileAdsConsentManager.getInstance(this).isPrivacyOptionsRequired());
moreMenu.setVisible(googleMobileAdsConsentManager.isPrivacyOptionsRequired());
return true;
}

Expand All @@ -50,14 +55,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
popupMenuItem -> {
if (popupMenuItem.getItemId() == R.id.privacy_settings) {
// Handle changes to user consent.
GoogleMobileAdsConsentManager.getInstance(this)
.showPrivacyOptionsForm(
this,
formError -> {
if (formError != null) {
Toast.makeText(this, formError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
googleMobileAdsConsentManager.showPrivacyOptionsForm(
this,
formError -> {
if (formError != null) {
Toast.makeText(this, formError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ private class AppOpenAdManager {
private static final String LOG_TAG = "AppOpenAdManager";
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/3419835294";

private final GoogleMobileAdsConsentManager googleMobileAdsConsentManager =
GoogleMobileAdsConsentManager.getInstance(getApplicationContext());
private AppOpenAd appOpenAd = null;
private boolean isLoadingAd = false;
private boolean isShowingAd = false;
Expand Down Expand Up @@ -237,7 +239,7 @@ private void showAdIfAvailable(
if (!isAdAvailable()) {
Log.d(LOG_TAG, "The app open ad is not ready yet.");
onShowAdCompleteListener.onShowAdComplete();
if (GoogleMobileAdsConsentManager.getInstance(activity).canRequestAds()) {
if (googleMobileAdsConsentManager.canRequestAds()) {
loadAd(currentActivity);
}
return;
Expand All @@ -258,7 +260,7 @@ public void onAdDismissedFullScreenContent() {
Toast.makeText(activity, "onAdDismissedFullScreenContent", Toast.LENGTH_SHORT).show();

onShowAdCompleteListener.onShowAdComplete();
if (GoogleMobileAdsConsentManager.getInstance(activity).canRequestAds()) {
if (googleMobileAdsConsentManager.canRequestAds()) {
loadAd(activity);
}
}
Expand All @@ -274,7 +276,7 @@ public void onAdFailedToShowFullScreenContent(AdError adError) {
.show();

onShowAdCompleteListener.onShowAdComplete();
if (GoogleMobileAdsConsentManager.getInstance(activity).canRequestAds()) {
if (googleMobileAdsConsentManager.canRequestAds()) {
loadAd(activity);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
public class SplashActivity extends AppCompatActivity {
private static final String LOG_TAG = "SplashActivity";
private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
private GoogleMobileAdsConsentManager googleMobileAdsConsentManager;

/**
* Number of milliseconds to count down before showing the app open ad. This simulates the time
Expand All @@ -48,29 +49,30 @@ protected void onCreate(Bundle savedInstanceState) {
// Create a timer so the SplashActivity will be displayed for a fixed amount of time.
createTimer(COUNTER_TIME_MILLISECONDS);

GoogleMobileAdsConsentManager.getInstance(this)
.gatherConsent(
this,
consentError -> {
if (consentError != null) {
// Consent not obtained in current session.
Log.w(
LOG_TAG,
String.format(
"%s: %s", consentError.getErrorCode(), consentError.getMessage()));
}

if (GoogleMobileAdsConsentManager.getInstance(this).canRequestAds()) {
initializeMobileAdsSdk();
}

if (secondsRemaining <= 0) {
startMainActivity();
}
});
googleMobileAdsConsentManager =
GoogleMobileAdsConsentManager.getInstance(getApplicationContext());
googleMobileAdsConsentManager.gatherConsent(
this,
consentError -> {
if (consentError != null) {
// Consent not obtained in current session.
Log.w(
LOG_TAG,
String.format(
"%s: %s", consentError.getErrorCode(), consentError.getMessage()));
}

if (googleMobileAdsConsentManager.canRequestAds()) {
initializeMobileAdsSdk();
}

if (secondsRemaining <= 0) {
startMainActivity();
}
});

// This sample attempts to load ads using consent obtained in the previous session.
if (GoogleMobileAdsConsentManager.getInstance(this).canRequestAds()) {
if (googleMobileAdsConsentManager.canRequestAds()) {
initializeMobileAdsSdk();
}
}
Expand Down Expand Up @@ -106,8 +108,7 @@ public void onShowAdComplete() {
// Check if the consent form is currently on screen before moving to the
// main
// activity.
if (GoogleMobileAdsConsentManager.getInstance(SplashActivity.this)
.canRequestAds()) {
if (googleMobileAdsConsentManager.canRequestAds()) {
startMainActivity();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public class MyActivity extends AppCompatActivity {

private static final String TAG = "MyActivity";
private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false);
private final GoogleMobileAdsConsentManager googleMobileAdsConsentManager =
GoogleMobileAdsConsentManager.getInstance(getApplicationContext());
private GoogleMobileAdsConsentManager googleMobileAdsConsentManager;
private AdView adView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -60,6 +60,8 @@ protected void onCreate(Bundle savedInstanceState) {
// values/strings.xml.
adView = findViewById(R.id.ad_view);

googleMobileAdsConsentManager =
GoogleMobileAdsConsentManager.getInstance(getApplicationContext());
googleMobileAdsConsentManager.gatherConsent(
this,
consentError -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.google.android.gms.example.interstitialexample;

import android.app.Activity;
import androidx.annotation.NonNull;
import android.content.Context;
import com.google.android.ump.ConsentDebugSettings;
import com.google.android.ump.ConsentForm.OnConsentFormDismissedListener;
import com.google.android.ump.ConsentInformation;
Expand All @@ -16,21 +16,30 @@
* consent for users in GDPR impacted countries. This is an example and
* you can choose another consent management platform to capture consent.
*/
@SuppressWarnings("NonFinalStaticField")
public class GoogleMobileAdsConsentManager {
private final Activity activity;
private static GoogleMobileAdsConsentManager instance;
private final ConsentInformation consentInformation;

/** Private constructor. */
private GoogleMobileAdsConsentManager(Context context) {
this.consentInformation = UserMessagingPlatform.getConsentInformation(context);
}

/** Public constructor. */
public static GoogleMobileAdsConsentManager getInstance(Context context) {
if (instance == null) {
instance = new GoogleMobileAdsConsentManager(context);
}

return instance;
}

/** Interface definition for a callback to be invoked when consent gathering is complete. */
public interface OnConsentGatheringCompleteListener {
void consentGatheringComplete(FormError error);
}

/** Constructor */
public GoogleMobileAdsConsentManager(@NonNull Activity activity) {
this.activity = activity;
this.consentInformation = UserMessagingPlatform.getConsentInformation(activity);
}

/** Helper variable to determine if the app can request ads. */
public boolean canRequestAds() {
return consentInformation.canRequestAds();
Expand All @@ -42,10 +51,12 @@ public boolean isPrivacyOptionsRequired() {
== PrivacyOptionsRequirementStatus.REQUIRED;
}

/** Helper method to call the UMP SDK methods to request consent information and load/present a
* consent form if necessary. */
/**
* Helper method to call the UMP SDK methods to request consent information and load/present a
* consent form if necessary.
*/
public void gatherConsent(
OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) {
Activity activity, OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) {
// For testing purposes, you can force a DebugGeography of EEA or NOT_EEA.
ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(activity)
// .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ protected void onCreate(Bundle savedInstanceState) {
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});

googleMobileAdsConsentManager = new GoogleMobileAdsConsentManager(this);
googleMobileAdsConsentManager =
GoogleMobileAdsConsentManager.getInstance(getApplicationContext());
googleMobileAdsConsentManager.gatherConsent(
this,
consentError -> {
if (consentError != null) {
// Consent not obtained in current session.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.google.example.gms.nativeadvancedexample;

import android.app.Activity;
import androidx.annotation.NonNull;
import android.content.Context;
import com.google.android.ump.ConsentDebugSettings;
import com.google.android.ump.ConsentForm.OnConsentFormDismissedListener;
import com.google.android.ump.ConsentInformation;
Expand All @@ -16,21 +16,30 @@
* consent for users in GDPR impacted countries. This is an example and
* you can choose another consent management platform to capture consent.
*/
@SuppressWarnings("NonFinalStaticField")
public class GoogleMobileAdsConsentManager {
private final Activity activity;
private static GoogleMobileAdsConsentManager instance;
private final ConsentInformation consentInformation;

/** Private constructor. */
private GoogleMobileAdsConsentManager(Context context) {
this.consentInformation = UserMessagingPlatform.getConsentInformation(context);
}

/** Public constructor. */
public static GoogleMobileAdsConsentManager getInstance(Context context) {
if (instance == null) {
instance = new GoogleMobileAdsConsentManager(context);
}

return instance;
}

/** Interface definition for a callback to be invoked when consent gathering is complete. */
public interface OnConsentGatheringCompleteListener {
void consentGatheringComplete(FormError error);
}

/** Constructor */
public GoogleMobileAdsConsentManager(@NonNull Activity activity) {
this.activity = activity;
this.consentInformation = UserMessagingPlatform.getConsentInformation(activity);
}

/** Helper variable to determine if the app can request ads. */
public boolean canRequestAds() {
return consentInformation.canRequestAds();
Expand All @@ -42,10 +51,12 @@ public boolean isPrivacyOptionsRequired() {
== PrivacyOptionsRequirementStatus.REQUIRED;
}

/** Helper method to call the UMP SDK methods to request consent information and load/present a
* consent form if necessary. */
/**
* Helper method to call the UMP SDK methods to request consent information and load/present a
* consent form if necessary.
*/
public void gatherConsent(
OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) {
Activity activity, OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) {
// For testing purposes, you can force a DebugGeography of EEA or NOT_EEA.
ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(activity)
// .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ protected void onCreate(Bundle savedInstanceState) {
// Log the Mobile Ads SDK version.
Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion());

googleMobileAdsConsentManager = new GoogleMobileAdsConsentManager(this);
googleMobileAdsConsentManager =
GoogleMobileAdsConsentManager.getInstance(getApplicationContext());
googleMobileAdsConsentManager.gatherConsent(
this,
consentError -> {
if (consentError != null) {
// Consent not obtained in current session.
Expand Down
Loading

0 comments on commit 54244d8

Please sign in to comment.