]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/free/java/com/stoutner/privacybrowser/dialogs/AdConsentDialog.kt
f22367cf22b73a3d4a328ce9127c518e104db465
[PrivacyBrowserAndroid.git] / app / src / free / java / com / stoutner / privacybrowser / dialogs / AdConsentDialog.kt
1 /*
2  * Copyright © 2018-2021 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.os.Build
25 import android.os.Bundle
26 import android.view.WindowManager
27
28 import androidx.appcompat.app.AlertDialog
29 import androidx.fragment.app.DialogFragment
30 import androidx.preference.PreferenceManager
31
32 import com.stoutner.privacybrowser.R
33 import com.stoutner.privacybrowser.helpers.AdConsentDatabaseHelper
34 import com.stoutner.privacybrowser.helpers.AdHelper
35 import kotlin.system.exitProcess
36
37 class AdConsentDialog : DialogFragment() {
38     // Declare the class variables.
39     private lateinit var adConsentDatabaseHelper: AdConsentDatabaseHelper
40
41     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
42         // Use a builder to create the alert dialog.
43         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
44
45         // Set the icon according to the theme.
46         dialogBuilder.setIconAttribute(R.attr.blockAdsBlueIcon)
47
48         // Initialize the bookmarks database helper.
49         adConsentDatabaseHelper = AdConsentDatabaseHelper(requireContext())
50
51         // Set the title.
52         dialogBuilder.setTitle(R.string.ad_consent)
53
54         // Set the text.
55         dialogBuilder.setMessage(R.string.ad_consent_text)
56
57         // Set the close browser button.
58         dialogBuilder.setNegativeButton(R.string.close_browser) { _: DialogInterface?, _: Int ->
59             // Update the ad consent database.
60             adConsentDatabaseHelper.updateAdConsent(false)
61
62             // Close the browser.  `finishAndRemoveTask` also removes Privacy Browser from the recent app list.
63             if (Build.VERSION.SDK_INT >= 21) {
64                 requireActivity().finishAndRemoveTask()
65             } else {
66                 requireActivity().finish()
67             }
68
69             // Remove the terminated program from RAM.  The status code is `0`.
70             exitProcess(0)
71         }
72
73         // Set the accept ads button.
74         dialogBuilder.setPositiveButton(R.string.accept_ads) { _: DialogInterface?, _: Int ->
75             // Update the ad consent database.
76             adConsentDatabaseHelper.updateAdConsent(true)
77
78             // Load an ad.
79             AdHelper.loadAd(requireActivity().findViewById(R.id.adview), requireContext(), requireActivity(), getString(R.string.ad_unit_id))
80         }
81
82         // Create an alert dialog from the alert dialog builder.
83         val alertDialog = dialogBuilder.create()
84
85         // Get a handle for the shared preferences.
86         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
87
88         // Get the screenshot preference.
89         val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
90
91         // Disable screenshots if not allowed.
92         if (!allowScreenshots) {
93             // Disable screenshots.
94             alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
95         }
96
97         // Return the alert dialog.
98         return alertDialog
99     }
100
101     // Close Privacy Browser Free if the dialog is cancelled without selecting a button (by tapping on the background).
102     override fun onCancel(dialogInterface: DialogInterface) {
103         // Update the ad consent database.
104         adConsentDatabaseHelper.updateAdConsent(false)
105
106         // Close the browser.  `finishAndRemoveTask()` also removes Privacy Browser from the recent app list.
107         if (Build.VERSION.SDK_INT >= 21) {
108             requireActivity().finishAndRemoveTask()
109         } else {
110             requireActivity().finish()
111         }
112
113         // Remove the terminated program from RAM.  The status code is `0`.
114         exitProcess(0)
115     }
116 }