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=f23a1168a91e8ae2e77ddd9f4bd1886df285f570;hp=0cf6c6f088f043c35d52325eb2d8c474adfd3543;hb=976d7da4c16f9ddc87f0cd7c67cd2eefc9f260b1;hpb=70a8bdb781941d9f433b18a91befe03137d65a6f 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..f23a116 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,20 @@ 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 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 +48,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 +119,27 @@ 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)) + } + } + + 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.