]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/free/java/com/stoutner/privacybrowser/helpers/AdHelper.java
Release 3.8.
[PrivacyBrowserAndroid.git] / app / src / free / java / com / stoutner / privacybrowser / helpers / AdHelper.java
1 /*
2  * Copyright © 2016-2020 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.Activity;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.util.DisplayMetrics;
26 import android.view.Display;
27 import android.view.View;
28 import android.widget.RelativeLayout;
29
30 import androidx.fragment.app.DialogFragment;
31 import androidx.fragment.app.FragmentManager;
32
33 import com.google.ads.mediation.admob.AdMobAdapter;
34 import com.google.android.gms.ads.AdRequest;
35 import com.google.android.gms.ads.AdSize;
36 import com.google.android.gms.ads.AdView;
37 import com.google.android.gms.ads.MobileAds;
38
39 import com.stoutner.privacybrowser.R;
40 import com.stoutner.privacybrowser.dialogs.AdConsentDialog;
41
42 public class AdHelper {
43     private static boolean initialized;
44
45     public static void initializeAds(View view, Context applicationContext, Activity activity, FragmentManager fragmentManager, String adUnitId) {
46         if (!initialized) {  // This is the first run.
47             // Initialize mobile ads.
48             MobileAds.initialize(applicationContext);
49
50             // Initialize the bookmarks database helper.  The `0` specifies a database version, but that is ignored and set instead using a constant in `AdConsentDatabaseHelper`.
51             AdConsentDatabaseHelper adConsentDatabaseHelper = new AdConsentDatabaseHelper(applicationContext, null, null, 0);
52
53             // Check to see if consent has been granted.
54             boolean adConsentGranted = adConsentDatabaseHelper.isGranted();
55
56             // Display the ad consent dialog if needed.
57             if (!adConsentGranted) {  // Ad consent has not been granted.
58                 // Instantiate the ad consent dialog.
59                 DialogFragment adConsentDialogFragment = new AdConsentDialog();
60
61                 // Display the ad consent dialog.
62                 adConsentDialogFragment.show(fragmentManager, "Ad Consent");
63             } else {  // Ad consent has been granted.
64                 // Load an ad.
65                 loadAd(view, applicationContext, activity, adUnitId);
66             }
67
68             // Set the initialized variable to true so this section doesn't run again.
69             initialized = true;
70         } else {  // Ads have previously been initialized.
71             // Load an ad.
72             loadAd(view, applicationContext, activity, adUnitId);
73         }
74     }
75
76     public static void loadAd(View view, Context applicationContext, Activity activity, String adUnitId) {
77         // Cast the generic view to an AdView.
78         AdView adView = (AdView) view;
79
80         // Save the layout parameters.  They are used when programatically recreating the ad below.
81         RelativeLayout.LayoutParams adViewLayoutParameters = (RelativeLayout.LayoutParams) adView.getLayoutParams();
82
83         // Get a handle for the ad view parent.
84         RelativeLayout adViewParentLayout = (RelativeLayout) adView.getParent();
85
86         // Remove the AdView.
87         adViewParentLayout.removeView(adView);
88
89         // Create a new AdView.  This is necessary because the size can change when the device is rotated.
90         adView = new AdView(applicationContext);
91
92         // Set the ad unit ID.
93         adView.setAdUnitId(adUnitId);
94
95         //  Set the view ID.
96         adView.setId(R.id.adview);
97
98         // Set the layout parameters.
99         adView.setLayoutParams(adViewLayoutParameters);
100
101         // Add the new ad view to the parent layout.
102         adViewParentLayout.addView(adView);
103
104         // Get a handle for the display.
105         Display display = activity.getWindowManager().getDefaultDisplay();
106
107         // Initialize a display metrics.
108         DisplayMetrics displayMetrics = new DisplayMetrics();
109
110         // Get the display metrics from the display.
111         display.getMetrics(displayMetrics);
112
113         // Get the width pixels and the density.
114         float widthPixels = displayMetrics.widthPixels;
115         float density = displayMetrics.density;
116
117         // Calculate the ad width.
118         int adWidth = (int) (widthPixels / density);
119
120         // Get the ad size.  This line should be enabled once Firebase Ads is updated to 20.0.0.
121         AdSize adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(applicationContext, adWidth);
122
123         // Set the ad size on the adView.
124         adView.setAdSize(adSize);
125
126         // Create an ad settings bundle.
127         Bundle adSettingsBundle = new Bundle();
128
129         // 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
130         adSettingsBundle.putString("npa", "1");
131
132         // Build the ad request.
133         AdRequest adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
134
135         // Make it so.
136         adView.loadAd(adRequest);
137     }
138
139     // This method exists here for the sake of consistency with the following two methods.
140     public static void hideAd(View view) {
141         // Cast the generic view to an AdView.
142         AdView adView = (AdView) view;
143
144         // Hide the ad.
145         adView.setVisibility(View.GONE);
146     }
147
148     // This method exists here so that the main WebView activity doesn't need to import `com.google.android.gms.ads.AdView`.
149     public static void pauseAd(View view) {
150         // Cast The generic view to an AdView.
151         AdView adView = (AdView) view;
152
153         // Pause the AdView.
154         adView.pause();
155     }
156
157     // This method exists here so that the main WebView activity doesn't need to import `com.google.android.gms.ads.AdView`.
158     public static void resumeAd(View view) {
159         // Cast the generic view to an AdView.
160         AdView adView = (AdView) view;
161
162         // Resume the AdView.
163         adView.resume();
164     }
165 }