use of net.osmand.osm.PoiType in project Osmand by osmandapp.
the class AmenityMenuController method getTypeStr.
public static String getTypeStr(Amenity amenity) {
PoiCategory pc = amenity.getType();
PoiType pt = pc.getPoiTypeByKeyName(amenity.getSubType());
String typeStr = amenity.getSubType();
if (pt != null) {
typeStr = pt.getTranslation();
} else if (typeStr != null) {
typeStr = Algorithms.capitalizeFirstLetterAndLowercase(typeStr.replace('_', ' '));
}
return typeStr;
}
use of net.osmand.osm.PoiType in project Osmand by osmandapp.
the class QuickSearchCustomPoiFragment method showDialog.
private void showDialog(final PoiCategory poiCategory, boolean selectAll) {
final int index = listView.getFirstVisiblePosition();
View v = listView.getChildAt(0);
final int top = (v == null) ? 0 : v.getTop();
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
final LinkedHashMap<String, String> subCategories = new LinkedHashMap<String, String>();
Set<String> acceptedCategories = filter.getAcceptedSubtypes(poiCategory);
if (acceptedCategories != null) {
for (String s : acceptedCategories) {
subCategories.put(s, Algorithms.capitalizeFirstLetterAndLowercase(s));
}
}
for (PoiType pt : poiCategory.getPoiTypes()) {
subCategories.put(pt.getKeyName(), pt.getTranslation());
}
final String[] array = subCategories.keySet().toArray(new String[0]);
final Collator cl = Collator.getInstance();
cl.setStrength(Collator.SECONDARY);
Arrays.sort(array, 0, array.length, new Comparator<String>() {
@Override
public int compare(String object1, String object2) {
String v1 = subCategories.get(object1);
String v2 = subCategories.get(object2);
return cl.compare(v1, v2);
}
});
final String[] visibleNames = new String[array.length];
final boolean[] selected = new boolean[array.length];
boolean allSelected = true;
for (int i = 0; i < array.length; i++) {
final String subcategory = array[i];
visibleNames[i] = subCategories.get(subcategory);
if (acceptedCategories == null || selectAll) {
selected[i] = true;
} else {
if (allSelected) {
allSelected = false;
}
selected[i] = acceptedCategories.contains(subcategory);
}
}
View titleView = LayoutInflater.from(getActivity()).inflate(R.layout.subcategories_dialog_title, null);
TextView titleTextView = (TextView) titleView.findViewById(R.id.title);
titleTextView.setText(poiCategory.getTranslation());
SwitchCompat check = (SwitchCompat) titleView.findViewById(R.id.check);
check.setChecked(allSelected);
builder.setCustomTitle(titleView);
builder.setCancelable(true);
builder.setNegativeButton(getContext().getText(R.string.shared_string_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
listAdapter.notifyDataSetChanged();
}
});
builder.setPositiveButton(getContext().getText(R.string.shared_string_apply), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LinkedHashSet<String> accepted = new LinkedHashSet<String>();
for (int i = 0; i < selected.length; i++) {
if (selected[i]) {
accepted.add(array[i]);
}
}
if (subCategories.size() == accepted.size()) {
filter.selectSubTypesToAccept(poiCategory, null);
} else if (accepted.size() == 0) {
filter.setTypeToAccept(poiCategory, false);
} else {
filter.selectSubTypesToAccept(poiCategory, accepted);
}
saveFilter();
listAdapter.notifyDataSetChanged();
listView.setSelectionFromTop(index, top);
}
});
builder.setMultiChoiceItems(visibleNames, selected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int item, boolean isChecked) {
selected[item] = isChecked;
}
});
final AlertDialog dialog = builder.show();
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Arrays.fill(selected, true);
} else {
Arrays.fill(selected, false);
}
for (int i = 0; i < selected.length; i++) {
dialog.getListView().setItemChecked(i, selected[i]);
}
}
});
}
use of net.osmand.osm.PoiType 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.osm.PoiType in project Osmand by osmandapp.
the class PoiUIFilter method updatePoiAdditionals.
private void updatePoiAdditionals() {
Iterator<Entry<PoiCategory, LinkedHashSet<String>>> e = acceptedTypes.entrySet().iterator();
poiAdditionals.clear();
while (e.hasNext()) {
Entry<PoiCategory, LinkedHashSet<String>> pc = e.next();
fillPoiAdditionals(pc.getKey(), pc.getValue() == null);
if (pc.getValue() != null) {
for (String s : pc.getValue()) {
PoiType subtype = poiTypes.getPoiTypeByKey(s);
if (subtype != null) {
fillPoiAdditionals(subtype, false);
}
}
}
}
addOtherPoiAdditionals();
}
use of net.osmand.osm.PoiType in project Osmand by osmandapp.
the class AmenityMenuBuilder method buildInternal.
@Override
public void buildInternal(View view) {
boolean hasWiki = false;
MapPoiTypes poiTypes = app.getPoiTypes();
String preferredLang = SampleApplication.LANGUAGE;
List<AmenityInfoRow> infoRows = new LinkedList<>();
List<AmenityInfoRow> descriptions = new LinkedList<>();
for (Map.Entry<String, String> e : amenity.getAdditionalInfo().entrySet()) {
int iconId;
Drawable icon = null;
int textColor = 0;
String key = e.getKey();
String vl = e.getValue();
String textPrefix = "";
View collapsableView = null;
boolean collapsable = false;
boolean isWiki = false;
boolean isText = false;
boolean isDescription = false;
boolean needLinks = !"population".equals(key);
boolean isPhoneNumber = false;
boolean isUrl = false;
int poiTypeOrder = 0;
String poiTypeKeyName = "";
AbstractPoiType pt = poiTypes.getAnyPoiAdditionalTypeByKey(key);
if (pt == null && !Algorithms.isEmpty(vl) && vl.length() < 50) {
pt = poiTypes.getAnyPoiAdditionalTypeByKey(key + "_" + vl);
}
PoiType pType = null;
if (pt != null) {
pType = (PoiType) pt;
if (pType.isFilterOnly()) {
continue;
}
poiTypeOrder = pType.getOrder();
poiTypeKeyName = pType.getKeyName();
}
if (amenity.getType().isWiki()) {
if (!hasWiki) {
iconId = OsmandResources.getDrawableId("ic_action_note_dark");
String lng = amenity.getContentLanguage("content", preferredLang, "en");
if (Algorithms.isEmpty(lng)) {
lng = "en";
}
final String langSelected = lng;
String content = amenity.getDescription(langSelected);
vl = (content != null) ? Html.fromHtml(content).toString() : "";
if (vl.length() > 300) {
vl = vl.substring(0, 300);
}
hasWiki = true;
isWiki = true;
needLinks = false;
} else {
continue;
}
} else if (key.startsWith("name:")) {
continue;
} else if (Amenity.OPENING_HOURS.equals(key)) {
iconId = OsmandResources.getDrawableId("ic_action_time");
collapsableView = getCollapsableTextView(view.getContext(), true, amenity.getOpeningHours());
collapsable = true;
OpeningHoursParser.OpeningHours rs = OpeningHoursParser.parseOpenedHours(amenity.getOpeningHours());
if (rs != null) {
vl = rs.toLocalString();
Calendar inst = Calendar.getInstance();
inst.setTimeInMillis(System.currentTimeMillis());
boolean opened = rs.isOpenedForTime(inst);
if (opened) {
textColor = R.color.color_ok;
} else {
textColor = R.color.color_invalid;
}
}
needLinks = false;
} else if (Amenity.PHONE.equals(key)) {
iconId = OsmandResources.getDrawableId("ic_action_call_dark");
isPhoneNumber = true;
} else if (Amenity.WEBSITE.equals(key)) {
iconId = OsmandResources.getDrawableId("ic_world_globe_dark");
isUrl = true;
} else if (Amenity.CUISINE.equals(key)) {
iconId = OsmandResources.getDrawableId("ic_action_cuisine");
StringBuilder sb = new StringBuilder();
for (String c : e.getValue().split(";")) {
if (sb.length() > 0) {
sb.append(", ");
} else {
sb.append(app.getString("poi_cuisine")).append(": ");
}
sb.append(poiTypes.getPoiTranslation("cuisine_" + c).toLowerCase());
}
vl = sb.toString();
} else {
if (key.contains(Amenity.DESCRIPTION)) {
iconId = OsmandResources.getDrawableId("ic_action_note_dark");
} else {
iconId = OsmandResources.getDrawableId("ic_action_info_dark");
}
if (pType != null) {
poiTypeOrder = pType.getOrder();
poiTypeKeyName = pType.getKeyName();
if (pType.getParentType() != null && pType.getParentType() instanceof PoiType) {
icon = getRowIcon(view.getContext(), ((PoiType) pType.getParentType()).getOsmTag() + "_" + pType.getOsmTag().replace(':', '_') + "_" + pType.getOsmValue());
}
if (!pType.isText()) {
if (!Algorithms.isEmpty(pType.getPoiAdditionalCategory())) {
vl = pType.getPoiAdditionalCategoryTranslation() + ": " + pType.getTranslation().toLowerCase();
} else {
vl = pType.getTranslation();
}
} else {
isText = true;
isDescription = iconId == OsmandResources.getDrawableId("ic_action_note_dark");
textPrefix = pType.getTranslation();
vl = amenity.unzipContent(e.getValue());
}
if (!isDescription && icon == null) {
icon = getRowIcon(view.getContext(), pType.getIconKeyName());
if (isText && icon != null) {
textPrefix = "";
}
}
if (icon == null && isText) {
iconId = OsmandResources.getDrawableId("ic_action_note_dark");
}
} else {
textPrefix = Algorithms.capitalizeFirstLetterAndLowercase(e.getKey());
vl = amenity.unzipContent(e.getValue());
}
}
if (vl.startsWith("http://") || vl.startsWith("https://") || vl.startsWith("HTTP://") || vl.startsWith("HTTPS://")) {
isUrl = true;
}
if (isDescription) {
descriptions.add(new AmenityInfoRow(key, OsmandResources.getDrawableId("ic_action_note_dark"), textPrefix, vl, collapsable, collapsableView, 0, false, true, true, 0, "", false, false));
} else if (icon != null) {
infoRows.add(new AmenityInfoRow(key, icon, textPrefix, vl, collapsable, collapsableView, textColor, isWiki, isText, needLinks, poiTypeOrder, poiTypeKeyName, isPhoneNumber, isUrl));
} else {
infoRows.add(new AmenityInfoRow(key, iconId, textPrefix, vl, collapsable, collapsableView, textColor, isWiki, isText, needLinks, poiTypeOrder, poiTypeKeyName, isPhoneNumber, isUrl));
}
}
Collections.sort(infoRows, new Comparator<AmenityInfoRow>() {
@Override
public int compare(AmenityInfoRow row1, AmenityInfoRow row2) {
if (row1.order < row2.order) {
return -1;
} else if (row1.order == row2.order) {
return row1.name.compareTo(row2.name);
} else {
return 1;
}
}
});
for (AmenityInfoRow info : infoRows) {
buildAmenityRow(view, info);
}
String langSuffix = ":" + preferredLang;
AmenityInfoRow descInPrefLang = null;
for (AmenityInfoRow desc : descriptions) {
if (desc.key.length() > langSuffix.length() && desc.key.substring(desc.key.length() - langSuffix.length(), desc.key.length()).equals(langSuffix)) {
descInPrefLang = desc;
break;
}
}
if (descInPrefLang != null) {
descriptions.remove(descInPrefLang);
descriptions.add(0, descInPrefLang);
}
for (AmenityInfoRow info : descriptions) {
buildAmenityRow(view, info);
}
if (processNearstWiki() && nearestWiki.size() > 0) {
AmenityInfoRow wikiInfo = new AmenityInfoRow("nearest_wiki", OsmandResources.getDrawableId("ic_action_wikipedia"), null, app.getString("wiki_around") + " (" + nearestWiki.size() + ")", true, getCollapsableWikiView(view.getContext(), true), 0, false, false, false, 1000, null, false, false);
buildAmenityRow(view, wikiInfo);
}
buildRow(view, OsmandResources.getDrawableId("ic_action_get_my_location"), PointDescription.getLocationName(app, amenity.getLocation().getLatitude(), amenity.getLocation().getLongitude(), true).replaceAll("\n", " "), 0, false, null, false, 0, false, null);
}
Aggregations