X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyCell.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacycell%2Fservices%2FRealtimeMonitoringService.kt;h=c54e290813c07abd015b1f3d0443dc7afdc45317;hp=11af7b7fc357718638e46ee3cb4a976d07993361;hb=HEAD;hpb=ff2f3992b7ecddb804a96322789bd35010529f43 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 11af7b7..72ace81 100644 --- a/app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt +++ b/app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt @@ -1,5 +1,5 @@ /* - * Copyright 2021-2022 Soren Stoutner . + * Copyright 2021-2023 Soren Stoutner . * * This file is part of Privacy Cell . * @@ -148,6 +148,9 @@ class RealtimeMonitoringService : Service() { // Set the color. notificationBuilder.setColor(getColor(R.color.red_notification_icon)) + // Prevent swiping to dismiss the notification. This no longer works (except on the lock screen) in API >= 34. + notificationBuilder.setOngoing(true) + // Start the foreground notification. startForeground(NOTIFICATION_ID, notificationBuilder.build()) @@ -201,9 +204,9 @@ class RealtimeMonitoringService : Service() { // Check to see if the read phone state permission has been granted. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { - // Create a register realtime listener work request that fires every hour. - // This periodic request will fire shortly after being created (it fires about every hour near the beginning of the hour) and will reregister the listener if it gets garbage collected. - val registerRealtimeListenerWorkRequest = PeriodicWorkRequestBuilder(1, TimeUnit.HOURS).build() + // Create a register realtime listener work request that fires every fifteen minutes. + // This periodic request will fire shortly after being created and will reregister the listener if it gets garbage collected. + val registerRealtimeListenerWorkRequest = PeriodicWorkRequestBuilder(15, TimeUnit.MINUTES).build() // Register the realtime listener work request. WorkManager.getInstance(this).enqueueUniquePeriodicWork(getString(R.string.register_listener_work_request), ExistingPeriodicWorkPolicy.REPLACE, registerRealtimeListenerWorkRequest) @@ -232,10 +235,16 @@ class RealtimeMonitoringService : Service() { } fun populateNotification() { + // Get the list of current notifications. + val activeNotificationsArray = notificationManager.activeNotifications + + // Check to see if there is a current notification. + val noCurrentNotification = activeNotificationsArray.isEmpty() + // Populate the notification according to the security status. if ((voiceNetworkSecurityStatus == ProtocolHelper.ANTIQUATED) || (dataNetworkSecurityStatus == ProtocolHelper.ANTIQUATED)) { // This is an antiquated network. // Only update the notification if the network status has changed. - if (currentStatus != ANTIQUATED_NETWORK) { + if ((currentStatus != ANTIQUATED_NETWORK) || noCurrentNotification) { // Create an antiquated network notification builder. val antiquatedNetworkNotificationBuilder = Notification.Builder(applicationContext, ANTIQUATED_NETWORK) @@ -251,6 +260,9 @@ class RealtimeMonitoringService : Service() { // Set the color. antiquatedNetworkNotificationBuilder.setColor(getColor(R.color.red_notification_icon)) + // Prevent swiping to dismiss the notification. This no longer works (except on the lock screen) in API >= 34. + antiquatedNetworkNotificationBuilder.setOngoing(true) + // Update the notification. notificationManager.notify(NOTIFICATION_ID, antiquatedNetworkNotificationBuilder.build()) @@ -259,7 +271,7 @@ class RealtimeMonitoringService : Service() { } } else if ((voiceNetworkSecurityStatus == ProtocolHelper.INSECURE) || (dataNetworkSecurityStatus == ProtocolHelper.INSECURE)) { // This is an insecure network. // Only update the notification if the network status has changed. - if (currentStatus != INSECURE_NETWORK) { + if ((currentStatus != INSECURE_NETWORK) || noCurrentNotification) { // Create an insecure network notification builder. val insecureNetworkNotificationBuilder = Notification.Builder(applicationContext, INSECURE_NETWORK) @@ -275,6 +287,9 @@ class RealtimeMonitoringService : Service() { // Set the color. insecureNetworkNotificationBuilder.setColor(getColor(R.color.yellow_notification_icon)) + // Prevent swiping to dismiss the notification. This no longer works (except on the lock screen) in API >= 34. + insecureNetworkNotificationBuilder.setOngoing(true) + // Update the notification. notificationManager.notify(NOTIFICATION_ID, insecureNetworkNotificationBuilder.build()) @@ -283,7 +298,7 @@ class RealtimeMonitoringService : Service() { } } else { // This is a secure network. // Only update the notification if the network status has changed. - if (currentStatus != SECURE_NETWORK) { + if ((currentStatus != SECURE_NETWORK) || noCurrentNotification) { // Create a secure network notification builder. val secureNetworkNotificationBuilder = Notification.Builder(applicationContext, SECURE_NETWORK) @@ -299,6 +314,9 @@ class RealtimeMonitoringService : Service() { // Set the color. secureNetworkNotificationBuilder.setColor(getColor(R.color.blue_icon)) + // Prevent swiping to dismiss the notification. This no longer works (except on the lock screen) in API >= 34. + secureNetworkNotificationBuilder.setOngoing(true) + // Update the notification. notificationManager.notify(NOTIFICATION_ID, secureNetworkNotificationBuilder.build())