// Declare the public static variables.
lateinit var appBarLayout: AppBarLayout
+ lateinit var defaultFavoriteIconBitmap : Bitmap
}
// Declare the class variables.
// Update the bookmarks drawer pinned image view.
updateBookmarksDrawerPinnedImageView()
+ // Get the default favorite icon drawable.
+ val favoriteIconDrawable = AppCompatResources.getDrawable(this, R.drawable.world)
+
+ // Cast the favorite icon drawable to a bitmap drawable.
+ val favoriteIconBitmapDrawable = (favoriteIconDrawable as BitmapDrawable?)!!
+
+ // Store the default favorite icon bitmap.
+ defaultFavoriteIconBitmap = favoriteIconBitmapDrawable.bitmap
+
// Initialize the app.
initializeApp()
exitFullScreenVideo()
// It shouldn't be possible for the currentWebView to be null, but crash logs indicate it sometimes happens.
} else if (currentWebView != null && currentWebView!!.canGoBack()) { // There is at least one item in the current WebView history.
- // Get the current web back forward list.
- val webBackForwardList = currentWebView!!.copyBackForwardList()
-
- // Get the previous entry data.
- val previousUrl = webBackForwardList.getItemAtIndex(webBackForwardList.currentIndex - 1).url
- val previousFavoriteIcon = webBackForwardList.getItemAtIndex(webBackForwardList.currentIndex - 1).favicon
-
- // Apply the domain settings.
- applyDomainSettings(currentWebView!!, previousUrl, resetTab = false, reloadWebsite = false, loadUrl = false)
-
- // Get the current tab.
- val tab = tabLayout.getTabAt(tabLayout.selectedTabPosition)!!
-
- // Get the custom view from the tab.
- val tabView = tab.customView!!
-
- // Get the favorite icon image view from the tab.
- val tabFavoriteIconImageView = tabView.findViewById<ImageView>(R.id.favorite_icon_imageview)
-
- // Set the previous favorite icon if it isn't null.
- if (previousFavoriteIcon != null)
- tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(previousFavoriteIcon, 64, 64, true))
-
- // Go back.
- currentWebView!!.goBack()
-
- // Update the URL edit text after a delay.
- updateUrlEditTextAfterDelay()
+ // Navigate back one page.
+ navigateHistory(-1)
} else { // Close the current tab.
// A view is required because the method is also called by an XML `onClick`.
closeTab(null)
R.id.back -> { // Back.
// Check if the WebView can go back.
if (currentWebView!!.canGoBack()) {
- // Get the current web back forward list.
- val webBackForwardList = currentWebView!!.copyBackForwardList()
-
- // Get the previous entry data.
- val previousUrl = webBackForwardList.getItemAtIndex(webBackForwardList.currentIndex - 1).url
- val previousFavoriteIcon = webBackForwardList.getItemAtIndex(webBackForwardList.currentIndex - 1).favicon!!
-
- // Apply the domain settings.
- applyDomainSettings(currentWebView!!, previousUrl, resetTab = false, reloadWebsite = false, loadUrl = false)
-
- // Get the current tab.
- val tab = tabLayout.getTabAt(tabLayout.selectedTabPosition)!!
-
- // Get the custom view from the tab.
- val tabView = tab.customView!!
-
- // Get the favorite icon image view from the tab.
- val tabFavoriteIconImageView = tabView.findViewById<ImageView>(R.id.favorite_icon_imageview)
-
- // Set the previous favorite icon.
- tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(previousFavoriteIcon, 64, 64, true))
-
- // Load the previous website in the history.
- currentWebView!!.goBack()
-
- // Update the URL edit text after a delay.
- updateUrlEditTextAfterDelay()
+ // Navigate back one page.
+ navigateHistory(-1)
}
}
R.id.forward -> { // Forward.
// Check if the WebView can go forward.
if (currentWebView!!.canGoForward()) {
- // Get the current web back forward list.
- val webBackForwardList = currentWebView!!.copyBackForwardList()
-
- // Get the next entry data.
- val nextUrl = webBackForwardList.getItemAtIndex(webBackForwardList.currentIndex + 1).url
- val nextFavoriteIcon = webBackForwardList.getItemAtIndex(webBackForwardList.currentIndex + 1).favicon!!
-
- // Apply the domain settings.
- applyDomainSettings(currentWebView!!, nextUrl, resetTab = false, reloadWebsite = false, loadUrl = false)
-
- // Get the current tab.
- val tab = tabLayout.getTabAt(tabLayout.selectedTabPosition)!!
-
- // Get the custom view from the tab.
- val tabView = tab.customView!!
-
- // Get the favorite icon image view from the tab.
- val tabFavoriteIconImageView = tabView.findViewById<ImageView>(R.id.favorite_icon_imageview)
-
- // Set the next favorite icon.
- tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(nextFavoriteIcon, 64, 64, true))
-
- // Load the next website in the history.
- currentWebView!!.goForward()
-
- // Update the URL edit text after a delay.
- updateUrlEditTextAfterDelay()
+ // Navigate forward one page.
+ navigateHistory(+1)
}
}
nestedScrollWebView.clearPinnedSslCertificate()
nestedScrollWebView.pinnedIpAddresses = ""
- // Reset the favorite icon if specified.
+ // Reset the tab if specified.
if (resetTab) {
// Initialize the favorite icon.
- nestedScrollWebView.initializeFavoriteIcon()
+ nestedScrollWebView.resetFavoriteIcon()
// Get the current page position.
val currentPagePosition = webViewStateAdapter!!.getPositionForId(nestedScrollWebView.webViewFragmentId)
loadUrl(currentWebView!!, urlString)
}
- override fun navigateHistory(url: String, steps: Int) {
+ override fun navigateHistory(steps: Int) {
+ // Get the current web back forward list.
+ val webBackForwardList = currentWebView!!.copyBackForwardList()
+
+ // Calculate the target index.
+ val targetIndex = webBackForwardList.currentIndex + steps
+
+ // Get the previous entry data.
+ val previousUrl = webBackForwardList.getItemAtIndex(targetIndex).url
+ val previousFavoriteIcon = webBackForwardList.getItemAtIndex(targetIndex).favicon
+
// Apply the domain settings.
- applyDomainSettings(currentWebView!!, url, resetTab = false, reloadWebsite = false, loadUrl = false)
+ applyDomainSettings(currentWebView!!, previousUrl, resetTab = false, reloadWebsite = false, loadUrl = false)
+
+ // Get the current tab.
+ val tab = tabLayout.getTabAt(tabLayout.selectedTabPosition)!!
+
+ // Get the custom view from the tab.
+ val tabView = tab.customView!!
+
+ // Get the favorite icon image view from the tab.
+ val tabFavoriteIconImageView = tabView.findViewById<ImageView>(R.id.favorite_icon_imageview)
+
+ // Set the previous favorite icon.
+ if (previousFavoriteIcon == null)
+ tabFavoriteIconImageView.setImageBitmap(defaultFavoriteIconBitmap)
+ else
+ tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(previousFavoriteIcon, 64, 64, true))
// Load the history entry.
currentWebView!!.goBackOrForward(steps)
- // Update the URL edit text after a delay.
- updateUrlEditTextAfterDelay()
+ // Create a handler to update the URL edit box.
+ val urlEditTextUpdateHandler = Handler(Looper.getMainLooper())
+
+ // Create a runnable to update the URL edit box.
+ val urlEditTextUpdateRunnable = Runnable {
+ // Update the URL edit text.
+ urlEditText.setText(currentWebView!!.url)
+
+ // Disable the wide viewport if the source is being viewed.
+ if (currentWebView!!.url!!.startsWith("view-source:"))
+ currentWebView!!.settings.useWideViewPort = false
+ }
+
+ // Update the URL edit text after 50 milliseconds, so that the WebView has enough time to navigate to the new URL.
+ urlEditTextUpdateHandler.postDelayed(urlEditTextUpdateRunnable, 50)
}
override fun openFile(dialogFragment: DialogFragment) {
}
override fun pinnedErrorGoBack() {
- // Get the current web back forward list.
- val webBackForwardList = currentWebView!!.copyBackForwardList()
-
- // Get the previous entry URL.
- val previousUrl = webBackForwardList.getItemAtIndex(webBackForwardList.currentIndex - 1).url
-
- // Apply the domain settings.
- applyDomainSettings(currentWebView!!, previousUrl, resetTab = false, reloadWebsite = false, loadUrl = false)
-
- // Go back.
- currentWebView!!.goBack()
-
- // Update the URL edit text after a delay.
- updateUrlEditTextAfterDelay()
+ // Navigate back one page.
+ navigateHistory(-1)
}
private fun sanitizeUrl(urlString: String): String {
invalidateOptionsMenu()
}
}
-
- fun updateUrlEditTextAfterDelay() {
- // Create a handler to update the URL edit box.
- val urlEditTextUpdateHandler = Handler(Looper.getMainLooper())
-
- // Create a runnable to update the URL edit box.
- val urlEditTextUpdateRunnable = Runnable {
- // Update the URL edit text.
- urlEditText.setText(currentWebView!!.url)
-
- // Disable the wide viewport if the source is being viewed.
- if (currentWebView!!.url!!.startsWith("view-source:"))
- currentWebView!!.settings.useWideViewPort = false
- }
-
- // Update the URL edit text after 50 milliseconds, so that the WebView has enough time to navigate to the new URL.
- urlEditTextUpdateHandler.postDelayed(urlEditTextUpdateRunnable, 50)
- }
}
/*
- * Copyright 2016-2023 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2016-2024 Soren Stoutner <soren@stoutner.com>.
*
* This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
*
import android.widget.AdapterView
import android.widget.AdapterView.OnItemClickListener
import android.widget.ListView
-import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import com.stoutner.privacybrowser.views.NestedScrollWebView
// Define the class constants.
-private const val WEBVIEW_FRAGMENT_ID = "webview_fragment_id"
+private const val WEBVIEW_FRAGMENT_ID = "A"
class UrlHistoryDialog : DialogFragment() {
companion object {
// The public interface is used to send information back to the parent activity.
interface NavigateHistoryListener {
- fun navigateHistory(url: String, steps: Int)
+ fun navigateHistory(steps: Int)
}
override fun onAttach(context: Context) {
listView.adapter = historyArrayAdapter
// Listen for clicks on entries in the list view.
- listView.onItemClickListener = OnItemClickListener { _: AdapterView<*>?, view: View, _: Int, id: Long ->
+ listView.onItemClickListener = OnItemClickListener { _: AdapterView<*>?, _: View, _: Int, id: Long ->
// Convert the long ID to an int.
val itemId = id.toInt()
// Only consume the click if it is not on the current page ID.
if (itemId != currentPageId) {
- // Get a handle for the URL text view.
- val urlTextView = view.findViewById<TextView>(R.id.history_url_textview)
-
- // Get the URL.
- val url = urlTextView.text.toString()
-
- // Invoke the navigate history listener in the calling activity. These commands cannot be run here because they need access to `applyDomainSettings()`.
- navigateHistoryListener.navigateHistory(url, currentPageId - itemId)
+ // Invoke the navigate history listener in the calling activity. Those commands cannot be run here because they need access to `applyDomainSettings()`.
+ navigateHistoryListener.navigateHistory(currentPageId - itemId)
// Dismiss the alert dialog.
alertDialog.dismiss()