From 6c4410a4a8c146b4e76b872b66bb8880b8f85b71 Mon Sep 17 00:00:00 2001
From: Soren Stoutner <soren@stoutner.com>
Date: Tue, 21 Jun 2022 11:33:13 -0700
Subject: [PATCH] Rix notifications reenabling.
 https://redmine.stoutner.com/issues/864

---
 app/build.gradle                              |  2 +-
 app/src/main/assets/de/changelog.html         |  4 +-
 app/src/main/assets/en/changelog.html         |  4 +-
 app/src/main/assets/es/changelog.html         |  4 +-
 app/src/main/assets/fr/changelog.html         |  4 +-
 app/src/main/assets/it/changelog.html         |  4 +-
 app/src/main/assets/ru/changelog.html         |  4 +-
 .../services/RealtimeMonitoringService.kt     | 53 ++++++++++++-------
 build.gradle                                  |  2 +-
 9 files changed, 48 insertions(+), 33 deletions(-)

diff --git a/app/build.gradle b/app/build.gradle
index cfe53cd..e9ea6d2 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -59,7 +59,7 @@ dependencies {
     implementation 'androidx.work:work-runtime-ktx:2.7.1'
 
     // Include the Kotlin standard library.  This should be the same version number listed in the project build.gradle.
-    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.21'
+    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.0'
 
     // Include the Google material library.
     implementation 'com.google.android.material:material:1.6.1'
diff --git a/app/src/main/assets/de/changelog.html b/app/src/main/assets/de/changelog.html
index a2c7fb6..b761189 100644
--- a/app/src/main/assets/de/changelog.html
+++ b/app/src/main/assets/de/changelog.html
@@ -29,8 +29,8 @@
     </head>
 
     <body>
-        <h3>1.8 (Version Code 9)</h3>
-        <p>14. Juni 2022 - Mindest-API 30, Ziel-API 32</p>
+        <h3><a href="https://www.stoutner.com/privacy-cell-1-8/">1.8</a> (Version Code 9)</h3>
+        <p><a href="https://gitweb.stoutner.com/?p=PrivacyCell.git;a=commitdiff;h=f60b140868c2f0d564657e96d48508b4fbdc546b">14. Juni 2022</a> - Mindest-API 30, Ziel-API 32</p>
         <ul>
             <li>Der <a href="https://redmine.stoutner.com/issues/851">Sprach-Netzwerk-Status</a> wird nun bei der Einschätzung der Gesamt-Sicherheit des Funknetzwerks berücksichtigt.</li>
             <li>Fehler behoben, durch den gelegentlich <a href="https://redmine.stoutner.com/issues/850">Benachrichtigungen nicht abgeschaltet werden konnten</a>.</li>
diff --git a/app/src/main/assets/en/changelog.html b/app/src/main/assets/en/changelog.html
index b253e64..0558159 100644
--- a/app/src/main/assets/en/changelog.html
+++ b/app/src/main/assets/en/changelog.html
@@ -27,8 +27,8 @@
     </head>
 
     <body>
-        <h3>1.8 (version code 9)</h3>
-        <p>14 June 2022 - minimum API 30, target API 32</p>
+        <h3><a href="https://www.stoutner.com/privacy-cell-1-8/">1.8</a> (version code 9)</h3>
+        <p><a href="https://gitweb.stoutner.com/?p=PrivacyCell.git;a=commitdiff;h=f60b140868c2f0d564657e96d48508b4fbdc546b">14 June 2022</a> - minimum API 30, target API 32</p>
         <ul>
             <li>Include the <a href="https://redmine.stoutner.com/issues/851">voice network status</a> in determining the overall security of the cell network.</li>
             <li>Fix a bug that sometimes <a href="https://redmine.stoutner.com/issues/850">prevented notifications from being disabled</a>.</li>
diff --git a/app/src/main/assets/es/changelog.html b/app/src/main/assets/es/changelog.html
index 7336a95..1bf7ded 100644
--- a/app/src/main/assets/es/changelog.html
+++ b/app/src/main/assets/es/changelog.html
@@ -29,8 +29,8 @@
     </head>
 
     <body>
-        <h3>1.8 (versión del código 9)</h3>
-        <p>14 de junio de 2022 - API mínimo 30, API objetivo 32</p>
+        <h3><a href="https://www.stoutner.com/privacy-cell-1-8/">1.8</a> (versión del código 9)</h3>
+        <p><a href="https://gitweb.stoutner.com/?p=PrivacyCell.git;a=commitdiff;h=f60b140868c2f0d564657e96d48508b4fbdc546b">14 de junio de 2022</a> - API mínimo 30, API objetivo 32</p>
         <ul>
             <li>Incluir el <a href="https://redmine.stoutner.com/issues/851">estado de la red de voz</a> para determinar la seguridad general de la red celular.</li>
             <li>Corregir un error que a veces <a href="https://redmine.stoutner.com/issues/850">impedía desactivar las notificaciones</a>.</li>
diff --git a/app/src/main/assets/fr/changelog.html b/app/src/main/assets/fr/changelog.html
index 25415db..9ff5061 100644
--- a/app/src/main/assets/fr/changelog.html
+++ b/app/src/main/assets/fr/changelog.html
@@ -29,8 +29,8 @@
     </head>
 
     <body>
-        <h3>1.8 (version de code 9)</h3>
-        <p>14 June 2022 - API minimum 30, API cible 32</p>
+        <h3><a href="https://www.stoutner.com/privacy-cell-1-8/">1.8</a> (version de code 9)</h3>
+        <p><a href="https://gitweb.stoutner.com/?p=PrivacyCell.git;a=commitdiff;h=f60b140868c2f0d564657e96d48508b4fbdc546b">14 June 2022</a> - API minimum 30, API cible 32</p>
         <ul>
             <li>Include the <a href="https://redmine.stoutner.com/issues/851">voice network status</a> in determining the overall security of the cell network.</li>
             <li>Fix a bug that sometimes <a href="https://redmine.stoutner.com/issues/850">prevented notifications from being disabled</a>.</li>
diff --git a/app/src/main/assets/it/changelog.html b/app/src/main/assets/it/changelog.html
index 24cde78..a7b4bc9 100644
--- a/app/src/main/assets/it/changelog.html
+++ b/app/src/main/assets/it/changelog.html
@@ -29,8 +29,8 @@
     </head>
 
     <body>
-        <h3>1.8 (versione codice 9)</h3>
-        <p>14 Giugno 2022 - minima API 30, target API 32</p>
+        <h3><a href="https://www.stoutner.com/privacy-cell-1-8/">1.8</a> (versione codice 9)</h3>
+        <p><a href="https://gitweb.stoutner.com/?p=PrivacyCell.git;a=commitdiff;h=f60b140868c2f0d564657e96d48508b4fbdc546b">14 Giugno 2022</a> - minima API 30, target API 32</p>
         <ul>
             <li>Incluso lo <a href="https://redmine.stoutner.com/issues/851">stato della rete voce</a> nella determinazione della sicurezza complessiva della rete.</li>
             <li>Correzione di un baco che talvolta <a href="https://redmine.stoutner.com/issues/850">impediva di disabilitare le notifiche</a>.</li>
diff --git a/app/src/main/assets/ru/changelog.html b/app/src/main/assets/ru/changelog.html
index feb457f..85e82bc 100644
--- a/app/src/main/assets/ru/changelog.html
+++ b/app/src/main/assets/ru/changelog.html
@@ -27,8 +27,8 @@
     </head>
 
     <body>
-        <h3>1.8 (код версии 9)</h3>
-        <p>14 июня 2022 года - минимальный API 30, целевой API 32</p>
+        <h3><a href="https://www.stoutner.com/privacy-cell-1-8/">1.8</a> (код версии 9)</h3>
+        <p><a href="https://gitweb.stoutner.com/?p=PrivacyCell.git;a=commitdiff;h=f60b140868c2f0d564657e96d48508b4fbdc546b">14 июня 2022 года</a> - минимальный API 30, целевой API 32</p>
         <ul>
             <li>Теперь при определении общей безопасности сотовой сети <a href="https://redmine.stoutner.com/issues/851">будет учитываться статус голосовой сети</a>.</li>
             <li>Исправлена ошибка, из-за которой иногда <a href="https://redmine.stoutner.com/issues/850">не удавалось отключить уведомления</a>.</li>
diff --git a/app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt b/app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt
index 5d9ad3a..c54e290 100644
--- a/app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt
+++ b/app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt
@@ -154,33 +154,48 @@ class RealtimeMonitoringService : Service() {
             // Instantiate the protocol helper.
             val protocolHelper = ProtocolHelper()
 
+            // Get a handle for the telephony manager.
+            val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
+
             // Define the phone state listener.  The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
             phoneStateListener = object : PhoneStateListener() {
                 @Deprecated("Deprecated in Java")
                 override fun onServiceStateChanged(serviceState: ServiceState) {  // Update the voice network status.
-                    // Get the network registration info for the voice network, which is the second of the three entries (the first appears to be Wi-Fi and the third appears to be the cell data network).
-                    val networkRegistrationInfo = serviceState.networkRegistrationInfoList[1]
-
-                    // Get the consider 3G antiquated preference.
-                    val consider3gAntiquated = sharedPreferences.getBoolean(getString(R.string.consider_3g_antiquated_key), false)
-
-                    // Update the voice network security status.
-                    voiceNetworkSecurityStatus = protocolHelper.checkNetwork(networkRegistrationInfo.accessNetworkTechnology, consider3gAntiquated)
-
-                    // Populate the notification.
-                    populateNotification()
+                    // Check to see if realtime monitoring is enabled.  Sometimes the system keeps running the service even when it is supposed to shut down.
+                    if (sharedPreferences.getBoolean(applicationContext.getString(R.string.realtime_monitoring_key), true)) {  // Realtime monitoring is enabled.
+                        // Get the network registration info for the voice network, which is the second of the three entries (the first appears to be Wi-Fi and the third appears to be the cell data network).
+                        val networkRegistrationInfo = serviceState.networkRegistrationInfoList[1]
+
+                        // Get the consider 3G antiquated preference.
+                        val consider3gAntiquated = sharedPreferences.getBoolean(getString(R.string.consider_3g_antiquated_key), false)
+
+                        // Update the voice network security status.
+                        voiceNetworkSecurityStatus = protocolHelper.checkNetwork(networkRegistrationInfo.accessNetworkTechnology, consider3gAntiquated)
+
+                        // Populate the notification.
+                        populateNotification()
+                    } else {  // Realtime monitoring is disabled.
+                        // Cancel the current listener if it exists.  The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
+                        telephonyManager.listen(phoneStateListener, LISTEN_NONE)
+                    }
                 }
 
                 @Deprecated("Deprecated in Java")
                 override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {  // Update the data network status.
-                    // Get the consider 3G antiquated preference.
-                    val consider3gAntiquated = sharedPreferences.getBoolean(getString(R.string.consider_3g_antiquated_key), false)
-
-                    // Update the data network security status.
-                    dataNetworkSecurityStatus = protocolHelper.checkNetwork(telephonyDisplayInfo.networkType, consider3gAntiquated)
-
-                    // Populate the notification.
-                    populateNotification()
+                    // Check to see if realtime monitoring is enabled.  Sometimes the system keeps running the service even when it is supposed to shut down.
+                    if (sharedPreferences.getBoolean(applicationContext.getString(R.string.realtime_monitoring_key), true)) {  // Realtime monitoring is enabled.
+                        // Get the consider 3G antiquated preference.
+                        val consider3gAntiquated = sharedPreferences.getBoolean(getString(R.string.consider_3g_antiquated_key), false)
+
+                        // Update the data network security status.
+                        dataNetworkSecurityStatus = protocolHelper.checkNetwork(telephonyDisplayInfo.networkType, consider3gAntiquated)
+
+                        // Populate the notification.
+                        populateNotification()
+                    } else {  // Realtime monitoring is disabled.
+                        // Cancel the current listener if it exists.  The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
+                        telephonyManager.listen(phoneStateListener, LISTEN_NONE)
+                    }
                 }
             }
 
diff --git a/build.gradle b/build.gradle
index a994e46..7083cd3 100644
--- a/build.gradle
+++ b/build.gradle
@@ -26,7 +26,7 @@ buildscript {
 
     dependencies {
         classpath 'com.android.tools.build:gradle:7.2.1'
-        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21"
+        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"
 
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
-- 
2.47.2