2 * Copyright © 2016-2021 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.View
27 import android.widget.RelativeLayout
29 import androidx.fragment.app.FragmentManager
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
37 import com.stoutner.privacybrowser.R
38 import com.stoutner.privacybrowser.dialogs.AdConsentDialog
41 // Define the class variables.
42 private var initialized = false
44 // The `@JvmStatic` notation can be removed once all the code has migrated to Kotlin.
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)
52 // Initialize the bookmarks database helper.
53 val adConsentDatabaseHelper = AdConsentDatabaseHelper(context)
55 // Check to see if consent has been granted.
56 val adConsentGranted = adConsentDatabaseHelper.isGranted
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()
63 // Display the ad consent dialog.
64 adConsentDialogFragment.show(fragmentManager,"Ad Consent")
65 } else { // Ad consent has already been granted.
67 loadAd(view, context, activity, adUnitId)
70 // Set the initialized variable to true so this section doesn't run again.
72 } else { // Ads have previously been initialized.
74 loadAd(view, context, activity, adUnitId)
78 // The `@JvmStatic` notation can be removed once all the code has migrated to Kotlin.
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
84 // Save the layout parameters. They are used when programatically recreating the ad below.
85 val adViewLayoutParameters = adView.layoutParams as RelativeLayout.LayoutParams
87 // Get a handle for the ad view parent.
88 val adViewParentLayout = adView.parent as RelativeLayout
91 adViewParentLayout.removeView(adView)
93 // Create a new AdView. This is necessary because the size can change when the device is rotated.
94 adView = AdView(context)
96 // Set the ad unit ID.
97 adView.adUnitId = adUnitId
100 adView.id = R.id.adview
102 // Set the layout parameters.
103 adView.layoutParams = adViewLayoutParameters
105 // Add the new ad view to the parent layout.
106 adViewParentLayout.addView(adView)
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
111 // Initialize a display metrics.
112 val displayMetrics = DisplayMetrics()
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)
118 // Get the width pixels and the density.
119 val widthPixels = displayMetrics.widthPixels.toFloat()
120 val density = displayMetrics.density
122 // Calculate the ad width.
123 val adWidth = (widthPixels / density).toInt()
126 val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(context, adWidth)
128 // Set the ad size on the adView.
129 adView.adSize = adSize
131 // Create an ad settings bundle.
132 val adSettingsBundle = Bundle()
134 // 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>
135 adSettingsBundle.putString("npa", "1")
137 // Build the ad request.
138 val adRequest = AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter::class.java, adSettingsBundle).build()
141 adView.loadAd(adRequest)
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.
147 fun hideAd(view: View) {
148 // Cast the generic view to an AdView.
149 val adView = view as AdView
152 adView.visibility = View.GONE
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`.
158 fun pauseAd(view: View) {
159 // Cast The generic view to an AdView.
160 val adView = view as AdView
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`.
169 fun resumeAd(view: View) {
170 // Cast the generic view to an AdView.
171 val adView = view as AdView
173 // Resume the AdView.