From 053abea161e0907eb141c6c2020dc4a829d69f4c Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 12 Nov 2015 10:30:43 +0100 Subject: [PATCH] Refactor edit poi --- OsmAnd-java/src/net/osmand/data/Amenity.java | 2 +- .../src/net/osmand/data/MapObject.java | 2 +- .../src/net/osmand/osm/AbstractPoiType.java | 10 ++- .../src/net/osmand/osm/MapPoiTypes.java | 1 + OsmAnd-java/src/net/osmand/osm/PoiType.java | 11 +--- .../plus/osmedit/AdvancedEditPoiFragment.java | 9 ++- .../net/osmand/plus/osmedit/EditPoiData.java | 61 +++++++++++++++---- .../plus/osmedit/EditPoiDialogFragment.java | 46 ++++++-------- .../dialogs/PoiSubTypeDialogFragment.java | 12 ++-- .../dialogs/PoiTypeDialogFragment.java | 15 +---- 10 files changed, 94 insertions(+), 75 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/data/Amenity.java b/OsmAnd-java/src/net/osmand/data/Amenity.java index 5094c698b1..d077605839 100644 --- a/OsmAnd-java/src/net/osmand/data/Amenity.java +++ b/OsmAnd-java/src/net/osmand/data/Amenity.java @@ -28,7 +28,7 @@ public class Amenity extends MapObject { private static final long serialVersionUID = 132083949926339552L; private String subType; - private transient PoiCategory type; + private PoiCategory type; // duplicate for fast access private String openingHours; private Map additionalInfo; diff --git a/OsmAnd-java/src/net/osmand/data/MapObject.java b/OsmAnd-java/src/net/osmand/data/MapObject.java index 7211e48d50..59c382a890 100644 --- a/OsmAnd-java/src/net/osmand/data/MapObject.java +++ b/OsmAnd-java/src/net/osmand/data/MapObject.java @@ -18,7 +18,7 @@ import net.osmand.util.Algorithms; import net.sf.junidecode.Junidecode; -public abstract class MapObject implements Comparable, Serializable { +public abstract class MapObject implements Comparable { protected String name = null; protected String enName = null; protected Map names = null; diff --git a/OsmAnd-java/src/net/osmand/osm/AbstractPoiType.java b/OsmAnd-java/src/net/osmand/osm/AbstractPoiType.java index ab6db1c5ee..c1e851a408 100644 --- a/OsmAnd-java/src/net/osmand/osm/AbstractPoiType.java +++ b/OsmAnd-java/src/net/osmand/osm/AbstractPoiType.java @@ -14,7 +14,7 @@ public abstract class AbstractPoiType { private boolean topVisible; private String lang; private AbstractPoiType baseLangType; - + private boolean notEditableOsm; public AbstractPoiType(String keyName, MapPoiTypes registry) { this.keyName = keyName; @@ -76,6 +76,14 @@ public abstract class AbstractPoiType { return poiAdditionals; } + public boolean isNotEditableOsm() { + return notEditableOsm; + } + + public void setNotEditableOsm(boolean notEditableOsm) { + this.notEditableOsm = notEditableOsm; + } + public abstract Map> putTypes(Map> acceptedTypes); diff --git a/OsmAnd-java/src/net/osmand/osm/MapPoiTypes.java b/OsmAnd-java/src/net/osmand/osm/MapPoiTypes.java index ee1702c730..151d9e2913 100644 --- a/OsmAnd-java/src/net/osmand/osm/MapPoiTypes.java +++ b/OsmAnd-java/src/net/osmand/osm/MapPoiTypes.java @@ -274,6 +274,7 @@ public class MapPoiTypes { if (name.equals("poi_category")) { lastCategory = new PoiCategory(this, parser.getAttributeValue("", "name"), categories.size()); lastCategory.setTopVisible(Boolean.parseBoolean(parser.getAttributeValue("", "top"))); + lastCategory.setNotEditableOsm("true".equals(parser.getAttributeValue("", "no_edit"))); categories.add(lastCategory); } else if (name.equals("poi_filter")) { PoiFilter tp = new PoiFilter(this, lastCategory, parser.getAttributeValue("", "name")); diff --git a/OsmAnd-java/src/net/osmand/osm/PoiType.java b/OsmAnd-java/src/net/osmand/osm/PoiType.java index 0e9b3741f2..7e00309faf 100644 --- a/OsmAnd-java/src/net/osmand/osm/PoiType.java +++ b/OsmAnd-java/src/net/osmand/osm/PoiType.java @@ -12,7 +12,7 @@ public class PoiType extends AbstractPoiType { private String osmTag2; private String osmValue; private String osmValue2; - private boolean notEditableOsm; + private String nameTag; private boolean text; @@ -145,14 +145,7 @@ public class PoiType extends AbstractPoiType { this.relation = relation; } - public boolean isNotEditableOsm() { - return notEditableOsm; - } - - public void setNotEditableOsm(boolean notEditableOsm) { - this.notEditableOsm = notEditableOsm; - } - + @Override public String toString() { return "PoiType{" + diff --git a/OsmAnd/src/net/osmand/plus/osmedit/AdvancedEditPoiFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/AdvancedEditPoiFragment.java index a5f58bc825..53d99a198d 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/AdvancedEditPoiFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/AdvancedEditPoiFragment.java @@ -110,14 +110,13 @@ public class AdvancedEditPoiFragment extends Fragment nameTextView.setText(value); } if (Algorithms.objectEquals(anyTag, EditPoiData.POI_TYPE_TAG)) { - String subType = value == null ? "" : value.trim().toLowerCase(); - if (allTranslatedSubTypes.get(subType) != null) { - PoiType pt = allTranslatedSubTypes.get(subType); + PoiType pt = getData().getPoiTypeDefined(); + if (pt != null) { amenityTagTextView.setText(pt.getOsmTag()); amenityTextView.setText(pt.getOsmValue()); } else { - amenityTagTextView.setText(getData().amenity.getType().getDefaultTag()); - amenityTextView.setText(subType); + amenityTagTextView.setText(getData().getPoiCategory().getDefaultTag()); + amenityTextView.setText(getData().getPoiTypeString()); } } } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/EditPoiData.java b/OsmAnd/src/net/osmand/plus/osmedit/EditPoiData.java index 2057c00765..c8cb9bc86e 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/EditPoiData.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/EditPoiData.java @@ -2,10 +2,12 @@ package net.osmand.plus.osmedit; import net.osmand.PlatformUtil; import net.osmand.data.Amenity; +import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiType; import net.osmand.osm.edit.Node; import net.osmand.osm.edit.OSMSettings; import net.osmand.osm.edit.OSMSettings.OSMTagKey; +import net.osmand.plus.OsmandApplication; import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -22,16 +24,42 @@ public class EditPoiData { private LinkedHashMap tagValues = new LinkedHashMap(); private boolean isInEdit = false; private Node entity; - public final Amenity amenity; + public static final String POI_TYPE_TAG = "poi_type_tag"; private boolean hasChangesBeenMade = false; - - public EditPoiData(Amenity amenity, Node node, Map allTranslatedSubTypes) { - this.amenity = amenity; + private Map allTranslatedSubTypes; + private PoiCategory category; + private String subtype; + + public EditPoiData(Node node, OsmandApplication app) { + allTranslatedSubTypes = app.getPoiTypes().getAllTranslatedNames(true); + category = app.getPoiTypes().getOtherPoiCategory(); + this.subtype = ""; entity = node; - initTags(node, allTranslatedSubTypes); + initTags(node); } + public Map getAllTranslatedSubTypes() { + return allTranslatedSubTypes; + } + + public void updateType(PoiCategory type) { + category = type; + subtype = ""; + } + + + public PoiCategory getPoiCategory() { + return category; + } + + public PoiType getPoiTypeDefined() { + return allTranslatedSubTypes.get(getPoiTypeString()); + } + + public String getPoiTypeString() { + return subtype; + } public Node getEntity() { return entity; @@ -42,8 +70,10 @@ public class EditPoiData { } public void updateTags(Map mp) { + checkNotInEdit(); this.tagValues.clear(); this.tagValues.putAll(mp); + retrieveType(); } private void tryAddTag(String key, String value) { @@ -52,18 +82,23 @@ public class EditPoiData { } } - private void initTags(Node node, Map allTranslatedSubTypes) { + private void initTags(Node node) { checkNotInEdit(); for (String s : node.getTagKeySet()) { tryAddTag(s, node.getTag(s)); } - String subType = amenity.getSubType(); - String value =""; - PoiType pt = amenity.getType().getPoiTypeByKeyName(subType); - if (pt != null) { - value = pt.getTranslation(); + retrieveType(); + } + + private void retrieveType() { + String tp = tagValues.get(POI_TYPE_TAG); + if(tp != null) { + PoiType pt = allTranslatedSubTypes.get(tp); + if (pt != null) { + subtype = tp; + category = pt.getCategory(); + } } - tagValues.put(POI_TYPE_TAG, value); } @@ -146,5 +181,7 @@ public class EditPoiData { public boolean hasChangesBeenMade() { return hasChangesBeenMade; } + + } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/osmedit/EditPoiDialogFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/EditPoiDialogFragment.java index b74048b7ab..2d2b194c4e 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/EditPoiDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/EditPoiDialogFragment.java @@ -44,11 +44,11 @@ import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; - import net.osmand.PlatformUtil; import net.osmand.access.AccessibleToast; import net.osmand.data.Amenity; import net.osmand.data.LatLon; +import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiType; import net.osmand.osm.edit.EntityInfo; import net.osmand.osm.edit.Node; @@ -75,7 +75,6 @@ public class EditPoiDialogFragment extends DialogFragment { private static final Log LOG = PlatformUtil.getLog(EditPoiDialogFragment.class); private static final String KEY_AMENITY_NODE = "key_amenity_node"; - private static final String KEY_AMENITY = "key_amenity"; private static final String TAGS_LIST = "tags_list"; private static final String IS_ADDING_POI = "is_adding_poi"; @@ -99,10 +98,7 @@ public class EditPoiDialogFragment extends DialogFragment { } Node node = (Node) getArguments().getSerializable(KEY_AMENITY_NODE); - allTranslatedSubTypes = getMyApplication().getPoiTypes().getAllTranslatedNames(true); - - Amenity amenity = (Amenity) getArguments().getSerializable(KEY_AMENITY); - editPoiData = new EditPoiData(amenity, node, allTranslatedSubTypes); + editPoiData = new EditPoiData(node, getMyApplication()); } @Override @@ -201,7 +197,7 @@ public class EditPoiDialogFragment extends DialogFragment { poiTypeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - DialogFragment fragment = PoiTypeDialogFragment.createInstance(editPoiData.amenity); + DialogFragment fragment = PoiTypeDialogFragment.createInstance(); fragment.show(getChildFragmentManager(), "PoiTypeDialogFragment"); } }); @@ -249,7 +245,7 @@ public class EditPoiDialogFragment extends DialogFragment { }); poiNameEditText.setOnEditorActionListener(mOnEditorActionListener); poiTypeEditText.setOnEditorActionListener(mOnEditorActionListener); - poiTypeEditText.setText(editPoiData.amenity.getSubType()); + poiTypeEditText.setText(editPoiData.getPoiTypeString()); Button saveButton = (Button) view.findViewById(R.id.saveButton); saveButton.setText(mOpenstreetmapUtil instanceof OpenstreetmapRemoteUtil @@ -297,7 +293,7 @@ public class EditPoiDialogFragment extends DialogFragment { node.putTag(poiType.getOsmTag2(), poiType.getOsmValue2()); } } else { - node.putTag(editPoiData.amenity.getType().getDefaultTag(), tag.getValue()); + node.putTag(editPoiData.getPoiCategory().getDefaultTag(), tag.getValue()); } } else if (!Algorithms.isEmpty(tag.getKey()) && !Algorithms.isEmpty(tag.getValue())) { @@ -306,15 +302,15 @@ public class EditPoiDialogFragment extends DialogFragment { } commitNode(action, node, mOpenstreetmapUtil.getEntityInfo(), "", - false,//closeChange.isSelected(), + false, new Runnable() { @Override public void run() { OsmEditingPlugin plugin = OsmandPlugin.getPlugin(OsmEditingPlugin.class); - if (plugin != null) { + if (plugin != null && mOpenstreetmapUtil instanceof OpenstreetmapLocalUtil) { List points = plugin.getDBPOI().getOpenstreetmapPoints(); - OsmPoint point = points.get(points.size() - 1); - if (getActivity() instanceof MapActivity) { + if (getActivity() instanceof MapActivity && points.size() > 0) { + OsmPoint point = points.get(points.size() - 1); MapActivity mapActivity = (MapActivity) getActivity(); mapActivity.getContextMenu().showOrUpdate(new LatLon(point.getLatitude(), point.getLongitude()), plugin.getOsmEditsLayer(mapActivity).getObjectName(point), point); @@ -408,9 +404,10 @@ public class EditPoiDialogFragment extends DialogFragment { }.execute(); } - public void updateType(Amenity amenity) { - poiTypeEditText.setText(amenity.getSubType()); - poiTypeTextInputLayout.setHint(amenity.getType().getTranslation()); + public void updateType(PoiCategory type) { + editPoiData.updateType(type); + poiTypeEditText.setText(editPoiData.getPoiTypeString()); + poiTypeTextInputLayout.setHint(editPoiData.getPoiCategory().getTranslation()); setAdapterForPoiTypeEditText(); poiTypeEditText.setOnTouchListener(new View.OnTouchListener() { @Override @@ -421,9 +418,9 @@ public class EditPoiDialogFragment extends DialogFragment { if (event.getX() >= (editText.getRight() - editText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width() - editText.getPaddingRight())) { - if (editPoiData.amenity.getType() != null) { + if (editPoiData.getPoiCategory() != null) { DialogFragment dialogFragment = - PoiSubTypeDialogFragment.createInstance(editPoiData.amenity); + PoiSubTypeDialogFragment.createInstance(editPoiData.getPoiCategory()); dialogFragment.show(getChildFragmentManager(), "PoiSubTypeDialogFragment"); } @@ -497,20 +494,13 @@ public class EditPoiDialogFragment extends DialogFragment { public static EditPoiDialogFragment createAddPoiInstance(double latitude, double longitude, OsmandApplication application) { Node node = new Node(latitude, longitude, -1); - Amenity amenity; - amenity = new Amenity(); - amenity.setType(application.getPoiTypes().getOtherPoiCategory()); - amenity.setSubType(""); - amenity.setAdditionalInfo(OSMSettings.OSMTagKey.OPENING_HOURS.getValue(), ""); - return createInstance(node, amenity, true); + return createInstance(node, true); } - public static EditPoiDialogFragment createInstance(Node node, Amenity amenity, - boolean isAddingPoi) { + public static EditPoiDialogFragment createInstance(Node node, boolean isAddingPoi) { EditPoiDialogFragment editPoiDialogFragment = new EditPoiDialogFragment(); Bundle args = new Bundle(); args.putSerializable(KEY_AMENITY_NODE, node); - args.putSerializable(KEY_AMENITY, amenity); args.putBoolean(IS_ADDING_POI, isAddingPoi); editPoiDialogFragment.setArguments(args); return editPoiDialogFragment; @@ -537,7 +527,7 @@ public class EditPoiDialogFragment extends DialogFragment { protected void onPostExecute(Node n) { if (n != null) { EditPoiDialogFragment fragment = - EditPoiDialogFragment.createInstance(n, amenity, false); + EditPoiDialogFragment.createInstance(n, false); fragment.show(activity.getSupportFragmentManager(), TAG); } else { AccessibleToast.makeText(activity, diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/PoiSubTypeDialogFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/PoiSubTypeDialogFragment.java index 9bd5ea3084..f2e9d8216a 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/PoiSubTypeDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/PoiSubTypeDialogFragment.java @@ -6,9 +6,9 @@ import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; - import net.osmand.data.Amenity; import net.osmand.osm.MapPoiTypes; +import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiType; import net.osmand.plus.OsmandApplication; import net.osmand.plus.osmedit.EditPoiDialogFragment; @@ -17,15 +17,15 @@ import java.util.Map; import java.util.Set; public class PoiSubTypeDialogFragment extends DialogFragment { - private static final String KEY_AMENITY = "amenity"; + private static final String KEY_POI_CATEGORY = "amenity"; @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { MapPoiTypes poiTypes = ((OsmandApplication) getActivity().getApplication()).getPoiTypes(); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - final Amenity a = (Amenity) getArguments().getSerializable(KEY_AMENITY); - final Map allTranslatedNames = poiTypes.getAllTranslatedNames(a.getType(), true); + final PoiCategory a = poiTypes.getPoiCategoryByName((String) getArguments().getSerializable(KEY_POI_CATEGORY)); + final Map allTranslatedNames = poiTypes.getAllTranslatedNames(a, true); Set strings = allTranslatedNames.keySet(); final String[] subCats = strings.toArray(new String[strings.size()]); builder.setItems(subCats, new DialogInterface.OnClickListener() { @@ -38,10 +38,10 @@ public class PoiSubTypeDialogFragment extends DialogFragment { return builder.create(); } - public static PoiSubTypeDialogFragment createInstance(Amenity amenity) { + public static PoiSubTypeDialogFragment createInstance(PoiCategory cat) { PoiSubTypeDialogFragment fragment = new PoiSubTypeDialogFragment(); Bundle args = new Bundle(); - args.putSerializable(KEY_AMENITY, amenity); + args.putSerializable(KEY_POI_CATEGORY, cat.getKeyName()); fragment.setArguments(args); return fragment; } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/PoiTypeDialogFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/PoiTypeDialogFragment.java index b9f1641914..5fc7c685a8 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/PoiTypeDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/PoiTypeDialogFragment.java @@ -17,21 +17,17 @@ import java.util.ArrayList; import java.util.List; public class PoiTypeDialogFragment extends DialogFragment { - private static final String KEY_AMENITY = "amenity"; @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { MapPoiTypes poiTypes = ((OsmandApplication) getActivity().getApplication()).getPoiTypes(); - final Amenity amenity = (Amenity) getArguments().getSerializable(KEY_AMENITY); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final List categories = poiTypes.getCategories(false); ArrayList vals = new ArrayList<>(categories.size()); ArrayList toDelete = new ArrayList<>(); - // TODO replace with constants for (PoiCategory category : categories) { - if (category.getKeyName().equals("user_defined_other") - || category.getKeyName().equals("osmwiki")) { + if (!category.isNotEditableOsm()) { toDelete.add(category); } else { vals.add(category.getTranslation()); @@ -42,21 +38,16 @@ public class PoiTypeDialogFragment extends DialogFragment { @Override public void onClick(DialogInterface dialog, int which) { PoiCategory aType = categories.get(which); - if (aType != amenity.getType()) { - amenity.setType(aType); - amenity.setSubType(""); //$NON-NLS-1$ - ((EditPoiDialogFragment) getParentFragment()).updateType(amenity); - } + ((EditPoiDialogFragment) getParentFragment()).updateType(aType); dismiss(); } }); return builder.create(); } - public static PoiTypeDialogFragment createInstance(Amenity amenity) { + public static PoiTypeDialogFragment createInstance() { PoiTypeDialogFragment poiTypeDialogFragment = new PoiTypeDialogFragment(); Bundle args = new Bundle(); - args.putSerializable(KEY_AMENITY, amenity); poiTypeDialogFragment.setArguments(args); return poiTypeDialogFragment; }