X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.kt;h=d1fb09ae8afd1bc2bb36e7586c2c5455b11c1390;hb=83a2b6e5635956bb940642e0e5beb47453145dc3;hp=dc63adf65cb190d9b0adfa2edd14e6b75758dfb0;hpb=227f44a771ae2d21259b1a8a5c9377f2c2b1d627;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 dc63adf6..d1fb09ae 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt @@ -377,7 +377,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook private var domainsDatabaseHelper: DomainsDatabaseHelper? = null private var downloadWithExternalApp = false private var fullScreenBrowsingModeEnabled = false - private var hideAppBar = false + private var hideAppBar = true private var inFullScreenBrowsingMode = false private var incognitoModeEnabled = false private var loadingNewIntent = false @@ -524,6 +524,15 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false) bottomAppBar = sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false) displayAdditionalAppBarIcons = sharedPreferences.getBoolean(getString(R.string.display_additional_app_bar_icons_key), false) + val displayUnderCutouts = sharedPreferences.getBoolean(getString(R.string.display_under_cutouts_key), false) + + // Display under cutouts if specified. This must be done here as it doesn't appear to work correctly if handled after the app is fully initialized. + if (displayUnderCutouts) { + if (Build.VERSION.SDK_INT >= 30) + window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS + else if (Build.VERSION.SDK_INT >= 28) + window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES + } // Get the theme entry values string array. val appThemeEntryValuesStringArray = resources.getStringArray(R.array.app_theme_entry_values) @@ -686,12 +695,26 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Get the current web back forward list. val webBackForwardList = currentWebView!!.copyBackForwardList() - // Get the previous entry URL. + // 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() @@ -778,7 +801,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook loadingNewIntent = true // Add a new tab. - addNewTab(url!!, true) + addNewPage(url!!, adjacent = false, moveToTab = true) } else { // Load the URL in the current tab. // Make it so. loadUrl(currentWebView!!, url!!) @@ -2006,10 +2029,10 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Open a new tab according to the current URL. if (currentWebView!!.currentUrl.startsWith("view-source:")) { // The source is currently viewed. // Open the rendered website in a new tab. - addNewTab(currentWebView!!.currentUrl.substring(12, currentWebView!!.currentUrl.length), true) + addNewPage(currentWebView!!.currentUrl.substring(12, currentWebView!!.currentUrl.length), true, moveToTab = true) } else { // The rendered website is currently viewed. // Open the source in a new tab. - addNewTab("view-source:${currentWebView!!.currentUrl}", true) + addNewPage("view-source:${currentWebView!!.currentUrl}", adjacent = true, moveToTab = true) } // Consume the event. @@ -2325,12 +2348,25 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Get the current web back forward list. val webBackForwardList = currentWebView!!.copyBackForwardList() - // Get the previous entry URL. + // 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() @@ -2345,12 +2381,25 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Get the current web back forward list. val webBackForwardList = currentWebView!!.copyBackForwardList() - // Get the next entry URL. + // 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() @@ -2579,7 +2628,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Add an open in new tab entry. contextMenu.add(R.string.open_in_new_tab).setOnMenuItemClickListener { // Load the link URL in a new tab and move to it. - addNewTab(linkUrl, true) + addNewPage(linkUrl, adjacent = true, moveToTab = true) // Consume the event. true @@ -2588,7 +2637,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Add an open in background entry. contextMenu.add(R.string.open_in_background).setOnMenuItemClickListener { // Load the link URL in a new tab but do not move to it. - addNewTab(linkUrl, false) + addNewPage(linkUrl, adjacent = true, moveToTab = false) // Consume the event. true @@ -2675,7 +2724,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Add an open in new tab entry. contextMenu.add(R.string.open_image_in_new_tab).setOnMenuItemClickListener { // Load the image in a new tab. - addNewTab(imageUrl, true) + addNewPage(imageUrl, adjacent = true, moveToTab = true) // Consume the event. true @@ -2781,7 +2830,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Add an open in new tab entry. contextMenu.add(R.string.open_in_new_tab).setOnMenuItemClickListener { // Load the link URL in a new tab and move to it. - addNewTab(linkUrl, true) + addNewPage(linkUrl, adjacent = true, moveToTab = true) // Consume the event. true @@ -2790,7 +2839,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Add an open in background entry. contextMenu.add(R.string.open_in_background).setOnMenuItemClickListener { // Lod the link URL in a new tab but do not move to it. - addNewTab(linkUrl, false) + addNewPage(linkUrl, adjacent = true, moveToTab = false) // Consume the event. true @@ -2799,7 +2848,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Add an open image in new tab entry. contextMenu.add(R.string.open_image_in_new_tab).setOnMenuItemClickListener { // Load the image in a new tab and move to it. - addNewTab(imageUrl, true) + addNewPage(imageUrl, adjacent = true, moveToTab = true) // Consume the event. true @@ -2965,38 +3014,68 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // The view parameter cannot be removed because it is called from the layout onClick. fun addTab(@Suppress("UNUSED_PARAMETER")view: View?) { // Add a new tab with a blank URL. - addNewTab("", true) + addNewPage(urlString = "", adjacent = true, moveToTab = true) } - private fun addNewTab(urlString: String, moveToTab: Boolean) { + private fun addNewPage(urlString: String, adjacent: Boolean, moveToTab: Boolean) { // Clear the focus from the URL edit text, so that it will be populated with the information from the new tab. urlEditText.clearFocus() - // Get the new page number. The page numbers are 0 indexed, so the new page number will match the current count. - val newTabNumber = tabLayout.tabCount + // Get the new tab position. + val newTabPosition = if (adjacent) // The new tab position is immediately to the right of the current tab position. + tabLayout.selectedTabPosition + 1 + else // The new tab position is at the end. The tab positions are 0 indexed, so the new page number will match the current count. + tabLayout.tabCount - // Add a new tab. - tabLayout.addTab(tabLayout.newTab()) + // Add the new WebView page. + webViewStateAdapter!!.addPage(newTabPosition, urlString) - // Get the new tab. - val newTab = tabLayout.getTabAt(newTabNumber)!! + // Add the new tab. + addNewTab(newTabPosition, moveToTab) + } - // Set a custom view on the new tab. - newTab.setCustomView(R.layout.tab_custom_view) + private fun addNewTab(newTabPosition: Int, moveToTab: Boolean) { + // Check to see if the new page is ready. + if (webViewStateAdapter!!.itemCount >= tabLayout.tabCount) { // The new page is ready. + // Create a new tab. + val newTab = tabLayout.newTab() - // Scroll to the new tab position. - tabLayout.post { tabLayout.setScrollPosition(newTabNumber, 0F, false, false) } + // Set a custom view on the new tab. + newTab.setCustomView(R.layout.tab_custom_view) - // Add the new WebView page. - webViewStateAdapter!!.addPage(newTabNumber, newTab, urlString, moveToTab) + // Add the new tab. + tabLayout.addTab(newTab, newTabPosition, 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) { - // Animate the bottom app bar onto the screen. - objectAnimator = ObjectAnimator.ofFloat(appBarLayout, "translationY", 0f) + // Select the new tab if it is the first one. For some odd reason, Android doesn't select the first tab if it is the only one, which causes problems with the new tab position logic above. + if (newTabPosition == 0) + tabLayout.selectTab(newTab) - // Make it so. - objectAnimator.start() + // Scroll to the new tab position if moving to the new tab. + if (moveToTab) + tabLayout.post { + tabLayout.setScrollPosition(newTabPosition, 0F, false, false) + } + + // 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) { + // Animate the bottom app bar onto the screen. + objectAnimator = ObjectAnimator.ofFloat(appBarLayout, "translationY", 0f) + + // Make it so. + objectAnimator.start() + } + } else { // The new page is not ready. + // Create a new tab handler. + val newTabHandler = Handler(Looper.getMainLooper()) + + // Create a new tab runnable. + val newTabRunnable = Runnable { + // Create the new tab. + addNewTab(newTabPosition, moveToTab) + } + + // Try adding the new tab again after 50 milliseconds. + newTabHandler.postDelayed(newTabRunnable, 50) } } @@ -4092,11 +4171,11 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook val databaseId = bookmarksListView.getItemIdAtPosition(i).toInt() // Move the bookmark down one slot. - bookmarksDatabaseHelper!!.updateDisplayOrder(databaseId, i + 1) + bookmarksDatabaseHelper!!.updateDisplayOrder(databaseId, displayOrder = i + 1) } // Create the folder, which will be placed at the top of the list view. - bookmarksDatabaseHelper!!.createFolder(folderNameString, currentBookmarksFolderId, folderIconByteArray) + bookmarksDatabaseHelper!!.createFolder(folderNameString, currentBookmarksFolderId, displayOrder = 0, folderIconByteArray) // Update the bookmarks cursor with the current contents of this folder. bookmarksCursor = bookmarksDatabaseHelper!!.getBookmarksByDisplayOrder(currentBookmarksFolderId) @@ -4193,7 +4272,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Check to see if the activity has been restarted with a saved state. 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) + addNewPage(urlString = "", adjacent = false, moveToTab = false) } else { // The activity has been restarted with a saved state. // Restore each tab. for (i in savedStateArrayList!!.indices) { @@ -4252,7 +4331,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook loadingNewIntent = true // Add a new tab. - addNewTab(urlString, true) + addNewPage(urlString, adjacent = false, moveToTab = true) } else { // Load the URL in the current tab. // Make it so. loadUrl(currentWebView!!, urlString) @@ -4397,8 +4476,10 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Select the same page in the view pager. webViewViewPager2.currentItem = tab.position - // Set the current WebView. - setCurrentWebView(tab.position) + // Set the current WebView after the tab layout has quiesced (otherwise, sometimes the wong WebView might be used). See + tabLayout.post { + setCurrentWebView(tab.position) + } } } @@ -4573,7 +4654,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Open each bookmark for (i in 0 until bookmarksCursor.count) { // Load the bookmark in a new tab, moving to the tab for the first bookmark if the drawer is not pinned. - addNewTab(bookmarksCursor.getString(bookmarksCursor.getColumnIndexOrThrow(BOOKMARK_URL)), !bookmarksDrawerPinned && (i == 0)) + addNewPage(bookmarksCursor.getString(bookmarksCursor.getColumnIndexOrThrow(BOOKMARK_URL)), adjacent = false, moveToTab = !bookmarksDrawerPinned && (i == 0)) // Move to the next bookmark. bookmarksCursor.moveToNext() @@ -4589,7 +4670,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook bookmarkCursor.moveToFirst() // Load the bookmark in a new tab and move to the tab if the drawer is not pinned. - addNewTab(bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(BOOKMARK_URL)), !bookmarksDrawerPinned) + addNewPage(bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(BOOKMARK_URL)), adjacent = true, moveToTab = !bookmarksDrawerPinned) // Close the cursor. bookmarkCursor.close() @@ -4690,7 +4771,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook } @SuppressLint("ClickableViewAccessibility") - override fun initializeWebView(nestedScrollWebView: NestedScrollWebView, pageNumber: Int, progressBar: ProgressBar, urlString: String, restoringState: Boolean) { + override fun initializeWebView(nestedScrollWebView: NestedScrollWebView, pagePosition: Int, progressBar: ProgressBar, urlString: String, restoringState: Boolean) { // Get the WebView theme. val webViewTheme = sharedPreferences.getString(getString(R.string.webview_theme_key), getString(R.string.webview_theme_default_value)) @@ -4817,8 +4898,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // The deprecated command can be switched to `WindowInsetsController` once the minimum API >= 30. @Suppress("DEPRECATION") - rootFrameLayout.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or - View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY + rootFrameLayout.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY } else { // Switch to normal viewing mode. // Show the app bar if it was hidden. if (hideAppBar) { @@ -5838,7 +5918,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook if (restoringState) { // The state is being restored. // Resume the nested scroll WebView JavaScript timers. nestedScrollWebView.resumeTimers() - } else if (pageNumber == 0) { // The first page is being loaded. + } else if (pagePosition == 0) { // The first page is being loaded. // Set this nested scroll WebView as the current WebView. currentWebView = nestedScrollWebView @@ -5893,17 +5973,10 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Request focus for the URL text box. urlEditText.requestFocus() - // Create a display keyboard handler. - val displayKeyboardHandler = Handler(Looper.getMainLooper()) - - // Create a display keyboard runnable. - val displayKeyboardRunnable = Runnable { - // Display the keyboard. + // Display the keyboard once the tab layout has settled. + tabLayout.post { inputMethodManager.showSoftInput(urlEditText, 0) } - - // Display the keyboard after 100 milliseconds, which leaves enough time for the tab to transition. - displayKeyboardHandler.postDelayed(displayKeyboardRunnable, 100) } } } @@ -6261,8 +6334,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Remove any background on the URL relative layout. urlRelativeLayout.background = AppCompatResources.getDrawable(this, R.color.transparent) } - } catch (exception: Exception) { - // Try again in 100 milliseconds if the WebView has not yet been populated. + } catch (exception: Exception) { // Try again in 10 milliseconds if the WebView has not yet been populated. // Create a handler to set the current WebView. val setCurrentWebViewHandler = Handler(Looper.getMainLooper()) @@ -6272,8 +6344,8 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook setCurrentWebView(pageNumber) } - // Try setting the current WebView again after 50 milliseconds. - setCurrentWebViewHandler.postDelayed(setCurrentWebWebRunnable, 50) + // Try setting the current WebView again after 10 milliseconds. + setCurrentWebViewHandler.postDelayed(setCurrentWebWebRunnable, 10) } }