From: Soren Stoutner Date: Wed, 6 Jul 2016 02:48:28 +0000 (-0700) Subject: Add the ability to delete bookmarks. X-Git-Tag: v1.8~6 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=c6907b02eb70e1a4bde274e66101f9f54f77a660;p=PrivacyBrowserAndroid.git Add the ability to delete bookmarks. --- diff --git a/app/src/main/assets/about_license.html b/app/src/main/assets/about_license.html index 6e4dc0a8..b495595d 100644 --- a/app/src/main/assets/about_license.html +++ b/app/src/main/assets/about_license.html @@ -77,6 +77,7 @@

ic_add.

+

ic_download.


GNU General Public License

diff --git a/app/src/main/assets/guide_clear_and_exit.html b/app/src/main/assets/guide_clear_and_exit.html index 9d5c1f34..c00ac623 100644 --- a/app/src/main/assets/guide_clear_and_exit.html +++ b/app/src/main/assets/guide_clear_and_exit.html @@ -41,6 +41,7 @@
  • Removes all form data.
  • Clears the cache, including disk files.
  • Clears the back/forward history.
  • +
  • Deletes the current URL.
  • Destroys the internal state of the WebView.
  • Closes Privacy Browser. For Android Lollipop and newer (version >= 5.0 or API >= 21), Privacy Browser is also removed from the recent app list.
  • diff --git a/app/src/main/assets/images/ic_add.png b/app/src/main/assets/images/ic_add.png new file mode 100644 index 00000000..5c486bb4 Binary files /dev/null and b/app/src/main/assets/images/ic_add.png differ diff --git a/app/src/main/assets/images/ic_delete.png b/app/src/main/assets/images/ic_delete.png new file mode 100644 index 00000000..46c58c3d Binary files /dev/null and b/app/src/main/assets/images/ic_delete.png differ diff --git a/app/src/main/java/com/stoutner/privacybrowser/BookmarksActivity.java b/app/src/main/java/com/stoutner/privacybrowser/BookmarksActivity.java index 6d081991..556bd60c 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/BookmarksActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/BookmarksActivity.java @@ -21,21 +21,29 @@ package com.stoutner.privacybrowser; import android.app.Activity; import android.app.DialogFragment; +import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; +import android.support.design.widget.Snackbar; import android.support.v4.app.NavUtils; +import android.support.v4.widget.CursorAdapter; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; +import android.view.ActionMode; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; +import android.view.ViewGroup; +import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; -import android.widget.SimpleCursorAdapter; +import android.widget.TextView; import java.io.ByteArrayOutputStream; @@ -43,13 +51,16 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma private BookmarksDatabaseHandler bookmarksDatabaseHandler; private ListView bookmarksListView; + // deleteBookmarkMenuItem is used in onCreate() and onPrepareOptionsMenu(). + private MenuItem deleteBookmarkMenuItem; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bookmarks_coordinatorlayout); // We need to use the SupportActionBar from android.support.v7.app.ActionBar until the minimum API is >= 21. - Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.bookmarks_toolbar); + final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.bookmarks_toolbar); setSupportActionBar(bookmarksAppBar); // Display the home arrow on supportAppBar. @@ -81,6 +92,101 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma } }); + // registerForContextMenu(bookmarksListView); + + bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { + @Override + public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { + String numberSelectedString; + long[] selectedItemsLongArray = bookmarksListView.getCheckedItemIds(); + + numberSelectedString = selectedItemsLongArray.length + " " + getString(R.string.selected); + + mode.setSubtitle(numberSelectedString); + } + + @Override + public boolean onCreateActionMode(ActionMode mode, Menu menu) { + // Inflate the menu for the contextual app bar. + getMenuInflater().inflate(R.menu.bookmarks_context_menu, menu); + + mode.setTitle(R.string.bookmarks); + + return true; + } + + @Override + public boolean onPrepareActionMode(ActionMode mode, Menu menu) { + return false; + } + + @Override + public boolean onActionItemClicked(ActionMode mode, MenuItem item) { + int menuItemId = item.getItemId(); + + switch (menuItemId) { + case R.id.delete_bookmark: + // Get an array of the selected rows. + final long[] selectedItemsLongArray = bookmarksListView.getCheckedItemIds(); + + String snackbarMessage; + + // Determine how many items are in the array and prepare an appropriate Snackbar message. + if (selectedItemsLongArray.length == 1) { + snackbarMessage = getString(R.string.one_bookmark_deleted); + } else { + snackbarMessage = selectedItemsLongArray.length + " " + getString(R.string.bookmarks_deleted); + } + + updateBookmarksListViewExcept(selectedItemsLongArray); + + // Show a SnackBar. + Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), snackbarMessage, Snackbar.LENGTH_LONG) + .setAction(R.string.undo, new View.OnClickListener() { + @Override + public void onClick(View view) { + // Do nothing because everything will be handled by `onDismissed()` below. + } + }) + .setCallback(new Snackbar.Callback() { + @Override + public void onDismissed(Snackbar snackbar, int event) { + switch (event) { + // The user pushed the "Undo" button. + case Snackbar.Callback.DISMISS_EVENT_ACTION: + // Refresh the ListView to show the rows again. + updateBookmarksListView(); + + break; + + // The Snackbar was dismissed without the "Undo" button being pushed. + default: + // Delete each selected row. + for (long databaseIdLong : selectedItemsLongArray) { + // Convert `databaseIdLong` to an int. + int databaseIdInt = (int) databaseIdLong; + + // Delete the database row. + bookmarksDatabaseHandler.deleteBookmark(databaseIdInt); + } + break; + } + } + }) + .show(); + + // Close the contextual app bar. + mode.finish(); + } + return true; + } + + @Override + public void onDestroyActionMode(ActionMode mode) { + + } + }); + // Set a FloatingActionButton for creating new bookmarks. FloatingActionButton createBookmarkFAB = (FloatingActionButton) findViewById(R.id.create_bookmark_fab); assert createBookmarkFAB != null; @@ -94,6 +200,33 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma }); } + /* + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + super.onCreateContextMenu(menu, v, menuInfo); + getMenuInflater().inflate(R.menu.bookmarks_context_menu, menu); + } */ + + /*@Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu_bookmarks_options, menu); + + return true; + } + + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + // Disable the delete icon. + deleteBookmarkMenuItem = menu.findItem(R.id.delete_bookmark); + deleteBookmarkMenuItem.setVisible(false); + + // Run all the other default commands. + super.onPrepareOptionsMenu(menu); + + // `return true` displays the menu; + return true; + }*/ + @Override public void onCreateBookmarkCancel(DialogFragment createBookmarkDialogFragment) { // Do nothing because the user selected "Cancel". @@ -123,35 +256,73 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma // Get a Cursor with the current contents of the bookmarks database. final Cursor bookmarksCursor = bookmarksDatabaseHandler.getBookmarksCursor(); - // The last argument is 0 because no special behavior is required. - SimpleCursorAdapter bookmarksAdapter = new SimpleCursorAdapter(this, - R.layout.bookmarks_item_linearlayout, - bookmarksCursor, - new String[] { BookmarksDatabaseHandler.FAVORITEICON, BookmarksDatabaseHandler.BOOKMARK_NAME }, - new int[] { R.id.bookmark_favorite_icon, R.id.bookmark_name }, - 0); - - // Override the handling of R.id.bookmark_favorite_icon to load an image instead of a string. - bookmarksAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { - public boolean setViewValue(View view, Cursor cursor, int columnIndex) { - if (view.getId() == R.id.bookmark_favorite_icon) { - // Get the byte array from the database. - byte[] favoriteIconByteArray = cursor.getBlob(columnIndex); - - // Convert the byte array to a Bitmap beginning at the first byte and ending at the last. - Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); - - // Set the favoriteIconBitmap. - ImageView bookmarkFavoriteIcon = (ImageView) view; - bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap); - return true; - } else { // Process the rest of the bookmarksAdapter with default settings. - return false; - } + // Setup bookmarksCursorAdapter with `this` context. The `false` disables autoRequery. + CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) { + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + // Inflate the individual item layout. `false` does not attach it to the root. + return getLayoutInflater().inflate(R.layout.bookmarks_item_linearlayout, parent, false); } - }); + + @Override + public void bindView(View view, Context context, Cursor cursor) { + // Get the favorite icon byte array from the cursor. + byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITEICON)); + + // Convert the byte array to a Bitmap beginning at the first byte and ending at the last. + Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); + + // Display the bitmap in `bookmarkFavoriteIcon`. + ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon); + bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap); + + + // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`. + String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)); + TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name); + assert bookmarkNameTextView != null; // This assert removes the warning that bookmarkNameTextView might be null. + bookmarkNameTextView.setText(bookmarkNameString); + } + }; + + // Update the ListView. + bookmarksListView.setAdapter(bookmarksCursorAdapter); + } + + private void updateBookmarksListViewExcept(long[] exceptIdLongArray) { + // Get a Cursor with the current contents of the bookmarks database except for the specified database IDs. + final Cursor bookmarksCursor = bookmarksDatabaseHandler.getBookmarksCursorExcept(exceptIdLongArray); + + // Setup bookmarksCursorAdapter with `this` context. The `false` disables autoRequery. + CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) { + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + // Inflate the individual item layout. `false` does not attach it to the root. + return getLayoutInflater().inflate(R.layout.bookmarks_item_linearlayout, parent, false); + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + // Get the favorite icon byte array from the cursor. + byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHandler.FAVORITEICON)); + + // Convert the byte array to a Bitmap beginning at the first byte and ending at the last. + Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); + + // Display the bitmap in `bookmarkFavoriteIcon`. + ImageView bookmarkFavoriteIcon = (ImageView) view.findViewById(R.id.bookmark_favorite_icon); + bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap); + + + // Get the bookmark name from the cursor and display it in `bookmarkNameTextView`. + String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME)); + TextView bookmarkNameTextView = (TextView) view.findViewById(R.id.bookmark_name); + assert bookmarkNameTextView != null; // This assert removes the warning that bookmarkNameTextView might be null. + bookmarkNameTextView.setText(bookmarkNameString); + } + }; // Update the ListView. - bookmarksListView.setAdapter(bookmarksAdapter); + bookmarksListView.setAdapter(bookmarksCursorAdapter); } } \ No newline at end of file diff --git a/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java b/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java index 45344d3b..cacc8530 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java +++ b/app/src/main/java/com/stoutner/privacybrowser/BookmarksDatabaseHandler.java @@ -30,7 +30,7 @@ public class BookmarksDatabaseHandler extends SQLiteOpenHelper { private static final String BOOKMARKS_DATABASE = "bookmarks.db"; private static final String BOOKMARKS_TABLE = "bookmarks"; - public static final String ID = "_id"; + public static final String _ID = "_id"; public static final String DISPLAYORDER = "displayorder"; public static final String BOOKMARK_NAME = "bookmarkname"; public static final String BOOKMARK_URL = "bookmarkurl"; @@ -46,7 +46,7 @@ public class BookmarksDatabaseHandler extends SQLiteOpenHelper { public void onCreate(SQLiteDatabase bookmarksDatabase) { // Create the database if it doesn't exist. String CREATE_BOOKMARKS_TABLE = "CREATE TABLE " + BOOKMARKS_TABLE + " (" + - ID + " integer primary key, " + + _ID + " integer primary key, " + DISPLAYORDER + " integer, " + BOOKMARK_NAME + " text, " + BOOKMARK_URL + " text, " + @@ -87,20 +87,45 @@ public class BookmarksDatabaseHandler extends SQLiteOpenHelper { SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); // Get everything in the BOOKMARKS_TABLE. - String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE; + final String GET_ALL_BOOKMARKS = "Select * FROM " + BOOKMARKS_TABLE; - // Return the results as a Cursor. The second argument is "null" because there are no selectionArgs. + // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs. // We can't close the Cursor because we need to use it in the parent activity. return bookmarksDatabase.rawQuery(GET_ALL_BOOKMARKS, null); } - public String getBookmarkURL(int databaseID) { + public Cursor getBookmarksCursorExcept(long[] exceptIdLongArray) { // Get a readable database handle. SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); - // Get the row for the selected databaseID. - String GET_BOOKMARK_URL = "Select * FROM " + BOOKMARKS_TABLE + - " WHERE " + ID + " = " + databaseID; + // Prepare a string that contains the comma-separated list of IDs not to get. + String doNotGetIdsString = ""; + // Extract the array to `doNotGetIdsString`. + for (long databaseIdLong : exceptIdLongArray) { + // If this is the first number, only add the number. + if (doNotGetIdsString.isEmpty()) { + doNotGetIdsString = String.valueOf(databaseIdLong); + } else { // If there already is a number in the string, place a `,` before the number. + doNotGetIdsString = doNotGetIdsString + "," + databaseIdLong; + } + } + + // Prepare the SQL statement to select all items except those with the specified IDs. + final String GET_All_BOOKMARKS_EXCEPT_SPECIFIED = "Select * FROM " + BOOKMARKS_TABLE + + " WHERE " + _ID + " NOT IN (" + doNotGetIdsString + ")"; + + // Return the results as a Cursor. The second argument is `null` because there are no selectionArgs. + // We can't close the Cursor because we need to use it in the parent activity. + return bookmarksDatabase.rawQuery(GET_All_BOOKMARKS_EXCEPT_SPECIFIED, null); + } + + public String getBookmarkURL(int databaseId) { + // Get a readable database handle. + SQLiteDatabase bookmarksDatabase = this.getReadableDatabase(); + + // Prepare the SQL statement to get the row for the selected databaseId. + final String GET_BOOKMARK_URL = "Select * FROM " + BOOKMARKS_TABLE + + " WHERE " + _ID + " = " + databaseId; // Save the results as Cursor and move it to the first (only) row. The second argument is "null" because there are no selectionArgs. Cursor bookmarksCursor = bookmarksDatabase.rawQuery(GET_BOOKMARK_URL, null); @@ -117,4 +142,15 @@ public class BookmarksDatabaseHandler extends SQLiteOpenHelper { // Return the bookmarkURLString. return bookmarkURLString; } -} + + public void deleteBookmark(int databaseId) { + // Get a writable database handle. + SQLiteDatabase bookmarksDatabase = this.getWritableDatabase(); + + // Deletes the row with the given databaseId. The last argument is null because we don't need additional parameters. + bookmarksDatabase.delete(BOOKMARKS_TABLE, _ID + " = " + databaseId, null); + + // Close the database handle. + bookmarksDatabase.close(); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java index c83b8d6b..5f549b25 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/MainWebViewActivity.java @@ -445,7 +445,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_options, menu); + getMenuInflater().inflate(R.menu.webview_options_menu, menu); // Set mainMenu so it can be used by onOptionsItemSelected. mainMenu = menu; @@ -497,7 +497,7 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Run all the other default commands. super.onPrepareOptionsMenu(menu); - // return true displays the menu. + // `return true` displays the menu. return true; } @@ -723,6 +723,8 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation // Clear the back/forward history. mainWebView.clearHistory(); + formattedUrlString = null; + // Destroy the internal state of the webview. mainWebView.destroy(); diff --git a/app/src/main/res/drawable/bookmarks_list_selector.xml b/app/src/main/res/drawable/bookmarks_list_selector.xml new file mode 100644 index 00000000..834b9ec6 --- /dev/null +++ b/app/src/main/res/drawable/bookmarks_list_selector.xml @@ -0,0 +1,26 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/delete.xml b/app/src/main/res/drawable/delete.xml new file mode 100644 index 00000000..d314874d --- /dev/null +++ b/app/src/main/res/drawable/delete.xml @@ -0,0 +1,14 @@ + + + + + + diff --git a/app/src/main/res/layout/bookmarks_coordinatorlayout.xml b/app/src/main/res/layout/bookmarks_coordinatorlayout.xml index 152e3b3a..15bde099 100644 --- a/app/src/main/res/layout/bookmarks_coordinatorlayout.xml +++ b/app/src/main/res/layout/bookmarks_coordinatorlayout.xml @@ -51,10 +51,16 @@ app:popupTheme="@style/PrivacyBrowser.PopupOverlay" /> + + android:layout_width="match_parent" + android:choiceMode="multipleChoiceModal" + android:divider="@color/white" + android:dividerHeight="0dp" /> + app:menu="@menu/webview_navigation_menu"/> + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_bookmarks_options.xml b/app/src/main/res/menu/menu_bookmarks_options.xml new file mode 100644 index 00000000..97d6487b --- /dev/null +++ b/app/src/main/res/menu/menu_bookmarks_options.xml @@ -0,0 +1,31 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_navigation.xml b/app/src/main/res/menu/menu_navigation.xml deleted file mode 100644 index 756013c3..00000000 --- a/app/src/main/res/menu/menu_navigation.xml +++ /dev/null @@ -1,87 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/menu/menu_options.xml b/app/src/main/res/menu/menu_options.xml deleted file mode 100644 index d380a060..00000000 --- a/app/src/main/res/menu/menu_options.xml +++ /dev/null @@ -1,96 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/menu/webview_navigation_menu.xml b/app/src/main/res/menu/webview_navigation_menu.xml new file mode 100644 index 00000000..756013c3 --- /dev/null +++ b/app/src/main/res/menu/webview_navigation_menu.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/webview_options_menu.xml b/app/src/main/res/menu/webview_options_menu.xml new file mode 100644 index 00000000..d380a060 --- /dev/null +++ b/app/src/main/res/menu/webview_options_menu.xml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values-v19/styles.xml b/app/src/main/res/values-v19/styles.xml deleted file mode 100644 index ea2d867a..00000000 --- a/app/src/main/res/values-v19/styles.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 77428aee..6c87a126 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -36,7 +36,7 @@ Favorite Icon - + Navigation Drawer Navigation Home @@ -49,7 +49,7 @@ About Clear and Exit - + JavaScript First-Party Cookies Third-Party Cookies @@ -73,6 +73,13 @@ Bookmark name Bookmark URL + + Selected + Delete + 1 Bookmark Deleted + Bookmarks Deleted + Undo + Privacy Browser Guide Overview diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 36b66785..0b1697e7 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -19,23 +19,35 @@ along with Privacy Browser. If not, see . --> - + - + - + - \ No newline at end of file