2 * Copyright © 2021 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
6 * Privacy Cell is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Cell is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Cell. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacycell.fragments
22 import android.content.Intent
23 import android.content.SharedPreferences
24 import android.os.Bundle
25 import android.os.Handler
26 import android.os.Looper
28 import androidx.preference.Preference
29 import androidx.preference.PreferenceFragmentCompat
31 import com.stoutner.privacycell.R
33 class SettingsFragment : PreferenceFragmentCompat() {
34 // Declare the class variables.
35 private lateinit var sharedPreferenceChangeListener: SharedPreferences.OnSharedPreferenceChangeListener
37 // Declare the class views.
38 private lateinit var bottomAppBarPreference: Preference
40 override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
41 // Load the preferences from the XML file.
42 setPreferencesFromResource(R.xml.preferences, rootKey)
44 // Get a handle for the shared preferences.
45 val sharedPreferences = preferenceScreen.sharedPreferences
47 // Get handles for the preferences.
48 bottomAppBarPreference = findPreference(getString(R.string.bottom_app_bar_key))!!
50 // Set the bottom app bar preference icon.
51 if (sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)) {
52 // Set the enabled icon.
53 bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_enabled)
55 // Set the disabled icon.
56 bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled)
60 // The listener should be unregistered when the app is paused.
61 override fun onPause() {
62 // Run the default commands.
65 // Get a handle for the shared preferences.
66 val sharedPreferences = preferenceScreen.sharedPreferences
68 // Unregister the shared preference listener.
69 sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
72 // The listener should be re-registered when the app is resumed.
73 override fun onResume() {
74 // Run the default commands.
77 // Get a new shared preference change listener.
78 sharedPreferenceChangeListener = getSharedPreferenceChangeListener()
80 // Get a handle for the shared preferences.
81 val sharedPreferences = preferenceScreen.sharedPreferences
83 // Re-register the shared preference listener.
84 sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
87 private fun getSharedPreferenceChangeListener(): SharedPreferences.OnSharedPreferenceChangeListener {
88 // Return the shared preference change listener.
89 return SharedPreferences.OnSharedPreferenceChangeListener {sharedPreferences, key ->
93 if (sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)) {
94 // Set the enabled icon.
95 bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_enabled)
97 // Set the disabled icon.
98 bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled)
101 // Create an intent to restart Privacy Cell.
102 val restartIntent = requireActivity().parentActivityIntent!!
104 // `Intent.FLAG_ACTIVITY_CLEAR_TASK` removes all activities from the stack. It requires `Intent.FLAG_ACTIVITY_NEW_TASK`.
105 restartIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
107 // Create a handler to restart the activity.
108 val restartHandler = Handler(Looper.getMainLooper())
110 // Create a runnable to restart the activity.
111 val restartRunnable = Runnable {
112 // Restart the activity.
113 startActivity(restartIntent)
116 // Restart the activity after 400 milliseconds, so that the app has enough time to save the change to the preference.
117 restartHandler.postDelayed(restartRunnable, 400)