]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/AddDomainDialog.kt
Update the URL in the copyright header. https://redmine.stoutner.com/issues/796
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / AddDomainDialog.kt
1 /*
2  * Copyright © 2017-2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
6  * Privacy Browser Android 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 Android 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 Android.  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.Context
24 import android.content.DialogInterface
25 import android.net.Uri
26 import android.os.Bundle
27 import android.text.Editable
28 import android.text.TextWatcher
29 import android.view.KeyEvent
30 import android.view.View
31 import android.view.WindowManager
32 import android.widget.EditText
33 import android.widget.TextView
34
35 import androidx.appcompat.app.AlertDialog
36 import androidx.fragment.app.DialogFragment
37 import androidx.preference.PreferenceManager
38
39 import com.stoutner.privacybrowser.R
40 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper
41
42 // Define the class constants.
43 private const val URL_STRING = "url_string"
44
45 class AddDomainDialog : DialogFragment() {
46     // Declare the class variables
47     private lateinit var addDomainListener: AddDomainListener
48
49     // The public interface is used to send information back to the parent activity.
50     interface AddDomainListener {
51         fun onAddDomain(dialogFragment: DialogFragment)
52     }
53
54     override fun onAttach(context: Context) {
55         // Run the default commands.
56         super.onAttach(context)
57
58         // Get a handle for the listener from the launching context.
59         addDomainListener = context as AddDomainListener
60     }
61
62     companion object {
63         // `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin.
64         @JvmStatic
65         fun addDomain(urlString: String): AddDomainDialog {
66             // Create an arguments bundle.
67             val argumentsBundle = Bundle()
68
69             // Store the URL in the bundle.
70             argumentsBundle.putString(URL_STRING, urlString)
71
72             // Create a new instance of the dialog.
73             val addDomainDialog = AddDomainDialog()
74
75             // Add the arguments bundle to the dialog.
76             addDomainDialog.arguments = argumentsBundle
77
78             // Return the new dialog.
79             return addDomainDialog
80         }
81     }
82
83     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
84         // Get the arguments.
85         val arguments = requireArguments()
86
87         // Get the URL from the bundle.
88         val urlString = arguments.getString(URL_STRING)
89
90         // Use an alert dialog builder to create the alert dialog.
91         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
92
93         // Set the icon according to the theme.
94         dialogBuilder.setIconAttribute(R.attr.domainsBlueIcon)
95
96         // Set the title.
97         dialogBuilder.setTitle(R.string.add_domain)
98
99         // Set the view.
100         dialogBuilder.setView(R.layout.add_domain_dialog)
101
102         // Set the cancel button listener.  Using `null` as the listener closes the dialog without doing anything else.
103         dialogBuilder.setNegativeButton(R.string.cancel, null)
104
105         // Set the add button listener.
106         dialogBuilder.setPositiveButton(R.string.add) { _: DialogInterface, _: Int ->
107             // Return the dialog fragment to the parent activity on add.
108             addDomainListener.onAddDomain(this)
109         }
110
111         // Create an alert dialog from the builder.
112         val alertDialog = dialogBuilder.create()
113
114         // Get a handle for the shared preferences.
115         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
116
117         // Get the screenshot preference.
118         val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots), false)
119
120         // Disable screenshots if not allowed.
121         if (!allowScreenshots) {
122             alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
123         }
124
125         // The alert dialog must be shown before the contents can be modified.
126         alertDialog.show()
127
128         // Initialize the domains database helper.  The `0` specifies the database version, but that is ignored and set instead using a constant in domains database helper.
129         val domainsDatabaseHelper = DomainsDatabaseHelper(context, null, null, 0)
130
131         // Get handles for the views in the alert dialog.
132         val addDomainEditText = alertDialog.findViewById<EditText>(R.id.domain_name_edittext)!!
133         val domainNameAlreadyExistsTextView = alertDialog.findViewById<TextView>(R.id.domain_name_already_exists_textview)!!
134         val addButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
135
136         //  Update the status of the warning text and the add button when the domain name changes.
137         addDomainEditText.addTextChangedListener(object: TextWatcher {
138             override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
139                 // Do nothing.
140             }
141
142             override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
143                 // Do nothing.
144             }
145
146             override fun afterTextChanged(s: Editable) {
147                 if (domainsDatabaseHelper.getCursorForDomainName(addDomainEditText.text.toString()).count > 0) {  // The domain already exists.
148                     // Show the warning text.
149                     domainNameAlreadyExistsTextView.visibility = View.VISIBLE
150
151                     // Disable the add button.
152                     addButton.isEnabled = false
153                 } else {  // The domain do not yet exist.
154                     // Hide the warning text.
155                     domainNameAlreadyExistsTextView.visibility = View.GONE
156
157                     // Enable the add button.
158                     addButton.isEnabled = true
159                 }
160             }
161         })
162
163         // Convert the URL string to a URI.
164         val currentUri = Uri.parse(urlString)
165
166         // Display the host in the add domain edit text.
167         addDomainEditText.setText(currentUri.host)
168
169         // Allow the enter key on the keyboard to create the domain from the add domain edit text.
170         addDomainEditText.setOnKeyListener { _: View, keyCode: Int, keyEvent: KeyEvent ->
171             // Check the key code and event.
172             if (keyCode == KeyEvent.KEYCODE_ENTER && keyEvent.action == KeyEvent.ACTION_DOWN) {  // The event is a key-down on the enter key.
173                 // Trigger the add domain listener and return the dialog fragment to the parent activity.
174                 addDomainListener.onAddDomain(this)
175
176                 // Manually dismiss the alert dialog.
177                 alertDialog.dismiss()
178
179                 // Consume the event.
180                 return@setOnKeyListener true
181             } else {  // Some other key was pressed.
182                 // Do not consume the event.
183                 return@setOnKeyListener false
184             }
185         }
186
187         // Return the alert dialog.
188         return alertDialog
189     }
190 }