// Get a handle for the shared preferences.
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
- // Get a handle for the notification manager.
- val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
+ // Check to see if realtime monitoring is enabled. Sometimes the shared preferences can't return a value in time, because Android sucks.
+ // So, the default value is set to true, which is the safest value if the shared preferences can't be queried.
+ if (sharedPreferences.getBoolean(applicationContext.getString(R.string.realtime_monitoring_key), true)) { // Realtime monitoring is enabled.
+ // Get a handle for the notification manager.
+ val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
+
+ // Create a notification channel group.
+ notificationManager.createNotificationChannelGroup(NotificationChannelGroup(REALTIME_MONITORING, getString(R.string.realtime_monitoring)))
+
+ // Prepare the notification channels.
+ val secureNetworkChannel = NotificationChannel(SECURE_NETWORK, getString(R.string.secure_network_channel), NotificationManager.IMPORTANCE_HIGH)
+ val insecureNetworkChannel = NotificationChannel(INSECURE_NETWORK, getString(R.string.insecure_network_channel), NotificationManager.IMPORTANCE_HIGH)
+ val antiquatedNetworkChannel = NotificationChannel(ANTIQUATED_NETWORK, getString(R.string.antiquated_network_channel), NotificationManager.IMPORTANCE_HIGH)
+ val unknownNetworkChannel = NotificationChannel(UNKNOWN_NETWORK, getString(R.string.unknown_network_channel), NotificationManager.IMPORTANCE_LOW)
+
+ // Set the notification channel group.
+ secureNetworkChannel.group = REALTIME_MONITORING
+ insecureNetworkChannel.group = REALTIME_MONITORING
+ antiquatedNetworkChannel.group = REALTIME_MONITORING
+ unknownNetworkChannel.group = REALTIME_MONITORING
+
+ // Disable the notification dots.
+ secureNetworkChannel.setShowBadge(false)
+ insecureNetworkChannel.setShowBadge(false)
+ antiquatedNetworkChannel.setShowBadge(false)
+ unknownNetworkChannel.setShowBadge(false)
+
+ // Set the primary channel notifications to be public.
+ secureNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
+ insecureNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
+ antiquatedNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
+
+ // Create the notification channels.
+ notificationManager.createNotificationChannel(secureNetworkChannel)
+ notificationManager.createNotificationChannel(insecureNetworkChannel)
+ notificationManager.createNotificationChannel(antiquatedNetworkChannel)
+ notificationManager.createNotificationChannel(unknownNetworkChannel)
+
+ // Create a notification builder.
+ val notificationBuilder = Notification.Builder(this, UNKNOWN_NETWORK)
+
+ // 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)
+
+ // Set the notification to open Privacy Cell.
+ notificationBuilder.setContentIntent(privacyCellPendingIntent)
+
+ // Set the notification text.
+ notificationBuilder.setContentText(getString(R.string.unknown_network))
+
+ // Set the notification icon.
+ notificationBuilder.setSmallIcon(R.drawable.antiquated_notification_enabled)
+
+ // Set the color.
+ notificationBuilder.setColor(getColor(R.color.red_notification_icon))
+
+ // Start the foreground notification.
+ startForeground(NOTIFICATION_ID, notificationBuilder.build())
+
+ // 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 onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
+ // Get the consider 3G antiquated preference.
+ val consider3gAntiquated = sharedPreferences.getBoolean(getString(R.string.consider_3g_antiquated_key), false)
+
+ // Populate the notification according to the network type.
+ if ((telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_NR) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_IWLAN) ||
+ (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_UNKNOWN)) { // This is a secure network.
+ // Only update the notification if the network status has changed.
+ if (currentStatus != SECURE_NETWORK) {
+ // Create a secure network notification builder.
+ val secureNetworkNotificationBuilder = Notification.Builder(applicationContext, SECURE_NETWORK)
+
+ // Set the notification to open Privacy Cell.
+ secureNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent)
+
+ // Set the notification text.
+ secureNetworkNotificationBuilder.setContentText(getString(R.string.secure_network))
+
+ // Set the notification icon.
+ secureNetworkNotificationBuilder.setSmallIcon(R.drawable.secure_notification_enabled)
+
+ // Set the color.
+ secureNetworkNotificationBuilder.setColor(getColor(R.color.blue_icon))
+
+ // Update the notification.
+ notificationManager.notify(NOTIFICATION_ID, secureNetworkNotificationBuilder.build())
+
+ // Store the new network status.
+ currentStatus = SECURE_NETWORK
+ }
+ } else if ((telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_LTE) || (!consider3gAntiquated && (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_1xRTT ||
+ (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EVDO_0) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EVDO_A) ||
+ (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EVDO_B) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EHRPD) ||
+ (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_UMTS) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_TD_SCDMA) ||
+ (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSDPA) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSUPA) ||
+ (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSPA) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSPAP)))) {
+ // This is an insecure network.
+ // Only update the notification if the network status has changed.
+ if (currentStatus != INSECURE_NETWORK) {
+ // Create an insecure network notification builder.
+ val insecureNetworkNotificationBuilder = Notification.Builder(applicationContext, INSECURE_NETWORK)
+
+ // Set the notification to open Privacy Cell.
+ insecureNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent)
+
+ // Set the notification text.
+ insecureNetworkNotificationBuilder.setContentText(getString(R.string.insecure_network))
+
+ // Set the notification icon.
+ insecureNetworkNotificationBuilder.setSmallIcon(R.drawable.insecure_notification_enabled)
+
+ // Set the color.
+ insecureNetworkNotificationBuilder.setColor(getColor(R.color.yellow_notification_icon))
+
+ // Update the notification.
+ notificationManager.notify(NOTIFICATION_ID, insecureNetworkNotificationBuilder.build())
+
+ // Store the new network status.
+ currentStatus = INSECURE_NETWORK
+ }
+ } else { // This is an antiquated network.
+ // Only update the notification if the network status has changed.
+ if (currentStatus != ANTIQUATED_NETWORK) {
+ // Create an antiquated network notification builder.
+ val antiquatedNetworkNotificationBuilder = Notification.Builder(applicationContext, ANTIQUATED_NETWORK)
- // Create a notification channel group.
- notificationManager.createNotificationChannelGroup(NotificationChannelGroup(REALTIME_MONITORING, getString(R.string.realtime_monitoring)))
+ // Set the notification to open Privacy Cell.
+ antiquatedNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent)
+
+ // Set the notification text.
+ antiquatedNetworkNotificationBuilder.setContentText(getString(R.string.antiquated_network))
- // Prepare the notification channels.
- val secureNetworkChannel = NotificationChannel(SECURE_NETWORK, getString(R.string.secure_network_channel), NotificationManager.IMPORTANCE_HIGH)
- val insecureNetworkChannel = NotificationChannel(INSECURE_NETWORK, getString(R.string.insecure_network_channel), NotificationManager.IMPORTANCE_HIGH)
- val antiquatedNetworkChannel = NotificationChannel(ANTIQUATED_NETWORK, getString(R.string.antiquated_network_channel), NotificationManager.IMPORTANCE_HIGH)
- val unknownNetworkChannel = NotificationChannel(UNKNOWN_NETWORK, getString(R.string.unknown_network_channel), NotificationManager.IMPORTANCE_LOW)
+ // Set the notification icon.
+ antiquatedNetworkNotificationBuilder.setSmallIcon(R.drawable.antiquated_notification_enabled)
- // Set the notification channel group.
- secureNetworkChannel.group = REALTIME_MONITORING
- insecureNetworkChannel.group = REALTIME_MONITORING
- antiquatedNetworkChannel.group = REALTIME_MONITORING
- unknownNetworkChannel.group = REALTIME_MONITORING
+ // Set the color.
+ antiquatedNetworkNotificationBuilder.setColor(getColor(R.color.red_notification_icon))
- // Disable the notification dots.
- secureNetworkChannel.setShowBadge(false)
- insecureNetworkChannel.setShowBadge(false)
- antiquatedNetworkChannel.setShowBadge(false)
- unknownNetworkChannel.setShowBadge(false)
-
- // Set the primary channel notifications to be public.
- secureNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
- insecureNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
- antiquatedNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
-
- // Create the notification channels.
- notificationManager.createNotificationChannel(secureNetworkChannel)
- notificationManager.createNotificationChannel(insecureNetworkChannel)
- notificationManager.createNotificationChannel(antiquatedNetworkChannel)
- notificationManager.createNotificationChannel(unknownNetworkChannel)
-
- // Create a notification builder.
- val notificationBuilder = Notification.Builder(this, UNKNOWN_NETWORK)
-
- // 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)
-
- // Set the notification to open Privacy Cell.
- notificationBuilder.setContentIntent(privacyCellPendingIntent)
-
- // Set the notification text.
- notificationBuilder.setContentText(getString(R.string.unknown_network))
-
- // Set the notification icon.
- notificationBuilder.setSmallIcon(R.drawable.antiquated_notification_enabled)
-
- // Set the color.
- notificationBuilder.setColor(getColor(R.color.red_notification_icon))
-
- // Start the foreground notification.
- startForeground(NOTIFICATION_ID, notificationBuilder.build())
-
- // Define the phone state listener. The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
- phoneStateListener = object : PhoneStateListener() {
- override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
- // Get the consider 3G antiquated preference.
- val consider3gAntiquated = sharedPreferences.getBoolean(getString(R.string.consider_3g_antiquated_key), false)
-
- // Populate the notification according to the network type.
- if ((telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_NR) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_IWLAN) ||
- (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_UNKNOWN)) { // This is a secure network.
- // Only update the notification if the network status has changed.
- if (currentStatus != SECURE_NETWORK) {
- // Create a secure network notification builder.
- val secureNetworkNotificationBuilder = Notification.Builder(applicationContext, SECURE_NETWORK)
-
- // Set the notification to open Privacy Cell.
- secureNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent)
-
- // Set the notification text.
- secureNetworkNotificationBuilder.setContentText(getString(R.string.secure_network))
-
- // Set the notification icon.
- secureNetworkNotificationBuilder.setSmallIcon(R.drawable.secure_notification_enabled)
-
- // Set the color.
- secureNetworkNotificationBuilder.setColor(getColor(R.color.blue_icon))
-
- // Update the notification.
- notificationManager.notify(NOTIFICATION_ID, secureNetworkNotificationBuilder.build())
-
- // Store the new network status.
- currentStatus = SECURE_NETWORK
- }
- } else if ((telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_LTE) || (!consider3gAntiquated && (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_1xRTT ||
- (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EVDO_0) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EVDO_A) ||
- (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EVDO_B) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EHRPD) ||
- (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_UMTS) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_TD_SCDMA) ||
- (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSDPA) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSUPA) ||
- (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSPA) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSPAP)))) {
- // This is an insecure network.
- // Only update the notification if the network status has changed.
- if (currentStatus != INSECURE_NETWORK) {
- // Create an insecure network notification builder.
- val insecureNetworkNotificationBuilder = Notification.Builder(applicationContext, INSECURE_NETWORK)
-
- // Set the notification to open Privacy Cell.
- insecureNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent)
-
- // Set the notification text.
- insecureNetworkNotificationBuilder.setContentText(getString(R.string.insecure_network))
-
- // Set the notification icon.
- insecureNetworkNotificationBuilder.setSmallIcon(R.drawable.insecure_notification_enabled)
-
- // Set the color.
- insecureNetworkNotificationBuilder.setColor(getColor(R.color.yellow_notification_icon))
-
- // Update the notification.
- notificationManager.notify(NOTIFICATION_ID, insecureNetworkNotificationBuilder.build())
-
- // Store the new network status.
- currentStatus = INSECURE_NETWORK
- }
- } else { // This is an antiquated network.
- // Only update the notification if the network status has changed.
- if (currentStatus != ANTIQUATED_NETWORK) {
- // Create an antiquated network notification builder.
- val antiquatedNetworkNotificationBuilder = Notification.Builder(applicationContext, ANTIQUATED_NETWORK)
-
- // Set the notification to open Privacy Cell.
- antiquatedNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent)
-
- // Set the notification text.
- antiquatedNetworkNotificationBuilder.setContentText(getString(R.string.antiquated_network))
-
- // Set the notification icon.
- antiquatedNetworkNotificationBuilder.setSmallIcon(R.drawable.antiquated_notification_enabled)
-
- // Set the color.
- antiquatedNetworkNotificationBuilder.setColor(getColor(R.color.red_notification_icon))
-
- // Update the notification.
- notificationManager.notify(NOTIFICATION_ID, antiquatedNetworkNotificationBuilder.build())
-
- // Store the new network status.
- currentStatus = ANTIQUATED_NETWORK
+ // Update the notification.
+ notificationManager.notify(NOTIFICATION_ID, antiquatedNetworkNotificationBuilder.build())
+
+ // Store the new network status.
+ currentStatus = ANTIQUATED_NETWORK
+ }
}
}
}
- }
- // 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<RegisterRealtimeListenerWorker>(1, TimeUnit.HOURS).build()
+ // 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<RegisterRealtimeListenerWorker>(1, TimeUnit.HOURS).build()
- // Register the realtime listener work request.
- WorkManager.getInstance(this).enqueueUniquePeriodicWork(getString(R.string.register_listener_work_request), ExistingPeriodicWorkPolicy.REPLACE, registerRealtimeListenerWorkRequest)
+ // Register the realtime listener work request.
+ WorkManager.getInstance(this).enqueueUniquePeriodicWork(getString(R.string.register_listener_work_request), ExistingPeriodicWorkPolicy.REPLACE, registerRealtimeListenerWorkRequest)
+ }
+ } else { // Realtime monitoring is disabled. This can happen if the restart listener work request fires after realtime monitoring has been disabled.
+ // Cancel the realtime listener work request.
+ WorkManager.getInstance(applicationContext).cancelUniqueWork(applicationContext.getString(R.string.register_listener_work_request))
}
// Return a sticky service.