]> gitweb.stoutner.com Git - PrivacyCell.git/blobdiff - app/src/main/java/com/stoutner/privacycell/activities/PrivacyCellActivity.kt
Fix problems with the realtime notifications. https://redmine.stoutner.com/issues/764
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / activities / PrivacyCellActivity.kt
index 296a95290ffbe6e1d4836dfcb842afb992ea85f8..13cfe7bda17cd00a996ae16b5303fb76b3160e91 100644 (file)
@@ -20,6 +20,7 @@
 package com.stoutner.privacycell.activities
 
 import android.Manifest
+import android.app.ActivityManager
 import android.content.Context
 import android.content.Intent
 import android.content.pm.PackageManager
@@ -50,27 +51,16 @@ import com.google.android.material.navigation.NavigationView
 import com.stoutner.privacycell.R
 import com.stoutner.privacycell.dialogs.PhonePermissionDialog
 import com.stoutner.privacycell.dialogs.WebViewDialog
+import com.stoutner.privacycell.services.RealtimeMonitoringService
 
 class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener, PhonePermissionDialog.StoragePermissionDialogListener {
     // Declare the class variables.
-    private lateinit var context: Context
-    private lateinit var telephonyManager: TelephonyManager
     private lateinit var actionBarDrawerToggle: ActionBarDrawerToggle
+    private lateinit var phoneStateListener: PhoneStateListener
 
-    // Declare the views.
+    // Declare the class views.
     private lateinit var drawerLayout: DrawerLayout
-    private lateinit var stingrayLinearLayout: LinearLayout
-    private lateinit var stingrayImageView: ImageView
     private lateinit var stingrayTextView: TextView
-    private lateinit var voiceNetworkLinearLayout: LinearLayout
-    private lateinit var voiceNetworkTextView: TextView
-    private lateinit var voiceNetworkDetailsTextView: TextView
-    private lateinit var dataNetworkLinearLayout: LinearLayout
-    private lateinit var dataNetworkTextView: TextView
-    private lateinit var dataNetworkDetailsTextView: TextView
-    private lateinit var additionalNetworkInfoLinearLayout: LinearLayout
-    private lateinit var additionalNetworkInfoTextView: TextView
-    private lateinit var additionalNetworkInfoDetailsTextView: TextView
 
     override fun onCreate(savedInstanceState: Bundle?) {
         // Run the default commands.
@@ -79,7 +69,8 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem
         // Get a handle for the shared preferences.
         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
 
-        // Get the bottom app bar preference.
+        // Get the preferences.
+        val realtimeMonitoring = sharedPreferences.getBoolean(getString(R.string.realtime_monitoring_key), false)
         val bottomAppBar = sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)
 
         // Set the content view.
@@ -92,24 +83,20 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem
         // Get handles for the views.
         drawerLayout = findViewById(R.id.drawerlayout)
         val toolbar = findViewById<Toolbar>(R.id.toolbar)
-        stingrayLinearLayout = findViewById(R.id.stingray_linearlayout)
-        stingrayImageView = findViewById(R.id.stingray_imageview)
+        val stingrayLinearLayout = findViewById<LinearLayout>(R.id.stingray_linearlayout)
+        val stingrayImageView = findViewById<ImageView>(R.id.stingray_imageview)
         stingrayTextView = findViewById(R.id.stingray_textview)
-        voiceNetworkLinearLayout = findViewById(R.id.voice_network_linearlayout)
-        voiceNetworkTextView = findViewById(R.id.voice_network)
-        voiceNetworkDetailsTextView = findViewById(R.id.voice_network_details)
-        dataNetworkLinearLayout = findViewById(R.id.data_network_linearlayout)
-        dataNetworkTextView = findViewById(R.id.data_network)
-        dataNetworkDetailsTextView = findViewById(R.id.data_network_details)
-        additionalNetworkInfoLinearLayout = findViewById(R.id.additional_network_info_linearlayout)
-        additionalNetworkInfoTextView = findViewById(R.id.additional_network_info)
-        additionalNetworkInfoDetailsTextView = findViewById(R.id.additional_network_info_details)
+        val voiceNetworkLinearLayout = findViewById<LinearLayout>(R.id.voice_network_linearlayout)
+        val voiceNetworkTextView = findViewById<TextView>(R.id.voice_network)
+        val voiceNetworkDetailsTextView = findViewById<TextView>(R.id.voice_network_details)
+        val dataNetworkLinearLayout = findViewById<LinearLayout>(R.id.data_network_linearlayout)
+        val dataNetworkTextView = findViewById<TextView>(R.id.data_network)
+        val dataNetworkDetailsTextView = findViewById<TextView>(R.id.data_network_details)
+        val additionalNetworkInfoLinearLayout = findViewById<LinearLayout>(R.id.additional_network_info_linearlayout)
+        val additionalNetworkInfoTextView = findViewById<TextView>(R.id.additional_network_info)
+        val additionalNetworkInfoDetailsTextView = findViewById<TextView>(R.id.additional_network_info_details)
         val navigationView = findViewById<NavigationView>(R.id.navigationview)
 
-        // Get handles for the context and the telephony manager.
-        context = this
-        telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
-
         // Set the support action bar.
         setSupportActionBar(toolbar)
 
@@ -147,6 +134,155 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem
                 // Do nothing.
             }
         })
+
+        // Define the phone state listener.
+        phoneStateListener = object : PhoneStateListener() {
+            override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
+                // Populate the stingray security information.  <https://source.android.com/devices/tech/connect/acts-5g-testing>
+                if (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_NR) {  // This is a secure 5G NR SA network.
+                    // Populate the image view.
+                    stingrayImageView.setImageDrawable(AppCompatResources.getDrawable(applicationContext, R.drawable.secure_5g_nr_sa))
+
+                    // Set the text.
+                    stingrayTextView.text = getString(R.string.secure_from_stingray)
+
+                    // Set the text color.
+                    stingrayTextView.setTextColor(getColor(R.color.blue_text))
+                } else {  // This is not a secure 5G NR SA network.
+                    // Populate the image view.
+                    stingrayImageView.setImageDrawable(AppCompatResources.getDrawable(applicationContext, R.drawable.not_secure))
+
+                    // Set the text.
+                    stingrayTextView.text = getString(R.string.not_secure_from_stingray)
+
+                    // Set the text color.
+                    stingrayTextView.setTextColor(getColor(R.color.red_text))
+                }
+
+                // Get the strings that correspond to the network information.
+                val dataNetworkType = getNetworkType(telephonyDisplayInfo.networkType)
+                val additionalNetworkInfo = getAdditionalNetworkInfo(telephonyDisplayInfo.overrideNetworkType)
+
+                // Populate the data network text views.
+                dataNetworkTextView.text = getString(R.string.data_network, dataNetworkType[0])
+                dataNetworkDetailsTextView.text = dataNetworkType[1]
+                additionalNetworkInfoTextView.text = getString(R.string.additional_network_info, additionalNetworkInfo[0])
+                additionalNetworkInfoDetailsTextView.text = additionalNetworkInfo[1]
+
+                // Set the stingray click listener.
+                stingrayLinearLayout.setOnClickListener {
+                    // Instantiate the stingray dialog fragment.
+                    val stingrayDialogFragment = WebViewDialog().type(WebViewDialog.STINGRAY)
+
+                    // Show the alert dialog.
+                    stingrayDialogFragment.show(supportFragmentManager, getString(R.string.stingrays))
+                }
+
+                // Set the data network click listener.
+                dataNetworkLinearLayout.setOnClickListener {
+                    // Instantiate the data network dialog fragment according to the network type.
+                    val dataNetworkDialogFragment = when (telephonyDisplayInfo.networkType) {
+                        TelephonyManager.NETWORK_TYPE_UNKNOWN -> WebViewDialog().type(WebViewDialog.NETWORK_UNKNOWN)
+                        TelephonyManager.NETWORK_TYPE_GPRS -> WebViewDialog().type(WebViewDialog.NETWORK_GPRS)
+                        TelephonyManager.NETWORK_TYPE_EDGE -> WebViewDialog().type(WebViewDialog.NETWORK_EDGE)
+                        TelephonyManager.NETWORK_TYPE_UMTS -> WebViewDialog().type(WebViewDialog.NETWORK_UMTS)
+                        TelephonyManager.NETWORK_TYPE_CDMA -> WebViewDialog().type(WebViewDialog.NETWORK_CDMA)
+                        TelephonyManager.NETWORK_TYPE_EVDO_0 -> WebViewDialog().type(WebViewDialog.NETWORK_EVDO_0)
+                        TelephonyManager.NETWORK_TYPE_EVDO_A -> WebViewDialog().type(WebViewDialog.NETWORK_EVDO_A)
+                        TelephonyManager.NETWORK_TYPE_1xRTT -> WebViewDialog().type(WebViewDialog.NETWORK_1xRTT)
+                        TelephonyManager.NETWORK_TYPE_HSDPA -> WebViewDialog().type(WebViewDialog.NETWORK_HSDPA)
+                        TelephonyManager.NETWORK_TYPE_HSUPA -> WebViewDialog().type(WebViewDialog.NETWORK_HSUPA)
+                        TelephonyManager.NETWORK_TYPE_HSPA -> WebViewDialog().type(WebViewDialog.NETWORK_HSPA)
+                        TelephonyManager.NETWORK_TYPE_IDEN -> WebViewDialog().type(WebViewDialog.NETWORK_IDEN)
+                        TelephonyManager.NETWORK_TYPE_EVDO_B -> WebViewDialog().type(WebViewDialog.NETWORK_EVDO_B)
+                        TelephonyManager.NETWORK_TYPE_LTE -> WebViewDialog().type(WebViewDialog.NETWORK_LTE)
+                        TelephonyManager.NETWORK_TYPE_EHRPD -> WebViewDialog().type(WebViewDialog.NETWORK_EHRPD)
+                        TelephonyManager.NETWORK_TYPE_HSPAP -> WebViewDialog().type(WebViewDialog.NETWORK_HSPAP)
+                        TelephonyManager.NETWORK_TYPE_GSM -> WebViewDialog().type(WebViewDialog.NETWORK_GSM)
+                        TelephonyManager.NETWORK_TYPE_TD_SCDMA -> WebViewDialog().type(WebViewDialog.NETWORK_TD_SCDMA)
+                        TelephonyManager.NETWORK_TYPE_IWLAN -> WebViewDialog().type(WebViewDialog.NETWORK_IWLAN)
+                        TelephonyManager.NETWORK_TYPE_NR -> WebViewDialog().type(WebViewDialog.NETWORK_NR)
+                        else -> WebViewDialog().type(WebViewDialog.NETWORK_UNKNOWN)
+                    }
+
+                    // Show the alert dialog.
+                    dataNetworkDialogFragment.show(supportFragmentManager, getString(R.string.voice_network))
+                }
+
+                // Set the additional network info click listener.
+                additionalNetworkInfoLinearLayout.setOnClickListener {
+                    // Instantiate the initial network info dialog fragment according to the network type.
+                    val additionalNetworkInfoDialogFragment = when (telephonyDisplayInfo.overrideNetworkType) {
+                        TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE -> WebViewDialog().type(WebViewDialog.OVERRIDE_NETWORK_NONE)
+                        TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA -> WebViewDialog().type(WebViewDialog.OVERRIDE_NETWORK_LTE_CA)
+                        TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO -> WebViewDialog().type(WebViewDialog.OVERRIDE_NETWORK_LTE_ADVANCED_PRO)
+                        TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA -> WebViewDialog().type(WebViewDialog.OVERRIDE_NETWORK_NR_NSA)
+                        TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE -> WebViewDialog().type(WebViewDialog.OVERRIDE_NETWORK_NR_NSA_MMWAVE)
+                        else -> WebViewDialog().type(WebViewDialog.OVERRIDE_NETWORK_NR_NSA_MMWAVE)
+                    }
+
+                    // Show the alert dialog.
+                    additionalNetworkInfoDialogFragment.show(supportFragmentManager, getString(R.string.voice_network))
+                }
+            }
+
+            override fun onServiceStateChanged(serviceState: ServiceState) {
+                // 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 voice network type.
+                val voiceNetworkType = getNetworkType(networkRegistrationInfo.accessNetworkTechnology)
+
+                // Populate the voice network text views.
+                voiceNetworkTextView.text = getString(R.string.voice_network, voiceNetworkType[0])
+                voiceNetworkDetailsTextView.text = voiceNetworkType[1]
+
+                // Set the voice network click listener.
+                voiceNetworkLinearLayout.setOnClickListener {
+                    // Instantiate the voice network dialog fragment according to the network type.
+                    val voiceNetworkDialogFragment = when (networkRegistrationInfo.accessNetworkTechnology) {
+                        TelephonyManager.NETWORK_TYPE_UNKNOWN -> WebViewDialog().type(WebViewDialog.NETWORK_UNKNOWN)
+                        TelephonyManager.NETWORK_TYPE_GPRS -> WebViewDialog().type(WebViewDialog.NETWORK_GPRS)
+                        TelephonyManager.NETWORK_TYPE_EDGE -> WebViewDialog().type(WebViewDialog.NETWORK_EDGE)
+                        TelephonyManager.NETWORK_TYPE_UMTS -> WebViewDialog().type(WebViewDialog.NETWORK_UMTS)
+                        TelephonyManager.NETWORK_TYPE_CDMA -> WebViewDialog().type(WebViewDialog.NETWORK_CDMA)
+                        TelephonyManager.NETWORK_TYPE_EVDO_0 -> WebViewDialog().type(WebViewDialog.NETWORK_EVDO_0)
+                        TelephonyManager.NETWORK_TYPE_EVDO_A -> WebViewDialog().type(WebViewDialog.NETWORK_EVDO_A)
+                        TelephonyManager.NETWORK_TYPE_1xRTT -> WebViewDialog().type(WebViewDialog.NETWORK_1xRTT)
+                        TelephonyManager.NETWORK_TYPE_HSDPA -> WebViewDialog().type(WebViewDialog.NETWORK_HSDPA)
+                        TelephonyManager.NETWORK_TYPE_HSUPA -> WebViewDialog().type(WebViewDialog.NETWORK_HSUPA)
+                        TelephonyManager.NETWORK_TYPE_HSPA -> WebViewDialog().type(WebViewDialog.NETWORK_HSPA)
+                        TelephonyManager.NETWORK_TYPE_IDEN -> WebViewDialog().type(WebViewDialog.NETWORK_IDEN)
+                        TelephonyManager.NETWORK_TYPE_EVDO_B -> WebViewDialog().type(WebViewDialog.NETWORK_EVDO_B)
+                        TelephonyManager.NETWORK_TYPE_LTE -> WebViewDialog().type(WebViewDialog.NETWORK_LTE)
+                        TelephonyManager.NETWORK_TYPE_EHRPD -> WebViewDialog().type(WebViewDialog.NETWORK_EHRPD)
+                        TelephonyManager.NETWORK_TYPE_HSPAP -> WebViewDialog().type(WebViewDialog.NETWORK_HSPAP)
+                        TelephonyManager.NETWORK_TYPE_GSM -> WebViewDialog().type(WebViewDialog.NETWORK_GSM)
+                        TelephonyManager.NETWORK_TYPE_TD_SCDMA -> WebViewDialog().type(WebViewDialog.NETWORK_TD_SCDMA)
+                        TelephonyManager.NETWORK_TYPE_IWLAN -> WebViewDialog().type(WebViewDialog.NETWORK_IWLAN)
+                        TelephonyManager.NETWORK_TYPE_NR -> WebViewDialog().type(WebViewDialog.NETWORK_NR)
+                        else -> WebViewDialog().type(WebViewDialog.NETWORK_UNKNOWN)
+                    }
+
+                    // Show the alert dialog.
+                    voiceNetworkDialogFragment.show(supportFragmentManager, getString(R.string.voice_network))
+                }
+            }
+        }
+
+        // Start the realtime monitoring service if it is enabled.
+        if (realtimeMonitoring) {
+            // Get a handle for the activity manager.
+            val activityManager: ActivityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
+
+            // Get a list of the running service info.  The deprecated `getRunningServices()` now only returns services stared by Privacy Cell, but that is all we want to know anyway.
+            val runningServiceInfoList: List<ActivityManager.RunningServiceInfo> = activityManager.getRunningServices(1)
+
+            // Start the service if it is not already running.
+            if (runningServiceInfoList.isEmpty()) {
+                startService(Intent(this, RealtimeMonitoringService::class.java))
+            }
+        }
     }
 
     override fun onPostCreate(savedInstanceState: Bundle?) {
@@ -157,14 +293,14 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem
         actionBarDrawerToggle.syncState()
     }
 
-    override fun onResume() {
+    override fun onStart() {
         // Run the default commands.
-        super.onResume()
+        super.onStart()
 
-        // Check to see if the read phone state permission has been granted.  These commands need to be run on every resume so that the listener gets reassigned as it is automatically paused.
+        // Check to see if the read phone state permission has been granted.  These commands need to be run on every start so that the listener is reregistered.
         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {  // The storage permission has been granted.
-            // Populate Privacy Cell.
-            populatePrivacyCell()
+            // Register the telephony manager listener.
+            registerTelephonyManagerListener()
         } else {  // The phone permission has not been granted.
             // Check if the user has previously denied the storage permission.
             if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_PHONE_STATE)) {  // Show a dialog explaining the request first.
@@ -183,6 +319,17 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem
         }
     }
 
+    override fun onStop() {
+        // Run the default commands.
+        super.onStop()
+
+        // Get a handle for the telephony manager.
+        val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
+
+        // Unregister the telephony manager listener.
+        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)
+    }
+
     override fun onNavigationItemSelected(menuItem: MenuItem) : Boolean {
         // Run the commands that correspond to the selected menu item.
         when (menuItem.itemId) {
@@ -194,6 +341,14 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem
                 startActivity(settingsIntent)
             }
 
+            R.id.logcat -> {  // Logcat.
+                // Create an intent to load the Logcat activity.
+                val logcatIntent = Intent(this, LogcatActivity::class.java)
+
+                // Make it so.
+                startActivity(logcatIntent)
+            }
+
             R.id.permissions -> {  // Permissions.
                 // Instantiate the permissions dialog fragment.
                 val permissionsDialogFragment = WebViewDialog().type(WebViewDialog.PERMISSIONS)
@@ -311,7 +466,7 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem
             // Check to see if the read phone state permission was granted.  If the dialog was canceled the grant results will be empty.
             if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {  // The read phone state permission was granted.
                 // Populate Privacy Cell.
-                populatePrivacyCell()
+                registerTelephonyManagerListener()
             } else {  // The read phone state permission was denied.
                 // Display the phone permission text on the main activity.
                 stingrayTextView.text = getString(R.string.phone_permission_text)
@@ -319,141 +474,12 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem
         }
     }
 
-    private fun populatePrivacyCell() {
-        // Listen to changes in the cell network state.
-        telephonyManager.listen(object : PhoneStateListener() {
-            override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
-                // Populate the stingray security information.  <https://source.android.com/devices/tech/connect/acts-5g-testing>
-                if (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_NR) {  // This is a secure 5G NR SA network.
-                    // Populate the image view.
-                    stingrayImageView.setImageDrawable(AppCompatResources.getDrawable(context, R.drawable.secure_5g_nr_sa))
-
-                    // Set the text.
-                    stingrayTextView.text = getString(R.string.secure_from_stingray)
-
-                    // Set the text color.
-                    stingrayTextView.setTextColor(getColor(R.color.blue_text))
-                } else {  // This is not a secure 5G NR SA network.
-                    // Populate the image view.
-                    stingrayImageView.setImageDrawable(AppCompatResources.getDrawable(context, R.drawable.not_secure))
-
-                    // Set the text.
-                    stingrayTextView.text = getString(R.string.not_secure_from_stingray)
-
-                    // Set the text color.
-                    stingrayTextView.setTextColor(getColor(R.color.red_text))
-                }
-
-                // Get the strings that correspond to the network information.
-                val dataNetworkType = getNetworkType(telephonyDisplayInfo.networkType)
-                val additionalNetworkInfo = getAdditionalNetworkInfo(telephonyDisplayInfo.overrideNetworkType)
-
-                // Populate the data network text views.
-                dataNetworkTextView.text = getString(R.string.data_network, dataNetworkType[0])
-                dataNetworkDetailsTextView.text = dataNetworkType[1]
-                additionalNetworkInfoTextView.text = getString(R.string.additional_network_info, additionalNetworkInfo[0])
-                additionalNetworkInfoDetailsTextView.text = additionalNetworkInfo[1]
-
-                // Set the stingray click listener.
-                stingrayLinearLayout.setOnClickListener {
-                    // Instantiate the stingray dialog fragment.
-                    val stingrayDialogFragment = WebViewDialog().type(WebViewDialog.STINGRAY)
-
-                    // Show the alert dialog.
-                    stingrayDialogFragment.show(supportFragmentManager, getString(R.string.stingrays))
-                }
-
-                // Set the data network click listener.
-                dataNetworkLinearLayout.setOnClickListener {
-                    // Instantiate the data network dialog fragment according to the network type.
-                    val dataNetworkDialogFragment = when (telephonyDisplayInfo.networkType) {
-                        TelephonyManager.NETWORK_TYPE_UNKNOWN -> WebViewDialog().type(WebViewDialog.NETWORK_UNKNOWN)
-                        TelephonyManager.NETWORK_TYPE_GPRS -> WebViewDialog().type(WebViewDialog.NETWORK_GPRS)
-                        TelephonyManager.NETWORK_TYPE_EDGE -> WebViewDialog().type(WebViewDialog.NETWORK_EDGE)
-                        TelephonyManager.NETWORK_TYPE_UMTS -> WebViewDialog().type(WebViewDialog.NETWORK_UMTS)
-                        TelephonyManager.NETWORK_TYPE_CDMA -> WebViewDialog().type(WebViewDialog.NETWORK_CDMA)
-                        TelephonyManager.NETWORK_TYPE_EVDO_0 -> WebViewDialog().type(WebViewDialog.NETWORK_EVDO_0)
-                        TelephonyManager.NETWORK_TYPE_EVDO_A -> WebViewDialog().type(WebViewDialog.NETWORK_EVDO_A)
-                        TelephonyManager.NETWORK_TYPE_1xRTT -> WebViewDialog().type(WebViewDialog.NETWORK_1xRTT)
-                        TelephonyManager.NETWORK_TYPE_HSDPA -> WebViewDialog().type(WebViewDialog.NETWORK_HSDPA)
-                        TelephonyManager.NETWORK_TYPE_HSUPA -> WebViewDialog().type(WebViewDialog.NETWORK_HSUPA)
-                        TelephonyManager.NETWORK_TYPE_HSPA -> WebViewDialog().type(WebViewDialog.NETWORK_HSPA)
-                        TelephonyManager.NETWORK_TYPE_IDEN -> WebViewDialog().type(WebViewDialog.NETWORK_IDEN)
-                        TelephonyManager.NETWORK_TYPE_EVDO_B -> WebViewDialog().type(WebViewDialog.NETWORK_EVDO_B)
-                        TelephonyManager.NETWORK_TYPE_LTE -> WebViewDialog().type(WebViewDialog.NETWORK_LTE)
-                        TelephonyManager.NETWORK_TYPE_EHRPD -> WebViewDialog().type(WebViewDialog.NETWORK_EHRPD)
-                        TelephonyManager.NETWORK_TYPE_HSPAP -> WebViewDialog().type(WebViewDialog.NETWORK_HSPAP)
-                        TelephonyManager.NETWORK_TYPE_GSM -> WebViewDialog().type(WebViewDialog.NETWORK_GSM)
-                        TelephonyManager.NETWORK_TYPE_TD_SCDMA -> WebViewDialog().type(WebViewDialog.NETWORK_TD_SCDMA)
-                        TelephonyManager.NETWORK_TYPE_IWLAN -> WebViewDialog().type(WebViewDialog.NETWORK_IWLAN)
-                        TelephonyManager.NETWORK_TYPE_NR -> WebViewDialog().type(WebViewDialog.NETWORK_NR)
-                        else -> WebViewDialog().type(WebViewDialog.NETWORK_UNKNOWN)
-                    }
-
-                    // Show the alert dialog.
-                    dataNetworkDialogFragment.show(supportFragmentManager, getString(R.string.voice_network))
-                }
+    private fun registerTelephonyManagerListener() {
+        // Get a handle for the telephony manager.
+        val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
 
-                // Set the additional network info click listener.
-                additionalNetworkInfoLinearLayout.setOnClickListener {
-                    // Instantiate the initial network info dialog fragment according to the network type.
-                    val additionalNetworkInfoDialogFragment = when (telephonyDisplayInfo.overrideNetworkType) {
-                        TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE -> WebViewDialog().type(WebViewDialog.OVERRIDE_NETWORK_NONE)
-                        TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA -> WebViewDialog().type(WebViewDialog.OVERRIDE_NETWORK_LTE_CA)
-                        TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_ADVANCED_PRO -> WebViewDialog().type(WebViewDialog.OVERRIDE_NETWORK_LTE_ADVANCED_PRO)
-                        TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA -> WebViewDialog().type(WebViewDialog.OVERRIDE_NETWORK_NR_NSA)
-                        TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA_MMWAVE -> WebViewDialog().type(WebViewDialog.OVERRIDE_NETWORK_NR_NSA_MMWAVE)
-                        else -> WebViewDialog().type(WebViewDialog.OVERRIDE_NETWORK_NR_NSA_MMWAVE)
-                    }
-
-                    // Show the alert dialog.
-                    additionalNetworkInfoDialogFragment.show(supportFragmentManager, getString(R.string.voice_network))
-                }
-            }
-
-            override fun onServiceStateChanged(serviceState: ServiceState) {
-                // 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 voice network type.
-                val voiceNetworkType = getNetworkType(networkRegistrationInfo.accessNetworkTechnology)
-
-                // Populate the voice network text views.
-                voiceNetworkTextView.text = getString(R.string.voice_network, voiceNetworkType[0])
-                voiceNetworkDetailsTextView.text = voiceNetworkType[1]
-
-                // Set the voice network click listener.
-                voiceNetworkLinearLayout.setOnClickListener {
-                    // Instantiate the voice network dialog fragment according to the network type.
-                    val voiceNetworkDialogFragment = when (networkRegistrationInfo.accessNetworkTechnology) {
-                        TelephonyManager.NETWORK_TYPE_UNKNOWN -> WebViewDialog().type(WebViewDialog.NETWORK_UNKNOWN)
-                        TelephonyManager.NETWORK_TYPE_GPRS -> WebViewDialog().type(WebViewDialog.NETWORK_GPRS)
-                        TelephonyManager.NETWORK_TYPE_EDGE -> WebViewDialog().type(WebViewDialog.NETWORK_EDGE)
-                        TelephonyManager.NETWORK_TYPE_UMTS -> WebViewDialog().type(WebViewDialog.NETWORK_UMTS)
-                        TelephonyManager.NETWORK_TYPE_CDMA -> WebViewDialog().type(WebViewDialog.NETWORK_CDMA)
-                        TelephonyManager.NETWORK_TYPE_EVDO_0 -> WebViewDialog().type(WebViewDialog.NETWORK_EVDO_0)
-                        TelephonyManager.NETWORK_TYPE_EVDO_A -> WebViewDialog().type(WebViewDialog.NETWORK_EVDO_A)
-                        TelephonyManager.NETWORK_TYPE_1xRTT -> WebViewDialog().type(WebViewDialog.NETWORK_1xRTT)
-                        TelephonyManager.NETWORK_TYPE_HSDPA -> WebViewDialog().type(WebViewDialog.NETWORK_HSDPA)
-                        TelephonyManager.NETWORK_TYPE_HSUPA -> WebViewDialog().type(WebViewDialog.NETWORK_HSUPA)
-                        TelephonyManager.NETWORK_TYPE_HSPA -> WebViewDialog().type(WebViewDialog.NETWORK_HSPA)
-                        TelephonyManager.NETWORK_TYPE_IDEN -> WebViewDialog().type(WebViewDialog.NETWORK_IDEN)
-                        TelephonyManager.NETWORK_TYPE_EVDO_B -> WebViewDialog().type(WebViewDialog.NETWORK_EVDO_B)
-                        TelephonyManager.NETWORK_TYPE_LTE -> WebViewDialog().type(WebViewDialog.NETWORK_LTE)
-                        TelephonyManager.NETWORK_TYPE_EHRPD -> WebViewDialog().type(WebViewDialog.NETWORK_EHRPD)
-                        TelephonyManager.NETWORK_TYPE_HSPAP -> WebViewDialog().type(WebViewDialog.NETWORK_HSPAP)
-                        TelephonyManager.NETWORK_TYPE_GSM -> WebViewDialog().type(WebViewDialog.NETWORK_GSM)
-                        TelephonyManager.NETWORK_TYPE_TD_SCDMA -> WebViewDialog().type(WebViewDialog.NETWORK_TD_SCDMA)
-                        TelephonyManager.NETWORK_TYPE_IWLAN -> WebViewDialog().type(WebViewDialog.NETWORK_IWLAN)
-                        TelephonyManager.NETWORK_TYPE_NR -> WebViewDialog().type(WebViewDialog.NETWORK_NR)
-                        else -> WebViewDialog().type(WebViewDialog.NETWORK_UNKNOWN)
-                    }
-
-                    // Show the alert dialog.
-                    voiceNetworkDialogFragment.show(supportFragmentManager, getString(R.string.voice_network))
-                }
-            }
-        }, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED or PhoneStateListener.LISTEN_SERVICE_STATE)
+        // Listen to changes in the cell network state.
+        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED or PhoneStateListener.LISTEN_SERVICE_STATE)
     }
 
     private fun getNetworkType(networkType: Int) : Array<String> {