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.view.ActionMode;
39 import android.view.Menu;
40 import android.view.MenuItem;
41 import android.view.View;
42 import android.view.ViewGroup;
43 import android.widget.AbsListView;
44 import android.widget.AdapterView;
45 import android.widget.CheckBox;
46 import android.widget.EditText;
47 import android.widget.ImageView;
48 import android.widget.ListView;
49 import android.widget.TextView;
51 import java.io.ByteArrayOutputStream;
53 public class BookmarksActivity extends AppCompatActivity implements CreateBookmark.CreateBookmarkListener, EditBookmark.EditBookmarkListener {
54 // `bookmarksDatabaseHandler` is public static so it can be accessed from EditBookmark. It is also used in `onCreate()`,
55 // `onCreateBookmarkCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`.
56 public static BookmarksDatabaseHandler bookmarksDatabaseHandler;
58 // `bookmarksListView` is public static so it can be accessed from EditBookmark.
59 // It is also used in `onCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`.
60 public static ListView bookmarksListView;
62 // `contextualActionMode` is used in `onCreate()` and `onEditBookmarkSave()`.
63 private ActionMode contextualActionMode;
66 protected void onCreate(Bundle savedInstanceState) {
67 super.onCreate(savedInstanceState);
68 setContentView(R.layout.bookmarks_coordinatorlayout);
70 // We need to use the SupportActionBar from android.support.v7.app.ActionBar until the minimum API is >= 21.
71 final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.bookmarks_toolbar);
72 setSupportActionBar(bookmarksAppBar);
74 // Display the home arrow on supportAppBar.
75 final ActionBar appBar = getSupportActionBar();
76 assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that appBar might be null.
77 appBar.setDisplayHomeAsUpEnabled(true);
79 // Initialize the database handler and the ListView.
80 // `this` specifies the context. The two `null`s do not specify the database name or a `CursorFactory`.
81 // The `0` is to specify a database version, but that is set instead using a constant in BookmarksDatabaseHandler.
82 bookmarksDatabaseHandler = new BookmarksDatabaseHandler(this, null, null, 0);
83 bookmarksListView = (ListView) findViewById(R.id.bookmarks_listview);
85 // Display the bookmarks in the ListView.
86 updateBookmarksListView();
88 // Set a listener so that tapping a list item loads the URL. We need to store the activity
89 // in a variable so that we can return to the parent activity after loading the URL.
90 final Activity bookmarksActivity = this;
91 bookmarksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
93 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
94 // Convert the id from long to int to match the format of the bookmarks database.
95 int databaseID = (int) id;
97 // Get the bookmark URL and assign it to formattedUrlString.
98 MainWebViewActivity.formattedUrlString = bookmarksDatabaseHandler.getBookmarkURL(databaseID);
100 // Load formattedUrlString and return to the main activity.
101 MainWebViewActivity.mainWebView.loadUrl(MainWebViewActivity.formattedUrlString);
102 NavUtils.navigateUpFromSameTask(bookmarksActivity);
106 // `MultiChoiceModeListener` handles long clicks.
107 bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
108 // `editBookmarkMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`.
109 MenuItem editBookmarkMenuItem;
112 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
113 // Inflate the menu for the contextual app bar and set the title.
114 getMenuInflater().inflate(R.menu.bookmarks_context_menu, menu);
115 mode.setTitle(R.string.bookmarks);
117 // Get a handle for `R.id.edit_bookmark`.
118 editBookmarkMenuItem = menu.findItem(R.id.edit_bookmark);
124 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
129 public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
130 // Get an array of the selected bookmarks.
131 long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
133 // Calculate the number of selected bookmarks.
134 int numberOfSelectedBookmarks = selectedBookmarksLongArray.length;
136 // List the number of selected bookmarks in the subtitle.
137 mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected));
139 // Show the `Edit` option only if 1 bookmark is selected.
140 if (numberOfSelectedBookmarks == 1) {
141 editBookmarkMenuItem.setVisible(true);
143 editBookmarkMenuItem.setVisible(false);
148 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
149 int menuItemId = item.getItemId();
151 switch (menuItemId) {
152 case R.id.edit_bookmark:
153 // Show the `EditBookmark` `AlertDialog` and name the instance `@string/edit_bookmark`.
154 DialogFragment editBookmarkDialog = new EditBookmark();
155 editBookmarkDialog.show(getFragmentManager(), "@string/edit_bookmark");
157 contextualActionMode = mode;
160 case R.id.delete_bookmark:
161 // Get an array of the selected rows.
162 final long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
164 String snackbarMessage;
166 // Determine how many items are in the array and prepare an appropriate Snackbar message.
167 if (selectedBookmarksLongArray.length == 1) {
168 snackbarMessage = getString(R.string.one_bookmark_deleted);
170 snackbarMessage = selectedBookmarksLongArray.length + " " + getString(R.string.bookmarks_deleted);
173 updateBookmarksListViewExcept(selectedBookmarksLongArray);
176 Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), snackbarMessage, Snackbar.LENGTH_LONG)
177 .setAction(R.string.undo, new View.OnClickListener() {
179 public void onClick(View view) {
180 // Do nothing because everything will be handled by `onDismissed()` below.
183 .setCallback(new Snackbar.Callback() {
185 public void onDismissed(Snackbar snackbar, int event) {
187 // The user pushed the "Undo" button.
188 case Snackbar.Callback.DISMISS_EVENT_ACTION:
189 // Refresh the ListView to show the rows again.
190 updateBookmarksListView();
194 // The Snackbar was dismissed without the "Undo" button being pushed.
196 // Delete each selected row.
197 for (long databaseIdLong : selectedBookmarksLongArray) {
198 // Convert `databaseIdLong` to an int.
199 int databaseIdInt = (int) databaseIdLong;
201 // Delete the database row.
202 bookmarksDatabaseHandler.deleteBookmark(databaseIdInt);
210 // Close the contextual app bar.
214 // Consume the click.
219 public void onDestroyActionMode(ActionMode mode) {
224 // Set a FloatingActionButton for creating new bookmarks.
225 FloatingActionButton createBookmarkFAB = (FloatingActionButton) findViewById(R.id.create_bookmark_fab);
226 assert createBookmarkFAB != null;
227 createBookmarkFAB.setOnClickListener(new View.OnClickListener() {
229 public void onClick(View view) {
230 // Show the `CreateBookmark` `AlertDialog` and name the instance `@string/create_bookmark`.
231 DialogFragment createBookmarkDialog = new CreateBookmark();
232 createBookmarkDialog.show(getFragmentManager(), "@string/create_bookmark");
238 public void onCreateBookmarkCancel(DialogFragment createBookmarkDialogFragment) {
239 // Do nothing because the user selected `Cancel`.
243 public void onCreateBookmarkCreate(DialogFragment createBookmarkDialogFragment) {
244 // Get the `EditText`s from the `createBookmarkDialogFragment` and extract the strings.
245 EditText createBookmarkNameEditText = (EditText) createBookmarkDialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext);
246 String bookmarkNameString = createBookmarkNameEditText.getText().toString();
247 EditText createBookmarkUrlEditText = (EditText) createBookmarkDialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext);
248 String bookmarkUrlString = createBookmarkUrlEditText.getText().toString();
250 // Convert the favoriteIcon Bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
251 ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
252 MainWebViewActivity.favoriteIcon.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
253 byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
255 // Create the bookmark.
256 bookmarksDatabaseHandler.createBookmark(bookmarkNameString, bookmarkUrlString, favoriteIconByteArray);
258 // Refresh the ListView.
259 updateBookmarksListView();
263 public void onEditBookmarkCancel(DialogFragment editBookmarkDialogFragment) {
264 // Do nothing because the user selected `Cancel`.
268 public void onEditBookmarkSave(DialogFragment editBookmarkDialogFragment) {
269 // Get the `EditText`s from the `editBookmarkDialogFragment` and extract the strings.
270 EditText editBookmarkNameEditText = (EditText) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
271 String bookmarkNameString = editBookmarkNameEditText.getText().toString();
272 EditText editBookmarkUrlEditText = (EditText) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
273 String bookmarkUrlString = editBookmarkUrlEditText.getText().toString();
275 CheckBox useNewFavoriteIconBitmap = (CheckBox) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_use_new_favorite_icon_checkbox);
276 byte[] favoriteIconByteArray;
278 // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`.
279 long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds();
280 int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0];
282 if (useNewFavoriteIconBitmap.isChecked()) {
283 ImageView newFavoriteIconImageView = (ImageView) editBookmarkDialogFragment.getDialog().findViewById(R.id.edit_bookmark_new_favorite_icon);
284 Drawable favoriteIconDrawable = newFavoriteIconImageView.getDrawable();
285 Bitmap favoriteIconBitmap = ((BitmapDrawable) favoriteIconDrawable).getBitmap();
286 ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
287 favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
288 favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
289 bookmarksDatabaseHandler.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, favoriteIconByteArray);
292 // Update the bookmark.
293 bookmarksDatabaseHandler.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString);
296 contextualActionMode.finish();
298 // Refresh the `ListView`.
299 updateBookmarksListView();
302 private void updateBookmarksListView() {
303 // Get a Cursor with the current contents of the bookmarks database.
304 final Cursor bookmarksCursor = bookmarksDatabaseHandler.getAllBookmarksCursor();
306 // Setup bookmarksCursorAdapter with `this` context. The `false` disables autoRequery.
307 CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
309 public View newView(Context context, Cursor cursor, ViewGroup parent) {
310 // Inflate the individual item layout. `false` does not attach it to the root.
311 return getLayoutInflater().inflate(R.layout.bookmarks_item_linearlayout, parent, false);
315 public void bindView(View view, Context context, Cursor cursor) {
316 // Get the favorite icon byte array from the `Cursor`.
317 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
319 // Convert the byte array to a `Bitmap` beginning at the first byte and ending at the last.
320 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
322 // Display the bitmap in `bookmarkFavoriteIcon`.
323 ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon);
324 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
327 // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`.
328 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
329 TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name);
330 assert bookmarkNameTextView != null; // This assert removes the warning that bookmarkNameTextView might be null.
331 bookmarkNameTextView.setText(bookmarkNameString);
335 // Update the ListView.
336 bookmarksListView.setAdapter(bookmarksCursorAdapter);
339 private void updateBookmarksListViewExcept(long[] exceptIdLongArray) {
340 // Get a Cursor with the current contents of the bookmarks database except for the specified database IDs.
341 final Cursor bookmarksCursor = bookmarksDatabaseHandler.getBookmarksCursorExcept(exceptIdLongArray);
343 // Setup bookmarksCursorAdapter with `this` context. The `false` disables autoRequery.
344 CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
346 public View newView(Context context, Cursor cursor, ViewGroup parent) {
347 // Inflate the individual item layout. `false` does not attach it to the root.
348 return getLayoutInflater().inflate(R.layout.bookmarks_item_linearlayout, parent, false);
352 public void bindView(View view, Context context, Cursor cursor) {
353 // Get the favorite icon byte array from the cursor.
354 byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITE_ICON));
356 // Convert the byte array to a Bitmap beginning at the first byte and ending at the last.
357 Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
359 // Display the bitmap in `bookmarkFavoriteIcon`.
360 ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon);
361 bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
364 // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`.
365 String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
366 TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name);
367 assert bookmarkNameTextView != null; // This assert removes the warning that bookmarkNameTextView might be null.
368 bookmarkNameTextView.setText(bookmarkNameString);
372 // Update the ListView.
373 bookmarksListView.setAdapter(bookmarksCursorAdapter);