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