From: Soren Stoutner Date: Fri, 15 Oct 2021 17:24:54 +0000 (-0700) Subject: Fix problems with the realtime notifications. https://redmine.stoutner.com/issues/764 X-Git-Tag: v1.4~1 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=fb7c0422487bfb7cbb65427468c2688c24b4b99b;p=PrivacyCell.git Fix problems with the realtime notifications. https://redmine.stoutner.com/issues/764 --- diff --git a/app/src/main/java/com/stoutner/privacycell/activities/PrivacyCellActivity.kt b/app/src/main/java/com/stoutner/privacycell/activities/PrivacyCellActivity.kt index 618ef56..13cfe7b 100644 --- a/app/src/main/java/com/stoutner/privacycell/activities/PrivacyCellActivity.kt +++ b/app/src/main/java/com/stoutner/privacycell/activities/PrivacyCellActivity.kt @@ -56,21 +56,11 @@ import com.stoutner.privacycell.services.RealtimeMonitoringService class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener, PhonePermissionDialog.StoragePermissionDialogListener { // Declare the class variables. 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. @@ -93,18 +83,18 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem // Get handles for the views. drawerLayout = findViewById(R.id.drawerlayout) val toolbar = findViewById(R.id.toolbar) - stingrayLinearLayout = findViewById(R.id.stingray_linearlayout) - stingrayImageView = findViewById(R.id.stingray_imageview) + val stingrayLinearLayout = findViewById(R.id.stingray_linearlayout) + val stingrayImageView = findViewById(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(R.id.voice_network_linearlayout) + val voiceNetworkTextView = findViewById(R.id.voice_network) + val voiceNetworkDetailsTextView = findViewById(R.id.voice_network_details) + val dataNetworkLinearLayout = findViewById(R.id.data_network_linearlayout) + val dataNetworkTextView = findViewById(R.id.data_network) + val dataNetworkDetailsTextView = findViewById(R.id.data_network_details) + val additionalNetworkInfoLinearLayout = findViewById(R.id.additional_network_info_linearlayout) + val additionalNetworkInfoTextView = findViewById(R.id.additional_network_info) + val additionalNetworkInfoDetailsTextView = findViewById(R.id.additional_network_info_details) val navigationView = findViewById(R.id.navigationview) // Set the support action bar. @@ -145,6 +135,141 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem } }) + // Define the phone state listener. + phoneStateListener = object : PhoneStateListener() { + override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) { + // Populate the stingray security information. + 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. @@ -168,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(this) + // 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. @@ -194,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) { @@ -330,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(this) + 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) @@ -338,144 +474,12 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem } } - private fun populatePrivacyCell(context: Context) { + private fun registerTelephonyManagerListener() { // Get a handle for the telephony manager. val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager // Listen to changes in the cell network state. - telephonyManager.listen(object : PhoneStateListener() { - override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) { - // Populate the stingray security information. - 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)) - } - - // 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) + telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED or PhoneStateListener.LISTEN_SERVICE_STATE) } private fun getNetworkType(networkType: Int) : Array { 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 2d3b184..681a3da 100644 --- a/app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt +++ b/app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt @@ -57,18 +57,19 @@ class RealtimeMonitoringService : Service() { // Define the class variables. private var currentStatus = "" + private lateinit var phoneStateListener: PhoneStateListener inner class ServiceBinder : Binder() { // Get a copy of this service as a binder. fun getService(): RealtimeMonitoringService = this@RealtimeMonitoringService } - override fun onBind(intent: Intent): IBinder { + override fun onBind(intent: Intent?): IBinder { // Return a copy of the service binder. return ServiceBinder() } - override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { // Get a handle for the notification manager. val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager @@ -123,34 +124,8 @@ class RealtimeMonitoringService : Service() { // Start the foreground notification. startForeground(NOTIFICATION_ID, notificationBuilder.build()) - // Register the telephony manager listener. - registerTelephonyManagerListener() - - // Create a register realtime listener work request that fires every 15 minutes with a 1 minute initial delay. - val registerRealtimeListenerWorkRequest = PeriodicWorkRequestBuilder(15, TimeUnit.MINUTES).setInitialDelay(1, TimeUnit.MINUTES).build() - - // Register the realtime listener work request. - WorkManager.getInstance(this).enqueueUniquePeriodicWork(getString(R.string.register_listener_work_request), ExistingPeriodicWorkPolicy.REPLACE, registerRealtimeListenerWorkRequest) - - // Return a sticky service. - return START_STICKY - } - - fun registerTelephonyManagerListener() { - // Get a handle for the telephony manager. - val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager - - // Get a handle for the notification manager. - val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - - // 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) - - // Listen for changes to the phone state. - telephonyManager.listen(object : PhoneStateListener() { + // Define the phone state listener. + phoneStateListener = object : PhoneStateListener() { override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) { // Populate the notification according to the network type. if (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_NR) { // This is a secure 5G NR SA network. @@ -179,7 +154,7 @@ class RealtimeMonitoringService : Service() { } } else { // This is not a secure 5G NR SA network. // Only update the notification if the network status has changed. - if (currentStatus !=INSECURE_NETWORK) { + if (currentStatus != INSECURE_NETWORK) { // Create an insecure network notification builder. val insecureNetworkNotificationBuilder = Notification.Builder(applicationContext, INSECURE_NETWORK) @@ -203,6 +178,27 @@ class RealtimeMonitoringService : Service() { } } } - }, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED) + } + + // Create a register realtime listener work request that fires every hour. For some reason, when the service launches it will initially register the listener and then unregister it. + // This periodic request will fire shortly thereafter (it fires about every hour near the beginning of the hour) and will reregister the listener, which will stick this time. + val registerRealtimeListenerWorkRequest = PeriodicWorkRequestBuilder(1, TimeUnit.HOURS).build() + + // Register the realtime listener work request. + WorkManager.getInstance(this).enqueueUniquePeriodicWork(getString(R.string.register_listener_work_request), ExistingPeriodicWorkPolicy.REPLACE, registerRealtimeListenerWorkRequest) + + // Return a sticky service. + return START_STICKY + } + + fun registerTelephonyManagerListener() { + // Get a handle for the telephony manager. + val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager + + // Cancel the current listener if it exists. + telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE) + + // Listen for changes to the phone state. + telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED) } } \ No newline at end of file diff --git a/app/src/main/java/com/stoutner/privacycell/workers/RegisterRealtimeListener.kt b/app/src/main/java/com/stoutner/privacycell/workers/RegisterRealtimeListener.kt index eaa8a55..b2cd834 100644 --- a/app/src/main/java/com/stoutner/privacycell/workers/RegisterRealtimeListener.kt +++ b/app/src/main/java/com/stoutner/privacycell/workers/RegisterRealtimeListener.kt @@ -35,21 +35,18 @@ import com.stoutner.privacycell.R import com.stoutner.privacycell.services.RealtimeMonitoringService class RegisterRealtimeListener (appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) { - // Store the application context. - private val context = appContext - override fun doWork(): Result { // Get a handle for the shared preferences. - val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext) // Get the realtime monitoring preference. 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. - val realtimeMonitoring = sharedPreferences.getBoolean(context.getString(R.string.realtime_monitoring_key), true) + val realtimeMonitoring = sharedPreferences.getBoolean(applicationContext.getString(R.string.realtime_monitoring_key), true) // Perform the functions according to the realtime monitoring status. if (realtimeMonitoring) { // Realtime monitoring is enabled. // Get a handle for the activity manager. - val activityManager: ActivityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + val activityManager: ActivityManager = applicationContext.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.getRunningServices(1) @@ -57,7 +54,7 @@ class RegisterRealtimeListener (appContext: Context, workerParams: WorkerParamet // Check to see if the service is currently running. if (runningServiceInfoList.isEmpty()) { // The service is currently stopped. // Start the service. - context.startService(Intent(context, RealtimeMonitoringService::class.java)) + applicationContext.startService(Intent(applicationContext, RealtimeMonitoringService::class.java)) } else { // The service is currently running. // Create a service connection. val serviceConnection = object : ServiceConnection { @@ -72,7 +69,7 @@ class RegisterRealtimeListener (appContext: Context, workerParams: WorkerParamet realtimeMonitoringService.registerTelephonyManagerListener() // Unbind the service. - context.unbindService(this) + applicationContext.unbindService(this) } override fun onServiceDisconnected(componentName: ComponentName) { @@ -81,11 +78,11 @@ class RegisterRealtimeListener (appContext: Context, workerParams: WorkerParamet } // Bind to the realtime monitoring service. - context.bindService(Intent(context, RealtimeMonitoringService::class.java), serviceConnection, 0) + applicationContext.bindService(Intent(applicationContext, RealtimeMonitoringService::class.java), serviceConnection, 0) } } else { // Realtime monitoring is disabled. // Cancel the realtime listener work request. - WorkManager.getInstance(context).cancelUniqueWork(context.getString(R.string.register_listener_work_request)) + WorkManager.getInstance(applicationContext).cancelUniqueWork(applicationContext.getString(R.string.register_listener_work_request)) } // Return a success. diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 51db896..5747a03 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -38,6 +38,7 @@ Ajustes + Logcat Permisos Política de privacidad Registro de cambios @@ -121,6 +122,16 @@ Barra inferior de app Mueve la barra de aplicaciones a la parte inferior de la pantalla. Al cambiar esta configuración se reiniciará Privacy Cell. + + Copiar + Guardar + Limpiar + Logcat copiado. + Privacy Cell Logcat.txt + %1$s guardado. + Error guardando logcat: \u0020 %1$s + Seguro Inseguro diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index d3bf213..dd21983 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -38,6 +38,7 @@ Impostazioni + Logcat Autorizzazioni Privacy Policy Changelog @@ -121,6 +122,16 @@ Barra dell\'app in basso Sposta la barra dell\'app nella parte inferiore dello schermo. La modifica di questa impostazione provoca il riavvio di Privacy Cell. + + Copia + Salva + Cancella + Logcat copiato. + Privacy Cell Logcat.txt + %1$s salvato. + Errore salvataggio logcat: \u0020 %1$s + Sicura Insicura diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 551e449..c637402 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -40,6 +40,7 @@ Settings + Logcat Permissions Privacy Policy Changelog @@ -49,7 +50,6 @@ Roadmap Bug Tracker Forum - Logcat Donations @@ -124,8 +124,7 @@ Bottom app bar Move the app bar to the bottom of the screen. Changing this setting will restart Privacy Cell. - Copy Save