Update poi types

This commit is contained in:
Victor Shcherb 2015-04-08 00:55:20 +02:00
parent c6ad42ee79
commit f498454734
2 changed files with 115 additions and 42 deletions

View file

@ -14,6 +14,7 @@ import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.StringMatcher;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -120,6 +121,31 @@ public class MapPoiTypes {
return translation; return translation;
} }
public Map<String, AbstractPoiType> getAllTypesTranslatedNames(StringMatcher matcher) {
TreeMap<String, AbstractPoiType> tm = new TreeMap<String, AbstractPoiType>(Collator.getInstance());
Map<String, PoiType> translation = new TreeMap<String, PoiType>();
for(PoiCategory pc : categories) {
addIf(tm, pc, matcher);
for(PoiFilter pt : pc.getPoiFilters()) {
addIf(tm, pt, matcher);
}
for(PoiType pt : pc.getPoiTypes()) {
if(pt.isReference()) {
continue;
}
addIf(tm, pt, matcher);
}
}
return tm;
}
private void addIf(Map<String, AbstractPoiType> tm, AbstractPoiType pc, StringMatcher matcher) {
if(matcher.matches(pc.getTranslation()) || matcher.matches(pc.getKeyName().replace('_', ' '))) {
tm.put(pc.getTranslation(), pc);
}
}
public Map<String, PoiType> getAllTranslatedNames(PoiCategory pc, boolean onlyTranslation) { public Map<String, PoiType> getAllTranslatedNames(PoiCategory pc, boolean onlyTranslation) {
Map<String, PoiType> translation = new TreeMap<String, PoiType>(); Map<String, PoiType> translation = new TreeMap<String, PoiType>();
for (PoiType pt : pc.getPoiTypes()) { for (PoiType pt : pc.getPoiTypes()) {

View file

@ -5,9 +5,14 @@ package net.osmand.plus.activities.search;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import net.osmand.CollatorStringMatcher;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.osm.AbstractPoiType;
import net.osmand.osm.PoiType;
import net.osmand.plus.IconsCache; import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
@ -19,6 +24,7 @@ import net.osmand.plus.poi.PoiLegacyFilter;
import net.osmand.plus.poi.SearchByNameFilter; import net.osmand.plus.poi.SearchByNameFilter;
import net.osmand.plus.render.RenderingIcons; import net.osmand.plus.render.RenderingIcons;
import net.osmand.plus.resources.ResourceManager; import net.osmand.plus.resources.ResourceManager;
import net.osmand.util.Algorithms;
import android.content.Intent; import android.content.Intent;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
@ -85,6 +91,11 @@ public class SearchPoiFilterFragment extends ListFragment implements SearchActiv
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
if(currentTask != null) {
currentTask.cancel(true);
}
currentTask = new SearchPoiByNameTask();
currentTask.execute(s.toString());
} }
}); });
} }
@ -92,16 +103,30 @@ public class SearchPoiFilterFragment extends ListFragment implements SearchActiv
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
refreshPoiListAdapter(); poiFitlersAdapter = new PoiFiltersAdapter(getFilters(""));
setListAdapter(poiFitlersAdapter);
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }
public void refreshPoiListAdapter() { public List<Object> getFilters(String s) {
List<Object> filters = new ArrayList<Object>() ;
if (Algorithms.isEmpty(s)) {
PoiFiltersHelper poiFilters = getApp().getPoiFilters(); PoiFiltersHelper poiFilters = getApp().getPoiFilters();
List<PoiLegacyFilter> filters = new ArrayList<PoiLegacyFilter>() ;
filters.addAll(poiFilters.getTopDefinedPoiFilters()); filters.addAll(poiFilters.getTopDefinedPoiFilters());
poiFitlersAdapter = new PoiFiltersAdapter(filters); } else {
setListAdapter(poiFitlersAdapter); PoiFiltersHelper poiFilters = getApp().getPoiFilters();
for(PoiLegacyFilter pf : poiFilters.getTopDefinedPoiFilters()) {
if(!pf.isStandardFilter()) {
filters.add(pf);
}
}
Map<String, AbstractPoiType> res =
getApp().getPoiTypes().getAllTypesTranslatedNames(new CollatorStringMatcher(s, StringMatcherMode.CHECK_STARTS_FROM_SPACE));
for(AbstractPoiType p : res.values()) {
filters.add(p);
}
}
return filters;
} }
public OsmandApplication getApp(){ public OsmandApplication getApp(){
@ -137,76 +162,100 @@ public class SearchPoiFilterFragment extends ListFragment implements SearchActiv
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_POI_EDIT) { if(requestCode == REQUEST_POI_EDIT) {
refreshPoiListAdapter(); poiFitlersAdapter.setResult(getFilters(searchEditText == null ? "" : searchEditText.getText().toString()));
} }
} }
@Override @Override
public void onListItemClick(ListView listView, View v, int position, long id) { public void onListItemClick(ListView listView, View v, int position, long id) {
final PoiLegacyFilter filter = ((PoiFiltersAdapter) getListAdapter()).getItem(position); final Object item = ((PoiFiltersAdapter) getListAdapter()).getItem(position);
if(!(filter instanceof NameFinderPoiFilter)){
ResourceManager rm = getApp().getResourceManager(); ResourceManager rm = getApp().getResourceManager();
if(!rm.containsAmenityRepositoryToSearch(filter instanceof SearchByNameFilter)){ if (!rm.containsAmenityRepositoryToSearch(false)) {
AccessibleToast.makeText(getActivity(), R.string.data_to_search_poi_not_available, Toast.LENGTH_LONG); AccessibleToast.makeText(getActivity(), R.string.data_to_search_poi_not_available, Toast.LENGTH_LONG);
return; return;
} }
if (item instanceof PoiLegacyFilter) {
showFilterActivity(((PoiLegacyFilter) item).getFilterId());
} else {
showFilterActivity(PoiLegacyFilter.STD_PREFIX + ((AbstractPoiType) item).getKeyName());
} }
showFilterActivity(filter);
} }
private void showFilterActivity(final PoiLegacyFilter filter) { private void showFilterActivity(String filterId) {
final Intent newIntent = new Intent(getActivity(), SearchPOIActivity.class); final Intent newIntent = new Intent(getActivity(), SearchPOIActivity.class);
newIntent.putExtra(SearchPOIActivity.AMENITY_FILTER, filter.getFilterId()); newIntent.putExtra(SearchPOIActivity.AMENITY_FILTER, filterId);
updateIntentToLaunch(newIntent); updateIntentToLaunch(newIntent);
startActivityForResult(newIntent, 0); startActivityForResult(newIntent, 0);
} }
class SearchPoiByNameTask extends AsyncTask<Void, PoiLegacyFilter, List<PoiLegacyFilter>> { class SearchPoiByNameTask extends AsyncTask<String, Object, List<Object>> {
@Override @Override
protected List<PoiLegacyFilter> doInBackground(Void... params) { protected List<Object> doInBackground(String... params) {
return null; String filter = params[0];
return getFilters(filter);
} }
@Override @Override
protected void onPostExecute(List<PoiLegacyFilter> result) { protected void onPostExecute(List<Object> result) {
super.onPostExecute(result); if(!isCancelled() && isVisible()){
poiFitlersAdapter.setResult(result);
}
} }
} }
class PoiFiltersAdapter extends ArrayAdapter<PoiLegacyFilter> { class PoiFiltersAdapter extends ArrayAdapter<Object> {
PoiFiltersAdapter(List<Object> list) {
PoiFiltersAdapter(List<PoiLegacyFilter> list) {
super(getActivity(), R.layout.searchpoifolder_list, list); super(getActivity(), R.layout.searchpoifolder_list, list);
} }
public void setResult(List<Object> filters) {
setNotifyOnChange(false);
clear();
for(Object o : filters) {
add(o);
}
setNotifyOnChange(true);
notifyDataSetInvalidated();
}
@Override @Override
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView; View row = convertView;
if(row == null) { if (row == null) {
LayoutInflater inflater = getActivity().getLayoutInflater(); LayoutInflater inflater = getActivity().getLayoutInflater();
row = inflater.inflate(R.layout.searchpoifolder_list, parent, false); row = inflater.inflate(R.layout.searchpoifolder_list, parent, false);
} }
TextView label = (TextView) row.findViewById(R.id.folder_label); TextView label = (TextView) row.findViewById(R.id.folder_label);
ImageView icon = (ImageView) row.findViewById(R.id.folder_icon); ImageView icon = (ImageView) row.findViewById(R.id.folder_icon);
OsmandApplication app = getMyApplication(); Object item = getItem(position);
final PoiLegacyFilter model = getItem(position); String name;
label.setText(model.getName()); if (item instanceof PoiLegacyFilter) {
IconsCache iconsCache = app.getIconsCache(); final PoiLegacyFilter model = (PoiLegacyFilter) item;
if(model.getFilterId().equals(PoiLegacyFilter.CUSTOM_FILTER_ID)) { if (RenderingIcons.containsBigIcon(model.getSimplifiedId())) {
icon.setImageDrawable(iconsCache.getContentIcon(R.drawable.ic_action_filter_dark));
} else if (model.getFilterId().equals(PoiLegacyFilter.BY_NAME_FILTER_ID)) {
icon.setImageResource(android.R.drawable.ic_search_category_default);
} else {
if(RenderingIcons.containsBigIcon(model.getSimplifiedId())) {
icon.setImageDrawable(RenderingIcons.getBigIcon(getActivity(), model.getSimplifiedId())); icon.setImageDrawable(RenderingIcons.getBigIcon(getActivity(), model.getSimplifiedId()));
} else { } else {
icon.setImageResource(R.drawable.mx_user_defined); icon.setImageResource(R.drawable.mx_user_defined);
} }
name = model.getName();
} else {
AbstractPoiType st = (AbstractPoiType) item;
if (RenderingIcons.containsBigIcon(st.getKeyName())) {
icon.setImageDrawable(RenderingIcons.getBigIcon(getActivity(), st.getKeyName()));
} else if (st instanceof PoiType
&& RenderingIcons.containsBigIcon(((PoiType) st).getOsmTag() + "_"
+ ((PoiType) st).getOsmValue())) {
icon.setImageResource(RenderingIcons.getBigIconResourceId(((PoiType) st).getOsmTag() + "_"
+ ((PoiType) st).getOsmValue()));
} else {
icon.setImageDrawable(null);
} }
name = st.getTranslation();
}
label.setText(name);
return (row); return (row);
} }
} }
@ -221,11 +270,9 @@ public class SearchPoiFilterFragment extends ListFragment implements SearchActiv
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override @Override
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
PoiLegacyFilter filter = getApp().getPoiFilters().getFilterById(PoiLegacyFilter.CUSTOM_FILTER_ID); PoiLegacyFilter filter = getApp().getPoiFilters().getCustomPOIFilter();
if(filter != null) {
filter.clearFilter(); filter.clearFilter();
showEditActivity(filter); showEditActivity(filter);
}
return true; return true;
} }
}); });