2 * Copyright © 2017-2023 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
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.
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.
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/>.
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 addDomain(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 fun addDomain(urlString: String?): AddDomainDialog {
64 // Create an arguments bundle.
65 val argumentsBundle = Bundle()
67 // Store the URL in the bundle.
68 argumentsBundle.putString(URL_STRING, urlString)
70 // Create a new instance of the dialog.
71 val addDomainDialog = AddDomainDialog()
73 // Add the arguments bundle to the dialog.
74 addDomainDialog.arguments = argumentsBundle
76 // Return the new dialog.
77 return addDomainDialog
81 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
83 val arguments = requireArguments()
85 // Get the URL from the bundle.
86 val urlString = arguments.getString(URL_STRING)
88 // Use an alert dialog builder to create the alert dialog.
89 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
92 dialogBuilder.setIcon(R.drawable.domains)
95 dialogBuilder.setTitle(R.string.add_domain)
98 dialogBuilder.setView(R.layout.add_domain_dialog)
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)
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.addDomain(this)
109 // Create an alert dialog from the builder.
110 val alertDialog = dialogBuilder.create()
112 // Get a handle for the shared preferences.
113 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
115 // Get the screenshot preference.
116 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
118 // Disable screenshots if not allowed.
119 if (!allowScreenshots) {
120 alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
123 // The alert dialog must be shown before the contents can be modified.
126 // Initialize the domains database helper.
127 val domainsDatabaseHelper = DomainsDatabaseHelper(requireContext())
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)
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) {
140 override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
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
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
155 // Enable the add button.
156 addButton.isEnabled = true
161 // Convert the URL string to a URI.
162 val currentUri = Uri.parse(urlString)
164 // Display the host in the add domain edit text.
165 addDomainEditText.setText(currentUri.host)
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.addDomain(this)
174 // Manually dismiss the alert dialog.
175 alertDialog.dismiss()
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
185 // Return the alert dialog.