+
+ // Check if the fragment has been restarted.
+ if (savedInstanceState != null) {
+ // Set the fragment restored flag.
+ fragmentRestarted = true
+
+ // Save the scroll Y.
+ scrollY = savedInstanceState.getInt(SCROLL_Y)
+ }
+ }
+
+ private fun getSharedPreferenceChangeListener(): OnSharedPreferenceChangeListener {
+ // Return the shared preference change listener.
+ return OnSharedPreferenceChangeListener { sharedPreferences: SharedPreferences, key: String? ->
+ when (key) {
+ getString(R.string.realtime_monitoring_key) -> {
+ // Update the 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)
+ }
+
+ // Start or stop the service.
+ if (sharedPreferences.getBoolean(getString(R.string.realtime_monitoring_key), false)) { // Realtime monitoring has been enabled.
+ // Start the service according to the API.
+ if (Build.VERSION.SDK_INT >= 33) { // The device API is >= 33.
+ // Check to see if the post notification has been granted.
+ if (ActivityCompat.checkSelfPermission(requireContext(), Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) { // The permission has been granted.
+ // Start the realtime monitoring service.
+ requireActivity().startService(Intent(context, RealtimeMonitoringService::class.java))
+ } else { // The post notification permission has not been granted.
+ // Check if the user has previously denied the post notifications permission.
+ if (ActivityCompat.shouldShowRequestPermissionRationale(requireActivity(), Manifest.permission.POST_NOTIFICATIONS)) { // Show a dialog explaining the request first.
+ // Check to see if a notification permission dialog is already displayed. This happens if the app is restarted while the dialog is shown.
+ if (requireActivity().supportFragmentManager.findFragmentByTag(getString(R.string.notification_permission)) == null) { // No dialog is currently shown.
+ // Instantiate the notification permission dialog fragment.
+ val notificationPermissionDialogFragment = NotificationPermissionDialog()
+
+ // Show the notification permission alert dialog. The permission will be requested when the dialog is closed.
+ notificationPermissionDialogFragment.show(requireActivity().supportFragmentManager, getString(R.string.notification_permission))
+ }
+ } else { // Show the permission request directly.
+ // Request the post notifications permission directly.
+ ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.POST_NOTIFICATIONS), PrivacyCellActivity.NOTIFICATION_PERMISSION_REQUEST_CODE)
+ }
+ }
+ } else { // The device API is < 33.
+ // 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))
+
+ // Cancel the realtime listener work request.
+ WorkManager.getInstance(requireContext()).cancelUniqueWork(getString(R.string.register_listener_work_request))
+ }
+ }
+
+ 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)
+ }
+
+ // 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 Privacy Cell after 400 milliseconds.
+ restartPrivacyCell()
+ }
+ }
+ }