package com.stoutner.privacybrowser.activities;
+import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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;
-// `AppCompatDialogFragment` is required instead of `DialogFragment` or an error is produced on API <=22.
-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.widget.RadioButton;
import android.widget.TextView;
+import androidx.appcompat.app.ActionBar;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.appcompat.widget.Toolbar; // The AndroidX toolbar must be used until the minimum API is >= 21.
+import androidx.core.app.NavUtils;
+import androidx.fragment.app.DialogFragment;
+
+import com.google.android.material.floatingactionbutton.FloatingActionButton;
+import com.google.android.material.snackbar.Snackbar;
+
import com.stoutner.privacybrowser.dialogs.CreateBookmarkDialog;
import com.stoutner.privacybrowser.dialogs.CreateBookmarkFolderDialog;
import com.stoutner.privacybrowser.dialogs.EditBookmarkDialog;
// `restartFromBookmarksDatabaseViewActivity` is public static so it can be accessed from `BookmarksDatabaseViewActivity`. It is also used in `onRestart()`.
public static boolean restartFromBookmarksDatabaseViewActivity;
+ // The current WebView strings are public static so they can be updated from `MainWebViewActivity`. They are use in `onCreate()`.
+ public static String currentWebViewUrl;
+ public static String currentWebViewTitle;
+
// `bookmarksDatabaseHelper` is used in `onCreate()`, `onOptionsItemSelected()`, `onBackPressed()`, `onCreateBookmark()`, `onCreateBookmarkFolder()`, `onSaveBookmark()`, `onSaveBookmarkFolder()`,
// `onMoveToFolder()`, `deleteBookmarkFolderContents()`, `loadFolder()`, and `onDestroy()`.
// `closeActivityAfterDismissingSnackbar` is used in `onCreate()`, `onOptionsItemSelected()`, and `onBackPressed()`.
private boolean closeActivityAfterDismissingSnackbar;
+ // The favorite icon byte array is populated in `onCreate()` and used in `onOptionsItemSelected()`.
+ private byte[] favoriteIconByteArray;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
// Disable screenshots if not allowed.
Intent launchingIntent = getIntent();
// Set the current folder variable.
- if (launchingIntent.getStringExtra("Current Folder") != null) { // Set the current folder from the intent.
- currentFolder = launchingIntent.getStringExtra("Current Folder");
+ if (launchingIntent.getStringExtra("current_folder") != null) { // Set the current folder from the intent.
+ currentFolder = launchingIntent.getStringExtra("current_folder");
} else { // Set the current folder to be `""`, which is the home folder.
currentFolder = "";
}
+ // Get the favorite icon byte array.
+ favoriteIconByteArray = launchingIntent.getByteArrayExtra("favorite_icon_byte_array");
+
+ // Convert the favorite icon byte array to a bitmap.
+ Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
+
// 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 = findViewById(R.id.bookmarks_toolbar);
- setSupportActionBar(bookmarksAppBar);
+ // The AndroidX toolbar must be used until the minimum API is >= 21.
+ final Toolbar toolbar = findViewById(R.id.bookmarks_toolbar);
+ setSupportActionBar(toolbar);
// Get a handle for the activity, the app bar, and the ListView.
final Activity bookmarksActivity = this;
bookmarkCursor.close();
});
- // Get a handle for the activity.
- Activity activity = this;
-
// Handle long presses on the list view.
bookmarksListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
// Instantiate the common variables.
checkedItemIds = bookmarksListView.getCheckedItemIds();
// Show the `MoveToFolderDialog` `AlertDialog` and name the instance `@string/move_to_folder
- AppCompatDialogFragment moveToFolderDialog = new MoveToFolderDialog();
+ DialogFragment moveToFolderDialog = new MoveToFolderDialog();
moveToFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.move_to_folder));
break;
oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
// Show the edit bookmark folder dialog.
- AppCompatDialogFragment editFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId);
+ DialogFragment editFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId, favoriteIconBitmap);
editFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder));
} else {
// Show the edit bookmark dialog.
- AppCompatDialogFragment editBookmarkDialog = EditBookmarkDialog.bookmarkDatabaseId(databaseId);
+ DialogFragment editBookmarkDialog = EditBookmarkDialog.bookmarkDatabaseId(databaseId, favoriteIconBitmap);
editBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark));
}
break;
// Do nothing because everything will be handled by `onDismissed()` below.
})
.addCallback(new Snackbar.Callback() {
+ @SuppressLint("SwitchIntDef") // Ignore the lint warning about not handling the other possible events as they are covered by `default:`.
@Override
public void onDismissed(Snackbar snackbar, int event) {
switch (event) {
// Set the create new bookmark folder FAB to display the `AlertDialog`.
createBookmarkFolderFab.setOnClickListener(v -> {
- // Show the `CreateBookmarkFolderDialog` `AlertDialog` and name the instance `@string/create_folder`.
- AppCompatDialogFragment createBookmarkFolderDialog = new CreateBookmarkFolderDialog();
- createBookmarkFolderDialog.show(getSupportFragmentManager(), getResources().getString(R.string.create_folder));
+ // Create a create bookmark folder dialog.
+ DialogFragment createBookmarkFolderDialog = CreateBookmarkFolderDialog.createBookmarkFolder(favoriteIconBitmap);
+
+ // Show the create bookmark folder dialog.
+ createBookmarkFolderDialog.show(getSupportFragmentManager(), getString(R.string.create_folder));
});
// Set the create new bookmark FAB to display the `AlertDialog`.
createBookmarkFab.setOnClickListener(view -> {
- // Show the `CreateBookmarkDialog` `AlertDialog` and name the instance `@string/create_bookmark`.
- AppCompatDialogFragment createBookmarkDialog = new CreateBookmarkDialog();
+ // Instantiate the create bookmark dialog.
+ DialogFragment createBookmarkDialog = CreateBookmarkDialog.createBookmark(currentWebViewUrl, currentWebViewTitle, favoriteIconBitmap);
+
+ // Display the create bookmark dialog.
createBookmarkDialog.show(getSupportFragmentManager(), getResources().getString(R.string.create_bookmark));
});
}
break;
case R.id.bookmarks_database_view:
- // Launch `BookmarksDatabaseViewActivity`.
+ // Create an intent to launch the bookmarks database view activity.
Intent bookmarksDatabaseViewIntent = new Intent(this, BookmarksDatabaseViewActivity.class);
+
+ // Include the favorite icon byte array to the intent.
+ bookmarksDatabaseViewIntent.putExtra("favorite_icon_byte_array", favoriteIconByteArray);
+
+ // Make it so.
startActivity(bookmarksDatabaseViewIntent);
break;
}
}
@Override
- public void onCreateBookmark(AppCompatDialogFragment dialogFragment) {
- // Get the `EditTexts` from the `dialogFragment`.
+ public void onCreateBookmark(DialogFragment dialogFragment, Bitmap favoriteIconBitmap) {
+ // Get the views from the dialog fragment.
EditText createBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext);
EditText createBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext);
- // Extract the strings from the `EditTexts`.
+ // Extract the strings from the edit texts.
String bookmarkNameString = createBookmarkNameEditText.getText().toString();
String bookmarkUrlString = createBookmarkUrlEditText.getText().toString();
- // Convert the favoriteIcon Bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
+ // Create a favorite icon byte array output stream.
ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
- MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
+
+ // Convert the favorite icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
+ favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
+
+ // Convert the favorite icon byte array stream to a byte array.
byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
// Display the new bookmark below the current items in the (0 indexed) list.
}
@Override
- public void onCreateBookmarkFolder(AppCompatDialogFragment dialogFragment) {
- // Get handles for the views in `dialogFragment`.
+ public void onCreateBookmarkFolder(DialogFragment dialogFragment, Bitmap favoriteIconBitmap) {
+ // Get handles for the views in the dialog fragment.
EditText createFolderNameEditText = dialogFragment.getDialog().findViewById(R.id.create_folder_name_edittext);
RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon_radiobutton);
ImageView folderIconImageView = dialogFragment.getDialog().findViewById(R.id.create_folder_default_icon);
// Get new folder name string.
String folderNameString = createFolderNameEditText.getText().toString();
- // Get the new folder icon `Bitmap`.
+ // Create a folder icon bitmap.
Bitmap folderIconBitmap;
+
+ // Set the folder icon bitmap according to the dialog.
if (defaultFolderIconRadioButton.isChecked()) { // Use the default folder icon.
- // Get the default folder icon and convert it to a `Bitmap`.
+ // Get the default folder icon drawable.
Drawable folderIconDrawable = folderIconImageView.getDrawable();
+
+ // Convert the folder icon drawable to a bitmap drawable.
BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
+
+ // Convert the folder icon bitmap drawable to a bitmap.
folderIconBitmap = folderIconBitmapDrawable.getBitmap();
- } else { // Use the `WebView` favorite icon.
- folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
+ } else { // Use the WebView favorite icon.
+ // Copy the favorite icon bitmap to the folder icon bitmap.
+ folderIconBitmap = favoriteIconBitmap;
}
- // Convert `folderIconBitmap` to a byte array. `0` is for lossless compression (the only option for a PNG).
+ // Create a folder icon byte array output stream.
ByteArrayOutputStream folderIconByteArrayOutputStream = new ByteArrayOutputStream();
+
+ // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, folderIconByteArrayOutputStream);
+
+ // Convert the folder icon byte array stream to a byte array.
byte[] folderIconByteArray = folderIconByteArrayOutputStream.toByteArray();
// Move all the bookmarks down one in the display order.
}
@Override
- public void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) {
+ public void onSaveBookmark(DialogFragment dialogFragment, int selectedBookmarkDatabaseId, Bitmap favoriteIconBitmap) {
// Get handles for the views from `dialogFragment`.
EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext);
EditText editBookmarkUrlEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_url_edittext);
// Update the bookmark.
if (currentBookmarkIconRadioButton.isChecked()) { // Update the bookmark without changing the favorite icon.
bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString);
- } else { // Update the bookmark using the `WebView` favorite icon.
- // Convert the favorite icon to a byte array. `0` is for lossless compression (the only option for a PNG).
+ } else { // Update the bookmark using the WebView favorite icon.
+ // Create a favorite icon byte array output stream.
ByteArrayOutputStream newFavoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
- MainWebViewActivity.favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
+
+ // Convert the favorite icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
+ favoriteIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFavoriteIconByteArrayOutputStream);
+
+ // Convert the favorite icon byte array stream to a byte array.
byte[] newFavoriteIconByteArray = newFavoriteIconByteArrayOutputStream.toByteArray();
// Update the bookmark and the favorite icon.
}
@Override
- public void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedFolderDatabaseId) {
+ public void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedFolderDatabaseId, Bitmap favoriteIconBitmap) {
// Get handles for the views from `dialogFragment`.
RadioButton currentFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton);
RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton);
// Update the name in the database.
bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString);
} else if (!currentFolderIconRadioButton.isChecked() && newFolderNameString.equals(oldFolderNameString)) { // Only the icon has changed.
- // Get the new folder icon `Bitmap`.
+ // Create the new folder icon Bitmap.
Bitmap folderIconBitmap;
+
+ // Populate the new folder icon bitmap.
if (defaultFolderIconRadioButton.isChecked()) {
- // Get the default folder icon and convert it to a `Bitmap`.
+ // Get the default folder icon drawable.
Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
+
+ // Convert the folder icon drawable to a bitmap drawable.
BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
+
+ // Convert the folder icon bitmap drawable to a bitmap.
folderIconBitmap = folderIconBitmapDrawable.getBitmap();
- } else { // Use the `WebView` favorite icon.
- folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
+ } else { // Use the WebView favorite icon.
+ // Copy the favorite icon bitmap to the folder icon bitmap.
+ folderIconBitmap = favoriteIconBitmap;
}
- // Convert the folder `Bitmap` 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();
+ // Create a folder icon byte array output stream.
+ ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
+
+ // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
+ folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
+
+ // Convert the folder icon byte array stream to a byte array.
+ byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
// Update the folder icon in the database.
- bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, folderIconByteArray);
+ bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, newFolderIconByteArray);
} else { // The folder icon and the name have changed.
// Instantiate the new folder icon `Bitmap`.
Bitmap folderIconBitmap;
// Populate the new folder icon bitmap.
if (defaultFolderIconRadioButton.isChecked()) {
- // Get the default folder icon and convert it to a `Bitmap`.
+ // Get the default folder icon drawable.
Drawable folderIconDrawable = defaultFolderIconImageView.getDrawable();
+
+ // Convert the folder icon drawable to a bitmap drawable.
BitmapDrawable folderIconBitmapDrawable = (BitmapDrawable) folderIconDrawable;
+
+ // Convert the folder icon bitmap drawable to a bitmap.
folderIconBitmap = folderIconBitmapDrawable.getBitmap();
- } else { // Use the `WebView` favorite icon.
- folderIconBitmap = MainWebViewActivity.favoriteIconBitmap;
+ } else { // Use the WebView favorite icon.
+ // Copy the favorite icon bitmap to the folder icon bitmap.
+ folderIconBitmap = favoriteIconBitmap;
}
- // Convert the folder `Bitmap` 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();
+ // Create a folder icon byte array output stream.
+ ByteArrayOutputStream newFolderIconByteArrayOutputStream = new ByteArrayOutputStream();
+
+ // Convert the folder icon bitmap to a byte array. `0` is for lossless compression (the only option for a PNG).
+ folderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, newFolderIconByteArrayOutputStream);
+
+ // Convert the folder icon byte array stream to a byte array.
+ byte[] newFolderIconByteArray = newFolderIconByteArrayOutputStream.toByteArray();
// Update the folder name and icon in the database.
- bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, folderIconByteArray);
+ bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString, newFolderIconByteArray);
}
// Update the bookmarks cursor with the current contents of this folder.
}
@Override
- public void onMoveToFolder(AppCompatDialogFragment dialogFragment) {
+ public void onMoveToFolder(DialogFragment dialogFragment) {
// Get a handle for the `ListView` from `dialogFragment`.
ListView folderListView = dialogFragment.getDialog().findViewById(R.id.move_to_folder_listview);
String folderName = bookmarksDatabaseHelper.getFolderName(databaseId);
// Get the contents of the folder.
- Cursor folderCursor = bookmarksDatabaseHelper.getBookmarksByDisplayOrder(folderName);
+ Cursor folderCursor = bookmarksDatabaseHelper.getBookmarkIDs(folderName);
// Delete each of the bookmarks in the folder.
for (int i = 0; i < folderCursor.getCount(); i++) {