2 * Copyright © 2021 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
6 * Privacy Cell is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Cell is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Cell. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacycell.services
22 import android.app.Notification
23 import android.app.NotificationChannel
24 import android.app.NotificationChannelGroup
25 import android.app.NotificationManager
26 import android.app.PendingIntent
27 import android.app.Service
28 import android.content.Context
29 import android.content.Intent
30 import android.os.IBinder
31 import android.telephony.PhoneStateListener
32 import android.telephony.TelephonyDisplayInfo
33 import android.telephony.TelephonyManager
35 import com.stoutner.privacycell.R
36 import com.stoutner.privacycell.activities.PrivacyCellActivity
38 // Define the class constants.
39 const val REALTIME_MONITORING = "realtime_monitoring"
40 const val NOTIFICATION_ID = 1
42 class RealtimeMonitoringService : Service() {
44 // Define the public constants.
45 const val SECURE_NETWORK = "secure_network"
46 const val INSECURE_NETWORK = "insecure_network"
49 override fun onBind(intent: Intent?): IBinder? {
54 override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
55 // Get a handle for the notification manager.
56 val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
58 // Create a notification channel group.
59 notificationManager.createNotificationChannelGroup(NotificationChannelGroup(REALTIME_MONITORING, getString(R.string.realtime_monitoring)))
61 // Prepare the notification channels.
62 val secureNetworkChannel = NotificationChannel(SECURE_NETWORK, getString(R.string.secure_network_channel), NotificationManager.IMPORTANCE_DEFAULT)
63 val insecureNetworkChannel = NotificationChannel(INSECURE_NETWORK, getString(R.string.insecure_network_channel), NotificationManager.IMPORTANCE_DEFAULT)
65 // Set the notification channel group.
66 secureNetworkChannel.group = REALTIME_MONITORING
67 insecureNetworkChannel.group = REALTIME_MONITORING
69 // Disable the notification dots.
70 secureNetworkChannel.setShowBadge(false)
71 insecureNetworkChannel.setShowBadge(false)
73 // Set the notifications to be public.
74 secureNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
75 insecureNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
77 // Create the notification channels.
78 notificationManager.createNotificationChannel(secureNetworkChannel)
79 notificationManager.createNotificationChannel(insecureNetworkChannel)
81 // Create a notification builder.
82 val notificationBuilder = Notification.Builder(this, INSECURE_NETWORK)
84 // Create an intent to open Privacy Cell.
85 val privacyCellIntent = Intent(this, PrivacyCellActivity::class.java)
87 // Create a pending intent from the Privacy Cell intent.
88 val privacyCellPendingIntent = PendingIntent.getActivity(this, 0, privacyCellIntent, PendingIntent.FLAG_IMMUTABLE)
90 // Set the notification to open Privacy Cell.
91 notificationBuilder.setContentIntent(privacyCellPendingIntent)
93 // Set the notification text.
94 notificationBuilder.setContentText(getString(R.string.unknown_network))
96 // Set the notification icon.
97 notificationBuilder.setSmallIcon(R.drawable.insecure_notification)
100 notificationBuilder.setColor(getColor(R.color.red_text))
102 // Start the foreground notification.
103 startForeground(NOTIFICATION_ID, notificationBuilder.build())
105 // Get a handle for the telephony manager and the context.
106 val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
108 // Initialize the current status.
109 var currentStatus = ""
111 // Listen for changes to the phone state.
112 telephonyManager.listen(object : PhoneStateListener() {
113 override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
114 // Populate the notification according to the network type.
115 if (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_NR) { // This is a secure 5G NR SA network.
116 // Only update the notification if the network status has changed.
117 if (currentStatus != SECURE_NETWORK) {
118 // Create a secure network notification builder.
119 val secureNetworkNotificationBuilder = Notification.Builder(applicationContext, SECURE_NETWORK)
121 // Set the notification to open Privacy Cell.
122 secureNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent)
124 // Set the notification text.
125 secureNetworkNotificationBuilder.setContentText(getString(R.string.secure_network))
127 // Set the notification icon.
128 secureNetworkNotificationBuilder.setSmallIcon(R.drawable.secure_notification)
131 secureNetworkNotificationBuilder.setColor(getColor(R.color.blue_text))
133 // Update the notification.
134 notificationManager.notify(NOTIFICATION_ID, secureNetworkNotificationBuilder.build())
136 // Store the new network status.
137 currentStatus = SECURE_NETWORK
139 } else { // This is not a secure 5G NR SA network.
140 // Only update the notification if the network status has changed.
141 if (currentStatus !=INSECURE_NETWORK) {
142 // Create an insecure network notification builder.
143 val insecureNetworkNotificationBuilder = Notification.Builder(applicationContext, INSECURE_NETWORK)
145 // Set the notification to open Privacy Cell.
146 insecureNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent)
148 // Set the notification text.
149 insecureNetworkNotificationBuilder.setContentText(getString(R.string.insecure_network))
151 // Set the notification icon.
152 insecureNetworkNotificationBuilder.setSmallIcon(R.drawable.insecure_notification)
155 insecureNetworkNotificationBuilder.setColor(getColor(R.color.red_text))
157 // Update the notification.
158 notificationManager.notify(NOTIFICATION_ID, insecureNetworkNotificationBuilder.build())
160 // Store the new network status.
161 currentStatus = INSECURE_NETWORK
165 }, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)
167 // Return a sticky service.