From 186efc0a23fb1d4ea8d2159f78408cadca72b87e Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Sun, 21 Jul 2019 18:15:34 +0200 Subject: [PATCH] Removed IntegerListPreference and use a ListPreference instead. --- .../settings/AdvancedSettingsActivity.java | 3 +- .../settings/IntegerListPreference.java | 96 ------------------- .../apps/mytracks/util/PreferencesUtils.java | 37 ++----- src/main/res/xml/settings_advanced.xml | 2 +- src/main/res/xml/settings_recording.xml | 14 +-- 5 files changed, 19 insertions(+), 133 deletions(-) delete mode 100644 src/main/java/com/google/android/apps/mytracks/settings/IntegerListPreference.java diff --git a/src/main/java/com/google/android/apps/mytracks/settings/AdvancedSettingsActivity.java b/src/main/java/com/google/android/apps/mytracks/settings/AdvancedSettingsActivity.java index bab10c62d..0c61db445 100644 --- a/src/main/java/com/google/android/apps/mytracks/settings/AdvancedSettingsActivity.java +++ b/src/main/java/com/google/android/apps/mytracks/settings/AdvancedSettingsActivity.java @@ -80,8 +80,7 @@ public class AdvancedSettingsActivity extends AbstractSettingsActivity { addPreferencesFromResource(R.xml.settings_advanced); ListPreference preference = (ListPreference) findPreference(getString(R.string.photo_size_key)); - int value = PreferencesUtils.getInt( - this, R.string.photo_size_key, PreferencesUtils.PHOTO_SIZE_DEFAULT); + int value = PreferencesUtils.getInt(this, R.string.photo_size_key, PreferencesUtils.PHOTO_SIZE_DEFAULT); String[] values = getResources().getStringArray(R.array.photo_size_values); String[] options = new String[values.length]; String[] summary = new String[values.length]; diff --git a/src/main/java/com/google/android/apps/mytracks/settings/IntegerListPreference.java b/src/main/java/com/google/android/apps/mytracks/settings/IntegerListPreference.java deleted file mode 100644 index f121ac51d..000000000 --- a/src/main/java/com/google/android/apps/mytracks/settings/IntegerListPreference.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2010 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.google.android.apps.mytracks.settings; - -import android.content.Context; -import android.preference.ListPreference; -import android.util.AttributeSet; - -/** - * A list preference which persists its values as integers instead of strings. - * Code reading the values should use - * {@link android.content.SharedPreferences#getInt}. - * When using XML-declared arrays for entry values, the arrays should be regular - * string arrays containing valid integer values. - * - * @author Rodrigo Damazio - */ -@Deprecated -public class IntegerListPreference extends ListPreference { - - public IntegerListPreference(Context context) { - super(context); - - verifyEntryValues(null); - } - - public IntegerListPreference(Context context, AttributeSet attrs) { - super(context, attrs); - - verifyEntryValues(null); - } - - @Override - public void setEntryValues(CharSequence[] entryValues) { - CharSequence[] oldValues = getEntryValues(); - super.setEntryValues(entryValues); - verifyEntryValues(oldValues); - } - - @Override - public void setEntryValues(int entryValuesResId) { - CharSequence[] oldValues = getEntryValues(); - super.setEntryValues(entryValuesResId); - verifyEntryValues(oldValues); - } - - @Override - protected String getPersistedString(String defaultReturnValue) { - // During initial load, there's no known default value - int defaultIntegerValue = Integer.MIN_VALUE; - if (defaultReturnValue != null) { - defaultIntegerValue = Integer.parseInt(defaultReturnValue); - } - - // When the list preference asks us to read a string, instead read an - // integer. - int value = getPersistedInt(defaultIntegerValue); - return Integer.toString(value); - } - - @Override - protected boolean persistString(String value) { - // When asked to save a string, instead save an integer - return persistInt(Integer.parseInt(value)); - } - - private void verifyEntryValues(CharSequence[] oldValues) { - CharSequence[] entryValues = getEntryValues(); - if (entryValues == null) { - return; - } - - for (CharSequence entryValue : entryValues) { - try { - Integer.parseInt(entryValue.toString()); - } catch (NumberFormatException nfe) { - super.setEntryValues(oldValues); - throw nfe; - } - } - } -} diff --git a/src/main/java/com/google/android/apps/mytracks/util/PreferencesUtils.java b/src/main/java/com/google/android/apps/mytracks/util/PreferencesUtils.java index 39e1e58a5..613a046cf 100644 --- a/src/main/java/com/google/android/apps/mytracks/util/PreferencesUtils.java +++ b/src/main/java/com/google/android/apps/mytracks/util/PreferencesUtils.java @@ -35,6 +35,7 @@ public class PreferencesUtils { * Preferences values. The defaults need to match the defaults in the xml * files. */ + @Deprecated //TODO: to be removed. public static final boolean ALLOW_ACCESS_DEFAULT = false; public static final int AUTO_RESUME_TRACK_CURRENT_RETRY_DEFAULT = 0; @@ -139,8 +140,16 @@ public class PreferencesUtils { * @param defaultValue the default value */ public static int getInt(Context context, int keyId, int defaultValue) { + //NOTE: We assume that the data was stored as String due to use of ListPreference. SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE); - return sharedPreferences.getInt(getKey(context, keyId), defaultValue); + String stringValue = sharedPreferences.getString(getKey(context, keyId), null); + + int intValue = defaultValue; + try { + intValue = Integer.parseInt(stringValue); + } catch (NumberFormatException e){ + } + return intValue; } /** @@ -157,32 +166,6 @@ public class PreferencesUtils { editor.apply(); } - /** - * Gets a float preference value. - * - * @param context the context - * @param keyId the key id - * @param defaultValue the default value - */ - public static float getFloat(Context context, int keyId, float defaultValue) { - SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE); - return sharedPreferences.getFloat(getKey(context, keyId), defaultValue); - } - - /** - * Sets a float preference value. - * - * @param context the context - * @param keyId the key id - * @param value the value - */ - public static void setFloat(Context context, int keyId, float value) { - SharedPreferences sharedPreferences = context.getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE); - Editor editor = sharedPreferences.edit(); - editor.putFloat(getKey(context, keyId), value); - editor.apply(); - } - /** * Gets a long preference value. * diff --git a/src/main/res/xml/settings_advanced.xml b/src/main/res/xml/settings_advanced.xml index e4d34d0fd..49f54965e 100644 --- a/src/main/res/xml/settings_advanced.xml +++ b/src/main/res/xml/settings_advanced.xml @@ -16,7 +16,7 @@ limitations under the License. --> - diff --git a/src/main/res/xml/settings_recording.xml b/src/main/res/xml/settings_recording.xml index c2f508f12..3a70f1eee 100644 --- a/src/main/res/xml/settings_recording.xml +++ b/src/main/res/xml/settings_recording.xml @@ -16,11 +16,11 @@ limitations under the License. --> - - @@ -32,23 +32,23 @@ limitations under the License. android:defaultValue="@string/activity_type_running" android:key="@string/default_activity_key" android:title="@string/settings_recording_default_activity_title" /> - - - - -