Place map settings in MapSettingsActivity.

This commit is contained in:
Jimmy Shih
2012-05-08 09:11:08 -07:00
parent 3461a50288
commit 26a44dfd9a
17 changed files with 566 additions and 584 deletions
@@ -23,6 +23,7 @@ import com.google.android.apps.mytracks.maps.TrackPathPainterFactory;
import com.google.android.apps.mytracks.maps.TrackPathUtilities;
import com.google.android.apps.mytracks.util.IntentUtils;
import com.google.android.apps.mytracks.util.LocationUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
@@ -462,10 +463,8 @@ public class MapOverlay extends Overlay implements OnSharedPreferenceChangeListe
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.d(TAG, "MapOverlay: onSharedPreferences changed " + key);
if (key != null) {
if (key.equals(context.getString(R.string.track_color_mode_key))) {
trackPathPainter = TrackPathPainterFactory.getTrackPathPainter(context);
}
if (PreferencesUtils.getKey(context, R.string.track_color_mode_key).equals(key)) {
trackPathPainter = TrackPathPainterFactory.getTrackPathPainter(context);
}
}
}
@@ -42,7 +42,6 @@ public class DynamicSpeedTrackPathDescriptor implements TrackPathDescriptor,
private int slowSpeed;
private int normalSpeed;
private int speedMargin;
private final int speedMarginDefault;
private double averageMovingSpeed;
private final Context context;
@VisibleForTesting
@@ -50,29 +49,12 @@ public class DynamicSpeedTrackPathDescriptor implements TrackPathDescriptor,
public DynamicSpeedTrackPathDescriptor(Context context) {
this.context = context;
speedMarginDefault = Integer.parseInt(context
.getString(R.string.color_mode_dynamic_percentage_default));
SharedPreferences prefs = context.getSharedPreferences(Constants.SETTINGS_NAME,
Context.MODE_PRIVATE);
if (prefs == null) {
speedMargin = speedMarginDefault;
return;
}
prefs.registerOnSharedPreferenceChangeListener(this);
speedMargin = getSpeedMargin(prefs);
}
@VisibleForTesting
int getSpeedMargin(SharedPreferences sharedPreferences) {
try {
return Integer.parseInt(sharedPreferences.getString(
context.getString(R.string.track_color_mode_dynamic_speed_variation_key),
Integer.toString(speedMarginDefault)));
} catch (NumberFormatException e) {
return speedMarginDefault;
}
speedMargin = PreferencesUtils.getInt(context, R.string.track_color_mode_percentage_key,
PreferencesUtils.TRACK_COLOR_MODE_PERCENTAGE_DEFAULT);
}
/**
@@ -98,17 +80,10 @@ public class DynamicSpeedTrackPathDescriptor implements TrackPathDescriptor,
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.d(TAG, "DynamicSpeedTrackPathDescriptor: onSharedPreferences changed " + key);
if (key == null
|| !key.equals(context.getString(R.string.track_color_mode_dynamic_speed_variation_key))) { return; }
SharedPreferences prefs = context.getSharedPreferences(Constants.SETTINGS_NAME,
Context.MODE_PRIVATE);
if (prefs == null) {
speedMargin = speedMarginDefault;
return;
if (PreferencesUtils.getKey(context, R.string.track_color_mode_percentage_key).equals(key)) {
speedMargin = PreferencesUtils.getInt(context, R.string.track_color_mode_percentage_key,
PreferencesUtils.TRACK_COLOR_MODE_PERCENTAGE_DEFAULT);
}
speedMargin = getSpeedMargin(prefs);
}
@Override
@@ -13,104 +13,67 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.maps;
import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.util.Log;
/**
* A fixed speed path descriptor.
*
*
* @author Vangelis S.
*/
public class FixedSpeedTrackPathDescriptor implements TrackPathDescriptor, OnSharedPreferenceChangeListener {
public class FixedSpeedTrackPathDescriptor
implements TrackPathDescriptor, OnSharedPreferenceChangeListener {
private int slowSpeed;
private int normalSpeed;
private final int slowDefault;
private final int normalDefault;
private final Context context;
public FixedSpeedTrackPathDescriptor(Context context) {
this.context = context;
slowDefault = Integer.parseInt(context.getString(R.string.color_mode_fixed_slow_default));
normalDefault = Integer.parseInt(context.getString(R.string.color_mode_fixed_medium_default));
SharedPreferences prefs = context.getSharedPreferences(
SharedPreferences sharedPreferences = context.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
if (prefs == null) {
slowSpeed = slowDefault;
normalSpeed = normalDefault;
return;
}
prefs.registerOnSharedPreferenceChangeListener(this);
try {
slowSpeed = Integer.parseInt(prefs.getString(context.getString(
R.string.track_color_mode_fixed_speed_slow_key), Integer.toString(slowDefault)));
} catch (NumberFormatException e) {
slowSpeed = slowDefault;
}
try {
normalSpeed = Integer.parseInt(prefs.getString(context.getString(
R.string.track_color_mode_fixed_speed_medium_key), Integer.toString(normalDefault)));
} catch (NumberFormatException e) {
normalSpeed = normalDefault;
}
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
slowSpeed = PreferencesUtils.getInt(context, R.string.track_color_mode_slow_key,
PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT);
normalSpeed = PreferencesUtils.getInt(context, R.string.track_color_mode_medium_key,
PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT);
}
/**
* Gets the slow speed for reference.
*
* @return The speed limit considered as slow.
*/
public int getSlowSpeed() {
return slowSpeed;
}
/**
* Gets the normal speed for reference.
*
* @return The speed limit considered as normal.
*/
public int getNormalSpeed() {
return normalSpeed;
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.d(TAG, "FixedSpeedTrackPathDescriptor: onSharedPreferences changed " + key);
if (key == null
|| (!key.equals(context.getString(R.string.track_color_mode_fixed_speed_slow_key))
&& !key.equals(context.getString(R.string.track_color_mode_fixed_speed_medium_key)))) {
return;
}
SharedPreferences prefs = context.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
if (prefs == null) {
slowSpeed = slowDefault;
normalSpeed = normalDefault;
return;
}
try {
slowSpeed = Integer.parseInt(prefs.getString(context.getString(
R.string.track_color_mode_fixed_speed_slow_key), Integer.toString(slowDefault)));
} catch (NumberFormatException e) {
slowSpeed = slowDefault;
}
try {
normalSpeed = Integer.parseInt(prefs.getString(context.getString(
R.string.track_color_mode_fixed_speed_medium_key), Integer.toString(normalDefault)));
} catch (NumberFormatException e) {
normalSpeed = normalDefault;
if (PreferencesUtils.getKey(context, R.string.track_color_mode_slow_key).equals(key)) {
slowSpeed = PreferencesUtils.getInt(context, R.string.track_color_mode_slow_key,
PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT);
} else if (PreferencesUtils.getKey(context, R.string.track_color_mode_medium_key).equals(key)) {
normalSpeed = PreferencesUtils.getInt(context, R.string.track_color_mode_medium_key,
PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT);
}
}
@@ -17,11 +17,10 @@ package com.google.android.apps.mytracks.maps;
import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
/**
@@ -40,26 +39,18 @@ public class TrackPathPainterFactory {
* @return The TrackPathPainter that corresponds to the track color mode setting.
*/
public static TrackPathPainter getTrackPathPainter(Context context) {
SharedPreferences prefs = context.getSharedPreferences(
Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
if (prefs == null) {
return new SingleColorTrackPathPainter(context);
}
String colorMode = prefs.getString(context.getString(R.string.track_color_mode_key), null);
Log.i(TAG, "Creating track path painter of type: " + colorMode);
String trackColorMode = PreferencesUtils.getString(context, R.string.track_color_mode_key,
context.getString(R.string.settings_map_track_color_mode_single_value));
Log.i(TAG, "Creating track path painter of type: " + trackColorMode);
if (colorMode == null
|| colorMode.equals(context.getString(R.string.display_track_color_value_none))) {
return new SingleColorTrackPathPainter(context);
} else if (colorMode.equals(context.getString(R.string.display_track_color_value_fixed))) {
return new DynamicSpeedTrackPathPainter(context,
new FixedSpeedTrackPathDescriptor(context));
} else if (colorMode.equals(context.getString(R.string.display_track_color_value_dynamic))) {
return new DynamicSpeedTrackPathPainter(context,
new DynamicSpeedTrackPathDescriptor(context));
if (context.getString(R.string.settings_map_track_color_mode_fixed_value)
.equals(trackColorMode)) {
return new DynamicSpeedTrackPathPainter(context, new FixedSpeedTrackPathDescriptor(context));
} else if (context.getString(R.string.settings_map_track_color_mode_dynamic_value)
.equals(trackColorMode)) {
return new DynamicSpeedTrackPathPainter(context, new DynamicSpeedTrackPathDescriptor(
context));
} else {
Log.w(TAG, "Using default track path painter. Unrecognized painter: " + colorMode);
return new SingleColorTrackPathPainter(context);
}
}
@@ -0,0 +1,274 @@
/*
* Copyright 2012 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 com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R;
import android.app.Dialog;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
/**
* An activity for accessing map settings.
*
* @author Jimmy Shih
*/
public class MapSettingsActivity extends AbstractSettingsActivity {
private static final String TAG = MapSettingsActivity.class.getSimpleName();
private EditTextPreference slowEditTextPreference;
private EditTextPreference mediumEditTextPreference;
private EditTextPreference percentageEditTextPreference;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.map_settings);
ListPreference trackColorModeListPreference = (ListPreference) findPreference(
getString(R.string.track_color_mode_key));
trackColorModeListPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
updateUiByTrackColorMode((String) newValue);
return true;
}
});
updateUiByTrackColorMode(trackColorModeListPreference.getValue());
slowEditTextPreference = (EditTextPreference) findPreference(
getString(R.string.settings_map_slow_display_key));
configureSpeedEditTextPreference(R.string.track_color_mode_slow_key);
mediumEditTextPreference = (EditTextPreference) findPreference(
getString(R.string.settings_map_medium_display_key));
configureSpeedEditTextPreference(R.string.track_color_mode_medium_key);
percentageEditTextPreference = (EditTextPreference) findPreference(
getString(R.string.settings_map_percentage_display_key));
configurePercentageEditTextPreference();
updatePercentageSummary();
configureImeActionDone(slowEditTextPreference);
configureImeActionDone(mediumEditTextPreference);
configureImeActionDone(percentageEditTextPreference);
}
/**
* Configures the speed edit text preference.
*
* @param keyId the key id
*/
private void configureSpeedEditTextPreference(final int keyId) {
final EditTextPreference editTextPreference = keyId == R.string.track_color_mode_slow_key ? slowEditTextPreference
: mediumEditTextPreference;
editTextPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
storeSpeedValue(keyId, (String) newValue);
updateSpeedSummary(keyId);
return true;
}
});
editTextPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
int value = getSpeedDisplayValue(keyId);
((EditTextPreference) preference).getEditText().setText(String.valueOf(value));
return true;
}
});
}
/**
* Configures the percentage edit text preference.
*/
private void configurePercentageEditTextPreference() {
percentageEditTextPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String displayValue = (String) newValue;
int value;
try {
value = Integer.parseInt(displayValue);
} catch (NumberFormatException e) {
Log.e(TAG, "invalid value " + displayValue);
value = PreferencesUtils.TRACK_COLOR_MODE_PERCENTAGE_DEFAULT;
}
if (value < 0) {
value = PreferencesUtils.TRACK_COLOR_MODE_PERCENTAGE_DEFAULT;
}
PreferencesUtils.setInt(
MapSettingsActivity.this, R.string.track_color_mode_percentage_key, value);
updatePercentageSummary();
return true;
}
});
percentageEditTextPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
int value = PreferencesUtils.getInt(
MapSettingsActivity.this, R.string.track_color_mode_percentage_key,
PreferencesUtils.TRACK_COLOR_MODE_PERCENTAGE_DEFAULT);
((EditTextPreference) preference).getEditText().setText(String.valueOf(value));
return true;
}
});
}
/**
* Configures the IME action done.
*
* @param editTextPreference the edit text preference
*/
private void configureImeActionDone(final EditTextPreference editTextPreference) {
editTextPreference.getEditText()
.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
Dialog dialog = editTextPreference.getDialog();
editTextPreference.onClick(dialog, Dialog.BUTTON_POSITIVE);
dialog.dismiss();
return true;
}
return false;
}
});
}
@Override
protected void onResume() {
super.onResume();
updateSpeedSummary(R.string.track_color_mode_slow_key);
updateSpeedSummary(R.string.track_color_mode_medium_key);
}
/**
* Updates the UI by the track color mode.
*
* @param trackColorMode the track color mode
*/
@SuppressWarnings("deprecation")
private void updateUiByTrackColorMode(String trackColorMode) {
boolean isFixedValue = trackColorMode.equals(
getString(R.string.settings_map_track_color_mode_fixed_value));
boolean isDynamicValue = trackColorMode.equals(
getString(R.string.settings_map_track_color_mode_dynamic_value));
findPreference(getString(R.string.settings_map_slow_display_key)).setEnabled(isFixedValue);
findPreference(getString(R.string.settings_map_medium_display_key)).setEnabled(isFixedValue);
findPreference(getString(R.string.settings_map_percentage_display_key))
.setEnabled(isDynamicValue);
}
/**
* Stores the speed value, always in metric units.
*
* @param keyId the key id
* @param displayValue the display value
*/
private void storeSpeedValue(int keyId, String displayValue) {
int maxValue;
int minValue;
int defaultValue;
if (keyId == R.string.track_color_mode_slow_key) {
minValue = 0;
maxValue = PreferencesUtils.getInt(this, R.string.track_color_mode_medium_key,
PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT);
defaultValue = PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT;
} else {
minValue = PreferencesUtils.getInt(
this, R.string.track_color_mode_slow_key, PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT);
maxValue = Integer.MAX_VALUE;
defaultValue = PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT;
}
int value;
try {
value = Integer.parseInt(displayValue);
if (!PreferencesUtils.getBoolean(
this, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT)) {
value = (int) (value * UnitConversions.MI_TO_KM);
}
} catch (NumberFormatException e) {
Log.e(TAG, "invalid value " + displayValue);
value = defaultValue;
}
if (value > maxValue) {
value = maxValue;
}
if (value < minValue) {
value = minValue;
}
PreferencesUtils.setInt(this, keyId, value);
}
/**
* Gets the speed display value, in metric or imperial depending on the preferred
* units.
*
* @param keyId the key id
*/
private int getSpeedDisplayValue(int keyId) {
int defaultValue = keyId == R.string.track_color_mode_slow_key ? PreferencesUtils.TRACK_COLOR_MODE_SLOW_DEFAULT
: PreferencesUtils.TRACK_COLOR_MODE_MEDIUM_DEFAULT;
int value = PreferencesUtils.getInt(this, keyId, defaultValue);
if (!PreferencesUtils.getBoolean(
this, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT)) {
value = (int) (value * UnitConversions.KM_TO_MI);
}
return value;
}
/**
* Updates the speed summary.
*
* @param keyId the key id
*/
private void updateSpeedSummary(int keyId) {
EditTextPreference editTextPreference = keyId == R.string.track_color_mode_slow_key ? slowEditTextPreference
: mediumEditTextPreference;
boolean metric = PreferencesUtils.getBoolean(
this, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT);
int displayValue = getSpeedDisplayValue(keyId);
editTextPreference.setSummary(getString(metric ? R.string.value_integer_kilometer_hour
: R.string.value_integer_mile_hour, displayValue));
}
/**
* Updates the percentage summary.
*/
private void updatePercentageSummary() {
int value = PreferencesUtils.getInt(
MapSettingsActivity.this, R.string.track_color_mode_percentage_key,
PreferencesUtils.TRACK_COLOR_MODE_PERCENTAGE_DEFAULT);
percentageEditTextPreference.setSummary(getString(R.string.settings_map_percentage_summary) + "\n"
+ getString(R.string.value_integer_percent, value));
}
}
@@ -19,11 +19,9 @@ package com.google.android.apps.mytracks.settings;
import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import com.google.android.apps.mytracks.util.DialogUtils;
import com.google.android.apps.mytracks.util.IntentUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R;
import android.app.Dialog;
@@ -31,14 +29,9 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceCategory;
import android.util.Log;
import android.widget.Toast;
@@ -58,19 +51,26 @@ public class SettingsActivity extends AbstractSettingsActivity {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.preferences);
customizeTrackColorModePreferences();
Preference mapPreference = findPreference(getString(R.string.settings_map_key));
mapPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = IntentUtils.newIntent(SettingsActivity.this, MapSettingsActivity.class);
startActivity(intent);
return true;
}
});
Preference statsPreference = findPreference(getString(R.string.settings_stats_key));
statsPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = IntentUtils.newIntent(
SettingsActivity.this, StatsSettingsActivity.class);
Intent intent = IntentUtils.newIntent(SettingsActivity.this, StatsSettingsActivity.class);
startActivity(intent);
return true;
}
});
Preference recordingPreference = findPreference(getString(R.string.settings_recording_key));
recordingPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
@@ -139,31 +139,6 @@ public class SettingsActivity extends AbstractSettingsActivity {
}
}
private void customizeTrackColorModePreferences() {
ListPreference trackColorModePreference =
(ListPreference) findPreference(getString(R.string.track_color_mode_key));
trackColorModePreference.setOnPreferenceChangeListener(
new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
updateTrackColorModeSettings((String) newValue);
return true;
}
});
updateTrackColorModeSettings(trackColorModePreference.getValue());
setTrackColorModePreferenceListeners();
PreferenceCategory speedOptionsCategory = (PreferenceCategory) findPreference(
getString(R.string.track_color_mode_fixed_speed_options_key));
speedOptionsCategory.removePreference(
findPreference(getString(R.string.track_color_mode_fixed_speed_slow_key)));
speedOptionsCategory.removePreference(
findPreference(getString(R.string.track_color_mode_fixed_speed_medium_key)));
}
@Override
protected void onResume() {
super.onResume();
@@ -175,20 +150,6 @@ public class SettingsActivity extends AbstractSettingsActivity {
: R.string.settings_reset_summary);
}
private void updateTrackColorModeSettings(String trackColorMode) {
boolean usesFixedSpeed =
trackColorMode.equals(getString(R.string.display_track_color_value_fixed));
boolean usesDynamicSpeed =
trackColorMode.equals(getString(R.string.display_track_color_value_dynamic));
findPreference(getString(R.string.track_color_mode_fixed_speed_slow_display_key))
.setEnabled(usesFixedSpeed);
findPreference(getString(R.string.track_color_mode_fixed_speed_medium_display_key))
.setEnabled(usesFixedSpeed);
findPreference(getString(R.string.track_color_mode_dynamic_speed_variation_key))
.setEnabled(usesDynamicSpeed);
}
/** Callback for when user confirms resetting all settings. */
private void onResetPreferencesConfirmed() {
// Change preferences in a separate thread.
@@ -221,84 +182,4 @@ public class SettingsActivity extends AbstractSettingsActivity {
}
}.start();
}
/**
* Set the given edit text preference text.
* If the units are not metric convert the value before displaying.
*/
private void viewTrackColorModeSettings(EditTextPreference preference, int id) {
if (PreferencesUtils.getBoolean(
this, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT)) {
return;
}
// Convert miles/h to km/h
SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
String metricspeed = prefs.getString(getString(id), null);
int englishspeed;
try {
englishspeed = (int) (Double.parseDouble(metricspeed) * UnitConversions.KM_TO_MI);
} catch (NumberFormatException e) {
englishspeed = 0;
}
preference.getEditText().setText(String.valueOf(englishspeed));
}
/**
* Saves the given edit text preference value.
* If the units are not metric convert the value before saving.
*/
private void validateTrackColorModeSettings(String newValue, int id) {
String metricspeed;
if (PreferencesUtils.getBoolean(
this, R.string.metric_units_key, PreferencesUtils.METRIC_UNITS_DEFAULT)) {
metricspeed = newValue;
} else {
// Convert miles/h to km/h
try {
metricspeed = String.valueOf(
(int) (Double.parseDouble(newValue) * UnitConversions.MI_TO_KM));
} catch (NumberFormatException e) {
metricspeed = "0";
}
}
SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
Editor editor = prefs.edit();
editor.putString(getString(id), metricspeed);
ApiAdapterFactory.getApiAdapter().applyPreferenceChanges(editor);
}
/**
* Sets the TrackColorMode preference listeners.
*/
private void setTrackColorModePreferenceListeners() {
setTrackColorModePreferenceListener(R.string.track_color_mode_fixed_speed_slow_display_key,
R.string.track_color_mode_fixed_speed_slow_key);
setTrackColorModePreferenceListener(R.string.track_color_mode_fixed_speed_medium_display_key,
R.string.track_color_mode_fixed_speed_medium_key);
}
/**
* Sets a TrackColorMode preference listener.
*/
private void setTrackColorModePreferenceListener(int displayKey, final int metricKey) {
EditTextPreference trackColorModePreference =
(EditTextPreference) findPreference(getString(displayKey));
trackColorModePreference.setOnPreferenceChangeListener(
new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
validateTrackColorModeSettings((String) newValue, metricKey);
return true;
}
});
trackColorModePreference.setOnPreferenceClickListener(
new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
viewTrackColorModeSettings((EditTextPreference) preference, metricKey);
return true;
}
});
}
}
@@ -68,6 +68,10 @@ public class PreferencesUtils {
public static final boolean STATS_SHOW_GRADE_DEFAULT = false;
public static final boolean STATS_USE_TOTAL_TIME_DEFAULT = true;
public static final int TRACK_COLOR_MODE_MEDIUM_DEFAULT = 15;
public static final int TRACK_COLOR_MODE_PERCENTAGE_DEFAULT = 25;
public static final int TRACK_COLOR_MODE_SLOW_DEFAULT = 9;
private PreferencesUtils() {}
/**