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{
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;
// 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
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">
</com.google.android.gms.ads.AdView>
<!-- Activities. -->
<string name="privacy_browser">Privacy Browser Free</string>
- <!-- Ad control strings. -->
+ <!-- Ad control. -->
<string name="ad_id">ca-app-pub-5962503714887045/2738552414</string>
</resources>
\ No newline at end of file
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.
// 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);
// 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.
// 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
}
}
+ @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();
// 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);
+ }
}
<item>Custom</item>
</string-array>
<string-array name="user_agent_entry_values">
- <item>Default user agent</item> <!--This item must not be translated into other languages because it is referenced in code. It is never displayed on the screen.-->
+ <item>Default user agent</item> <!-- This item must not be translated into other languages because it is referenced in code. It is never displayed on the screen. -->
<item>PrivacyBrowser/1.0</item>
<item>Mozilla/5.0 (Android 6.0.1; Mobile; rv:46.0) Gecko/46.0 Firefox/46.0</item>
<item>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</item>
<item>Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36</item>
<item>Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko</item>
<item>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</item>
- <item>Custom user agent</item> <!--This item must not be translated into other languages because it is referenced in code. It is never displayed on the screen.-->
+ <item>Custom user agent</item> <!-- This item must not be translated into other languages because it is referenced in code. It is never displayed on the screen. -->
</string-array>
<string name="custom_user_agent">Custom user agent</string>
<string name="search">Search</string>
<item>https://www.google.com/search?q=</item>
<item>https://www.bing.com/search?q=</item>
<item>https://search.yahoo.com/mobile/s?nojs=1&p=</item>
- <item>Custom URL</item> <!--This item must not be translated into other languages because it is referenced in code. It is never displayed on the screen.-->
+ <item>Custom URL</item> <!-- This item must not be translated into other languages because it is referenced in code. It is never displayed on the screen. -->
</string-array>
<string name="javascript_disabled_search_custom_url">JavaScript-disabled search custom URL</string>
<string name="javascript_enabled_search">JavaScript-enabled search</string>
<item>https://www.google.com/search?q=</item>
<item>https://www.bing.com/search?q=</item>
<item>https://search.yahoo.com/mobile/s?p=</item>
- <item>Custom URL</item> <!--This item must not be translated into other languages because it is referenced in code. It is never displayed on the screen.-->
+ <item>Custom URL</item> <!-- This item must not be translated into other languages because it is referenced in code. It is never displayed on the screen. -->
</string-array>
<string name="javascript_enabled_search_custom_url">JavaScript-enabled search custom URL</string>
<string name="custom_url">Custom URL</string>
<!-- About Dialog. -->
<string name="about_privacy_browser">About Privacy Browser</string>
<string name="dismiss">Dismiss</string>
+
+ <!-- Ad control. -->
+ <string name="ad_id">Null</string> <!-- There are no ads in the standard flavor, but this string must exist because it is referenced in the code. -->
</resources>
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