2 * Copyright © 2017-2020 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.annotation.SuppressLint
23 import android.app.Dialog
24 import android.content.Context
25 import android.content.DialogInterface
26 import android.net.Uri
27 import android.os.Bundle
28 import android.text.Editable
29 import android.text.TextWatcher
30 import android.view.KeyEvent
31 import android.view.View
32 import android.view.WindowManager
33 import android.widget.EditText
34 import android.widget.TextView
36 import androidx.appcompat.app.AlertDialog
37 import androidx.fragment.app.DialogFragment
38 import androidx.preference.PreferenceManager
40 import com.stoutner.privacybrowser.R
41 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper
43 class AddDomainDialog: DialogFragment() {
44 // The public interface is used to send information back to the parent activity.
45 interface AddDomainListener {
46 fun onAddDomain(dialogFragment: DialogFragment)
49 // The add domain listener is initialized in `onAttach()` and used in `onCreateDialog()`.
50 private lateinit var addDomainListener: AddDomainListener
52 override fun onAttach(context: Context) {
53 // Run the default commands.
54 super.onAttach(context)
56 // Get a handle for the listener from the launching context.
57 addDomainListener = context as AddDomainListener
61 // `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin. Also, the function can then be moved out of a companion object and just become a package-level function.
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 // `@SuppressLing("InflateParams")` removes the warning about using `null` as the parent view group when inflating the alert dialog.
82 @SuppressLint("InflateParams")
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 = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
93 // Set the icon according to the theme.
94 dialogBuilder.setIconAttribute(R.attr.domainsIcon)
97 dialogBuilder.setTitle(R.string.add_domain)
99 // Set the view. The parent view is `null` because it will be assigned by the alert dialog.
100 dialogBuilder.setView(requireActivity().layoutInflater.inflate(R.layout.add_domain_dialog, null))
102 // Set a listener on the cancel button. Using `null` as the listener closes the dialog without doing anything else.
103 dialogBuilder.setNegativeButton(R.string.cancel, null)
105 // Set a listener on the add button.
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("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.