From 77756d29db4d52a35a3999f67b101e22525c97e2 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Wed, 18 May 2016 21:25:17 -0700 Subject: [PATCH] Update adView to use a SMART_BANNER for the free flavor. --- .../com/stoutner/privacybrowser/BannerAd.java | 45 +++++++++++++++++++ app/src/free/res/layout/main_webview.xml | 2 +- app/src/free/res/values/strings.xml | 2 +- .../privacybrowser/MainWebViewActivity.java | 32 ++++++++++--- .../privacybrowser/SettingsFragment.java | 12 ++--- app/src/main/res/values/strings.xml | 11 +++-- .../com/stoutner/privacybrowser/BannerAd.java | 19 ++++++-- 7 files changed, 103 insertions(+), 20 deletions(-) diff --git a/app/src/free/java/com/stoutner/privacybrowser/BannerAd.java b/app/src/free/java/com/stoutner/privacybrowser/BannerAd.java index 6a50a0ad..9cfe9ab2 100644 --- a/app/src/free/java/com/stoutner/privacybrowser/BannerAd.java +++ b/app/src/free/java/com/stoutner/privacybrowser/BannerAd.java @@ -19,10 +19,13 @@ package com.stoutner.privacybrowser; +import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.view.View; +import android.widget.RelativeLayout; import com.google.android.gms.ads.AdRequest; +import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdView; class BannerAd extends AppCompatActivity{ @@ -35,6 +38,32 @@ class BannerAd extends AppCompatActivity{ adView.loadAd(adRequest); } + public static void reloadAfterRotate (View view, Context applicationContext, String ad_id) { + // Cast view to an AdView. + AdView adView = (AdView) view; + + // Save the layout parameters. + RelativeLayout.LayoutParams adViewLayoutParameters = (RelativeLayout.LayoutParams) adView.getLayoutParams(); + + // Remove the AdView. + RelativeLayout adViewParentLayout = (RelativeLayout) adView.getParent(); + adViewParentLayout.removeView(adView); + + // Setup the new AdView. + adView = new AdView(applicationContext); + adView.setAdSize(AdSize.SMART_BANNER); + adView.setAdUnitId(ad_id); + adView.setId(R.id.adView); + adView.setLayoutParams(adViewLayoutParameters); + + // Display the new AdView. + adViewParentLayout.addView(adView); + + // Request a new ad. + AdRequest adRequest = new AdRequest.Builder().build(); + adView.loadAd(adRequest); + } + public static void hideAd(View view) { // Cast view to an AdView. AdView adView = (AdView) view; @@ -50,4 +79,20 @@ class BannerAd extends AppCompatActivity{ // Hide the ad. adView.setVisibility(View.VISIBLE); } + + public static void pauseAd(View view) { + // Cast view to an AdView. + AdView adView = (AdView) view; + + // Pause the AdView. + adView.pause(); + } + + public static void resumeAd(View view) { + // Cast view to an AdView. + AdView adView = (AdView) view; + + // Resume the AdView. + adView.resume(); + } } \ No newline at end of file diff --git a/app/src/free/res/layout/main_webview.xml b/app/src/free/res/layout/main_webview.xml index 2b878190..1c05c00f 100644 --- a/app/src/free/res/layout/main_webview.xml +++ b/app/src/free/res/layout/main_webview.xml @@ -36,7 +36,7 @@ android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" - ads:adSize="BANNER" + ads:adSize="SMART_BANNER" ads:adUnitId="@string/ad_id"> diff --git a/app/src/free/res/values/strings.xml b/app/src/free/res/values/strings.xml index cd10412a..8854d583 100644 --- a/app/src/free/res/values/strings.xml +++ b/app/src/free/res/values/strings.xml @@ -22,6 +22,6 @@ Privacy Browser Free - + ca-app-pub-5962503714887045/2738552414 \ No newline at end of file diff --git a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java index f2f8b040..9e2d0496 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java @@ -101,9 +101,10 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation private String formattedUrlString; // privacyIcon is used in onCreateOptionsMenu() and updatePrivacyIcon(). private MenuItem privacyIcon; - // urlTextBox is used in onCreate(), onOptionsItemSelected(), and loadUrlFromTextBox(). private EditText urlTextBox; + // adView is used in onCreate() and onConfigurationChanged(). + private View adView; @Override // Remove Android Studio's warning about the dangers of using SetJavaScriptEnabled. The whole premise of Privacy Browser is built around an understanding of these dangers. @@ -121,9 +122,6 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // We need to use the SupportActionBar from android.support.v7.app.ActionBar until the minimum API is >= 21. final ActionBar appBar = getSupportActionBar(); - // Setup AdView for the free flavor. - final View adView = findViewById(R.id.adView); - // Implement swipe to refresh swipeToRefresh = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); swipeToRefresh.setColorSchemeResources(R.color.blue); @@ -403,10 +401,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Load the initial website. mainWebView.loadUrl(formattedUrlString); - // Load the ad if this is the free flavor. + // Initialize AdView for the free flavor and request an ad. If this is not the free flavor BannerAd.requestAd() does nothing. + adView = findViewById(R.id.adView); BannerAd.requestAd(adView); } + @Override protected void onNewIntent(Intent intent) { // Sets the new intent as the activity intent, so that any future getIntent()s pick up this one instead of creating a new activity. @@ -704,6 +704,12 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Update the status of the drawerToggle icon. drawerToggle.onConfigurationChanged(newConfig); + + // Reload the ad if this is the free flavor. + BannerAd.reloadAfterRotate(adView, getApplicationContext(), getString(R.string.ad_id)); + + // Reinitialize the adView variable, as the View will have been removed and readded. + adView = findViewById(R.id.adView); } @Override @@ -749,6 +755,22 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation } } + @Override + public void onPause() { + // We need to pause the adView or it will continue to consume resources in the background on the free flavor. + BannerAd.pauseAd(adView); + + super.onPause(); + } + + @Override + public void onResume() { + super.onResume(); + + // We need to resume the adView for the free flavor. + BannerAd.resumeAd(adView); + } + private void loadUrlFromTextBox() throws UnsupportedEncodingException { // Get the text from urlTextBox and convert it to a string. String unformattedUrlString = urlTextBox.getText().toString(); diff --git a/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java b/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java index 30a34f76..a6ea1a96 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java +++ b/app/src/main/java/com/stoutner/privacybrowser/SettingsFragment.java @@ -318,15 +318,15 @@ public class SettingsFragment extends PreferenceFragment { // It is necessary to re-register the listener on every resume or it will randomly stop working because apps can be paused and resumed at any time // even while running in the foreground. - @Override - public void onResume() { - super.onResume(); - savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener); - } - @Override public void onPause() { super.onPause(); savedPreferences.unregisterOnSharedPreferenceChangeListener(preferencesListener); } + + @Override + public void onResume() { + super.onResume(); + savedPreferences.registerOnSharedPreferenceChangeListener(preferencesListener); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a47e02fc..6795393d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -93,7 +93,7 @@ Custom - Default user agent + Default user agent PrivacyBrowser/1.0 Mozilla/5.0 (Android 6.0.1; Mobile; rv:46.0) Gecko/46.0 Firefox/46.0 Mozilla/5.0 (Linux; Android 6.0.1; Nexus 6P Build/MHC19Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.89 Mobile Safari/537.36 @@ -104,7 +104,7 @@ Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36 Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586 - Custom user agent + Custom user agent Custom user agent Search @@ -121,7 +121,7 @@ https://www.google.com/search?q= https://www.bing.com/search?q= https://search.yahoo.com/mobile/s?nojs=1&p= - Custom URL + Custom URL JavaScript-disabled search custom URL JavaScript-enabled search @@ -137,7 +137,7 @@ https://www.google.com/search?q= https://www.bing.com/search?q= https://search.yahoo.com/mobile/s?p= - Custom URL + Custom URL JavaScript-enabled search custom URL Custom URL @@ -149,4 +149,7 @@ About Privacy Browser Dismiss + + + Null diff --git a/app/src/standard/java/com/stoutner/privacybrowser/BannerAd.java b/app/src/standard/java/com/stoutner/privacybrowser/BannerAd.java index 69152689..1afe688a 100644 --- a/app/src/standard/java/com/stoutner/privacybrowser/BannerAd.java +++ b/app/src/standard/java/com/stoutner/privacybrowser/BannerAd.java @@ -19,18 +19,31 @@ package com.stoutner.privacybrowser; +import android.content.Context; import android.view.View; class BannerAd { public static void requestAd(View view) { - // Do nothing because this is the standard version. + // Do nothing because this is the standard flavor. + } + + public static void reloadAfterRotate(View view, Context applicationContext, String ad_id) { + // Do nothing because this is the standard flavor. } public static void hideAd(View view) { - // Do nothing because this is the standard version. + // Do nothing because this is the standard flavor. } public static void showAd(View view) { - // Do nothing because this is the standard version. + // Do nothing because this is the standard flavor. + } + + public static void pauseAd(View view) { + // Do nothing because this is the standard flavor. + } + + public static void resumeAd(View view) { + // Do nothing because this is the standard flavor. } } \ No newline at end of file -- 2.43.0