]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/free/java/com/stoutner/privacybrowser/helpers/AdHelper.java
7de4e522dec4a6655c6c7e4237e231600115929a
[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.mediation.admob.AdMobAdapter;
30 import com.google.android.gms.ads.AdRequest;
31 import com.google.android.gms.ads.AdSize;
32 import com.google.android.gms.ads.AdView;
33 import com.google.android.gms.ads.MobileAds;
34 import com.stoutner.privacybrowser.R;
35 import com.stoutner.privacybrowser.dialogs.AdConsentDialog;
36
37 public class AdHelper {
38     private static boolean initialized;
39
40     public static void initializeAds (View view, Context applicationContext, FragmentManager fragmentManager, String googleAppId, String adUnitId) {
41         if (!initialized) {  // This is the first run.
42             // Initialize mobile ads.
43             MobileAds.initialize(applicationContext, googleAppId);
44
45             // Initialize the bookmarks database helper.  The `0` specifies a database version, but that is ignored and set instead using a constant in `AdConsentDatabaseHelper`.
46             AdConsentDatabaseHelper adConsentDatabaseHelper = new AdConsentDatabaseHelper(applicationContext, null, null, 0);
47
48             // Check to see if consent has been granted.
49             boolean adConsentGranted = adConsentDatabaseHelper.isGranted();
50
51             // Display the ad consent dialog if needed.
52             if (!adConsentGranted) {  // Ad consent has not been granted.
53                 // Display the ad consent dialog.
54                 DialogFragment adConsentDialogFragment = new AdConsentDialog();
55                 adConsentDialogFragment.show(fragmentManager, "Ad Consent");
56             } else {  // Ad consent has been granted.
57                 // Load an ad.
58                 loadAd(view, applicationContext, adUnitId);
59             }
60
61             // Set the initialized variable to true so this section doesn't run again.
62             initialized = true;
63         } else {  // Ads have previously been initialized.
64             // Load an ad.
65             loadAd(view, applicationContext, adUnitId);
66         }
67     }
68
69     public static void loadAd (View view, Context applicationContext, String adUnitId) {
70         // Cast the generic view to an AdView.
71         AdView adView = (AdView) view;
72
73         // Save the layout parameters.  They are used when programatically recreating the add below.
74         RelativeLayout.LayoutParams adViewLayoutParameters = (RelativeLayout.LayoutParams) adView.getLayoutParams();
75
76         // Remove the AdView.
77         RelativeLayout adViewParentLayout = (RelativeLayout) adView.getParent();
78         adViewParentLayout.removeView(adView);
79
80         // Setup the new AdView.  This is necessary because the size of the banner ad can change on rotate.
81         adView = new AdView(applicationContext);
82         adView.setAdSize(AdSize.SMART_BANNER);
83         adView.setAdUnitId(adUnitId);
84         adView.setId(R.id.adview);
85         adView.setLayoutParams(adViewLayoutParameters);
86
87         // Display the new AdView.
88         adViewParentLayout.addView(adView);
89
90         // 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
91         Bundle adSettingsBundle = new Bundle();
92         adSettingsBundle.putString("npa", "1");
93
94         // Build the ad request.
95         AdRequest adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
96
97         // Make it so.
98         adView.loadAd(adRequest);
99     }
100
101     public static void hideAd(View view) {
102         // Cast the generic view to an AdView.
103         AdView adView = (AdView) view;
104
105         // Hide the ad.
106         adView.setVisibility(View.GONE);
107     }
108
109     public static void pauseAd(View view) {
110         // Cast The generic view to an AdView.
111         AdView adView = (AdView) view;
112
113         // Pause the AdView.
114         adView.pause();
115     }
116
117     public static void resumeAd(View view) {
118         // Cast the generic view to an AdView.
119         AdView adView = (AdView) view;
120
121         // Resume the AdView.
122         adView.resume();
123     }
124 }