]> gitweb.stoutner.com Git - PrivacyCell.git/blobdiff - app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt
Fix problems with the realtime notifications. https://redmine.stoutner.com/issues/764
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / services / RealtimeMonitoringService.kt
index 2d3b184abb3f56bad08b6fb3b65091b245ca285f..681a3da2b41ccb1c8f0d88f30d685533a19c790b 100644 (file)
@@ -57,18 +57,19 @@ class RealtimeMonitoringService : Service() {
 
     // Define the class variables.
     private var currentStatus = ""
+    private lateinit var phoneStateListener: PhoneStateListener
 
     inner class ServiceBinder : Binder() {
         // Get a copy of this service as a binder.
         fun getService(): RealtimeMonitoringService = this@RealtimeMonitoringService
     }
 
-    override fun onBind(intent: Intent): IBinder {
+    override fun onBind(intent: Intent?): IBinder {
         // Return a copy of the service binder.
         return ServiceBinder()
     }
 
-    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
+    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
         // Get a handle for the notification manager.
         val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
 
@@ -123,34 +124,8 @@ class RealtimeMonitoringService : Service() {
         // Start the foreground notification.
         startForeground(NOTIFICATION_ID, notificationBuilder.build())
 
-        // Register the telephony manager listener.
-        registerTelephonyManagerListener()
-
-        // Create a register realtime listener work request that fires every 15 minutes with a 1 minute initial delay.
-        val registerRealtimeListenerWorkRequest = PeriodicWorkRequestBuilder<RegisterRealtimeListener>(15, TimeUnit.MINUTES).setInitialDelay(1, TimeUnit.MINUTES).build()
-
-        // Register the realtime listener work request.
-        WorkManager.getInstance(this).enqueueUniquePeriodicWork(getString(R.string.register_listener_work_request), ExistingPeriodicWorkPolicy.REPLACE, registerRealtimeListenerWorkRequest)
-
-        // Return a sticky service.
-        return START_STICKY
-    }
-
-    fun registerTelephonyManagerListener() {
-        // Get a handle for the telephony manager.
-        val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
-
-        // Get a handle for the notification manager.
-        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
-
-        // Create an intent to open Privacy Cell.
-        val privacyCellIntent = Intent(this, PrivacyCellActivity::class.java)
-
-        // Create a pending intent from the Privacy Cell intent.
-        val privacyCellPendingIntent = PendingIntent.getActivity(this, 0, privacyCellIntent, PendingIntent.FLAG_IMMUTABLE)
-
-        // Listen for changes to the phone state.
-        telephonyManager.listen(object : PhoneStateListener() {
+        // Define the phone state listener.
+        phoneStateListener = object : PhoneStateListener() {
             override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
                 // Populate the notification according to the network type.
                 if (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_NR) {  // This is a secure 5G NR SA network.
@@ -179,7 +154,7 @@ class RealtimeMonitoringService : Service() {
                     }
                 } else {  // This is not a secure 5G NR SA network.
                     // Only update the notification if the network status has changed.
-                    if (currentStatus !=INSECURE_NETWORK) {
+                    if (currentStatus != INSECURE_NETWORK) {
                         // Create an insecure network notification builder.
                         val insecureNetworkNotificationBuilder = Notification.Builder(applicationContext, INSECURE_NETWORK)
 
@@ -203,6 +178,27 @@ class RealtimeMonitoringService : Service() {
                     }
                 }
             }
-        }, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)
+        }
+
+        // Create a register realtime listener work request that fires every hour.  For some reason, when the service launches it will initially register the listener and then unregister it.
+        // This periodic request will fire shortly thereafter (it fires about every hour near the beginning of the hour) and will reregister the listener, which will stick this time.
+        val registerRealtimeListenerWorkRequest = PeriodicWorkRequestBuilder<RegisterRealtimeListener>(1, TimeUnit.HOURS).build()
+
+        // Register the realtime listener work request.
+        WorkManager.getInstance(this).enqueueUniquePeriodicWork(getString(R.string.register_listener_work_request), ExistingPeriodicWorkPolicy.REPLACE, registerRealtimeListenerWorkRequest)
+
+        // Return a sticky service.
+        return START_STICKY
+    }
+
+    fun registerTelephonyManagerListener() {
+        // Get a handle for the telephony manager.
+        val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
+
+        // Cancel the current listener if it exists.
+        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)
+
+        // Listen for changes to the phone state.
+        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)
     }
 }
\ No newline at end of file