]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/free/java/com/stoutner/privacybrowser/dialogs/AdConsentDialog.java
Migrate to AndroidX from the Android Support Library. https://redmine.stoutner.com...
[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.os.Build;
26 import android.os.Bundle;
27
28 import androidx.annotation.NonNull;
29 import androidx.fragment.app.DialogFragment;
30
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
33 import com.stoutner.privacybrowser.helpers.AdConsentDatabaseHelper;
34 import com.stoutner.privacybrowser.helpers.AdHelper;
35
36 public class AdConsentDialog extends DialogFragment {
37     @NonNull
38     @Override
39     public Dialog onCreateDialog(Bundle savedInstanceState) {
40         // Use a builder to create the alert dialog.
41         AlertDialog.Builder dialogBuilder;
42
43         // Set the style and the icon according to the theme.
44         if (MainWebViewActivity.darkTheme) {
45             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
46             dialogBuilder.setIcon(R.drawable.block_ads_enabled_dark);
47         } else {
48             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
49             dialogBuilder.setIcon(R.drawable.block_ads_enabled_light);
50         }
51
52         // Remove the incorrect lint warning below that `getApplicationContext()` might be null.
53         assert getActivity() != null;
54
55         // Initialize the bookmarks database helper.  The `0` specifies a database version, but that is ignored and set instead using a constant in `AdConsentDatabaseHelper`.
56         // `getContext()` can be used instead of `getActivity.getApplicationContext()` when the minimum API >= 23.
57         AdConsentDatabaseHelper adConsentDatabaseHelper = new AdConsentDatabaseHelper(getActivity().getApplicationContext(), null, null, 0);
58
59         // Set the title.
60         dialogBuilder.setTitle(R.string.ad_consent);
61
62         // Set the text.
63         dialogBuilder.setMessage(R.string.ad_consent_text);
64
65         // Configure the close button.
66         dialogBuilder.setNegativeButton(R.string.close_browser, (DialogInterface dialog, int which) -> {
67             // Update the ad consent database.
68             adConsentDatabaseHelper.updateAdConsent(false);
69
70             // Close the browser.  `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
71             if (Build.VERSION.SDK_INT >= 21) {
72                 getActivity().finishAndRemoveTask();
73             } else {
74                 getActivity().finish();
75             }
76
77             // Remove the terminated program from RAM.  The status code is `0`.
78             System.exit(0);
79         });
80
81         // Configure the accept button.
82         dialogBuilder.setPositiveButton(R.string.accept_ads, (DialogInterface dialog, int which) -> {
83             // Update the ad consent database.
84             adConsentDatabaseHelper.updateAdConsent(true);
85
86             // Load an ad.  `getContext()` can be used instead of `getActivity.getApplicationContext()` on the minimum API >= 23.
87             AdHelper.loadAd(getActivity().findViewById(R.id.adview), getActivity().getApplicationContext(), getString(R.string.ad_unit_id));
88         });
89
90         // Return the alert dialog.
91         return dialogBuilder.create();
92     }
93
94     // Close Privacy Browser Free if the dialog is cancelled without selecting a button (by tapping on the background).
95     @Override
96     public void onCancel(DialogInterface dialogInterface) {
97         // Remove the incorrect lint warning below that `getApplicationContext()` might be null.
98         assert getActivity() != null;
99
100         // Initialize the bookmarks database helper.  The `0` specifies a database version, but that is ignored and set instead using a constant in `AdConsentDatabaseHelper`.
101         // `getContext()` can be used instead of `getActivity.getApplicationContext()` when the minimum API >= 23.
102         AdConsentDatabaseHelper adConsentDatabaseHelper = new AdConsentDatabaseHelper(getActivity().getApplicationContext(), null, null, 0);
103
104         // Update the ad consent database.
105         adConsentDatabaseHelper.updateAdConsent(false);
106
107         // Close the browser.  `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
108         if (Build.VERSION.SDK_INT >= 21) {
109             getActivity().finishAndRemoveTask();
110         } else {
111             getActivity().finish();
112         }
113
114         // Remove the terminated program from RAM.  The status code is `0`.
115         System.exit(0);
116     }
117 }