use of net.osmand.data.Amenity in project Osmand by osmandapp.
the class NominatimPoiFilter method searchAmenitiesInternal.
@Override
protected List<Amenity> searchAmenitiesInternal(double lat, double lon, double topLatitude, double bottomLatitude, double leftLongitude, double rightLongitude, int zoom, ResultMatcher<Amenity> matcher) {
final int deviceApiVersion = android.os.Build.VERSION.SDK_INT;
String NOMINATIM_API;
if (deviceApiVersion >= android.os.Build.VERSION_CODES.GINGERBREAD) {
NOMINATIM_API = "https://nominatim.openstreetmap.org/search/";
} else {
NOMINATIM_API = "http://nominatim.openstreetmap.org/search/";
}
currentSearchResult = new ArrayList<Amenity>();
if (Algorithms.isEmpty(getFilterByName())) {
return currentSearchResult;
}
String viewbox = "viewboxlbrt=" + ((float) leftLongitude) + "," + ((float) bottomLatitude) + "," + ((float) rightLongitude) + "," + ((float) topLatitude);
try {
lastError = "";
String urlq;
if (addressQuery) {
urlq = NOMINATIM_API + "?format=xml&addressdetails=0&accept-language=" + Locale.getDefault().getLanguage() + "&q=" + URLEncoder.encode(getFilterByName());
} else {
urlq = NOMINATIM_API + "?format=xml&addressdetails=1&limit=" + LIMIT + "&bounded=1&" + viewbox + "&q=" + URLEncoder.encode(getFilterByName());
}
log.info(urlq);
// $NON-NLS-1$
URLConnection connection = NetworkUtils.getHttpURLConnection(urlq);
InputStream stream = connection.getInputStream();
XmlPullParser parser = PlatformUtil.newXMLPullParser();
// $NON-NLS-1$
parser.setInput(stream, "UTF-8");
int eventType;
int namedDepth = 0;
Amenity a = null;
MapPoiTypes poiTypes = ((OsmandApplication) getApplication()).getPoiTypes();
while ((eventType = parser.next()) != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("searchresults")) {
// $NON-NLS-1$
// $NON-NLS-1$ //$NON-NLS-2$
String err = parser.getAttributeValue("", "error");
if (err != null && err.length() > 0) {
lastError = err;
stream.close();
return currentSearchResult;
}
}
if (parser.getName().equals("place")) {
// $NON-NLS-1$
namedDepth++;
if (namedDepth == 1) {
try {
a = new Amenity();
// $NON-NLS-1$//$NON-NLS-2$
a.setLocation(// $NON-NLS-1$//$NON-NLS-2$
Double.parseDouble(parser.getAttributeValue("", "lat")), // $NON-NLS-1$//$NON-NLS-2$
Double.parseDouble(parser.getAttributeValue("", "lon")));
// $NON-NLS-1$ //$NON-NLS-2$
a.setId(Long.parseLong(parser.getAttributeValue("", "place_id")));
// $NON-NLS-1$//$NON-NLS-2$
String name = parser.getAttributeValue("", "display_name");
a.setName(name);
a.setEnName(Junidecode.unidecode(name));
// $NON-NLS-1$//$NON-NLS-2$
a.setSubType(parser.getAttributeValue("", "type"));
PoiType pt = poiTypes.getPoiTypeByKey(a.getSubType());
a.setType(pt != null ? pt.getCategory() : poiTypes.getOtherPoiCategory());
if (matcher == null || matcher.publish(a)) {
currentSearchResult.add(a);
}
} catch (NumberFormatException e) {
// $NON-NLS-1$
log.info("Invalid attributes", e);
}
}
} else if (a != null && parser.getName().equals(a.getSubType())) {
if (parser.next() == XmlPullParser.TEXT) {
String name = parser.getText();
if (name != null) {
a.setName(name);
a.setEnName(Junidecode.unidecode(name));
}
}
}
} else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("place")) {
// $NON-NLS-1$
namedDepth--;
if (namedDepth == 0) {
a = null;
}
}
}
}
stream.close();
} catch (IOException e) {
// $NON-NLS-1$
log.error("Error loading name finder poi", e);
// $NON-NLS-1$
lastError = getApplication().getString(R.string.shared_string_io_error);
} catch (XmlPullParserException e) {
// $NON-NLS-1$
log.error("Error parsing name finder poi", e);
// $NON-NLS-1$
lastError = getApplication().getString(R.string.shared_string_io_error);
}
MapUtils.sortListOfMapObject(currentSearchResult, lat, lon);
return currentSearchResult;
}
use of net.osmand.data.Amenity in project Osmand by osmandapp.
the class MenuBuilder method processNearstWiki.
protected boolean processNearstWiki() {
if (showNearestWiki && latLon != null) {
QuadRect rect = MapUtils.calculateLatLonBbox(latLon.getLatitude(), latLon.getLongitude(), 250);
nearestWiki = app.getResourceManager().searchAmenities(new BinaryMapIndexReader.SearchPoiTypeFilter() {
@Override
public boolean accept(PoiCategory type, String subcategory) {
return type.isWiki();
}
@Override
public boolean isEmpty() {
return false;
}
}, rect.top, rect.left, rect.bottom, rect.right, -1, null);
Collections.sort(nearestWiki, new Comparator<Amenity>() {
@Override
public int compare(Amenity o1, Amenity o2) {
double d1 = MapUtils.getDistance(latLon, o1.getLocation());
double d2 = MapUtils.getDistance(latLon, o2.getLocation());
return Double.compare(d1, d2);
}
});
Long id = objectId;
if (id != 0) {
for (Amenity wiki : nearestWiki) {
if (wiki.getId().equals(id)) {
nearestWiki.remove(wiki);
break;
}
}
}
return true;
}
return false;
}
use of net.osmand.data.Amenity in project Osmand by osmandapp.
the class MenuController method getMenuController.
public static MenuController getMenuController(MainActivity mainActivity, LatLon latLon, PointDescription pointDescription, Object object, MenuType menuType) {
SampleApplication app = mainActivity.getMyApplication();
MenuController menuController = null;
if (object != null) {
if (object instanceof Amenity) {
menuController = new AmenityMenuController(mainActivity, pointDescription, (Amenity) object);
} else if (object instanceof LatLon) {
if (pointDescription.isMyLocation()) {
menuController = new MyLocationMenuController(mainActivity, pointDescription);
}
}
}
if (menuController == null) {
menuController = new PointDescriptionMenuController(mainActivity, pointDescription);
}
menuController.menuType = menuType;
menuController.setLatLon(latLon);
return menuController;
}
use of net.osmand.data.Amenity in project Osmand by osmandapp.
the class QuickSearchListAdapter method getView.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final QuickSearchListItem listItem = getItem(position);
int viewType = getItemViewType(position);
LinearLayout view;
if (viewType == ITEM_TYPE_SEARCH_MORE) {
if (convertView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
view = (LinearLayout) inflater.inflate(R.layout.search_more_list_item, null);
} else {
view = (LinearLayout) convertView;
}
((TextView) view.findViewById(R.id.title)).setText(listItem.getName());
} else {
if (convertView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
view = (LinearLayout) inflater.inflate(R.layout.search_list_item, null);
} else {
view = (LinearLayout) convertView;
}
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView title = (TextView) view.findViewById(R.id.title);
TextView subtitle = (TextView) view.findViewById(R.id.subtitle);
imageView.setImageDrawable(listItem.getIcon());
String name = listItem.getName();
title.setText(name);
String desc = listItem.getTypeName();
boolean hasDesc = false;
if (!Algorithms.isEmpty(desc) && !desc.equals(name)) {
subtitle.setText(desc);
subtitle.setVisibility(View.VISIBLE);
hasDesc = true;
} else {
subtitle.setVisibility(View.GONE);
}
view.findViewById(R.id.type_name_icon).setVisibility(View.GONE);
TextView timeText = (TextView) view.findViewById(R.id.time);
ImageView timeIcon = (ImageView) view.findViewById(R.id.time_icon);
if (listItem.getSearchResult().object instanceof Amenity && ((Amenity) listItem.getSearchResult().object).getOpeningHours() != null) {
Amenity amenity = (Amenity) listItem.getSearchResult().object;
OpeningHoursParser.OpeningHours rs = OpeningHoursParser.parseOpenedHours(amenity.getOpeningHours());
if (rs != null) {
Calendar inst = Calendar.getInstance();
inst.setTimeInMillis(System.currentTimeMillis());
boolean worksNow = rs.isOpenedForTime(inst);
// 30 minutes later
inst.setTimeInMillis(System.currentTimeMillis() + 30 * 60 * 1000);
boolean worksLater = rs.isOpenedForTime(inst);
int colorId = worksNow ? worksLater ? R.color.color_ok : R.color.color_intermediate : R.color.color_warning;
timeIcon.setVisibility(View.VISIBLE);
timeText.setVisibility(View.VISIBLE);
timeIcon.setImageDrawable(app.getIconsCache().getIcon("ic_small_time", colorId));
timeText.setTextColor(app.getResources().getColor(colorId));
String rt = rs.getCurrentRuleTime(inst);
timeText.setText(rt == null ? "" : rt);
} else {
timeIcon.setVisibility(View.GONE);
timeText.setVisibility(View.GONE);
}
} else {
timeIcon.setVisibility(View.GONE);
timeText.setVisibility(View.GONE);
}
updateCompassVisibility(view, listItem);
}
view.setBackgroundColor(app.getResources().getColor(R.color.bg_color_light));
View divider = view.findViewById(R.id.divider);
if (divider != null) {
if (position == getCount() - 1) {
divider.setVisibility(View.GONE);
} else {
divider.setVisibility(View.VISIBLE);
if (position + 1 == searchMoreItemPosition) {
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp1);
p.setMargins(0, 0, 0, 0);
divider.setLayoutParams(p);
} else {
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp1);
p.setMargins(dp56, 0, 0, 0);
divider.setLayoutParams(p);
}
}
}
return view;
}
use of net.osmand.data.Amenity in project Osmand by osmandapp.
the class QuickSearchListAdapter method getView.
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
final QuickSearchListItem listItem = getItem(position);
QuickSearchListItemType type = listItem.getType();
LinearLayout view;
if (type == QuickSearchListItemType.SEARCH_MORE) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.search_more_list_item, null);
} else {
view = (LinearLayout) convertView;
}
if (listItem.getSpannableName() != null) {
((TextView) view.findViewById(R.id.title)).setText(listItem.getSpannableName());
} else {
((TextView) view.findViewById(R.id.title)).setText(listItem.getName());
}
final QuickSearchMoreListItem searchMoreItem = (QuickSearchMoreListItem) listItem;
int emptyDescId = searchMoreItem.isSearchMoreAvailable() ? R.string.nothing_found_descr : R.string.modify_the_search_query;
((TextView) view.findViewById(R.id.empty_search_description)).setText(emptyDescId);
boolean emptySearchVisible = searchMoreItem.isEmptySearch() && !searchMoreItem.isInterruptedSearch();
boolean moreDividerVisible = emptySearchVisible && searchMoreItem.isSearchMoreAvailable();
view.findViewById(R.id.empty_search).setVisibility(emptySearchVisible ? View.VISIBLE : View.GONE);
view.findViewById(R.id.more_divider).setVisibility(moreDividerVisible ? View.VISIBLE : View.GONE);
SearchUICore searchUICore = app.getSearchUICore().getCore();
String textTitle = app.getString(R.string.nothing_found_in_radius) + " " + OsmAndFormatter.getFormattedDistance(searchUICore.getMinimalSearchRadius(searchUICore.getPhrase()), app);
((TextView) view.findViewById(R.id.empty_search_title)).setText(textTitle);
View increaseRadiusRow = view.findViewById(R.id.increase_radius_row);
increaseRadiusRow.setVisibility(searchMoreItem.isSearchMoreAvailable() ? View.VISIBLE : View.GONE);
increaseRadiusRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((QuickSearchMoreListItem) listItem).increaseRadiusOnClick();
}
});
if (!searchMoreItem.isOnlineSearch()) {
View onlineSearchRow = view.findViewById(R.id.online_search_row);
onlineSearchRow.setVisibility(View.VISIBLE);
onlineSearchRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
searchMoreItem.onlineSearchOnClick();
}
});
}
} else if (type == QuickSearchListItemType.BUTTON) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.search_custom_list_item, null);
} else {
view = (LinearLayout) convertView;
}
((ImageView) view.findViewById(R.id.imageView)).setImageDrawable(listItem.getIcon());
if (listItem.getSpannableName() != null) {
((TextView) view.findViewById(R.id.title)).setText(listItem.getSpannableName());
} else {
((TextView) view.findViewById(R.id.title)).setText(listItem.getName());
}
} else if (type == QuickSearchListItemType.SELECT_ALL) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.select_all_list_item, null);
} else {
view = (LinearLayout) convertView;
}
final CheckBox ch = (CheckBox) view.findViewById(R.id.toggle_item);
ch.setVisibility(View.VISIBLE);
ch.setChecked(selectAll);
ch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleCheckbox(position, ch);
}
});
} else if (type == QuickSearchListItemType.HEADER) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.search_header_list_item, null);
} else {
view = (LinearLayout) convertView;
}
view.findViewById(R.id.top_divider).setVisibility(((QuickSearchHeaderListItem) listItem).isShowTopDivider() ? View.VISIBLE : View.GONE);
if (listItem.getSpannableName() != null) {
((TextView) view.findViewById(R.id.title)).setText(listItem.getSpannableName());
} else {
((TextView) view.findViewById(R.id.title)).setText(listItem.getName());
}
} else if (type == QuickSearchListItemType.TOP_SHADOW) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.list_shadow_header, null);
} else {
view = (LinearLayout) convertView;
}
return view;
} else if (type == QuickSearchListItemType.BOTTOM_SHADOW) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.list_shadow_footer, null);
} else {
view = (LinearLayout) convertView;
}
return view;
} else {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.search_list_item, null);
} else {
view = (LinearLayout) convertView;
}
final CheckBox ch = (CheckBox) view.findViewById(R.id.toggle_item);
if (selectionMode) {
ch.setVisibility(View.VISIBLE);
ch.setChecked(selectedItems.contains(listItem));
ch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleCheckbox(position, ch);
}
});
} else {
ch.setVisibility(View.GONE);
}
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView title = (TextView) view.findViewById(R.id.title);
TextView subtitle = (TextView) view.findViewById(R.id.subtitle);
imageView.setImageDrawable(listItem.getIcon());
String name = listItem.getName();
if (listItem.getSpannableName() != null) {
title.setText(listItem.getSpannableName());
} else {
title.setText(name);
}
String desc = listItem.getTypeName();
Object searchResultObject = listItem.getSearchResult().object;
if (searchResultObject instanceof AbstractPoiType) {
AbstractPoiType abstractPoiType = (AbstractPoiType) searchResultObject;
String[] synonyms = abstractPoiType.getSynonyms().split(";");
QuickSearchHelper searchHelper = app.getSearchUICore();
SearchUICore searchUICore = searchHelper.getCore();
String searchPhrase = searchUICore.getPhrase().getText(true);
SearchPhrase.NameStringMatcher nm = new SearchPhrase.NameStringMatcher(searchPhrase, CollatorStringMatcher.StringMatcherMode.CHECK_STARTS_FROM_SPACE);
if (!searchPhrase.isEmpty() && !nm.matches(abstractPoiType.getTranslation())) {
if (nm.matches(abstractPoiType.getEnTranslation())) {
desc = listItem.getTypeName() + " (" + abstractPoiType.getEnTranslation() + ")";
} else {
for (String syn : synonyms) {
if (nm.matches(syn)) {
desc = listItem.getTypeName() + " (" + syn + ")";
break;
}
}
}
}
}
boolean hasDesc = false;
if (!Algorithms.isEmpty(desc) && !desc.equals(name)) {
subtitle.setText(desc);
subtitle.setVisibility(View.VISIBLE);
hasDesc = true;
} else {
subtitle.setVisibility(View.GONE);
}
Drawable typeIcon = listItem.getTypeIcon();
ImageView group = (ImageView) view.findViewById(R.id.type_name_icon);
if (typeIcon != null && hasDesc) {
group.setImageDrawable(typeIcon);
group.setVisibility(View.VISIBLE);
} else {
group.setVisibility(View.GONE);
}
LinearLayout timeLayout = (LinearLayout) view.findViewById(R.id.time_layout);
TextView timeText = (TextView) view.findViewById(R.id.time);
ImageView timeIcon = (ImageView) view.findViewById(R.id.time_icon);
if (listItem.getSearchResult().object instanceof Amenity && ((Amenity) listItem.getSearchResult().object).getOpeningHours() != null) {
Amenity amenity = (Amenity) listItem.getSearchResult().object;
OpeningHoursParser.OpeningHours rs = OpeningHoursParser.parseOpenedHours(amenity.getOpeningHours());
if (rs != null) {
Calendar inst = Calendar.getInstance();
inst.setTimeInMillis(System.currentTimeMillis());
boolean worksNow = rs.isOpenedForTime(inst);
// 30 minutes later
inst.setTimeInMillis(System.currentTimeMillis() + 30 * 60 * 1000);
boolean worksLater = rs.isOpenedForTime(inst);
int colorId = worksNow ? worksLater ? R.color.color_ok : R.color.color_intermediate : R.color.color_warning;
timeLayout.setVisibility(View.VISIBLE);
timeIcon.setImageDrawable(app.getIconsCache().getIcon(R.drawable.ic_small_time, colorId));
timeText.setTextColor(app.getResources().getColor(colorId));
String rt = rs.getCurrentRuleTime(inst);
timeText.setText(rt == null ? "" : rt);
} else {
timeLayout.setVisibility(View.GONE);
}
} else {
timeLayout.setVisibility(View.GONE);
}
updateCompassVisibility(view, listItem);
}
view.setBackgroundColor(app.getResources().getColor(app.getSettings().isLightContent() ? R.color.bg_color_light : R.color.bg_color_dark));
View divider = view.findViewById(R.id.divider);
if (divider != null) {
if (position == getCount() - 1 || getItem(position + 1).getType() == QuickSearchListItemType.HEADER || getItem(position + 1).getType() == QuickSearchListItemType.BOTTOM_SHADOW) {
divider.setVisibility(View.GONE);
} else {
divider.setVisibility(View.VISIBLE);
if (getItem(position + 1).getType() == QuickSearchListItemType.SEARCH_MORE || type == QuickSearchListItemType.SELECT_ALL) {
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp1);
p.setMargins(0, 0, 0, 0);
divider.setLayoutParams(p);
} else {
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp1);
p.setMargins(dp56, 0, 0, 0);
divider.setLayoutParams(p);
}
}
}
ViewCompat.setAccessibilityDelegate(view, accessibilityAssistant);
return view;
}
Aggregations