]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/free/java/com/stoutner/privacybrowser/helpers/AdHelper.java
Scroll to the beginning of the URL text box when it loses focus. https://redmine...
[PrivacyBrowserAndroid.git] / app / src / free / java / com / stoutner / privacybrowser / helpers / AdHelper.java
1 /*
2  * Copyright © 2016-2018 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.helpers;
21
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;
28
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;
39
40 public class AdHelper {
41     private static boolean initialized;
42
43     public static void initializeAds (View view, Context applicationContext, FragmentManager fragmentManager, String googleAppId, String adUnitId) {
44         if (!initialized) {  // This is the first run.
45             // Initialize mobile ads.
46             MobileAds.initialize(applicationContext, googleAppId);
47
48             // Store the publisher ID in a string array.
49             String[] publisherIds = {"pub-5962503714887045"};
50
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() {
54                 @Override
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);
63
64                         // Load an ad.
65                         loadAd(view, applicationContext, adUnitId);
66                     }
67                 }
68
69                 @Override
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);
73
74                     // Load an ad.
75                     loadAd(view, applicationContext, adUnitId);
76                 }
77             });
78
79             // Set the initialized variable to true so this section doesn't run again.
80             initialized = true;
81         } else {  // Ads have previously been initialized.
82             // Load an ad.
83             loadAd(view, applicationContext, adUnitId);
84         }
85     }
86
87     public static void loadAd (View view, Context applicationContext, String adUnitId) {
88         // Cast the generic view to an AdView.
89         AdView adView = (AdView) view;
90
91         // Save the layout parameters.  They are used when programatically recreating the add below.
92         RelativeLayout.LayoutParams adViewLayoutParameters = (RelativeLayout.LayoutParams) adView.getLayoutParams();
93
94         // Remove the AdView.
95         RelativeLayout adViewParentLayout = (RelativeLayout) adView.getParent();
96         adViewParentLayout.removeView(adView);
97
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(adUnitId);
102         adView.setId(R.id.adview);
103         adView.setLayoutParams(adViewLayoutParameters);
104
105         // Display the new AdView.
106         adViewParentLayout.addView(adView);
107
108         // Only request non-personalized ads.
109         Bundle adSettingsBundle = new Bundle();
110         adSettingsBundle.putString("npa", "1");
111
112         // Request a new ad.
113         AdRequest adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
114         // Pixel test ads.
115         // AdRequest adRequest = new AdRequest.Builder().addTestDevice("20DAEEF7662E2238C99A509BE5D78A26").addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
116         // Pixel 2 XL test ads.
117         // AdRequest adRequest = new AdRequest.Builder().addTestDevice("137D42984218CEECDFD11927BB7D6416").addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
118         adView.loadAd(adRequest);
119     }
120
121     public static void hideAd(View view) {
122         // Cast the generic view to an AdView.
123         AdView adView = (AdView) view;
124
125         // Hide the ad.
126         adView.setVisibility(View.GONE);
127     }
128
129     public static void pauseAd(View view) {
130         // Cast The generic view to an AdView.
131         AdView adView = (AdView) view;
132
133         // Pause the AdView.
134         adView.pause();
135     }
136
137     public static void resumeAd(View view) {
138         // Cast the generic view to an AdView.
139         AdView adView = (AdView) view;
140
141         // Resume the AdView.
142         adView.resume();
143     }
144 }