/*
- * Copyright 2021-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2021-2023 Soren Stoutner <soren@stoutner.com>.
*
* This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
*
import com.stoutner.privacycell.dialogs.NotificationPermissionDialog
import com.stoutner.privacycell.services.RealtimeMonitoringService
+// Define the class constants.
+private const val SCROLL_Y = "scroll_y"
+
class SettingsFragment : PreferenceFragmentCompat() {
// Declare the class variables.
private lateinit var sharedPreferenceChangeListener: OnSharedPreferenceChangeListener
private lateinit var consider3gAntiquatedPreference: Preference
private lateinit var bottomAppBarPreference: Preference
+ companion object {
+ // Declare the private static class variables. For some reason (I'm looking at you Android's Activity Lifecycle) this only works if these are static.
+ private var fragmentRestarted: Boolean = false
+ private var scrollY: Int = 0
+ }
+
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
// Load the preferences from the XML file.
setPreferencesFromResource(R.xml.preferences, rootKey)
} else {
bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled)
}
- }
-
- // The listener should be unregistered when the app is paused.
- override fun onPause() {
- // Run the default commands.
- super.onPause()
-
- // Get a handle for the shared preferences.
- val sharedPreferences = preferenceScreen.sharedPreferences!!
-
- // Unregister the shared preference listener.
- sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
- }
-
- // The listener should be re-registered when the app is resumed.
- override fun onResume() {
- // Run the default commands.
- super.onResume()
- // Get a new shared preference change listener.
- sharedPreferenceChangeListener = getSharedPreferenceChangeListener()
-
- // Get a handle for the shared preferences.
- val sharedPreferences = preferenceScreen.sharedPreferences!!
+ // Check if the fragment has been restarted.
+ if (savedInstanceState != null) {
+ // Set the fragment restarted flag.
+ fragmentRestarted = true
- // Re-register the shared preference listener.
- sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
+ // Save the scroll Y.
+ scrollY = savedInstanceState.getInt(SCROLL_Y)
+ }
}
private fun getSharedPreferenceChangeListener(): OnSharedPreferenceChangeListener {
}
}
+ // The listener should be unregistered when the app is paused.
+ override fun onPause() {
+ // Run the default commands.
+ super.onPause()
+
+ // Get a handle for the shared preferences.
+ val sharedPreferences = preferenceScreen.sharedPreferences!!
+
+ // Unregister the shared preference listener.
+ sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
+ }
+
+ // The listener should be re-registered when the app is resumed.
+ override fun onResume() {
+ // Run the default commands.
+ super.onResume()
+
+ // Get a new shared preference change listener.
+ sharedPreferenceChangeListener = getSharedPreferenceChangeListener()
+
+ // Get a handle for the shared preferences.
+ val sharedPreferences = preferenceScreen.sharedPreferences!!
+
+ // Re-register the shared preference listener.
+ sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
+
+ // Update the realtime monitoring preference summary.
+ updateRealtimeMonitoringSummary()
+
+ // Restore the scroll position if the fragment has been restarted.
+ if (fragmentRestarted) {
+ // Reset the fragment restarted flag.
+ fragmentRestarted = false
+
+ // Set the scroll position.
+ listView.smoothScrollBy(0, scrollY)
+ }
+ }
+
+ override fun onSaveInstanceState(savedInstanceState: Bundle) {
+ // Run the default commands.
+ super.onSaveInstanceState(savedInstanceState)
+
+ // Save the scroll position.
+ savedInstanceState.putInt(SCROLL_Y, listView.computeVerticalScrollOffset())
+ }
+
private fun restartPrivacyCell() {
// Create an intent to restart Privacy Cell.
val restartIntent = requireActivity().parentActivityIntent!!
// Restart the activity after 400 milliseconds, so that the app has enough time to save the change to the preference.
restartHandler.postDelayed(restartRunnable, 400)
}
+
+ fun updateRealtimeMonitoringSummary() {
+ // Update the summary according to the API.
+ if (Build.VERSION.SDK_INT >= 33) { // The API >= 33.
+ // Set the summary according to the notification permission status.
+ if (ActivityCompat.checkSelfPermission(requireContext(), Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED)
+ realtimeMonitoringPreference.summary = getString(R.string.realtime_monitoring_summary)
+ else
+ realtimeMonitoringPreference.summary = (getString(R.string.realtime_monitoring_summary) + " " + getString(R.string.notification_permission_denied))
+ } else { // The API is < 33.
+ // Set the realtime monitoring summary.
+ realtimeMonitoringPreference.summary = getString(R.string.realtime_monitoring_summary)
+ }
+ }
}