Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,6 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}

private void updateTheme(Activity activity, int index) {
CharSequence[] entries = themePreference.getEntries();
themePreference.setSummary(entries[index]);

AnalyticsTracker.track(
AnalyticsTracker.Stat.SETTINGS_THEME_UPDATED,
AnalyticsTracker.CATEGORY_USER,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.automattic.simplenote.utils

import android.content.Context
import android.util.AttributeSet
import androidx.preference.ListPreference

/**
* Custom ListPreference that handles invalid index values to prevent ArrayIndexOutOfBoundsException.
* This preference safely handles cases where the stored value doesn't match any of the available entries.
*/
class SafeThemeListPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = androidx.preference.R.attr.preferenceStyle,
defStyleRes: Int = 0
) : ListPreference(context, attrs, defStyleAttr, defStyleRes) {

override fun getSummary(): CharSequence? {
val value = getValue()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mzorz - is there a change that getValueI() can return null? I'm can't tell if findIndexOfValue(value) handles that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great observation! exactly that case is handled in findIndexOfValue, so no worries 👍

val entries = getEntries()

if (entries == null || entries.isEmpty()) {
return super.getSummary()
}

val index = findIndexOfValue(value)

// If index is invalid (out of bounds or -1), fallback to default value
return if (index >= 0 && index < entries.size) {
entries[index]
} else {
// Get the default value (2 = theme_system) and return its entry
val defaultIndex = findIndexOfValue("2")
if (defaultIndex >= 0 && defaultIndex < entries.size) {
entries[defaultIndex]
} else {
// Ultimate fallback - return the first entry
entries[0]
}
}
}
}
5 changes: 2 additions & 3 deletions Simplenote/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@
android:key="pref_key_appearance_preferences"
android:title="@string/appearance">

<ListPreference
<com.automattic.simplenote.utils.SafeThemeListPreference
android:key="pref_key_theme"
android:defaultValue="2"
android:entries="@array/array_theme_names"
android:entryValues="@array/array_theme_values"
android:summary="%s"
android:title="@string/theme"
tools:summary="@string/theme_system">
</ListPreference>
</com.automattic.simplenote.utils.SafeThemeListPreference>

<Preference
android:key="pref_key_style"
Expand Down
Loading