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=11af7b7fc357718638e46ee3cb4a976d07993361;hp=5d9ad3a4a76f6f7d746c9dfc6148e0ebb7fe8467;hb=ff2f3992b7ecddb804a96322789bd35010529f43;hpb=e917bd2fc33983d1a3194a032b1f2a1cc3879502 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..11af7b7 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-2022 Soren Stoutner . * * This file is part of Privacy Cell . * @@ -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) + } } } @@ -292,4 +307,4 @@ class RealtimeMonitoringService : Service() { } } } -} \ No newline at end of file +}