X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyCell.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacycell%2Ffragments%2FSettingsFragment.kt;h=573cec3fa62fdce2024fdf6d867879a8a048ca83;hp=0cf6c6f088f043c35d52325eb2d8c474adfd3543;hb=cb92ea552a5ffa8ca3142053660e3a73afc9240a;hpb=031b4a8ea78fdbb776a1c5991f246b4f2a3e7ed1 diff --git a/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt b/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt index 0cf6c6f..573cec3 100644 --- a/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt +++ b/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt @@ -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(getString(R.string.secure_network_notification_key))!! + val insecureNetworkNotificationPreference = findPreference(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.