X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyCell.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacycell%2Factivities%2FPrivacyCellActivity.kt;h=091586fdf9386896a8161013ddd2825551ab1650;hp=296a95290ffbe6e1d4836dfcb842afb992ea85f8;hb=cb92ea552a5ffa8ca3142053660e3a73afc9240a;hpb=031b4a8ea78fdbb776a1c5991f246b4f2a3e7ed1 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 296a952..091586f 100644 --- a/app/src/main/java/com/stoutner/privacycell/activities/PrivacyCellActivity.kt +++ b/app/src/main/java/com/stoutner/privacycell/activities/PrivacyCellActivity.kt @@ -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,11 +51,10 @@ 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 // Declare the views. @@ -79,7 +79,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. @@ -106,10 +107,6 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem additionalNetworkInfoDetailsTextView = findViewById(R.id.additional_network_info_details) val navigationView = findViewById(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 +144,20 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem // Do nothing. } }) + + // 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.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?) { @@ -164,7 +175,7 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem // 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. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted. // Populate Privacy Cell. - populatePrivacyCell() + populatePrivacyCell(this) } 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. @@ -311,7 +322,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() + populatePrivacyCell(this) } 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,7 +330,10 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem } } - private fun populatePrivacyCell() { + private fun populatePrivacyCell(context: Context) { + // 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) {