X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;ds=sidebyside;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.kt;h=75aa38beaf7139c878410fd0a05f80a4b09fe3e6;hb=74e8a8368db991ee511c216a298c4ff5201f41de;hp=204023e332e89df7fc8e47780877d43ebf22903f;hpb=e065315a36c804626a7dba38d3edad05e9fdb473;p=PrivacyBrowserAndroid.git diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt index 204023e3..75aa38be 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt @@ -250,6 +250,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Declare the public static variables. lateinit var appBarLayout: AppBarLayout + lateinit var defaultFavoriteIconBitmap : Bitmap } // Declare the class variables. @@ -656,6 +657,15 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // 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() @@ -677,34 +687,8 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook 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(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) @@ -2237,66 +2221,16 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook 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(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(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) } } @@ -3171,10 +3105,10 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook 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) @@ -3196,7 +3130,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook nestedScrollWebView.previousWebpageTitle = tabTitleTextView.text.toString() // Set the default favorite icon as the favorite icon for this tab. - tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(nestedScrollWebView.getFavoriteIcon(), 64, 64, true)) + tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(nestedScrollWebView.getFavoriteIcon(), 128, 128, true)) // Set the loading title text. tabTitleTextView.setText(R.string.loading) @@ -3951,17 +3885,32 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook } } - override fun createBookmark(dialogFragment: DialogFragment, favoriteIconBitmap: Bitmap) { + override fun createBookmark(dialogFragment: DialogFragment) { // Get the dialog. val dialog = dialogFragment.dialog!! // Get the views from the dialog fragment. - val createBookmarkNameEditText = dialog.findViewById(R.id.create_bookmark_name_edittext) - val createBookmarkUrlEditText = dialog.findViewById(R.id.create_bookmark_url_edittext) + val webpageFavoriteIconRadioButton = dialog.findViewById(R.id.webpage_favorite_icon_radiobutton) + val webpageFavoriteIconImageView = dialog.findViewById(R.id.webpage_favorite_icon_imageview) + val customIconImageView = dialog.findViewById(R.id.custom_icon_imageview) + val bookmarkNameEditText = dialog.findViewById(R.id.bookmark_name_edittext) + val bookmarkUrlEditText = dialog.findViewById(R.id.bookmark_url_edittext) // Extract the strings from the edit texts. - val bookmarkNameString = createBookmarkNameEditText.text.toString() - val bookmarkUrlString = createBookmarkUrlEditText.text.toString() + val bookmarkNameString = bookmarkNameEditText.text.toString() + val bookmarkUrlString = bookmarkUrlEditText.text.toString() + + // Get the selected favorite icon drawable. + val favoriteIconDrawable = if (webpageFavoriteIconRadioButton.isChecked) // Use the webpage favorite icon. + webpageFavoriteIconImageView.drawable + else // Use the custom icon. + customIconImageView.drawable + + // Cast the favorite icon bitmap to a bitmap drawable + val favoriteIconBitmapDrawable = favoriteIconDrawable as BitmapDrawable + + // Convert the favorite icon bitmap drawable to a bitmap. + val favoriteIconBitmap = favoriteIconBitmapDrawable.bitmap // Create a favorite icon byte array output stream. val favoriteIconByteArrayOutputStream = ByteArrayOutputStream() @@ -3988,32 +3937,34 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook bookmarksListView.setSelection(newBookmarkDisplayOrder) } - override fun createBookmarkFolder(dialogFragment: DialogFragment, favoriteIconBitmap: Bitmap) { + override fun createBookmarkFolder(dialogFragment: DialogFragment) { // Get the dialog. val dialog = dialogFragment.dialog!! // Get handles for the views in the dialog fragment. + val defaultFolderIconRadioButton = dialog.findViewById(R.id.default_folder_icon_radiobutton) + val defaultFolderIconImageView = dialog.findViewById(R.id.default_folder_icon_imageview) + val webpageFavoriteIconRadioButton = dialog.findViewById(R.id.webpage_favorite_icon_radiobutton) + val webpageFavoriteIconImageView = dialog.findViewById(R.id.webpage_favorite_icon_imageview) + val customIconImageView = dialog.findViewById(R.id.custom_icon_imageview) val folderNameEditText = dialog.findViewById(R.id.folder_name_edittext) - val defaultIconRadioButton = dialog.findViewById(R.id.default_icon_radiobutton) - val defaultIconImageView = dialog.findViewById(R.id.default_icon_imageview) // Get new folder name string. val folderNameString = folderNameEditText.text.toString() // Set the folder icon bitmap according to the dialog. - val folderIconBitmap: Bitmap = if (defaultIconRadioButton.isChecked) { // Use the default folder icon. - // Get the default folder icon drawable. - val folderIconDrawable = defaultIconImageView.drawable - - // Convert the folder icon drawable to a bitmap drawable. - val folderIconBitmapDrawable = folderIconDrawable as BitmapDrawable - - // Convert the folder icon bitmap drawable to a bitmap. - folderIconBitmapDrawable.bitmap - } else { // Use the WebView favorite icon. - // Copy the favorite icon bitmap to the folder icon bitmap. - favoriteIconBitmap - } + val folderIconDrawable = if (defaultFolderIconRadioButton.isChecked) // Use the default folder icon. + defaultFolderIconImageView.drawable + else if (webpageFavoriteIconRadioButton.isChecked) // Use the webpage favorite icon. + webpageFavoriteIconImageView.drawable + else // Use the custom icon. + customIconImageView.drawable + + // Cast the folder icon bitmap to a bitmap drawable. + val folderIconBitmapDrawable = folderIconDrawable as BitmapDrawable + + // Convert the folder icon bitmap drawable to a bitmap. + val folderIconBitmap = folderIconBitmapDrawable.bitmap // Create a folder icon byte array output stream. val folderIconByteArrayOutputStream = ByteArrayOutputStream() @@ -4973,7 +4924,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook val tabFavoriteIconImageView = tabView.findViewById(R.id.favorite_icon_imageview) // Display the favorite icon in the tab. - tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(icon, 64, 64, true)) + tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(icon, 128, 128, true)) } } } @@ -5504,8 +5455,14 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Instantiate an HTTP authentication dialog. val httpAuthenticationDialogFragment = HttpAuthenticationDialog.displayDialog(host, realm, nestedScrollWebView.webViewFragmentId) - // Show the HTTP authentication dialog. - httpAuthenticationDialogFragment.show(supportFragmentManager, getString(R.string.http_authentication)) + // Try to show the dialog. WebView can receive an HTTP authentication request even after the app has been paused. Attempting to display a dialog in that state leads to a crash. + try { + // Show the HTTP authentication dialog. + httpAuthenticationDialogFragment.show(supportFragmentManager, getString(R.string.http_authentication)) + } catch (exception: Exception) { // The dialog could not be shown. + // Add the dialog to the pending dialog array list. It will be displayed in `onStart()`. + pendingDialogsArrayList.add(PendingDialogDataClass(httpAuthenticationDialogFragment, getString(R.string.http_authentication))) + } } override fun onPageStarted(webView: WebView, url: String, favicon: Bitmap?) { @@ -5743,7 +5700,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Instantiate an SSL certificate error alert dialog. val sslCertificateErrorDialogFragment = SslCertificateErrorDialog.displayDialog(error, nestedScrollWebView.webViewFragmentId) - // Try to show the dialog. The SSL error handler continues to function even when the WebView is paused. Attempting to display a dialog in that state leads to a crash. + // Try to show the dialog. The SSL error handler continues to function even when the app has been stopped. Attempting to display a dialog in that state leads to a crash. try { // Show the SSL certificate error dialog. sslCertificateErrorDialogFragment.show(supportFragmentManager, getString(R.string.ssl_certificate_error)) @@ -5942,15 +5899,56 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook 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(R.id.favorite_icon_imageview) + + // Store the previous favorite icon. + if (previousFavoriteIcon == null) + currentWebView!!.setFavoriteIcon(defaultFavoriteIconBitmap) + else + currentWebView!!.setFavoriteIcon(previousFavoriteIcon) + + // Display the previous favorite icon in the tab. + tabFavoriteIconImageView.setImageBitmap(Bitmap.createScaledBitmap(currentWebView!!.getFavoriteIcon(), 128, 128, 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) { @@ -6055,20 +6053,8 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook } 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 { @@ -6356,22 +6342,4 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook 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) - } }