]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/AddDomainDialog.kt
Fix crash when adding domain settings with null domain. https://redmine.stoutner...
[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         fun addDomain(urlString: String?): AddDomainDialog {
64             // Create an arguments bundle.
65             val argumentsBundle = Bundle()
66
67             // Store the URL in the bundle.
68             argumentsBundle.putString(URL_STRING, urlString)
69
70             // Create a new instance of the dialog.
71             val addDomainDialog = AddDomainDialog()
72
73             // Add the arguments bundle to the dialog.
74             addDomainDialog.arguments = argumentsBundle
75
76             // Return the new dialog.
77             return addDomainDialog
78         }
79     }
80
81     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
82         // Get the arguments.
83         val arguments = requireArguments()
84
85         // Get the URL from the bundle.
86         val urlString = arguments.getString(URL_STRING)
87
88         // Use an alert dialog builder to create the alert dialog.
89         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
90
91         // Set the icon.
92         dialogBuilder.setIcon(R.drawable.domains)
93
94         // Set the title.
95         dialogBuilder.setTitle(R.string.add_domain)
96
97         // Set the view.
98         dialogBuilder.setView(R.layout.add_domain_dialog)
99
100         // Set the cancel button listener.  Using `null` as the listener closes the dialog without doing anything else.
101         dialogBuilder.setNegativeButton(R.string.cancel, null)
102
103         // Set the add button listener.
104         dialogBuilder.setPositiveButton(R.string.add) { _: DialogInterface, _: Int ->
105             // Return the dialog fragment to the parent activity on add.
106             addDomainListener.onAddDomain(this)
107         }
108
109         // Create an alert dialog from the builder.
110         val alertDialog = dialogBuilder.create()
111
112         // Get a handle for the shared preferences.
113         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
114
115         // Get the screenshot preference.
116         val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots), false)
117
118         // Disable screenshots if not allowed.
119         if (!allowScreenshots) {
120             alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
121         }
122
123         // The alert dialog must be shown before the contents can be modified.
124         alertDialog.show()
125
126         // Initialize the domains database helper.
127         val domainsDatabaseHelper = DomainsDatabaseHelper(requireContext())
128
129         // Get handles for the views in the alert dialog.
130         val addDomainEditText = alertDialog.findViewById<EditText>(R.id.domain_name_edittext)!!
131         val domainNameAlreadyExistsTextView = alertDialog.findViewById<TextView>(R.id.domain_name_already_exists_textview)!!
132         val addButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
133
134         //  Update the status of the warning text and the add button when the domain name changes.
135         addDomainEditText.addTextChangedListener(object: TextWatcher {
136             override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
137                 // Do nothing.
138             }
139
140             override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
141                 // Do nothing.
142             }
143
144             override fun afterTextChanged(s: Editable) {
145                 if (domainsDatabaseHelper.getCursorForDomainName(addDomainEditText.text.toString()).count > 0) {  // The domain already exists.
146                     // Show the warning text.
147                     domainNameAlreadyExistsTextView.visibility = View.VISIBLE
148
149                     // Disable the add button.
150                     addButton.isEnabled = false
151                 } else {  // The domain do not yet exist.
152                     // Hide the warning text.
153                     domainNameAlreadyExistsTextView.visibility = View.GONE
154
155                     // Enable the add button.
156                     addButton.isEnabled = true
157                 }
158             }
159         })
160
161         // Convert the URL string to a URI.
162         val currentUri = Uri.parse(urlString)
163
164         // Display the host in the add domain edit text.
165         addDomainEditText.setText(currentUri.host)
166
167         // Allow the enter key on the keyboard to create the domain from the add domain edit text.
168         addDomainEditText.setOnKeyListener { _: View, keyCode: Int, keyEvent: KeyEvent ->
169             // Check the key code and event.
170             if (keyCode == KeyEvent.KEYCODE_ENTER && keyEvent.action == KeyEvent.ACTION_DOWN) {  // The event is a key-down on the enter key.
171                 // Trigger the add domain listener and return the dialog fragment to the parent activity.
172                 addDomainListener.onAddDomain(this)
173
174                 // Manually dismiss the alert dialog.
175                 alertDialog.dismiss()
176
177                 // Consume the event.
178                 return@setOnKeyListener true
179             } else {  // Some other key was pressed.
180                 // Do not consume the event.
181                 return@setOnKeyListener false
182             }
183         }
184
185         // Return the alert dialog.
186         return alertDialog
187     }
188 }