- // 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();
-
- // Create an intent to launch the bookmarks activity.
- Intent bookmarksIntent = new Intent(getApplicationContext(), BookmarksActivity.class);
-
- // Add the extra information to the intent.
- bookmarksIntent.putExtra("current_url", currentWebView.getUrl());
- bookmarksIntent.putExtra("current_title", currentWebView.getTitle());
- bookmarksIntent.putExtra("current_folder", currentBookmarksFolder);
- bookmarksIntent.putExtra("favorite_icon_byte_array", favoriteIconByteArray);
-
- // Make it so.
- startActivity(bookmarksIntent);
- });
-
- // Set the create new bookmark folder FAB to display an alert dialog.
- createBookmarkFolderFab.setOnClickListener(v -> {
- // Create a create bookmark folder dialog.
- DialogFragment createBookmarkFolderDialog = CreateBookmarkFolderDialog.createBookmarkFolder(currentWebView.getFavoriteOrDefaultIcon());
-
- // Show the create bookmark folder dialog.
- createBookmarkFolderDialog.show(getSupportFragmentManager(), getString(R.string.create_folder));
- });
-
- // Set the create new bookmark FAB to display an alert dialog.
- createBookmarkFab.setOnClickListener(view -> {
- // Instantiate the create bookmark dialog.
- DialogFragment createBookmarkDialog = CreateBookmarkDialog.createBookmark(currentWebView.getUrl(), currentWebView.getTitle(), currentWebView.getFavoriteOrDefaultIcon());
-
- // Display the create bookmark dialog.
- createBookmarkDialog.show(getSupportFragmentManager(), getString(R.string.create_bookmark));
- });
-
- // Search for the string on the page whenever a character changes in the `findOnPageEditText`.
- findOnPageEditText.addTextChangedListener(new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- // Do nothing.
- }
-
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- // Do nothing.
- }
-
- @Override
- public void afterTextChanged(Editable s) {
- // Search for the text in `mainWebView`.
- currentWebView.findAllAsync(findOnPageEditText.getText().toString());
- }
- });
-
- // Set the `check mark` button for the `findOnPageEditText` keyboard to close the soft keyboard.
- findOnPageEditText.setOnKeyListener((v, keyCode, event) -> {
- if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { // The `enter` key was pressed.
- // Hide the soft keyboard.
- inputMethodManager.hideSoftInputFromWindow(currentWebView.getWindowToken(), 0);
-
- // Consume the event.
- return true;
- } else { // A different key was pressed.
- // Do not consume the event.
- return false;
- }
- });
-
- // Implement swipe to refresh.
- swipeRefreshLayout.setOnRefreshListener(() -> currentWebView.reload());
-
- // The swipe to refresh circle doesn't always hide itself completely unless it is moved up 10 pixels.
- swipeRefreshLayout.setProgressViewOffset(false, swipeRefreshLayout.getProgressViewStartOffset() - 10, swipeRefreshLayout.getProgressViewEndOffset());
-
- // Set the swipe to refresh color according to the theme.
- if (darkTheme) {
- swipeRefreshLayout.setColorSchemeResources(R.color.blue_800);
- swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.gray_850);
- } else {
- swipeRefreshLayout.setColorSchemeResources(R.color.blue_500);
- }
-
- // `DrawerTitle` identifies the `DrawerLayouts` in accessibility mode.
- drawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.navigation_drawer));
- drawerLayout.setDrawerTitle(GravityCompat.END, getString(R.string.bookmarks));
-
- // Initialize the bookmarks database helper. 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);
-
- // Initialize `currentBookmarksFolder`. `""` is the home folder in the database.
- currentBookmarksFolder = "";
-
- // Load the home folder, which is `""` in the database.
- loadBookmarksFolder();
-
- bookmarksListView.setOnItemClickListener((parent, view, position, id) -> {
- // Convert the id from long to int to match the format of the bookmarks database.
- int databaseID = (int) id;
-
- // Get the bookmark cursor for this ID and move it to the first row.
- Cursor bookmarkCursor = bookmarksDatabaseHelper.getBookmark(databaseID);
- bookmarkCursor.moveToFirst();
-
- // Act upon the bookmark according to the type.
- if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { // The selected bookmark is a folder.
- // Store the new folder name in `currentBookmarksFolder`.
- currentBookmarksFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
-
- // Load the new folder.
- loadBookmarksFolder();
- } else { // The selected bookmark is not a folder.
- // Load the bookmark URL.
- loadUrl(bookmarkCursor.getString(bookmarkCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)));
-
- // Close the bookmarks drawer.
- drawerLayout.closeDrawer(GravityCompat.END);
- }
-
- // Close the `Cursor`.
- bookmarkCursor.close();
- });
-
- bookmarksListView.setOnItemLongClickListener((parent, view, position, id) -> {
- // Convert the database ID from `long` to `int`.
- int databaseId = (int) id;
-
- // Find out if the selected bookmark is a folder.
- boolean isFolder = bookmarksDatabaseHelper.isFolder(databaseId);
-
- if (isFolder) {
- // Save the current folder name, which is used in `onSaveEditBookmarkFolder()`.
- oldFolderNameString = bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME));
-
- // Show the edit bookmark folder `AlertDialog` and name the instance `@string/edit_folder`.
- DialogFragment editBookmarkFolderDialog = EditBookmarkFolderDialog.folderDatabaseId(databaseId, currentWebView.getFavoriteOrDefaultIcon());
- editBookmarkFolderDialog.show(getSupportFragmentManager(), getString(R.string.edit_folder));
- } else {
- // Show the edit bookmark `AlertDialog` and name the instance `@string/edit_bookmark`.
- DialogFragment editBookmarkDialog = EditBookmarkDialog.bookmarkDatabaseId(databaseId, currentWebView.getFavoriteOrDefaultIcon());
- editBookmarkDialog.show(getSupportFragmentManager(), getString(R.string.edit_bookmark));
- }
-
- // Consume the event.
- return true;
- });
-
- // Get the status bar pixel size.
- int statusBarResourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
- int statusBarPixelSize = getResources().getDimensionPixelSize(statusBarResourceId);
-
- // Get the resource density.
- float screenDensity = getResources().getDisplayMetrics().density;
-
- // Calculate the drawer header padding. This is used to move the text in the drawer headers below any cutouts.
- drawerHeaderPaddingLeftAndRight = (int) (15 * screenDensity);
- drawerHeaderPaddingTop = statusBarPixelSize + (int) (4 * screenDensity);
- drawerHeaderPaddingBottom = (int) (8 * screenDensity);
-
- // The drawer listener is used to update the navigation menu.`
- drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
- @Override
- public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
- }
-
- @Override
- public void onDrawerOpened(@NonNull View drawerView) {
- }
-
- @Override
- public void onDrawerClosed(@NonNull View drawerView) {
- }
-
- @Override
- public void onDrawerStateChanged(int newState) {
- if ((newState == DrawerLayout.STATE_SETTLING) || (newState == DrawerLayout.STATE_DRAGGING)) { // A drawer is opening or closing.
- // Get handles for the drawer headers.
- TextView navigationHeaderTextView = findViewById(R.id.navigationText);
- TextView bookmarksHeaderTextView = findViewById(R.id.bookmarks_title_textview);
-
- // Apply the navigation header paddings if the view is not null (sometimes it is null if another activity has already started). This moves the text in the header below any cutouts.
- if (navigationHeaderTextView != null) {
- navigationHeaderTextView.setPadding(drawerHeaderPaddingLeftAndRight, drawerHeaderPaddingTop, drawerHeaderPaddingLeftAndRight, drawerHeaderPaddingBottom);
- }
-
- // Apply the bookmarks header paddings if the view is not null (sometimes it is null if another activity has already started). This moves the text in the header below any cutouts.
- if (bookmarksHeaderTextView != null) {
- bookmarksHeaderTextView.setPadding(drawerHeaderPaddingLeftAndRight, drawerHeaderPaddingTop, drawerHeaderPaddingLeftAndRight, drawerHeaderPaddingBottom);