diff --git a/OsmAnd/src/net/osmand/plus/GPXUtilities.java b/OsmAnd/src/net/osmand/plus/GPXUtilities.java index bf37c31cb1..315896d9ad 100644 --- a/OsmAnd/src/net/osmand/plus/GPXUtilities.java +++ b/OsmAnd/src/net/osmand/plus/GPXUtilities.java @@ -6,6 +6,7 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.support.annotation.ColorInt; +import android.text.TextUtils; import net.osmand.Location; import net.osmand.PlatformUtil; @@ -1034,6 +1035,19 @@ public class GPXUtilities { } return count; } + + public List getWaypointCategories() { + List categories = new ArrayList<>(); + for (WptPt pt : points) { + String category = pt.category; + if (!TextUtils.isEmpty(category)) { + if (!categories.contains(category)) { + categories.add(category); + } + } + } + return categories; + } } public static String asString(GPXFile file, OsmandApplication ctx) { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragment.java index 15f9925d72..0c532d421d 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragment.java @@ -118,7 +118,7 @@ public abstract class PointEditorFragment extends Fragment { public boolean onTouch(final View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { DialogFragment dialogFragment = - SelectCategoryDialogFragment.createInstance(getEditor().getFragmentTag()); + createSelectCategoryDialog(); dialogFragment.show(getChildFragmentManager(), SelectCategoryDialogFragment.TAG); return true; } @@ -176,6 +176,10 @@ public abstract class PointEditorFragment extends Fragment { return view; } + protected DialogFragment createSelectCategoryDialog() { + return SelectCategoryDialogFragment.createInstance(getEditor().getFragmentTag()); + } + public Drawable getRowIcon(int iconId) { IconsCache iconsCache = getMyApplication().getIconsCache(); return iconsCache.getIcon(iconId, diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/RtePtEditorFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/RtePtEditorFragment.java index 250faf4d24..97ead03fae 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/RtePtEditorFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/RtePtEditorFragment.java @@ -1,5 +1,7 @@ package net.osmand.plus.mapcontextmenu.editors; +import android.support.v4.app.DialogFragment; + import net.osmand.plus.GPXUtilities; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; @@ -16,6 +18,11 @@ public class RtePtEditorFragment extends WptPtEditorFragment { return getMapActivity().getResources().getString(R.string.save_route_point); } + @Override + protected DialogFragment createSelectCategoryDialog() { + return SelectCategoryDialogFragment.createInstance(getEditor().getFragmentTag()); + } + public static void showInstance(final MapActivity mapActivity) { RtePtEditor editor = mapActivity.getContextMenu().getRtePtPointEditor(); //int slideInAnim = editor.getSlideInAnimation(); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/SelectCategoryDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/SelectCategoryDialogFragment.java index 6a0587cfd2..87ed37f512 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/SelectCategoryDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/SelectCategoryDialogFragment.java @@ -15,6 +15,7 @@ import android.widget.Button; import android.widget.LinearLayout; import net.osmand.plus.FavouritesDbHelper; +import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.IconsCache; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; @@ -40,6 +41,15 @@ public class SelectCategoryDialogFragment extends DialogFragment { private String editorTag; private CategorySelectionListener selectionListener; + private GPXFile gpxFile; + + public void setGpxFile(GPXFile gpxFile) { + this.gpxFile = gpxFile; + } + + public GPXFile getGpxFile() { + return gpxFile; + } @NonNull @Override @@ -58,36 +68,16 @@ public class SelectCategoryDialogFragment extends DialogFragment { LinearLayout ll = (LinearLayout) v.findViewById(R.id.list_container); final FavouritesDbHelper helper = ((OsmandApplication) getActivity().getApplication()).getFavorites(); - List gs = helper.getFavoriteGroups(); - for (final FavouritesDbHelper.FavoriteGroup category : gs) { - View itemView = getActivity().getLayoutInflater().inflate(R.layout.favorite_category_dialog_item, null); - Button button = (Button)itemView.findViewById(R.id.button); - if (category.color != 0) { - button.setCompoundDrawablesWithIntrinsicBounds(getIcon(getActivity(), R.drawable.ic_action_folder, category.color), null, null, null); - } else { - button.setCompoundDrawablesWithIntrinsicBounds(getIcon(getActivity(), R.drawable.ic_action_folder, getResources().getColor(R.color.color_favorite)), null, null, null); + if (gpxFile != null) { + List categories = gpxFile.getWaypointCategories(); + for (final String category : categories) { + addCategory(ll, category, 0); + } + } else { + List gs = helper.getFavoriteGroups(); + for (final FavouritesDbHelper.FavoriteGroup category : gs) { + addCategory(ll, category.name, category.color); } - button.setCompoundDrawablePadding(dpToPx(15f)); - String name = category.name.length() == 0 ? getString(R.string.shared_string_favorites) : category.name; - button.setText(name); - button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - - PointEditor editor = ((MapActivity) getActivity()).getContextMenu().getPointEditor(editorTag); - - if (editor != null) { - editor.setCategory(category.name); - } - - if (selectionListener != null) { - selectionListener.onCategorySelected(category.name, category.color); - } - - dismiss(); - } - }); - ll.addView(itemView); } View itemView = getActivity().getLayoutInflater().inflate(R.layout.favorite_category_dialog_item, null); Button button = (Button)itemView.findViewById(R.id.button); @@ -111,6 +101,40 @@ public class SelectCategoryDialogFragment extends DialogFragment { return builder.create(); } + private void addCategory(LinearLayout ll, final String categoryName, final int categoryColor) { + View itemView = getActivity().getLayoutInflater().inflate(R.layout.favorite_category_dialog_item, null); + Button button = (Button)itemView.findViewById(R.id.button); + if (categoryColor != 0) { + button.setCompoundDrawablesWithIntrinsicBounds(getIcon(getActivity(), R.drawable.ic_action_folder, categoryColor), null, null, null); + } else { + button.setCompoundDrawablesWithIntrinsicBounds(getIcon(getActivity(), R.drawable.ic_action_folder, getResources().getColor(R.color.color_favorite)), null, null, null); + } + button.setCompoundDrawablePadding(dpToPx(15f)); + String name = categoryName.length() == 0 ? getString(R.string.shared_string_favorites) : categoryName; + button.setText(name); + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + PointEditor editor = ((MapActivity) getActivity()).getContextMenu().getPointEditor(editorTag); + + if (editor != null) { + editor.setCategory(categoryName); + if (gpxFile != null && editor instanceof WptPtEditor) { + ((WptPtEditor) editor).getWptPt().category = categoryName; + } + } + + if (selectionListener != null) { + selectionListener.onCategorySelected(categoryName, categoryColor); + } + + dismiss(); + } + }); + ll.addView(itemView); + } + public static SelectCategoryDialogFragment createInstance(String editorTag) { SelectCategoryDialogFragment fragment = new SelectCategoryDialogFragment(); Bundle bundle = new Bundle(); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/WptPtEditorFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/WptPtEditorFragment.java index 9bc3edb6b7..770213aa8f 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/WptPtEditorFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/WptPtEditorFragment.java @@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.v4.app.DialogFragment; import android.view.View; import net.osmand.data.LatLon; @@ -47,6 +48,16 @@ public class WptPtEditorFragment extends PointEditorFragment { defaultColor = getResources().getColor(R.color.gpx_color_point); } + @Override + protected DialogFragment createSelectCategoryDialog() { + SelectCategoryDialogFragment selectCategoryDialogFragment = SelectCategoryDialogFragment.createInstance(getEditor().getFragmentTag()); + GPXFile gpx = editor.getGpxFile(); + if (gpx != null) { + selectCategoryDialogFragment.setGpxFile(gpx); + } + return selectCategoryDialogFragment; + } + protected void assignEditor() { editor = getMapActivity().getContextMenu().getWptPtPointEditor(); }