]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/free/java/com/stoutner/privacybrowser/dialogs/AdConsentDialog.java
Make SSL errors tab aware.
[PrivacyBrowserAndroid.git] / app / src / free / java / com / stoutner / privacybrowser / dialogs / AdConsentDialog.java
index 8808923a641be83c0591ba8941c00853560e24e2..bce865f224443ddd18559ff83f09c90b764a0f31 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2018 Soren Stoutner <soren@stoutner.com>.
+ * Copyright © 2018-2019 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
@@ -21,25 +21,36 @@ package com.stoutner.privacybrowser.dialogs;
 
 import android.app.AlertDialog;
 import android.app.Dialog;
-import android.app.DialogFragment;
 import android.content.DialogInterface;
+import android.content.SharedPreferences;
 import android.os.Build;
 import android.os.Bundle;
+import android.preference.PreferenceManager;
+import android.view.WindowManager;
+
+import androidx.annotation.NonNull;
+import androidx.fragment.app.DialogFragment;
 
-import com.google.ads.consent.ConsentInformation;
-import com.google.ads.consent.ConsentStatus;
 import com.stoutner.privacybrowser.R;
-import com.stoutner.privacybrowser.activities.MainWebViewActivity;
+import com.stoutner.privacybrowser.helpers.AdConsentDatabaseHelper;
 import com.stoutner.privacybrowser.helpers.AdHelper;
 
 public class AdConsentDialog extends DialogFragment {
+    @NonNull
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
+        // Get a handle for the shared preferences.
+        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
+
+        // Get the screenshot and theme preferences.
+        boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
+        boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
+
         // Use a builder to create the alert dialog.
         AlertDialog.Builder dialogBuilder;
 
         // Set the style and the icon according to the theme.
-        if (MainWebViewActivity.darkTheme) {
+        if (darkTheme) {
             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
             dialogBuilder.setIcon(R.drawable.block_ads_enabled_dark);
         } else {
@@ -47,19 +58,23 @@ public class AdConsentDialog extends DialogFragment {
             dialogBuilder.setIcon(R.drawable.block_ads_enabled_light);
         }
 
+        // Remove the incorrect lint warning below that `getApplicationContext()` might be null.
+        assert getActivity() != null;
+
+        // Initialize the bookmarks database helper.  The `0` specifies a database version, but that is ignored and set instead using a constant in `AdConsentDatabaseHelper`.
+        // `getContext()` can be used instead of `getActivity.getApplicationContext()` when the minimum API >= 23.
+        AdConsentDatabaseHelper adConsentDatabaseHelper = new AdConsentDatabaseHelper(getActivity().getApplicationContext(), null, null, 0);
+
         // Set the title.
         dialogBuilder.setTitle(R.string.ad_consent);
 
         // Set the text.
         dialogBuilder.setMessage(R.string.ad_consent_text);
 
-        // Get a handle for the consent information.
-        ConsentInformation consentInformation = ConsentInformation.getInstance(getActivity().getApplicationContext());
-
         // Configure the close button.
         dialogBuilder.setNegativeButton(R.string.close_browser, (DialogInterface dialog, int which) -> {
-            // Set the consent status to Unknown.
-            consentInformation.setConsentStatus(ConsentStatus.UNKNOWN);
+            // Update the ad consent database.
+            adConsentDatabaseHelper.updateAdConsent(false);
 
             // Close the browser.  `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
             if (Build.VERSION.SDK_INT >= 21) {
@@ -74,25 +89,41 @@ public class AdConsentDialog extends DialogFragment {
 
         // Configure the accept button.
         dialogBuilder.setPositiveButton(R.string.accept_ads, (DialogInterface dialog, int which) -> {
-            // Set the consent status to Non-Personalized.
-            consentInformation.setConsentStatus(ConsentStatus.NON_PERSONALIZED);
-
-            // Indicate the user is under age, which disables personalized advertising and remarketing.  https://developers.google.com/admob/android/eu-consent
-            consentInformation.setTagForUnderAgeOfConsent(true);
+            // Update the ad consent database.
+            adConsentDatabaseHelper.updateAdConsent(true);
 
-            // Load an ad.
+            // Load an ad.  `getContext()` can be used instead of `getActivity.getApplicationContext()` on the minimum API >= 23.
             AdHelper.loadAd(getActivity().findViewById(R.id.adview), getActivity().getApplicationContext(), getString(R.string.ad_unit_id));
         });
 
+        // Create an alert dialog from the alert dialog builder.
+        AlertDialog alertDialog = dialogBuilder.create();
+
+        // Disable screenshots if not allowed.
+        if (!allowScreenshots) {
+            // Remove the warning below that `getWindow()` might be null.
+            assert alertDialog.getWindow() != null;
+
+            // Disable screenshots.
+            alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
+        }
+
         // Return the alert dialog.
-        return dialogBuilder.create();
+        return alertDialog;
     }
 
     // Close Privacy Browser Free if the dialog is cancelled without selecting a button (by tapping on the background).
     @Override
     public void onCancel(DialogInterface dialogInterface) {
-        // Set the consent status to Unknown.
-        ConsentInformation.getInstance(getActivity().getApplicationContext()).setConsentStatus(ConsentStatus.UNKNOWN);
+        // Remove the incorrect lint warning below that `getApplicationContext()` might be null.
+        assert getActivity() != null;
+
+        // Initialize the bookmarks database helper.  The `0` specifies a database version, but that is ignored and set instead using a constant in `AdConsentDatabaseHelper`.
+        // `getContext()` can be used instead of `getActivity.getApplicationContext()` when the minimum API >= 23.
+        AdConsentDatabaseHelper adConsentDatabaseHelper = new AdConsentDatabaseHelper(getActivity().getApplicationContext(), null, null, 0);
+
+        // Update the ad consent database.
+        adConsentDatabaseHelper.updateAdConsent(false);
 
         // Close the browser.  `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
         if (Build.VERSION.SDK_INT >= 21) {