Make some progress on fixing realtime notifications.
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / fragments / SettingsFragment.kt
index 0cf6c6f088f043c35d52325eb2d8c474adfd3543..573cec3fa62fdce2024fdf6d867879a8a048ca83 100644 (file)
@@ -24,17 +24,21 @@ import android.content.SharedPreferences
 import android.os.Bundle
 import android.os.Handler
 import android.os.Looper
+import android.provider.Settings
 
 import androidx.preference.Preference
 import androidx.preference.PreferenceFragmentCompat
+import androidx.work.WorkManager
 
 import com.stoutner.privacycell.R
+import com.stoutner.privacycell.services.RealtimeMonitoringService
 
 class SettingsFragment : PreferenceFragmentCompat() {
     // Declare the class variables.
     private lateinit var sharedPreferenceChangeListener: SharedPreferences.OnSharedPreferenceChangeListener
 
     // Declare the class views.
+    private lateinit var realtimeMonitoringPreference: Preference
     private lateinit var bottomAppBarPreference: Preference
 
     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
@@ -45,8 +49,36 @@ class SettingsFragment : PreferenceFragmentCompat() {
         val sharedPreferences = preferenceScreen.sharedPreferences
 
         // Get handles for the preferences.
+        realtimeMonitoringPreference = findPreference(getString(R.string.realtime_monitoring_key))!!
+        val secureNetworkNotificationPreference = findPreference<Preference>(getString(R.string.secure_network_notification_key))!!
+        val insecureNetworkNotificationPreference = findPreference<Preference>(getString(R.string.insecure_network_notification_key))!!
         bottomAppBarPreference = findPreference(getString(R.string.bottom_app_bar_key))!!
 
+        // Set the realtime monitoring preference icon.
+        if (sharedPreferences.getBoolean(getString(R.string.realtime_monitoring_key), false)) {
+            // Set the enabled icon.
+            realtimeMonitoringPreference.setIcon(R.drawable.realtime_monitoring_enabled)
+        } else {
+            // Set the disabled icon.
+            realtimeMonitoringPreference.setIcon(R.drawable.realtime_monitoring_disabled)
+        }
+
+        // Set the notification preferences to depend on the realtime monitoring preference.
+        secureNetworkNotificationPreference.dependency = getString(R.string.realtime_monitoring_key)
+        insecureNetworkNotificationPreference.dependency = getString(R.string.realtime_monitoring_key)
+
+        // Create the notification intents.
+        val secureNetworkNotificationIntent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
+            .putExtra(Settings.EXTRA_APP_PACKAGE, requireContext().packageName)
+            .putExtra(Settings.EXTRA_CHANNEL_ID, RealtimeMonitoringService.SECURE_NETWORK)
+        val insecureNetworkNotificationIntent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
+            .putExtra(Settings.EXTRA_APP_PACKAGE, requireContext().packageName)
+            .putExtra(Settings.EXTRA_CHANNEL_ID, RealtimeMonitoringService.INSECURE_NETWORK)
+
+        // Set the notification preference intents.
+        secureNetworkNotificationPreference.intent = secureNetworkNotificationIntent
+        insecureNetworkNotificationPreference.intent = insecureNetworkNotificationIntent
+
         // Set the bottom app bar preference icon.
         if (sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)) {
             // Set the enabled icon.
@@ -88,7 +120,30 @@ class SettingsFragment : PreferenceFragmentCompat() {
         // Return the shared preference change listener.
         return SharedPreferences.OnSharedPreferenceChangeListener {sharedPreferences, key ->
             when (key) {
-                "bottom_app_bar" -> {
+                getString(R.string.realtime_monitoring_key) -> {
+                    // Update the icon.
+                    if (sharedPreferences.getBoolean(getString(R.string.realtime_monitoring_key), false)) {
+                        // Set the enabled icon.
+                        realtimeMonitoringPreference.setIcon(R.drawable.realtime_monitoring_enabled)
+                    } else {
+                        // Set the disabled icon.
+                        realtimeMonitoringPreference.setIcon(R.drawable.realtime_monitoring_disabled)
+                    }
+
+                    // Start or stop the service.
+                    if (sharedPreferences.getBoolean(getString(R.string.realtime_monitoring_key), false)) {  // Realtime monitoring has been enabled.
+                        // Start the realtime monitoring service.
+                        requireActivity().startService(Intent(context, RealtimeMonitoringService::class.java))
+                    } else {  // Realtime monitoring has been disabled.
+                        // Stop the realtime monitoring service.
+                        requireActivity().stopService(Intent(context, RealtimeMonitoringService::class.java))
+
+                        // Cancel the realtime listener work request.
+                        WorkManager.getInstance(requireContext()).cancelUniqueWork(getString(R.string.register_listener_work_request))
+                    }
+                }
+
+                getString(R.string.bottom_app_bar_key) -> {
                     // Update the icon.
                     if (sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)) {
                         // Set the enabled icon.