Added an option to reset all preferences and implemented a different popup for selecting preferences. Also fixed a context menu bug.

This commit is contained in:
ottoptj 2024-09-02 06:09:25 +03:00
commit be4dd79e89
11 changed files with 309 additions and 90 deletions

View file

@ -17,13 +17,15 @@ class SettingsFragment : PreferenceFragmentCompat(), TitleProvider {
sharedPreferenceManager = SharedPreferenceManager(requireContext())
val homePref = findPreference<Preference?>("defaultHome")
val homePref = findPreference<Preference>("defaultHome")
val uiSettings = findPreference<Preference>("uiSettings")
val homeSettings = findPreference<Preference>("homeSettings")
val appMenuSettings = findPreference<Preference>("appMenuSettings")
val hiddenPref = findPreference<Preference?>("hiddenApps")
val aboutPref = findPreference<Preference?>("aboutPage")
val hiddenPref = findPreference<Preference>("hiddenApps")
val aboutPref = findPreference<Preference>("aboutPage")
val resetPref = findPreference<Preference>("resetAll")
homePref?.onPreferenceClickListener =
Preference.OnPreferenceClickListener {
@ -35,6 +37,15 @@ class SettingsFragment : PreferenceFragmentCompat(), TitleProvider {
}
true }
uiSettings?.onPreferenceClickListener =
Preference.OnPreferenceClickListener {
requireActivity().supportFragmentManager
.beginTransaction()
.replace(R.id.settingsLayout, UISettingsFragment())
.addToBackStack(null)
.commit()
true }
homeSettings?.onPreferenceClickListener =
Preference.OnPreferenceClickListener {
requireActivity().supportFragmentManager
@ -71,10 +82,14 @@ class SettingsFragment : PreferenceFragmentCompat(), TitleProvider {
.commit()
true }
resetPref?.onPreferenceClickListener =
Preference.OnPreferenceClickListener {
sharedPreferenceManager.resetAllPreferences(requireActivity())
true }
}
override fun getTitle(): String {
return "Launcher Settings"
}
}

View file

@ -1,10 +1,13 @@
package eu.ottop.yamlauncher.settings
import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.util.TypedValue
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import androidx.preference.PreferenceManager
import eu.ottop.yamlauncher.R
class SharedPreferenceManager (private val context: Context) {
@ -221,4 +224,39 @@ class SharedPreferenceManager (private val context: Context) {
editor.remove("name$packageName-$profile")
editor.apply()
}
fun resetAllPreferences(activity: FragmentActivity) {
AlertDialog.Builder(context).apply {
setTitle("Confirmation")
setMessage("You will lose ALL changes that you have made to the launcher settings. Are you sure?")
setPositiveButton("Yes") { _, _ ->
performReset(activity)
}
setNegativeButton("Cancel") { _, _ ->
}
}.create().show()
}
private fun performReset(activity: FragmentActivity) {
val editor = preferences.edit()
editor.clear()
editor.apply()
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.settingsLayout, UISettingsFragment())
.commit()
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.settingsLayout, HomeSettingsFragment())
.commit()
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.settingsLayout, AppMenuSettingsFragment())
.commit()
activity.supportFragmentManager
.beginTransaction()
.replace(R.id.settingsLayout, SettingsFragment())
.commit()
}
}

View file

@ -0,0 +1,88 @@
package eu.ottop.yamlauncher.settings
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import eu.ottop.yamlauncher.R
class SpinnerPreference (context: Context, attrs: AttributeSet? = null): Preference(context, attrs) {
private var entries: Array<CharSequence>? = null
private var entryValues: Array<CharSequence>? = null
private var currentValue: String? = null
private var defaultNo: String? = null
private var spinner: Spinner? = null
init {
widgetLayoutResource = R.layout.preference_spinner
context.theme.obtainStyledAttributes(
attrs,
R.styleable.SpinnerPreference,
0, 0).apply {
try {
entries = getTextArray(R.styleable.SpinnerPreference_android_entries)
entryValues = getTextArray(R.styleable.SpinnerPreference_android_entryValues)
defaultNo = getString(R.styleable.SpinnerPreference_android_defaultValue)
} finally {
recycle()
}
}}
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
spinner = holder.findViewById(R.id.preferenceOptions) as Spinner
if (entries != null) {
val adapter = ArrayAdapter(context, android.R.layout.simple_spinner_item, entries!!)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner?.adapter = adapter
}
val selectedIndex = entryValues?.indexOf(currentValue as? CharSequence) ?: 0
spinner?.setSelection(selectedIndex)
// Somehow prevents an error :D
val handler = Handler(Looper.getMainLooper())
handler.postDelayed({
if (selectedIndex >= 0) {
summary = entries?.get(selectedIndex)
}
}, 0)
spinner?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
val newValue = entryValues?.get(position).toString()
if (callChangeListener(newValue)) {
currentValue = newValue
persistString(newValue)
summary = entries?.get(position)
}
}
override fun onNothingSelected(parent: AdapterView<*>) {}
}
}
override fun onClick() {
// Open the spinner dropdown when the preference is clicked
spinner?.performClick()
}
override fun onSetInitialValue(defaultValue: Any?) {
currentValue = getPersistedString(defaultValue as? String)
}
// This is required to ensure that default values are stored (needed for full settings reset)
override fun onAttached() {
super.onAttached()
persistString(getPersistedString(defaultNo))
}
}

