+ // 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!!
+
+ // `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)
+ }
+
+ 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)