]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blobdiff - app/src/main/java/com/stoutner/privacybrowser/views/NestedScrollWebView.kt
Migrate the remaining classes to Kotlin. https://redmine.stoutner.com/issues/989
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / views / NestedScrollWebView.kt
index 4011f5a5aba4fe689a628fbd8523afa306e634f6..aa2ea0984035fe561888abcce7f505b635d36f16 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2019-2023 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
  *
@@ -31,7 +31,7 @@ import android.webkit.HttpAuthHandler
 import android.webkit.SslErrorHandler
 import android.webkit.WebView
 
-import androidx.core.content.ContextCompat
+import androidx.appcompat.content.res.AppCompatResources.getDrawable
 import androidx.core.view.NestedScrollingChild2
 import androidx.core.view.NestedScrollingChildHelper
 import androidx.core.view.ViewCompat
@@ -44,9 +44,17 @@ import java.util.Date
 
 import kotlin.collections.ArrayList
 
-import kotlin.jvm.JvmOverloads
-
-// Define the saved state constants.
+// 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 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"
@@ -80,17 +88,6 @@ private const val FONT_SIZE = "font_size"
 // 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
@@ -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,7 +134,6 @@ 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.
@@ -147,22 +143,27 @@ class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeS
         initializeFavoriteIcon()
     }
 
-
     // 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)
+        // Get the default favorite icon drawable.
+        val favoriteIconDrawable = getDrawable(context, R.drawable.world)
 
         // Cast the favorite icon drawable to a bitmap drawable.
         val favoriteIconBitmapDrawable = (favoriteIconDrawable as BitmapDrawable?)!!
 
         // Store the default icon bitmap.
-        favoriteOrDefaultIcon = favoriteIconBitmapDrawable.bitmap
+        favoriteIcon = favoriteIconBitmapDrawable.bitmap
+
+        // 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) {
+    fun setFavoriteIcon(icon: Bitmap) {
+        // Store the current favorite icon height.
+        favoriteIconHeight = icon.height
+
         // 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) {
+        favoriteIcon = if (icon.height > 256 || icon.width > 256) {
             Bitmap.createScaledBitmap(icon, 256, 256, true)
         } else {
             // Store the icon as presented.
@@ -170,11 +171,15 @@ class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeS
         }
     }
 
-    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() {
@@ -296,24 +301,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,7 +389,7 @@ 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()
@@ -442,6 +429,7 @@ class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeS
         return savedState
     }
 
+    // Restore the state.
     fun restoreNestedScrollWebViewState(savedState: Bundle) {
         // Restore the class variables.
         domainSettingsApplied = savedState.getBoolean(DOMAIN_SETTINGS_APPLIED)
@@ -559,4 +547,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
+}