]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - 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
1 /*
2  * Copyright © 2018-2019 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.dialogs;
21
22 import android.app.AlertDialog;
23 import android.app.Dialog;
24 import android.content.DialogInterface;
25 import android.content.SharedPreferences;
26 import android.os.Build;
27 import android.os.Bundle;
28 import android.preference.PreferenceManager;
29 import android.view.WindowManager;
30
31 import androidx.annotation.NonNull;
32 import androidx.fragment.app.DialogFragment;
33
34 import com.stoutner.privacybrowser.R;
35 import com.stoutner.privacybrowser.helpers.AdConsentDatabaseHelper;
36 import com.stoutner.privacybrowser.helpers.AdHelper;
37
38 public class AdConsentDialog extends DialogFragment {
39     @NonNull
40     @Override
41     public Dialog onCreateDialog(Bundle savedInstanceState) {
42         // Get a handle for the shared preferences.
43         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
44
45         // Get the screenshot and theme preferences.
46         boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false);
47         boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false);
48
49         // Use a builder to create the alert dialog.
50         AlertDialog.Builder dialogBuilder;
51
52         // Set the style and the icon according to the theme.
53         if (darkTheme) {
54             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
55             dialogBuilder.setIcon(R.drawable.block_ads_enabled_dark);
56         } else {
57             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
58             dialogBuilder.setIcon(R.drawable.block_ads_enabled_light);
59         }
60
61         // Remove the incorrect lint warning below that `getApplicationContext()` might be null.
62         assert getActivity() != null;
63
64         // Initialize the bookmarks database helper.  The `0` specifies a database version, but that is ignored and set instead using a constant in `AdConsentDatabaseHelper`.
65         // `getContext()` can be used instead of `getActivity.getApplicationContext()` when the minimum API >= 23.
66         AdConsentDatabaseHelper adConsentDatabaseHelper = new AdConsentDatabaseHelper(getActivity().getApplicationContext(), null, null, 0);
67
68         // Set the title.
69         dialogBuilder.setTitle(R.string.ad_consent);
70
71         // Set the text.
72         dialogBuilder.setMessage(R.string.ad_consent_text);
73
74         // Configure the close button.
75         dialogBuilder.setNegativeButton(R.string.close_browser, (DialogInterface dialog, int which) -> {
76             // Update the ad consent database.
77             adConsentDatabaseHelper.updateAdConsent(false);
78
79             // Close the browser.  `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
80             if (Build.VERSION.SDK_INT >= 21) {
81                 getActivity().finishAndRemoveTask();
82             } else {
83                 getActivity().finish();
84             }
85
86             // Remove the terminated program from RAM.  The status code is `0`.
87             System.exit(0);
88         });
89
90         // Configure the accept button.
91         dialogBuilder.setPositiveButton(R.string.accept_ads, (DialogInterface dialog, int which) -> {
92             // Update the ad consent database.
93             adConsentDatabaseHelper.updateAdConsent(true);
94
95             // Load an ad.  `getContext()` can be used instead of `getActivity.getApplicationContext()` on the minimum API >= 23.
96             AdHelper.loadAd(getActivity().findViewById(R.id.adview), getActivity().getApplicationContext(), getString(R.string.ad_unit_id));
97         });
98
99         // Create an alert dialog from the alert dialog builder.
100         AlertDialog alertDialog = dialogBuilder.create();
101
102         // Disable screenshots if not allowed.
103         if (!allowScreenshots) {
104             // Remove the warning below that `getWindow()` might be null.
105             assert alertDialog.getWindow() != null;
106
107             // Disable screenshots.
108             alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
109         }
110
111         // Return the alert dialog.
112         return alertDialog;
113     }
114
115     // Close Privacy Browser Free if the dialog is cancelled without selecting a button (by tapping on the background).
116     @Override
117     public void onCancel(DialogInterface dialogInterface) {
118         // Remove the incorrect lint warning below that `getApplicationContext()` might be null.
119         assert getActivity() != null;
120
121         // Initialize the bookmarks database helper.  The `0` specifies a database version, but that is ignored and set instead using a constant in `AdConsentDatabaseHelper`.
122         // `getContext()` can be used instead of `getActivity.getApplicationContext()` when the minimum API >= 23.
123         AdConsentDatabaseHelper adConsentDatabaseHelper = new AdConsentDatabaseHelper(getActivity().getApplicationContext(), null, null, 0);
124
125         // Update the ad consent database.
126         adConsentDatabaseHelper.updateAdConsent(false);
127
128         // Close the browser.  `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
129         if (Build.VERSION.SDK_INT >= 21) {
130             getActivity().finishAndRemoveTask();
131         } else {
132             getActivity().finish();
133         }
134
135         // Remove the terminated program from RAM.  The status code is `0`.
136         System.exit(0);
137     }
138 }