X-Git-Url: https://gitweb.stoutner.com/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Factivities%2FMainWebViewActivity.kt;h=d586dddd77d6cc195468b122a5d2010e553b8767;hb=a424c7a4dd110995277c9f4753fecb21a4a4991a;hp=16b40d855ab46df94eab92f0e5f33589a5f7aaec;hpb=09795c801b5e5d85beb63259a2d9cb39f756fa61;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 16b40d85..d586dddd 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt @@ -53,6 +53,7 @@ import android.provider.OpenableColumns import android.text.Editable import android.text.TextWatcher import android.text.style.ForegroundColorSpan +import android.util.Log import android.util.Patterns import android.util.TypedValue import android.view.ContextMenu @@ -137,6 +138,8 @@ import com.stoutner.privacybrowser.dialogs.UrlHistoryDialog import com.stoutner.privacybrowser.dialogs.ViewSslCertificateDialog import com.stoutner.privacybrowser.dialogs.WaitingForProxyDialog import com.stoutner.privacybrowser.fragments.WebViewTabFragment +import com.stoutner.privacybrowser.helpers.BOOKMARK_NAME +import com.stoutner.privacybrowser.helpers.BOOKMARK_URL import com.stoutner.privacybrowser.helpers.COOKIES import com.stoutner.privacybrowser.helpers.DARK_THEME import com.stoutner.privacybrowser.helpers.DISABLED @@ -151,9 +154,12 @@ import com.stoutner.privacybrowser.helpers.ENABLE_FORM_DATA import com.stoutner.privacybrowser.helpers.ENABLE_JAVASCRIPT import com.stoutner.privacybrowser.helpers.ENABLE_ULTRAPRIVACY import com.stoutner.privacybrowser.helpers.ENABLED +import com.stoutner.privacybrowser.helpers.FAVORITE_ICON +import com.stoutner.privacybrowser.helpers.FOLDER_ID import com.stoutner.privacybrowser.helpers.FONT_SIZE import com.stoutner.privacybrowser.helpers.ID import com.stoutner.privacybrowser.helpers.IP_ADDRESSES +import com.stoutner.privacybrowser.helpers.IS_FOLDER import com.stoutner.privacybrowser.helpers.LIGHT_THEME import com.stoutner.privacybrowser.helpers.PINNED_IP_ADDRESSES import com.stoutner.privacybrowser.helpers.PINNED_SSL_CERTIFICATE @@ -234,7 +240,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook companion object { // Define the public static variables. - var currentBookmarksFolder = "" + var currentBookmarksFolderId = 0L val executorService = Executors.newFixedThreadPool(4)!! var orbotStatus = "unknown" val pendingDialogsArrayList = ArrayList() @@ -769,7 +775,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 @@ -994,6 +1000,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) @@ -1053,9 +1076,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) @@ -1137,8 +1157,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) } @@ -1896,7 +1916,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) ) @@ -3216,8 +3236,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. @@ -3336,8 +3356,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. @@ -3518,12 +3538,12 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // The view parameter cannot be removed because it is called from the layout onClick. fun bookmarksBack(@Suppress("UNUSED_PARAMETER")view: View?) { - if (currentBookmarksFolder.isEmpty()) { // The home folder is displayed. + if (currentBookmarksFolderId == HOME_FOLDER_ID) { // The home folder is displayed. // close the bookmarks drawer. drawerLayout.closeDrawer(GravityCompat.END) } else { // A subfolder is displayed. // Set the former parent folder as the current folder. - currentBookmarksFolder = bookmarksDatabaseHelper!!.getParentFolderName(currentBookmarksFolder) + currentBookmarksFolderId = bookmarksDatabaseHelper!!.getParentFolderId(currentBookmarksFolderId) // Load the new folder. loadBookmarksFolder() @@ -3782,10 +3802,10 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook val newBookmarkDisplayOrder = bookmarksListView.count // Create the bookmark. - bookmarksDatabaseHelper!!.createBookmark(bookmarkNameString, bookmarkUrlString, currentBookmarksFolder, newBookmarkDisplayOrder, favoriteIconByteArray) + bookmarksDatabaseHelper!!.createBookmark(bookmarkNameString, bookmarkUrlString, currentBookmarksFolderId, newBookmarkDisplayOrder, favoriteIconByteArray) // Update the bookmarks cursor with the current contents of this folder. - bookmarksCursor = bookmarksDatabaseHelper!!.getBookmarksByDisplayOrder(currentBookmarksFolder) + bookmarksCursor = bookmarksDatabaseHelper!!.getBookmarksByDisplayOrder(currentBookmarksFolderId) // Update the list view. bookmarksCursorAdapter.changeCursor(bookmarksCursor) @@ -3840,10 +3860,10 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook } // Create the folder, which will be placed at the top of the list view. - bookmarksDatabaseHelper!!.createFolder(folderNameString, currentBookmarksFolder, folderIconByteArray) + bookmarksDatabaseHelper!!.createFolder(folderNameString, currentBookmarksFolderId, folderIconByteArray) // Update the bookmarks cursor with the current contents of this folder. - bookmarksCursor = bookmarksDatabaseHelper!!.getBookmarksByDisplayOrder(currentBookmarksFolder) + bookmarksCursor = bookmarksDatabaseHelper!!.getBookmarksByDisplayOrder(currentBookmarksFolderId) // Update the list view. bookmarksCursorAdapter.changeCursor(bookmarksCursor) @@ -3958,13 +3978,19 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook savedStateArrayList = null savedNestedScrollWebViewStateArrayList = null + Log.i("Tab", "Saved tab position: $savedTabPosition") + // 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. - // Move to the selected tab. - webViewViewPager2.currentItem = savedTabPosition + // Switch to the page before the saved tab position. + webViewViewPager2.post { webViewViewPager2.currentItem = (savedTabPosition - 1) } + + // 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. @@ -4186,7 +4212,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook val bookmarksIntent = Intent(applicationContext, BookmarksActivity::class.java) // Add the extra information to the intent. - bookmarksIntent.putExtra(CURRENT_FOLDER, currentBookmarksFolder) + bookmarksIntent.putExtra(CURRENT_FOLDER_ID, currentBookmarksFolderId) bookmarksIntent.putExtra(CURRENT_TITLE, currentWebView!!.title) bookmarksIntent.putExtra(CURRENT_URL, currentWebView!!.url) bookmarksIntent.putExtra(CURRENT_FAVORITE_ICON_BYTE_ARRAY, currentFavoriteIconByteArray) @@ -4283,15 +4309,15 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook bookmarkCursor.moveToFirst() // Act upon the bookmark according to the type. - if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.IS_FOLDER)) == 1) { // The selected bookmark is a folder. - // Store the folder name. - currentBookmarksFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.BOOKMARK_NAME)) + if (bookmarkCursor.getInt(bookmarkCursor.getColumnIndexOrThrow(IS_FOLDER)) == 1) { // The selected bookmark is a folder. + // Store the folder ID. + currentBookmarksFolderId = bookmarkCursor.getLong(bookmarkCursor.getColumnIndexOrThrow(FOLDER_ID)) // Load the new folder. loadBookmarksFolder() } else { // The selected bookmark is not a folder. // Load the bookmark URL. - loadUrl(currentWebView!!, bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.BOOKMARK_URL))) + loadUrl(currentWebView!!, bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(BOOKMARK_URL))) // Close the bookmarks drawer if it is not pinned. if (!bookmarksDrawerPinned) @@ -4309,8 +4335,11 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Run the commands associated with the type. if (bookmarksDatabaseHelper!!.isFolder(databaseId)) { // The bookmark is a folder. + // Get the folder ID. + val folderId = bookmarksDatabaseHelper!!.getFolderId(databaseId) + // Get a cursor of all the bookmarks in the folder. - val bookmarksCursor = bookmarksDatabaseHelper!!.getFolderBookmarks(databaseId) + val bookmarksCursor = bookmarksDatabaseHelper!!.getFolderBookmarks(folderId) // Move to the first entry in the cursor. bookmarksCursor.moveToFirst() @@ -4318,7 +4347,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(BookmarksDatabaseHelper.BOOKMARK_URL)), !bookmarksDrawerPinned && (i == 0)) + addNewTab(bookmarksCursor.getString(bookmarksCursor.getColumnIndexOrThrow(BOOKMARK_URL)), !bookmarksDrawerPinned && (i == 0)) // Move to the next bookmark. bookmarksCursor.moveToNext() @@ -4334,7 +4363,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(BookmarksDatabaseHelper.BOOKMARK_URL)), !bookmarksDrawerPinned) + addNewTab(bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(BOOKMARK_URL)), !bookmarksDrawerPinned) // Close the cursor. bookmarkCursor.close() @@ -4409,8 +4438,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. @@ -5603,7 +5632,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook private fun loadBookmarksFolder() { // Update the bookmarks cursor with the contents of the bookmarks database for the current folder. - bookmarksCursor = bookmarksDatabaseHelper!!.getBookmarksByDisplayOrder(currentBookmarksFolder) + bookmarksCursor = bookmarksDatabaseHelper!!.getBookmarksByDisplayOrder(currentBookmarksFolderId) // Populate the bookmarks cursor adapter. bookmarksCursorAdapter = object : CursorAdapter(this, bookmarksCursor, false) { @@ -5618,7 +5647,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook val bookmarkNameTextView = view.findViewById(R.id.bookmark_name) // Get the favorite icon byte array from the cursor. - val favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.FAVORITE_ICON)) + val favoriteIconByteArray = cursor.getBlob(cursor.getColumnIndexOrThrow(FAVORITE_ICON)) // Convert the byte array to a bitmap beginning at the first byte and ending at the last. val favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.size) @@ -5627,10 +5656,10 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap) // Display the bookmark name from the cursor in the bookmark name text view. - bookmarkNameTextView.text = cursor.getString(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.BOOKMARK_NAME)) + bookmarkNameTextView.text = cursor.getString(cursor.getColumnIndexOrThrow(BOOKMARK_NAME)) // Make the font bold for folders. - if (cursor.getInt(cursor.getColumnIndexOrThrow(BookmarksDatabaseHelper.IS_FOLDER)) == 1) + if (cursor.getInt(cursor.getColumnIndexOrThrow(IS_FOLDER)) == 1) bookmarkNameTextView.typeface = Typeface.DEFAULT_BOLD else // Reset the font to default for normal bookmarks. bookmarkNameTextView.typeface = Typeface.DEFAULT @@ -5641,10 +5670,10 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook bookmarksListView.adapter = bookmarksCursorAdapter // Set the bookmarks drawer title. - if (currentBookmarksFolder.isEmpty()) + if (currentBookmarksFolderId == HOME_FOLDER_ID) // The current bookmarks folder is the home folder. bookmarksTitleTextView.setText(R.string.bookmarks) else - bookmarksTitleTextView.text = currentBookmarksFolder + bookmarksTitleTextView.text = bookmarksDatabaseHelper!!.getFolderName(currentBookmarksFolderId) } private fun loadUrl(nestedScrollWebView: NestedScrollWebView, url: String) {