From d144cd84730c45364ce6b915061a792a994daf2e Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Tue, 11 Apr 2023 16:33:15 -0700 Subject: [PATCH] Create a class domains settings set. https://redmine.stoutner.com/issues/990 --- .../activities/BookmarksActivity.kt | 12 +--- .../activities/MainWebViewActivity.kt | 68 +++++++++++-------- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.kt b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.kt index 43350b11..99d3f539 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/BookmarksActivity.kt @@ -127,16 +127,6 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkListener, CreateBoo val currentUrl = launchingIntent.getStringExtra(CURRENT_URL)!! currentFavoriteIconByteArray = launchingIntent.getByteArrayExtra(CURRENT_FAVORITE_ICON_BYTE_ARRAY)!! - /* TODO. Test if not needed. - // Set the current folder variable. - if (launchingIntent.getStringExtra(CURRENT_FOLDER) != null) { // Set the current folder from the intent. - currentFolder = launchingIntent.getStringExtra(CURRENT_FOLDER) - } else { // Set the current folder to be `""`, which is the home folder. - currentFolder = "" - } - - */ - // Convert the favorite icon byte array to a bitmap. val currentFavoriteIconBitmap = BitmapFactory.decodeByteArray(currentFavoriteIconByteArray, 0, currentFavoriteIconByteArray.size) @@ -829,7 +819,7 @@ class BookmarksActivity : AppCompatActivity(), CreateBookmarkListener, CreateBoo if (currentFolderIconRadioButton.isChecked) { // Only the name has changed. // Update the name in the database. bookmarksDatabaseHelper.updateFolder(selectedFolderDatabaseId, oldFolderNameString, newFolderNameString) - } else { // The icon has changed. TODO: Test. + } else { // The icon has changed. // Populate the new folder icon bitmap. val folderIconBitmap: Bitmap = if (defaultFolderIconRadioButton.isChecked) { // Get the default folder icon drawable. 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 0c703540..d56d9b71 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt @@ -310,6 +310,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook private var displayAdditionalAppBarIcons = false private var displayingFullScreenVideo = false private var domainsDatabaseHelper: DomainsDatabaseHelper? = null + private var domainsSettingsSet: MutableSet = HashSet() private var downloadWithExternalApp = false private var fullScreenBrowsingModeEnabled = false private var hideAppBar = false @@ -734,6 +735,9 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Reset the reapply domain settings on restart flag. reapplyDomainSettingsOnRestart = false + // Update the domains settings set. + updateDomainsSettingsSet() + // Reapply the domain settings for each tab. for (i in 0 until webViewPagerAdapter!!.count) { // Get the WebView tab fragment. @@ -919,11 +923,9 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook } // Close the bookmarks cursor if it exists. - if (bookmarksCursor != null) { - bookmarksCursor!!.close() - } + bookmarksCursor?.close() - // Close the databases database if it exists. + // Close the databases if they exist. bookmarksDatabaseHelper?.close() domainsDatabaseHelper?.close() @@ -3361,6 +3363,9 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Destroy the bare WebView. bareWebView.destroy() + + // Update the domains settings set. + updateDomainsSettingsSet() } private fun applyAppSettings() { @@ -3579,32 +3584,11 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook } } - // Get a full domain name cursor. - val domainNameCursor = domainsDatabaseHelper!!.domainNameCursorOrderedByDomain - - // Initialize the domain settings set. - val domainSettingsSet: MutableSet = HashSet() - - // Get the domain name column index. - val domainNameColumnIndex = domainNameCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.DOMAIN_NAME) - - // Populate the domain settings set. - for (i in 0 until domainNameCursor.count) { - // Move the domains cursor to the current row. - domainNameCursor.moveToPosition(i) - - // Store the domain name in the domain settings set. - domainSettingsSet.add(domainNameCursor.getString(domainNameColumnIndex)) - } - - // Close the domain name cursor. - domainNameCursor.close() - // Initialize the domain name in database variable. var domainNameInDatabase: String? = null // Check the hostname against the domain settings set. - if (domainSettingsSet.contains(newHostName)) { // The hostname is contained in the domain settings set. + if (domainsSettingsSet.contains(newHostName)) { // The hostname is contained in the domain settings set. // Record the domain name in the database. domainNameInDatabase = newHostName @@ -3617,7 +3601,7 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook // Check all the subdomains of the host name against wildcard domains in the domain cursor. while (!nestedScrollWebView.domainSettingsApplied && newHostName!!.contains(".")) { // Stop checking if domain settings are already applied or there are no more `.` in the hostname. - if (domainSettingsSet.contains("*.$newHostName")) { // Check the host name prepended by `*.`. + if (domainsSettingsSet.contains("*.$newHostName")) { // Check the host name prepended by `*.`. // Set the domain settings applied tracker to true. nestedScrollWebView.domainSettingsApplied = true @@ -4409,8 +4393,10 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook } private fun clearAndExit() { - // Close the cursor and databases. TODO: Also domains cursor. + // Close the bookmarks cursor if it exists. bookmarksCursor?.close() + + // Close the databases helpers if they exist. bookmarksDatabaseHelper?.close() domainsDatabaseHelper?.close() @@ -5914,4 +5900,30 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook } } } + + private fun updateDomainsSettingsSet() { + // Reset the domains settings set. + domainsSettingsSet = HashSet() + + // Get a domains cursor. + val domainsCursor = domainsDatabaseHelper!!.domainNameCursorOrderedByDomain + + // Get the current count of domains. + val domainsCount = domainsCursor.count + + // Get the domain name column index. + val domainNameColumnIndex = domainsCursor.getColumnIndexOrThrow(DomainsDatabaseHelper.DOMAIN_NAME) + + // Populate the domain settings set. + for (i in 0 until domainsCount) { + // Move the domains cursor to the current row. + domainsCursor.moveToPosition(i) + + // Store the domain name in the domain settings set. + domainsSettingsSet.add(domainsCursor.getString(domainNameColumnIndex)) + } + + // Close the domains cursor. + domainsCursor.close() + } } -- 2.43.0