]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/free/java/com/stoutner/privacybrowser/helpers/AdHelper.java
Combine the light and dark Guide and About pages. https://redmine.stoutner.com/issue...
[PrivacyBrowserAndroid.git] / app / src / free / java / com / stoutner / privacybrowser / helpers / AdHelper.java
1 /*
2  * Copyright © 2016-2019 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.content.Context;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.widget.RelativeLayout;
26
27 import androidx.fragment.app.DialogFragment;
28 import androidx.fragment.app.FragmentManager;
29
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;
37
38 public class AdHelper {
39     private static boolean initialized;
40
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);
45
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);
48
49             // Check to see if consent has been granted.
50             boolean adConsentGranted = adConsentDatabaseHelper.isGranted();
51
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.
58                 // Load an ad.
59                 loadAd(view, applicationContext, adUnitId);
60             }
61
62             // Set the initialized variable to true so this section doesn't run again.
63             initialized = true;
64         } else {  // Ads have previously been initialized.
65             // Load an ad.
66             loadAd(view, applicationContext, adUnitId);
67         }
68     }
69
70     public static void loadAd(View view, Context applicationContext, String adUnitId) {
71         // Cast the generic view to an AdView.
72         AdView adView = (AdView) view;
73
74         // Save the layout parameters.  They are used when programatically recreating the ad below.
75         RelativeLayout.LayoutParams adViewLayoutParameters = (RelativeLayout.LayoutParams) adView.getLayoutParams();
76
77         // Remove the AdView.
78         RelativeLayout adViewParentLayout = (RelativeLayout) adView.getParent();
79         adViewParentLayout.removeView(adView);
80
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);
87
88         // Display the new AdView.
89         adViewParentLayout.addView(adView);
90
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");
94
95         // Build the ad request.
96         AdRequest adRequest = new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, adSettingsBundle).build();
97
98         // Make it so.
99         adView.loadAd(adRequest);
100     }
101
102     public static void hideAd(View view) {
103         // Cast the generic view to an AdView.
104         AdView adView = (AdView) view;
105
106         // Hide the ad.
107         adView.setVisibility(View.GONE);
108     }
109
110     public static void pauseAd(View view) {
111         // Cast The generic view to an AdView.
112         AdView adView = (AdView) view;
113
114         // Pause the AdView.
115         adView.pause();
116     }
117
118     public static void resumeAd(View view) {
119         // Cast the generic view to an AdView.
120         AdView adView = (AdView) view;
121
122         // Resume the AdView.
123         adView.resume();
124     }
125 }