Fail safety: if settings cannot loaded (invalid data in sharedpreferences), reset it and try again.

This commit is contained in:
Dennis Guse
2019-11-07 18:59:30 +01:00
parent e5d1048c8d
commit 40f2463f09
2 changed files with 16 additions and 3 deletions
@@ -2,6 +2,7 @@ package de.dennisguse.opentracks.settings;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
@@ -69,7 +70,15 @@ public class SettingsActivity extends AppCompatActivity implements ChooseActivit
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.settings, rootKey);
try {
setPreferencesFromResource(R.xml.settings, rootKey);
} catch (ClassCastException e) {
// Some sharedPreference is broken: delete all and reset them; it is just a last resort...
Toast.makeText(getContext(), R.string.settings_error_initial_values_restored, Toast.LENGTH_LONG).show();
PreferencesUtils.resetPreferences(getContext(), true);
setPreferencesFromResource(R.xml.settings, rootKey);
}
updateUnits();
}
@@ -94,7 +103,6 @@ public class SettingsActivity extends AppCompatActivity implements ChooseActivit
}
private void configListPreference(int key, int valueArray, boolean metricUnits) {
//TODO Can we make values an int array?
String[] values = getResources().getStringArray(valueArray);
final String[] options = new String[values.length];
switch (key) {
@@ -162,6 +170,7 @@ public class SettingsActivity extends AppCompatActivity implements ChooseActivit
private void updateUnits() {
boolean metricUnits = PreferencesUtils.isMetricUnits(getActivity());
//TODO Refactor this!
configFrequencyPreference(R.string.voice_frequency_key, metricUnits);
configFrequencyPreference(R.string.split_frequency_key, metricUnits);
configListPreference(R.string.min_recording_interval_key, R.array.min_recording_interval_values, metricUnits);
+5 -1
View File
@@ -1219,7 +1219,11 @@ limitations under the License.
All settings have been reverted to the default values
</string>
<!-- Settings Sensor -->
<string name="settings_error_initial_values_restored" translation_description="If the ">
Sorry, but an error occurred: and the all settings were reset to there default values.
</string>
<!-- Settings Sensor -->
<string name="settings_sensor" translation_description="In the 'Settings' page, the option to change the sensor settings.
Character limit: ~26 (1 line)">