X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.kt;h=8fb1bc38900941cfceedfbcd400f160bb12f6138;hb=80219baeee5b43d8b71162781ee18bdde0509df1;hp=0dab90f19e458e0414bb153bf1eeaf182930867e;hpb=feca0fdd5129f23c694c00ef171d40c65f68ffce;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 0dab90f1..8fb1bc38 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt @@ -774,7 +774,7 @@ 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 at the new tab will be the tab position that is restored. + // 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 @@ -999,6 +999,23 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook super.onDestroy() } + override fun onConfigurationChanged(newConfig: Configuration) { + // Run the default commands. + super.onConfigurationChanged(newConfig) + + // 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 back to the current page after the view pager has quiesced. + webViewViewPager2.post { webViewViewPager2.currentItem = currentPage } + } + } + override fun onCreateOptionsMenu(menu: Menu): Boolean { // Inflate the menu. This adds items to the app bar if it is present. menuInflater.inflate(R.menu.webview_options_menu, menu) @@ -1058,9 +1075,6 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Disable the clear form data menu item if the API >= 26 so that the status of the main Clear Data is calculated correctly. optionsClearFormDataMenuItem.isEnabled = Build.VERSION.SDK_INT < 26 - // Only display the dark WebView menu item if the API >= 29. - optionsDarkWebViewMenuItem.isVisible = Build.VERSION.SDK_INT >= 29 - // Set the status of the additional app bar icons. Setting the refresh menu item to `SHOW_AS_ACTION_ALWAYS` makes it appear even on small devices like phones. if (displayAdditionalAppBarIcons) { // Display the additional icons. optionsRefreshMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS) @@ -1142,8 +1156,8 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Enable dark WebView if night mode is enabled. optionsDarkWebViewMenuItem.isEnabled = (currentThemeStatus == Configuration.UI_MODE_NIGHT_YES) - // Set the checkbox status for dark WebView if the device is running API >= 29 and algorithmic darkening is supported. - if ((Build.VERSION.SDK_INT >= 29) && WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) + // Set the checkbox status for dark WebView if algorithmic darkening is supported. + if (WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) optionsDarkWebViewMenuItem.isChecked = WebSettingsCompat.isAlgorithmicDarkeningAllowed(currentWebView!!.settings) } @@ -1901,7 +1915,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook R.id.dark_webview -> { // Dark WebView. // Toggle dark WebView if supported. - if ((Build.VERSION.SDK_INT >= 29) && WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) + if (WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) WebSettingsCompat.setAlgorithmicDarkeningAllowed(currentWebView!!.settings, !WebSettingsCompat.isAlgorithmicDarkeningAllowed(currentWebView!!.settings) ) @@ -3221,8 +3235,8 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook } } - // Set the WebView theme if device is running API >= 29 and algorithmic darkening is supported. - if ((Build.VERSION.SDK_INT >= 29) && WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) { + // Set the WebView theme if algorithmic darkening is supported. + if (WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) { // Set the WebView theme. when (webViewThemeInt) { // Set the WebView theme. @@ -3341,8 +3355,8 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook else -> nestedScrollWebView.settings.userAgentString = userAgentDataArray[userAgentArrayPosition] } - // Set the WebView theme if the device is running API >= 29 and algorithmic darkening is supported. - if ((Build.VERSION.SDK_INT >= 29) && WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) { + // Set the WebView theme if algorithmic darkening is supported. + if (WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) { // Set the WebView theme. when (defaultWebViewTheme) { // The light theme is selected. Turn off algorithmic darkening. @@ -3968,17 +3982,12 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Set the first page as the current WebView. setCurrentWebView(0) } else { // The first tab is not selected. - // Create a handler move to the page. - val setCurrentPageHandler = Handler(Looper.getMainLooper()) - - // Create a runnable to move to the page. - val setCurrentPageRunnable = Runnable { - // Move to the page. - webViewViewPager2.currentItem = savedTabPosition - } + // Switch to the page before the saved tab position. + webViewViewPager2.post { webViewViewPager2.currentItem = (savedTabPosition - 1) } - // Move to the page after 50 milliseconds, which should be enough time to for the WebView state adapter to populate the restored pages. - setCurrentPageHandler.postDelayed(setCurrentPageRunnable, 50) + // 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 } } // Get the intent that started the app. @@ -4426,8 +4435,8 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Get the WebView theme entry values string array. val webViewThemeEntryValuesStringArray = resources.getStringArray(R.array.webview_theme_entry_values) - // Set the WebView theme if device is running API >= 29 and algorithmic darkening is supported. - if (Build.VERSION.SDK_INT >= 29 && WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) { + // Set the WebView theme if algorithmic darkening is supported. + if (WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) { // Set the WebView them. A switch statement cannot be used because the WebView theme entry values string array is not a compile time constant. if (webViewTheme == webViewThemeEntryValuesStringArray[1]) { // The light theme is selected. // Turn off algorithmic darkening.