]> gitweb.stoutner.com Git - PrivacyCell.git/blob - app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt
0cf6c6f088f043c35d52325eb2d8c474adfd3543
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / fragments / SettingsFragment.kt
1 /*
2  * Copyright © 2021 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacycell.fragments
21
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
27
28 import androidx.preference.Preference
29 import androidx.preference.PreferenceFragmentCompat
30
31 import com.stoutner.privacycell.R
32
33 class SettingsFragment : PreferenceFragmentCompat() {
34     // Declare the class variables.
35     private lateinit var sharedPreferenceChangeListener: SharedPreferences.OnSharedPreferenceChangeListener
36
37     // Declare the class views.
38     private lateinit var bottomAppBarPreference: Preference
39
40     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
41         // Load the preferences from the XML file.
42         setPreferencesFromResource(R.xml.preferences, rootKey)
43
44         // Get a handle for the shared preferences.
45         val sharedPreferences = preferenceScreen.sharedPreferences
46
47         // Get handles for the preferences.
48         bottomAppBarPreference = findPreference(getString(R.string.bottom_app_bar_key))!!
49
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)
54         } else {
55             // Set the disabled icon.
56             bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled)
57         }
58     }
59
60     // The listener should be unregistered when the app is paused.
61     override fun onPause() {
62         // Run the default commands.
63         super.onPause()
64
65         // Get a handle for the shared preferences.
66         val sharedPreferences = preferenceScreen.sharedPreferences
67
68         // Unregister the shared preference listener.
69         sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
70     }
71
72     // The listener should be re-registered when the app is resumed.
73     override fun onResume() {
74         // Run the default commands.
75         super.onResume()
76
77         // Get a new shared preference change listener.
78         sharedPreferenceChangeListener = getSharedPreferenceChangeListener()
79
80         // Get a handle for the shared preferences.
81         val sharedPreferences = preferenceScreen.sharedPreferences
82
83         // Re-register the shared preference listener.
84         sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
85     }
86
87     private fun getSharedPreferenceChangeListener(): SharedPreferences.OnSharedPreferenceChangeListener {
88         // Return the shared preference change listener.
89         return SharedPreferences.OnSharedPreferenceChangeListener {sharedPreferences, key ->
90             when (key) {
91                 "bottom_app_bar" -> {
92                     // Update the icon.
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)
96                     } else {
97                         // Set the disabled icon.
98                         bottomAppBarPreference.setIcon(R.drawable.bottom_app_bar_disabled)
99                     }
100
101                     // Create an intent to restart Privacy Cell.
102                     val restartIntent = requireActivity().parentActivityIntent!!
103
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
106
107                     // Create a handler to restart the activity.
108                     val restartHandler = Handler(Looper.getMainLooper())
109
110                     // Create a runnable to restart the activity.
111                     val restartRunnable = Runnable {
112                         // Restart the activity.
113                         startActivity(restartIntent)
114                     }
115
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)
118                 }
119             }
120         }
121     }
122 }