]> 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 1eefe7378006ea496de2bdba521b0da9b26dc303..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>.
  *
@@ -27,11 +27,11 @@ import android.graphics.drawable.BitmapDrawable
 import android.os.Bundle
 import android.util.AttributeSet
 import android.view.MotionEvent
-import android.webkit.WebView
-import android.webkit.SslErrorHandler
 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 = ""
@@ -146,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.
@@ -169,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() {
@@ -208,22 +214,15 @@ class NestedScrollWebView @JvmOverloads constructor(context: Context, attributeS
         hasPinnedSslCertificate = true
     }
 
-    fun getPinnedSslCertificate(): ArrayList<Any> {
-        // Initialize an array list.
-        val arrayList = ArrayList<Any>()
-
+    fun getPinnedSslCertificate(): Pair<Array<String>, Array<Date>> {
         // Create the SSL certificate string array.
         val sslCertificateStringArray = arrayOf(pinnedSslIssuedToCName, pinnedSslIssuedToOName, pinnedSslIssuedToUName, pinnedSslIssuedByCName, pinnedSslIssuedByOName, pinnedSslIssuedByUName)
 
         // Create the SSL certificate date array.
         val sslCertificateDateArray = arrayOf(pinnedSslStartDate, pinnedSslEndDate)
 
-        // Add the arrays to the array list.
-        arrayList.add(sslCertificateStringArray)
-        arrayList.add(sslCertificateDateArray)
-
-        // Return the pinned SSL certificate array list.
-        return arrayList
+        // Return the pinned SSL certificate pair.
+        return Pair(sslCertificateStringArray, sslCertificateDateArray)
     }
 
     fun clearPinnedSslCertificate() {
@@ -390,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()
@@ -430,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)
@@ -547,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
+}