Android: Prevent getSetting ClassCastExceptions in ItemViews

This commit is contained in:
Ryan Meredith 2020-08-21 11:09:24 -04:00
parent 905df6756b
commit d5ea4b4b80
8 changed files with 24 additions and 25 deletions

View File

@ -16,7 +16,7 @@ public final class CheckBoxSetting extends SettingsItem
public boolean isChecked() public boolean isChecked()
{ {
if (getSetting() == null) if (getSetting() == null || !(getSetting() instanceof BooleanSetting))
{ {
return mDefaultValue; return mDefaultValue;
} }
@ -34,7 +34,7 @@ public final class CheckBoxSetting extends SettingsItem
*/ */
public BooleanSetting setChecked(boolean checked) public BooleanSetting setChecked(boolean checked)
{ {
if (getSetting() == null) if (getSetting() == null || !(getSetting() instanceof BooleanSetting))
{ {
BooleanSetting setting = new BooleanSetting(getKey(), getSection(), checked); BooleanSetting setting = new BooleanSetting(getKey(), getSection(), checked);
setSetting(setting); setSetting(setting);

View File

@ -25,14 +25,13 @@ public final class FilePicker extends SettingsItem
public String getSelectedValue() public String getSelectedValue()
{ {
StringSetting setting = (StringSetting) getSetting(); if (getSetting() == null || !(getSetting() instanceof StringSetting))
if (setting == null)
{ {
return mDefaultValue; return mDefaultValue;
} }
else else
{ {
StringSetting setting = (StringSetting) getSetting();
return setting.getValue(); return setting.getValue();
} }
} }

View File

@ -22,7 +22,7 @@ public class InputBindingSetting extends SettingsItem
public String getValue() public String getValue()
{ {
if (getSetting() == null) if (getSetting() == null || !(getSetting() instanceof StringSetting))
{ {
return ""; return "";
} }
@ -78,7 +78,7 @@ public class InputBindingSetting extends SettingsItem
editor.putString(getKey() + gameId, ui); editor.putString(getKey() + gameId, ui);
editor.apply(); editor.apply();
if (getSetting() == null) if (getSetting() == null || !(getSetting() instanceof StringSetting))
{ {
StringSetting setting = new StringSetting(getKey(), getSection(), bind); StringSetting setting = new StringSetting(getKey(), getSection(), bind);
setSetting(setting); setSetting(setting);

View File

@ -22,7 +22,7 @@ public class RumbleBindingSetting extends InputBindingSetting
@Override @Override
public String getValue() public String getValue()
{ {
if (getSetting() == null) if (getSetting() == null || !(getSetting() instanceof StringSetting))
{ {
return ""; return "";
} }

View File

@ -40,14 +40,14 @@ public final class SingleChoiceSetting extends SettingsItem
public int getSelectedValue() public int getSelectedValue()
{ {
if (getSetting() != null) if (getSetting() == null || !(getSetting() instanceof IntSetting))
{ {
IntSetting setting = (IntSetting) getSetting(); return mDefaultValue;
return setting.getValue();
} }
else else
{ {
return mDefaultValue; IntSetting setting = (IntSetting) getSetting();
return setting.getValue();
} }
} }
@ -65,7 +65,7 @@ public final class SingleChoiceSetting extends SettingsItem
*/ */
public IntSetting setSelectedValue(int selection) public IntSetting setSelectedValue(int selection)
{ {
if (getSetting() == null) if (getSetting() == null || !(getSetting() instanceof IntSetting))
{ {
IntSetting setting = new IntSetting(getKey(), getSection(), selection); IntSetting setting = new IntSetting(getKey(), getSection(), selection);
setSetting(setting); setSetting(setting);

View File

@ -59,14 +59,14 @@ public final class SingleChoiceSettingDynamicDescriptions extends SettingsItem
public int getSelectedValue() public int getSelectedValue()
{ {
if (getSetting() != null) if (getSetting() == null || !(getSetting() instanceof IntSetting))
{ {
IntSetting setting = (IntSetting) getSetting(); return mDefaultValue;
return setting.getValue();
} }
else else
{ {
return mDefaultValue; IntSetting setting = (IntSetting) getSetting();
return setting.getValue();
} }
} }
@ -84,7 +84,7 @@ public final class SingleChoiceSettingDynamicDescriptions extends SettingsItem
*/ */
public IntSetting setSelectedValue(int selection) public IntSetting setSelectedValue(int selection)
{ {
if (getSetting() == null) if (getSetting() == null || !(getSetting() instanceof IntSetting))
{ {
IntSetting setting = new IntSetting(getKey(), getSection(), selection); IntSetting setting = new IntSetting(getKey(), getSection(), selection);
setSetting(setting); setSetting(setting);

View File

@ -75,7 +75,7 @@ public final class SliderSetting extends SettingsItem
*/ */
public IntSetting setSelectedValue(int selection) public IntSetting setSelectedValue(int selection)
{ {
if (getSetting() == null) if (getSetting() == null || !(getSetting() instanceof IntSetting))
{ {
IntSetting setting = new IntSetting(getKey(), getSection(), selection); IntSetting setting = new IntSetting(getKey(), getSection(), selection);
setSetting(setting); setSetting(setting);
@ -98,7 +98,7 @@ public final class SliderSetting extends SettingsItem
*/ */
public FloatSetting setSelectedValue(float selection) public FloatSetting setSelectedValue(float selection)
{ {
if (getSetting() == null) if (getSetting() == null || !(getSetting() instanceof FloatSetting))
{ {
FloatSetting setting = new FloatSetting(getKey(), getSection(), selection); FloatSetting setting = new FloatSetting(getKey(), getSection(), selection);
setSetting(setting); setSetting(setting);

View File

@ -71,14 +71,14 @@ public class StringSingleChoiceSetting extends SettingsItem
public String getSelectedValue() public String getSelectedValue()
{ {
if (getSetting() != null) if (getSetting() == null || !(getSetting() instanceof StringSetting))
{ {
StringSetting setting = (StringSetting) getSetting(); return mDefaultValue;
return setting.getValue();
} }
else else
{ {
return mDefaultValue; StringSetting setting = (StringSetting) getSetting();
return setting.getValue();
} }
} }
@ -110,7 +110,7 @@ public class StringSingleChoiceSetting extends SettingsItem
*/ */
public StringSetting setSelectedValue(String selection) public StringSetting setSelectedValue(String selection)
{ {
if (getSetting() == null) if (getSetting() == null || !(getSetting() instanceof StringSetting))
{ {
StringSetting setting = new StringSetting(getKey(), getSection(), selection); StringSetting setting = new StringSetting(getKey(), getSection(), selection);
setSetting(setting); setSetting(setting);