Use a list preference for the preferred rate setting

This commit is contained in:
Jimmy Shih
2013-08-20 14:43:25 -07:00
parent 7a8a95853e
commit 594f156a1f
14 changed files with 83 additions and 58 deletions
@@ -282,9 +282,8 @@ public class TrackDataHub implements DataSourceListener {
}
}
if (key == null
|| key.equals(PreferencesUtils.getKey(context, R.string.report_speed_key))) {
reportSpeed = PreferencesUtils.getBoolean(
context, R.string.report_speed_key, PreferencesUtils.REPORT_SPEED_DEFAULT);
|| key.equals(PreferencesUtils.getKey(context, R.string.stats_rate_key))) {
reportSpeed = PreferencesUtils.isReportSpeed(context);
if (key != null) {
for (TrackDataListener trackDataListener :
trackDataManager.getListeners(TrackDataType.PREFERENCE)) {
@@ -65,7 +65,7 @@ public class ChartFragment extends Fragment implements TrackDataListener {
private long startTime;
private boolean metricUnits = true;
private boolean reportSpeed = PreferencesUtils.REPORT_SPEED_DEFAULT;
private boolean reportSpeed = true;
private int recordingDistanceInterval = PreferencesUtils.RECORDING_DISTANCE_INTERVAL_DEFAULT;
// Modes of operation
@@ -228,8 +228,7 @@ public class AnnouncementPeriodicTask implements PeriodicTask {
@VisibleForTesting
protected String getAnnouncement(TripStatistics tripStatistics) {
boolean metricUnits = PreferencesUtils.isMetricUnits(context);
boolean reportSpeed = PreferencesUtils.getBoolean(
context, R.string.report_speed_key, PreferencesUtils.REPORT_SPEED_DEFAULT);
boolean reportSpeed = PreferencesUtils.isReportSpeed(context);
double distance = tripStatistics.getTotalDistance() * UnitConversions.M_TO_KM;
double speed = tripStatistics.getAverageMovingSpeed() * UnitConversions.MS_TO_KMH;
@@ -46,8 +46,7 @@ public class ChartSettingsActivity extends AbstractSettingsActivity {
private void updateUi() {
CheckBoxPreference speedCheckBoxPreference = (CheckBoxPreference) findPreference(
getString(R.string.chart_show_speed_key));
boolean reportSpeed = PreferencesUtils.getBoolean(
this, R.string.report_speed_key, PreferencesUtils.REPORT_SPEED_DEFAULT);
boolean reportSpeed = PreferencesUtils.isReportSpeed(this);
speedCheckBoxPreference.setTitle(reportSpeed ? R.string.stats_speed
: R.string.stats_pace);
}
@@ -20,7 +20,6 @@ import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
@@ -32,21 +31,30 @@ import android.preference.Preference.OnPreferenceChangeListener;
*/
public class StatsSettingsActivity extends AbstractSettingsActivity {
private String statsUnits;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.stats_settings);
ListPreference preference = (ListPreference) findPreference(
/*
* Note configureUnitsListPreference will trigger
* configureRateListPreference
*/
configUnitsListPreference();
}
/**
* Configures the preferred units list preference.
*/
private void configUnitsListPreference() {
@SuppressWarnings("deprecation")
ListPreference listPreference = (ListPreference) findPreference(
getString(R.string.stats_units_key));
OnPreferenceChangeListener listener = new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference pref, Object newValue) {
statsUnits = (String) newValue;
updateUi();
configRateListPreference(PreferencesUtils.STATS_UNITS_DEFAULT.equals((String) newValue));
return true;
}
};
@@ -54,27 +62,23 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
this, R.string.stats_units_key, PreferencesUtils.STATS_UNITS_DEFAULT);
String[] values = getResources().getStringArray(R.array.stats_units_values);
String[] options = getResources().getStringArray(R.array.stats_units_options);
configureListPreference(preference, options, options, values, value, listener);
configureListPreference(listPreference, options, options, values, value, listener);
}
@Override
protected void onResume() {
super.onResume();
statsUnits = PreferencesUtils.getString(
this, R.string.stats_units_key, PreferencesUtils.STATS_UNITS_DEFAULT);
updateUi();
}
@SuppressWarnings("deprecation")
private void updateUi() {
CheckBoxPreference reportSpeedCheckBoxPreference = (CheckBoxPreference) findPreference(
getString(R.string.report_speed_key));
boolean metricUnits = PreferencesUtils.STATS_UNITS_DEFAULT.equals(statsUnits);
reportSpeedCheckBoxPreference.setSummaryOn(
metricUnits ? getString(R.string.description_speed_metric)
: getString(R.string.description_speed_imperial));
reportSpeedCheckBoxPreference.setSummaryOff(
metricUnits ? getString(R.string.description_pace_metric)
: getString(R.string.description_pace_imperial));
/**
* Configures the preferred rate list preference.
*
* @param metricUnits true if metric units
*/
private void configRateListPreference(boolean metricUnits) {
@SuppressWarnings("deprecation")
ListPreference listPreference = (ListPreference) findPreference(
getString(R.string.stats_rate_key));
String value = PreferencesUtils.getString(
this, R.string.stats_rate_key, PreferencesUtils.STATS_RATE_DEFAULT);
String[] values = getResources().getStringArray(R.array.stats_rate_values);
String[] options = getResources().getStringArray(
metricUnits ? R.array.stats_rate_metric_options : R.array.stats_rate_imperial_options);
configureListPreference(listPreference, options, options, values, value, null);
}
}
@@ -91,7 +91,6 @@ public class PreferencesUtils {
public static final long RECORDING_TRACK_ID_DEFAULT = -1L;
public static final boolean RECORDING_TRACK_PAUSED_DEFAULT = true;
public static final boolean REPORT_SPEED_DEFAULT = true;
public static final long SELECTED_TRACK_ID_DEFAULT = -1L;
public static final String SENSOR_TYPE_DEFAULT = "NONE";
@@ -102,6 +101,7 @@ public class PreferencesUtils {
public static final int SPLIT_FREQUENCY_DEFAULT = 0;
// Stats
public static final String STATS_RATE_DEFAULT = "SPEED";
public static final boolean STATS_SHOW_COORDINATE_DEFAULT = false;
public static final boolean STATS_SHOW_GRADE_ELEVATION_DEFAULT = false;
public static final String STATS_UNITS_DEFAULT = "METRIC";
@@ -257,4 +257,14 @@ public class PreferencesUtils {
return PreferencesUtils.STATS_UNITS_DEFAULT.equals(
getString(context, R.string.stats_units_key, PreferencesUtils.STATS_UNITS_DEFAULT));
}
/**
* Returns true if the preferred rate is speed, false if the preferred rate is pace.
*
* @param context the context
*/
public static boolean isReportSpeed(Context context) {
return PreferencesUtils.STATS_RATE_DEFAULT.equals(
getString(context, R.string.stats_rate_key, PreferencesUtils.STATS_RATE_DEFAULT));
}
}
@@ -50,8 +50,7 @@ public class StatsUtils {
*/
public static void setLocationValues(Activity activity, Location location, boolean isRecording) {
boolean metricUnits = PreferencesUtils.isMetricUnits(activity);
boolean reportSpeed = PreferencesUtils.getBoolean(
activity, R.string.report_speed_key, PreferencesUtils.REPORT_SPEED_DEFAULT);
boolean reportSpeed = PreferencesUtils.isReportSpeed(activity);
// Set speed/pace
double speed = isRecording && location != null && location.hasSpeed() ? location.getSpeed()
@@ -111,8 +110,7 @@ public class StatsUtils {
*/
public static void setTripStatisticsValues(Activity activity, TripStatistics tripStatistics) {
boolean metricUnits = PreferencesUtils.isMetricUnits(activity);
boolean reportSpeed = PreferencesUtils.getBoolean(
activity, R.string.report_speed_key, PreferencesUtils.REPORT_SPEED_DEFAULT);
boolean reportSpeed = PreferencesUtils.isReportSpeed(activity);
// Set total distance
double totalDistance = tripStatistics == null ? Double.NaN : tripStatistics.getTotalDistance();
@@ -60,8 +60,7 @@ public class TrackWidgetConfigActivity extends Activity {
item3 = (Spinner) findViewById(R.id.track_widget_config_item3);
item4 = (Spinner) findViewById(R.id.track_widget_config_item4);
boolean reportSpeed = PreferencesUtils.getBoolean(
this, R.string.report_speed_key, PreferencesUtils.REPORT_SPEED_DEFAULT);
boolean reportSpeed = PreferencesUtils.isReportSpeed(this);
List<CharSequence> list = new ArrayList<CharSequence>();
addItem(list, R.string.stats_distance);
@@ -196,8 +196,7 @@ public class TrackWidgetProvider extends AppWidgetProvider {
boolean isPaused = PreferencesUtils.getBoolean(context, R.string.recording_track_paused_key,
PreferencesUtils.RECORDING_TRACK_PAUSED_DEFAULT);
boolean metricUnits = PreferencesUtils.isMetricUnits(context);
boolean reportSpeed = PreferencesUtils.getBoolean(
context, R.string.report_speed_key, PreferencesUtils.REPORT_SPEED_DEFAULT);
boolean reportSpeed = PreferencesUtils.isReportSpeed(context);
int item1 = PreferencesUtils.getInt(
context, R.string.track_widget_item1, PreferencesUtils.TRACK_WIDGET_ITEM1_DEFAULT);
int item2 = PreferencesUtils.getInt(