+/*
+ * Copyright © 2021 Soren Stoutner <soren@stoutner.com>.
+ *
+ * This file is part of Privacy Cell <https://www.stoutner.com/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 <http://www.gnu.org/licenses/>.
+ */
+
+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 androidx.preference.Preference
+import androidx.preference.PreferenceFragmentCompat
+
+import com.stoutner.privacycell.R
+
+class SettingsFragment : PreferenceFragmentCompat() {
+ // Declare the class variables.
+ private lateinit var sharedPreferenceChangeListener: SharedPreferences.OnSharedPreferenceChangeListener
+
+ // Declare the class views.
+ 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.
+ bottomAppBarPreference = findPreference(getString(R.string.bottom_app_bar_key))!!
+
+ // 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) {
+ "bottom_app_bar" -> {
+ // 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)
+ }
+ }
+ }
+ }
+}
\ No newline at end of file