]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/views/NestedScrollWebView.kt
Implement selecting bookmark favorite icons from the file system. https://redmine...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / views / NestedScrollWebView.kt
index 4011f5a5aba4fe689a628fbd8523afa306e634f6..6076950291c304e74fa75d0326913973da8c1984 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2019-2024 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
  *
@@ -23,7 +23,7 @@ import android.animation.ObjectAnimator
 import android.annotation.SuppressLint
 import android.content.Context
 import android.graphics.Bitmap
-import android.graphics.drawable.BitmapDrawable
+import android.graphics.drawable.Drawable
 import android.os.Bundle
 import android.util.AttributeSet
 import android.view.MotionEvent
@@ -31,12 +31,10 @@ import android.webkit.HttpAuthHandler
 import android.webkit.SslErrorHandler
 import android.webkit.WebView
 
-import androidx.core.content.ContextCompat
 import androidx.core.view.NestedScrollingChild2
 import androidx.core.view.NestedScrollingChildHelper
 import androidx.core.view.ViewCompat
 
-import com.stoutner.privacybrowser.R
 import com.stoutner.privacybrowser.activities.MainWebViewActivity
 
 import java.util.Collections
@@ -44,53 +42,50 @@ import java.util.Date
 
 import kotlin.collections.ArrayList
 
