]> gitweb.stoutner.com Git - PrivacyCell.git/blobdiff - app/src/main/java/com/stoutner/privacycell/activities/PrivacyCellActivity.kt
Add a realtime monitoring service. https://redmine.stoutner.com/issues/750
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / activities / PrivacyCellActivity.kt
index 296a95290ffbe6e1d4836dfcb842afb992ea85f8..cd0e1a1fcc10eb15355be338eb476418815f756d 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,6 +51,7 @@ 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.
@@ -79,7 +81,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.
@@ -147,6 +150,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.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?) {
@@ -164,7 +181,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 +328,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 +336,7 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem
         }
     }
 
-    private fun populatePrivacyCell() {
+    private fun populatePrivacyCell(context: Context) {
         // Listen to changes in the cell network state.
         telephonyManager.listen(object : PhoneStateListener() {
             override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {