2 * Copyright 2016 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;
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;
52 import java.io.ByteArrayOutputStream;
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;
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;
63 // `contextualActionMode` is used in `onCreate()` and `onEditBookmarkSave()`.
64 private ActionMode contextualActionMode;
66 // `selectedBookmarkPosition` is used in `onCreate()` and `onEditBookarkSave()`.
67 private int selectedBookmarkPosition;
70 protected void onCreate(Bundle savedInstanceState) {
71 super.onCreate(savedInstanceState);
72 setContentView(R.layout.bookmarks_coordinatorlayout);
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);
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);
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);
89 // Display the bookmarks in the ListView.
90 updateBookmarksListView();
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() {
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;
101 // Get the bookmark URL and assign it to formattedUrlString.
102 MainWebViewActivity.formattedUrlString = bookmarksDatabaseHandler.getBookmarkURL(databaseID);
104 // Load formattedUrlString and return to the main activity.
105 MainWebViewActivity.mainWebView.loadUrl(MainWebViewActivity.formattedUrlString);
106 NavUtils.navigateUpFromSameTask(bookmarksActivity);
110 // `MultiChoiceModeListener` handles long clicks.
111 bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
112 // `moveBookmarkUpMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`.
113 MenuItem moveBookmarkUpMenuItem;
115 // `moveBookmarkDownMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`.
116 MenuItem moveBookmarkDownMenuItem;
118 // `editBookmarkMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`.
119 MenuItem editBookmarkMenuItem;
121 // `selectAllBookmarks` is used in `onCreateActionMode()` and `onItemCheckedStateChanges`.
122 MenuItem selectAllBookmarksMenuItem;
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);
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);
140 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
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();
149 // Calculate the number of selected bookmarks.
150 int numberOfSelectedBookmarks = selectedBookmarksLongArray.length;
152 // List the number of selected bookmarks in the subtitle.
153 mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected));
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);
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);
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);
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);
184 } else { // Hide the MenuItems because more than one bookmark is selected.
185 moveBookmarkUpMenuItem.setVisible(false);
186 moveBookmarkDownMenuItem.setVisible(false);
187 editBookmarkMenuItem.setVisible(false);
190 // Do not show `Select All` if all the bookmarks are already checked.
191 if (bookmarksListView.getCheckedItemIds().length == bookmarksListView.getCount()) {
192 selectAllBookmarksMenuItem.setVisible(false);
194 selectAllBookmarksMenuItem.setVisible(true);
199 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
200 int menuItemId = item.getItemId();
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;
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;
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];
218 // Initialize `selectedBookmarkNewPosition`.
219 selectedBookmarkNewPosition = 0;
221 for (int i = 0; i < bookmarksListView.getCount(); i++) {
222 int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
223 int nextBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i + 1);
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);
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);
240 // Refresh the ListView.
241 updateBookmarksListView();
243 // Select the previously selected bookmark in the new location.
244 bookmarksListView.setItemChecked(selectedBookmarkNewPosition, true);
246 bookmarksListView.setSelection(selectedBookmarkNewPosition - 5);
250 case R.id.move_bookmark_down:
251 // Get the selected bookmark database ID.
252 selectedBookmarkLongArray = bookmarksListView.getCheckedItemIds();
253 selectedBookmarkDatabaseId = (int) selectedBookmarkLongArray[0];
255 // Initialize `selectedBookmarkNewPosition`.
256 selectedBookmarkNewPosition = 0;
258 for (int i = 0; i <bookmarksListView.getCount(); i++) {
259 int databaseId = (int) bookmarksListView.getItemIdAtPosition(i);
260 int previousBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i - 1);
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);
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);
277 // Refresh the ListView.
278 updateBookmarksListView();
280 // Select the previously selected bookmark in the new location.
281 bookmarksListView.setItemChecked(selectedBookmarkNewPosition, true);
283 bookmarksListView.setSelection(selectedBookmarkNewPosition - 5);
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);
291 // Get a handle for `contextualActionMode` so we can close it when `editBookmarkDialog` is finished.
292 contextualActionMode = mode;
294 // Show the `EditBookmark` `AlertDialog` and name the instance `@string/edit_bookmark`.
295 DialogFragment editBookmarkDialog = new EditBookmark();
296 editBookmarkDialog.show(getFragmentManager(), "@string/edit_bookmark");
299 case R.id.delete_bookmark:
300 // Get an array of the selected rows.
301 final long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
303 String snackbarMessage;
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);
309 snackbarMessage = selectedBookmarksLongArray.length + " " + getString(R.string.bookmarks_deleted);
312 updateBookmarksListViewExcept(selectedBookmarksLongArray);
315 Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), snackbarMessage, Snackbar.LENGTH_LONG)
316 .setAction(R.string.undo, new View.OnClickListener() {
318 public void onClick(View view) {
319 // Do nothing because everything will be handled by `onDismissed()` below.
322 .setCallback(new Snackbar.Callback() {
324 public void onDismissed(Snackbar snackbar, int 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();
333 // The Snackbar was dismissed without the "Undo" button being pushed.
335 // Delete each selected row.
336 for (long databaseIdLong : selectedBookmarksLongArray) {
337 // Convert `databaseIdLong` to an int.
338 int databaseIdInt = (int) databaseIdLong;
340 // Delete the database row.
341 bookmarksDatabaseHandler.deleteBookmark(databaseIdInt);
349 // Close the contextual app bar.
353 case R.id.context_menu_select_all_bookmarks:
354 numberOfBookmarks = bookmarksListView.getCount();
356 for (int i = 0; i < numberOfBookmarks; i++) {
357 bookmarksListView.setItemChecked(i, true);
361 // Consume the click.
366 public void onDestroyActionMode(ActionMode mode) {
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() {
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");
385 public boolean onCreateOptionsMenu(Menu menu) {
387 getMenuInflater().inflate(R.menu.bookmarks_options_menu, menu);
393 public boolean onPrepareOptionsMenu(Menu menu) {
394 super.onPrepareOptionsMenu(menu);
400 public boolean onOptionsItemSelected(MenuItem menuItem) {
401 int menuItemId = menuItem.getItemId();
403 switch (menuItemId) {
404 case android.R.id.home:
405 NavUtils.navigateUpFromSameTask(this);
408 case R.id.options_menu_select_all_bookmarks:
409 int numberOfBookmarks = bookmarksListView.getCount();
411 for (int i = 0; i < numberOfBookmarks; i++) {
412 bookmarksListView.setItemChecked(i, true);
420 public void onCreateBookmarkCancel(DialogFragment createBookmarkDialogFragment) {
421 // Do nothing because the user selected `Cancel`.
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();
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();
437 // Display the new bookmark below the current items in the (0 indexed) list.
438 int newBookmarkDisplayOrder = bookmarksListView.getCount();
440 // Create the bookmark.
441 bookmarksDatabaseHandler.createBookmark(bookmarkNameString, bookmarkUrlString, newBookmarkDisplayOrder, favoriteIconByteArray);
443 // Refresh the ListView. `setSelection` scrolls to the bottom of the list.
444 updateBookmarksListView();
445 bookmarksListView.setSelection(bookmarksListView.getCount());
449 public void onEditBookmarkCancel(DialogFragment editBookmarkDialogFragment) {
450 // Do nothing because the user selected `Cancel`.
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();
461 CheckBox useNewFavoriteIconBitmap = (CheckBox) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_use_new_favorite_icon_checkbox);
462 byte[] favoriteIconByteArray;
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];
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();
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();
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);
485 // Close the contextual action mode.
486 contextualActionMode.finish();
488 // Refresh the `ListView`. `setSelection` scrolls to that position.
489 updateBookmarksListView();
490 bookmarksListView.setSelection(selectedBookmarkPosition);
493 private void updateBookmarksListView() {
494 // Get a Cursor with the current contents of the bookmarks database.
495 final Cursor bookmarksCursor = bookmarksDatabaseHandler.getAllBookmarksCursor();
497 // Setup bookmarksCursorAdapter with `this` context. The `false` disables autoRequery.
498 CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
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);
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));
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);
513 // Display the bitmap in `bookmarkFavoriteIcon`.
514 ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon);
515 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
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);
526 // Update the ListView.
527 bookmarksListView.setAdapter(bookmarksCursorAdapter);
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);
534 // Setup bookmarksCursorAdapter with `this` context. The `false` disables autoRequery.
535 CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
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);
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));
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);
550 // Display the bitmap in `bookmarkFavoriteIcon`.
551 ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon);
552 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
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);
563 // Update the ListView.
564 bookmarksListView.setAdapter(bookmarksCursorAdapter);