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
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.
// 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.
// 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?) {
// 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.
// 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)
}
}
- 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) {