X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyCell.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacycell%2Ffragments%2FSettingsFragment.kt;fp=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacycell%2Ffragments%2FSettingsFragment.kt;h=0cf6c6f088f043c35d52325eb2d8c474adfd3543;hp=0000000000000000000000000000000000000000;hb=031b4a8ea78fdbb776a1c5991f246b4f2a3e7ed1;hpb=1b722c3d327fa2d571abee745a115a1f54ab2a27 diff --git a/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt b/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt new file mode 100644 index 0000000..0cf6c6f --- /dev/null +++ b/app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt @@ -0,0 +1,122 @@ +/* + * 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 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