X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FBookmarksDatabaseViewActivity.java;h=3cdc67923370809053db8bf92d72bc2e73eb6459;hp=70bd7735c0a5a81d47ee9f3a2fd2681612faa45b;hb=f0393ca22075be3e5fe9199c7db87381256236fa;hpb=47b689dbdaf08b9636021ddd8f72ca9ee7f11998 diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java index 70bd7735..3cdc6792 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java @@ -19,7 +19,10 @@ package com.stoutner.privacybrowser.activities; +import android.annotation.SuppressLint; import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; import android.database.Cursor; import android.database.MatrixCursor; import android.database.MergeCursor; @@ -29,15 +32,7 @@ import android.graphics.Typeface; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; -import android.support.design.widget.Snackbar; -import android.support.v4.content.ContextCompat; -import android.support.v4.widget.CursorAdapter; -import android.support.v4.widget.ResourceCursorAdapter; -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.preference.PreferenceManager; import android.util.SparseBooleanArray; import android.view.ActionMode; import android.view.Menu; @@ -51,9 +46,19 @@ import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.RadioButton; +import android.widget.ResourceCursorAdapter; import android.widget.Spinner; 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.content.ContextCompat; +import androidx.cursoradapter.widget.CursorAdapter; +import androidx.fragment.app.DialogFragment; // The AndroidX dialog fragment must be used or an error is produced on API <=22. + +import com.google.android.material.snackbar.Snackbar; + import com.stoutner.privacybrowser.R; import com.stoutner.privacybrowser.dialogs.EditBookmarkDatabaseViewDialog; import com.stoutner.privacybrowser.dialogs.EditBookmarkFolderDatabaseViewDialog; @@ -97,13 +102,20 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements @Override public void onCreate(Bundle savedInstanceState) { + // Get a handle for the shared preferences. + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + + // Get the theme and screenshot preferences. + boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false); + boolean allowScreenshots = sharedPreferences.getBoolean("allow_screenshots", false); + // Disable screenshots if not allowed. - if (!MainWebViewActivity.allowScreenshots) { + if (!allowScreenshots) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); } // Set the activity theme. - if (MainWebViewActivity.darkTheme) { + if (darkTheme) { setTheme(R.style.PrivacyBrowserDark_SecondaryActivity); } else { setTheme(R.style.PrivacyBrowserLight_SecondaryActivity); @@ -112,22 +124,34 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Run the default commands. super.onCreate(savedInstanceState); + // Get the intent that launched the activity. + Intent launchingIntent = getIntent(); + + // Get the favorite icon byte array. + byte[] favoriteIconByteArray = launchingIntent.getByteArrayExtra("favorite_icon_byte_array"); + + // Remove the incorrect lint warning below that the favorite icon byte array might be null. + assert favoriteIconByteArray != null; + + // Convert the favorite icon byte array to a bitmap and store it in a class variable. + Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); + // Set the content view. setContentView(R.layout.bookmarks_databaseview_coordinatorlayout); - // The `SupportActionBar` from `android.support.v7.app.ActionBar` must be used until the minimum API is >= 21. - Toolbar bookmarksDatabaseViewAppBar = findViewById(R.id.bookmarks_databaseview_toolbar); - setSupportActionBar(bookmarksDatabaseViewAppBar); + // The AndroidX toolbar must be used until the minimum API is >= 21. + Toolbar toolbar = findViewById(R.id.bookmarks_databaseview_toolbar); + setSupportActionBar(toolbar); - // Get a handle for the `AppBar`. - ActionBar appBar = getSupportActionBar(); + // Get a handle for the action bar. + ActionBar actionBar = getSupportActionBar(); - // Remove the incorrect warning in Android Studio that `appBar` might be null. - assert appBar != null; + // Remove the incorrect lint warning that the action bar might be null. + assert actionBar != null; - // Display the spinner and the back arrow in the app bar. - appBar.setCustomView(R.layout.spinner); - appBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP); + // Display the spinner and the back arrow in the action bar. + actionBar.setCustomView(R.layout.spinner); + actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP); // Initialize the database handler. The `0` is to specify a database version, but that is set instead using a constant in `BookmarksDatabaseHelper`. bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this, null, null, 0); @@ -309,7 +333,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements bookmarkParentFolderTextView.setText(bookmarkParentFolder); // Set the text color according to the theme. - if (MainWebViewActivity.darkTheme) { + if (darkTheme) { bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.gray_300)); } else { bookmarkParentFolderTextView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black)); @@ -335,11 +359,11 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); // Show the edit bookmark folder dialog. - AppCompatDialogFragment editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog.folderDatabaseId(databaseId); + DialogFragment editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog.folderDatabaseId(databaseId, favoriteIconBitmap); editBookmarkFolderDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_folder)); } else { // Show the edit bookmark dialog. - AppCompatDialogFragment editBookmarkDatabaseViewDialog = EditBookmarkDatabaseViewDialog.bookmarkDatabaseId(databaseId); + DialogFragment editBookmarkDatabaseViewDialog = EditBookmarkDatabaseViewDialog.bookmarkDatabaseId(databaseId, favoriteIconBitmap); editBookmarkDatabaseViewDialog.show(getSupportFragmentManager(), getResources().getString(R.string.edit_bookmark)); } }); @@ -502,6 +526,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // 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) { @@ -568,9 +593,16 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements @Override public boolean onOptionsItemSelected(MenuItem menuItem) { + // Get a handle for the shared preferences. + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + + // Get the theme preferences. + boolean darkTheme = sharedPreferences.getBoolean("dark_theme", false); + // Get the ID of the menu item that was selected. int menuItemId = menuItem.getItemId(); + // Run the command that corresponds to the selected menu item. switch (menuItemId) { case android.R.id.home: // The home arrow is identified as `android.R.id.home`, not just `R.id.home`. // Exit the activity. @@ -587,7 +619,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Update the icon and display a snackbar. if (sortByDisplayOrder) { // Sort by display order. // Update the icon according to the theme. - if (MainWebViewActivity.darkTheme) { + if (darkTheme) { menuItem.setIcon(R.drawable.sort_selected_dark); } else { menuItem.setIcon(R.drawable.sort_selected_light); @@ -597,7 +629,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements Snackbar.make(bookmarksListView, R.string.sorted_by_display_order, Snackbar.LENGTH_SHORT).show(); } else { // Sort by database id. // Update the icon according to the theme. - if (MainWebViewActivity.darkTheme) { + if (darkTheme) { menuItem.setIcon(R.drawable.sort_dark); } else { menuItem.setIcon(R.drawable.sort_light); @@ -733,7 +765,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements } @Override - public void onSaveBookmark(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) { + public void onSaveBookmark(DialogFragment dialogFragment, int selectedBookmarkDatabaseId, Bitmap favoriteIconBitmap) { // Get handles for the views from dialog fragment. RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_current_icon_radiobutton); EditText editBookmarkNameEditText = dialogFragment.getDialog().findViewById(R.id.edit_bookmark_name_edittext); @@ -761,9 +793,13 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements if (currentBookmarkIconRadioButton.isChecked()) { // Update the bookmark without changing the favorite icon. bookmarksDatabaseHelper.updateBookmark(selectedBookmarkDatabaseId, bookmarkNameString, bookmarkUrlString, parentFolderNameString, displayOrderInt); } 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). + // 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. @@ -775,7 +811,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements } @Override - public void onSaveBookmarkFolder(AppCompatDialogFragment dialogFragment, int selectedBookmarkDatabaseId) { + public void onSaveBookmarkFolder(DialogFragment dialogFragment, int selectedBookmarkDatabaseId, Bitmap favoriteIconBitmap) { // Get handles for the views from dialog fragment. RadioButton currentBookmarkIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_current_icon_radiobutton); RadioButton defaultFolderIconRadioButton = dialogFragment.getDialog().findViewById(R.id.edit_folder_default_icon_radiobutton); @@ -803,22 +839,31 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements if (currentBookmarkIconRadioButton.isChecked()) { // Update the folder without changing the favorite icon. bookmarksDatabaseHelper.updateFolder(selectedBookmarkDatabaseId, oldFolderNameString, newFolderNameString, parentFolderNameString, displayOrderInt); } else { // Update the folder and the icon. - // Instantiate 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; + // Get a copy of the favorite icon bitmap. + folderIconBitmap = favoriteIconBitmap; } - // Convert the folder icon to a byte array. `0` is for lossless compression (the only option for a PNG). + // 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 and the icon.