]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/free/java/com/stoutner/privacybrowser/helpers/AdHelper.java
Migrate to AndroidX from the Android Support Library. https://redmine.stoutner.com...
[PrivacyBrowserAndroid.git] / app / src / free / java / com / stoutner / privacybrowser / helpers / AdHelper.java
index d1638c7386582faab50cf7c40ba83ddd86e57686..c3aae5d4acdf50a01b3036e413bb13de96f238ae 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2016-2018 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
 
 package com.stoutner.privacybrowser.helpers;
 
-import android.app.DialogFragment;
-import android.app.FragmentManager;
 import android.content.Context;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.RelativeLayout;
 
-import com.google.ads.consent.ConsentInfoUpdateListener;
-import com.google.ads.consent.ConsentInformation;
-import com.google.ads.consent.ConsentStatus;
+import androidx.fragment.app.DialogFragment;
+import androidx.fragment.app.FragmentManager;
+
 import com.google.ads.mediation.admob.AdMobAdapter;
 import com.google.android.gms.ads.AdRequest;
 import com.google.android.gms.ads.AdSize;
@@ -40,41 +38,26 @@ import com.stoutner.privacybrowser.dialogs.AdConsentDialog;
 public class AdHelper {
     private static boolean initialized;
 
-    public static void initializeAds (View view, Context applicationContext, FragmentManager fragmentManager, String googleAppId, String adUnitId) {
+    public static void initializeAds(View view, Context applicationContext, FragmentManager fragmentManager, String googleAppId, String adUnitId) {
         if (!initialized) {  // This is the first run.
             // Initialize mobile ads.
             MobileAds.initialize(applicationContext, googleAppId);
 
-            // Store the publisher ID in a string array.
-            String[] publisherIds = {"pub-5962503714887045"};
-
-            // Check to see if consent is needed in Europe to comply with the GDPR.
-            ConsentInformation consentInformation = ConsentInformation.getInstance(applicationContext);
-            consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
-                @Override
-                public void onConsentInfoUpdated(ConsentStatus consentStatus) {
-                    if (consentStatus == ConsentStatus.UNKNOWN) {  // The user has not yet consented to ads.
-                        // Display the ad consent dialog.
-                        DialogFragment adConsentDialogFragment = new AdConsentDialog();
-                        adConsentDialogFragment.show(fragmentManager, "Ad Consent");
-                    } else {  // The user has consented to ads.
-                        // Indicate the user is under age, which disables personalized advertising and remarketing.  https://developers.google.com/admob/android/eu-consent
-                        consentInformation.setTagForUnderAgeOfConsent(true);
-
-                        // Load an ad.
-                        loadAd(view, applicationContext, adUnitId);
-                    }
-                }
-
-                @Override
-                public void onFailedToUpdateConsentInfo(String reason) {  // The user is not in Europe.
-                    // Indicate the user is under age, which disables personalized advertising and remarketing.  https://developers.google.com/admob/android/eu-consent
-                    consentInformation.setTagForUnderAgeOfConsent(true);
-
-                    // Load an ad.
-                    loadAd(view, applicationContext, adUnitId);
-                }
-            });
+            // Initialize the bookmarks database helper.  The `0` specifies a database version, but that is ignored and set instead using a constant in `AdConsentDatabaseHelper`.
+            AdConsentDatabaseHelper adConsentDatabaseHelper = new AdConsentDatabaseHelper(applicationContext, null, null, 0);
+
+            // Check to see if consent has been granted.
+            boolean adConsentGranted = adConsentDatabaseHelper.isGranted();
+
+            // Display the ad consent dialog if needed.
+            if (!adConsentGranted) {  // Ad consent has not been granted.
+                // Display the ad consent dialog.
+                DialogFragment adConsentDialogFragment = new AdConsentDialog();
+                adConsentDialogFragment.show(fragmentManager, "Ad Consent");
+            } else {  // Ad consent has been granted.
+                // Load an ad.
+                loadAd(view, applicationContext, adUnitId);
+            }
 
             // Set the initialized variable to true so this section doesn't run again.
             initialized = true;
@@ -84,7 +67,7 @@ public class AdHelper {
         }
     }
 
-    public static void loadAd (View view, Context applicationContext, String adUnitId) {
+    public static void loadAd(View view, Context applicationContext, String adUnitId) {
         // Cast the generic view to an AdView.
         AdView adView = (AdView) view;
 
@@ -105,16 +88,14 @@ public class AdHelper {
         // Display the new AdView.
         adViewParentLayout.addView(adView);
 
-        // Only request non-personalized ads.
+        // Only request non-personalized ads.  https://developers.google.com/ad-manager/mobile-ads-sdk/android/eu-consent#forward_consent_to_the_google_mobile_ads_sdk
         Bundle adSettingsBundle = new Bundle();
         adSettingsBundle.putString("npa", "1");
 
-        // Request a new ad.
+        // Build the ad request.
         AdRequest adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
-        // Pixel test ads.
-        // AdRequest adRequest = new AdRequest.Builder().addTestDevice("20DAEEF7662E2238C99A509BE5D78A26").addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
-        // Pixel 2 XL test ads.
-        // AdRequest adRequest = new AdRequest.Builder().addTestDevice("137D42984218CEECDFD11927BB7D6416").addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
+
+        // Make it so.
         adView.loadAd(adRequest);
     }