Add a dialog for user to choose the activty type.

By apoorvn.
This commit is contained in:
Jimmy Shih
2013-03-06 13:46:44 -08:00
parent e561a25542
commit 89e01a706e
7 changed files with 322 additions and 83 deletions
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2013 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.
-->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:numColumns="4"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:stretchMode="columnWidth"
android:verticalSpacing="20dp" />
+21 -6
View File
@@ -35,12 +35,27 @@ limitations under the License.
<TextView
android:id="@+id/track_edit_activity_type_label"
android:text="@string/track_edit_activity_type"
style="@style/EditLabel.WithIcon.WithPaddingTop" />
<AutoCompleteTextView
android:hint="@string/track_edit_activity_type_hint"
android:id="@+id/track_edit_activity_type"
android:imeOptions="actionNext"
style="@style/EditCategory" />
style="@style/EditLabel.WithPaddingTop" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal" >
<AutoCompleteTextView
android:hint="@string/track_edit_activity_type_hint"
android:id="@+id/track_edit_activity_type"
android:imeOptions="actionNext"
android:layout_gravity="bottom"
android:layout_weight="1"
android:layout_width="0dp"
style="@style/EditCategory" />
<ImageButton
android:contentDescription="@string/activity_type_walking"
android:id="@+id/track_edit_activity_type_icon"
android:layout_gravity="bottom"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/track_walk" />
</LinearLayout>
<TextView
android:text="@string/generic_description"
style="@style/EditLabel.WithPaddingTop" />
-4
View File
@@ -68,10 +68,6 @@ limitations under the License.
<style name="EditLabel.WithIcon" parent="@style/TextMedium.WithIcon">
<item name="android:layout_width">match_parent</item>
</style>
<!-- edit label with icon, width padding top -->
<style name="EditLabel.WithIcon.WithPaddingTop">
<item name="android:paddingTop">16dp</item>
</style>
<!-- edit label with padding top -->
<style name="EditLabel.WithPaddingTop">
<item name="android:paddingTop">16dp</item>
@@ -18,6 +18,8 @@ package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.fragments.ChooseActivityTypeDialogFragment;
import com.google.android.apps.mytracks.fragments.ChooseActivityTypeDialogFragment.ChooseActivityTypeCaller;
import com.google.android.apps.mytracks.util.TrackIconUtils;
import com.google.android.apps.mytracks.util.TrackNameUtils;
import com.google.android.maps.mytracks.R;
@@ -30,7 +32,7 @@ import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ImageButton;
/**
* An activity that let's the user see and edit the user editable track meta
@@ -38,20 +40,23 @@ import android.widget.TextView;
*
* @author Leif Hendrik Wilden
*/
public class TrackEditActivity extends AbstractMyTracksActivity {
public class TrackEditActivity extends AbstractMyTracksActivity
implements ChooseActivityTypeCaller {
public static final String EXTRA_TRACK_ID = "track_id";
public static final String EXTRA_NEW_TRACK = "new_track";
private static final String TAG = TrackEditActivity.class.getSimpleName();
private static final String ICON_VALUE = "icon_value";
private Long trackId;
private MyTracksProviderUtils myTracksProviderUtils;
private Track track;
private String iconValue;
private EditText name;
private TextView activityTypeLabel;
private AutoCompleteTextView activityType;
private ImageButton activityTypeIcon;
private EditText description;
@Override
@@ -79,37 +84,50 @@ public class TrackEditActivity extends AbstractMyTracksActivity {
activityType = (AutoCompleteTextView) findViewById(R.id.track_edit_activity_type);
activityType.setText(track.getCategory());
activityTypeLabel = (TextView) findViewById(R.id.track_edit_activity_type_label);
setActivityTypeIcon(track.getIcon());
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.activity_types, android.R.layout.simple_dropdown_item_1line);
activityType.setAdapter(adapter);
activityType.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String iconValue = TrackIconUtils.getIconValue(
TrackEditActivity.this, (String) activityType.getAdapter().getItem(position));
setActivityTypeIcon(iconValue);
setActivityTypeIcon(TrackIconUtils.getIconValue(
TrackEditActivity.this, (String) activityType.getAdapter().getItem(position)));
}
});
activityType.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
String iconValue = TrackIconUtils.getIconValue(
TrackEditActivity.this, activityType.getText().toString());
setActivityTypeIcon(iconValue);
setActivityTypeIcon(TrackIconUtils.getIconValue(
TrackEditActivity.this, activityType.getText().toString()));
}
}
});
activityTypeIcon = (ImageButton) findViewById(R.id.track_edit_activity_type_icon);
activityTypeIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ChooseActivityTypeDialogFragment().show(getSupportFragmentManager(),
ChooseActivityTypeDialogFragment.CHOOSE_ACTIVITY_TYPE_DIALOG_TAG);
}
});
iconValue = null;
if (bundle != null) {
iconValue = bundle.getString(ICON_VALUE);
}
if (iconValue == null) {
iconValue = track.getIcon();
}
setActivityTypeIcon(iconValue);
description = (EditText) findViewById(R.id.track_edit_description);
description.setText(track.getDescription());
Button save = (Button) findViewById(R.id.track_edit_save);
save.setOnClickListener(new View.OnClickListener() {
@Override
@Override
public void onClick(View v) {
track.setName(name.getText().toString());
String category = activityType.getText().toString();
@@ -134,7 +152,7 @@ public class TrackEditActivity extends AbstractMyTracksActivity {
} else {
setTitle(R.string.menu_edit);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
@Override
public void onClick(View v) {
finish();
}
@@ -143,6 +161,12 @@ public class TrackEditActivity extends AbstractMyTracksActivity {
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(ICON_VALUE, iconValue);
}
@Override
protected int getLayoutResId() {
return R.layout.track_edit;
@@ -151,10 +175,16 @@ public class TrackEditActivity extends AbstractMyTracksActivity {
/**
* Sets the activity type icon.
*
* @param iconValue the icon value
* @param value the icon value
*/
private void setActivityTypeIcon(String iconValue) {
activityTypeLabel.setCompoundDrawablesWithIntrinsicBounds(
TrackIconUtils.getIconDrawable(iconValue), 0, 0, 0);
private void setActivityTypeIcon(String value) {
iconValue = value;
activityTypeIcon.setImageResource(TrackIconUtils.getIconDrawable(value));
}
@Override
public void onChooseActivityTypeDone(String value) {
activityType.setText(getString(TrackIconUtils.getIconActivityType(value)));
setActivityTypeIcon(value);
}
}
@@ -0,0 +1,95 @@
/*
* Copyright 2013 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.fragments;
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.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
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 {
/**
* Called when choose activity type is done.
*/
public void onChooseActivityTypeDone(String iconValue);
}
public static final String CHOOSE_ACTIVITY_TYPE_DIALOG_TAG = "chooseActivityType";
private ChooseActivityTypeCaller caller;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
caller = (ChooseActivityTypeCaller) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement "
+ ChooseActivityTypeCaller.class.getSimpleName());
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
GridView gridView = (GridView) getActivity()
.getLayoutInflater().inflate(R.layout.choose_activity_type, null);
final List<String> iconValues = TrackIconUtils.getAllIconValues();
List<Integer> imageIds = new ArrayList<Integer>();
for (String iconValue : iconValues) {
imageIds.add(TrackIconUtils.getIconDrawable(iconValue));
}
ChooseActivityTypeImageAdapter imageAdapter = new ChooseActivityTypeImageAdapter(
getActivity(), imageIds);
gridView.setAdapter(imageAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
dismiss();
caller.onChooseActivityTypeDone(iconValues.get(position));
}
});
return new AlertDialog.Builder(getActivity()).setNegativeButton(R.string.generic_cancel, null)
.setTitle(R.string.track_edit_activity_type_hint).setView(gridView).create();
}
}
@@ -0,0 +1,68 @@
/*
* Copyright 2013 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.fragments;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import java.util.List;
/**
* Image adapter for choosing an activity type.
*
* @author apoorvn
*/
public class ChooseActivityTypeImageAdapter extends BaseAdapter {
private final Context context;
private final List<Integer> imageIds;
public ChooseActivityTypeImageAdapter(Context context, List<Integer> imageIds) {
this.context = context;
this.imageIds = imageIds;
}
@Override
public int getCount() {
return imageIds.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIds.get(position));
return imageView;
}
}
@@ -19,12 +19,15 @@ package com.google.android.apps.mytracks.util;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.util.Pair;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
/**
* Utilities for track icon.
*
*
* @author Jimmy Shih
*/
public class TrackIconUtils {
@@ -40,60 +43,44 @@ public class TrackIconUtils {
private TrackIconUtils() {}
private static HashMap<String, Integer> map = new HashMap<String, Integer>();
private static LinkedHashMap<String, Pair<Integer, Integer>>
map = new LinkedHashMap<String, Pair<Integer, Integer>>();
static {
map.put(AIRPLANE, R.drawable.track_airplane);
map.put(BIKE, R.drawable.track_bike);
map.put(BOAT, R.drawable.track_boat);
map.put(DRIVE, R.drawable.track_drive);
map.put(RUN, R.drawable.track_run);
map.put(SKI, R.drawable.track_ski);
map.put(SNOW_BOARDING, R.drawable.track_snow_boarding);
map.put(WALK, R.drawable.track_walk);
map.put(AIRPLANE,
new Pair<Integer, Integer>(R.string.activity_type_airplane, R.drawable.track_airplane));
map.put(BIKE, new Pair<Integer, Integer>(R.string.activity_type_biking, R.drawable.track_bike));
map.put(BOAT, new Pair<Integer, Integer>(R.string.activity_type_boat, R.drawable.track_boat));
map.put(
DRIVE, new Pair<Integer, Integer>(R.string.activity_type_driving, R.drawable.track_drive));
map.put(RUN, new Pair<Integer, Integer>(R.string.activity_type_running, R.drawable.track_run));
map.put(SKI, new Pair<Integer, Integer>(R.string.activity_type_skiing, R.drawable.track_ski));
map.put(SNOW_BOARDING, new Pair<Integer, Integer>(
R.string.activity_type_snow_boarding, R.drawable.track_snow_boarding));
map.put(
WALK, new Pair<Integer, Integer>(R.string.activity_type_walking, R.drawable.track_walk));
}
private static int[] airplane = new int[] {
R.string.activity_type_airplane,
R.string.activity_type_commercial_airplane,
R.string.activity_type_rc_airplane
};
private static int[] bike = new int[] {
R.string.activity_type_biking,
R.string.activity_type_cycling,
R.string.activity_type_dirt_bike,
R.string.activity_type_motor_bike,
R.string.activity_type_mountain_biking,
R.string.activity_type_road_biking,
R.string.activity_type_track_cycling };
private static int[] boat = new int[] {
R.string.activity_type_boat,
R.string.activity_type_ferry,
R.string.activity_type_motor_boating,
R.string.activity_type_rc_boat
};
private static int[] drive = new int[] {
R.string.activity_type_atv,
R.string.activity_type_driving,
R.string.activity_type_driving_bus,
private static int[] airplane = new int[] { R.string.activity_type_airplane,
R.string.activity_type_commercial_airplane, R.string.activity_type_rc_airplane };
private static int[] bike = new int[] { R.string.activity_type_biking,
R.string.activity_type_cycling, R.string.activity_type_dirt_bike,
R.string.activity_type_motor_bike, R.string.activity_type_mountain_biking,
R.string.activity_type_road_biking, R.string.activity_type_track_cycling };
private static int[] boat = new int[] { R.string.activity_type_boat, R.string.activity_type_ferry,
R.string.activity_type_motor_boating, R.string.activity_type_rc_boat };
private static int[] drive = new int[] { R.string.activity_type_atv,
R.string.activity_type_driving, R.string.activity_type_driving_bus,
R.string.activity_type_driving_car };
private static int[] run = new int[] {
R.string.activity_type_running,
R.string.activity_type_street_running,
R.string.activity_type_track_running,
private static int[] run = new int[] { R.string.activity_type_running,
R.string.activity_type_street_running, R.string.activity_type_track_running,
R.string.activity_type_trail_running };
private static int[] ski = new int[] {
R.string.activity_type_cross_country_skiing,
R.string.activity_type_skiing };
private static int[] snowBoarding = new int[] {
R.string.activity_type_snow_boarding
};
private static int[] walk = new int[] {
R.string.activity_type_hiking,
R.string.activity_type_off_trail_hiking,
R.string.activity_type_speed_walking,
R.string.activity_type_trail_hiking,
R.string.activity_type_walking };
R.string.activity_type_cross_country_skiing, R.string.activity_type_skiing };
private static int[] snowBoarding = new int[] { R.string.activity_type_snow_boarding };
private static int[] walk = new int[] { R.string.activity_type_hiking,
R.string.activity_type_off_trail_hiking, R.string.activity_type_speed_walking,
R.string.activity_type_trail_hiking, R.string.activity_type_walking };
/**
* Gets the icon drawable.
@@ -104,10 +91,34 @@ public class TrackIconUtils {
if (iconValue == null || iconValue.equals("")) {
return R.drawable.track_walk;
}
Integer drawable = map.get(iconValue);
return drawable == null ? R.drawable.track_walk : drawable;
Pair<Integer, Integer> pair = map.get(iconValue);
return pair == null ? R.drawable.track_walk : pair.second;
}
/**
* Gets the icon activity type.
*
* @param iconValue the icon value
*/
public static int getIconActivityType(String iconValue) {
if (iconValue == null || iconValue.equals("")) {
return R.string.activity_type_walking;
}
Pair<Integer, Integer> pair = map.get(iconValue);
return pair == null ? R.string.activity_type_walking : pair.first;
}
/**
* Gets all icon values.
*/
public static List<String> getAllIconValues() {
List<String> values = new ArrayList<String>();
for (String value : map.keySet()) {
values.add(value);
}
return values;
}
/**
* Gets the icon value.
*
@@ -117,13 +128,13 @@ public class TrackIconUtils {
public static String getIconValue(Context context, String activityType) {
if (inList(context, activityType, airplane)) {
return AIRPLANE;
}
}
if (inList(context, activityType, bike)) {
return BIKE;
}
if (inList(context, activityType, boat)) {
return BOAT;
}
}
if (inList(context, activityType, drive)) {
return DRIVE;
}