Give an weight input dialog when the weight value is not set yet. Make the weight value/display can be changed by unit setting.

This commit is contained in:
youtaol
2013-08-23 12:30:24 +08:00
parent 2468062eff
commit 16ff92230a
5 changed files with 172 additions and 43 deletions
@@ -17,15 +17,21 @@
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.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.text.InputType;
import android.util.Log;
import android.widget.EditText;
/**
* An activity for accessing stats settings.
@@ -33,24 +39,22 @@ import android.util.Log;
* @author Jimmy Shih
*/
public class StatsSettingsActivity extends AbstractSettingsActivity {
private static final String TAG = MapSettingsActivity.class.getSimpleName();
private CheckBoxPreference caloriePreference;
private EditTextPreference weightPreference;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
addPreferencesFromResource(R.xml.stats_settings);
caloriePreference = (CheckBoxPreference) findPreference(getString(R.string.stats_show_calorie_key));
weightPreference = (EditTextPreference) findPreference(getString(R.string.stats_weight_key));
configCaloriePreference(caloriePreference, R.string.stats_show_calorie_key,
PreferencesUtils.STATS_SHOW_CALORIE_DEFAULT);
configCaloriePreference(caloriePreference);
configWeightPreference(weightPreference, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT, caloriePreference.isChecked());
/*
@@ -65,17 +69,24 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
*/
private void configUnitsListPreference() {
@SuppressWarnings("deprecation")
ListPreference listPreference = (ListPreference) findPreference(
getString(R.string.stats_units_key));
ListPreference listPreference = (ListPreference) findPreference(getString(R.string.stats_units_key));
OnPreferenceChangeListener listener = new OnPreferenceChangeListener() {
@Override
@Override
public boolean onPreferenceChange(Preference pref, Object newValue) {
configRateListPreference(PreferencesUtils.STATS_UNITS_DEFAULT.equals((String) newValue));
/*
* It is necessary to change the weight summary as they are in the same
* activity.
*/
updateWeightSummary(weightPreference, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT,
PreferencesUtils.STATS_UNITS_DEFAULT.equals((String) newValue));
return true;
}
};
String value = PreferencesUtils.getString(
this, R.string.stats_units_key, PreferencesUtils.STATS_UNITS_DEFAULT);
String value = PreferencesUtils.getString(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(listPreference, options, options, values, value, listener);
@@ -88,16 +99,15 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
*/
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);
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);
}
/**
* Configures the calorie preference.
*
@@ -105,8 +115,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
* @param key of the preference
* @param defaultValue default value of this preference
*/
private void configCaloriePreference(CheckBoxPreference preference, int key, boolean defaultValue) {
PreferencesUtils.setBoolean(this, key, PreferencesUtils.getBoolean(this, key, defaultValue));
private void configCaloriePreference(CheckBoxPreference preference) {
preference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@SuppressWarnings("hiding")
@Override
@@ -114,6 +123,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
boolean value = (Boolean) newValue;
if (value) {
weightPreference.setEnabled(true);
showWeightInputPreferenceDialog();
} else {
weightPreference.setEnabled(false);
}
@@ -122,6 +132,42 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
});
}
/**
* Shows this dialog to input weight value while enable show calorie and the
* value is not set yet.
*/
private void showWeightInputPreferenceDialog() {
int value = PreferencesUtils.getInt(this, R.string.stats_weight_key, -1);
if (value == -1) {
final EditText weightInput = new EditText(this);
weightInput.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
weightInput.setText(Integer.toString(PreferencesUtils.STATS_WEIGHT_DEFAULT));
weightInput.setSelectAllOnFocus(true);
AlertDialog.Builder inputDialogBuilder = new AlertDialog.Builder(this);
inputDialogBuilder
.setTitle(R.string.settings_stats_calorie_weight)
.setMessage(R.string.settings_stats_calorie_weight_description)
.setCancelable(false)
.setView(weightInput)
.setPositiveButton(getString(R.string.generic_ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
storeWeightValue(R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT,
weightInput.getText().toString());
updateWeightSummary(weightPreference, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT);
dialog.cancel();
}
})
.setNegativeButton(getString(R.string.generic_cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
inputDialogBuilder.create().show();
}
}
/**
* Configures the weight preference.
*
@@ -130,9 +176,9 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
* @param defaultValue default value of this preference
* @param isEnable true means enable the weight preference
*/
private void configWeightPreference(EditTextPreference preference, int key, int defaultValue,
boolean isEnable) {
PreferencesUtils.setInt(this, key, PreferencesUtils.getInt(this, key, defaultValue));
private void configWeightPreference(EditTextPreference preference, final int key,
final int defaultValue, boolean isEnable) {
updateWeightSummary(preference, key, defaultValue);
if (isEnable) {
preference.setEnabled(true);
} else {
@@ -143,17 +189,84 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
@SuppressWarnings("hiding")
@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;
}
PreferencesUtils.setInt(StatsSettingsActivity.this, R.string.stats_weight_key, value);
storeWeightValue(key, defaultValue, (String) newValue);
updateWeightSummary(preference, key, defaultValue);
return true;
}
});
preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference pref) {
int value = getWeightDisplayValue(key, defaultValue,
PreferencesUtils.isMetricUnits(getApplicationContext()));
((EditTextPreference) pref).getEditText().setText(String.valueOf(value));
return true;
}
});
}
/**
* Updates the weight summary.
*
* @param preference the preference
* @param keyId the key id
* @param defaultValue the default value
*/
private void updateWeightSummary(Preference preference, int keyId, int defaultValue) {
boolean metricUnits = PreferencesUtils.isMetricUnits(this);
updateWeightSummary(preference, keyId, defaultValue, metricUnits);
}
/**
* Updates the weight summary.
*
* @param preference the preference
* @param keyId the key id
* @param defaultValue the default value
* @param metricUnits the status of metric units
*/
private void updateWeightSummary(Preference preference, int keyId, int defaultValue,
boolean metricUnits) {
int displayValue = getWeightDisplayValue(keyId, defaultValue, metricUnits);
preference.setSummary(getString(metricUnits ? R.string.value_integer_kilogram
: R.string.value_integer_pound, displayValue));
}
/**
* Gets the weight display value in metric or imperial depending on the
* preferred units.
*
* @param keyId the key id
* @param defaultValue the default value
*/
private int getWeightDisplayValue(int keyId, int defaultValue, boolean metricUnits) {
int value = PreferencesUtils.getInt(this, keyId, defaultValue);
if (!metricUnits) {
value = (int) (value * UnitConversions.KG_TO_LB);
}
return value;
}
/**
* Stores the weight value, always in metric units.
*
* @param keyId the key id
* @param defaultValue the default value
* @param displayValue the display value
*/
private void storeWeightValue(int keyId, int defaultValue, String displayValue) {
int value;
try {
value = Integer.parseInt(displayValue);
if (!PreferencesUtils.isMetricUnits(this)) {
value = (int) (value * UnitConversions.LB_TO_KG);
}
} catch (NumberFormatException e) {
Log.e(TAG, "invalid value " + displayValue);
value = defaultValue;
}
PreferencesUtils.setInt(this, keyId, value);
}
}
@@ -106,7 +106,7 @@ public class PreferencesUtils {
public static final boolean STATS_SHOW_GRADE_ELEVATION_DEFAULT = false;
public static final String STATS_UNITS_DEFAULT = "METRIC";
public static final boolean STATS_SHOW_CALORIE_DEFAULT = false;
public static final int STATS_WEIGHT_DEFAULT = 65; //in kilogram
public static final int STATS_WEIGHT_DEFAULT = 65; // in kilogram
// Track color
public static final String TRACK_COLOR_MODE_DEFAULT = "SINGLE";
@@ -45,6 +45,12 @@ public class UnitConversions {
// multiplication factor to convert kilometers to miles
public static final double KM_TO_MI = 0.621371192;
// multiplication factor to convert kilogram to pounds
public static final double KG_TO_LB = 2.2046;
// multiplication factor to convert kilogram to pounds
public static final double LB_TO_KG = 1 / KG_TO_LB;
// multiplication factor to convert miles to kilometers
public static final double MI_TO_KM = 1 / KM_TO_MI;