X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FBookmarksDatabaseViewActivity.java;h=0560c017a0a78deb65ceba3e9bc952103b1bc7f5;hb=44a7a1d29d8e4722e2ea522075878a13b89bcb52;hp=d084d5c68ffc7e977b00850396e74899c3f4d5ae;hpb=cd609c9888924549c03cd6509ed889cdb052d5bb;p=PrivacyBrowserAndroid.git 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 d084d5c6..0560c017 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksDatabaseViewActivity.java @@ -1,20 +1,20 @@ /* - * Copyright © 2016-2021 Soren Stoutner . + * Copyright 2016-2022 Soren Stoutner . * - * This file is part of Privacy Browser . + * This file is part of Privacy Browser Android . * - * Privacy Browser is free software: you can redistribute it and/or modify + * Privacy Browser Android 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, + * Privacy Browser Android 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 . + * along with Privacy Browser Android. If not, see . */ package com.stoutner.privacybrowser.activities; @@ -34,7 +34,6 @@ import android.graphics.Typeface; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; -import android.preference.PreferenceManager; import android.util.SparseBooleanArray; import android.view.ActionMode; import android.view.Menu; @@ -53,6 +52,7 @@ import android.widget.ResourceCursorAdapter; import android.widget.Spinner; import android.widget.TextView; +import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; @@ -60,6 +60,7 @@ import androidx.appcompat.widget.Toolbar; // The AndroidX toolbar must be used 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 androidx.preference.PreferenceManager; import com.google.android.material.snackbar.Snackbar; @@ -107,9 +108,6 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); } - // Set the activity theme. - setTheme(R.style.PrivacyBrowser); - // Run the default commands. super.onCreate(savedInstanceState); @@ -128,13 +126,13 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Set the view according to the theme. if (bottomAppBar) { // Set the content view. - setContentView(R.layout.bookmarks_databaseview_coordinatorlayout_bottom_appbar); + setContentView(R.layout.bookmarks_databaseview_bottom_appbar); } else { // `Window.FEATURE_ACTION_MODE_OVERLAY` makes the contextual action mode cover the support action bar. It must be requested before the content is set. supportRequestWindowFeature(Window.FEATURE_ACTION_MODE_OVERLAY); // Set the content view. - setContentView(R.layout.bookmarks_databaseview_coordinatorlayout_top_appbar); + setContentView(R.layout.bookmarks_databaseview_top_appbar); } // Get a handle for the toolbar. @@ -153,11 +151,23 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements 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); + // Control what the system back command does. + OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) { + @Override + public void handleOnBackPressed() { + // Prepare to finish the activity. + prepareFinish(); + } + }; + + // Register the on back pressed callback. + getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback); + + // Initialize the database handler. + bookmarksDatabaseHelper = new BookmarksDatabaseHelper(this); // Setup a matrix cursor for "All Folders" and "Home Folder". - String[] matrixCursorColumnNames = {BookmarksDatabaseHelper._ID, BookmarksDatabaseHelper.BOOKMARK_NAME}; + String[] matrixCursorColumnNames = {BookmarksDatabaseHelper.ID, BookmarksDatabaseHelper.BOOKMARK_NAME}; MatrixCursor matrixCursor = new MatrixCursor(matrixCursorColumnNames); matrixCursor.addRow(new Object[]{ALL_FOLDERS_DATABASE_ID, getString(R.string.all_folders)}); matrixCursor.addRow(new Object[]{HOME_FOLDER_DATABASE_ID, getString(R.string.home_folder)}); @@ -203,7 +213,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Get the folder icon byte array from the cursor. - byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON)); + byte[] folderIconByteArray = cursor.getBlob(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.FAVORITE_ICON)); // Convert the byte array to a bitmap beginning at the first byte and ending at the last. Bitmap folderIconBitmap = BitmapFactory.decodeByteArray(folderIconByteArray, 0, folderIconByteArray.length); @@ -223,7 +233,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements } // Set the text view to display the folder name. - spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME))); + spinnerItemTextView.setText(cursor.getString(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.BOOKMARK_NAME))); } }; @@ -293,15 +303,15 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements @Override public void bindView(View view, Context context, Cursor cursor) { - boolean isFolder = (cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1); + boolean isFolder = (cursor.getInt(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.IS_FOLDER)) == 1); // Get the database ID from the `Cursor` and display it in `bookmarkDatabaseIdTextView`. - int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper._ID)); + int bookmarkDatabaseId = cursor.getInt(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.ID)); TextView bookmarkDatabaseIdTextView = view.findViewById(R.id.bookmarks_databaseview_database_id); bookmarkDatabaseIdTextView.setText(String.valueOf(bookmarkDatabaseId)); // Get the favorite icon byte array from the `Cursor`. - byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON)); + byte[] favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.FAVORITE_ICON)); // Convert the byte array to a `Bitmap` beginning at the beginning at the first byte and ending at the last. Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length); // Display the bitmap in `bookmarkFavoriteIcon`. @@ -309,7 +319,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap); // Get the bookmark name from the `Cursor` and display it in `bookmarkNameTextView`. - String bookmarkNameString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); + String bookmarkNameString = cursor.getString(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.BOOKMARK_NAME)); TextView bookmarkNameTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_name); bookmarkNameTextView.setText(bookmarkNameString); @@ -322,7 +332,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements } // Get the bookmark URL form the `Cursor` and display it in `bookmarkUrlTextView`. - String bookmarkUrlString = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)); + String bookmarkUrlString = cursor.getString(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.BOOKMARK_URL)); TextView bookmarkUrlTextView = view.findViewById(R.id.bookmarks_databaseview_bookmark_url); bookmarkUrlTextView.setText(bookmarkUrlString); @@ -334,12 +344,12 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements } // Get the display order from the `Cursor` and display it in `bookmarkDisplayOrderTextView`. - int bookmarkDisplayOrder = cursor.getInt(cursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)); + int bookmarkDisplayOrder = cursor.getInt(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.DISPLAY_ORDER)); TextView bookmarkDisplayOrderTextView = view.findViewById(R.id.bookmarks_databaseview_display_order); bookmarkDisplayOrderTextView.setText(String.valueOf(bookmarkDisplayOrder)); // Get the parent folder from the `Cursor` and display it in `bookmarkParentFolder`. - String bookmarkParentFolder = cursor.getString(cursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER)); + String bookmarkParentFolder = cursor.getString(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.PARENT_FOLDER)); ImageView parentFolderImageView = view.findViewById(R.id.bookmarks_databaseview_parent_folder_icon); TextView bookmarkParentFolderTextView = view.findViewById(R.id.bookmarks_databaseview_parent_folder); @@ -377,7 +387,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Show the edit bookmark or edit bookmark folder dialog. if (bookmarksDatabaseHelper.isFolder(databaseId)) { // Save the current folder name, which is used in `onSaveBookmarkFolder()`. - oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)); + oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.BOOKMARK_NAME)); // Show the edit bookmark folder dialog. DialogFragment editBookmarkFolderDatabaseViewDialog = EditBookmarkFolderDatabaseViewDialog.folderDatabaseId(databaseId, favoriteIconBitmap); @@ -472,7 +482,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Get the position of the folder in the bookmarks cursor. while ((folderPosition < 0) && (bookmarksCursor.getPosition() < bookmarksCursor.getCount())) { // Check if the folder database ID matches the bookmark database ID. - if (folderDatabaseId == bookmarksCursor.getInt((bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper._ID)))) { + if (folderDatabaseId == bookmarksCursor.getInt(bookmarksCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.ID))) { // Get the folder position. folderPosition = bookmarksCursor.getPosition(); @@ -588,7 +598,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Close the activity if back has been pressed. if (closeActivityAfterDismissingSnackbar) { - onBackPressed(); + finish(); } } }); @@ -613,19 +623,12 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Inflate the menu. getMenuInflater().inflate(R.menu.bookmarks_databaseview_options_menu, menu); - // Get the current theme status. - int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; - // Get a handle for the sort menu item. MenuItem sortMenuItem = menu.findItem(R.id.sort); // Change the sort menu item icon if the listview is sorted by display order, which restores the state after a restart. if (sortByDisplayOrder) { - if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { - sortMenuItem.setIcon(R.drawable.sort_selected_day); - } else { - sortMenuItem.setIcon(R.drawable.sort_selected_night); - } + sortMenuItem.setIcon(R.drawable.sort_selected); } // Success. @@ -639,8 +642,8 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Run the command that corresponds to the selected menu item. if (menuItemId == android.R.id.home) { // Go Home. The home arrow is identified as `android.R.id.home`, not just `R.id.home`. - // Exit the activity. - onBackPressed(); + // Prepare to finish the activity. + prepareFinish(); } else if (menuItemId == R.id.sort) { // Toggle the sort mode. // Update the sort by display order tracker. sortByDisplayOrder = !sortByDisplayOrder; @@ -648,27 +651,16 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Get a handle for the bookmarks list view. ListView bookmarksListView = findViewById(R.id.bookmarks_databaseview_listview); - // Get the current theme status. - int currentThemeStatus = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; - // Update the icon and display a snackbar. if (sortByDisplayOrder) { // Sort by display order. - // Update the icon according to the theme. - if (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { - menuItem.setIcon(R.drawable.sort_selected_day); - } else { - menuItem.setIcon(R.drawable.sort_selected_night); - } + // Update the icon. + menuItem.setIcon(R.drawable.sort_selected); // Display a Snackbar indicating the current sort type. 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 (currentThemeStatus == Configuration.UI_MODE_NIGHT_NO) { - menuItem.setIcon(R.drawable.sort_day); - } else { - menuItem.setIcon(R.drawable.sort_night); - } + // Update the icon. + menuItem.setIcon(R.drawable.sort); // Display a Snackbar indicating the current sort type. Snackbar.make(bookmarksListView, R.string.sorted_by_database_id, Snackbar.LENGTH_SHORT).show(); @@ -693,8 +685,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements savedInstanceState.putBoolean(SORT_BY_DISPLAY_ORDER, sortByDisplayOrder); } - @Override - public void onBackPressed() { + private void prepareFinish() { // Check to see if a snackbar is currently displayed. If so, it must be closed before existing so that a pending delete is completed before reloading the list view in the bookmarks activity. if ((bookmarksDeletedSnackbar != null) && bookmarksDeletedSnackbar.isShown()) { // Close the bookmarks deleted snackbar before going home. // Set the close flag. @@ -716,7 +707,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements BookmarksActivity.restartFromBookmarksDatabaseViewActivity = true; // Exit the bookmarks database view activity. - super.onBackPressed(); + finish(); } } @@ -772,7 +763,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements while (folderCursor.getPosition() < folderCursor.getCount()) { // Get the bookmark database ID. - int bookmarkId = folderCursor.getInt(folderCursor.getColumnIndex(BookmarksDatabaseHelper._ID)); + int bookmarkId = folderCursor.getInt(folderCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.ID)); // Move the bookmarks cursor to the first position. bookmarksCursor.moveToFirst(); @@ -783,7 +774,7 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Get the position of this bookmark in the bookmarks cursor. while ((bookmarkPosition < 0) && (bookmarksCursor.getPosition() < bookmarksCursor.getCount())) { // Check if the bookmark IDs match. - if (bookmarkId == bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper._ID))) { + if (bookmarkId == bookmarksCursor.getInt(bookmarksCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.ID))) { // Get the bookmark position. bookmarkPosition = bookmarksCursor.getPosition(); @@ -936,4 +927,4 @@ public class BookmarksDatabaseViewActivity extends AppCompatActivity implements // Run the default commands. super.onDestroy(); } -} \ No newline at end of file +}