Merge remote-tracking branch 'origin/master'

This commit is contained in:
Weblate 2016-06-18 09:40:28 +02:00
commit 02c310a438
8 changed files with 275 additions and 184 deletions

View file

@ -40,17 +40,15 @@ import net.osmand.core.jni.MapRasterLayerProvider_Software;
import net.osmand.core.jni.MapStylesCollection;
import net.osmand.core.jni.ObfMapObjectsProvider;
import net.osmand.core.jni.ObfsCollection;
import net.osmand.core.jni.OsmAndCore;
import net.osmand.core.jni.OsmAndCoreJNI;
import net.osmand.core.jni.PointI;
import net.osmand.core.jni.QIODeviceLogSink;
import net.osmand.core.jni.ResolvedMapStyle;
import net.osmand.core.jni.Utilities;
import net.osmand.core.samples.android.sample1.MultiTouchSupport.MultiTouchZoomListener;
import net.osmand.core.samples.android.sample1.SearchAPI.SearchAPICallback;
import net.osmand.core.samples.android.sample1.SearchAPI.SearchItem;
import net.osmand.core.samples.android.sample1.SearchUIHelper.SearchListAdapter;
import net.osmand.core.samples.android.sample1.SearchUIHelper.SearchRow;
import net.osmand.core.samples.android.sample1.search.SearchAPI;
import net.osmand.core.samples.android.sample1.search.SearchAPI.SearchAPICallback;
import net.osmand.core.samples.android.sample1.search.SearchListAdapter;
import net.osmand.core.samples.android.sample1.search.SearchListItem;
import java.util.ArrayList;
import java.util.Comparator;
@ -264,7 +262,7 @@ public class MainActivity extends Activity {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
hideSearchList();
mapView.requestFocus();
SearchRow item = adapter.getItem(position);
SearchListItem item = adapter.getItem(position);
PointI target = Utilities.convertLatLonTo31(new LatLon(item.getLatitude(), item.getLongitude()));
setTarget(target);
setZoom(17f);
@ -409,24 +407,25 @@ public class MainActivity extends Activity {
searchAPI.setObfAreaFilter(bounds31);
searchAPI.startSearch(keyword, MAX_SEARCH_RESULTS, new SearchAPICallback() {
@Override
public void onSearchFinished(List<SearchItem> searchItems, boolean cancelled) {
public void onSearchFinished(List<SearchAPI.SearchItem> searchItems, boolean cancelled) {
if (searchItems != null && !cancelled) {
LatLon latLon = Utilities.convert31ToLatLon(target31);
List<SearchRow> rows = new ArrayList<>();
for (SearchItem item : searchItems) {
SearchRow row = new SearchRow(item);
List<SearchListItem> rows = new ArrayList<>();
for (SearchAPI.SearchItem item : searchItems) {
SearchListItem row =
SearchListItem.buildListItem((SampleApplication)getApplication(), item);
rows.add(row);
}
adapter.clear();
adapter.addAll(rows);
adapter.updateDistance(latLon.getLatitude(), latLon.getLongitude());
adapter.sort(new Comparator<SearchRow>() {
adapter.sort(new Comparator<SearchListItem>() {
@Override
public int compare(SearchRow lhs, SearchRow rhs) {
public int compare(SearchListItem lhs, SearchListItem rhs) {
int res = Double.compare(lhs.getDistance(), rhs.getDistance());
if (res == 0) {
return lhs.getSearchItem().getLocalizedName().compareToIgnoreCase(rhs.getSearchItem().getLocalizedName());
return lhs.getName().compareToIgnoreCase(rhs.getName());
} else {
return res;
}

View file

@ -6,13 +6,6 @@ import android.os.Environment;
import net.osmand.osm.AbstractPoiType;
import net.osmand.osm.MapPoiTypes;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.lang.reflect.Field;
public class SampleApplication extends Application
@ -24,7 +17,7 @@ public class SampleApplication extends Application
{
super.onCreate();
//initPoiTypes();
initPoiTypes();
}
public MapPoiTypes getPoiTypes() {

View file

@ -1,139 +0,0 @@
package net.osmand.core.samples.android.sample1;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.osmand.core.jni.Utilities;
import net.osmand.core.samples.android.sample1.SearchAPI.SearchItem;
import java.text.MessageFormat;
public class SearchUIHelper {
public static Drawable getIcon(Context ctx, SearchItem item) {
return null;
}
public static class SearchRow {
private SearchItem searchItem;
private double distance;
public SearchRow(SearchItem searchItem) {
this.searchItem = searchItem;
}
public SearchItem getSearchItem() {
return searchItem;
}
public double getLatitude() {
return searchItem.getLatitude();
}
public double getLongitude() {
return searchItem.getLongitude();
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
}
public static class SearchListAdapter extends ArrayAdapter<SearchRow> {
private Context ctx;
public SearchListAdapter(Context ctx) {
super(ctx, R.layout.search_list_item);
this.ctx = ctx;
}
public void updateDistance(double latitude, double longitude) {
for (int i = 0; i < getCount(); i++) {
SearchRow item = getItem(i);
item.setDistance(Utilities.distance(
longitude, latitude,
item.getLongitude(), item.getLatitude()));
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SearchRow item = getItem(position);
LinearLayout view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(
R.layout.search_list_item, null);
} else {
view = (LinearLayout) convertView;
}
TextView title = (TextView) view.findViewById(R.id.title);
TextView subtitle = (TextView) view.findViewById(R.id.subtitle);
TextView distance = (TextView) view.findViewById(R.id.distance);
title.setText(item.searchItem.getLocalizedName());
StringBuilder sb = new StringBuilder();
if (!item.searchItem.getSubTypeName().isEmpty()) {
sb.append(getNiceString(item.searchItem.getSubTypeName()));
}
if (!item.searchItem.getTypeName().isEmpty()) {
if (sb.length() > 0) {
sb.append("");
}
sb.append(getNiceString(item.searchItem.getTypeName()));
}
subtitle.setText(sb.toString());
if (item.getDistance() == 0) {
distance.setText("");
} else {
distance.setText(getFormattedDistance(item.getDistance()));
}
//text1.setTextColor(ctx.getResources().getColor(R.color.listTextColor));
//view.setCompoundDrawablesWithIntrinsicBounds(getIcon(ctx, item), null, null, null);
//view.setCompoundDrawablePadding(ctx.getResources().getDimensionPixelSize(R.dimen.list_content_padding));
return view;
}
}
public static String capitalizeFirstLetterAndLowercase(String s) {
if (s != null && s.length() > 1) {
// not very efficient algorithm
return Character.toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase();
} else {
return s;
}
}
public static String getNiceString(String s) {
return capitalizeFirstLetterAndLowercase(s.replaceAll("_", " "));
}
public static String getFormattedDistance(double meters) {
double mainUnitInMeters = 1000;
String mainUnitStr = "km";
if (meters >= 100 * mainUnitInMeters) {
return (int) (meters / mainUnitInMeters + 0.5) + " " + mainUnitStr;
} else if (meters > 9.99f * mainUnitInMeters) {
return MessageFormat.format("{0,number,#.#} " + mainUnitStr, ((float) meters) / mainUnitInMeters).replace('\n', ' ');
} else if (meters > 0.999f * mainUnitInMeters) {
return MessageFormat.format("{0,number,#.##} " + mainUnitStr, ((float) meters) / mainUnitInMeters).replace('\n', ' ');
} else {
return ((int) (meters + 0.5)) + " m";
}
}
}

View file

@ -0,0 +1,21 @@
package net.osmand.core.samples.android.sample1;
import java.text.MessageFormat;
public class Utils {
public static String getFormattedDistance(double meters) {
double mainUnitInMeters = 1000;
String mainUnitStr = "km";
if (meters >= 100 * mainUnitInMeters) {
return (int) (meters / mainUnitInMeters + 0.5) + " " + mainUnitStr;
} else if (meters > 9.99f * mainUnitInMeters) {
return MessageFormat.format("{0,number,#.#} " + mainUnitStr, ((float) meters) / mainUnitInMeters).replace('\n', ' ');
} else if (meters > 0.999f * mainUnitInMeters) {
return MessageFormat.format("{0,number,#.##} " + mainUnitStr, ((float) meters) / mainUnitInMeters).replace('\n', ' ');
} else {
return ((int) (meters + 0.5)) + " m";
}
}
}

View file

@ -0,0 +1,72 @@
package net.osmand.core.samples.android.sample1.search;
import net.osmand.core.samples.android.sample1.MapUtils;
import net.osmand.core.samples.android.sample1.SampleApplication;
import net.osmand.core.samples.android.sample1.search.SearchAPI.AmenitySearchItem;
import net.osmand.data.Amenity;
import net.osmand.osm.MapPoiTypes;
import net.osmand.osm.PoiCategory;
import net.osmand.util.Algorithms;
import java.util.Map.Entry;
public class AmenitySearchListItem extends SearchListItem {
private Amenity amenity;
private String nameStr;
private String typeStr;
public AmenitySearchListItem(SampleApplication app, AmenitySearchItem searchItem) {
super(app, searchItem);
amenity = parseAmenity(searchItem);
nameStr = amenity.getName(MapUtils.LANGUAGE);
typeStr = getTypeStr();
}
private Amenity parseAmenity(AmenitySearchItem searchItem) {
MapPoiTypes poiTypes = app.getPoiTypes();
Amenity a = new Amenity();
PoiCategory category = poiTypes.getPoiCategoryByName(searchItem.getCategory());
a.setType(category);
a.setSubType(searchItem.getSubcategory());
a.setName(searchItem.getNativeName());
for (Entry<String, String> entry : searchItem.getLocalizedNames().entrySet()) {
a.setName(entry.getKey(), entry.getValue());
}
//a.setAdditionalInfo(searchItem.getValues());
return a;
}
public Amenity getAmenity() {
return amenity;
}
@Override
public String getName() {
return nameStr;
}
@Override
public String getType() {
return typeStr;
}
private String getTypeStr() {
/*
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;
*/
return Algorithms.capitalizeFirstLetterAndLowercase(amenity.getSubType().replace('_', ' '));
}
}

View file

@ -1,24 +1,29 @@
package net.osmand.core.samples.android.sample1;
package net.osmand.core.samples.android.sample1.search;
import android.os.AsyncTask;
import net.osmand.core.jni.AmenitiesByNameSearch;
import net.osmand.core.jni.Amenity;
import net.osmand.core.jni.Amenity.DecodedCategory;
import net.osmand.core.jni.Amenity.DecodedValue;
import net.osmand.core.jni.AreaI;
import net.osmand.core.jni.DecodedCategoryList;
import net.osmand.core.jni.DecodedValueList;
import net.osmand.core.jni.IQueryController;
import net.osmand.core.jni.ISearch;
import net.osmand.core.jni.LatLon;
import net.osmand.core.jni.NullableAreaI;
import net.osmand.core.jni.ObfsCollection;
import net.osmand.core.jni.PointI;
import net.osmand.core.jni.QStringList;
import net.osmand.core.jni.QStringStringHash;
import net.osmand.core.jni.Utilities;
import net.osmand.osm.PoiType;
import net.osmand.util.Algorithms;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SearchAPI {
@ -27,7 +32,7 @@ public class SearchAPI {
private AreaI obfAreaFilter;
private SearchRequestExecutor executor;
interface SearchAPICallback {
public interface SearchAPICallback {
void onSearchFinished(List<SearchItem> searchItems, boolean cancelled);
}
@ -205,13 +210,9 @@ public class SearchAPI {
longitude = latLon.getLongitude();
}
public abstract String getNativeName();
public abstract String getName();
public abstract String getLocalizedName();
public abstract String getTypeName();
public abstract String getSubTypeName();
public abstract String getType();
public double getLatitude() {
return latitude;
@ -223,24 +224,28 @@ public class SearchAPI {
@Override
public String toString() {
return getNativeName() + " {lat:" + getLatitude() + " lon: " + getLongitude() + "}";
return getName() + " {lat:" + getLatitude() + " lon: " + getLongitude() + "}";
}
}
public static class AmenitySearchItem extends SearchItem {
private String nativeName;
private String localizedName;
private String category;
private String subcategory;
private Map<String, String> localizedNames = new HashMap<>();
private Map<String, String> values = new HashMap<>();
public AmenitySearchItem(Amenity amenity) {
super(amenity.getPosition31());
nativeName = amenity.getNativeName();
QStringStringHash locNames = amenity.getLocalizedNames();
if (locNames.has_key(MapUtils.LANGUAGE)) {
localizedName = locNames.get(MapUtils.LANGUAGE);
QStringList locNamesKeys = locNames.keys();
for (int i = 0; i < locNamesKeys.size(); i++) {
String key = locNamesKeys.get(i);
String val = locNames.get(key);
localizedNames.put(key, val);
}
DecodedCategoryList catList = amenity.getDecodedCategories();
@ -249,28 +254,48 @@ public class SearchAPI {
category = decodedCategory.getCategory();
subcategory = decodedCategory.getSubcategory();
}
DecodedValueList decodedValueList = amenity.getDecodedValues();
if (decodedValueList.size() > 0) {
for (int i = 0; i < decodedValueList.size(); i++) {
DecodedValue decodedValue = decodedValueList.get(i);
String tag = decodedValue.getDeclaration().getTagName();
String value = decodedValue.getValue().toString();
values.put(tag, value);
}
}
}
@Override
public String getNativeName() {
return nativeName;
}
@Override
public String getLocalizedName() {
return localizedName != null ? localizedName : nativeName;
}
@Override
public String getTypeName() {
public String getCategory() {
return category;
}
@Override
public String getSubTypeName() {
public String getSubcategory() {
return subcategory;
}
public Map<String, String> getLocalizedNames() {
return localizedNames;
}
public Map<String, String> getValues() {
return values;
}
@Override
public String getName() {
return nativeName;
}
@Override
public String getType() {
return Algorithms.capitalizeFirstLetterAndLowercase(subcategory);
}
@Override
public double getLatitude() {
return latitude;

View file

@ -0,0 +1,64 @@
package net.osmand.core.samples.android.sample1.search;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.osmand.core.jni.Utilities;
import net.osmand.core.samples.android.sample1.R;
import net.osmand.core.samples.android.sample1.Utils;
public class SearchListAdapter extends ArrayAdapter<SearchListItem> {
private Context ctx;
public SearchListAdapter(Context ctx) {
super(ctx, R.layout.search_list_item);
this.ctx = ctx;
}
public void updateDistance(double latitude, double longitude) {
for (int i = 0; i < getCount(); i++) {
SearchListItem item = getItem(i);
item.setDistance(Utilities.distance(
longitude, latitude,
item.getLongitude(), item.getLatitude()));
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SearchListItem listItem = getItem(position);
LinearLayout view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(
R.layout.search_list_item, null);
} else {
view = (LinearLayout) convertView;
}
TextView title = (TextView) view.findViewById(R.id.title);
TextView subtitle = (TextView) view.findViewById(R.id.subtitle);
TextView distance = (TextView) view.findViewById(R.id.distance);
title.setText(listItem.getName());
subtitle.setText(listItem.getType());
if (listItem.getDistance() == 0) {
distance.setText("");
} else {
distance.setText(Utils.getFormattedDistance(listItem.getDistance()));
}
//text1.setTextColor(ctx.getResources().getColor(R.color.listTextColor));
//view.setCompoundDrawablesWithIntrinsicBounds(getIcon(ctx, item), null, null, null);
//view.setCompoundDrawablePadding(ctx.getResources().getDimensionPixelSize(R.dimen.list_content_padding));
return view;
}
}

View file

@ -0,0 +1,56 @@
package net.osmand.core.samples.android.sample1.search;
import android.graphics.drawable.Drawable;
import net.osmand.core.samples.android.sample1.SampleApplication;
import net.osmand.core.samples.android.sample1.search.SearchAPI.AmenitySearchItem;
import net.osmand.core.samples.android.sample1.search.SearchAPI.SearchItem;
public class SearchListItem {
protected SampleApplication app;
private SearchItem searchItem;
private double distance;
public SearchListItem(SampleApplication app, SearchItem searchItem) {
this.app = app;
this.searchItem = searchItem;
}
public static SearchListItem buildListItem(SampleApplication app, SearchItem item) {
if (item instanceof AmenitySearchItem) {
return new AmenitySearchListItem(app, (AmenitySearchItem) item);
}
return null;
}
public double getLatitude() {
return searchItem.getLatitude();
}
public double getLongitude() {
return searchItem.getLongitude();
}
public String getName() {
return searchItem.getName();
}
public String getType() {
return searchItem.getType();
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
public Drawable getIcon() {
return null;
}
}