X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FBookmarksActivity.java;h=905a98d004df241e1e8c129df5bf7ef296ce0d93;hb=HEAD;hp=68830965c84182d1abcada6c7061740500e79fab;hpb=871520acac2bd75f528e943c1b2a650017c852b9;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java deleted file mode 100644 index 68830965..00000000 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.java +++ /dev/null @@ -1,925 +0,0 @@ -/* - * Copyright © 2016-2017 Soren Stoutner . - * - * This file is part of Privacy Browser . - * - * Privacy Browser is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Privacy Browser is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Privacy Browser. If not, see . - */ - -package com.stoutner.privacybrowser.activities; - -import android.app.Activity; -import android.content.Context; -import android.content.Intent; -import android.database.Cursor; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.Typeface; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; -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.v7.app.ActionBar; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.app.AppCompatDialogFragment; -import android.support.v7.widget.Toolbar; -import android.util.SparseBooleanArray; -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.CursorAdapter; -import android.widget.EditText; -import android.widget.ImageView; -import android.widget.ListView; -import android.widget.RadioButton; -import android.widget.TextView; - -import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog; -import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolderDialog; -import com.stoutner.privacybrowser.dialogs.EditBookmarkDialog; -import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDialog; -import com.stoutner.privacybrowser.dialogs.MoveToFolderDialog; -import com.stoutner.privacybrowser.R; -import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper; - -import java.io.ByteArrayOutputStream; - -public class BookmarksActivity extends AppCompatActivity implements CreateBookmarkDialog.CreateBookmarkListener, CreateBookmarkFolderDialog.CreateBookmarkFolderListener, EditBookmarkDialog.EditBookmarkListener, EditBookmarkFolderDialog.EditBookmarkFolderListener, - MoveToFolderDialog.MoveToFolderListener { - - // `bookmarksDatabaseHelper` is public static so it can be accessed from `CreateBookmarkFolderDialog`, `EditBookmarkDialog`, `EditBookmarkFolderDialog` and `MoveToFolderDialog`. It is also used in `onCreate()`, - // `onCreateBookmarkCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`. - public static BookmarksDatabaseHelper bookmarksDatabaseHelper; - - // `currentFolder` is public static so it can be accessed from `MoveToFolderDialog`. - // It is used in `onCreate`, `onOptionsItemSelected()`, `onCreateBookmarkCreate`, `onCreateBookmarkFolderCreate`, and `onEditBookmarkSave`. - public static String currentFolder; - - // `checkedItemIds` is public static so it can be accessed from `EditBookmarkDialog`, `EditBookmarkFolderDialog`, and `MoveToFolderDialog`. - // It is also used in `onActionItemClicked`. - public static long[] checkedItemIds; - - - // `bookmarksListView` is used in `onCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`. - private ListView bookmarksListView; - - // `contextualActionMode` is used in `onCreate()` and `onEditBookmarkSave()`. - private ActionMode contextualActionMode; - - // `selectedBookmarkPosition` is used in `onCreate()` and `onEditBookmarkSave()`. - private int selectedBookmarkPosition; - - // `appBar` is used in `onCreate()` and `updateBookmarksListView()`. - private ActionBar appBar; - - // `bookmarksCursor` is used in `onCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`. - private Cursor bookmarksCursor; - - // `oldFolderName` is used in `onCreate()` and `onEditBookmarkFolderSave()`. - private String oldFolderNameString; - - @Override - protected void onCreate(Bundle savedInstanceState) { - // Set the activity theme. - if (MainWebViewActivity.darkTheme) { - setTheme(R.style.PrivacyBrowserDark_SecondaryActivity); - } else { - setTheme(R.style.PrivacyBrowserLight_SecondaryActivity); - } - - // Run the default commands. - super.onCreate(savedInstanceState); - - // Set the content view. - setContentView(R.layout.bookmarks_coordinatorlayout); - - // Use the `SupportActionBar` from `android.support.v7.app.ActionBar` until the minimum API is >= 21. - final Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.bookmarks_toolbar); - setSupportActionBar(bookmarksAppBar); - - // Display the home arrow on `SupportActionBar`. - appBar = getSupportActionBar(); - assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that `appBar` might be null. - appBar.setDisplayHomeAsUpEnabled(true); - - - // Initialize the database helper and the `ListView`. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`. - // The `0` specifies a database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`. - bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0); - bookmarksListView = (ListView) findViewById(R.id.bookmarks_listview); - - // Set currentFolder to the home folder, which is `""` in the database. - currentFolder = ""; - - // Display the bookmarks in the ListView. - updateBookmarksListView(currentFolder); - - // Set a listener so that tapping a list item loads the URL. We need to store the activity in a variable so that we can return to the parent activity after loading the URL. - final Activity bookmarksActivity = this; - bookmarksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - // Convert the id from long to int to match the format of the bookmarks database. - int databaseID = (int) id; - - // Get the bookmark `Cursor` for this ID and move it to the first row. - Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmarkCursor(databaseID); - bookmarkCursor.moveToFirst(); - - // If the bookmark is a folder load its contents into the ListView. - if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { - // Update `currentFolder`. - currentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); - - // Reload the ListView with `currentFolder`. - updateBookmarksListView(currentFolder); - } else { // Load the URL into `mainWebView`. - // Get the bookmark URL and assign it to formattedUrlString. `mainWebView` will automatically reload when `BookmarksActivity` closes. - MainWebViewActivity.formattedUrlString = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)); - - NavUtils.navigateUpFromSameTask(bookmarksActivity); - } - - // Close the `Cursor`. - bookmarkCursor.close(); - } - }); - - // `MultiChoiceModeListener` handles long clicks. - bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() { - // `moveBookmarkUpMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`. - MenuItem moveBookmarkUpMenuItem; - - // `moveBookmarkDownMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`. - MenuItem moveBookmarkDownMenuItem; - - // `editBookmarkMenuItem` is used in `onCreateActionMode()` and `onItemCheckedStateChanged`. - MenuItem editBookmarkMenuItem; - - // `selectAllBookmarks` is used in `onCreateActionMode()` and `onItemCheckedStateChanges`. - MenuItem selectAllBookmarksMenuItem; - - @Override - public boolean onCreateActionMode(ActionMode mode, Menu menu) { - // Inflate the menu for the contextual app bar and set the title. - getMenuInflater().inflate(R.menu.bookmarks_context_menu, menu); - - // Set the title. - if (currentFolder.isEmpty()) { - // Use `R.string.bookmarks` if we are in the home folder. - mode.setTitle(R.string.bookmarks); - } else { // Use the current folder name as the title. - mode.setTitle(currentFolder); - } - - // Get a handle for MenuItems we need to selectively disable. - moveBookmarkUpMenuItem = menu.findItem(R.id.move_bookmark_up); - moveBookmarkDownMenuItem = menu.findItem(R.id.move_bookmark_down); - editBookmarkMenuItem = menu.findItem(R.id.edit_bookmark); - selectAllBookmarksMenuItem = menu.findItem(R.id.context_menu_select_all_bookmarks); - - // Store `contextualActionMode` so we can close it programatically. - contextualActionMode = mode; - - return true; - } - - @Override - public boolean onPrepareActionMode(ActionMode mode, Menu menu) { - // Get a handle for the move to folder menu item. - MenuItem moveToFolderMenuItem = menu.findItem(R.id.move_to_folder); - - // Get a `Cursor` with all of the folders. - Cursor folderCursor = bookmarksDatabaseHelper.getAllFoldersCursor(); - - // Enable the move to folder menu item if at least one folder exists. - moveToFolderMenuItem.setVisible(folderCursor.getCount() > 0); - - // `return true` indicates that the menu has been updated. - return true; - } - - @Override - public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { - // Get an array of the selected bookmarks. - long[] selectedBookmarksLongArray = bookmarksListView.getCheckedItemIds(); - - // Calculate the number of selected bookmarks. - int numberOfSelectedBookmarks = selectedBookmarksLongArray.length; - - // Adjust the `mode` and the menu for the number of selected bookmarks. - if (numberOfSelectedBookmarks == 0) { - mode.finish(); - } else if (numberOfSelectedBookmarks == 1) { - // List the number of selected bookmarks in the subtitle. - mode.setSubtitle(getString(R.string.one_selected)); - - // Show the `Move Up`, `Move Down`, and `Edit` options. - moveBookmarkUpMenuItem.setVisible(true); - moveBookmarkDownMenuItem.setVisible(true); - editBookmarkMenuItem.setVisible(true); - - // Get the database IDs for the bookmarks. - int selectedBookmarkDatabaseId = (int) selectedBookmarksLongArray[0]; - int firstBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(0); - // bookmarksListView is 0 indexed. - int lastBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(bookmarksListView.getCount() - 1); - - // Disable `moveBookmarkUpMenuItem` if the selected bookmark is at the top of the ListView. - if (selectedBookmarkDatabaseId == firstBookmarkDatabaseId) { - moveBookmarkUpMenuItem.setEnabled(false); - moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_disabled); - } else { // Otherwise enable `moveBookmarkUpMenuItem`. - moveBookmarkUpMenuItem.setEnabled(true); - - // Set the icon according to the theme. - if (MainWebViewActivity.darkTheme) { - moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_dark); - } else { - moveBookmarkUpMenuItem.setIcon(R.drawable.move_up_enabled_light); - } - } - - // Disable `moveBookmarkDownMenuItem` if the selected bookmark is at the bottom of the ListView. - if (selectedBookmarkDatabaseId == lastBookmarkDatabaseId) { - moveBookmarkDownMenuItem.setEnabled(false); - moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_disabled); - } else { // Otherwise enable `moveBookmarkDownMenuItem`. - moveBookmarkDownMenuItem.setEnabled(true); - - // Set the icon according to the theme. - if (MainWebViewActivity.darkTheme) { - moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_dark); - } else { - moveBookmarkDownMenuItem.setIcon(R.drawable.move_down_enabled_light); - } - } - } else { // More than one bookmark is selected. - // List the number of selected bookmarks in the subtitle. - mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected)); - - // Hide non-applicable `MenuItems`. - moveBookmarkUpMenuItem.setVisible(false); - moveBookmarkDownMenuItem.setVisible(false); - editBookmarkMenuItem.setVisible(false); - } - - // Do not show `Select All` if all the bookmarks are already checked. - if (bookmarksListView.getCheckedItemIds().length == bookmarksListView.getCount()) { - selectAllBookmarksMenuItem.setVisible(false); - } else { - selectAllBookmarksMenuItem.setVisible(true); - } - } - - @Override - public boolean onActionItemClicked(ActionMode mode, MenuItem item) { - // Get the menu item ID. - int menuItemId = item.getItemId(); - - // Instantiate the common variables. - int numberOfBookmarks; - int selectedBookmarkNewPosition; - SparseBooleanArray bookmarkPositionSparseBooleanArray; - - switch (menuItemId) { - case R.id.move_bookmark_up: - // Get the array of checked bookmarks. - bookmarkPositionSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); - - // Store the position of the selected bookmark. - selectedBookmarkPosition = bookmarkPositionSparseBooleanArray.keyAt(0); - - // Initialize `selectedBookmarkNewPosition`. - selectedBookmarkNewPosition = 0; - - // Iterate through the bookmarks. - for (int i = 0; i < bookmarksListView.getCount(); i++) { - // Get the database ID for the current bookmark. - int currentBookmarkDatabaseId = (int) bookmarksListView.getItemIdAtPosition(i); - - // Update the display order for the current bookmark. - if (i == selectedBookmarkPosition) { // The current bookmark is the selected bookmark. - // Move the current bookmark up one. - bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i - 1); - selectedBookmarkNewPosition = i - 1; - } else if ((i + 1) == selectedBookmarkPosition){ // The current bookmark is immediately above the selected bookmark. - // Move the current bookmark down one. - bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i + 1); - } else { // The current bookmark is not changing positions. - // Move `bookmarksCursor` to the current bookmark position. - bookmarksCursor.moveToPosition(i); - - // Update the display order only if it is not correct in the database. - if (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)) != i) { - bookmarksDatabaseHelper.updateDisplayOrder(currentBookmarkDatabaseId, i); - } - } - } - - // Refresh the ListView. - updateBookmarksListView(currentFolder); - - // Select the previously selected bookmark in the new location. - bookmarksListView.setItemChecked(selectedBookmarkNewPosition, true); - - // Scroll `bookmarksListView` to five items above `selectedBookmarkNewPosition`. - bookmarksListView.setSelection(selectedBookmarkNewPosition - 5); - - break; - - case R.id.move_bookmark_down: - // Get the array of checked bookmarks. - bookmarkPositionSparseBooleanArray = bookmarksListView.getCheckedItemPositions(); - - // Store the position of the selected bookmark. - selectedBookmarkPosition = bookmarkPositionSparseBooleanArray.keyAt(0); - - // Initialize `selectedBookmarkNewPosition`. - selectedBookmarkNewPosition = 0; - - // Iterate through the bookmarks. - for (int i = 0; i