/* * Copyright © 2021 Soren Stoutner . * * This file is part of Privacy Cell . * * Privacy Cell is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Privacy Cell is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Privacy Cell. If not, see . */ package com.stoutner.privacycell.fragments import android.content.Intent import android.content.SharedPreferences 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 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 bottomAppBarPreference: Preference override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { // Load the preferences from the XML file. setPreferencesFromResource(R.xml.preferences, rootKey) // Get a handle for the shared preferences. val sharedPreferences = preferenceScreen.sharedPreferences // Get handles for the preferences. realtimeMonitoringPreference = findPreference(getString(R.string.realtime_monitoring_key))!! val secureNetworkNotificationPreference = findPreference(getString(R.string.secure_network_notification_key))!! val insecureNetworkNotificationPreference = findPreference(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. bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_enabled) } else { // Set the disabled icon. 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 // Re-register the shared preference listener. sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener) } private fun getSharedPreferenceChangeListener(): SharedPreferences.OnSharedPreferenceChangeListener { // Return the shared preference change listener. return SharedPreferences.OnSharedPreferenceChangeListener {sharedPreferences, key -> when (key) { 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)) // Cancel the realtime listener work request. WorkManager.getInstance(requireContext()).cancelUniqueWork(getString(R.string.register_listener_work_request)) } } 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. bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_enabled) } else { // Set the disabled icon. bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled) } // 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) } } } } }