2 * Copyright © 2016-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.annotation.SuppressLint
23 import android.app.Dialog
24 import android.content.Context
25 import android.content.DialogInterface
26 import android.graphics.Bitmap
27 import android.graphics.BitmapFactory
28 import android.os.Bundle
29 import android.text.Editable
30 import android.text.TextWatcher
31 import android.view.KeyEvent
32 import android.view.View
33 import android.view.WindowManager
34 import android.widget.EditText
35 import android.widget.ImageView
36 import androidx.appcompat.app.AlertDialog
38 import androidx.fragment.app.DialogFragment
39 import androidx.preference.PreferenceManager
41 import com.stoutner.privacybrowser.R
42 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper
44 import java.io.ByteArrayOutputStream
46 // Declare the class constants.
47 private const val FAVORITE_ICON_BYTE_ARRAY = "favorite_icon_byte_array"
49 class CreateBookmarkFolderDialog : DialogFragment() {
50 // Declare the class variables.
51 private lateinit var createBookmarkFolderListener: CreateBookmarkFolderListener
53 // The public interface is used to send information back to the parent activity.
54 interface CreateBookmarkFolderListener {
55 fun onCreateBookmarkFolder(dialogFragment: DialogFragment, favoriteIconBitmap: Bitmap)
58 override fun onAttach(context: Context) {
59 // Run the default commands.
60 super.onAttach(context)
62 // Get a handle for the create bookmark folder listener from the launching context.
63 createBookmarkFolderListener = context as CreateBookmarkFolderListener
67 // `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin.
69 fun createBookmarkFolder(favoriteIconBitmap: Bitmap): CreateBookmarkFolderDialog {
70 // Create a favorite icon byte array output stream.
71 val favoriteIconByteArrayOutputStream = ByteArrayOutputStream()
73 // 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).
74 favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream)
76 // Convert the byte array output stream to a byte array.
77 val favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray()
79 // Create an arguments bundle.
80 val argumentsBundle = Bundle()
82 // Store the favorite icon in the bundle.
83 argumentsBundle.putByteArray(FAVORITE_ICON_BYTE_ARRAY, favoriteIconByteArray)
85 // Create a new instance of the dialog.
86 val createBookmarkFolderDialog = CreateBookmarkFolderDialog()
88 // Add the bundle to the dialog.
89 createBookmarkFolderDialog.arguments = argumentsBundle
91 // Return the new dialog.
92 return createBookmarkFolderDialog
96 // `@SuppressLint("InflateParams")` removes the warning about using `null` as the parent view group when inflating the alert dialog.
97 @SuppressLint("InflateParams")
98 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
100 val arguments = requireArguments()
102 // Get the favorite icon byte array.
103 val favoriteIconByteArray = arguments.getByteArray(FAVORITE_ICON_BYTE_ARRAY)!!
105 // Convert the favorite icon byte array to a bitmap.
106 val favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.size)
108 // Use an alert dialog builder to create the dialog.
109 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
112 dialogBuilder.setTitle(R.string.create_folder)
114 // Set the view. The parent view is null because it will be assigned by the alert dialog.
115 dialogBuilder.setView(requireActivity().layoutInflater.inflate(R.layout.create_bookmark_folder_dialog, null))
117 // Set a listener on the cancel button. Using `null` as the listener closes the dialog without doing anything else.
118 dialogBuilder.setNegativeButton(R.string.cancel, null)
120 // Set a listener on the create button.
121 dialogBuilder.setPositiveButton(R.string.create) { _: DialogInterface, _: Int ->
122 // Return the dialog fragment to the parent activity on create.
123 createBookmarkFolderListener.onCreateBookmarkFolder(this, favoriteIconBitmap)
126 // Create an alert dialog from the builder.
127 val alertDialog = dialogBuilder.create()
129 // Get a handle for the shared preferences.
130 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
132 // Get the screenshot preference.
133 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
135 // Disable screenshots if not allowed.
136 if (!allowScreenshots) {
137 alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
140 // Display the keyboard.
141 alertDialog.window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
143 // The alert dialog must be shown before the content can be modified.
146 // Get handles for the views in the dialog.
147 val webPageIconImageView = alertDialog.findViewById<ImageView>(R.id.create_folder_web_page_icon)!!
148 val folderNameEditText = alertDialog.findViewById<EditText>(R.id.create_folder_name_edittext)!!
149 val createButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
151 // Display the current favorite icon.
152 webPageIconImageView.setImageBitmap(favoriteIconBitmap)
154 // Initially disable the create button.
155 createButton.isEnabled = false
157 // Initialize the database helper. The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
158 val bookmarksDatabaseHelper = BookmarksDatabaseHelper(context, null, null, 0)
160 // Enable the create button if the new folder name is unique.
161 folderNameEditText.addTextChangedListener(object: TextWatcher {
162 override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
166 override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
170 override fun afterTextChanged(editable: Editable) {
171 // Convert the current text to a string.
172 val folderName = editable.toString()
174 // Check if a folder with the name already exists.
175 val folderExistsCursor = bookmarksDatabaseHelper.getFolder(folderName)
177 // Enable the create button if the new folder name is not empty and doesn't already exist.
178 createButton.isEnabled = folderName.isNotEmpty() && (folderExistsCursor.count == 0)
182 // Set the enter key on the keyboard to create the folder from the edit text.
183 folderNameEditText.setOnKeyListener { _: View?, keyCode: Int, keyEvent: KeyEvent ->
184 // Check the key code, event, and button status.
185 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.
186 // Trigger the create bookmark folder listener and return the dialog fragment to the parent activity.
187 createBookmarkFolderListener.onCreateBookmarkFolder(this, favoriteIconBitmap)
189 // Manually dismiss the alert dialog.
190 alertDialog.dismiss()
192 // Consume the event.
193 return@setOnKeyListener true
194 } else { // Some other key was pressed or the create button is disabled.
195 return@setOnKeyListener false
199 // Return the alert dialog.