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=20dfb37be3cdbb74e847fabe05125ba0b4546569;hp=0cf6c6f088f043c35d52325eb2d8c474adfd3543;hb=7467e75bda34246c91c4e4989b1c32a81314d53c;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..20dfb37 100644 --- a/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt +++ b/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2021 Soren Stoutner . + * Copyright © 2021-2022 Soren Stoutner . * * This file is part of Privacy Cell . * @@ -19,22 +19,33 @@ package com.stoutner.privacycell.fragments +import android.Manifest import android.content.Intent import android.content.SharedPreferences +import android.content.pm.PackageManager import android.os.Bundle import android.os.Handler import android.os.Looper +import android.provider.Settings +import androidx.core.app.ActivityCompat 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 secureNetworkNotificationPreference: Preference + private lateinit var insecureNetworkNotificationPreference: Preference + private lateinit var antiquatedNetworkNotificationPreference: Preference + private lateinit var consider3gAntiquatedPreference: Preference private lateinit var bottomAppBarPreference: Preference override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { @@ -42,17 +53,75 @@ class SettingsFragment : PreferenceFragmentCompat() { setPreferencesFromResource(R.xml.preferences, rootKey) // Get a handle for the shared preferences. - val sharedPreferences = preferenceScreen.sharedPreferences + val sharedPreferences = preferenceScreen.sharedPreferences!! // Get handles for the preferences. + realtimeMonitoringPreference = findPreference(getString(R.string.realtime_monitoring_key))!! + secureNetworkNotificationPreference = findPreference(getString(R.string.secure_network_notification_key))!! + insecureNetworkNotificationPreference = findPreference(getString(R.string.insecure_network_notification_key))!! + antiquatedNetworkNotificationPreference = findPreference(getString(R.string.antiquated_network_notification_key))!! + consider3gAntiquatedPreference = findPreference(getString(R.string.consider_3g_antiquated_key))!! bottomAppBarPreference = findPreference(getString(R.string.bottom_app_bar_key))!! + // Only enable the realtime monitoring preference if the READ_PHONE_STATE permission has been granted. + realtimeMonitoringPreference.isEnabled = (ActivityCompat.checkSelfPermission(requireContext(), Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) + + // Set the realtime monitoring icon according to the status. + if (realtimeMonitoringPreference.isEnabled) { + // Set the realtime monitoring preference icon. + if (sharedPreferences.getBoolean(getString(R.string.realtime_monitoring_key), false)) { + // Set the icons. + realtimeMonitoringPreference.setIcon(R.drawable.secure_notification_enabled) + secureNetworkNotificationPreference.setIcon(R.drawable.secure_notification_enabled) + insecureNetworkNotificationPreference.setIcon(R.drawable.insecure_notification_enabled) + antiquatedNetworkNotificationPreference.setIcon(R.drawable.antiquated_notification_enabled) + } else { + // Set the icons. + realtimeMonitoringPreference.setIcon(R.drawable.secure_notification_disabled) + secureNetworkNotificationPreference.setIcon(R.drawable.secure_notification_disabled) + insecureNetworkNotificationPreference.setIcon(R.drawable.insecure_notification_disabled) + antiquatedNetworkNotificationPreference.setIcon(R.drawable.antiquated_notification_disabled) + } + } else { + // Set the icons. + realtimeMonitoringPreference.setIcon(R.drawable.secure_notification_ghosted) + secureNetworkNotificationPreference.setIcon(R.drawable.secure_notification_ghosted) + insecureNetworkNotificationPreference.setIcon(R.drawable.insecure_notification_ghosted) + antiquatedNetworkNotificationPreference.setIcon(R.drawable.antiquated_notification_ghosted) + } + + // 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) + antiquatedNetworkNotificationPreference.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) + val antiquatedNetworkNotificationIntent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) + .putExtra(Settings.EXTRA_APP_PACKAGE, requireContext().packageName) + .putExtra(Settings.EXTRA_CHANNEL_ID, RealtimeMonitoringService.ANTIQUATED_NETWORK) + + // Set the notification preference intents. + secureNetworkNotificationPreference.intent = secureNetworkNotificationIntent + insecureNetworkNotificationPreference.intent = insecureNetworkNotificationIntent + antiquatedNetworkNotificationPreference.intent = antiquatedNetworkNotificationIntent + + // Set the consider 3G antiquated preference icon. + if (sharedPreferences.getBoolean(getString(R.string.consider_3g_antiquated_key), false)) { + consider3gAntiquatedPreference.setIcon(R.drawable.antiquated_3g_enabled) + } else { + consider3gAntiquatedPreference.setIcon(R.drawable.antiquated_3g_disabled) + } + // Set the bottom app bar preference icon. if (sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)) { - // Set the enabled icon. bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_enabled) } else { - // Set the disabled icon. bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled) } } @@ -63,7 +132,7 @@ class SettingsFragment : PreferenceFragmentCompat() { super.onPause() // Get a handle for the shared preferences. - val sharedPreferences = preferenceScreen.sharedPreferences + val sharedPreferences = preferenceScreen.sharedPreferences!! // Unregister the shared preference listener. sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener) @@ -78,7 +147,7 @@ class SettingsFragment : PreferenceFragmentCompat() { sharedPreferenceChangeListener = getSharedPreferenceChangeListener() // Get a handle for the shared preferences. - val sharedPreferences = preferenceScreen.sharedPreferences + val sharedPreferences = preferenceScreen.sharedPreferences!! // Re-register the shared preference listener. sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener) @@ -88,35 +157,79 @@ 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.bottom_app_bar_key), false)) { - // Set the enabled icon. - bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_enabled) + if (sharedPreferences.getBoolean(getString(R.string.realtime_monitoring_key), false)) { + // Set the icons. + realtimeMonitoringPreference.setIcon(R.drawable.secure_notification_enabled) + secureNetworkNotificationPreference.setIcon(R.drawable.secure_notification_enabled) + insecureNetworkNotificationPreference.setIcon(R.drawable.insecure_notification_enabled) + antiquatedNetworkNotificationPreference.setIcon(R.drawable.antiquated_notification_enabled) } else { - // Set the disabled icon. - bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled) + // Set the icons. + realtimeMonitoringPreference.setIcon(R.drawable.secure_notification_disabled) + secureNetworkNotificationPreference.setIcon(R.drawable.secure_notification_disabled) + insecureNetworkNotificationPreference.setIcon(R.drawable.insecure_notification_disabled) + antiquatedNetworkNotificationPreference.setIcon(R.drawable.antiquated_notification_disabled) } - // Create an intent to restart Privacy Cell. - val restartIntent = requireActivity().parentActivityIntent!! + // 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)) - // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`. - restartIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + // Cancel the realtime listener work request. + WorkManager.getInstance(requireContext()).cancelUniqueWork(getString(R.string.register_listener_work_request)) + } + } - // Create a handler to restart the activity. - val restartHandler = Handler(Looper.getMainLooper()) + getString(R.string.consider_3g_antiquated_key) -> { + // Update the icon. + if (sharedPreferences.getBoolean(getString(R.string.consider_3g_antiquated_key), false)) { + consider3gAntiquatedPreference.setIcon(R.drawable.antiquated_3g_enabled) + } else { + consider3gAntiquatedPreference.setIcon(R.drawable.antiquated_3g_disabled) + } - // Create a runnable to restart the activity. - val restartRunnable = Runnable { - // Restart the activity. - startActivity(restartIntent) + // Restart Privacy Cell. + restartPrivacyCell() + } + + getString(R.string.bottom_app_bar_key) -> { + // Update the icon. + if (sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)) { + bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_enabled) + } else { + bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled) } - // Restart the activity after 400 milliseconds, so that the app has enough time to save the change to the preference. - restartHandler.postDelayed(restartRunnable, 400) + // Restart Privacy Cell after 400 milliseconds. + restartPrivacyCell() } } } } + + private fun restartPrivacyCell() { + // Create an intent to restart Privacy Cell. + val restartIntent = requireActivity().parentActivityIntent!! + + // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`. + restartIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + + // Create a handler to restart the activity. + val restartHandler = Handler(Looper.getMainLooper()) + + // Create a runnable to restart the activity. + val restartRunnable = Runnable { + // Restart the activity. + startActivity(restartIntent) + } + + // Restart the activity after 400 milliseconds, so that the app has enough time to save the change to the preference. + restartHandler.postDelayed(restartRunnable, 400) + } } \ No newline at end of file