X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.kt;h=ac27dcd657c4315e254ab1db442094f8963f0e1e;hb=f8486dc082d82deead08a6747670e22ce7263c97;hp=c6d91350ea3a2116eca251efa663e3bbd276749f;hpb=a54a66c7d169d2edf55ba560ec2d951e709188e6;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 c6d91350..ac27dcd6 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt @@ -104,6 +104,8 @@ import androidx.cursoradapter.widget.CursorAdapter import androidx.drawerlayout.widget.DrawerLayout import androidx.fragment.app.DialogFragment import androidx.preference.PreferenceManager +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import androidx.viewpager2.widget.ViewPager2 import androidx.webkit.WebSettingsCompat @@ -280,6 +282,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook private lateinit var navigationForwardMenuItem: MenuItem private lateinit var navigationHistoryMenuItem: MenuItem private lateinit var navigationRequestsMenuItem: MenuItem + private lateinit var navigationView: NavigationView private lateinit var optionsAddOrEditDomainMenuItem: MenuItem private lateinit var optionsBlockAllThirdPartyRequestsMenuItem: MenuItem private lateinit var optionsClearCookiesMenuItem: MenuItem @@ -375,11 +378,13 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook private var inFullScreenBrowsingMode = false private var incognitoModeEnabled = false private var loadingNewIntent = false + private var navigationDrawerFirstView = true private var objectAnimator = ObjectAnimator() private var optionsMenu: Menu? = null 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 @@ -588,7 +593,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook tabLayout = findViewById(R.id.tablayout) swipeRefreshLayout = findViewById(R.id.swiperefreshlayout) webViewViewPager2 = findViewById(R.id.webview_viewpager2) - val navigationView = findViewById(R.id.navigationview) + navigationView = findViewById(R.id.navigationview) bookmarksListView = findViewById(R.id.bookmarks_drawer_listview) bookmarksTitleTextView = findViewById(R.id.bookmarks_title_textview) bookmarksDrawerPinnedImageView = findViewById(R.id.bookmarks_drawer_pinned_imageview) @@ -1007,17 +1012,23 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Run the default commands. super.onConfigurationChanged(newConfig) + // Reset the navigation drawer first view flag. + navigationDrawerFirstView = true + // Get the current page. val currentPage = webViewViewPager2.currentItem // Toggle the pages if there is more than one so that the view pager will recalculate their size. if (currentPage > 0) { - // Switch to the previous page. - webViewViewPager2.currentItem = (currentPage - 1) + // Switch to the previous page after 25 milliseconds. + webViewViewPager2.postDelayed ({ webViewViewPager2.currentItem = (currentPage - 1) }, 25) - // Switch back to the current page after the view pager has quiesced. - webViewViewPager2.post { webViewViewPager2.currentItem = currentPage } + // Switch back to the current page after the view pager has quiesced (which we are deciding should be 25 milliseconds). + webViewViewPager2.postDelayed ({ webViewViewPager2.currentItem = currentPage }, 25) } + + // Scroll to the current tab position after 25 milliseconds. + tabLayout.postDelayed ({ tabLayout.setScrollPosition(currentPage, 0F, false, false) }, 25) } override fun onCreateOptionsMenu(menu: Menu): Boolean { @@ -2792,8 +2803,11 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Set a custom view on the new tab. newTab.setCustomView(R.layout.tab_custom_view) + // Scroll to the new tab position. + tabLayout.post { tabLayout.setScrollPosition(newTabNumber, 0F, false, false) } + // Add the new WebView page. - webViewStateAdapter!!.addPage(newTabNumber, webViewViewPager2, urlString, moveToTab) + webViewStateAdapter!!.addPage(newTabNumber, newTab, urlString, moveToTab) // Show the app bar if it is at the bottom of the screen and the new tab is taking focus. if (bottomAppBar && moveToTab && appBarLayout.translationY != 0f) { @@ -3992,7 +4006,10 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook if ((savedStateArrayList == null) || (savedStateArrayList!!.size == 0)) { // The activity has not been restarted or it was restarted on start to change the theme. // Add the first tab. addNewTab("", false) - } else { // The activity has been restarted. + } 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. @@ -4017,12 +4034,14 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Set the first page as the current WebView. setCurrentWebView(0) } else { // The first tab is not selected. - // Switch to the page before the saved tab position. - webViewViewPager2.post { webViewViewPager2.currentItem = (savedTabPosition - 1) } + // Select the tab when the layout has finished populating. + tabLayout.post { + // Get a handle for the tab. + val tab = tabLayout.getTabAt(savedTabPosition)!! - // Switch to the saved tab position. - // This has to be done twice because, for some reason, if the above step is skipped there is some race condition where nothing happens and the first page is displayed. - webViewViewPager2.post { webViewViewPager2.currentItem = savedTabPosition } + // Select the tab. + tab.select() + } } // Get the intent that started the app. @@ -4182,44 +4201,37 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook val createBookmarkFolderFab = findViewById(R.id.create_bookmark_folder_fab) val createBookmarkFab = findViewById(R.id.create_bookmark_fab) - // Update the WebView pager every time a tab is modified. - webViewViewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { - override fun onPageSelected(position: Int) { - // Close the find on page bar if it is open. - closeFindOnPage(null) - - // Set the current WebView. - setCurrentWebView(position) - - // Select the corresponding tab if it does not match the currently selected page. This will happen if the page was scrolled by creating a new tab. - if (tabLayout.selectedTabPosition != position) { - // Wait until the new tab has been created. - tabLayout.post { - // Get a handle for the tab. - val tab = tabLayout.getTabAt(position)!! - - // Select the tab. - tab.select() - } - } - } - }) - // Handle tab selections. tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { override fun onTabSelected(tab: TabLayout.Tab) { + // Close the find on page bar if it is open. + closeFindOnPage(null) + // Select the same page in the view pager. webViewViewPager2.currentItem = tab.position + + // Set the current WebView. + setCurrentWebView(tab.position) } override fun onTabUnselected(tab: TabLayout.Tab) {} override fun onTabReselected(tab: TabLayout.Tab) { - // 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)) + // 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 1 second since the last restart as deep restarts sometimes end up selecting a tab twice. + if (millisecondsSinceLastRestart > 1000) { + // 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)) + } + } } }) @@ -4439,6 +4451,20 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Clear the focus from from the WebView if it is not null, which can happen if a user opens a drawer while the browser is being resumed. // Clearing the focus from the WebView removes any text selection markers and context menus, which otherwise draw above the open drawers. currentWebView?.clearFocus() + + if (bottomAppBar && navigationDrawerFirstView) { + // Reset the navigation drawer first view flag. + navigationDrawerFirstView = false + + // Get a handle for the navigation recycler view. + val navigationRecyclerView = navigationView.getChildAt(0) as RecyclerView + + // Get the navigation linear layout manager. + val navigationLinearLayoutManager = navigationRecyclerView.layoutManager as LinearLayoutManager + + // Scroll the navigation drawer to the bottom. + navigationLinearLayoutManager.scrollToPositionWithOffset(13, 0) + } } } }) @@ -5946,16 +5972,16 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Stop the swipe to refresh indicator if it is running swipeRefreshLayout.isRefreshing = false - // Get the WebView tab fragment. - val webViewTabFragment = webViewStateAdapter!!.getPageFragment(pageNumber) + // Try to set the current WebView. This will fail if the WebView has not yet been populated. + try { + // Get the WebView tab fragment. + val webViewTabFragment = webViewStateAdapter!!.getPageFragment(pageNumber) - // Get the fragment view. - val webViewFragmentView = webViewTabFragment.view + // Get the fragment view. + val webViewFragmentView = webViewTabFragment.view - // Set the current WebView if the fragment view is not null. - if (webViewFragmentView != null) { // The fragment has been populated. // Store the current WebView. - currentWebView = webViewFragmentView.findViewById(R.id.nestedscroll_webview) + currentWebView = webViewFragmentView!!.findViewById(R.id.nestedscroll_webview) // Update the status of swipe to refresh. if (currentWebView!!.swipeToRefresh) { // Swipe to refresh is enabled. @@ -6015,8 +6041,8 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Remove any background on the URL relative layout. urlRelativeLayout.background = AppCompatResources.getDrawable(this, R.color.transparent) } - } else if ((pageNumber == savedTabPosition) || (pageNumber >= (webViewStateAdapter!!.itemCount - 1))) { // The tab has not been populated yet. - // Try again in 100 milliseconds if the app is being restored or the a new tab has been added (the last tab). + } catch (exception: Exception) { + // Try again in 100 milliseconds if the WebView has not yet been populated. // Create a handler to set the current WebView. val setCurrentWebViewHandler = Handler(Looper.getMainLooper()) @@ -6026,8 +6052,8 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook setCurrentWebView(pageNumber) } - // Try setting the current WebView again after 100 milliseconds. - setCurrentWebViewHandler.postDelayed(setCurrentWebWebRunnable, 100) + // Try setting the current WebView again after 50 milliseconds. + setCurrentWebViewHandler.postDelayed(setCurrentWebWebRunnable, 50) } }