View file

@ -0,0 +1,16 @@
package eu.ottop.yamlauncher.settings
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import eu.ottop.yamlauncher.R
class UISettingsFragment : PreferenceFragmentCompat(), TitleProvider {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.ui_preferences, rootKey)
}
override fun getTitle(): String {
return "General UI Settings"
}
}

View file

@ -88,6 +88,7 @@
android:background="#A7000000"
android:foreground="@drawable/app_action_foreground"
android:paddingVertical="3dp"
android:singleLine="true"
android:text="@string/info"
android:textAlignment="center"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"
@ -102,6 +103,7 @@
android:background="#A7000000"
android:foreground="@drawable/app_action_foreground"
android:paddingVertical="3dp"
android:singleLine="true"
android:text="@string/uninstall"
android:textAlignment="center"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"
@ -116,6 +118,7 @@
android:background="#A7000000"
android:foreground="@drawable/app_action_foreground"
android:paddingVertical="3dp"
android:singleLine="true"
android:text="@string/rename"
android:textAlignment="center"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"
@ -130,6 +133,7 @@
android:background="#A7000000"
android:foreground="@drawable/app_action_foreground"
android:paddingVertical="3dp"
android:singleLine="true"
android:text="@string/hide"
android:textAlignment="center"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"
@ -144,6 +148,7 @@
android:background="#A7000000"
android:foreground="@drawable/app_action_foreground"
android:paddingVertical="3dp"
android:singleLine="true"
android:text="@string/close"
android:textAlignment="center"
android:textAppearance="@android:style/TextAppearance.DeviceDefault"

View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="invisible">
<Spinner
android:id="@+id/preferenceOptions"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:spinnerMode="dialog" />
</LinearLayout>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SpinnerPreference">
<attr name="android:entries" />
<attr name="android:entryValues" />
<attr name="android:defaultValue" />
</declare-styleable>
</resources>

View file

@ -1,34 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
app:allowDividerAbove="false"
app:title="Apps">
<ListPreference
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="left"
app:entries="@array/h_alignment_options"
app:entryValues="@array/h_alignment_values"
android:defaultValue="left"
android:entries="@array/h_alignment_options"
android:entryValues="@array/h_alignment_values"
app:key="appMenuAlignment"
app:title="App Alignment"
app:useSimpleSummaryProvider="true" />
<ListPreference
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="medium"
app:entries="@array/size_options"
app:entryValues="@array/size_values"
android:defaultValue="medium"
android:entries="@array/size_options"
android:entryValues="@array/size_values"
app:key="appMenuSize"
app:title="App Size"
app:useSimpleSummaryProvider="true" />
<ListPreference
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="20"
app:entries="@array/app_spacing_options"
app:entryValues="@array/app_spacing_values"
android:defaultValue="20"
android:entries="@array/app_spacing_options"
android:entryValues="@array/app_spacing_values"
app:key="appSpacing"
app:title="App Spacing"
app:useSimpleSummaryProvider="true" />
@ -44,23 +44,23 @@
android:defaultValue="true"
android:title="Enable Search"
app:key="searchEnabled" />
<ListPreference
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="left"
android:defaultValue="left"
app:dependency="searchEnabled"
app:entries="@array/h_alignment_options"
app:entryValues="@array/h_alignment_values"
android:entries="@array/h_alignment_options"
android:entryValues="@array/h_alignment_values"
app:key="searchAlignment"
app:title="Search Alignment"
app:useSimpleSummaryProvider="true" />
<ListPreference
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="medium"
android:defaultValue="medium"
app:dependency="searchEnabled"
app:entries="@array/size_options"
app:entryValues="@array/size_values"
android:entries="@array/size_options"
android:entryValues="@array/size_values"
app:key="searchSize"
app:title="Search Size"
app:useSimpleSummaryProvider="true" />
@ -74,4 +74,4 @@
</PreferenceCategory>
</androidx.preference.PreferenceScreen>
</PreferenceScreen>

View file

