]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/free/java/com/stoutner/privacybrowser/helpers/AdHelper.kt
84c1b4927a646e6a75ecd44fbded1508719c32f3
[PrivacyBrowserAndroid.git] / app / src / free / java / com / stoutner / privacybrowser / helpers / AdHelper.kt
1 /*
2  * Copyright © 2016-2021 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.View
27 import android.widget.RelativeLayout
28
29 import androidx.fragment.app.FragmentManager
30
31 import com.google.ads.mediation.admob.AdMobAdapter
32 import com.google.android.gms.ads.AdRequest
33 import com.google.android.gms.ads.AdSize
34 import com.google.android.gms.ads.AdView
35 import com.google.android.gms.ads.MobileAds
36
37 import com.stoutner.privacybrowser.R
38 import com.stoutner.privacybrowser.dialogs.AdConsentDialog
39
40 object AdHelper {
41     // Define the class variables.
42     private var initialized = false
43
44     // The `@JvmStatic` notation can be removed once all the code has migrated to Kotlin.
45     @JvmStatic
46     fun initializeAds(view: View, context: Context, activity: Activity, fragmentManager: FragmentManager, adUnitId: String) {
47         // Check to see if the ads have been initialized.
48         if (!initialized) {  // This is the first run; the ads have not yet been initialized.
49             // Initialize mobile ads.
50             MobileAds.initialize(context)
51
52             // Initialize the bookmarks database helper.
53             val adConsentDatabaseHelper = AdConsentDatabaseHelper(context)
54
55             // Check to see if consent has been granted.
56             val adConsentGranted = adConsentDatabaseHelper.isGranted
57
58             // Display the ad consent dialog if needed.
59             if (!adConsentGranted) {  // Ad consent has not been granted.
60                 // Instantiate the ad consent dialog.
61                 val adConsentDialogFragment = AdConsentDialog()
62
63                 // Display the ad consent dialog.
64                 adConsentDialogFragment.show(fragmentManager,"Ad Consent")
65             } else {  // Ad consent has already been granted.
66                 // Load an ad.
67                 loadAd(view, context, activity, adUnitId)
68             }
69
70             // Set the initialized variable to true so this section doesn't run again.
71             initialized = true
72         } else {  // Ads have previously been initialized.
73             // Load an ad.
74             loadAd(view, context, activity, adUnitId)
75         }
76     }
77
78     // The `@JvmStatic` notation can be removed once all the code has migrated to Kotlin.
79     @JvmStatic
80     fun loadAd(view: View, context: Context, activity: Activity, adUnitId: String) {
81         // Cast the generic view to an AdView.
82         var adView = view as AdView
83
84         // Save the layout parameters.  They are used when programatically recreating the ad below.
85         val adViewLayoutParameters = adView.layoutParams as RelativeLayout.LayoutParams
86
87         // Get a handle for the ad view parent.
88         val adViewParentLayout = adView.parent as RelativeLayout
89
90         // Remove the AdView.
91         adViewParentLayout.removeView(adView)
92
93         // Create a new AdView.  This is necessary because the size can change when the device is rotated.
94         adView = AdView(context)
95
96         // Set the ad unit ID.
97         adView.adUnitId = adUnitId
98
99         //  Set the view ID.
100         adView.id = R.id.adview
101
102         // Set the layout parameters.
103         adView.layoutParams = adViewLayoutParameters
104
105         // Add the new ad view to the parent layout.
106         adViewParentLayout.addView(adView)
107
108         // Get a handle for the display.  Once the minimum API >= 30, this should be changed to `context.getDisplay()`.
109         @Suppress("DEPRECATION") val display = activity.windowManager.defaultDisplay
110
111         // Initialize a display metrics.
112         val displayMetrics = DisplayMetrics()
113
114         // Get the display metrics from the display.  Once the minimum APO >= 30, this should be replaced with `WindowMetrics.getBounds()` and `Configuration.densityDpi`.
115         @Suppress("DEPRECATION")
116         display.getMetrics(displayMetrics)
117
118         // Get the width pixels and the density.
119         val widthPixels = displayMetrics.widthPixels.toFloat()
120         val density = displayMetrics.density
121
122         // Calculate the ad width.
123         val adWidth = (widthPixels / density).toInt()
124
125         // Get the ad size.
126         val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(context, adWidth)
127
128         // Set the ad size on the adView.
129         adView.adSize = adSize
130
131         // Create an ad settings bundle.
132         val adSettingsBundle = Bundle()
133
134         // Only request non-personalized ads.  <https://developers.google.com/ad-manager/mobile-ads-sdk/android/eu-consent#forward-consent>
135         adSettingsBundle.putString("npa", "1")
136
137         // Build the ad request.
138         val adRequest = AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter::class.java, adSettingsBundle).build()
139
140         // Make it so.
141         adView.loadAd(adRequest)
142     }
143
144     // The `@JvmStatic` notation can be removed once all the code has migrated to Kotlin.
145     // This method exists here for the sake of consistency with the following two methods.
146     @JvmStatic
147     fun hideAd(view: View) {
148         // Cast the generic view to an AdView.
149         val adView = view as AdView
150
151         // Hide the ad.
152         adView.visibility = View.GONE
153     }
154
155     // The `@JvmStatic` notation can be removed once all the code has migrated to Kotlin.
156     // This method exists here so that the main WebView activity doesn't need to import `com.google.android.gms.ads.AdView`.
157     @JvmStatic
158     fun pauseAd(view: View) {
159         // Cast The generic view to an AdView.
160         val adView = view as AdView
161
162         // Pause the AdView.
163         adView.pause()
164     }
165
166     // The `@JvmStatic` notation can be removed once all the code has migrated to Kotlin.
167     // This method exists here so that the main WebView activity doesn't need to import `com.google.android.gms.ads.AdView`.
168     @JvmStatic
169     fun resumeAd(view: View) {
170         // Cast the generic view to an AdView.
171         val adView = view as AdView
172
173         // Resume the AdView.
174         adView.resume()
175     }
176 }