2 * Copyright © 2016-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.graphics.Bitmap
26 import android.graphics.BitmapFactory
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.ImageView
35 import android.widget.LinearLayout
36 import android.widget.RadioButton
38 import androidx.appcompat.app.AlertDialog
39 import androidx.fragment.app.DialogFragment
40 import androidx.preference.PreferenceManager
42 import com.stoutner.privacybrowser.R
43 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper
45 import java.io.ByteArrayOutputStream
47 // Define the class constants.
48 private const val FAVORITE_ICON_BYTE_ARRAY = "favorite_icon_byte_array"
50 class CreateBookmarkFolderDialog : DialogFragment() {
52 fun createBookmarkFolder(favoriteIconBitmap: Bitmap): CreateBookmarkFolderDialog {
53 // Create a favorite icon byte array output stream.
54 val favoriteIconByteArrayOutputStream = ByteArrayOutputStream()
56 // Convert the favorite icon to a PNG and place it in the byte array output stream. `0` is for lossless compression (the only option for a PNG).
57 favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream)
59 // Convert the byte array output stream to a byte array.
60 val favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray()
62 // Create an arguments bundle.
63 val argumentsBundle = Bundle()
65 // Store the favorite icon in the bundle.
66 argumentsBundle.putByteArray(FAVORITE_ICON_BYTE_ARRAY, favoriteIconByteArray)
68 // Create a new instance of the dialog.
69 val createBookmarkFolderDialog = CreateBookmarkFolderDialog()
71 // Add the bundle to the dialog.
72 createBookmarkFolderDialog.arguments = argumentsBundle
74 // Return the new dialog.
75 return createBookmarkFolderDialog
79 // Declare the class variables.
80 private lateinit var createBookmarkFolderListener: CreateBookmarkFolderListener
82 // The public interface is used to send information back to the parent activity.
83 interface CreateBookmarkFolderListener {
84 fun createBookmarkFolder(dialogFragment: DialogFragment, favoriteIconBitmap: Bitmap)
87 override fun onAttach(context: Context) {
88 // Run the default commands.
89 super.onAttach(context)
91 // Get a handle for the create bookmark folder listener from the launching context.
92 createBookmarkFolderListener = context as CreateBookmarkFolderListener
95 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
97 val arguments = requireArguments()
99 // Get the favorite icon byte array.
100 val favoriteIconByteArray = arguments.getByteArray(FAVORITE_ICON_BYTE_ARRAY)!!
102 // Convert the favorite icon byte array to a bitmap.
103 val favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.size)
105 // Use an alert dialog builder to create the dialog.
106 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
109 dialogBuilder.setTitle(R.string.create_folder)
112 dialogBuilder.setView(R.layout.create_bookmark_folder_dialog)
114 // Set a listener on the cancel button. Using `null` as the listener closes the dialog without doing anything else.
115 dialogBuilder.setNegativeButton(R.string.cancel, null)
117 // Set the create button listener.
118 dialogBuilder.setPositiveButton(R.string.create) { _: DialogInterface, _: Int ->
119 // Return the dialog fragment to the parent activity on create.
120 createBookmarkFolderListener.createBookmarkFolder(this, favoriteIconBitmap)
123 // Create an alert dialog from the builder.
124 val alertDialog = dialogBuilder.create()
126 // Get a handle for the shared preferences.
127 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
129 // Get the screenshot preference.
130 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
132 // Disable screenshots if not allowed.
133 if (!allowScreenshots) {
134 alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
137 // Display the keyboard.
138 alertDialog.window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
140 // The alert dialog must be shown before the content can be modified.
143 // Get handles for the views in the dialog.
144 val defaultIconLinearLayout = alertDialog.findViewById<LinearLayout>(R.id.default_icon_linearlayout)!!
145 val defaultIconRadioButton = alertDialog.findViewById<RadioButton>(R.id.default_icon_radiobutton)!!
146 val webpageFavoriteIconLinearLayout = alertDialog.findViewById<LinearLayout>(R.id.webpage_favorite_icon_linearlayout)!!
147 val webpageFavoriteIconRadioButton = alertDialog.findViewById<RadioButton>(R.id.webpage_favorite_icon_radiobutton)!!
148 val webpageFavoriteIconImageView = alertDialog.findViewById<ImageView>(R.id.webpage_favorite_icon_imageview)!!
149 val folderNameEditText = alertDialog.findViewById<EditText>(R.id.folder_name_edittext)!!
150 val createButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
152 // Display the current favorite icon.
153 webpageFavoriteIconImageView.setImageBitmap(favoriteIconBitmap)
155 // Initially disable the create button.
156 createButton.isEnabled = false
158 // Set the radio button listeners. These perform a click on the linear layout, which contains the necessary logic.
159 defaultIconRadioButton.setOnClickListener { defaultIconLinearLayout.performClick() }
160 webpageFavoriteIconRadioButton.setOnClickListener { webpageFavoriteIconLinearLayout.performClick() }
162 // Set the default icon linear layout click listener.
163 defaultIconLinearLayout.setOnClickListener {
164 // Check the default icon radio button.
165 defaultIconRadioButton.isChecked = true
167 // Uncheck the webpage favorite icon radio button.
168 webpageFavoriteIconRadioButton.isChecked = false
171 // Set the webpage favorite icon linear layout click listener.
172 webpageFavoriteIconLinearLayout.setOnClickListener {
173 // Check the webpage favorite icon radio button.
174 webpageFavoriteIconRadioButton.isChecked = true
176 // Uncheck the default icon radio button.
177 defaultIconRadioButton.isChecked = false
180 // Initialize the database helper.
181 val bookmarksDatabaseHelper = BookmarksDatabaseHelper(requireContext())
183 // Enable the create button if the new folder name is unique.
184 folderNameEditText.addTextChangedListener(object: TextWatcher {
185 override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
189 override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
193 override fun afterTextChanged(editable: Editable) {
194 // Convert the current text to a string.
195 val folderName = editable.toString()
197 // Check if a folder with the name already exists.
198 val folderExistsCursor = bookmarksDatabaseHelper.getFolder(folderName)
200 // Enable the create button if the new folder name is not empty and doesn't already exist.
201 createButton.isEnabled = folderName.isNotEmpty() && (folderExistsCursor.count == 0)
205 // Set the enter key on the keyboard to create the folder from the edit text.
206 folderNameEditText.setOnKeyListener { _: View?, keyCode: Int, keyEvent: KeyEvent ->
207 // Check the key code, event, and button status.
208 if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER && createButton.isEnabled) { // The event is a key-down on the enter key and the create button is enabled.
209 // Trigger the create bookmark folder listener and return the dialog fragment to the parent activity.
210 createBookmarkFolderListener.createBookmarkFolder(this, favoriteIconBitmap)
212 // Manually dismiss the alert dialog.
213 alertDialog.dismiss()
215 // Consume the event.
216 return@setOnKeyListener true
217 } else { // Some other key was pressed or the create button is disabled.
218 return@setOnKeyListener false
222 // Return the alert dialog.