@ -13,21 +13,21 @@
android:defaultValue="true"
android:title="Show Clock"
app:key="clockEnabled" />
<ListPreference
app:defaultValue="left"
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:defaultValue="left"
app:dependency="clockEnabled"
app:entries="@array/h_alignment_options"
app:entryValues="@array/h_alignment_values"
android:entries="@array/h_alignment_options"
android:entryValues="@array/h_alignment_values"
app:key="clockAlignment"
app:title="Clock Alignment"
app:useSimpleSummaryProvider="true" />
<ListPreference
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="medium"
android:defaultValue="medium"
app:dependency="clockEnabled"
app:entries="@array/size_options"
app:entryValues="@array/size_values"
android:entries="@array/size_options"
android:entryValues="@array/size_values"
app:key="clockSize"
app:title="Clock Size"
app:useSimpleSummaryProvider="true" />
@ -50,13 +50,13 @@
android:defaultValue="true"
android:title="Show Date"
app:key="dateEnabled" />
<ListPreference
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="medium"
android:defaultValue="medium"
app:dependency="dateEnabled"
app:entries="@array/size_options"
app:entryValues="@array/size_values"
android:entries="@array/size_options"
android:entryValues="@array/size_values"
app:key="dateSize"
app:title="Date Size"
app:useSimpleSummaryProvider="true" />
@ -102,13 +102,13 @@
app:key="manualLocation"
app:selectable="true"
app:title="Set Manual Location" />
<ListPreference
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="celsius"
android:defaultValue="celsius"
app:dependency="weatherEnabled"
app:entries="@array/temp_units"
app:entryValues="@array/unit_values"
android:entries="@array/temp_units"
android:entryValues="@array/unit_values"
app:key="tempUnits"
app:title="Units"
app:useSimpleSummaryProvider="true" />
@ -119,39 +119,39 @@
app:allowDividerAbove="false"
app:allowDividerBelow="false"
app:title="Shortcuts">
<ListPreference
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="4"
app:entries="@array/shortcut_options"
app:entryValues="@array/shortcut_options"
android:defaultValue="4"
android:entries="@array/shortcut_options"
android:entryValues="@array/shortcut_options"
app:key="shortcutNo"
app:title="Number of Shortcuts"
app:useSimpleSummaryProvider="true" />
<ListPreference
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="left"
app:entries="@array/h_alignment_options"
app:entryValues="@array/h_alignment_values"
android:defaultValue="left"
android:entries="@array/h_alignment_options"
android:entryValues="@array/h_alignment_values"
app:key="shortcutAlignment"
app:title="Shortcut Alignment"
app:useSimpleSummaryProvider="true" />
<ListPreference
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="medium"
app:entries="@array/size_options"
app:entryValues="@array/size_values"
android:defaultValue="medium"
android:entries="@array/size_options"
android:entryValues="@array/size_values"
app:key="shortcutSize"
app:title="Shortcut Size"
app:useSimpleSummaryProvider="true" />
<ListPreference
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="0.11"
app:entries="@array/shortcut_spacing_options"
app:entryValues="@array/shortcut_spacing_values"
android:defaultValue="0.11"
android:entries="@array/shortcut_spacing_options"
android:entryValues="@array/shortcut_spacing_values"
app:key="shortcutWeight"
app:title="Shortcut Spacing"
app:useSimpleSummaryProvider="true" />

View file

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
@ -7,44 +8,19 @@
app:key="defaultHome"
app:selectable="true"
app:title="Set Default Home" />
<PreferenceCategory
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:allowDividerAbove="false"
app:title="Customization">
<ListPreference
app:title="Customization" >
<Preference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="#00000000"
app:entries="@array/bg_options"
app:entryValues="@array/bg_values"
app:key="bgColor"
app:title="Background Color"
app:useSimpleSummaryProvider="true" />
<ListPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="#FFF3F3F3"
app:entries="@array/color_options"
app:entryValues="@array/color_values"
app:key="textColor"
app:title="Text Color"
app:useSimpleSummaryProvider="true" />
<SwitchPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="false"
android:title="Show Status Bar"
app:key="barVisibility" />
<ListPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:defaultValue="200"
app:entries="@array/animation_options"
app:entryValues="@array/animation_values"
app:key="animationSpeed"
app:title="Animation Speed"
app:useSimpleSummaryProvider="true" />
app:key="uiSettings"
app:selectable="true"
app:summary="Configure the overall look"
app:title="General UI" />
<Preference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -85,5 +61,17 @@
app:selectable="true"
app:title="About YAM Launcher" />
</PreferenceCategory>
<PreferenceCategory
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:allowDividerAbove="false"
app:title="Reset">
<Preference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:key="resetAll"
app:selectable="true"
app:title="Reset All Settings" />
</PreferenceCategory>
</PreferenceScreen>

View file

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:title="Colors"
app:allowDividerAbove="false">
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="#00000000"
android:entries="@array/bg_options"
android:entryValues="@array/bg_values"
app:key="bgColor"
app:title="Background Color"
app:useSimpleSummaryProvider="true" />
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="#FFF3F3F3"
android:entries="@array/color_options"
android:entryValues="@array/color_values"
app:key="textColor"
app:title="Text Color"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory
android:title="Operation"
app:allowDividerAbove="false">
<eu.ottop.yamlauncher.settings.SpinnerPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="200"
android:entries="@array/animation_options"
android:entryValues="@array/animation_values"
app:key="animationSpeed"
app:title="Animation Speed"
app:useSimpleSummaryProvider="true" />
<SwitchPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="false"
android:title="Show Status Bar"
app:key="barVisibility" />
</PreferenceCategory>
</PreferenceScreen>