]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/BookmarksActivity.java
Remove lint errors in non-java files. Fix download dialog when download size is...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / BookmarksActivity.java
index 9b9bfb71dc11e385c3316ef606079be1d36f2890..65d1cfddb7e9110b515bc4786fc4e9d7b90c40d7 100644 (file)
@@ -33,7 +33,6 @@ 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.content.ContextCompat;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
@@ -57,22 +56,27 @@ import java.io.ByteArrayOutputStream;
 public class BookmarksActivity extends AppCompatActivity implements CreateBookmark.CreateBookmarkListener,
         CreateBookmarkFolder.CreateBookmarkFolderListener, EditBookmark.EditBookmarkListener,
         EditBookmarkFolder.EditBookmarkFolderListener, MoveToFolder.MoveToFolderListener {
+
     // `bookmarksDatabaseHandler` is public static so it can be accessed from `EditBookmark` and `MoveToFolder`.  It is also used in `onCreate()`,
     // `onCreateBookmarkCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`.
     public static BookmarksDatabaseHandler bookmarksDatabaseHandler;
 
-    // `bookmarksListView` is public static so it can be accessed from `EditBookmark`.
-    // It is also used in `onCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`.
-    public static ListView bookmarksListView;
-
     // `currentFolder` is public static so it can be accessed from `MoveToFolder`.
     // It is used in `onCreate`, `onOptionsItemSelected()`, `onCreateBookmarkCreate`, `onCreateBookmarkFolderCreate`, and `onEditBookmarkSave`.
     public static String currentFolder;
 
+    // `checkedItemIds` is public static so it can be accessed from `EditBookmark`, `EditBookmarkFolder`, and `MoveToFolder`.
+    // 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 `onEditBookarkSave()`.
+    // `selectedBookmarkPosition` is used in `onCreate()` and `onEditBookmarkSave()`.
     private int selectedBookmarkPosition;
 
     // `appBar` is used in `onCreate()` and `updateBookmarksListView()`.
@@ -132,11 +136,9 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                     // Reload the ListView with `currentFolder`.
                     updateBookmarksListView(currentFolder);
                 } else {  // Load the URL into `mainWebView`.
-                    // Get the bookmark URL and assign it to formattedUrlString.
+                    // Get the bookmark URL and assign it to formattedUrlString.  `mainWebView` will automatically reload when `BookmarksActivity` closes.
                     MainWebViewActivity.formattedUrlString = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_URL));
 
-                    //  Load formattedUrlString and return to the main activity.
-                    MainWebViewActivity.mainWebView.loadUrl(MainWebViewActivity.formattedUrlString);
                     NavUtils.navigateUpFromSameTask(bookmarksActivity);
                 }
 
@@ -339,6 +341,9 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                         break;
 
                     case R.id.move_to_folder:
+                        // Store `checkedItemIds` for use by the `AlertDialog`.
+                        checkedItemIds = bookmarksListView.getCheckedItemIds();
+
                         // Show the `MoveToFolder` `AlertDialog` and name the instance `@string/move_to_folder
                         DialogFragment moveToFolderDialog = new MoveToFolder();
                         moveToFolderDialog.show(getFragmentManager(), getResources().getString(R.string.move_to_folder));
@@ -357,6 +362,9 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                         bookmarksCursor.moveToPosition(selectedBookmarkPosition);
                         boolean isFolder = (bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHandler.IS_FOLDER)) == 1);
 
+                        // Store `checkedItemIds` for use by the `AlertDialog`.
+                        checkedItemIds = bookmarksListView.getCheckedItemIds();
+
                         if (isFolder) {
                             // Save the current folder name.
                             oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHandler.BOOKMARK_NAME));
