2 * Copyright © 2017-2021 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.dialogs
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
35 import androidx.appcompat.app.AlertDialog
36 import androidx.fragment.app.DialogFragment
37 import androidx.preference.PreferenceManager
39 import com.stoutner.privacybrowser.R
40 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper
42 // Define the class constants.
43 private const val URL_STRING = "url_string"
45 class AddDomainDialog : DialogFragment() {
46 // Declare the class variables
47 private lateinit var addDomainListener: AddDomainListener
49 // The public interface is used to send information back to the parent activity.
50 interface AddDomainListener {
51 fun onAddDomain(dialogFragment: DialogFragment)
54 override fun onAttach(context: Context) {
55 // Run the default commands.
56 super.onAttach(context)
58 // Get a handle for the listener from the launching context.
59 addDomainListener = context as AddDomainListener
63 // `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin.
65 fun addDomain(urlString: String): AddDomainDialog {
66 // Create an arguments bundle.
67 val argumentsBundle = Bundle()
69 // Store the URL in the bundle.
70 argumentsBundle.putString(URL_STRING, urlString)
72 // Create a new instance of the dialog.
73 val addDomainDialog = AddDomainDialog()
75 // Add the arguments bundle to the dialog.
76 addDomainDialog.arguments = argumentsBundle
78 // Return the new dialog.
79 return addDomainDialog
83 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
85 val arguments = requireArguments()
87 // Get the URL from the bundle.
88 val urlString = arguments.getString(URL_STRING)
90 // Use an alert dialog builder to create the alert dialog.
91 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
93 // Set the icon according to the theme.
94 dialogBuilder.setIconAttribute(R.attr.domainsBlueIcon)
97 dialogBuilder.setTitle(R.string.add_domain)
100 dialogBuilder.setView(R.layout.add_domain_dialog)
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)
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)
111 // Create an alert dialog from the builder.
112 val alertDialog = dialogBuilder.create()
114 // Get a handle for the shared preferences.
115 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
117 // Get the screenshot preference.
118 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots), false)
120 // Disable screenshots if not allowed.
121 if (!allowScreenshots) {
122 alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
125 // The alert dialog must be shown before the contents can be modified.
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)
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)
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) {
142 override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
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
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
157 // Enable the add button.
158 addButton.isEnabled = true
163 // Convert the URL string to a URI.
164 val currentUri = Uri.parse(urlString)
166 // Display the host in the add domain edit text.
167 addDomainEditText.setText(currentUri.host)
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)
176 // Manually dismiss the alert dialog.
177 alertDialog.dismiss()
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
187 // Return the alert dialog.