]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/activities/Bookmarks.java
Partial Domain implementation.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / activities / Bookmarks.java
index 78cb806e6c17085bb865a7f35f5dcb631a1e6e37..03ee0d17699d94680b63e06bb44a919e66647a85 100644 (file)
@@ -1,5 +1,5 @@
-/**
- * Copyright 2016 Soren Stoutner <soren@stoutner.com>.
+/*
+ * Copyright 2016-2017 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
  *
@@ -61,9 +61,8 @@ import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolder;
 
 import java.io.ByteArrayOutputStream;
 
-public class Bookmarks extends AppCompatActivity implements CreateBookmark.CreateBookmarkListener,
-        CreateBookmarkFolder.CreateBookmarkFolderListener, EditBookmark.EditBookmarkListener,
-        EditBookmarkFolder.EditBookmarkFolderListener, MoveToFolder.MoveToFolderListener {
+public class Bookmarks extends AppCompatActivity implements CreateBookmark.CreateBookmarkListener, CreateBookmarkFolder.CreateBookmarkFolderListener, EditBookmark.EditBookmarkListener, EditBookmarkFolder.EditBookmarkFolderListener,
+        MoveToFolder.MoveToFolderListener {
 
     // `bookmarksDatabaseHelper` is public static so it can be accessed from `EditBookmark` and `MoveToFolder`.  It is also used in `onCreate()`,
     // `onCreateBookmarkCreate()`, `updateBookmarksListView()`, and `updateBookmarksListViewExcept()`.
@@ -107,24 +106,22 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat
 
         // 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.
+        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 handler and the ListView.
-        // `this` specifies the context.  The two `null`s do not specify the database name or a `CursorFactory`.
-        // The `0` is to specify a database version, but that is set instead using a constant in `BookmarksDatabaseHelper`.
+        // Initialize the database handler 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 null in the database.
+        // 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.
+        // 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
@@ -132,7 +129,7 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat
                 // Convert the id from long to int to match the format of the bookmarks database.
                 int databaseID = (int) id;
 
-                // Get the bookmark `Cursor` and move it to the first row.
+                // Get the bookmark `Cursor` for this ID and move it to the first row.
                 Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmarkCursor(databaseID);
                 bookmarkCursor.moveToFirst();
 
@@ -207,16 +204,14 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat
                 // Calculate the number of selected bookmarks.
                 int numberOfSelectedBookmarks = selectedBookmarksLongArray.length;
 
-                // Sometimes Android forgets to close the contextual app bar when all the items are deselected.
+                // Adjust the `mode` and the menu for the number of selected bookmarks.
                 if (numberOfSelectedBookmarks == 0) {
                     mode.finish();
-                }
-
-                // List the number of selected bookmarks in the subtitle.
-                mode.setSubtitle(numberOfSelectedBookmarks + " " + getString(R.string.selected));
+                } else if (numberOfSelectedBookmarks == 1) {
+                    // List the number of selected bookmarks in the subtitle.
+                    mode.setSubtitle(getString(R.string.one_selected));
 
-                if (numberOfSelectedBookmarks == 1) {
-                    // Show the `Move Up`, `Move Down`, and  `Edit` option only if 1 bookmark is selected.
+                    // Show the `Move Up`, `Move Down`, and  `Edit` options.
                     moveBookmarkUpMenuItem.setVisible(true);
                     moveBookmarkDownMenuItem.setVisible(true);
                     editBookmarkMenuItem.setVisible(true);
@@ -244,7 +239,11 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat
                         moveBookmarkDownMenuItem.setEnabled(true);
                         moveBookmarkDownMenuItem.setIcon(R.drawable.move_bookmark_down_enabled);
                     }
-                } else {  // Hide the MenuItems because more than one bookmark is selected.
+                } 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);
@@ -421,7 +420,7 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat
                                         // Do nothing because everything will be handled by `onDismissed()` below.
                                     }
                                 })
-                                .setCallback(new Snackbar.Callback() {
+                                .addCallback(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.
@@ -523,13 +522,13 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat
 
         switch (menuItemId) {
             case android.R.id.home:
-                // Exit Bookmarks if currently at the home folder.
-                if (currentFolder.isEmpty()) {
+                if (currentFolder.isEmpty()) {  // Exit Bookmarks if currently in the home folder.
                     NavUtils.navigateUpFromSameTask(this);
                 } else {  // Navigate up one folder.
                     // Place the former parent folder in `currentFolder`.
                     currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder);
 
+                    // Exit Bookmarks if currently in the home folder.
                     updateBookmarksListView(currentFolder);
                 }
                 break;
@@ -557,12 +556,27 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat
         return true;
     }
 
+    @Override
+    public void onBackPressed() {
+        if (currentFolder.isEmpty()) {  // Exit Bookmarks if currently in the home folder.
+            super.onBackPressed();
+        } else {  // Navigate up one folder.
+            // Place the former parent folder in `currentFolder`.
+            currentFolder = bookmarksDatabaseHelper.getParentFolder(currentFolder);
+
+            // Reload the `ListView`.
+            updateBookmarksListView(currentFolder);
+        }
+    }
+
     @Override
     public void onCreateBookmark(AppCompatDialogFragment dialogFragment) {
-        // Get the `EditText`s from the `createBookmarkDialogFragment` and extract the strings.
+        // Get the `EditTexts` from the `dialogFragment`.
         EditText createBookmarkNameEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext);
-        String bookmarkNameString = createBookmarkNameEditText.getText().toString();
         EditText createBookmarkUrlEditText = (EditText) dialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext);
+
+        // Extract the strings from the `EditTexts`.
+        String bookmarkNameString = createBookmarkNameEditText.getText().toString();
         String bookmarkUrlString = createBookmarkUrlEditText.getText().toString();
 
         // Convert the favoriteIcon Bitmap to a byte array.
@@ -577,7 +591,7 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat
         // Create the bookmark.
         bookmarksDatabaseHelper.createBookmark(bookmarkNameString, bookmarkUrlString, newBookmarkDisplayOrder, currentFolder, favoriteIconByteArray);
 
-        // Refresh the ListView.  `setSelection` scrolls to the bottom of the list.
+        // Refresh the `ListView`.  `setSelection` scrolls to the bottom of the list.
         updateBookmarksListView(currentFolder);
         bookmarksListView.setSelection(newBookmarkDisplayOrder);
     }
@@ -716,7 +730,7 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat
                 bookmarksListView.setSelection(selectedBookmarkPosition);
             }
         } else {  // Don't edit the folder because the new name is not unique.
-            String cannot_rename_folder = getResources().getString(R.string.cannot_rename_folder) + " \"" + newFolderNameString + "\"";
+            String cannot_rename_folder = getResources().getString(R.string.cannot_save_folder) + " \"" + newFolderNameString + "\"";
             Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), cannot_rename_folder, Snackbar.LENGTH_INDEFINITE).show();
         }
 
@@ -731,7 +745,7 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat
         long[] newFolderLongArray = folderListView.getCheckedItemIds();
 
         if (newFolderLongArray.length == 0) {  // No new folder was selected.
-            Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), getString(R.string.cannot_move_bookmarks), Snackbar.LENGTH_LONG).show();
+            Snackbar.make(findViewById(R.id.bookmarks_coordinatorlayout), getString(R.string.cannot_move_bookmarks), Snackbar.LENGTH_INDEFINITE).show();
         } else {  // Move the selected bookmarks.
             // Get the new folder database ID.
             int newFolderDatabaseId = (int) newFolderLongArray[0];
@@ -769,7 +783,7 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat
         // Get a `Cursor` with the current contents of the bookmarks database.
         bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarksCursorByDisplayOrder(folderName);
 
-        // Setup `bookmarksCursorAdapter` with `this` context.  `false` disables autoRequery.
+        // Setup `bookmarksCursorAdapter` with `this` context.  `false` disables `autoRequery`.
         CursorAdapter bookmarksCursorAdapter = new CursorAdapter(this, bookmarksCursor, false) {
             @Override
             public View newView(Context context, Cursor cursor, ViewGroup parent) {
@@ -855,7 +869,7 @@ public class Bookmarks extends AppCompatActivity implements CreateBookmark.Creat
             }
         };
 
-        // Update the ListView.
+        // Update the `ListView`.
         bookmarksListView.setAdapter(bookmarksCursorAdapter);
     }