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.database.Cursor
27 import android.database.DatabaseUtils
28 import android.database.MatrixCursor
29 import android.database.MergeCursor
30 import android.graphics.Bitmap
31 import android.graphics.BitmapFactory
32 import android.os.Bundle
33 import android.text.Editable
34 import android.text.TextWatcher
35 import android.view.KeyEvent
36 import android.view.View
37 import android.view.WindowManager
38 import android.widget.*
39 import android.widget.AdapterView.OnItemSelectedListener
41 import androidx.appcompat.app.AlertDialog
42 import androidx.core.content.ContextCompat
43 import androidx.cursoradapter.widget.ResourceCursorAdapter
44 import androidx.fragment.app.DialogFragment
45 import androidx.preference.PreferenceManager
47 import com.stoutner.privacybrowser.R
48 import com.stoutner.privacybrowser.activities.BookmarksDatabaseViewActivity
49 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper
51 import java.io.ByteArrayOutputStream
53 // Define the class constants.
54 private const val DATABASE_ID = "database_id"
55 private const val FAVORITE_ICON_BYTE_ARRAY = "favorite_icon_byte_array"
57 class EditBookmarkFolderDatabaseViewDialog : DialogFragment() {
58 // Declare the class variables.
59 private lateinit var editBookmarkFolderDatabaseViewListener: EditBookmarkFolderDatabaseViewListener
61 // Declare the class views.
62 private lateinit var currentIconRadioButton: RadioButton
63 private lateinit var nameEditText: EditText
64 private lateinit var parentFolderSpinner: Spinner
65 private lateinit var displayOrderEditText: EditText
66 private lateinit var saveButton: Button
68 // The public interface is used to send information back to the parent activity.
69 interface EditBookmarkFolderDatabaseViewListener {
70 fun onSaveBookmarkFolder(dialogFragment: DialogFragment, selectedFolderDatabaseId: Int, favoriteIconBitmap: Bitmap)
73 override fun onAttach(context: Context) {
74 // Run the default commands.
75 super.onAttach(context)
77 // Get a handle for edit bookmark database view listener from the launching context.
78 editBookmarkFolderDatabaseViewListener = context as EditBookmarkFolderDatabaseViewListener
82 // `@JvmStatic` will no longer be required once all the code has transitioned to Kotlin.
84 fun folderDatabaseId(databaseId: Int, favoriteIconBitmap: Bitmap): EditBookmarkFolderDatabaseViewDialog {
85 // Create a favorite icon byte array output stream.
86 val favoriteIconByteArrayOutputStream = ByteArrayOutputStream()
88 // 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).
89 favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream)
91 // Convert the byte array output stream to a byte array.
92 val favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray()
94 // Create an arguments bundle.
95 val argumentsBundle = Bundle()
97 // Store the variables in the bundle.
98 argumentsBundle.putInt(DATABASE_ID, databaseId)
99 argumentsBundle.putByteArray(FAVORITE_ICON_BYTE_ARRAY, favoriteIconByteArray)
101 // Create a new instance of the dialog.
102 val editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog()
104 // Add the arguments bundle to the dialog.
105 editBookmarkFolderDatabaseViewDialog.arguments = argumentsBundle
107 // Return the new dialog.
108 return editBookmarkFolderDatabaseViewDialog
112 // `@SuppressLint("InflateParams")` removes the warning about using `null` as the parent view group when inflating the alert dialog.
113 @SuppressLint("InflateParams")
114 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
115 // Get a handle for the arguments.
116 val arguments = requireArguments()
118 // Get the variables from the arguments.
119 val folderDatabaseId = arguments.getInt(DATABASE_ID)
120 val favoriteIconByteArray = arguments.getByteArray(FAVORITE_ICON_BYTE_ARRAY)!!
122 // Convert the favorite icon byte array to a bitmap.
123 val favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.size)
125 // Initialize the bookmarks database helper. The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
126 val bookmarksDatabaseHelper = BookmarksDatabaseHelper(context, null, null, 0)
128 // Get a cursor with the selected bookmark.
129 val folderCursor = bookmarksDatabaseHelper.getBookmark(folderDatabaseId)
131 // Move the cursor to the first position.
132 folderCursor.moveToFirst()
134 // Use an alert dialog builder to create the alert dialog.
135 val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
138 dialogBuilder.setTitle(R.string.edit_folder)
140 // Set the view. The parent view is `null` because it will be assigned by the alert dialog.
141 dialogBuilder.setView(layoutInflater.inflate(R.layout.edit_bookmark_folder_databaseview_dialog, null))
143 // Set the cancel button listener. Using `null` as the listener closes the dialog without doing anything else.
144 dialogBuilder.setNegativeButton(R.string.cancel, null)
146 // Set the save button listener.
147 dialogBuilder.setPositiveButton(R.string.save) { _: DialogInterface?, _: Int ->
148 // Return the dialog fragment to the parent activity.
149 editBookmarkFolderDatabaseViewListener.onSaveBookmarkFolder(this, folderDatabaseId, favoriteIconBitmap)
152 // Create an alert dialog from the alert dialog builder.
153 val alertDialog = dialogBuilder.create()
155 // Get a handle for the shared preferences.
156 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
158 // Get the screenshot preference.
159 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
161 // Disable screenshots if not allowed.
162 if (!allowScreenshots) {
163 alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
166 // The alert dialog must be shown before items in the layout can be modified.
169 // Get handles for the views in the alert dialog.
170 val databaseIdTextView = alertDialog.findViewById<TextView>(R.id.folder_database_id_textview)!!
171 val currentIconLinearLayout = alertDialog.findViewById<LinearLayout>(R.id.current_icon_linearlayout)!!
172 currentIconRadioButton = alertDialog.findViewById(R.id.current_icon_radiobutton)!!
173 val currentIconImageView = alertDialog.findViewById<ImageView>(R.id.current_icon_imageview)!!
174 val defaultIconLinearLayout = alertDialog.findViewById<LinearLayout>(R.id.default_icon_linearlayout)!!
175 val defaultIconRadioButton = alertDialog.findViewById<RadioButton>(R.id.default_icon_radiobutton)!!
176 val webpageFavoriteIconLinearLayout = alertDialog.findViewById<LinearLayout>(R.id.webpage_favorite_icon_linearlayout)!!
177 val webpageFavoriteIconRadioButton = alertDialog.findViewById<RadioButton>(R.id.webpage_favorite_icon_radiobutton)!!
178 val webpageFavoriteIconImageView = alertDialog.findViewById<ImageView>(R.id.webpage_favorite_icon_imageview)!!
179 nameEditText = alertDialog.findViewById(R.id.folder_name_edittext)!!
180 parentFolderSpinner = alertDialog.findViewById(R.id.parent_folder_spinner)!!
181 displayOrderEditText = alertDialog.findViewById(R.id.display_order_edittext)!!
182 saveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
184 // Store the current folder values.
185 val currentFolderName = folderCursor.getString(folderCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME))
186 val currentDisplayOrder = folderCursor.getInt(folderCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER))
187 val parentFolder = folderCursor.getString(folderCursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER))
189 // Populate the database ID text view.
190 databaseIdTextView.text = folderCursor.getInt(folderCursor.getColumnIndex(BookmarksDatabaseHelper._ID)).toString()
192 // Get the current favorite icon byte array from the cursor.
193 val currentIconByteArray = folderCursor.getBlob(folderCursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON))
195 // Convert the byte array to a bitmap beginning at the first byte and ending at the last.
196 val currentIconBitmap = BitmapFactory.decodeByteArray(currentIconByteArray, 0, currentIconByteArray.size)
198 // Populate the current icon image view.
199 currentIconImageView.setImageBitmap(currentIconBitmap)
201 // Populate the webpage favorite icon image view.
202 webpageFavoriteIconImageView.setImageBitmap(favoriteIconBitmap)
204 // Populate the folder name edit text.
205 nameEditText.setText(currentFolderName)
207 // Define an array of matrix cursor column names.
208 val matrixCursorColumnNames = arrayOf(BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME)
210 // Create a matrix cursor.
211 val matrixCursor = MatrixCursor(matrixCursorColumnNames)
213 // Add `Home Folder` to the matrix cursor.
214 matrixCursor.addRow(arrayOf(BookmarksDatabaseViewActivity.HOME_FOLDER_DATABASE_ID, getString(R.string.home_folder)))
216 // Get a string of the current folder and all subfolders.
217 val currentAndSubfolderString = getStringOfSubfolders(currentFolderName, bookmarksDatabaseHelper)
219 // Get a cursor with the list of all the folders.
220 val foldersCursor = bookmarksDatabaseHelper.getFoldersExcept(currentAndSubfolderString)
222 // Combine the matrix cursor and the folders cursor.
223 val combinedFoldersCursor = MergeCursor(arrayOf(matrixCursor, foldersCursor))
225 // Create a resource cursor adapter for the spinner.
226 val foldersCursorAdapter: ResourceCursorAdapter = object: ResourceCursorAdapter(context, R.layout.databaseview_spinner_item, combinedFoldersCursor, 0) {
227 override fun bindView(view: View, context: Context, cursor: Cursor) {
228 // Get handles for the spinner views.
229 val spinnerItemImageView = view.findViewById<ImageView>(R.id.spinner_item_imageview)
230 val spinnerItemTextView = view.findViewById<TextView>(R.id.spinner_item_textview)
232 // Set the folder icon according to the type.
233 if (combinedFoldersCursor.position == 0) { // Set the `Home Folder` icon.
234 // Set the gray folder image. `ContextCompat` must be used until the minimum API >= 21.
235 spinnerItemImageView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.folder_gray))
236 } else { // Set a user folder icon.
237 // Get the folder icon byte array.
238 val folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON))
240 // Convert the byte array to a bitmap beginning at the first byte and ending at the last.
241 val folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.size)
243 // Set the folder icon.
244 spinnerItemImageView.setImageBitmap(folderIconBitmap)
247 // Set the text view to display the folder name.
248 spinnerItemTextView.text = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME))
252 // Set the folders cursor adapter drop drown view resource.
253 foldersCursorAdapter.setDropDownViewResource(R.layout.databaseview_spinner_dropdown_items)
255 // Set the parent folder spinner adapter.
256 parentFolderSpinner.adapter = foldersCursorAdapter
258 // Select the current folder in the spinner if the bookmark isn't in the "Home Folder".
259 if (parentFolder != "") {
260 // Get the database ID of the parent folder as a long.
261 val parentFolderDatabaseId = bookmarksDatabaseHelper.getFolderDatabaseId(folderCursor.getString(folderCursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER))).toLong()
263 // Initialize the parent folder position and the iteration variable.
264 var parentFolderPosition = 0
267 // Find the parent folder position in the folders cursor adapter.
269 if (foldersCursorAdapter.getItemId(i) == parentFolderDatabaseId) {
270 // Store the current position for the parent folder.
271 parentFolderPosition = i
273 // Try the next entry.
276 // Stop when the parent folder position is found or all the items in the folders cursor adapter have been checked.
277 } while (parentFolderPosition == 0 && i < foldersCursorAdapter.count)
279 // Select the parent folder in the spinner.
280 parentFolderSpinner.setSelection(parentFolderPosition)
283 // Store the current folder database ID.
284 val currentParentFolderDatabaseId = parentFolderSpinner.selectedItemId.toInt()
286 // Populate the display order edit text.
287 displayOrderEditText.setText(folderCursor.getInt(folderCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)).toString())
289 // Initially disable the edit button.
290 saveButton.isEnabled = false
292 // Set the radio button listeners. These perform a click on the linear layout, which contains the necessary logic.
293 currentIconRadioButton.setOnClickListener { currentIconLinearLayout.performClick() }
294 defaultIconRadioButton.setOnClickListener { defaultIconLinearLayout.performClick() }
295 webpageFavoriteIconRadioButton.setOnClickListener { webpageFavoriteIconLinearLayout.performClick() }
297 // Set the current icon linear layout click listener.
298 currentIconLinearLayout.setOnClickListener {
299 // Check the current icon radio button.
300 currentIconRadioButton.isChecked = true
302 // Uncheck the other radio buttons.
303 defaultIconRadioButton.isChecked = false
304 webpageFavoriteIconRadioButton.isChecked = false
306 // Update the save button.
307 updateSaveButton(bookmarksDatabaseHelper, currentFolderName, currentParentFolderDatabaseId, currentDisplayOrder)
310 // Set the default icon linear layout click listener.
311 defaultIconLinearLayout.setOnClickListener {
312 // Check the default icon radio button.
313 defaultIconRadioButton.isChecked = true
315 // Uncheck the other radio buttons.
316 currentIconRadioButton.isChecked = false
317 webpageFavoriteIconRadioButton.isChecked = false
319 // Update the save button.
320 updateSaveButton(bookmarksDatabaseHelper, currentFolderName, currentParentFolderDatabaseId, currentDisplayOrder)
323 // Set the webpage favorite icon linear layout click listener.
324 webpageFavoriteIconLinearLayout.setOnClickListener {
325 // Check the webpage favorite icon radio button.
326 webpageFavoriteIconRadioButton.isChecked = true
328 // Uncheck the other radio buttons.
329 currentIconRadioButton.isChecked = false
330 defaultIconRadioButton.isChecked = false
332 // Update the save button.
333 updateSaveButton(bookmarksDatabaseHelper, currentFolderName, currentParentFolderDatabaseId, currentDisplayOrder)
336 // Update the save button if the bookmark name changes.
337 nameEditText.addTextChangedListener(object: TextWatcher {
338 override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
342 override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
346 override fun afterTextChanged(s: Editable) {
347 // Update the save button.
348 updateSaveButton(bookmarksDatabaseHelper, currentFolderName, currentParentFolderDatabaseId, currentDisplayOrder)
352 // Wait to set the on item selected listener until the spinner has been inflated. Otherwise the dialog will crash on restart.
353 parentFolderSpinner.post {
354 // Update the save button if the parent folder changes.
355 parentFolderSpinner.onItemSelectedListener = object: OnItemSelectedListener {
356 override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
357 // Update the save button.
358 updateSaveButton(bookmarksDatabaseHelper, currentFolderName, currentParentFolderDatabaseId, currentDisplayOrder)
361 override fun onNothingSelected(parent: AdapterView<*>) {
367 // Update the save button if the display order changes.
368 displayOrderEditText.addTextChangedListener(object: TextWatcher {
369 override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
373 override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
377 override fun afterTextChanged(s: Editable) {
378 // Update the save button.
379 updateSaveButton(bookmarksDatabaseHelper, currentFolderName, currentParentFolderDatabaseId, currentDisplayOrder)
383 // Allow the enter key on the keyboard to save the bookmark from the bookmark name edit text.
384 nameEditText.setOnKeyListener { _: View?, keyCode: Int, event: KeyEvent ->
385 // Check the key code, event, and button status.
386 if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER && saveButton.isEnabled) { // The enter key was pressed and the save button is enabled.
387 // Trigger the listener and return the dialog fragment to the parent activity.
388 editBookmarkFolderDatabaseViewListener.onSaveBookmarkFolder(this, folderDatabaseId, favoriteIconBitmap)
390 // Manually dismiss the alert dialog.
391 alertDialog.dismiss()
393 // Consume the event.
394 return@setOnKeyListener true
395 } else { // If any other key was pressed, or if the save button is currently disabled, do not consume the event.
396 return@setOnKeyListener false
400 // Allow the enter key on the keyboard to save the bookmark from the display order edit text.
401 displayOrderEditText.setOnKeyListener { _: View?, keyCode: Int, event: KeyEvent ->
402 // Check the key code, event, and button status.
403 if (event.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER && saveButton.isEnabled) { // The enter key was pressed and the save button is enabled.
404 // Trigger the listener and return the dialog fragment to the parent activity.
405 editBookmarkFolderDatabaseViewListener.onSaveBookmarkFolder(this, folderDatabaseId, favoriteIconBitmap)
407 // Manually dismiss the alert dialog.
408 alertDialog.dismiss()
410 // Consume the event.
411 return@setOnKeyListener true
412 } else { // If any other key was pressed, or if the save button is currently disabled, do not consume the event.
413 return@setOnKeyListener false
417 // Return the alert dialog.
421 private fun updateSaveButton(bookmarksDatabaseHelper: BookmarksDatabaseHelper, currentFolderName: String, currentParentFolderDatabaseId: Int, currentDisplayOrder: Int) {
422 // Get the values from the views.
423 val newFolderName = nameEditText.text.toString()
424 val newParentFolderDatabaseId = parentFolderSpinner.selectedItemId.toInt()
425 val newDisplayOrder = displayOrderEditText.text.toString()
427 // Get a cursor for the new folder name if it exists.
428 val folderExistsCursor = bookmarksDatabaseHelper.getFolder(newFolderName)
430 // Is the new folder name empty?
431 val folderNameNotEmpty = newFolderName.isNotEmpty()
433 // Does the folder name already exist?
434 val folderNameAlreadyExists = (newFolderName != currentFolderName) && folderExistsCursor.count > 0
436 // Has the favorite icon changed?
437 val iconChanged = !currentIconRadioButton.isChecked
439 // Has the folder been renamed?
440 val folderRenamed = (newFolderName != currentFolderName) && !folderNameAlreadyExists
442 // Has the parent folder changed?
443 val parentFolderChanged = newParentFolderDatabaseId != currentParentFolderDatabaseId
445 // Has the display order changed?
446 val displayOrderChanged = newDisplayOrder != currentDisplayOrder.toString()
448 // Is the display order empty?
449 val displayOrderNotEmpty = newDisplayOrder.isNotEmpty()
451 // Update the enabled status of the edit button.
452 saveButton.isEnabled = (iconChanged || folderRenamed || parentFolderChanged || displayOrderChanged) && folderNameNotEmpty && displayOrderNotEmpty
455 private fun getStringOfSubfolders(folderName: String, bookmarksDatabaseHelper: BookmarksDatabaseHelper): String {
456 // Get a cursor will all the immediate subfolders.
457 val subfoldersCursor = bookmarksDatabaseHelper.getSubfolders(folderName)
459 // Initialize the subfolder string builder and populate it with the current folder.
460 val currentAndSubfolderStringBuilder = StringBuilder(DatabaseUtils.sqlEscapeString(folderName))
462 // Populate the subfolder string builder
463 for (i in 0 until subfoldersCursor.count) {
464 // Move the subfolder cursor to the current item.
465 subfoldersCursor.moveToPosition(i)
467 // Get the name of the subfolder.
468 val subfolderName = subfoldersCursor.getString(subfoldersCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME))
470 // Add a comma to the end of the existing string.
471 currentAndSubfolderStringBuilder.append(",")
473 // Get the folder name and run the task for any subfolders.
474 val subfolderString = getStringOfSubfolders(subfolderName, bookmarksDatabaseHelper)
476 // Add the folder name to the string builder.
477 currentAndSubfolderStringBuilder.append(subfolderString)
480 // Return the string of folders.
481 return currentAndSubfolderStringBuilder.toString()