2 * Copyright © 2016-2018 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.helpers;
22 import android.app.DialogFragment;
23 import android.app.FragmentManager;
24 import android.content.Context;
25 import android.os.Bundle;
26 import android.view.View;
27 import android.widget.RelativeLayout;
29 import com.google.ads.consent.ConsentInfoUpdateListener;
30 import com.google.ads.consent.ConsentInformation;
31 import com.google.ads.consent.ConsentStatus;
32 import com.google.ads.mediation.admob.AdMobAdapter;
33 import com.google.android.gms.ads.AdRequest;
34 import com.google.android.gms.ads.AdSize;
35 import com.google.android.gms.ads.AdView;
36 import com.google.android.gms.ads.MobileAds;
37 import com.stoutner.privacybrowser.R;
38 import com.stoutner.privacybrowser.dialogs.AdConsentDialog;
40 public class AdHelper {
41 private static boolean initialized;
43 public static void initializeAds (View view, Context applicationContext, FragmentManager fragmentManager, String adId) {
44 if (!initialized) { // This is the first run.
45 // Initialize mobile ads.
46 MobileAds.initialize(applicationContext, adId);
48 // Store the publisher ID in a string array.
49 String[] publisherIds = {"pub-5962503714887045"};
51 // Check to see if consent is needed in Europe to comply with the GDPR.
52 ConsentInformation consentInformation = ConsentInformation.getInstance(applicationContext);
53 consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
55 public void onConsentInfoUpdated(ConsentStatus consentStatus) {
56 if (consentStatus == ConsentStatus.UNKNOWN) { // The user has not yet consented to ads.
57 // Display the ad consent dialog.
58 DialogFragment adConsentDialogFragment = new AdConsentDialog();
59 adConsentDialogFragment.show(fragmentManager, "Ad Consent");
60 } else { // The user has consented to ads.
61 // Indicate the user is under age, which disables personalized advertising and remarketing. https://developers.google.com/admob/android/eu-consent
62 consentInformation.setTagForUnderAgeOfConsent(true);
65 loadAd(view, applicationContext, adId);
70 public void onFailedToUpdateConsentInfo(String reason) { // The user is not in Europe.
71 // Indicate the user is under age, which disables personalized advertising and remarketing. https://developers.google.com/admob/android/eu-consent
72 consentInformation.setTagForUnderAgeOfConsent(true);
75 loadAd(view, applicationContext, adId);
79 // Set the initialized variable to true so this section doesn't run again.
81 } else { // Ads have previously been initialized.
83 loadAd(view, applicationContext, adId);
87 public static void loadAd (View view, Context applicationContext, String adId) {
88 // Cast the generic view to an AdView.
89 AdView adView = (AdView) view;
91 // Save the layout parameters. They are used when programatically recreating the add below.
92 RelativeLayout.LayoutParams adViewLayoutParameters = (RelativeLayout.LayoutParams) adView.getLayoutParams();
95 RelativeLayout adViewParentLayout = (RelativeLayout) adView.getParent();
96 adViewParentLayout.removeView(adView);
98 // Setup the new AdView. This is necessary because the size of the banner ad can change on rotate.
99 adView = new AdView(applicationContext);
100 adView.setAdSize(AdSize.SMART_BANNER);
101 adView.setAdUnitId(adId);
102 adView.setId(R.id.adview);
103 adView.setLayoutParams(adViewLayoutParameters);
105 // Display the new AdView.
106 adViewParentLayout.addView(adView);
108 // Only request non-personalized ads.
109 Bundle adSettingsBundle = new Bundle();
110 adSettingsBundle.putString("npa", "1");
113 AdRequest adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
114 adView.loadAd(adRequest);
117 public static void hideAd(View view) {
118 // Cast the generic view to an AdView.
119 AdView adView = (AdView) view;
122 adView.setVisibility(View.GONE);
125 public static void pauseAd(View view) {
126 // Cast The generic view to an AdView.
127 AdView adView = (AdView) view;
133 public static void resumeAd(View view) {
134 // Cast the generic view to an AdView.
135 AdView adView = (AdView) view;
137 // Resume the AdView.