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