75a340211711d4f9fcb8f70d765fefacd3ac552d
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / BookmarksActivity.java
1 /**
2  * Copyright 2016 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;
21
22 import android.app.Activity;
23 import android.app.DialogFragment;
24 import android.content.Context;
25 import android.database.Cursor;
26 import android.graphics.Bitmap;
27 import android.graphics.BitmapFactory;
28 import android.graphics.drawable.BitmapDrawable;
29 import android.graphics.drawable.Drawable;
30 import android.os.Bundle;
31 import android.support.design.widget.FloatingActionButton;
32 import android.support.design.widget.Snackbar;
33 import android.support.v4.app.NavUtils;
34 import android.support.v4.widget.CursorAdapter;
35 import android.support.v7.app.ActionBar;
36 import android.support.v7.app.AppCompatActivity;
37 import android.support.v7.widget.Toolbar;
38 import android.util.SparseBooleanArray;
39 import android.view.ActionMode;
40 import android.view.Menu;
41 import android.view.MenuItem;
42 import android.view.View;
43 import android.view.ViewGroup;
44 import android.widget.AbsListView;
45 import android.widget.AdapterView;
46 import android.widget.CheckBox;
47 import android.widget.EditText;
48 import android.widget.ImageView;
49 import android.widget.ListView;
50 import android.widget.TextView;
51
52 import java.io.ByteArrayOutputStream;
53
54 public class BookmarksActivity extends AppCompatActivity implements CreateBookmark.CreateBookmarkListener, EditBookmark.EditBookmarkListener {
55     // `bookmarksDatabaseHandler` is public static so it can be accessed from EditBookmark.  It is also used in `onCreate()`,
56     // `onCreateBookmarkCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`.
57     public static BookmarksDatabaseHandler bookmarksDatabaseHandler;
58
59     // `bookmarksListView` is public static so it can be accessed from EditBookmark.
60     // It is also used in `onCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`.
61     public static ListView bookmarksListView;
62
63     // `contextualActionMode` is used in `onCreate()` and `onEditBookmarkSave()`.
64     private ActionMode contextualActionMode;
65
66     // `selectedBookmarkPosition` is used in `onCreate()` and `onEditBookarkSave()`.
67     private int selectedBookmarkPosition;
68
69     @Override
70     protected void onCreate(Bundle savedInstanceState) {
71         super.onCreate(savedInstanceState);
72         setContentView(R.layout.bookmarks_coordinatorlayout);
73
74         // We need to use the SupportActionBar from android.support.v7.app.ActionBar until the minimum API is >= 21.
75         final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.bookmarks_toolbar);
76         setSupportActionBar(bookmarksAppBar);
77
78         // Display the home arrow on supportAppBar.
79         final ActionBar appBar = getSupportActionBar();
80         assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that appBar might be null.
81         appBar.setDisplayHomeAsUpEnabled(true);
82
83         // Initialize the database handler and the ListView.
84         // `this` specifies the context.  The two `null`s do not specify the database name or a `CursorFactory`.
85         // The `0` is to specify a database version, but that is set instead using a constant in BookmarksDatabaseHandler.
86         bookmarksDatabaseHandler = new BookmarksDatabaseHandler(this, null, null, 0);
87         bookmarksListView = (ListView) findViewById(R.id.bookmarks_listview);
88
89         // Display the bookmarks in the ListView.
90         updateBookmarksListView();
91
92         // Set a listener so that tapping a list item loads the URL.  We need to store the activity
93         // in a variable so that we can return to the parent activity after loading the URL.
94         final Activity bookmarksActivity = this;
95         bookmarksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
96             @Override
97             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
98                 // Convert the id from long to int to match the format of the bookmarks database.
99                 int databaseID = (int) id;
100
101                 // Get the bookmark URL and assign it to formattedUrlString.
102                 MainWebViewActivity.formattedUrlString = bookmarksDatabaseHandler.getBookmarkURL(databaseID);
103
104                 //  Load formattedUrlString and return to the main activity.
105                 MainWebViewActivity.mainWebView.loadUrl(MainWebViewActivity.formattedUrlString);
106                 NavUtils.navigateUpFromSameTask(bookmarksActivity);
107             }
108         });
109
110         // `MultiChoiceModeListener` handles long clicks.
111         bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
112             // `moveBookmarkUpMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`.
113             MenuItem moveBookmarkUpMenuItem;
114
115             // `moveBookmarkDownMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`.
116             MenuItem moveBookmarkDownMenuItem;
117
118             // `editBookmarkMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`.
119             MenuItem editBookmarkMenuItem;
120
121             // `selectAllBookmarks` is used in `onCreateActionMode()` and `onItemCheckedStateChanges`.
122             MenuItem selectAllBookmarksMenuItem;
123
124             @Override
125             public boolean onCreateActionMode(ActionMode mode, Menu menu) {
126                 // Inflate the menu for the contextual app bar and set the title.
127                 getMenuInflater().inflate(R.menu.bookmarks_context_menu, menu);
128                 mode.setTitle(R.string.bookmarks);
129
130                 // Get a handle for MenuItems we need to selectively disable.
131                 moveBookmarkUpMenuItem = menu.findItem(R.id.move_bookmark_up);
132                 moveBookmarkDownMenuItem = menu.findItem(R.id.move_bookmark_down);
133                 editBookmarkMenuItem = menu.findItem(R.id.edit_bookmark);
134                 selectAllBookmarksMenuItem = menu.findItem(R.id.context_menu_select_all_bookmarks);
135
136                 return true;
137             }
138
139             @Override
140             public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
141                 return false;
142             }
143
144             @Override
145             public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
146                 // Get an array of the selected bookmarks.
147                 long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
148
149                 // Calculate the number of selected bookmarks.
150                 int numberOfSelectedBookmarks = selectedBookmarksLongArray.length;
151
152                 // List the number of selected bookmarks in the subtitle.
153                 mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected));
154
155                 if (numberOfSelectedBookmarks == 1) {
156                     // Show the `Move Up`, `Move Down`, and  `Edit` option only if 1 bookmark is selected.
157                     moveBookmarkUpMenuItem.setVisible(true);
158                     moveBookmarkDownMenuItem.setVisible(true);
159                     editBookmarkMenuItem.setVisible(true);
160
161                     // Get the database IDs for the bookmarks.
162                     int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0];
163                     int firstBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(0);
164                     // bookmarksListView is 0 indexed.
165                     int lastBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(bookmarksListView.getCount() - 1);
166
167                     // Disable `moveBookmarkUpMenuItem` if the selected bookmark is at the top of the ListView.
168                     if (selectedBookmarkDatabaseId == firstBookmarkDatabaseId) {
169                         moveBookmarkUpMenuItem.setEnabled(false);
170                         moveBookmarkUpMenuItem.setIcon(R.drawable.move_bookmark_up_disabled);
171                     } else {  // Otherwise enable `moveBookmarkUpMenuItem`.
172                         moveBookmarkUpMenuItem.setEnabled(true);
173                         moveBookmarkUpMenuItem.setIcon(R.drawable.move_bookmark_up_enabled);
174                     }
175
176                     // Disable `moveBookmarkDownMenuItem` if the selected bookmark is at the bottom of the ListView.
177                     if (selectedBookmarkDatabaseId == lastBookmarkDatabaseId) {
178                         moveBookmarkDownMenuItem.setEnabled(false);
179                         moveBookmarkDownMenuItem.setIcon(R.drawable.move_bookmark_down_disabled);
180                     } else {  // Otherwise enable `moveBookmarkDownMenuItem`.
181                         moveBookmarkDownMenuItem.setEnabled(true);
182                         moveBookmarkDownMenuItem.setIcon(R.drawable.move_bookmark_down_enabled);
183                     }
184                 } else {  // Hide the MenuItems because more than one bookmark is selected.
185                     moveBookmarkUpMenuItem.setVisible(false);
186                     moveBookmarkDownMenuItem.setVisible(false);
187                     editBookmarkMenuItem.setVisible(false);
188                 }
189
190                 // Do not show `Select All` if all the bookmarks are already checked.
191                 if (bookmarksListView.getCheckedItemIds().length == bookmarksListView.getCount()) {
192                     selectAllBookmarksMenuItem.setVisible(false);
193                 } else {
194                     selectAllBookmarksMenuItem.setVisible(true);
195                 }
196             }
197
198             @Override
199             public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
200                 int menuItemId = item.getItemId();
201
202                 // `numberOfBookmarks` is used in `R.id.move_bookmark_up_enabled`, `R.id.move_bookmark_down_enabled`, and `R.id.context_menu_select_all_bookmarks`.
203                 int numberOfBookmarks;
204
205                 // `selectedBookmarkLongArray` is used in `R.id.move_bookmark_up` and `R.id.move_bookmark_down`.
206                 long[]selectedBookmarkLongArray;
207                 // `selectedBookmarkDatabaseId` is used in `R.id.move_bookmark_up` and `R.id.move_bookmark_down`.
208                 int selectedBookmarkDatabaseId;
209                 // `selectedBookmarkNewPosition` is used in `R.id.move_bookmark_up` and `R.id.move_bookmark_down`.
210                 int selectedBookmarkNewPosition;
211
212                 switch (menuItemId) {
213                     case R.id.move_bookmark_up:
214                         // Get the selected bookmark database ID.
215                         selectedBookmarkLongArray = bookmarksListView.getCheckedItemIds();
216                         selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0];
217
218                         // Initialize `selectedBookmarkNewPosition`.
219                         selectedBookmarkNewPosition = 0;
220
221                         for (int i = 0; i < bookmarksListView.getCount(); i++) {
222                             int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
223                             int nextBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i + 1);
224
225                             if (databaseId == selectedBookmarkDatabaseId || nextBookmarkDatabaseId == selectedBookmarkDatabaseId) {
226                                 if (databaseId == selectedBookmarkDatabaseId) {
227                                     // Move the selected bookmark up one and store the new bookmark position.
228                                     bookmarksDatabaseHandler.updateBookmarkDisplayOrder(databaseId, i - 1);
229                                     selectedBookmarkNewPosition = i - 1;
230                                 } else {  // Move the bookmark above the selected bookmark down one.
231                                     bookmarksDatabaseHandler.updateBookmarkDisplayOrder(databaseId, i + 1);
232                                 }
233                             } else {
234                                 // Reset the rest of the bookmarks' DISPLAY_ORDER to match the position in the ListView.
235                                 // This isn't necessary, but it clears out any stray values that might have crept into the database.
236                                 bookmarksDatabaseHandler.updateBookmarkDisplayOrder(databaseId, i);
237                             }
238                         }
239
240                         // Refresh the ListView.
241                         updateBookmarksListView();
242
243                         // Select the previously selected bookmark in the new location.
244                         bookmarksListView.setItemChecked(selectedBookmarkNewPosition, true);
245
246                         bookmarksListView.setSelection(selectedBookmarkNewPosition - 5);
247
248                         break;
249
250                     case R.id.move_bookmark_down:
251                         // Get the selected bookmark database ID.
252                         selectedBookmarkLongArray = bookmarksListView.getCheckedItemIds();
253                         selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0];
254
255                         // Initialize `selectedBookmarkNewPosition`.
256                         selectedBookmarkNewPosition = 0;
257
258                         for (int i = 0; i <bookmarksListView.getCount(); i++) {
259                             int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
260                             int previousBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i - 1);
261
262                             if (databaseId == selectedBookmarkDatabaseId || previousBookmarkDatabaseId == selectedBookmarkDatabaseId) {
263                                 if (databaseId == selectedBookmarkDatabaseId) {
264                                     // Move the selected bookmark down one and store the new bookmark position.
265                                     bookmarksDatabaseHandler.updateBookmarkDisplayOrder(databaseId, i + 1);
266                                     selectedBookmarkNewPosition = i + 1;
267                                 } else {  // Move the bookmark below the selected bookmark up one.
268                                     bookmarksDatabaseHandler.updateBookmarkDisplayOrder(databaseId, i - 1);
269                                 }
270                             } else {
271                                 // Reset the rest of the bookmark' DISPLAY_ORDER to match the position in the ListView.
272                                 // This isn't necessary, but it clears out any stray values that might have crept into the database.
273                                 bookmarksDatabaseHandler.updateBookmarkDisplayOrder(databaseId, i);
274                             }
275                         }
276
277                         // Refresh the ListView.
278                         updateBookmarksListView();
279
280                         // Select the previously selected bookmark in the new location.
281                         bookmarksListView.setItemChecked(selectedBookmarkNewPosition, true);
282
283                         bookmarksListView.setSelection(selectedBookmarkNewPosition - 5);
284                         break;
285
286                     case R.id.edit_bookmark:
287                         // Get a handle for `selectedBookmarkPosition` so we can scroll to it after refreshing the ListView.
288                         SparseBooleanArray bookmarkPositionSparseBooleanArray = bookmarksListView.getCheckedItemPositions();
289                         selectedBookmarkPosition = bookmarkPositionSparseBooleanArray.keyAt(0);
290
291                         // Get a handle for `contextualActionMode` so we can close it when `editBookmarkDialog` is finished.
292                         contextualActionMode = mode;
293
294                         // Show the `EditBookmark` `AlertDialog` and name the instance `@string/edit_bookmark`.
295                         DialogFragment editBookmarkDialog = new EditBookmark();
296                         editBookmarkDialog.show(getFragmentManager(), "@string/edit_bookmark");
297                         break;
298
299                     case R.id.delete_bookmark:
300                         // Get an array of the selected rows.
301                         final long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
302
303                         String snackbarMessage;
304
305                         // Determine how many items are in the array and prepare an appropriate Snackbar message.
306                         if (selectedBookmarksLongArray.length == 1) {
307                             snackbarMessage = getString(R.string.one_bookmark_deleted);
308                         } else {
309                             snackbarMessage = selectedBookmarksLongArray.length + " " + getString(R.string.bookmarks_deleted);
310                         }
311
312                         updateBookmarksListViewExcept(selectedBookmarksLongArray);
313
314                         // Show a SnackBar.
315                         Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), snackbarMessage, Snackbar.LENGTH_LONG)
316                                 .setAction(R.string.undo, new View.OnClickListener() {
317                                     @Override
318                                     public void onClick(View view) {
319                                         // Do nothing because everything will be handled by `onDismissed()` below.
320                                     }
321                                 })
322                                 .setCallback(new Snackbar.Callback() {
323                                     @Override
324                                     public void onDismissed(Snackbar snackbar, int event) {
325                                         switch (event) {
326                                             // The user pushed the "Undo" button.
327                                             case Snackbar.Callback.DISMISS_EVENT_ACTION:
328                                                 // Refresh the ListView to show the rows again.
329                                                 updateBookmarksListView();
330
331                                                 break;
332
333                                             // The Snackbar was dismissed without the "Undo" button being pushed.
334                                             default:
335                                                 // Delete each selected row.
336                                                 for (long databaseIdLong : selectedBookmarksLongArray) {
337                                                     // Convert `databaseIdLong` to an int.
338                                                     int databaseIdInt = (int) databaseIdLong;
339
340                                                     // Delete the database row.
341                                                     bookmarksDatabaseHandler.deleteBookmark(databaseIdInt);
342                                                 }
343                                                 break;
344                                         }
345                                     }
346                                 })
347                                 .show();
348
349                         // Close the contextual app bar.
350                         mode.finish();
351                         break;
352
353                     case R.id.context_menu_select_all_bookmarks:
354                         numberOfBookmarks = bookmarksListView.getCount();
355
356                         for (int i = 0; i < numberOfBookmarks; i++) {
357                             bookmarksListView.setItemChecked(i, true);
358                         }
359                         break;
360                 }
361                 // Consume the click.
362                 return true;
363             }
364
365             @Override
366             public void onDestroyActionMode(ActionMode mode) {
367
368             }
369         });
370
371         // Set a FloatingActionButton for creating new bookmarks.
372         FloatingActionButton createBookmarkFAB = (FloatingActionButton) findViewById(R.id.create_bookmark_fab);
373         assert createBookmarkFAB != null;
374         createBookmarkFAB.setOnClickListener(new View.OnClickListener() {
375             @Override
376             public void onClick(View view) {
377                 // Show the `CreateBookmark` `AlertDialog` and name the instance `@string/create_bookmark`.
378                 DialogFragment createBookmarkDialog = new CreateBookmark();
379                 createBookmarkDialog.show(getFragmentManager(), "@string/create_bookmark");
380             }
381         });
382     }
383
384     @Override
385     public boolean onCreateOptionsMenu(Menu menu) {
386         //Inflate the menu.
387         getMenuInflater().inflate(R.menu.bookmarks_options_menu, menu);
388
389         return true;
390     }
391
392     @Override
393     public boolean onPrepareOptionsMenu(Menu menu) {
394         super.onPrepareOptionsMenu(menu);
395
396         return true;
397     }
398
399     @Override
400     public boolean onOptionsItemSelected(MenuItem menuItem) {
401         int menuItemId = menuItem.getItemId();
402
403         switch (menuItemId) {
404             case android.R.id.home:
405                 NavUtils.navigateUpFromSameTask(this);
406                 break;
407
408             case R.id.options_menu_select_all_bookmarks:
409                 int numberOfBookmarks = bookmarksListView.getCount();
410
411                 for (int i = 0; i < numberOfBookmarks; i++) {
412                     bookmarksListView.setItemChecked(i, true);
413                 }
414                 break;
415         }
416         return true;
417     }
418
419     @Override
420     public void onCreateBookmarkCancel(DialogFragment createBookmarkDialogFragment) {
421         // Do nothing because the user selected `Cancel`.
422     }
423
424     @Override
425     public void onCreateBookmarkCreate(DialogFragment createBookmarkDialogFragment) {
426         // Get the `EditText`s from the `createBookmarkDialogFragment` and extract the strings.
427         EditText createBookmarkNameEditText = (EditText) createBookmarkDialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext);
428         String bookmarkNameString = createBookmarkNameEditText.getText().toString();
429         EditText createBookmarkUrlEditText = (EditText) createBookmarkDialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext);
430         String bookmarkUrlString = createBookmarkUrlEditText.getText().toString();
431
432         // Convert the favoriteIcon Bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
433         ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
434         MainWebViewActivity.favoriteIcon.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
435         byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
436
437         // Display the new bookmark below the current items in the (0 indexed) list.
438         int newBookmarkDisplayOrder = bookmarksListView.getCount();
439
440         // Create the bookmark.
441         bookmarksDatabaseHandler.createBookmark(bookmarkNameString, bookmarkUrlString, newBookmarkDisplayOrder, favoriteIconByteArray);
442
443         // Refresh the ListView.  `setSelection` scrolls to the bottom of the list.
444         updateBookmarksListView();
445         bookmarksListView.setSelection(bookmarksListView.getCount());
446     }
447
448     @Override
449     public void onEditBookmarkCancel(DialogFragment editBookmarkDialogFragment) {
450         // Do nothing because the user selected `Cancel`.
451     }
452
453     @Override
454     public void onEditBookmarkSave(DialogFragment editBookmarkDialogFragment) {
455         // Get the `EditText`s from the `editBookmarkDialogFragment` and extract the strings.
456         EditText editBookmarkNameEditText = (EditText) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
457         String bookmarkNameString = editBookmarkNameEditText.getText().toString();
458         EditText editBookmarkUrlEditText = (EditText) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
459         String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
460
461         CheckBox useNewFavoriteIconBitmap = (CheckBox) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_use_new_favorite_icon_checkbox);
462         byte[] favoriteIconByteArray;
463
464         // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`.
465         long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
466         int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0];
467
468         if (useNewFavoriteIconBitmap.isChecked()) {
469             // Get the new favorite icon from the Dialog and convert it into a Bitmap.
470             ImageView newFavoriteIconImageView = (ImageView) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_new_favorite_icon);
471             Drawable favoriteIconDrawable = newFavoriteIconImageView.getDrawable();
472             Bitmap favoriteIconBitmap = ((BitmapDrawable) favoriteIconDrawable).getBitmap();
473
474             // Convert the new `favoriteIconBitmap` into a Byte Array.
475             ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
476             favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
477             favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
478
479             //  Update the bookmark and the favorite icon.
480             bookmarksDatabaseHandler.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, favoriteIconByteArray);
481         } else {  // Update the bookmark without changing the favorite icon.
482             bookmarksDatabaseHandler.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString);
483         }
484
485         // Close the contextual action mode.
486         contextualActionMode.finish();
487
488         // Refresh the `ListView`.  `setSelection` scrolls to that position.
489         updateBookmarksListView();
490         bookmarksListView.setSelection(selectedBookmarkPosition);
491     }
492
493     private void updateBookmarksListView() {
494         // Get a Cursor with the current contents of the bookmarks database.
495         final Cursor bookmarksCursor = bookmarksDatabaseHandler.getAllBookmarksCursor();
496
497         // Setup bookmarksCursorAdapter with `this` context.  The `false` disables autoRequery.
498         CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
499             @Override
500             public View newView(Context context, Cursor cursor, ViewGroup parent) {
501                 // Inflate the individual item layout.  `false` does not attach it to the root.
502                 return getLayoutInflater().inflate(R.layout.bookmarks_item_linearlayout, parent, false);
503             }
504
505             @Override
506             public void bindView(View view, Context context, Cursor cursor) {
507                 // Get the favorite icon byte array from the `Cursor`.
508                 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
509
510                 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
511                 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
512
513                 // Display the bitmap in `bookmarkFavoriteIcon`.
514                 ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon);
515                 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
516
517
518                 // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`.
519                 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
520                 TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name);
521                 assert bookmarkNameTextView != null;  // This assert removes the warning that bookmarkNameTextView might be null.
522                 bookmarkNameTextView.setText(bookmarkNameString);
523             }
524         };
525
526         // Update the ListView.
527         bookmarksListView.setAdapter(bookmarksCursorAdapter);
528     }
529
530     private void updateBookmarksListViewExcept(long[] exceptIdLongArray) {
531         // Get a Cursor with the current contents of the bookmarks database except for the specified database IDs.
532         final Cursor bookmarksCursor = bookmarksDatabaseHandler.getBookmarksCursorExcept(exceptIdLongArray);
533
534         // Setup bookmarksCursorAdapter with `this` context.  The `false` disables autoRequery.
535         CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
536             @Override
537             public View newView(Context context, Cursor cursor, ViewGroup parent) {
538                 // Inflate the individual item layout.  `false` does not attach it to the root.
539                 return getLayoutInflater().inflate(R.layout.bookmarks_item_linearlayout, parent, false);
540             }
541
542             @Override
543             public void bindView(View view, Context context, Cursor cursor) {
544                 // Get the favorite icon byte array from the cursor.
545                 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
546
547                 // Convert the byte array to a Bitmap beginning at the first byte and ending at the last.
548                 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
549
550                 // Display the bitmap in `bookmarkFavoriteIcon`.
551                 ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon);
552                 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
553
554
555                 // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`.
556                 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
557                 TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name);
558                 assert bookmarkNameTextView != null;  // This assert removes the warning that bookmarkNameTextView might be null.
559                 bookmarkNameTextView.setText(bookmarkNameString);
560             }
561         };
562
563         // Update the ListView.
564         bookmarksListView.setAdapter(bookmarksCursorAdapter);
565     }
566 }