Removed IntegerListPreference and use a ListPreference instead.

This commit is contained in:
Dennis Guse
2019-07-21 18:15:34 +02:00
parent a1f62ec79f
commit 186efc0a23
5 changed files with 19 additions and 133 deletions
@@ -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];
@@ -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;
}
}
}
}
@@ -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.
*
+1 -1
View File
@@ -16,7 +16,7 @@ limitations under the License.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/settings_advanced" >
<com.google.android.apps.mytracks.settings.IntegerListPreference
<ListPreference
android:defaultValue="1024"
android:key="@string/photo_size_key"
android:title="@string/settings_advanced_photo_size_title" />
+7 -7
View File
@@ -16,11 +16,11 @@ limitations under the License.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/settings_recording" >
<com.google.android.apps.mytracks.settings.IntegerListPreference
<ListPreference
android:defaultValue="0"
android:key="@string/voice_frequency_key"
android:title="@string/menu_voice_frequency" />
<com.google.android.apps.mytracks.settings.IntegerListPreference
<ListPreference
android:defaultValue="0"
android:key="@string/split_frequency_key"
android:title="@string/menu_split_frequency" />
@@ -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" />
<com.google.android.apps.mytracks.settings.IntegerListPreference
<ListPreference
android:defaultValue="0"
android:key="@string/min_recording_interval_key"
android:title="@string/settings_recording_min_recording_interval_title" />
<com.google.android.apps.mytracks.settings.IntegerListPreference
<ListPreference
android:defaultValue="10"
android:key="@string/recording_distance_interval_key"
android:title="@string/settings_recording_min_recording_distance_title" />
<com.google.android.apps.mytracks.settings.IntegerListPreference
<ListPreference
android:defaultValue="200"
android:key="@string/max_recording_distance_key"
android:title="@string/settings_recording_max_recording_distance_title" />
<com.google.android.apps.mytracks.settings.IntegerListPreference
<ListPreference
android:defaultValue="50"
android:key="@string/recording_gps_accuracy_key"
android:title="@string/settings_recording_min_required_accuracy_title" />
<com.google.android.apps.mytracks.settings.IntegerListPreference
<ListPreference
android:defaultValue="10"
android:key="@string/auto_resume_track_timeout_key"
android:title="@string/settings_recording_auto_resume_track_timeout_title" />