]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/free/java/com/stoutner/privacybrowser/dialogs/AdConsentDialog.java
6b771d6a1c40dc31f355e4dc4eb61befac9d897f
[PrivacyBrowserAndroid.git] / app / src / free / java / com / stoutner / privacybrowser / dialogs / AdConsentDialog.java
1 /*
2  * Copyright © 2018 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.app.DialogFragment;
25 import android.content.DialogInterface;
26 import android.os.Build;
27 import android.os.Bundle;
28
29 import com.google.ads.consent.ConsentInformation;
30 import com.google.ads.consent.ConsentStatus;
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
33 import com.stoutner.privacybrowser.helpers.AdHelper;
34
35 public class AdConsentDialog extends DialogFragment {
36     @Override
37     public Dialog onCreateDialog(Bundle savedInstanceState) {
38         // Use a builder to create the alert dialog.
39         AlertDialog.Builder dialogBuilder;
40
41         // Set the style and the icon according to the theme.
42         if (MainWebViewActivity.darkTheme) {
43             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogDark);
44             dialogBuilder.setIcon(R.drawable.block_ads_enabled_dark);
45         } else {
46             dialogBuilder = new AlertDialog.Builder(getActivity(), R.style.PrivacyBrowserAlertDialogLight);
47             dialogBuilder.setIcon(R.drawable.block_ads_enabled_light);
48         }
49
50         // Set the title.
51         dialogBuilder.setTitle(R.string.ad_consent);
52
53         // Set the text.
54         dialogBuilder.setMessage(R.string.ad_consent_text);
55
56         // Get a handle for the consent information.
57         ConsentInformation consentInformation = ConsentInformation.getInstance(getActivity().getApplicationContext());
58
59         // Configure the close button.
60         dialogBuilder.setNegativeButton(R.string.close_browser, (DialogInterface dialog, int which) -> {
61             // Set the consent status to Unknown.
62             consentInformation.setConsentStatus(ConsentStatus.UNKNOWN);
63
64             // Close the browser.  `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
65             if (Build.VERSION.SDK_INT >= 21) {
66                 getActivity().finishAndRemoveTask();
67             } else {
68                 getActivity().finish();
69             }
70
71             // Remove the terminated program from RAM.  The status code is `0`.
72             System.exit(0);
73         });
74
75         // Configure the accept button.
76         dialogBuilder.setPositiveButton(R.string.accept_ads, (DialogInterface dialog, int which) -> {
77             // Set the consent status to Non-Personalized.
78             consentInformation.setConsentStatus(ConsentStatus.NON_PERSONALIZED);
79
80             // Indicate the user is under age, which disables personalized advertising and remarketing.  https://developers.google.com/admob/android/eu-consent
81             consentInformation.setTagForUnderAgeOfConsent(true);
82
83             // Load an ad.
84             AdHelper.loadAd(getActivity().findViewById(R.id.adview), getActivity().getApplicationContext(), getString(R.string.ad_id));
85         });
86
87         // Return the alert dialog.
88         return dialogBuilder.create();
89     }
90
91     // Close Privacy Browser Free if the dialog is cancelled without selecting a button (by tapping on the background).
92     @Override
93     public void onCancel(DialogInterface dialogInterface) {
94         // Set the consent status to Unknown.
95         ConsentInformation.getInstance(getActivity().getApplicationContext()).setConsentStatus(ConsentStatus.UNKNOWN);
96
97         // Close the browser.  `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
98         if (Build.VERSION.SDK_INT >= 21) {
99             getActivity().finishAndRemoveTask();
100         } else {
101             getActivity().finish();
102         }
103
104         // Remove the terminated program from RAM.  The status code is `0`.
105         System.exit(0);
106     }
107 }