Fix parts of edit poi

This commit is contained in:
Victor Shcherb 2015-09-30 12:19:47 +02:00
parent 9feb3e0a4d
commit 21daf32a34
7 changed files with 319 additions and 366 deletions

View file

@ -29,7 +29,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"> android:layout_weight="1">
<EditText <AutoCompleteTextView
android:id="@+id/valueEditText" android:id="@+id/valueEditText"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -1,5 +1,24 @@
package net.osmand.plus.osmedit; package net.osmand.plus.osmedit;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import net.osmand.PlatformUtil;
import net.osmand.StringMatcher;
import net.osmand.osm.AbstractPoiType;
import net.osmand.osm.MapPoiTypes;
import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiFilter;
import net.osmand.osm.PoiType;
import net.osmand.osm.edit.OSMSettings;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.osmedit.data.EditPoiData;
import org.apache.commons.logging.Log;
import android.app.Dialog; import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.graphics.Point; import android.graphics.Point;
@ -17,37 +36,16 @@ import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView; import android.widget.AutoCompleteTextView;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import net.osmand.PlatformUtil;
import net.osmand.StringMatcher;
import net.osmand.osm.AbstractPoiType;
import net.osmand.osm.MapPoiTypes;
import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiFilter;
import net.osmand.osm.PoiType;
import net.osmand.osm.edit.OSMSettings;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.osmedit.data.EditPoiData;
import net.osmand.plus.osmedit.data.Tag;
import org.apache.commons.logging.Log;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class AdvancedDataFragment extends Fragment { public class AdvancedDataFragment extends Fragment {
private static final String TAG = "AdvancedDataFragment"; private static final String TAG = "AdvancedDataFragment";
private static final Log LOG = PlatformUtil.getLog(AdvancedDataFragment.class); private static final Log LOG = PlatformUtil.getLog(AdvancedDataFragment.class);
private TagAdapterLinearLayoutHack mAdapter; private TagAdapterLinearLayoutHack mAdapter;
private EditPoiData.TagsChangedListener mTagsChangedListener; private EditPoiData.TagsChangedListener mTagsChangedListener;
private boolean mIsUserInput = true;
private Drawable deleteDrawable; private Drawable deleteDrawable;
@Override @Override
@ -83,7 +81,7 @@ public class AdvancedDataFragment extends Fragment {
addTagButton.setOnClickListener(new View.OnClickListener() { addTagButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
mAdapter.addTag(new Tag("", "")); mAdapter.addTagView("", "");
} }
}); });
return view; return view;
@ -92,14 +90,11 @@ public class AdvancedDataFragment extends Fragment {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
// TODO read more about lifecycle
mAdapter.updateViews(); mAdapter.updateViews();
mTagsChangedListener = new EditPoiData.TagsChangedListener() { mTagsChangedListener = new EditPoiData.TagsChangedListener() {
@Override @Override
public void onTagsChanged() { public void onTagsChanged() {
mIsUserInput = false;
mAdapter.updateViews(); mAdapter.updateViews();
mIsUserInput = true;
} }
}; };
getData().addListener(mTagsChangedListener); getData().addListener(mTagsChangedListener);
@ -156,20 +151,13 @@ public class AdvancedDataFragment extends Fragment {
this.mapPoiTypes = mapPoiTypes; this.mapPoiTypes = mapPoiTypes;
} }
public void addTag(Tag tag) {
editPoiData.tags.add(tag);
if (mIsUserInput)
editPoiData.notifyDatasetChanged(mTagsChangedListener);
updateViews();
}
public void updateViews() { public void updateViews() {
linearLayout.removeAllViews(); linearLayout.removeAllViews();
for (Tag tag : editPoiData.tags) { for (Entry<String, String> tag : editPoiData.getTagValues().entrySet()) {
if (tag.tag.equals(OSMSettings.OSMTagKey.NAME.getValue())) { if (tag.getKey().equals(OSMSettings.OSMTagKey.NAME.getValue())) {
nameTextView.setText(tag.value); nameTextView.setText(tag.getValue());
} else if (tag.tag.equals(EditPoiFragment.POI_TYPE_TAG)) { } else if (tag.getKey().equals(EditPoiData.POI_TYPE_TAG)) {
String subType = tag.value.trim().toLowerCase(); String subType = tag.getValue().trim().toLowerCase();
if (allTranslatedSubTypes.get(subType) != null) { if (allTranslatedSubTypes.get(subType) != null) {
PoiType pt = allTranslatedSubTypes.get(subType); PoiType pt = allTranslatedSubTypes.get(subType);
amenityTagTextView.setText(pt.getOsmTag()); amenityTagTextView.setText(pt.getOsmTag());
@ -179,30 +167,33 @@ public class AdvancedDataFragment extends Fragment {
amenityTextView.setText(subType); amenityTextView.setText(subType);
} }
} else { } else {
View view = getView(tag); addTagView(tag.getKey(), tag.getValue());
linearLayout.addView(view);
} }
} }
} }
private View getView(final Tag tag) { public void addTagView(String tg, String vl) {
View view = getView(tg, vl);
linearLayout.addView(view);
}
private View getView(String tg, String vl) {
final View convertView = LayoutInflater.from(linearLayout.getContext()) final View convertView = LayoutInflater.from(linearLayout.getContext())
.inflate(R.layout.poi_tag_list_item, null, false); .inflate(R.layout.poi_tag_list_item, null, false);
final AutoCompleteTextView tagEditText = final AutoCompleteTextView tagEditText =
(AutoCompleteTextView) convertView.findViewById(R.id.tagEditText); (AutoCompleteTextView) convertView.findViewById(R.id.tagEditText);
tagEditText.setText(tag.tag); tagEditText.setText(tg);
final EditText valueEditText = (EditText) convertView.findViewById(R.id.valueEditText); final AutoCompleteTextView valueEditText =
(AutoCompleteTextView) convertView.findViewById(R.id.valueEditText);
ImageButton deleteItemImageButton = ImageButton deleteItemImageButton =
(ImageButton) convertView.findViewById(R.id.deleteItemImageButton); (ImageButton) convertView.findViewById(R.id.deleteItemImageButton);
valueEditText.setText(tag.value); valueEditText.setText(vl);
deleteItemImageButton.setImageDrawable(deleteDrawable); deleteItemImageButton.setImageDrawable(deleteDrawable);
deleteItemImageButton.setOnClickListener(new View.OnClickListener() { deleteItemImageButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
linearLayout.removeView((View) v.getParent()); linearLayout.removeView((View) v.getParent());
editPoiData.tags.remove(tag); editPoiData.removeTag(tagEditText.toString());
if (mIsUserInput)
editPoiData.notifyDatasetChanged(null);
} }
}); });
tagEditText.addTextChangedListener(new TextWatcher() { tagEditText.addTextChangedListener(new TextWatcher() {
@ -216,29 +207,26 @@ public class AdvancedDataFragment extends Fragment {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
editPoiData.tags.remove(tag); if (!editPoiData.isInEdit()) {
tag.tag = tagEditText.getText().toString(); editPoiData.putTag(s.toString(), valueEditText.getText().toString());
editPoiData.tags.add(tag); }
if (mIsUserInput)
editPoiData.notifyDatasetChanged(mTagsChangedListener);
} }
}); });
final Set<String> tagKeys = new HashSet<>(); final Set<String> tagKeys = new HashSet<>();
final Set<String> valueKeys = new HashSet<>();
for (String s : allTypes.keySet()) { for (String s : allTypes.keySet()) {
AbstractPoiType abstractPoiType = allTypes.get(s); AbstractPoiType abstractPoiType = allTypes.get(s);
addPoiToStringSet(abstractPoiType, tagKeys); addPoiToStringSet(abstractPoiType, tagKeys, valueKeys);
} }
// addPoiToStringSet(mapPoiTypes.getOtherPoiCategory(), tagKeys); addPoiToStringSet(mapPoiTypes.getOtherMapCategory(), tagKeys, valueKeys);
addPoiToStringSet(mapPoiTypes.getOtherMapCategory(), tagKeys);
ArrayAdapter<Object> adapter = new ArrayAdapter<>(linearLayout.getContext(), ArrayAdapter<Object> tagAdapter = new ArrayAdapter<>(linearLayout.getContext(),
R.layout.list_textview, tagKeys.toArray()); R.layout.list_textview, tagKeys.toArray());
tagEditText.setAdapter(adapter); tagEditText.setAdapter(tagAdapter);
tagEditText.setThreshold(1); tagEditText.setThreshold(1);
tagEditText.setOnClickListener(new View.OnClickListener() { tagEditText.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
// TODO: 8/29/15 Rewrite as dialog fragment
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final String[] tags = tagKeys.toArray(new String[tagKeys.size()]); final String[] tags = tagKeys.toArray(new String[tagKeys.size()]);
builder.setItems(tags, new Dialog.OnClickListener() { builder.setItems(tags, new Dialog.OnClickListener() {
@ -252,6 +240,7 @@ public class AdvancedDataFragment extends Fragment {
builder.show(); builder.show();
} }
}); });
valueEditText.addTextChangedListener(new TextWatcher() { valueEditText.addTextChangedListener(new TextWatcher() {
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@ -263,18 +252,38 @@ public class AdvancedDataFragment extends Fragment {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
editPoiData.tags.remove(tag); if (!editPoiData.isInEdit()) {
tag.value = valueEditText.getText().toString(); editPoiData.putTag(tagEditText.getText().toString(), s.toString());
editPoiData.tags.add(tag); }
if (mIsUserInput)
editPoiData.notifyDatasetChanged(mTagsChangedListener);
} }
}); });
ArrayAdapter<Object> valueAdapter = new ArrayAdapter<>(linearLayout.getContext(),
R.layout.list_textview, valueKeys.toArray());
valueEditText.setAdapter(valueAdapter);
valueEditText.setThreshold(1);
valueEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final String[] values = valueKeys.toArray(new String[tagKeys.size()]);
builder.setItems(values, new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
valueEditText.setText(values[which]);
}
});
builder.create();
builder.show();
}
});
return convertView; return convertView;
} }
} }
private static void addPoiToStringSet(AbstractPoiType abstractPoiType, Set<String> stringSet) { private static void addPoiToStringSet(AbstractPoiType abstractPoiType, Set<String> stringSet,
Set<String> values) {
if (abstractPoiType instanceof PoiType) { if (abstractPoiType instanceof PoiType) {
PoiType poiType = (PoiType) abstractPoiType; PoiType poiType = (PoiType) abstractPoiType;
if (poiType.getOsmTag() != null && if (poiType.getOsmTag() != null &&
@ -283,22 +292,29 @@ public class AdvancedDataFragment extends Fragment {
if (poiType.getOsmTag2() != null) { if (poiType.getOsmTag2() != null) {
stringSet.add(poiType.getOsmTag2()); stringSet.add(poiType.getOsmTag2());
} }
}
if (poiType.getOsmValue() != null) {
values.add(poiType.getOsmValue());
}
if (poiType.getOsmValue2() != null) {
values.add(poiType.getOsmValue2());
} }
} else if (abstractPoiType instanceof PoiCategory) { } else if (abstractPoiType instanceof PoiCategory) {
PoiCategory poiCategory = (PoiCategory) abstractPoiType; PoiCategory poiCategory = (PoiCategory) abstractPoiType;
for (PoiFilter filter : poiCategory.getPoiFilters()) { for (PoiFilter filter : poiCategory.getPoiFilters()) {
addPoiToStringSet(filter, stringSet); addPoiToStringSet(filter, stringSet, values);
} }
for (PoiType poiType : poiCategory.getPoiTypes()) { for (PoiType poiType : poiCategory.getPoiTypes()) {
addPoiToStringSet(poiType, stringSet); addPoiToStringSet(poiType, stringSet, values);
} }
for (PoiType poiType : poiCategory.getPoiAdditionals()) { for (PoiType poiType : poiCategory.getPoiAdditionals()) {
addPoiToStringSet(poiType, stringSet); addPoiToStringSet(poiType, stringSet, values);
} }
} else if (abstractPoiType instanceof PoiFilter) { } else if (abstractPoiType instanceof PoiFilter) {
PoiFilter poiFilter = (PoiFilter) abstractPoiType; PoiFilter poiFilter = (PoiFilter) abstractPoiType;
for (PoiType poiType : poiFilter.getPoiTypes()) { for (PoiType poiType : poiFilter.getPoiTypes()) {
addPoiToStringSet(poiType, stringSet); addPoiToStringSet(poiType, stringSet, values);
} }
} else { } else {
throw new IllegalArgumentException("abstractPoiType can't be instance of class " throw new IllegalArgumentException("abstractPoiType can't be instance of class "

View file

@ -1,5 +1,23 @@
package net.osmand.plus.osmedit; package net.osmand.plus.osmedit;
import java.util.Arrays;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import net.osmand.PlatformUtil;
import net.osmand.osm.edit.OSMSettings;
import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.osmedit.data.EditPoiData;
import net.osmand.plus.osmedit.dialogs.OpeningHoursDaysDialogFragment;
import net.osmand.plus.osmedit.dialogs.OpeningHoursHoursDialogFragment;
import net.osmand.util.OpeningHoursParser;
import net.osmand.util.OpeningHoursParser.BasicOpeningHourRule;
import org.apache.commons.logging.Log;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Point; import android.graphics.Point;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
@ -7,7 +25,6 @@ import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.Display; import android.view.Display;
@ -21,25 +38,6 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import net.osmand.PlatformUtil;
import net.osmand.osm.edit.OSMSettings;
import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.osmedit.data.EditPoiData;
import net.osmand.plus.osmedit.data.Tag;
import net.osmand.plus.osmedit.dialogs.OpeningHoursDaysDialogFragment;
import net.osmand.plus.osmedit.dialogs.OpeningHoursHoursDialogFragment;
import net.osmand.util.OpeningHoursParser;
import net.osmand.util.OpeningHoursParser.BasicOpeningHourRule;
import org.apache.commons.logging.Log;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
public class BasicDataFragment extends Fragment { public class BasicDataFragment extends Fragment {
private static final String TAG = "BasicDataFragment"; private static final String TAG = "BasicDataFragment";
private static final Log LOG = PlatformUtil.getLog(BasicDataFragment.class); private static final Log LOG = PlatformUtil.getLog(BasicDataFragment.class);
@ -49,8 +47,7 @@ public class BasicDataFragment extends Fragment {
private EditText phoneEditText; private EditText phoneEditText;
private EditText webSiteEditText; private EditText webSiteEditText;
private EditText descriptionEditText; private EditText descriptionEditText;
private EditPoiData.TagsChangedListener mTagsChangedListener; private List<EditPoiData.TagsChangedListener> listeners = new LinkedList<EditPoiData.TagsChangedListener>();
private boolean mIsUserInput = true;
OpeningHoursAdapter mOpeningHoursAdapter; OpeningHoursAdapter mOpeningHoursAdapter;
@Nullable @Nullable
@ -91,26 +88,20 @@ public class BasicDataFragment extends Fragment {
iconsCache.getPaintedContentIcon(R.drawable.ic_action_time, iconColor)); iconsCache.getPaintedContentIcon(R.drawable.ic_action_time, iconColor));
streetEditText = (EditText) view.findViewById(R.id.streetEditText); streetEditText = (EditText) view.findViewById(R.id.streetEditText);
streetEditText.addTextChangedListener(new MyOnFocusChangeListener(getData(),
OSMSettings.OSMTagKey.ADDR_STREET.getValue()));
houseNumberEditText = (EditText) view.findViewById(R.id.houseNumberEditText); houseNumberEditText = (EditText) view.findViewById(R.id.houseNumberEditText);
houseNumberEditText.addTextChangedListener(new MyOnFocusChangeListener(getData(),
OSMSettings.OSMTagKey.ADDR_HOUSE_NUMBER.getValue()));
phoneEditText = (EditText) view.findViewById(R.id.phoneEditText); phoneEditText = (EditText) view.findViewById(R.id.phoneEditText);
phoneEditText.addTextChangedListener(new MyOnFocusChangeListener(getData(),
OSMSettings.OSMTagKey.PHONE.getValue()));
webSiteEditText = (EditText) view.findViewById(R.id.webSiteEditText); webSiteEditText = (EditText) view.findViewById(R.id.webSiteEditText);
webSiteEditText.addTextChangedListener(new MyOnFocusChangeListener(getData(),
OSMSettings.OSMTagKey.WEBSITE.getValue()));
descriptionEditText = (EditText) view.findViewById(R.id.descriptionEditText); descriptionEditText = (EditText) view.findViewById(R.id.descriptionEditText);
descriptionEditText.addTextChangedListener(new MyOnFocusChangeListener(getData(), addTextWatcher(OSMSettings.OSMTagKey.ADDR_STREET.getValue(), streetEditText);
OSMSettings.OSMTagKey.DESCRIPTION.getValue())); addTextWatcher(OSMSettings.OSMTagKey.WEBSITE.getValue(), webSiteEditText);
addTextWatcher(OSMSettings.OSMTagKey.PHONE.getValue(), phoneEditText);
addTextWatcher(OSMSettings.OSMTagKey.ADDR_HOUSE_NUMBER.getValue(), houseNumberEditText);
addTextWatcher(OSMSettings.OSMTagKey.DESCRIPTION.getValue(), descriptionEditText);
Button addOpeningHoursButton = (Button) view.findViewById(R.id.addOpeningHoursButton); Button addOpeningHoursButton = (Button) view.findViewById(R.id.addOpeningHoursButton);
addOpeningHoursButton.setOnClickListener(new View.OnClickListener() { addOpeningHoursButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
BasicOpeningHourRule rule = new BasicOpeningHourRule(); BasicOpeningHourRule rule = new BasicOpeningHourRule();
// TODO: 8/27/15 Figure out some better defauls or leave it as it is
rule.setStartTime(9 * 60); rule.setStartTime(9 * 60);
rule.setEndTime(18 * 60); rule.setEndTime(18 * 60);
OpeningHoursDaysDialogFragment fragment = OpeningHoursDaysDialogFragment.createInstance(rule, -1); OpeningHoursDaysDialogFragment fragment = OpeningHoursDaysDialogFragment.createInstance(rule, -1);
@ -141,39 +132,57 @@ public class BasicDataFragment extends Fragment {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
mTagsChangedListener = new EditPoiData.TagsChangedListener() { for(EditPoiData.TagsChangedListener tl : listeners) {
@Override getData().addListener(tl);
public void onTagsChanged() { }
TagMapProcessor tagMapProcessor = new TagMapProcessor(); getData().notifyToUpdateUI();
tagMapProcessor.addFilter(OSMSettings.OSMTagKey.ADDR_STREET.getValue(),
new EditTextTagFilter(streetEditText)); // FIXME opening hours
tagMapProcessor.addFilter(OSMSettings.OSMTagKey.ADDR_HOUSE_NUMBER.getValue(), // @Override
new EditTextTagFilter(houseNumberEditText)); // public void process( String openingHoursString ) {
tagMapProcessor.addFilter(OSMSettings.OSMTagKey.PHONE.getValue(), // parseOpenedHoursHandleErrors(openingHoursString);
new EditTextTagFilter(phoneEditText)); // if (openingHours == null) {
tagMapProcessor.addFilter(OSMSettings.OSMTagKey.WEBSITE.getValue(), // openingHours = new OpeningHoursParser.OpeningHours();
new EditTextTagFilter(webSiteEditText)); // }
tagMapProcessor.addFilter(OSMSettings.OSMTagKey.DESCRIPTION.getValue(), // LOG.debug("openingHours=" + openingHours);
new EditTextTagFilter(descriptionEditText)); // adapter.replaceOpeningHours(openingHours);
tagMapProcessor.addFilter(OSMSettings.OSMTagKey.OPENING_HOURS.getValue(), // adapter.updateViews();
new OpenHoursTagFilter(mOpeningHoursAdapter)); // }
//
// @Override
// public void onUntriggered() {
// adapter.replaceOpeningHours(new OpeningHoursParser.OpeningHours());
// adapter.updateViews();
// }
}
mIsUserInput = false; protected void addTextWatcher(final String tag, EditText e) {
for (Tag tag : getData().tags) { e.addTextChangedListener(new TextWatcher() {
tagMapProcessor.process(tag);
} @Override
tagMapProcessor.clearEmptyFields(); public void beforeTextChanged(CharSequence s, int start, int count, int after) {
mIsUserInput = true;
} }
};
mTagsChangedListener.onTagsChanged(); @Override
getData().addListener(mTagsChangedListener); public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (!getData().isInEdit()) {
getData().putTag(tag, s.toString());
}
}
});
} }
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
getData().deleteListener(mTagsChangedListener); for(EditPoiData.TagsChangedListener tl : listeners) {
getData().deleteListener(tl);
}
} }
@Override @Override
@ -187,80 +196,10 @@ public class BasicDataFragment extends Fragment {
mOpeningHoursAdapter.setOpeningHoursRule(item, position); mOpeningHoursAdapter.setOpeningHoursRule(item, position);
} }
private static class TagMapProcessor {
private final Map<String, TagFilter> mFilters = new HashMap<>();
public void addFilter(String tag, TagFilter filter) {
mFilters.put(tag, filter);
}
public void process(Tag tag) {
if (mFilters.containsKey(tag.tag)) {
final TagFilter filter = mFilters.get(tag.tag);
filter.process(tag);
mFilters.remove(tag.tag);
}
}
public void clearEmptyFields() {
for (String tag : mFilters.keySet()) {
mFilters.get(tag).onUntriggered();
}
}
}
private interface TagFilter {
void process(Tag tag);
void onUntriggered();
}
private static class EditTextTagFilter implements TagFilter {
private final EditText editText;
private EditTextTagFilter(EditText editText) {
this.editText = editText;
}
@Override
public void process(Tag tag) {
editText.setText(tag.value);
}
@Override
public void onUntriggered() {
editText.setText(null);
}
}
private static class OpenHoursTagFilter implements TagFilter {
private final OpeningHoursAdapter adapter;
private OpenHoursTagFilter(OpeningHoursAdapter adapter) {
this.adapter = adapter;
}
@Override
public void process(Tag tag) {
String openingHoursString = tag.value;
LOG.debug("openingHoursString=" + openingHoursString);
OpeningHoursParser.OpeningHours openingHours =
parseOpenedHoursHandleErrors(openingHoursString);
if (openingHours == null) {
openingHours = new OpeningHoursParser.OpeningHours();
// TODO show error message
}
LOG.debug("openingHours=" + openingHours);
adapter.replaceOpeningHours(openingHours);
adapter.updateViews();
}
@Override
public void onUntriggered() {
adapter.replaceOpeningHours(new OpeningHoursParser.OpeningHours());
adapter.updateViews();
}
}
private EditPoiFragment getEditPoiFragment() { private EditPoiFragment getEditPoiFragment() {
return (EditPoiFragment) getParentFragment(); return (EditPoiFragment) getParentFragment();
@ -480,37 +419,7 @@ public class BasicDataFragment extends Fragment {
return basic; return basic;
} }
private class MyOnFocusChangeListener implements TextWatcher {
private final EditPoiData data;
private final String tagName;
public MyOnFocusChangeListener(EditPoiData data,
String tagName) {
this.data = data;
this.tagName = tagName;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (mIsUserInput) {
String string = s.toString();
if (!TextUtils.isEmpty(string)) {
Tag tag = new Tag(tagName, string);
data.tags.remove(tag);
data.tags.add(tag);
data.notifyDatasetChanged(mTagsChangedListener);
}
}
}
}
private class OpeningHoursAdapter { private class OpeningHoursAdapter {
private OpeningHoursParser.OpeningHours openingHours; private OpeningHoursParser.OpeningHours openingHours;
@ -547,12 +456,9 @@ public class BasicDataFragment extends Fragment {
for (int i = 0; i < openingHours.getRules().size(); i++) { for (int i = 0; i < openingHours.getRules().size(); i++) {
linearLayout.addView(getView(i)); linearLayout.addView(getView(i));
} }
if (mIsUserInput) { if (!data.isInEdit()) {
Tag openHours = new Tag(OSMSettings.OSMTagKey.OPENING_HOURS.getValue(), data.putTag(OSMSettings.OSMTagKey.OPENING_HOURS.getValue(),
openingHours.toStringNoMonths()); openingHours.toStringNoMonths());
data.tags.remove(openHours);
data.tags.add(openHours);
data.notifyDatasetChanged(null);
} }
} }

View file

@ -1,5 +1,30 @@
package net.osmand.plus.osmedit; package net.osmand.plus.osmedit;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.LinkedHashMap;
import java.util.Map;
import net.osmand.PlatformUtil;
import net.osmand.access.AccessibleToast;
import net.osmand.data.Amenity;
import net.osmand.osm.PoiType;
import net.osmand.osm.edit.EntityInfo;
import net.osmand.osm.edit.Node;
import net.osmand.osm.edit.OSMSettings;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.osmedit.data.EditPoiData;
import net.osmand.plus.osmedit.dialogs.DeletePoiDialogFragment;
import net.osmand.plus.osmedit.dialogs.PoiSubTypeDialogFragment;
import net.osmand.plus.osmedit.dialogs.PoiTypeDialogFragment;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.app.Dialog; import android.app.Dialog;
@ -41,32 +66,6 @@ import android.widget.ImageButton;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import net.osmand.PlatformUtil;
import net.osmand.access.AccessibleToast;
import net.osmand.data.Amenity;
import net.osmand.osm.PoiType;
import net.osmand.osm.edit.EntityInfo;
import net.osmand.osm.edit.Node;
import net.osmand.osm.edit.OSMSettings;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.osmedit.data.EditPoiData;
import net.osmand.plus.osmedit.data.Tag;
import net.osmand.plus.osmedit.dialogs.DeletePoiDialogFragment;
import net.osmand.plus.osmedit.dialogs.PoiSubTypeDialogFragment;
import net.osmand.plus.osmedit.dialogs.PoiTypeDialogFragment;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import java.text.MessageFormat;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
public class EditPoiFragment extends DialogFragment { public class EditPoiFragment extends DialogFragment {
public static final String TAG = "EditPoiFragment"; public static final String TAG = "EditPoiFragment";
private static final Log LOG = PlatformUtil.getLog(EditPoiFragment.class); private static final Log LOG = PlatformUtil.getLog(EditPoiFragment.class);
@ -75,7 +74,7 @@ public class EditPoiFragment extends DialogFragment {
private static final String KEY_AMENITY = "key_amenity"; private static final String KEY_AMENITY = "key_amenity";
private static final String TAGS_LIST = "tags_list"; private static final String TAGS_LIST = "tags_list";
private final EditPoiData editPoiData = new EditPoiData(); private EditPoiData editPoiData;
private ViewPager viewPager; private ViewPager viewPager;
private boolean isLocalEdit; private boolean isLocalEdit;
private boolean mIsUserInput = true; private boolean mIsUserInput = true;
@ -83,7 +82,7 @@ public class EditPoiFragment extends DialogFragment {
private AutoCompleteTextView poiTypeEditText; private AutoCompleteTextView poiTypeEditText;
private Node node; private Node node;
private Map<String, PoiType> allTranslatedSubTypes; private Map<String, PoiType> allTranslatedSubTypes;
public static final String POI_TYPE_TAG = "poi_type_tag";
private OpenstreetmapUtil mOpenstreetmapUtil; private OpenstreetmapUtil mOpenstreetmapUtil;
private TextInputLayout poiTypeTextInputLayout; private TextInputLayout poiTypeTextInputLayout;
@ -102,9 +101,10 @@ public class EditPoiFragment extends DialogFragment {
} }
node = (Node) getArguments().getSerializable(KEY_AMENITY_NODE); node = (Node) getArguments().getSerializable(KEY_AMENITY_NODE);
allTranslatedSubTypes = getMyApplication().getPoiTypes() allTranslatedSubTypes = getMyApplication().getPoiTypes().getAllTranslatedNames();
.getAllTranslatedNames();
editPoiData.amenity = (Amenity) getArguments().getSerializable(KEY_AMENITY); Amenity amenity = (Amenity) getArguments().getSerializable(KEY_AMENITY);
editPoiData = new EditPoiData(amenity, node, allTranslatedSubTypes);
} }
@Override @Override
@ -127,36 +127,8 @@ public class EditPoiFragment extends DialogFragment {
boolean isLightTheme = settings.OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME; boolean isLightTheme = settings.OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME;
if (savedInstanceState != null) { if (savedInstanceState != null) {
editPoiData.tags = (LinkedHashSet<Tag>) savedInstanceState.getSerializable(TAGS_LIST); Map<String, String> mp =(Map<String, String>) savedInstanceState.getSerializable(TAGS_LIST);
} else { editPoiData.updateTags(mp);
editPoiData.tags = new LinkedHashSet<>();
tryAddTag(OSMSettings.OSMTagKey.ADDR_STREET.getValue(),
node.getTag(OSMSettings.OSMTagKey.ADDR_STREET));
tryAddTag(OSMSettings.OSMTagKey.ADDR_HOUSE_NUMBER.getValue(),
node.getTag(OSMSettings.OSMTagKey.ADDR_HOUSE_NUMBER));
tryAddTag(OSMSettings.OSMTagKey.PHONE.getValue(),
editPoiData.amenity.getPhone());
tryAddTag(OSMSettings.OSMTagKey.WEBSITE.getValue(),
editPoiData.amenity.getSite());
for (String tag : node.getTagKeySet()) {
tryAddTag(tag, node.getTag(tag));
}
String subType = editPoiData.amenity.getSubType();
String key;
String value;
if (allTranslatedSubTypes.get(subType) != null) {
PoiType pt = allTranslatedSubTypes.get(subType);
key = pt.getOsmTag();
value = pt.getOsmValue();
} else {
key = editPoiData.amenity.getType().getDefaultTag();
value = subType;
}
final Tag tag = new Tag(key, value);
editPoiData.tags.remove(tag);
tag.tag = POI_TYPE_TAG;
editPoiData.tags.add(tag);
} }
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar); Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
@ -230,11 +202,8 @@ public class EditPoiFragment extends DialogFragment {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
final Tag tag = new Tag(OSMSettings.OSMTagKey.NAME.getValue(), s.toString()); if (!getEditPoiData().isInEdit()) {
if (mIsUserInput) { getEditPoiData().putTag(OSMSettings.OSMTagKey.NAME.getValue(), s.toString());
getEditPoiData().tags.remove(tag);
getEditPoiData().tags.add(tag);
getEditPoiData().notifyDatasetChanged(mTagsChangedListener);
} }
} }
}); });
@ -252,11 +221,8 @@ public class EditPoiFragment extends DialogFragment {
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
final Tag tag = new Tag(POI_TYPE_TAG, s.toString()); if (!getEditPoiData().isInEdit()) {
if (mIsUserInput) { getEditPoiData().putTag(EditPoiData.POI_TYPE_TAG, s.toString());
getEditPoiData().tags.remove(tag);
getEditPoiData().tags.add(tag);
getEditPoiData().notifyDatasetChanged(mTagsChangedListener);
} }
} }
}); });
@ -294,24 +260,24 @@ public class EditPoiFragment extends DialogFragment {
return; return;
} }
OsmPoint.Action action = node.getId() == -1 ? OsmPoint.Action.CREATE : OsmPoint.Action.MODIFY; OsmPoint.Action action = node.getId() == -1 ? OsmPoint.Action.CREATE : OsmPoint.Action.MODIFY;
for (Tag tag : editPoiData.tags) { for (Map.Entry<String, String> tag : editPoiData.getTagValues().entrySet()) {
if (tag.tag.equals(POI_TYPE_TAG)) { if (tag.getKey().equals(EditPoiData.POI_TYPE_TAG)) {
final PoiType poiType = allTranslatedSubTypes.get(tag.value.trim().toLowerCase()); final PoiType poiType = allTranslatedSubTypes.get(tag.getValue().trim().toLowerCase());
if (poiType != null) { if (poiType != null) {
node.putTag(poiType.getOsmTag(), poiType.getOsmValue()); node.putTag(poiType.getOsmTag(), poiType.getOsmValue());
if (poiType.getOsmTag2() != null) { if (poiType.getOsmTag2() != null) {
node.putTag(poiType.getOsmTag2(), poiType.getOsmValue2()); node.putTag(poiType.getOsmTag2(), poiType.getOsmValue2());
} }
} else { } else {
node.putTag(editPoiData.amenity.getType().getDefaultTag(), tag.value); node.putTag(editPoiData.amenity.getType().getDefaultTag(), tag.getValue());
} }
// } else if (tag.tag.equals(OSMSettings.OSMTagKey.DESCRIPTION.getValue())) { // } else if (tag.tag.equals(OSMSettings.OSMTagKey.DESCRIPTION.getValue())) {
// description = tag.value; // description = tag.value;
} else { } else {
if (tag.value.length() > 0) { if (tag.getKey().length() > 0) {
node.putTag(tag.tag, tag.value); node.putTag(tag.getKey(), tag.getValue());
} else { } else {
node.removeTag(tag.tag); node.removeTag(tag.getKey());
} }
} }
} }
@ -369,15 +335,10 @@ public class EditPoiFragment extends DialogFragment {
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
outState.putSerializable(TAGS_LIST, editPoiData.tags); outState.putSerializable(TAGS_LIST, (Serializable) editPoiData.getTagValues());
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
private void tryAddTag(String key, String value) {
if (!Algorithms.isEmpty(value)) {
editPoiData.tags.add(new Tag(key, value));
}
}
public static EditPoiFragment createAddPoiInstance(double latitude, double longitude, public static EditPoiFragment createAddPoiInstance(double latitude, double longitude,
OsmandApplication application) { OsmandApplication application) {
@ -407,7 +368,6 @@ public class EditPoiFragment extends DialogFragment {
poiTypeEditText.setText(subCategory); poiTypeEditText.setText(subCategory);
} }
// TODO: 8/28/15 Move to some king of helper class
public static void commitNode(final OsmPoint.Action action, public static void commitNode(final OsmPoint.Action action,
final Node n, final Node n,
final EntityInfo info, final EntityInfo info,

View file

@ -1,19 +1,120 @@
package net.osmand.plus.osmedit.data; package net.osmand.plus.osmedit.data;
import net.osmand.data.Amenity; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set; import java.util.Set;
import net.osmand.data.Amenity;
import net.osmand.osm.PoiType;
import net.osmand.osm.edit.Node;
import net.osmand.osm.edit.OSMSettings;
import net.osmand.util.Algorithms;
public class EditPoiData { public class EditPoiData {
private Set<TagsChangedListener> mListeners = new HashSet<>(); private Set<TagsChangedListener> mListeners = new HashSet<>();
public LinkedHashSet<Tag> tags; private LinkedHashMap<String, String > tagValues = new LinkedHashMap<String, String>();
public Amenity amenity; private boolean isInEdit = false;
public final Amenity amenity;
public static final String POI_TYPE_TAG = "poi_type_tag";
public EditPoiData(Amenity amenity, Node node, Map<String, PoiType> allTranslatedSubTypes) {
this.amenity = amenity;
initTags(node, allTranslatedSubTypes);
}
public void notifyDatasetChanged(TagsChangedListener listenerToSkip) {
public void updateTags(Map<String, String> mp) {
this.tagValues.clear();
this.tagValues.putAll(mp);
}
private void tryAddTag(String key, String value) {
if (!Algorithms.isEmpty(value)) {
tagValues.put(key, value);
}
}
private void initTags(Node node, Map<String, PoiType> allTranslatedSubTypes) {
tryAddTag(OSMSettings.OSMTagKey.ADDR_STREET.getValue(),
node.getTag(OSMSettings.OSMTagKey.ADDR_STREET));
tryAddTag(OSMSettings.OSMTagKey.ADDR_HOUSE_NUMBER.getValue(),
node.getTag(OSMSettings.OSMTagKey.ADDR_HOUSE_NUMBER));
tryAddTag(OSMSettings.OSMTagKey.PHONE.getValue(),
amenity.getPhone());
tryAddTag(OSMSettings.OSMTagKey.WEBSITE.getValue(),
amenity.getSite());
for (String tag : node.getTagKeySet()) {
tryAddTag(tag, node.getTag(tag));
}
String subType = amenity.getSubType();
String key;
String value;
if (allTranslatedSubTypes.get(subType) != null) {
PoiType pt = allTranslatedSubTypes.get(subType);
key = pt.getOsmTag();
value = pt.getOsmValue();
} else {
key = amenity.getType().getDefaultTag();
value = subType;
}
tagValues.remove(key);
tagValues.put(POI_TYPE_TAG, value);
}
public Map<String, String> getTagValues() {
return Collections.unmodifiableMap(tagValues);
}
public void putTag(String tag, String value) {
if(isInEdit) {
throw new IllegalStateException("Can't modify in edit mode");
}
try {
isInEdit = true;
tagValues.put(tag, value);
notifyDatasetChanged();
} finally {
isInEdit = false;
}
}
public void notifyToUpdateUI() {
if(isInEdit) {
throw new IllegalStateException("Can't modify in edit mode");
}
try {
isInEdit = true;
notifyDatasetChanged();
} finally {
isInEdit = false;
}
}
public void removeTag(String tag) {
if(isInEdit) {
throw new IllegalStateException("Can't modify in edit mode");
}
try {
isInEdit = true;
tagValues.remove(tag);
notifyDatasetChanged();
} finally {
isInEdit = false;
}
}
public boolean isInEdit() {
return isInEdit;
}
private void notifyDatasetChanged() {
for (TagsChangedListener listener : mListeners) { for (TagsChangedListener listener : mListeners) {
if (listener != listenerToSkip) listener.onTagsChanged(); listener.onTagsChanged();
} }
} }
@ -26,6 +127,12 @@ public class EditPoiData {
} }
public interface TagsChangedListener { public interface TagsChangedListener {
void onTagsChanged(); void onTagsChanged();
} }
} }

View file

@ -1,39 +0,0 @@
package net.osmand.plus.osmedit.data;
import net.osmand.PlatformUtil;
import org.apache.commons.logging.Log;
import java.io.Serializable;
public class Tag implements Serializable {
private static final Log LOG = PlatformUtil.getLog(Tag.class);
public String tag;
public String value;
public Tag(String tag, String value) {
this.tag = tag;
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Tag tag1 = (Tag) o;
return tag.equals(tag1.tag);
}
@Override
public int hashCode() {
return tag.hashCode();
}
@Override
public String toString() {
return "Tag{" +
"tag='" + tag + '\'' +
", value='" + value + '\'' +
'}';
}
}

View file

@ -831,6 +831,9 @@ public class VoiceRouter {
} }
private void makeSound(){ private void makeSound(){
if(isMute()) {
return;
}
SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0); SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
int soundClick = -1; int soundClick = -1;
boolean success = true; boolean success = true;