add option to set weight in choose activity type dialog

This commit is contained in:
Jimmy Shih
2013-11-13 17:13:16 -08:00
parent 4a5f2406e7
commit 8e361610bc
8 changed files with 195 additions and 68 deletions
+35 -5
View File
@@ -14,10 +14,40 @@ 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.
-->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:numColumns="4"
android:padding="4dp"
android:stretchMode="spacingWidth"
android:verticalSpacing="4dp" />
android:orientation="vertical" >
<GridView
android:id="@+id/choose_activity_type_grid_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:numColumns="4"
android:padding="4dp"
android:stretchMode="spacingWidth"
android:verticalSpacing="4dp" />
<LinearLayout
android:id="@+id/choose_activity_type_weight_container"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical" >
<TextView
android:layout_marginBottom="2dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="4dp"
android:text="@string/settings_stats_weight"
style="@style/TextMedium" />
<EditText
android:id="@+id/choose_activity_type_weight"
android:inputType="numberDecimal"
android:layout_marginBottom="4dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="2dp"
android:layout_width="match_parent"
android:selectAllOnFocus="true"
android:singleLine="true"
style="@style/TextMedium" />
</LinearLayout>
</LinearLayout>
@@ -620,8 +620,8 @@ public class TrackDetailActivity extends AbstractSendToGoogleActivity
setTitle(title);
}
public void chooseActivityType() {
new ChooseActivityTypeDialogFragment().show(getSupportFragmentManager(),
public void chooseActivityType(String category) {
ChooseActivityTypeDialogFragment.newInstance(category).show(getSupportFragmentManager(),
ChooseActivityTypeDialogFragment.CHOOSE_ACTIVITY_TYPE_DIALOG_TAG);
}
@@ -125,7 +125,8 @@ public class TrackEditActivity extends AbstractMyTracksActivity
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
new ChooseActivityTypeDialogFragment().show(getSupportFragmentManager(),
ChooseActivityTypeDialogFragment.newInstance(activityType.getText().toString()).show(
getSupportFragmentManager(),
ChooseActivityTypeDialogFragment.CHOOSE_ACTIVITY_TYPE_DIALOG_TAG);
}
return true;
@@ -135,7 +136,8 @@ public class TrackEditActivity extends AbstractMyTracksActivity
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
new ChooseActivityTypeDialogFragment().show(getSupportFragmentManager(),
ChooseActivityTypeDialogFragment.newInstance(activityType.getText().toString()).show(
getSupportFragmentManager(),
ChooseActivityTypeDialogFragment.CHOOSE_ACTIVITY_TYPE_DIALOG_TAG);
}
return true;
@@ -16,12 +16,15 @@
package com.google.android.apps.mytracks.fragments;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.apps.mytracks.util.TrackIconUtils;
import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
@@ -30,20 +33,21 @@ import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* A DialogFragment to choose an activity type.
*
*
* @author apoorvn
*/
public class ChooseActivityTypeDialogFragment extends DialogFragment {
/**
* Interface for caller of this dialog fragment.
*
*
* @author apoorvn
*/
public interface ChooseActivityTypeCaller {
@@ -56,7 +60,22 @@ public class ChooseActivityTypeDialogFragment extends DialogFragment {
public static final String CHOOSE_ACTIVITY_TYPE_DIALOG_TAG = "chooseActivityType";
private static final String KEY_CATEGORY = "category";
private ChooseActivityTypeCaller caller;
private ChooseActivityTypeImageAdapter imageAdapter;
private AlertDialog alertDialog;
private View weightContainer;
private TextView weight;
public static ChooseActivityTypeDialogFragment newInstance(String category) {
Bundle bundle = new Bundle();
bundle.putString(KEY_CATEGORY, category);
ChooseActivityTypeDialogFragment fragment = new ChooseActivityTypeDialogFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onAttach(Activity activity) {
@@ -71,12 +90,13 @@ public class ChooseActivityTypeDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
GridView gridView =
(GridView) getActivity().getLayoutInflater().inflate(R.layout.choose_activity_type, null);
View view = getActivity().getLayoutInflater().inflate(R.layout.choose_activity_type, null);
GridView gridView = (GridView) view.findViewById(R.id.choose_activity_type_grid_view);
weightContainer = view.findViewById(R.id.choose_activity_type_weight_container);
weight = (TextView) view.findViewById(R.id.choose_activity_type_weight);
final List<String> iconValues = TrackIconUtils.getAllIconValues();
List<Integer> imageIds = new ArrayList<Integer>();
for (String iconValue : iconValues) {
for (String iconValue : TrackIconUtils.getAllIconValues()) {
imageIds.add(TrackIconUtils.getIconDrawable(iconValue));
}
@@ -88,18 +108,70 @@ public class ChooseActivityTypeDialogFragment extends DialogFragment {
int height = options.outHeight + 2 * padding;
gridView.setColumnWidth(width);
ChooseActivityTypeImageAdapter imageAdapter =
new ChooseActivityTypeImageAdapter(getActivity(), imageIds, width, height, padding);
imageAdapter = new ChooseActivityTypeImageAdapter(
getActivity(), imageIds, width, height, padding);
gridView.setAdapter(imageAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
dismiss();
caller.onChooseActivityTypeDone(iconValues.get(position));
alertDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(true);
imageAdapter.setSelected(position);
imageAdapter.notifyDataSetChanged();
updateWeightContainer(position);
}
});
return new AlertDialog.Builder(getActivity()).setNegativeButton(R.string.generic_cancel, null)
.setTitle(R.string.track_edit_activity_type_hint).setView(gridView).create();
alertDialog = new AlertDialog.Builder(getActivity()).setNegativeButton(
R.string.generic_cancel, null)
.setPositiveButton(R.string.generic_ok, new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (weightContainer.getVisibility() == View.VISIBLE) {
PreferencesUtils.storeWeightValue(getActivity(), weight.getText().toString());
}
int selected = imageAdapter.getSelected();
caller.onChooseActivityTypeDone(TrackIconUtils.getAllIconValues().get(selected));
}
}).setTitle(R.string.track_edit_activity_type_hint).setView(view).create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
int position = getPosition();
alertDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(position != -1);
if (position != -1) {
imageAdapter.setSelected(position);
imageAdapter.notifyDataSetChanged();
}
updateWeightContainer(position);
double weightValue = PreferencesUtils.getWeightDisplayValue(getActivity());
weight.setText(StringUtils.formatWeight(weightValue));
}
});
return alertDialog;
}
private int getPosition() {
String category = getArguments().getString(KEY_CATEGORY);
if (category == null) {
return -1;
}
String iconValue = TrackIconUtils.getIconValue(getActivity(), category);
if (iconValue.equals("")) {
return -1;
}
List<String> iconValues = TrackIconUtils.getAllIconValues();
for (int i = 0; i < iconValues.size(); i++) {
if (iconValues.get(i).equals(iconValue)) {
return i;
}
}
return -1;
}
private void updateWeightContainer(int position) {
boolean showWeight = position == 0 || position == 1 || position == 2;
weightContainer.setVisibility(showWeight ? View.VISIBLE : View.GONE);
}
}
@@ -16,7 +16,10 @@
package com.google.android.apps.mytracks.fragments;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
@@ -36,6 +39,7 @@ public class ChooseActivityTypeImageAdapter extends BaseAdapter {
private final int width;
private final int height;
private final int padding;
private int selected = -1;
public ChooseActivityTypeImageAdapter(
Context context, List<Integer> imageIds, int width, int height, int padding) {
@@ -61,6 +65,14 @@ public class ChooseActivityTypeImageAdapter extends BaseAdapter {
return 0;
}
public int getSelected() {
return selected;
}
public void setSelected(int position) {
selected = position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
@@ -69,6 +81,11 @@ public class ChooseActivityTypeImageAdapter extends BaseAdapter {
} else {
imageView = (ImageView) convertView;
}
if (position == selected) {
imageView.setBackgroundResource(R.drawable.list_selector_background_transition_holo_dark);
} else {
imageView.setBackgroundColor(Color.TRANSPARENT);
}
imageView.setImageResource(imageIds.get(position));
imageView.setMinimumHeight(height);
imageView.setMinimumWidth(width);
@@ -95,7 +95,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
((TrackDetailActivity) getActivity()).chooseActivityType();
((TrackDetailActivity) getActivity()).chooseActivityType(category);
}
return true;
}
@@ -104,7 +104,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
((TrackDetailActivity) getActivity()).chooseActivityType();
((TrackDetailActivity) getActivity()).chooseActivityType(category);
}
return true;
}
@@ -18,7 +18,6 @@ package com.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R;
import android.app.Dialog;
@@ -28,7 +27,6 @@ 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;
@@ -40,8 +38,6 @@ import android.widget.TextView;
*/
public class StatsSettingsActivity extends AbstractSettingsActivity {
private static final String TAG = MapSettingsActivity.class.getSimpleName();
private EditTextPreference weightPreference;
@SuppressWarnings("deprecation")
@@ -69,7 +65,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
weightPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
storeWeightValue((String) newValue);
PreferencesUtils.storeWeightValue(StatsSettingsActivity.this, (String) newValue);
updateWeightSummary(PreferencesUtils.isMetricUnits(StatsSettingsActivity.this));
return true;
}
@@ -78,8 +74,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
weightPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference pref) {
double value = getWeightDisplayValue(
PreferencesUtils.isMetricUnits(getApplicationContext()));
double value = PreferencesUtils.getWeightDisplayValue(StatsSettingsActivity.this);
((EditTextPreference) pref).getEditText().setText(StringUtils.formatWeight(value));
return true;
}
@@ -112,7 +107,6 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
boolean metricUnits = PreferencesUtils.STATS_UNITS_DEFAULT.equals((String) newValue);
configRateListPreference(metricUnits);
updateWeightSummary(metricUnits);
return true;
}
};
@@ -145,41 +139,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
*/
private void updateWeightSummary(boolean metricUnits) {
weightPreference.setSummary(getString(
metricUnits ? R.string.value_kilogram : R.string.value_pound,
StringUtils.formatWeight(getWeightDisplayValue(metricUnits))));
}
/**
* Gets the weight display value in metric or imperial depending on the
* preferred units.
*
* @param metricUnits true if metric units
*/
private double getWeightDisplayValue(boolean metricUnits) {
double value = PreferencesUtils.getFloat(
this, R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT);
if (!metricUnits) {
value = value * UnitConversions.KG_TO_LB;
}
return value;
}
/**
* Stores the weight value, always in metric units.
*
* @param displayValue the display value
*/
private void storeWeightValue(String displayValue) {
double value;
try {
value = Double.parseDouble(displayValue);
if (!PreferencesUtils.isMetricUnits(this)) {
value = value * UnitConversions.LB_TO_KG;
}
} catch (NumberFormatException e) {
Log.e(TAG, "invalid value " + displayValue);
value = PreferencesUtils.STATS_WEIGHT_DEFAULT;
}
PreferencesUtils.setFloat(this, R.string.stats_weight_key, (float) value);
metricUnits ? R.string.value_kilogram : R.string.value_pound, StringUtils.formatWeight(
PreferencesUtils.getWeightDisplayValue(StatsSettingsActivity.this, metricUnits))));
}
}
@@ -27,6 +27,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;
/**
* Utilities to access preferences stored in {@link SharedPreferences}.
@@ -123,6 +124,8 @@ public class PreferencesUtils {
public static final int TRACK_WIDGET_ITEM4_DEFAULT = 2; // average speed
public static final int VOICE_FREQUENCY_DEFAULT = 0;
private static final String TAG = PreferencesUtils.class.getSimpleName();
private PreferencesUtils() {}
/**
@@ -332,4 +335,47 @@ public class PreferencesUtils {
}
setString(context, keyId, list + ";" + value);
}
/**
* Stores the weight value, always in metric units.
*
* @param displayValue the display value
*/
public static void storeWeightValue(Context context, String displayValue) {
double value;
try {
value = Double.parseDouble(displayValue);
if (!PreferencesUtils.isMetricUnits(context)) {
value = value * UnitConversions.LB_TO_KG;
}
} catch (NumberFormatException e) {
Log.e(TAG, "invalid value " + displayValue);
value = PreferencesUtils.STATS_WEIGHT_DEFAULT;
}
PreferencesUtils.setFloat(context, R.string.stats_weight_key, (float) value);
}
/**
* Gets the weight display value.
*
* @param context the context
*/
public static double getWeightDisplayValue(Context context) {
return getWeightDisplayValue(context, PreferencesUtils.isMetricUnits(context));
}
/**
* Gets the weight display value.
*
* @param context the context
* @param metricUnits true to get display value in metric units
*/
public static double getWeightDisplayValue(Context context, boolean metricUnits) {
double value = PreferencesUtils.getFloat(
context, R.string.stats_weight_key, PreferencesUtils.STATS_WEIGHT_DEFAULT);
if (!metricUnits) {
value = value * UnitConversions.KG_TO_LB;
}
return value;
}
}