@@ -408,6 +416,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                                 .setCallback(new Snackbar.Callback() {
                                     @Override
                                     public void onDismissed(Snackbar snackbar, int event) {
+                                        // Android Studio wants to see entries for every possible `Snackbar.Callback` even if they aren't used.
                                         switch (event) {
                                             // The user pushed the "Undo" button.
                                             case Snackbar.Callback.DISMISS_EVENT_ACTION:
@@ -419,6 +428,18 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
 
                                                 break;
 
+                                            case Snackbar.Callback.DISMISS_EVENT_CONSECUTIVE:
+                                                // Do nothing and let the default behavior run.
+
+                                            case Snackbar.Callback.DISMISS_EVENT_MANUAL:
+                                                // Do nothing and let the default behavior run.
+
+                                            case Snackbar.Callback.DISMISS_EVENT_SWIPE:
+                                                // Do nothing and let the default behavior run.
+
+                                            case Snackbar.Callback.DISMISS_EVENT_TIMEOUT:
+                                                // Do nothing and let the default behavior run.
+
                                             // The Snackbar was dismissed without the "Undo" button being pushed.
                                             default:
                                                 // Delete each selected row.
@@ -463,7 +484,6 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
 
         // Set a FloatingActionButton for creating new bookmarks.
         FloatingActionButton createBookmarkFAB = (FloatingActionButton) findViewById(R.id.create_bookmark_fab);
-        assert createBookmarkFAB != null;
         createBookmarkFAB.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
@@ -529,11 +549,6 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         return true;
     }
 
-    @Override
-    public void onCancelCreateBookmark(DialogFragment dialogFragment) {
-        // Do nothing because the user selected `Cancel`.
-    }
-
     @Override
     public void onCreateBookmark(DialogFragment dialogFragment) {
         // Get the `EditText`s from the `createBookmarkDialogFragment` and extract the strings.
@@ -542,8 +557,9 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         EditText createBookmarkUrlEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext);
         String bookmarkUrlString = createBookmarkUrlEditText.getText().toString();
 
-        // Convert the favoriteIcon Bitmap to a byte array.  `0` is for lossless compression (the only option for a PNG).
+        // Convert the favoriteIcon Bitmap to a byte array.
         ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
+        // `0` is for lossless compression (the only option for a PNG).
         MainWebViewActivity.favoriteIcon.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
         byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
 
@@ -558,11 +574,6 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         bookmarksListView.setSelection(newBookmarkDisplayOrder);
     }
 
-    @Override
-    public void onCancelCreateBookmarkFolder(DialogFragment dialogFragment) {
-        // Do nothing because the user selected `Cancel`.
-    }
-
     @Override
     public void onCreateBookmarkFolder(DialogFragment dialogFragment) {
         // Get `create_folder_name_edit_text` and extract the string.
@@ -577,19 +588,20 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
             String cannotCreateFolder = getResources().getString(R.string.cannot_create_folder) + " \"" + folderNameString + "\"";
             Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), cannotCreateFolder, Snackbar.LENGTH_INDEFINITE).show();
         } else {  // Create the folder.
-            // Get the new folder icon.
-            RadioButton defaultFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobuttion);
+            // Get the new folder icon `Bitmap`.
+            RadioButton defaultFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobutton);
             Bitmap folderIconBitmap;
             if (defaultFolderIconRadioButton.isChecked()) {
-                // Get the default folder icon drawable and convert it to a `Bitmap`.  `this` specifies the current context.
-                Drawable folderIconDrawable = ContextCompat.getDrawable(this, R.drawable.folder_blue_bitmap);
+                // Get the default folder icon `ImageView` from the `Dialog` and convert it to a `Bitmap`.
+                ImageView folderIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon);
+                Drawable folderIconDrawable = folderIconImageView.getDrawable();
                 BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
                 folderIconBitmap = folderIconBitmapDrawable.getBitmap();
-            } else {
+            } else {  // Assign `favoriteIcon` from the `WebView`.
                 folderIconBitmap = MainWebViewActivity.favoriteIcon;
             }
 
-            // Convert the folder `Bitmap` to a byte array.  `0` is for lossless compression (the only option for a PNG).
+            // Convert `folderIconBitmap` to a byte array.  `0` is for lossless compression (the only option for a PNG).
             ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
             folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
             byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
@@ -608,11 +620,6 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         }
     }
 
