2 * Copyright © 2016-2018 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.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;
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;
37 public class AdHelper {
38 private static boolean initialized;
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);
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);
48 // Check to see if consent has been granted.
49 boolean adConsentGranted = adConsentDatabaseHelper.isGranted();
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.
58 loadAd(view, applicationContext, adUnitId);
61 // Set the initialized variable to true so this section doesn't run again.
63 } else { // Ads have previously been initialized.
65 loadAd(view, applicationContext, adUnitId);
69 public static void loadAd (View view, Context applicationContext, String adUnitId) {
70 // Cast the generic view to an AdView.
71 AdView adView = (AdView) view;
73 // Save the layout parameters. They are used when programatically recreating the add below.
74 RelativeLayout.LayoutParams adViewLayoutParameters = (RelativeLayout.LayoutParams) adView.getLayoutParams();
77 RelativeLayout adViewParentLayout = (RelativeLayout) adView.getParent();
78 adViewParentLayout.removeView(adView);
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);
87 // Display the new AdView.
88 adViewParentLayout.addView(adView);
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");
94 // Build the ad request.
95 AdRequest adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
98 adView.loadAd(adRequest);
101 public static void hideAd(View view) {
102 // Cast the generic view to an AdView.
103 AdView adView = (AdView) view;
106 adView.setVisibility(View.GONE);
109 public static void pauseAd(View view) {
110 // Cast The generic view to an AdView.
111 AdView adView = (AdView) view;
117 public static void resumeAd(View view) {
118 // Cast the generic view to an AdView.
119 AdView adView = (AdView) view;
121 // Resume the AdView.