2 * Copyright © 2016-2019 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.content.Context;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.widget.RelativeLayout;
27 import androidx.fragment.app.DialogFragment;
28 import androidx.fragment.app.FragmentManager;
30 import com.google.ads.mediation.admob.AdMobAdapter;
31 import com.google.android.gms.ads.AdRequest;
32 import com.google.android.gms.ads.AdSize;
33 import com.google.android.gms.ads.AdView;
34 import com.google.android.gms.ads.MobileAds;
35 import com.stoutner.privacybrowser.R;
36 import com.stoutner.privacybrowser.dialogs.AdConsentDialog;
38 public class AdHelper {
39 private static boolean initialized;
41 public static void initializeAds(View view, Context applicationContext, FragmentManager fragmentManager, String googleAppId, String adUnitId) {
42 if (!initialized) { // This is the first run.
43 // Initialize mobile ads.
44 MobileAds.initialize(applicationContext, googleAppId);
46 // Initialize the bookmarks database helper. The `0` specifies a database version, but that is ignored and set instead using a constant in `AdConsentDatabaseHelper`.
47 AdConsentDatabaseHelper adConsentDatabaseHelper = new AdConsentDatabaseHelper(applicationContext, null, null, 0);
49 // Check to see if consent has been granted.
50 boolean adConsentGranted = adConsentDatabaseHelper.isGranted();
52 // Display the ad consent dialog if needed.
53 if (!adConsentGranted) { // Ad consent has not been granted.
54 // Display the ad consent dialog.
55 DialogFragment adConsentDialogFragment = new AdConsentDialog();
56 adConsentDialogFragment.show(fragmentManager, "Ad Consent");
57 } else { // Ad consent has been granted.
59 loadAd(view, applicationContext, adUnitId);
62 // Set the initialized variable to true so this section doesn't run again.
64 } else { // Ads have previously been initialized.
66 loadAd(view, applicationContext, adUnitId);
70 public static void loadAd(View view, Context applicationContext, String adUnitId) {
71 // Cast the generic view to an AdView.
72 AdView adView = (AdView) view;
74 // Save the layout parameters. They are used when programatically recreating the ad below.
75 RelativeLayout.LayoutParams adViewLayoutParameters = (RelativeLayout.LayoutParams) adView.getLayoutParams();
78 RelativeLayout adViewParentLayout = (RelativeLayout) adView.getParent();
79 adViewParentLayout.removeView(adView);
81 // Setup the new AdView. This is necessary because the size of the banner ad can change on rotate.
82 adView = new AdView(applicationContext);
83 adView.setAdSize(AdSize.SMART_BANNER);
84 adView.setAdUnitId(adUnitId);
85 adView.setId(R.id.adview);
86 adView.setLayoutParams(adViewLayoutParameters);
88 // Display the new AdView.
89 adViewParentLayout.addView(adView);
91 // 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
92 Bundle adSettingsBundle = new Bundle();
93 adSettingsBundle.putString("npa", "1");
95 // Build the ad request.
96 AdRequest adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
99 adView.loadAd(adRequest);
102 public static void hideAd(View view) {
103 // Cast the generic view to an AdView.
104 AdView adView = (AdView) view;
107 adView.setVisibility(View.GONE);
110 public static void pauseAd(View view) {
111 // Cast The generic view to an AdView.
112 AdView adView = (AdView) view;
118 public static void resumeAd(View view) {
119 // Cast the generic view to an AdView.
120 AdView adView = (AdView) view;
122 // Resume the AdView.