-    @Override
-    public void onCancelEditBookmark(DialogFragment dialogFragment) {
-        // Do nothing because the user selected `Cancel`.
-    }
-
     @Override
     public void onSaveEditBookmark(DialogFragment dialogFragment) {
         // Get a long array with the the databaseId of the selected bookmark and convert it to an `int`.
@@ -630,15 +637,9 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
 
         if (currentBookmarkIconRadioButton.isChecked()) {  // Update the bookmark without changing the favorite icon.
             bookmarksDatabaseHandler.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString);
-        } else {  // Update the bookmark and the favorite icon.
-            // Get the new favorite icon from the `Dialog` and convert it into a `Bitmap`.
-            ImageView newFavoriteIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.edit_bookmark_web_page_favorite_icon);
-            Drawable newFavoriteIconDrawable = newFavoriteIconImageView.getDrawable();
-            Bitmap newFavoriteIconBitmap = ((BitmapDrawable) newFavoriteIconDrawable).getBitmap();
-
-            // Convert `newFavoriteIconBitmap` into a Byte Array.
+        } else {  // Update the bookmark using the `WebView` favorite icon.
             ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
-            newFavoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
+            MainWebViewActivity.favoriteIcon.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
             byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
 
             //  Update the bookmark and the favorite icon.
@@ -653,11 +654,6 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         bookmarksListView.setSelection(selectedBookmarkPosition);
     }
 
-    @Override
-    public void onCancelEditBookmarkFolder(DialogFragment dialogFragment) {
-        // Do nothing because the user selected `Cancel`.
-    }
-
     @Override
     public void onSaveEditBookmarkFolder(DialogFragment dialogFragment) {
         // Get the new folder name.
@@ -676,11 +672,10 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
             // Get the `RadioButtons` from the `Dialog`.
             RadioButton currentFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
             RadioButton defaultFolderIconRadioButton = (RadioButton) dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
-            Bitmap folderIconBitmap;
 
-            // Prepare the favorite icon.
+            // Check if the favorite icon has changed.
             if (currentFolderIconRadioButton.isChecked()) {
-                // Update the folder name if it has changed.
+                // Update the folder name if it has changed without modifying the favorite icon.
                 if (!newFolderNameString.equals(oldFolderNameString)) {
                     bookmarksDatabaseHandler.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString);
 
@@ -688,13 +683,16 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
                     updateBookmarksListView(currentFolder);
                     bookmarksListView.setSelection(selectedBookmarkPosition);
                 }
-            } else {  // Prepare the new favorite icon.
+            } else {  // Update the folder icon.
+                // Get the new folder icon `Bitmap`.
+                Bitmap folderIconBitmap;
                 if (defaultFolderIconRadioButton.isChecked()) {
-                    // Get the default folder icon drawable and convert it to a `Bitmap`.  `this` specifies the current context.
-                    Drawable folderIconDrawable = ContextCompat.getDrawable(this, R.drawable.folder_blue_bitmap);
+                    // Get the default folder icon `ImageView` from the `Drawable` and convert it to a `Bitmap`.
+                    ImageView folderIconImageView = (ImageView) dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon);
+                    Drawable folderIconDrawable = folderIconImageView.getDrawable();
                     BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
                     folderIconBitmap = folderIconBitmapDrawable.getBitmap();
-                } else {  // Use the web page favorite icon.
+                } else {  // Get the web page icon `ImageView` from the `Dialog`.
                     folderIconBitmap = MainWebViewActivity.favoriteIcon;
                 }
 
@@ -718,11 +716,6 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
         contextualActionMode.finish();
     }
 
-    @Override
-    public void onCancelMoveToFolder(DialogFragment dialogFragment) {
-        // Do nothing because the user selected `Cancel`.
-    }
-
     @Override
     public void onMoveToFolder(DialogFragment dialogFragment) {
         // Get the new folder database id.
@@ -796,7 +789,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
 
                 // Make the font bold for folders.
                 if (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHandler.IS_FOLDER)) == 1) {
-                    // The first argument is `null` because we don't want to chage the font.
+                    // The first argument is `null` because we don't want to change the font.
                     bookmarkNameTextView.setTypeface(null, Typeface.BOLD);
                 } else {  // Reset the font to default.
                     bookmarkNameTextView.setTypeface(Typeface.DEFAULT);
@@ -847,7 +840,7 @@ public class BookmarksActivity extends AppCompatActivity implements CreateBookma
 
                 // Make the font bold for folders.
                 if (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHandler.IS_FOLDER)) == 1) {
-                    // The first argument is `null` because we don't want to chage the font.
+                    // The first argument is `null` because we don't want to change the font.
                     bookmarkNameTextView.setTypeface(null, Typeface.BOLD);
                 } else {  // Reset the font to default.
                     bookmarkNameTextView.setTypeface(Typeface.DEFAULT);