]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/dialogs/MoveToFolderDialog.kt
First wrong button text in View Headers in night theme. https://redmine.stoutner...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / dialogs / MoveToFolderDialog.kt
1 /*
2  * Copyright 2016-2023 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.dialogs
21
22 import android.app.Dialog
23 import android.content.Context
24 import android.content.DialogInterface
25 import android.database.Cursor
26 import android.database.MatrixCursor
27 import android.database.MergeCursor
28 import android.graphics.Bitmap
29 import android.graphics.BitmapFactory
30 import android.graphics.drawable.BitmapDrawable
31 import android.os.Bundle
32 import android.view.View
33 import android.view.ViewGroup
34 import android.view.WindowManager
35 import android.widget.AdapterView
36 import android.widget.AdapterView.OnItemClickListener
37 import android.widget.ImageView
38 import android.widget.ListView
39 import android.widget.TextView
40
41 import androidx.appcompat.app.AlertDialog
42 import androidx.core.content.ContextCompat
43 import androidx.cursoradapter.widget.CursorAdapter
44 import androidx.fragment.app.DialogFragment
45 import androidx.preference.PreferenceManager
46
47 import com.stoutner.privacybrowser.R
48 import com.stoutner.privacybrowser.activities.HOME_FOLDER_DATABASE_ID
49 import com.stoutner.privacybrowser.activities.HOME_FOLDER_ID
50 import com.stoutner.privacybrowser.helpers.BOOKMARK_NAME
51 import com.stoutner.privacybrowser.helpers.FAVORITE_ICON
52 import com.stoutner.privacybrowser.helpers.FOLDER_ID
53 import com.stoutner.privacybrowser.helpers.ID
54 import com.stoutner.privacybrowser.helpers.PARENT_FOLDER_ID
55 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper
56
57 import kotlinx.coroutines.CoroutineScope
58 import kotlinx.coroutines.Dispatchers
59 import kotlinx.coroutines.launch
60 import kotlinx.coroutines.withContext
61
62 import java.io.ByteArrayOutputStream
63
64 // Define the class constants.
65 private const val CURRENT_FOLDER_ID = "A"
66 private const val SELECTED_BOOKMARKS_LONG_ARRAY = "B"
67
68 class MoveToFolderDialog : DialogFragment() {
69     companion object {
70         fun moveBookmarks(currentFolderId: Long, selectedBookmarksLongArray: LongArray): MoveToFolderDialog {
71             // Create an arguments bundle.
72             val argumentsBundle = Bundle()
73
74             // Store the arguments in the bundle.
75             argumentsBundle.putLong(CURRENT_FOLDER_ID, currentFolderId)
76             argumentsBundle.putLongArray(SELECTED_BOOKMARKS_LONG_ARRAY, selectedBookmarksLongArray)
77
78             // Create a new instance of the dialog.
79             val moveToFolderDialog = MoveToFolderDialog()
80
81             // And the bundle to the dialog.
82             moveToFolderDialog.arguments = argumentsBundle
83
84             // Return the new dialog.
85             return moveToFolderDialog
86         }
87     }
88
89     // Declare the class variables.
90     private lateinit var moveToFolderListener: MoveToFolderListener
91     private lateinit var bookmarksDatabaseHelper: BookmarksDatabaseHelper
92
93     // The public interface is used to send information back to the parent activity.
94     interface MoveToFolderListener {
95         fun onMoveToFolder(dialogFragment: DialogFragment)
96     }
97
98     override fun onAttach(context: Context) {
99         // Run the default commands.
100         super.onAttach(context)
101
102         // Get a handle for the move to folder listener from the launching context.
103         moveToFolderListener = context as MoveToFolderListener
104     }
105
106     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
107         // Get the data from the arguments.
108         val currentFolderId = requireArguments().getLong(CURRENT_FOLDER_ID, HOME_FOLDER_ID)
109         val selectedBookmarksLongArray = requireArguments().getLongArray(SELECTED_BOOKMARKS_LONG_ARRAY)!!
110
111         // Initialize the database helper.
112         bookmarksDatabaseHelper = BookmarksDatabaseHelper(requireContext())
113
114         // Use an alert dialog builder to create the alert dialog.
115         val dialogBuilder = AlertDialog.Builder(requireContext(), R.style.PrivacyBrowserAlertDialog)
116
117         // Set the icon.
118         dialogBuilder.setIcon(R.drawable.move_to_folder_blue)
119
120         // Set the title.
121         dialogBuilder.setTitle(R.string.move_to_folder)
122
123         // Set the view.
124         dialogBuilder.setView(R.layout.move_to_folder_dialog)
125
126         // Set the listener for the cancel button.  Using `null` as the listener closes the dialog without doing anything else.
127         dialogBuilder.setNegativeButton(R.string.cancel, null)
128
129         // Set the listener fo the move button.
130         dialogBuilder.setPositiveButton(R.string.move) { _: DialogInterface?, _: Int ->
131             // Return the dialog fragment to the parent activity on move.
132             moveToFolderListener.onMoveToFolder(this)
133         }
134
135         // Create an alert dialog from the alert dialog builder.
136         val alertDialog = dialogBuilder.create()
137
138         // Get a handle for the shared preferences.
139         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
140
141         // Get the screenshot preference.
142         val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
143
144         // Disable screenshots if not allowed.
145         if (!allowScreenshots) {
146             // Disable screenshots.
147             alertDialog.window!!.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
148         }
149
150         // The alert dialog must be shown before items in the layout can be modified.
151         alertDialog.show()
152
153         // Get a handle for the positive button.
154         val moveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
155
156         // Initially disable the positive button.
157         moveButton.isEnabled = false
158
159         // Create a list of folders not to display.
160         val folderIdsNotToDisplay = mutableListOf<Long>()
161
162         // Add any selected folders and their subfolders to the list of folders not to display.
163         for (databaseIdLong in selectedBookmarksLongArray) {
164             // Get the database ID int for each selected bookmark.
165             val databaseIdInt = databaseIdLong.toInt()
166
167             // Check to see if the bookmark is a folder.
168             if (bookmarksDatabaseHelper.isFolder(databaseIdInt)) {
169                 // Add the folder to the list of folders not to display.
170                 folderIdsNotToDisplay.add(bookmarksDatabaseHelper.getFolderId(databaseIdInt))
171             }
172         }
173
174         // Check to see if the bookmark is currently in the home folder.
175         if (currentFolderId == HOME_FOLDER_ID) {  // The bookmark is currently in the home folder.  Don't display `Home Folder` at the top of the list view.
176             // Get a cursor containing the folders to display.
177             val foldersCursor = bookmarksDatabaseHelper.getFoldersExcept(folderIdsNotToDisplay)
178
179             // Populate the folders cursor adapter.
180             val foldersCursorAdapter = populateFoldersCursorAdapter(requireContext(), foldersCursor)
181
182             // Get a handle for the folders list view.
183             val foldersListView = alertDialog.findViewById<ListView>(R.id.move_to_folder_listview)!!
184
185             // Set the folder list view adapter.
186             foldersListView.adapter = foldersCursorAdapter
187
188             // Enable the move button when a folder is selected.
189             foldersListView.onItemClickListener = OnItemClickListener { _: AdapterView<*>?, _: View?, _: Int, _: Long ->
190                 // Enable the move button.
191                 moveButton.isEnabled = true
192             }
193         } else {  // The current folder is not directly in the home folder.  Display `Home Folder` at the top of the list view.
194             // Get the home folder icon drawable.
195             val homeFolderIconDrawable = ContextCompat.getDrawable(requireActivity().applicationContext, R.drawable.folder_gray_bitmap)
196
197             // Convert the home folder icon drawable to a bitmap drawable.
198             val homeFolderIconBitmapDrawable = homeFolderIconDrawable as BitmapDrawable
199
200             // Convert the home folder bitmap drawable to a bitmap.
201             val homeFolderIconBitmap = homeFolderIconBitmapDrawable.bitmap
202
203             // Create a home folder icon byte array output stream.
204             val homeFolderIconByteArrayOutputStream = ByteArrayOutputStream()
205
206             // Compress the bitmap using a coroutine with Dispatchers.Default.
207             CoroutineScope(Dispatchers.Main).launch {
208                 withContext(Dispatchers.Default) {
209                     // Convert the home folder bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
210                     homeFolderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, homeFolderIconByteArrayOutputStream)
211
212                     // Convert the home folder icon byte array output stream to a byte array.
213                     val homeFolderIconByteArray = homeFolderIconByteArrayOutputStream.toByteArray()
214
215                     // Setup the home folder matrix cursor column names.
216                     val homeFolderMatrixCursorColumnNames = arrayOf(ID, BOOKMARK_NAME, FAVORITE_ICON, PARENT_FOLDER_ID)
217
218                     // Setup a matrix cursor for the `Home Folder`.
219                     val homeFolderMatrixCursor = MatrixCursor(homeFolderMatrixCursorColumnNames)
220
221                     // Add the home folder to the home folder matrix cursor.
222                     homeFolderMatrixCursor.addRow(arrayOf<Any>(HOME_FOLDER_DATABASE_ID, getString(R.string.home_folder), homeFolderIconByteArray, HOME_FOLDER_ID))
223
224                     // Add the current folder to the list of folders not to display.
225                     folderIdsNotToDisplay.add(currentFolderId)
226
227                     // Get a cursor containing the folders to display.
228                     val foldersCursor = bookmarksDatabaseHelper.getFoldersExcept(folderIdsNotToDisplay)
229
230                     // Combine the home folder matrix cursor and the folders cursor.
231                     val foldersMergeCursor = MergeCursor(arrayOf(homeFolderMatrixCursor, foldersCursor))
232
233                     // Populate the folders cursor on the main thread.
234                     withContext(Dispatchers.Main) {
235                         // Populate the folders cursor adapter.
236                         val foldersCursorAdapter = populateFoldersCursorAdapter(requireContext(), foldersMergeCursor)
237
238                         // Get a handle for the folders list view.
239                         val foldersListView = alertDialog.findViewById<ListView>(R.id.move_to_folder_listview)!!
240
241                         // Set the folder list view adapter.
242                         foldersListView.adapter = foldersCursorAdapter
243
244                         // Enable the move button when a folder is selected.
245                         foldersListView.onItemClickListener = OnItemClickListener { _: AdapterView<*>?, _: View?, _: Int, _: Long ->
246                             // Enable the move button.
247                             moveButton.isEnabled = true
248                         }
249                     }
250                 }
251             }
252         }
253
254         // Return the alert dialog.
255         return alertDialog
256     }
257
258     private fun populateFoldersCursorAdapter(context: Context, cursor: Cursor): CursorAdapter {
259         // Return the folders cursor adapter.
260         return object : CursorAdapter(context, cursor, false) {
261             override fun newView(context: Context, cursor: Cursor, parent: ViewGroup): View {
262                 // Inflate the individual item layout.
263                 return requireActivity().layoutInflater.inflate(R.layout.move_to_folder_item_linearlayout, parent, false)
264             }
265
266             override fun bindView(view: View, context: Context, cursor: Cursor) {
267                 // Get the data from the cursor.
268                 val folderIconByteArray = cursor.getBlob(cursor.getColumnIndexOrThrow(FAVORITE_ICON))
269                 val folderName = cursor.getString(cursor.getColumnIndexOrThrow(BOOKMARK_NAME))
270
271                 // Get handles for the views.
272                 val subfolderSpacerTextView = view.findViewById<TextView>(R.id.subfolder_spacer_textview)
273                 val folderIconImageView = view.findViewById<ImageView>(R.id.folder_icon_imageview)
274                 val folderNameTextView = view.findViewById<TextView>(R.id.folder_name_textview)
275
276                 // Populate the subfolder spacer.
277                 if (cursor.getLong(cursor.getColumnIndexOrThrow(PARENT_FOLDER_ID)) != HOME_FOLDER_ID) {  // The folder is not in the home folder.
278                     // Get the subfolder spacer.
279                     subfolderSpacerTextView.text = bookmarksDatabaseHelper.getSubfolderSpacer(cursor.getLong(cursor.getColumnIndexOrThrow(FOLDER_ID)))
280                 } else {  // The folder is in the home folder.
281                     // Reset the subfolder spacer.
282                     subfolderSpacerTextView.text = ""
283                 }
284
285                 // Convert the byte array to a bitmap beginning at the first byte and ending at the last.
286                 val folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.size)
287
288                 // Display the folder icon bitmap.
289                 folderIconImageView.setImageBitmap(folderIconBitmap)
290
291                 // Display the folder name.
292                 folderNameTextView.text = folderName
293             }
294         }
295     }
296 }