From b664d689b9757ffd1fd224c85a9659d2f4df1204 Mon Sep 17 00:00:00 2001
From: Soren Stoutner <soren@stoutner.com>
Date: Wed, 4 Oct 2023 12:11:12 -0700
Subject: [PATCH] Prevent the wrong page being displayed on restart. 
 https://redmine.stoutner.com/issues/1051

---
 app/build.gradle                                  |  6 +++---
 app/src/main/assets/it/about_changelog.html       |  2 +-
 .../activities/MainWebViewActivity.kt             | 15 +++++++++------
 .../helpers/BookmarksDatabaseHelper.kt            |  2 +-
 build.gradle                                      |  2 +-
 5 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/app/build.gradle b/app/build.gradle
index ce9b5ec1..4e970eb1 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -85,14 +85,14 @@ dependencies {
     implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
     implementation 'androidx.core:core-ktx:1.10.1'
     implementation 'androidx.drawerlayout:drawerlayout:1.2.0'
-    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'
-    implementation 'androidx.preference:preference-ktx:1.2.0'
+    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2'
+    implementation 'androidx.preference:preference-ktx:1.2.1'
     implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
     implementation 'androidx.viewpager:viewpager:1.0.0'
     implementation 'androidx.webkit:webkit:1.7.0'
 
     // Include the Kotlin standard library.  This should be the same version number listed in project build.gradle.
-    implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.8.10'
+    implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.8.22'
 
     // Include the Google material library.
     implementation 'com.google.android.material:material:1.9.0'
diff --git a/app/src/main/assets/it/about_changelog.html b/app/src/main/assets/it/about_changelog.html
index 5fe05f99..368fb0cd 100644
--- a/app/src/main/assets/it/about_changelog.html
+++ b/app/src/main/assets/it/about_changelog.html
@@ -34,7 +34,7 @@
         <ul>
             <li>Sistemazione di un crash che avveniva muovendo un segnalibro <a href="https://redmine.stoutner.com/issues/1028">nella cartella principale</a>.</li>
             <li>Utilizzo della segreta e non documentata <a href="https://redmine.stoutner.com/issues/1023">funzionalità di visione della sorgente</a> di WebView.</li>
-            <li>Fix Domain Settings sometimes being created as <a href="https://redmine.stoutner.com/issues/1032">Enabled instead of System Default</a>.</li>
+            <li>Sistemazione delle impostazioni dei domini che a volte erano salvate come <a href="https://redmine.stoutner.com/issues/1032">Abilitate invece che come Default di Sistema</a>.</li>
             <li>Spostamento dello <a href="https://redmine.stoutner.com/issues/1011">User Agent al di sotto del DOM Storage</a> nell'elenco delle impostazioni dei domini.</li>
             <li>Creazione di una <a href="https://redmine.stoutner.com/issues/672">speciale finestra di dialogo di cifratura</a> per gli URL di contenuto.</li>
             <li>Sistemazione di un crash che avveniva quando Privacy Browser era riavviato mentre era visualizzata
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 de608492..1e41ca20 100644
--- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt
+++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.kt
@@ -4207,11 +4207,14 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook
                 // Close the find on page bar if it is open.
                 closeFindOnPage(null)
 
-                // Select the same page in the view pager.
-                webViewViewPager2.currentItem = tab.position
+                // Update the view pager when it has quiesced.  Otherwise, if a page launched by a new intent on restart has not yet been created, the view pager will not be updated to match the tab layout.
+                webViewViewPager2.post {
+                    // Select the same page in the view pager.
+                    webViewViewPager2.currentItem = tab.position
 
-                // Set the current WebView.
-                setCurrentWebView(tab.position)
+                    // Set the current WebView.
+                    setCurrentWebView(tab.position)
+                }
             }
 
             override fun onTabUnselected(tab: TabLayout.Tab) {}
@@ -4223,8 +4226,8 @@ class MainWebViewActivity : AppCompatActivity(), CreateBookmarkDialog.CreateBook
                     // Calculate the milliseconds since the last restart.  This can be replaced by the simpler LocalDateTime once the minimum API >= 26.
                     val millisecondsSinceLastRestart = Date().time - restartTime.time
 
-                    // Only display the SSL certificate dialog if it has been at least 2 seconds since the last restart as deep restarts sometimes end up selecting a tab twice.
-                    if (millisecondsSinceLastRestart > 2000) {
+                    // Only display the SSL certificate dialog if it has been at least 3 seconds since the last restart as deep restarts sometimes end up selecting a tab twice.
+                    if (millisecondsSinceLastRestart > 3000) {
                         // Instantiate the View SSL Certificate dialog.
                         val viewSslCertificateDialogFragment: DialogFragment = ViewSslCertificateDialog.displayDialog(currentWebView!!.webViewFragmentId, currentWebView!!.getFavoriteIcon())
 
diff --git a/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.kt b/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.kt
index c4c8ebb0..6241b49f 100644
--- a/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.kt
+++ b/app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.kt
@@ -885,7 +885,7 @@ class BookmarksDatabaseHelper(context: Context) : SQLiteOpenHelper(context, BOOK
     }
 
     private fun generateFolderId(): Long {
-        // Get the current time in epoch format.
+        // Get the current time in epoch format (in milliseconds).
         val possibleFolderId = Date().time
 
         // Get a readable database.
diff --git a/build.gradle b/build.gradle
index 64d9b31b..f5007a0a 100644
--- a/build.gradle
+++ b/build.gradle
@@ -27,7 +27,7 @@ buildscript {
 
     dependencies {
         classpath 'com.android.tools.build:gradle:8.0.2'
-        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.10"
+        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.22"
 
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
-- 
2.47.2