X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.kt;h=2409b18b1b744c3a218f45f5af56aee1c91ecf77;hb=777238422f9fce45de540f2f9b1647548200addf;hp=2c7560d6c459a90370f0bed7f6f77a2fdefa81ec;hpb=a9d00f526a6fc67f97d479188fae66dd7f0a6dde;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 2c7560d6..2409b18b 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt @@ -283,6 +283,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook private lateinit var navigationForwardMenuItem: MenuItem private lateinit var navigationHistoryMenuItem: MenuItem private lateinit var navigationRequestsMenuItem: MenuItem + private lateinit var navigationScrollToBottomMenuItem: MenuItem private lateinit var navigationView: NavigationView private lateinit var optionsAddOrEditDomainMenuItem: MenuItem private lateinit var optionsBlockAllThirdPartyRequestsMenuItem: MenuItem @@ -385,7 +386,6 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook private var orbotStatusBroadcastReceiver: BroadcastReceiver? = null private var reapplyAppSettingsOnRestart = false private var reapplyDomainSettingsOnRestart = false - private var restartTime = Date(0) private var sanitizeAmpRedirects = false private var sanitizeTrackingQueries = false private var savedProxyMode: String? = null @@ -606,6 +606,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Get handles for the navigation menu items. navigationBackMenuItem = navigationMenu.findItem(R.id.back) navigationForwardMenuItem = navigationMenu.findItem(R.id.forward) + navigationScrollToBottomMenuItem = navigationMenu.findItem(R.id.scroll_to_bottom) navigationHistoryMenuItem = navigationMenu.findItem(R.id.history) navigationRequestsMenuItem = navigationMenu.findItem(R.id.requests) @@ -783,11 +784,6 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook } } } else { // The app has been restarted. - // If the new intent will open a new tab, set the saved tab position to be the size of the saved state array list. - // The tab position is 0 based, meaning the new tab will be the tab position that is restored. - if ((intentUriData != null) || (intentStringExtra != null) || isWebSearch) - savedTabPosition = savedStateArrayList!!.size - // Replace the intent that started the app with this one. This will load the tab after the others have been restored. setIntent(intent) } @@ -2272,6 +2268,15 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook } } + R.id.scroll_to_bottom -> { // Scroll to Bottom. + // Check if the WebView is scrolled to the top. + if (currentWebView!!.scrollY == 0) { // The WebView is at the top; scroll to the bottom. Using a large Y number is more efficient than trying to calculate the exact WebView length. + currentWebView!!.scrollTo(0, 1_000_000_000) + } else { // The WebView is not at the top; scroll to the top. + currentWebView!!.scrollTo(0, 0) + } + } + R.id.history -> { // History. // Instantiate the URL history dialog. val urlHistoryDialogFragment: DialogFragment = UrlHistoryDialog.loadBackForwardList(currentWebView!!.webViewFragmentId) @@ -4003,9 +4008,6 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Add the first tab. addNewTab("", false) } else { // The activity has been restarted with a saved state. - // Set the current restart time. - restartTime = Date() - // Restore each tab. for (i in savedStateArrayList!!.indices) { // Add a new tab. @@ -4025,21 +4027,6 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook savedStateArrayList = null savedNestedScrollWebViewStateArrayList = null - // Restore the selected tab position. - if (savedTabPosition == 0) { // The first tab is selected. - // Set the first page as the current WebView. - setCurrentWebView(0) - } else { // The first tab is not selected. - // Select the tab when the layout has finished populating. - tabLayout.post { - // Get a handle for the tab. - val tab = tabLayout.getTabAt(savedTabPosition)!! - - // Select the tab. - tab.select() - } - } - // Get the intent that started the app. val intent = intent @@ -4055,7 +4042,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook val isWebSearch = (intentAction != null) && (intentAction == Intent.ACTION_WEB_SEARCH) // Only process the URI if it contains data or it is a web search. If the user pressed the desktop icon after the app was already running the URI will be null. - if ((intentUriData != null) || (intentStringExtra != null) || isWebSearch) { + if ((intentUriData != null) || (intentStringExtra != null) || isWebSearch) { // A new tab is being loaded. // Get the URL string. val urlString = if (isWebSearch) { // The intent is a web search. // Sanitize the search input. @@ -4083,6 +4070,21 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Make it so. loadUrl(currentWebView!!, urlString) } + } else { // A new tab is not being loaded. + // Restore the selected tab position. + if (savedTabPosition == 0) { // The first tab is selected. + // Set the first page as the current WebView. + setCurrentWebView(0) + } else { // The first tab is not selected. + // Select the tab when the layout has finished populating. + tabLayout.post { + // Get a handle for the tab. + val tab = tabLayout.getTabAt(savedTabPosition)!! + + // Select the tab. + tab.select() + } + } } } } @@ -4219,17 +4221,11 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Only display the view SSL certificate dialog if the current WebView is not null. // This can happen if the tab is programmatically reselected while the app is being restarted and is not yet populated. if (currentWebView != null) { - // Calculate the milliseconds since the last restart. This can be replaced by the simpler LocalDateTime once the minimum API >= 26. - val millisecondsSinceLastRestart = Date().time - restartTime.time - - // Only display the SSL certificate dialog if it has been at least 3 seconds since the last restart as deep restarts sometimes end up selecting a tab twice. - if (millisecondsSinceLastRestart > 3000) { - // Instantiate the View SSL Certificate dialog. - val viewSslCertificateDialogFragment: DialogFragment = ViewSslCertificateDialog.displayDialog(currentWebView!!.webViewFragmentId, currentWebView!!.getFavoriteIcon()) + // Instantiate the View SSL Certificate dialog. + val viewSslCertificateDialogFragment: DialogFragment = ViewSslCertificateDialog.displayDialog(currentWebView!!.webViewFragmentId, currentWebView!!.getFavoriteIcon()) - // Display the View SSL Certificate dialog. - viewSslCertificateDialogFragment.show(supportFragmentManager, getString(R.string.view_ssl_certificate)) - } + // Display the View SSL Certificate dialog. + viewSslCertificateDialogFragment.show(supportFragmentManager, getString(R.string.view_ssl_certificate)) } } }) @@ -4435,9 +4431,28 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook if (newState == DrawerLayout.STATE_SETTLING || newState == DrawerLayout.STATE_DRAGGING) { // A drawer is opening or closing. // Update the navigation menu items if the WebView is not null. if (currentWebView != null) { + // Set the enabled status of the menu items. navigationBackMenuItem.isEnabled = currentWebView!!.canGoBack() navigationForwardMenuItem.isEnabled = currentWebView!!.canGoForward() + navigationScrollToBottomMenuItem.isEnabled = (currentWebView!!.canScrollVertically(-1) || currentWebView!!.canScrollVertically(1)) navigationHistoryMenuItem.isEnabled = currentWebView!!.canGoBack() || currentWebView!!.canGoForward() + + // Update the scroll menu item. + if (currentWebView!!.scrollY == 0) { // The WebView is scrolled to the top. + // Set the title. + navigationScrollToBottomMenuItem.title = getString(R.string.scroll_to_bottom) + + // Set the icon. + navigationScrollToBottomMenuItem.icon = AppCompatResources.getDrawable(applicationContext, R.drawable.move_down_enabled) + } else { // The WebView is not scrolled to the top. + // Set the title. + navigationScrollToBottomMenuItem.title = getString(R.string.scroll_to_top) + + // Set the icon. + navigationScrollToBottomMenuItem.icon = AppCompatResources.getDrawable(applicationContext, R.drawable.move_up_enabled) + } + + // Display the number of blocked requests. navigationRequestsMenuItem.title = getString(R.string.requests) + " - " + currentWebView!!.getRequestsCount(BLOCKED_REQUESTS) // Hide the keyboard (if displayed). @@ -5659,10 +5674,6 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook } else { // Load the URL. loadUrl(nestedScrollWebView, urlToLoadString!!) } - - // Reset the intent. This prevents a duplicate tab from being created on a subsequent restart if loading an link from a new intent on restart. - // For example, this prevents a duplicate tab if a link is loaded from the Guide after changing the theme in the guide and then changing the theme again in the main activity. - intent = Intent() } else { // This is not the first tab. // Load the URL. loadUrl(nestedScrollWebView, urlString)