Skip to content

Commit 57de93c

Browse files
authored
Fix crash when opening settings after upgrading from previous version and having old theme selected (#1778)
2 parents 5ed8f29 + f5b1f07 commit 57de93c

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

Simplenote/src/main/java/com/automattic/simplenote/PreferencesFragment.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,6 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
215215
}
216216

217217
private void updateTheme(Activity activity, int index) {
218-
CharSequence[] entries = themePreference.getEntries();
219-
themePreference.setSummary(entries[index]);
220-
221218
AnalyticsTracker.track(
222219
AnalyticsTracker.Stat.SETTINGS_THEME_UPDATED,
223220
AnalyticsTracker.CATEGORY_USER,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.automattic.simplenote.utils
2+
3+
import android.content.Context
4+
import android.util.AttributeSet
5+
import androidx.preference.ListPreference
6+
7+
/**
8+
* Custom ListPreference that handles invalid index values to prevent ArrayIndexOutOfBoundsException.
9+
* This preference safely handles cases where the stored value doesn't match any of the available entries.
10+
*/
11+
class SafeThemeListPreference @JvmOverloads constructor(
12+
context: Context,
13+
attrs: AttributeSet? = null,
14+
defStyleAttr: Int = androidx.preference.R.attr.preferenceStyle,
15+
defStyleRes: Int = 0
16+
) : ListPreference(context, attrs, defStyleAttr, defStyleRes) {
17+
18+
override fun getSummary(): CharSequence? {
19+
val value = getValue()
20+
val entries = getEntries()
21+
22+
if (entries == null || entries.isEmpty()) {
23+
return super.getSummary()
24+
}
25+
26+
val index = findIndexOfValue(value)
27+
28+
// If index is invalid (out of bounds or -1), fallback to default value
29+
return if (index >= 0 && index < entries.size) {
30+
entries[index]
31+
} else {
32+
// Get the default value (2 = theme_system) and return its entry
33+
val defaultIndex = findIndexOfValue("2")
34+
if (defaultIndex >= 0 && defaultIndex < entries.size) {
35+
entries[defaultIndex]
36+
} else {
37+
// Ultimate fallback - return the first entry
38+
entries[0]
39+
}
40+
}
41+
}
42+
}

Simplenote/src/main/res/xml/preferences.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@
4141
android:key="pref_key_appearance_preferences"
4242
android:title="@string/appearance">
4343

44-
<ListPreference
44+
<com.automattic.simplenote.utils.SafeThemeListPreference
4545
android:key="pref_key_theme"
4646
android:defaultValue="2"
4747
android:entries="@array/array_theme_names"
4848
android:entryValues="@array/array_theme_values"
49-
android:summary="%s"
5049
android:title="@string/theme"
5150
tools:summary="@string/theme_system">
52-
</ListPreference>
51+
</com.automattic.simplenote.utils.SafeThemeListPreference>
5352

5453
<Preference
5554
android:key="pref_key_style"

0 commit comments

Comments
 (0)