1f011f72618dc53dfb8ac55a0ee70de24e8ccab3
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / activities / BookmarksDatabaseViewActivity.java
1 /*
2  * Copyright © 2016-2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.activities;
21
22 import android.content.Context;
23 import android.database.Cursor;
24 import android.database.MatrixCursor;
25 import android.database.MergeCursor;
26 import android.graphics.Bitmap;
27 import android.graphics.BitmapFactory;
28 import android.graphics.Typeface;
29 import android.graphics.drawable.BitmapDrawable;
30 import android.graphics.drawable.Drawable;
31 import android.os.Bundle;
32 import android.support.design.widget.Snackbar;
33 import android.support.v4.app.NavUtils;
34 import android.support.v4.content.ContextCompat;
35 import android.support.v4.widget.CursorAdapter;
36 import android.support.v4.widget.ResourceCursorAdapter;
37 import android.support.v7.app.ActionBar;
38 import android.support.v7.app.AppCompatActivity;
39 // `AppCompatDialogFragment` is required instead of `DialogFragment` or an error is produced on API <=22.
40 import android.support.v7.app.AppCompatDialogFragment;
41 import android.support.v7.widget.Toolbar;
42 import android.view.Menu;
43 import android.view.MenuItem;
44 import android.view.View;
45 import android.view.ViewGroup;
46 import android.view.WindowManager;
47 import android.widget.AdapterView;
48 import android.widget.EditText;
49 import android.widget.ImageView;
50 import android.widget.ListView;
51 import android.widget.RadioButton;
52 import android.widget.Spinner;
53 import android.widget.TextView;
54
55 import com.stoutner.privacybrowser.R;
56 import com.stoutner.privacybrowser.dialogs.EditBookmarkDatabaseViewDialog;
57 import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDatabaseViewDialog;
58 import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper;
59
60 import java.io.ByteArrayOutputStream;
61
62 public class BookmarksDatabaseViewActivity extends AppCompatActivity implements EditBookmarkDatabaseViewDialog.EditBookmarkDatabaseViewListener, EditBookmarkFolderDatabaseViewDialog.EditBookmarkFolderDatabaseViewListener {
63     // Instantiate the constants.
64     private static final int ALL_FOLDERS_DATABASE_ID = -2;
65     private static final int HOME_FOLDER_DATABASE_ID = -1;
66
67     // `bookmarksDatabaseHelper` is used in `onCreate()`, `updateBookmarksListView()`, and `onDestroy()`.
68     private BookmarksDatabaseHelper bookmarksDatabaseHelper;
69
70     // `bookmarksCursor` is used in `onCreate()`, `updateBookmarksListView()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`, and `onDestroy()`.
71     private Cursor bookmarksCursor;
72
73     // `bookmarksCursorAdapter` is used in `onCreate()` and `updateBookmarksListView()`.
74     private CursorAdapter bookmarksCursorAdapter;
75
76     // `oldFolderNameString` is used in `onCreate()` and `onSaveBookmarkFolder()`.
77     private String oldFolderNameString;
78
79     // `currentFolderDatabaseId` is used in `onCreate()`, `updateBookmarksListView()`, `onSaveBookmark()`, and `onSaveBookmarkFolder()`.
80     private int currentFolderDatabaseId;
81
82     // `currentFolder` is used in `onCreate()`, `onSaveBookmark()`, and `onSaveBookmarkFolder()`.
83     private String currentFolderName;
84
85     // `sortByDisplayOrder` is used in `onCreate()`, `onOptionsItemSelected()`, and `updateBookmarksListView()`.
86     private boolean sortByDisplayOrder;
87
88     @Override
89     public void onCreate(Bundle savedInstanceState) {
90         // Disable screenshots if not allowed.
91         if (!MainWebViewActivity.allowScreenshots) {
92             getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
93         }
94
95         // Set the activity theme.
96         if (MainWebViewActivity.darkTheme) {
97             setTheme(R.style.PrivacyBrowserDark_SecondaryActivity);
98         } else {
99             setTheme(R.style.PrivacyBrowserLight_SecondaryActivity);
100         }
101
102         // Run the default commands.
103         super.onCreate(savedInstanceState);
104
105         // Set the content view.
106         setContentView(R.layout.bookmarks_databaseview_coordinatorlayout);
107
108         // The `SupportActionBar` from `android.support.v7.app.ActionBar` must be used until the minimum API is >= 21.
109         Toolbar bookmarksDatabaseViewAppBar = findViewById(R.id.bookmarks_databaseview_toolbar);
110         setSupportActionBar(bookmarksDatabaseViewAppBar);
111
112         // Get a handle for the `AppBar`.
113         ActionBar appBar = getSupportActionBar();
114
115         // Remove the incorrect warning in Android Studio that `appBar` might be null.
116         assert appBar != null;
117
118         // Display the spinner and the back arrow in the app bar.
119         appBar.setCustomView(R.layout.spinner);
120         appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
121
122         // Initialize the database handler.  `this` specifies the context.  The two `null`s do not specify the database name or a `CursorFactory`.  The `0` is to specify a database version, but that is set instead using a constant in `BookmarksDatabaseHelper`.
123         bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0);
124
125         // Setup a matrix cursor for "All Folders" and "Home Folder".
126         String[] matrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME};
127         MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames);
128         matrixCursor.addRow(new Object[]{ALL_FOLDERS_DATABASE_ID, getString(R.string.all_folders)});
129         matrixCursor.addRow(new Object[]{HOME_FOLDER_DATABASE_ID, getString(R.string.home_folder)});
130
131         // Get a `Cursor` with the list of all the folders.
132         Cursor foldersCursor = bookmarksDatabaseHelper.getAllFoldersCursor();
133
134         // Combine `matrixCursor` and `foldersCursor`.
135         MergeCursor foldersMergeCursor = new MergeCursor(new Cursor[]{matrixCursor, foldersCursor});
136
137         // Create a resource cursor adapter for the spinner.
138         ResourceCursorAdapter foldersCursorAdapter = new ResourceCursorAdapter(this, R.layout.appbar_spinner_item, foldersMergeCursor, 0) {
139             @Override
140             public void bindView(View view, Context context, Cursor cursor) {
141                 // Get a handle for the spinner item text view.
142                 TextView spinnerItemTextView = view.findViewById(R.id.spinner_item_textview);
143
144                 // Set the text view to display the folder name.
145                 spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
146             }
147         };
148
149         // Set the resource cursor adapter drop drown view resource.
150         foldersCursorAdapter.setDropDownViewResource(R.layout.appbar_spinner_dropdown_item);
151
152         // Get a handle for the folder spinner and set the adapter.
153         Spinner folderSpinner = findViewById(R.id.spinner);
154         folderSpinner.setAdapter(foldersCursorAdapter);
155
156         // Handle clicks on the spinner dropdown.
157         folderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
158             @Override
159             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
160                 // Store the current folder database ID.
161                 currentFolderDatabaseId = (int) id;
162
163                 // Get a handle for the selected view.
164                 TextView selectedFolderTextView = findViewById(R.id.spinner_item_textview);
165
166                 // Store the current folder name.
167                 currentFolderName = selectedFolderTextView.getText().toString();
168
169                 // Update the list view.
170                 updateBookmarksListView();
171             }
172
173             @Override
174             public void onNothingSelected(AdapterView<?> parent) {
175                 // Do nothing.
176             }
177         });
178
179         // Get a handle for the bookmarks `ListView`.
180         ListView bookmarksListView = findViewById(R.id.bookmarks_databaseview_listview);
181
182         // Get a `Cursor` with the current contents of the bookmarks database.
183         bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
184
185         // Setup a `CursorAdapter` with `this` context.  `false` disables autoRequery.
186         bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
187             @Override
188             public View newView(Context context, Cursor cursor, ViewGroup parent) {
189                 // Inflate the individual item layout.  `false` does not attach it to the root.
190                 return getLayoutInflater().inflate(R.layout.bookmarks_databaseview_item_linearlayout, parent, false);
191             }
192
193             @Override
194             public void bindView(View view, Context context, Cursor cursor) {
195                 boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1);
196
197                 // Get the database ID from the `Cursor` and display it in `bookmarkDatabaseIdTextView`.
198                 int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper._ID));
199                 TextView bookmarkDatabaseIdTextView = view.findViewById(R.id.bookmarks_databaseview_database_id);
200                 bookmarkDatabaseIdTextView.setText(String.valueOf(bookmarkDatabaseId));
201
202                 // Get the favorite icon byte array from the `Cursor`.
203                 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON));
204                 // Convert the byte array to a `Bitmap` beginning at the beginning at the first byte and ending at the last.
205                 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
206                 // Display the bitmap in `bookmarkFavoriteIcon`.
207                 ImageView bookmarkFavoriteIcon = view.findViewById(R.id.bookmarks_databaseview_favorite_icon);
208                 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
209
210                 // Get the bookmark name from the `Cursor` and display it in `bookmarkNameTextView`.
211                 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
212                 TextView bookmarkNameTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_name);
213                 bookmarkNameTextView.setText(bookmarkNameString);
214
215                 // Make the font bold for folders.
216                 if (isFolder) {
217                     // The first argument is `null` prevent changing of the font.
218                     bookmarkNameTextView.setTypeface(null, Typeface.BOLD);
219                 } else {  // Reset the font to default.
220                     bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
221                 }
222
223                 // Get the bookmark URL form the `Cursor` and display it in `bookmarkUrlTextView`.
224                 String bookmarkUrlString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL));
225                 TextView bookmarkUrlTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_url);
226                 bookmarkUrlTextView.setText(bookmarkUrlString);
227
228                 // Hide the URL if the bookmark is a folder.
229                 if (isFolder) {
230                     bookmarkUrlTextView.setVisibility(View.GONE);
231                 } else {
232                     bookmarkUrlTextView.setVisibility(View.VISIBLE);
233                 }
234
235                 // Get the display order from the `Cursor` and display it in `bookmarkDisplayOrderTextView`.
236                 int bookmarkDisplayOrder = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER));
237                 TextView bookmarkDisplayOrderTextView = view.findViewById(R.id.bookmarks_databaseview_display_order);
238                 bookmarkDisplayOrderTextView.setText(String.valueOf(bookmarkDisplayOrder));
239
240                 // Get the parent folder from the `Cursor` and display it in `bookmarkParentFolder`.
241                 String bookmarkParentFolder = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER));
242                 ImageView parentFolderImageView = view.findViewById(R.id.bookmarks_databaseview_parent_folder_icon);
243                 TextView bookmarkParentFolderTextView = view.findViewById(R.id.bookmarks_databaseview_parent_folder);
244
245                 // Make the folder name gray if it is the home folder.
246                 if (bookmarkParentFolder.isEmpty()) {
247                     parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_gray));
248                     bookmarkParentFolderTextView.setText(R.string.home_folder);
249                     bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_500));
250                 } else {
251                     parentFolderImageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.folder_dark_blue));
252                     bookmarkParentFolderTextView.setText(bookmarkParentFolder);
253
254                     // Set the text color according to the theme.
255                     if (MainWebViewActivity.darkTheme) {
256                         bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_300));
257                     } else {
258                         bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black));
259                     }
260                 }
261             }
262         };
263
264         // Update the ListView.
265         bookmarksListView.setAdapter(bookmarksCursorAdapter);
266
267         // Set the current folder database ID.
268         currentFolderDatabaseId = ALL_FOLDERS_DATABASE_ID;
269
270         // Set a listener to edit a bookmark when it is tapped.
271         bookmarksListView.setOnItemClickListener((AdapterView<?> parent, View view, int position, long id) -> {
272             // Convert the database ID to an `int`.
273             int databaseId = (int) id;
274
275             // Show the edit bookmark or edit bookmark folder dialog.
276             if (bookmarksDatabaseHelper.isFolder(databaseId)) {
277                 // Save the current folder name, which is used in `onSaveBookmarkFolder()`.
278                 oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
279
280                 // Show the edit bookmark folder dialog.
281                 AppCompatDialogFragment editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog.folderDatabaseId(databaseId);
282                 editBookmarkFolderDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
283             } else {
284                 // Show the edit bookmark dialog.
285                 AppCompatDialogFragment editBookmarkDatabaseViewDialog = EditBookmarkDatabaseViewDialog.bookmarkDatabaseId(databaseId);
286                 editBookmarkDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
287             }
288         });
289     }
290
291     @Override
292     public boolean onCreateOptionsMenu(Menu menu) {
293         // Inflate the menu.
294         getMenuInflater().inflate(R.menu.bookmarks_databaseview_options_menu, menu);
295
296         // Success.
297         return true;
298     }
299
300     @Override
301     public boolean onOptionsItemSelected(MenuItem menuItem) {
302         // Get the ID of the menu item that was selected.
303         int menuItemId = menuItem.getItemId();
304
305         switch (menuItemId) {
306             case android.R.id.home:  // The home arrow is identified as `android.R.id.home`, not just `R.id.home`.
307                 // Return to `MainWebViewActivity`.
308                 NavUtils.navigateUpFromSameTask(this);
309                 break;
310
311             case R.id.options_menu_sort:
312                 // Update the sort by display order tracker.
313                 sortByDisplayOrder = !sortByDisplayOrder;
314
315                 // Get a handle for the bookmarks `ListView`.
316                 ListView bookmarksListView = findViewById(R.id.bookmarks_databaseview_listview);
317
318                 // Update the icon and display a snackbar.
319                 if (sortByDisplayOrder) {  // Sort by display order.
320                     // Update the icon according to the theme.
321                     if (MainWebViewActivity.darkTheme) {
322                         menuItem.setIcon(R.drawable.sort_selected_dark);
323                     } else {
324                         menuItem.setIcon(R.drawable.sort_selected_light);
325                     }
326
327                     // Display a Snackbar indicating the current sort type.
328                     Snackbar.make(bookmarksListView, R.string.sorted_by_display_order, Snackbar.LENGTH_SHORT).show();
329                 } else {  // Sort by database id.
330                     // Update the icon according to the theme.
331                     if (MainWebViewActivity.darkTheme) {
332                         menuItem.setIcon(R.drawable.sort_dark);
333                     } else {
334                         menuItem.setIcon(R.drawable.sort_light);
335                     }
336
337                     // Display a Snackbar indicating the current sort type.
338                     Snackbar.make(bookmarksListView, R.string.sorted_by_database_id, Snackbar.LENGTH_SHORT).show();
339                 }
340
341                 // Update the list view.
342                 updateBookmarksListView();
343                 break;
344         }
345         return true;
346     }
347
348     private void updateBookmarksListView() {
349         // Populate the bookmarks list view based on the spinner selection.
350         switch (currentFolderDatabaseId) {
351             // Get a cursor with all the folders.
352             case ALL_FOLDERS_DATABASE_ID:
353                 if (sortByDisplayOrder) {
354                     bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder();
355                 } else {
356                     bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor();
357                 }
358                 break;
359
360             // Get a cursor for the home folder.
361             case HOME_FOLDER_DATABASE_ID:
362                 if (sortByDisplayOrder) {
363                     bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder("");
364                 } else {
365                     bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor("");
366                 }
367                 break;
368
369             // Display the selected folder.
370             default:
371                 // Get a cursor for the selected folder.
372                 if (sortByDisplayOrder) {
373                     bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(currentFolderName);
374                 } else {
375                     bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursor(currentFolderName);
376                 }
377         }
378
379         // Update the list view.
380         bookmarksCursorAdapter.changeCursor(bookmarksCursor);
381     }
382
383     @Override
384     public void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
385         // Get handles for the views from dialog fragment.
386         RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton);
387         EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
388         EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
389         Spinner folderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_folder_spinner);
390         EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_display_order_edittext);
391
392         // Extract the bookmark information.
393         String bookmarkNameString = editBookmarkNameEditText.getText().toString();
394         String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
395         int folderDatabaseId = (int) folderSpinner.getSelectedItemId();
396         int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
397
398         // Instantiate the parent folder name `String`.
399         String parentFolderNameString;
400
401         // Set the parent folder name.
402         if (folderDatabaseId == EditBookmarkDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) {  // The home folder is selected.  Use `""`.
403             parentFolderNameString = "";
404         } else {  // Get the parent folder name from the database.
405             parentFolderNameString = bookmarksDatabaseHelper.getFolderName(folderDatabaseId);
406         }
407
408         // Update the bookmark.
409         if (currentBookmarkIconRadioButton.isChecked()) {  // Update the bookmark without changing the favorite icon.
410             bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt);
411         } else {  // Update the bookmark using the `WebView` favorite icon.
412             // Convert the favorite icon to a byte array.  `0` is for lossless compression (the only option for a PNG).
413             ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
414             MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
415             byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
416
417             //  Update the bookmark and the favorite icon.
418             bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt, newFavoriteIconByteArray);
419         }
420
421         // Update the list view.
422         updateBookmarksListView();
423     }
424
425     @Override
426     public void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
427         // Get handles for the views from dialog fragment.
428         RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
429         RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
430         ImageView defaultFolderIconImageView = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_imageview);
431         EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_name_edittext);
432         Spinner parentFolderSpinner = dialogFragment.getDialog().findViewById(R.id.edit_folder_parent_folder_spinner);
433         EditText displayOrderEditText = dialogFragment.getDialog().findViewById(R.id.edit_folder_display_order_edittext);
434
435         // Extract the folder information.
436         String newFolderNameString = editBookmarkNameEditText.getText().toString();
437         int parentFolderDatabaseId = (int) parentFolderSpinner.getSelectedItemId();
438         int displayOrderInt = Integer.valueOf(displayOrderEditText.getText().toString());
439
440         // Instantiate the parent folder name `String`.
441         String parentFolderNameString;
442
443         // Set the parent folder name.
444         if (parentFolderDatabaseId == EditBookmarkFolderDatabaseViewDialog.HOME_FOLDER_DATABASE_ID) {  // The home folder is selected.  Use `""`.
445             parentFolderNameString = "";
446         } else {  // Get the parent folder name from the database.
447             parentFolderNameString = bookmarksDatabaseHelper.getFolderName(parentFolderDatabaseId);
448         }
449
450         // Update the folder.
451         if (currentBookmarkIconRadioButton.isChecked()) {  // Update the folder without changing the favorite icon.
452             bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt);
453         } else {  // Update the folder and the icon.
454             // Instantiate the new folder icon `Bitmap`.
455             Bitmap folderIconBitmap;
456
457             // Populate the new folder icon bitmap.
458             if (defaultFolderIconRadioButton.isChecked()) {
459                 // Get the default folder icon and convert it to a `Bitmap`.
460                 Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
461                 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
462                 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
463             } else {  // Use the `WebView` favorite icon.
464                 folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
465             }
466
467             // Convert the folder icon to a byte array.  `0` is for lossless compression (the only option for a PNG).
468             ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
469             folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
470             byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
471
472             //  Update the folder and the icon.
473             bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt, newFolderIconByteArray);
474         }
475
476         // Update the list view.
477         updateBookmarksListView();
478     }
479
480     @Override
481     public void onDestroy() {
482         // Close the bookmarks cursor and database.
483         bookmarksCursor.close();
484         bookmarksDatabaseHelper.close();
485
486         // Run the default commands.
487         super.onDestroy();
488     }
489 }