-import kotlin.jvm.JvmOverloads
-
-// Define the saved state constants.
-private const val DOMAIN_SETTINGS_APPLIED = "domain_settings_applied"
-private const val DOMAIN_SETTINGS_DATABASE_ID = "domain_settings_database_id"
-private const val CURRENT_DOMAIN_NAME = "current_domain_name"
-private const val CURRENT_URl = "current_url"
-private const val ACCEPT_COOKIES = "accept_cookies"
-private const val EASYLIST_ENABLED = "easylist_enabled"
-private const val EASYPRIVACY_ENABLED = "easyprivacy_enabled"
-private const val FANBOYS_ANNOYANCE_LIST_ENABLED = "fanboys_annoyance_list_enabled"
-private const val FANBOYS_SOCIAL_BLOCKING_LIST_ENABLED = "fanboys_social_blocking_list_enabled"
-private const val ULTRALIST_ENABLED = "ultralist_enabled"
-private const val ULTRAPRIVACY_ENABLED = "ultraprivacy_enabled"
-private const val BLOCK_ALL_THIRD_PARTY_REQUESTS = "block_all_third_party_requests"
-private const val HAS_PINNED_SSL_CERTIFICATE = "has_pinned_ssl_certificate"
-private const val PINNED_SSL_ISSUED_TO_CNAME = "pinned_ssl_issued_to_cname"
-private const val PINNED_SSL_ISSUED_TO_ONAME = "pinned_ssl_issued_to_oname"
-private const val PINNED_SSL_ISSUED_TO_UNAME = "pinned_ssl_issued_to_uname"
-private const val PINNED_SSL_ISSUED_BY_CNAME = "pinned_ssl_issued_by_cname"
-private const val PINNED_SSL_ISSUED_BY_ONAME = "pinned_ssl_issued_by_oname"
-private const val PINNED_SSL_ISSUED_BY_UNAME = "pinned_ssl_issued_by_uname"
-private const val PINNED_SSL_START_DATE = "pinned_ssl_start_date"
-private const val PINNED_SSL_END_DATE = "pinned_ssl_end_date"
-private const val PINNED_IP_ADDRESSES = "pinned_ip_addresses"
-private const val IGNORE_PINNED_DOMAIN_INFORMATION = "ignore_pinned_domain_information"
-private const val SWIPE_TO_REFRESH = "swipe_to_refresh"
-private const val JAVASCRIPT_ENABLED = "javascript_enabled"
-private const val DOM_STORAGE_ENABLED = "dom_storage_enabled"
-private const val USER_AGENT = "user_agent"
-private const val WIDE_VIEWPORT = "wide_viewport"
-private const val FONT_SIZE = "font_size"
+// Define the public constants.
+const val BLOCKED_REQUESTS = 0
+const val EASYLIST = 1
+const val EASYPRIVACY = 2
+const val FANBOYS_ANNOYANCE_LIST = 3
+const val FANBOYS_SOCIAL_BLOCKING_LIST = 4
+const val ULTRALIST = 5
+const val ULTRAPRIVACY = 6
+const val THIRD_PARTY_REQUESTS = 7
+
+// Define the private class constants.
+private const val ACCEPT_COOKIES = "A"
+private const val BLOCK_ALL_THIRD_PARTY_REQUESTS = "B"
+private const val CURRENT_DOMAIN_NAME = "C"
+private const val CURRENT_URL = "D"
+private const val DOM_STORAGE_ENABLED = "E"
+private const val DOMAIN_SETTINGS_APPLIED = "F"
+private const val DOMAIN_SETTINGS_DATABASE_ID = "G"
+private const val EASYLIST_ENABLED = "H"
+private const val EASYPRIVACY_ENABLED = "I"
+private const val FANBOYS_ANNOYANCE_LIST_ENABLED = "J"
+private const val FANBOYS_SOCIAL_BLOCKING_LIST_ENABLED = "K"
+private const val FONT_SIZE = "L"
+private const val HAS_PINNED_SSL_CERTIFICATE = "M"
+private const val IGNORE_PINNED_DOMAIN_INFORMATION = "N"
+private const val JAVASCRIPT_ENABLED = "O"
+private const val PINNED_IP_ADDRESSES = "P"
+private const val PINNED_SSL_END_DATE = "Q"
+private const val PINNED_SSL_ISSUED_BY_CNAME = "R"
+private const val PINNED_SSL_ISSUED_BY_ONAME = "S"
+private const val PINNED_SSL_ISSUED_BY_UNAME = "T"
+private const val PINNED_SSL_ISSUED_TO_CNAME = "U"
+private const val PINNED_SSL_ISSUED_TO_ONAME = "V"
+private const val PINNED_SSL_ISSUED_TO_UNAME = "W"
+private const val PINNED_SSL_START_DATE = "X"
+private const val SWIPE_TO_REFRESH = "Y"
+private const val ULTRALIST_ENABLED = "Z"
+private const val ULTRAPRIVACY_ENABLED = "AA"
+private const val USER_AGENT = "AB"
+private const val WIDE_VIEWPORT = "AC"
 
 // NestedScrollWebView extends WebView to handle nested scrolls (scrolling the app bar off the screen).  It also stores extra information about the state of the WebView used by Privacy Browser.
 class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, defaultStyle: Int = android.R.attr.webViewStyle) : WebView(context, attributeSet, defaultStyle),
     NestedScrollingChild2 {
-    companion object {
-        // Define the public companion object constants.  These can be moved to public class constants once the entire project has migrated to Kotlin.
-        const val BLOCKED_REQUESTS = 0
-        const val EASYLIST = 1
-        const val EASYPRIVACY = 2
-        const val FANBOYS_ANNOYANCE_LIST = 3
-        const val FANBOYS_SOCIAL_BLOCKING_LIST = 4
-        const val ULTRALIST = 5
-        const val ULTRAPRIVACY = 6
-        const val THIRD_PARTY_REQUESTS = 7
-    }
 
     // Define the public variables.
     var acceptCookies = false
@@ -107,6 +102,8 @@ class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeS
     var httpAuthHandler: HttpAuthHandler? = null
     var ignorePinnedDomainInformation = false
     var pinnedIpAddresses = ""
+    var previousFavoriteIconDrawable: Drawable? = null
+    var previousWebpageTitle = ""
     var sslErrorHandler: SslErrorHandler? = null
     var swipeToRefresh = false
     var ultraListEnabled = true
@@ -114,10 +111,10 @@ class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeS
     var waitingForProxyUrlString = ""
     var webViewFragmentId: Long = 0
 
-
     // Define the private variables.
     private val nestedScrollingChildHelper: NestedScrollingChildHelper = NestedScrollingChildHelper(this)
-    private lateinit var favoriteOrDefaultIcon: Bitmap
+    private lateinit var favoriteIcon: Bitmap
+    private var favoriteIconHeight = 0
     private var previousYPosition = 0  // The previous Y position needs to be tracked between motion events.
     private var hasPinnedSslCertificate = false
     private var pinnedSslIssuedToCName = ""
@@ -137,44 +134,44 @@ class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeS
     private var ultraListBlockedRequests = 0
     private var ultraPrivacyBlockedRequests = 0
     private var thirdPartyBlockedRequests = 0
-    private var xRequestedWithHeader = mutableMapOf<String, String>()
 
     init {
         // Enable nested scrolling by default.
         nestedScrollingChildHelper.isNestedScrollingEnabled = true
 
         // Initialize the favorite icon.
-        initializeFavoriteIcon()
+        resetFavoriteIcon()
     }
 
-
     // Favorite or default icon.
-    fun initializeFavoriteIcon() {
-        // Get the default favorite icon drawable.  `ContextCompat` must be used until API >= 21.
-        val favoriteIconDrawable = ContextCompat.getDrawable(context, R.drawable.world)
-
-        // Cast the favorite icon drawable to a bitmap drawable.
-        val favoriteIconBitmapDrawable = (favoriteIconDrawable as BitmapDrawable?)!!
-
+    fun resetFavoriteIcon() {
         // Store the default icon bitmap.
-        favoriteOrDefaultIcon = favoriteIconBitmapDrawable.bitmap
+        favoriteIcon = MainWebViewActivity.defaultFavoriteIconBitmap
+
+        // Set the favorite icon height to be 0.  This way any favorite icons presented by the website will overwrite it.
+        favoriteIconHeight = 0
     }
 
-    fun setFavoriteOrDefaultIcon(icon: Bitmap) {
-        // Scale the favorite icon bitmap down if it is larger than 256 x 256.  Filtering uses bilinear interpolation.
-        favoriteOrDefaultIcon = if (icon.height > 256 || icon.width > 256) {
-            Bitmap.createScaledBitmap(icon, 256, 256, true)
-        } else {
-            // Store the icon as presented.
+    fun setFavoriteIcon(icon: Bitmap) {
+        // Store the current favorite icon height.
+        favoriteIconHeight = icon.height
+
+        // Scale the favorite icon bitmap down if it is larger than 128 in either direction.  Filtering uses bilinear interpolation.
+        favoriteIcon = if (icon.height > 128 || icon.width > 128)  // Scale the icon before storing it.
+            Bitmap.createScaledBitmap(icon, 128, 128, true)
+        else  // Store the icon as presented.
             icon
-        }
     }
 
-    fun getFavoriteOrDefaultIcon(): Bitmap {
-        // Return the favorite or default icon.  This is the only way to return a non-nullable variable while retaining the custom initialization and setter functions above.
-        return favoriteOrDefaultIcon
+    fun getFavoriteIcon(): Bitmap {
+        // Return the favorite icon.  This is the only way to return a non-nullable variable while retaining the custom initialization and setter functions above.
+        return favoriteIcon
     }
 
+    fun getFavoriteIconHeight(): Int {
+        // Return the favorite icon height.
+        return favoriteIconHeight
+    }
 
     // Reset the handlers.
     fun resetSslErrorHandler() {
@@ -254,9 +251,9 @@ class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeS
 
 
     // Resource request counters.
-    fun incrementRequestsCount(blocklist: Int) {
-        // Increment the count of the indicated blocklist.
-        when (blocklist) {
+    fun incrementRequestsCount(filterList: Int) {
+        // Increment the count of the indicated filter list.
+        when (filterList) {
             BLOCKED_REQUESTS -> blockedRequests++
             EASYLIST -> easyListBlockedRequests++
             EASYPRIVACY -> easyPrivacyBlockedRequests++
@@ -268,9 +265,9 @@ class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeS
         }
     }
 
-    fun getRequestsCount(blocklist: Int): Int {
-        // Return the count of the indicated blocklist.
-        return when (blocklist) {
+    fun getRequestsCount(filterList: Int): Int {
+        // Return the count of the indicated filter list.
+        return when (filterList) {
             BLOCKED_REQUESTS -> blockedRequests
             EASYLIST -> easyListBlockedRequests
             EASYPRIVACY -> easyPrivacyBlockedRequests
@@ -296,24 +293,6 @@ class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeS
     }
 
 
-    // X-Requested-With header.
-    fun getXRequestedWithHeader() : MutableMap<String, String> {
-        // Return the X-Requested-With header.
-        return xRequestedWithHeader
-    }
-
-    fun setXRequestedWithHeader() {
-        // Set the X-Requested-With header to use a null value.
-        if (xRequestedWithHeader.isEmpty())
-            xRequestedWithHeader["X-Requested-With"] = ""
-    }
-
-    fun resetXRequestedWithHeader() {
-        // Clear the map, which resets the X-Requested-With header to use the default value of the application ID (com.stoutner.privacybrowser.standard).
-        xRequestedWithHeader.clear()
-    }
-
-
     // Publicly expose the scroll ranges.
     fun getHorizontalScrollRange(): Int {
         // Return the horizontal scroll range.
@@ -402,77 +381,78 @@ class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeS
     }
 
 
-    // Save and restore state.
+    // Save the state.
     fun saveNestedScrollWebViewState(): Bundle {
         // Create a saved state bundle.
         val savedState = Bundle()
 
         // Populate the saved state bundle.
+        savedState.putBoolean(ACCEPT_COOKIES, acceptCookies)
+        savedState.putBoolean(BLOCK_ALL_THIRD_PARTY_REQUESTS, blockAllThirdPartyRequests)
+        savedState.putString(CURRENT_DOMAIN_NAME, currentDomainName)
+        savedState.putString(CURRENT_URL, currentUrl)
+        savedState.putBoolean(DOM_STORAGE_ENABLED, this.settings.domStorageEnabled)
         savedState.putBoolean(DOMAIN_SETTINGS_APPLIED, domainSettingsApplied)
         savedState.putInt(DOMAIN_SETTINGS_DATABASE_ID, domainSettingsDatabaseId)
-        savedState.putString(CURRENT_DOMAIN_NAME, currentDomainName)
-        savedState.putString(CURRENT_URl, currentUrl)
-        savedState.putBoolean(ACCEPT_COOKIES, acceptCookies)
         savedState.putBoolean(EASYLIST_ENABLED, easyListEnabled)
         savedState.putBoolean(EASYPRIVACY_ENABLED, easyPrivacyEnabled)
         savedState.putBoolean(FANBOYS_ANNOYANCE_LIST_ENABLED, fanboysAnnoyanceListEnabled)
         savedState.putBoolean(FANBOYS_SOCIAL_BLOCKING_LIST_ENABLED, fanboysSocialBlockingListEnabled)
-        savedState.putBoolean(ULTRALIST_ENABLED, ultraListEnabled)
-        savedState.putBoolean(ULTRAPRIVACY_ENABLED, ultraPrivacyEnabled)
-        savedState.putBoolean(BLOCK_ALL_THIRD_PARTY_REQUESTS, blockAllThirdPartyRequests)
+        savedState.putInt(FONT_SIZE, this.settings.textZoom)
         savedState.putBoolean(HAS_PINNED_SSL_CERTIFICATE, hasPinnedSslCertificate)
-        savedState.putString(PINNED_SSL_ISSUED_TO_CNAME, pinnedSslIssuedToCName)
-        savedState.putString(PINNED_SSL_ISSUED_TO_ONAME, pinnedSslIssuedToOName)
-        savedState.putString(PINNED_SSL_ISSUED_TO_UNAME, pinnedSslIssuedToUName)
+        savedState.putBoolean(IGNORE_PINNED_DOMAIN_INFORMATION, ignorePinnedDomainInformation)
+        savedState.putBoolean(JAVASCRIPT_ENABLED, this.settings.javaScriptEnabled)
+        savedState.putString(PINNED_IP_ADDRESSES, pinnedIpAddresses)
+        savedState.putLong(PINNED_SSL_END_DATE, pinnedSslEndDate.time)
         savedState.putString(PINNED_SSL_ISSUED_BY_CNAME, pinnedSslIssuedByCName)
         savedState.putString(PINNED_SSL_ISSUED_BY_ONAME, pinnedSslIssuedByOName)
         savedState.putString(PINNED_SSL_ISSUED_BY_UNAME, pinnedSslIssuedByUName)
+        savedState.putString(PINNED_SSL_ISSUED_TO_CNAME, pinnedSslIssuedToCName)
+        savedState.putString(PINNED_SSL_ISSUED_TO_ONAME, pinnedSslIssuedToOName)
+        savedState.putString(PINNED_SSL_ISSUED_TO_UNAME, pinnedSslIssuedToUName)
         savedState.putLong(PINNED_SSL_START_DATE, pinnedSslStartDate.time)
-        savedState.putLong(PINNED_SSL_END_DATE, pinnedSslEndDate.time)
-        savedState.putString(PINNED_IP_ADDRESSES, pinnedIpAddresses)
-        savedState.putBoolean(IGNORE_PINNED_DOMAIN_INFORMATION, ignorePinnedDomainInformation)
         savedState.putBoolean(SWIPE_TO_REFRESH, swipeToRefresh)
-        savedState.putBoolean(JAVASCRIPT_ENABLED, this.settings.javaScriptEnabled)
-        savedState.putBoolean(DOM_STORAGE_ENABLED, this.settings.domStorageEnabled)
+        savedState.putBoolean(ULTRALIST_ENABLED, ultraListEnabled)
+        savedState.putBoolean(ULTRAPRIVACY_ENABLED, ultraPrivacyEnabled)
         savedState.putString(USER_AGENT, this.settings.userAgentString)
         savedState.putBoolean(WIDE_VIEWPORT, this.settings.useWideViewPort)
-        savedState.putInt(FONT_SIZE, this.settings.textZoom)
 
         // Return the saved state bundle.
         return savedState
     }
 
+    // Restore the state.
     fun restoreNestedScrollWebViewState(savedState: Bundle) {
         // Restore the class variables.
+        acceptCookies = savedState.getBoolean(ACCEPT_COOKIES)
+        blockAllThirdPartyRequests = savedState.getBoolean(BLOCK_ALL_THIRD_PARTY_REQUESTS)
+        currentDomainName = savedState.getString(CURRENT_DOMAIN_NAME)!!
+        currentUrl = savedState.getString(CURRENT_URL)!!
+        this.settings.domStorageEnabled = savedState.getBoolean(DOM_STORAGE_ENABLED)
         domainSettingsApplied = savedState.getBoolean(DOMAIN_SETTINGS_APPLIED)
         domainSettingsDatabaseId = savedState.getInt(DOMAIN_SETTINGS_DATABASE_ID)
-        currentDomainName = savedState.getString(CURRENT_DOMAIN_NAME)!!
-        currentUrl = savedState.getString(CURRENT_URl)!!
-        acceptCookies = savedState.getBoolean(ACCEPT_COOKIES)
         easyListEnabled = savedState.getBoolean(EASYLIST_ENABLED)
         easyPrivacyEnabled = savedState.getBoolean(EASYPRIVACY_ENABLED)
         fanboysAnnoyanceListEnabled = savedState.getBoolean(FANBOYS_ANNOYANCE_LIST_ENABLED)
         fanboysSocialBlockingListEnabled = savedState.getBoolean(FANBOYS_SOCIAL_BLOCKING_LIST_ENABLED)
-        ultraListEnabled = savedState.getBoolean(ULTRALIST_ENABLED)
-        ultraPrivacyEnabled = savedState.getBoolean(ULTRAPRIVACY_ENABLED)
-        blockAllThirdPartyRequests = savedState.getBoolean(BLOCK_ALL_THIRD_PARTY_REQUESTS)
+        this.settings.textZoom = savedState.getInt(FONT_SIZE)
         hasPinnedSslCertificate = savedState.getBoolean(HAS_PINNED_SSL_CERTIFICATE)
-        pinnedSslIssuedToCName = savedState.getString(PINNED_SSL_ISSUED_TO_CNAME)!!
-        pinnedSslIssuedToOName = savedState.getString(PINNED_SSL_ISSUED_TO_ONAME)!!
-        pinnedSslIssuedToUName = savedState.getString(PINNED_SSL_ISSUED_TO_UNAME)!!
+        ignorePinnedDomainInformation = savedState.getBoolean(IGNORE_PINNED_DOMAIN_INFORMATION)
+        this.settings.javaScriptEnabled = savedState.getBoolean(JAVASCRIPT_ENABLED)
+        pinnedIpAddresses = savedState.getString(PINNED_IP_ADDRESSES)!!
+        pinnedSslEndDate = Date(savedState.getLong(PINNED_SSL_END_DATE))
         pinnedSslIssuedByCName = savedState.getString(PINNED_SSL_ISSUED_BY_CNAME)!!
         pinnedSslIssuedByOName = savedState.getString(PINNED_SSL_ISSUED_BY_ONAME)!!
         pinnedSslIssuedByUName = savedState.getString(PINNED_SSL_ISSUED_BY_UNAME)!!
+        pinnedSslIssuedToCName = savedState.getString(PINNED_SSL_ISSUED_TO_CNAME)!!
+        pinnedSslIssuedToOName = savedState.getString(PINNED_SSL_ISSUED_TO_ONAME)!!
+        pinnedSslIssuedToUName = savedState.getString(PINNED_SSL_ISSUED_TO_UNAME)!!
         pinnedSslStartDate = Date(savedState.getLong(PINNED_SSL_START_DATE))
-        pinnedSslEndDate = Date(savedState.getLong(PINNED_SSL_END_DATE))
-        pinnedIpAddresses = savedState.getString(PINNED_IP_ADDRESSES)!!
-        ignorePinnedDomainInformation = savedState.getBoolean(IGNORE_PINNED_DOMAIN_INFORMATION)
         swipeToRefresh = savedState.getBoolean(SWIPE_TO_REFRESH)
-        this.settings.javaScriptEnabled = savedState.getBoolean(JAVASCRIPT_ENABLED)
-        this.settings.domStorageEnabled = savedState.getBoolean(DOM_STORAGE_ENABLED)
+        ultraListEnabled = savedState.getBoolean(ULTRALIST_ENABLED)
+        ultraPrivacyEnabled = savedState.getBoolean(ULTRAPRIVACY_ENABLED)
         this.settings.userAgentString = savedState.getString(USER_AGENT)
         this.settings.useWideViewPort = savedState.getBoolean(WIDE_VIEWPORT)
-        this.settings.textZoom = savedState.getInt(FONT_SIZE)
     }
 
 
@@ -559,4 +539,4 @@ class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeS
         // Dispatch a nested fling with the specified velocity.
         return nestedScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed)
     }
-}
\ No newline at end of file
+}