2 * Copyright © 2016-2020 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.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;
30 import androidx.fragment.app.DialogFragment;
31 import androidx.fragment.app.FragmentManager;
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;
39 import com.stoutner.privacybrowser.R;
40 import com.stoutner.privacybrowser.dialogs.AdConsentDialog;
42 public class AdHelper {
43 private static boolean initialized;
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);
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);
53 // Check to see if consent has been granted.
54 boolean adConsentGranted = adConsentDatabaseHelper.isGranted();
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();
61 // Display the ad consent dialog.
62 adConsentDialogFragment.show(fragmentManager, "Ad Consent");
63 } else { // Ad consent has been granted.
65 loadAd(view, applicationContext, activity, adUnitId);
68 // Set the initialized variable to true so this section doesn't run again.
70 } else { // Ads have previously been initialized.
72 loadAd(view, applicationContext, activity, adUnitId);
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;
80 // Save the layout parameters. They are used when programatically recreating the ad below.
81 RelativeLayout.LayoutParams adViewLayoutParameters = (RelativeLayout.LayoutParams) adView.getLayoutParams();
83 // Get a handle for the ad view parent.
84 RelativeLayout adViewParentLayout = (RelativeLayout) adView.getParent();
87 adViewParentLayout.removeView(adView);
89 // Create a new AdView. This is necessary because the size can change when the device is rotated.
90 adView = new AdView(applicationContext);
92 // Set the ad unit ID.
93 adView.setAdUnitId(adUnitId);
96 adView.setId(R.id.adview);
98 // Set the layout parameters.
99 adView.setLayoutParams(adViewLayoutParameters);
101 // Add the new ad view to the parent layout.
102 adViewParentLayout.addView(adView);
104 // Get a handle for the display.
105 Display display = activity.getWindowManager().getDefaultDisplay();
107 // Initialize a display metrics.
108 DisplayMetrics displayMetrics = new DisplayMetrics();
110 // Get the display metrics from the display.
111 display.getMetrics(displayMetrics);
113 // Get the width pixels and the density.
114 float widthPixels = displayMetrics.widthPixels;
115 float density = displayMetrics.density;
117 // Calculate the ad width.
118 int adWidth = (int) (widthPixels / density);
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);
123 // Set the ad size on the adView.
124 adView.setAdSize(adSize);
126 // Create an ad settings bundle.
127 Bundle adSettingsBundle = new Bundle();
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");
132 // Build the ad request.
133 AdRequest adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
136 adView.loadAd(adRequest);
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;
145 adView.setVisibility(View.GONE);
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;
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;
162 // Resume the AdView.