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
+22 -10
View File
@@ -2007,6 +2007,12 @@ limitations under the License.
Character limit: ~23 (1 line)"> Character limit: ~23 (1 line)">
Latitude/Longitude Latitude/Longitude
</string> </string>
<string name="settings_stats_calorie"
translation_description="In the 'Stats settings, the option to show calorie value.
Character limit: ~23 (1 line)">
Calorie
</string>
<string name="settings_stats_grade_elevation" <string name="settings_stats_grade_elevation"
translation_description="In the 'Stats settings, the option to show grade and elevation. Grade translation_description="In the 'Stats settings, the option to show grade and elevation. Grade
is the amount of inclination to the horizontal, also known as slope or incline. is the amount of inclination to the horizontal, also known as slope or incline.
@@ -2014,21 +2020,15 @@ limitations under the License.
Character limit: ~23 (1 line)"> Character limit: ~23 (1 line)">
Grade/Elevation Grade/Elevation
</string> </string>
<string name="settings_stats_calorie"
translation_description="In the 'Stats settings, the option to show calorie value.
Character limit: ~23 (1 line)">
Calorie
</string>
<string name="settings_stats_calorie_weight" <string name="settings_stats_calorie_weight"
translation_description="In the 'Stats settings, the option to configure user's weight. translation_description="In the 'Stats settings, the option to configure user's weight.
Character limit: ~23 (1 line)"> Character limit: ~23 (1 line)">
Weight Weight
</string> </string>
<string name="settings_stats_calorie_weight_summary" <string name="settings_stats_calorie_weight_description"
translation_description="In the 'Stats settings, the description message about weight setting item."> translation_description="In the 'Stats settings, the description message about weight setting item.">
Your weight in kilogram. This value is used to calculate the calorie expenditure and would be private for yourself. This weight value is only used to calculate the calorie expenditure and would be private for yourself.
</string> </string>
<string name="settings_stats_rate_title" <string name="settings_stats_rate_title"
translation_description="In the 'Stats' settings, the option to change the preferred rate, translation_description="In the 'Stats' settings, the option to change the preferred rate,
@@ -2040,12 +2040,12 @@ limitations under the License.
<string name="settings_stats_units_imperial" <string name="settings_stats_units_imperial"
translation_description="In the 'Stats' settings, the option to use imperial units, with translation_description="In the 'Stats' settings, the option to use imperial units, with
examples."> examples.">
Imperial (mi, ft) Imperial (mi, ft, lb)
</string> </string>
<string name="settings_stats_units_metric" <string name="settings_stats_units_metric"
translation_description="In the 'Stats' settings, the option to use metric units, with translation_description="In the 'Stats' settings, the option to use metric units, with
examples."> examples.">
Metric (km, m) Metric (km, m, kg)
</string> </string>
<string name="settings_stats_units_title" <string name="settings_stats_units_title"
translation_description="In the 'Stats' settings page, the option to change the preferred units, translation_description="In the 'Stats' settings page, the option to change the preferred units,
@@ -2507,6 +2507,12 @@ limitations under the License.
%1$d: an integer value. E.g., '2'."> %1$d: an integer value. E.g., '2'.">
%1$d km %1$d km
</string> </string>
<string name="value_integer_kilogram"
translation_description="Display an integer value in kilogram (kg).
%1$d: an integer value. E.g., '2'.">
%1$d kg
</string>
<string name="value_integer_kilometer_hour" <string name="value_integer_kilometer_hour"
translation_description="Display an integer value in kilometers per hour (km/h). translation_description="Display an integer value in kilometers per hour (km/h).
@@ -2568,6 +2574,12 @@ limitations under the License.
%1$d: an integer value. E.g., '2'."> %1$d: an integer value. E.g., '2'.">
%1$d %% %1$d %%
</string> </string>
<string name="value_integer_pound"
translation_description="Display an integer value in pound (lb).
%1$d: an integer value. E.g., '2'.">
%1$d lb
</string>
<string name="value_integer_second" <string name="value_integer_second"
translation_description="In a setting picker, the string format to display an integer value in translation_description="In a setting picker, the string format to display an integer value in
seconds (s). seconds (s).
+1 -3
View File
@@ -42,8 +42,6 @@ limitations under the License.
android:inputType="number" android:inputType="number"
android:key="@string/stats_weight_key" android:key="@string/stats_weight_key"
android:persistent="false" android:persistent="false"
android:title="@string/settings_stats_calorie_weight" android:title="@string/settings_stats_calorie_weight" />
android:summary="@string/settings_stats_calorie_weight_summary">
</EditTextPreference>
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>
@@ -17,15 +17,21 @@
package com.google.android.apps.mytracks.settings; package com.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.util.PreferencesUtils; import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference; import android.preference.EditTextPreference;
import android.preference.ListPreference; import android.preference.ListPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.text.InputType;
import android.util.Log; import android.util.Log;
import android.widget.EditText;
/** /**
* An activity for accessing stats settings. * An activity for accessing stats settings.
@@ -33,24 +39,22 @@ import android.util.Log;
* @author Jimmy Shih * @author Jimmy Shih
*/ */
public class StatsSettingsActivity extends AbstractSettingsActivity { public class StatsSettingsActivity extends AbstractSettingsActivity {
private static final String TAG = MapSettingsActivity.class.getSimpleName(); private static final String TAG = MapSettingsActivity.class.getSimpleName();
private CheckBoxPreference caloriePreference; private CheckBoxPreference caloriePreference;
private EditTextPreference weightPreference; private EditTextPreference weightPreference;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
protected void onCreate(Bundle bundle) { protected void onCreate(Bundle bundle) {
super.onCreate(bundle); super.onCreate(bundle);
addPreferencesFromResource(R.xml.stats_settings); addPreferencesFromResource(R.xml.stats_settings);
caloriePreference = (CheckBoxPreference) findPreference(getString(R.string.stats_show_calorie_key)); caloriePreference = (CheckBoxPreference) findPreference(getString(R.string.stats_show_calorie_key));
weightPreference = (EditTextPreference) findPreference(getString(R.string.stats_weight_key)); weightPreference = (EditTextPreference) findPreference(getString(R.string.stats_weight_key));
configCaloriePreference(caloriePreference, R.string.stats_show_calorie_key, configCaloriePreference(caloriePreference);
PreferencesUtils.STATS_SHOW_CALORIE_DEFAULT);
configWeightPreference(weightPreference, R.string.stats_weight_key, configWeightPreference(weightPreference, R.string.stats_weight_key,
PreferencesUtils.STATS_WEIGHT_DEFAULT, caloriePreference.isChecked()); PreferencesUtils.STATS_WEIGHT_DEFAULT, caloriePreference.isChecked());
/* /*
@@ -65,17 +69,24 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
*/ */
private void configUnitsListPreference() { private void configUnitsListPreference() {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
ListPreference listPreference = (ListPreference) findPreference( ListPreference listPreference = (ListPreference) findPreference(getString(R.string.stats_units_key));
getString(R.string.stats_units_key));
OnPreferenceChangeListener listener = new OnPreferenceChangeListener() { OnPreferenceChangeListener listener = new OnPreferenceChangeListener() {
@Override @Override
public boolean onPreferenceChange(Preference pref, Object newValue) { public boolean onPreferenceChange(Preference pref, Object newValue) {
configRateListPreference(PreferencesUtils.STATS_UNITS_DEFAULT.equals((String) 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; return true;
} }
}; };
String value = PreferencesUtils.getString( String value = PreferencesUtils.getString(this, R.string.stats_units_key,
this, R.string.stats_units_key, PreferencesUtils.STATS_UNITS_DEFAULT); PreferencesUtils.STATS_UNITS_DEFAULT);
String[] values = getResources().getStringArray(R.array.stats_units_values); String[] values = getResources().getStringArray(R.array.stats_units_values);
String[] options = getResources().getStringArray(R.array.stats_units_options); String[] options = getResources().getStringArray(R.array.stats_units_options);
configureListPreference(listPreference, options, options, values, value, listener); configureListPreference(listPreference, options, options, values, value, listener);
@@ -88,16 +99,15 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
*/ */
private void configRateListPreference(boolean metricUnits) { private void configRateListPreference(boolean metricUnits) {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
ListPreference listPreference = (ListPreference) findPreference( ListPreference listPreference = (ListPreference) findPreference(getString(R.string.stats_rate_key));
getString(R.string.stats_rate_key)); String value = PreferencesUtils.getString(this, R.string.stats_rate_key,
String value = PreferencesUtils.getString( PreferencesUtils.STATS_RATE_DEFAULT);
this, R.string.stats_rate_key, PreferencesUtils.STATS_RATE_DEFAULT);
String[] values = getResources().getStringArray(R.array.stats_rate_values); String[] values = getResources().getStringArray(R.array.stats_rate_values);
String[] options = getResources().getStringArray( String[] options = getResources().getStringArray(
metricUnits ? R.array.stats_rate_metric_options : R.array.stats_rate_imperial_options); metricUnits ? R.array.stats_rate_metric_options : R.array.stats_rate_imperial_options);
configureListPreference(listPreference, options, options, values, value, null); configureListPreference(listPreference, options, options, values, value, null);
} }
/** /**
* Configures the calorie preference. * Configures the calorie preference.
* *
@@ -105,8 +115,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
* @param key of the preference * @param key of the preference
* @param defaultValue default value of this preference * @param defaultValue default value of this preference
*/ */
private void configCaloriePreference(CheckBoxPreference preference, int key, boolean defaultValue) { private void configCaloriePreference(CheckBoxPreference preference) {
PreferencesUtils.setBoolean(this, key, PreferencesUtils.getBoolean(this, key, defaultValue));
preference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { preference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@SuppressWarnings("hiding") @SuppressWarnings("hiding")
@Override @Override
@@ -114,6 +123,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
boolean value = (Boolean) newValue; boolean value = (Boolean) newValue;
if (value) { if (value) {
weightPreference.setEnabled(true); weightPreference.setEnabled(true);
showWeightInputPreferenceDialog();
} else { } else {
weightPreference.setEnabled(false); 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. * Configures the weight preference.
* *
@@ -130,9 +176,9 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
* @param defaultValue default value of this preference * @param defaultValue default value of this preference
* @param isEnable true means enable the weight preference * @param isEnable true means enable the weight preference
*/ */
private void configWeightPreference(EditTextPreference preference, int key, int defaultValue, private void configWeightPreference(EditTextPreference preference, final int key,
boolean isEnable) { final int defaultValue, boolean isEnable) {
PreferencesUtils.setInt(this, key, PreferencesUtils.getInt(this, key, defaultValue)); updateWeightSummary(preference, key, defaultValue);
if (isEnable) { if (isEnable) {
preference.setEnabled(true); preference.setEnabled(true);
} else { } else {
@@ -143,17 +189,84 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
@SuppressWarnings("hiding") @SuppressWarnings("hiding")
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
String displayValue = (String) newValue; storeWeightValue(key, defaultValue, (String) newValue);
int value; updateWeightSummary(preference, key, defaultValue);
try { return true;
value = Integer.parseInt(displayValue); }
} catch (NumberFormatException e) { });
Log.e(TAG, "invalid value " + displayValue);
value = PreferencesUtils.TRACK_COLOR_MODE_PERCENTAGE_DEFAULT; preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
} @Override
PreferencesUtils.setInt(StatsSettingsActivity.this, R.string.stats_weight_key, value); public boolean onPreferenceClick(Preference pref) {
int value = getWeightDisplayValue(key, defaultValue,
PreferencesUtils.isMetricUnits(getApplicationContext()));
((EditTextPreference) pref).getEditText().setText(String.valueOf(value));
return true; 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 boolean STATS_SHOW_GRADE_ELEVATION_DEFAULT = false;
public static final String STATS_UNITS_DEFAULT = "METRIC"; public static final String STATS_UNITS_DEFAULT = "METRIC";
public static final boolean STATS_SHOW_CALORIE_DEFAULT = false; 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 // Track color
public static final String TRACK_COLOR_MODE_DEFAULT = "SINGLE"; public static final String TRACK_COLOR_MODE_DEFAULT = "SINGLE";
@@ -45,6 +45,12 @@ public class UnitConversions {
// multiplication factor to convert kilometers to miles // multiplication factor to convert kilometers to miles
public static final double KM_TO_MI = 0.621371192; 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 // multiplication factor to convert miles to kilometers
public static final double MI_TO_KM = 1 / KM_TO_MI; public static final double MI_TO_KM = 1 / KM_TO_MI;