refactoring poi filter usage

git-svn-id: https://osmand.googlecode.com/svn/trunk@152 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-06-11 22:05:43 +00:00
parent efa35f6a16
commit 2304e94369
13 changed files with 210 additions and 181 deletions

View file

@ -51,6 +51,9 @@ public class ToDoConstants {
// 5. Improvement : Implement caching files existing on FS, implement specific method in RM // 5. Improvement : Implement caching files existing on FS, implement specific method in RM
// Introducing cache of file names that are on disk (creating new File() consumes a lot of memory) // Introducing cache of file names that are on disk (creating new File() consumes a lot of memory)
// BUGS Swing
// 1. Bug renaming region
// TODO swing // TODO swing
// 4. Fix issues with big files (such as netherlands) - save memory (!) // 4. Fix issues with big files (such as netherlands) - save memory (!)

View file

@ -1,6 +1,5 @@
package com.osmand.data; package com.osmand.data;
import com.osmand.Algoritms;
import com.osmand.osm.Entity; import com.osmand.osm.Entity;
import com.osmand.osm.OSMSettings.OSMTagKey; import com.osmand.osm.OSMSettings.OSMTagKey;
@ -98,8 +97,7 @@ public class Amenity extends MapObject {
public String getSimpleFormat(boolean en){ public String getSimpleFormat(boolean en){
return Algoritms.capitalizeFirstLetterAndLowercase(getType().toString()) + return AmenityType.toPublicString(type) + " : " + getStringWithoutType(en);
" : " + getStringWithoutType(en);
} }
public String getStringWithoutType(boolean en){ public String getStringWithoutType(boolean en){

View file

@ -5,6 +5,8 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.osmand.Algoritms;
// http://wiki.openstreetmap.org/wiki/Amenity // http://wiki.openstreetmap.org/wiki/Amenity
// POI tags : amenity, leisure, shop, sport, tourism, historic; accessories (internet-access), natural ? // POI tags : amenity, leisure, shop, sport, tourism, historic; accessories (internet-access), natural ?
public enum AmenityType { public enum AmenityType {
@ -23,7 +25,11 @@ public enum AmenityType {
; ;
public static AmenityType fromString(String s){ public static AmenityType fromString(String s){
try {
return AmenityType.valueOf(s.toUpperCase()); return AmenityType.valueOf(s.toUpperCase());
} catch (IllegalArgumentException e) {
return AmenityType.OTHER;
}
} }
public static String valueToString(AmenityType t){ public static String valueToString(AmenityType t){
@ -44,6 +50,10 @@ public enum AmenityType {
return list; return list;
} }
public static String toPublicString(AmenityType t){
return Algoritms.capitalizeFirstLetterAndLowercase(t.toString().replace('_', ' '));
}
protected static Map<String, AmenityType> amenityMap = new LinkedHashMap<String, AmenityType>(); protected static Map<String, AmenityType> amenityMap = new LinkedHashMap<String, AmenityType>();
static { static {

View file

@ -17,7 +17,7 @@
<activity android:name=".activities.FavouritesActivity" android:label="@string/favourites_activity"></activity> <activity android:name=".activities.FavouritesActivity" android:label="@string/favourites_activity"></activity>
<activity android:name=".activities.search.SearchPOIActivity" android:label="@string/searchpoi_activity"></activity> <activity android:name=".activities.search.SearchPOIActivity" android:label="@string/searchpoi_activity"></activity>
<activity android:name=".activities.search.SearchPOIListActivity"></activity> <activity android:name=".activities.search.SearchPoiFilterActivity"></activity>
<activity android:name=".activities.search.SearchAddressActivity"></activity> <activity android:name=".activities.search.SearchAddressActivity"></activity>
<activity android:name=".activities.search.SearchCityByNameActivity"></activity> <activity android:name=".activities.search.SearchCityByNameActivity"></activity>
<activity android:name=".activities.search.SearchRegionByNameActivity"></activity> <activity android:name=".activities.search.SearchRegionByNameActivity"></activity>

View file

@ -34,15 +34,20 @@ public class AmenityIndexRepository {
private double cBottomLatitude; private double cBottomLatitude;
private double cLeftLongitude; private double cLeftLongitude;
private double cRightLongitude; private double cRightLongitude;
private String cFilterId;
private final String[] columns = IndexConstants.generateColumnNames(IndexPoiTable.values()); private final String[] columns = IndexConstants.generateColumnNames(IndexPoiTable.values());
public List<Amenity> searchAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int limit, AmenityType type, List<Amenity> amenities){ public List<Amenity> searchAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int limit, PoiFilter filter, List<Amenity> amenities){
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
String squery = "? < latitude AND latitude < ? AND ? < longitude AND longitude < ?"; String squery = "? < latitude AND latitude < ? AND ? < longitude AND longitude < ?";
if(type != null){
squery += " AND type = " + "'" +AmenityType.valueToString(type)+ "'"; if(filter != null){
String sql = filter.buildSqlWhereFilter();
if(sql != null){
squery += " AND " + sql;
}
} }
Cursor query = db.query(IndexPoiTable.getTable(), columns, squery, Cursor query = db.query(IndexPoiTable.getTable(), columns, squery,
new String[]{Double.toString(bottomLatitude), new String[]{Double.toString(bottomLatitude),
@ -80,32 +85,34 @@ public class AmenityIndexRepository {
cBottomLatitude = 0; cBottomLatitude = 0;
cRightLongitude = 0; cRightLongitude = 0;
cLeftLongitude = 0; cLeftLongitude = 0;
cFilterId = null;
} }
public void evaluateCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, List<Amenity> toFill){ public void evaluateCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, PoiFilter filter, List<Amenity> toFill){
cachedAmenities.clear(); cachedAmenities.clear();
cTopLatitude = topLatitude + (topLatitude -bottomLatitude); cTopLatitude = topLatitude + (topLatitude -bottomLatitude);
cBottomLatitude = bottomLatitude - (topLatitude -bottomLatitude); cBottomLatitude = bottomLatitude - (topLatitude -bottomLatitude);
cLeftLongitude = leftLongitude - (rightLongitude - leftLongitude); cLeftLongitude = leftLongitude - (rightLongitude - leftLongitude);
cRightLongitude = rightLongitude + (rightLongitude - leftLongitude); cRightLongitude = rightLongitude + (rightLongitude - leftLongitude);
cFilterId = filter == null? null :filter.getFilterId();
// first of all put all entities in temp list in order to not freeze other read threads // first of all put all entities in temp list in order to not freeze other read threads
ArrayList<Amenity> tempList = new ArrayList<Amenity>(); ArrayList<Amenity> tempList = new ArrayList<Amenity>();
searchAmenities(cTopLatitude, cLeftLongitude, cBottomLatitude, cRightLongitude, -1, null, tempList); searchAmenities(cTopLatitude, cLeftLongitude, cBottomLatitude, cRightLongitude, -1, filter, tempList);
synchronized (this) { synchronized (this) {
cachedAmenities.clear(); cachedAmenities.clear();
cachedAmenities.addAll(tempList); cachedAmenities.addAll(tempList);
} }
checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill); checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, filter.getFilterId(), toFill);
} }
public synchronized boolean checkCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, List<Amenity> toFill, boolean fillFound){ public synchronized boolean checkCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, String filterId, List<Amenity> toFill, boolean fillFound){
if (db == null) { if (db == null) {
return true; return true;
} }
boolean inside = cTopLatitude >= topLatitude && cLeftLongitude <= leftLongitude && cRightLongitude >= rightLongitude boolean inside = cTopLatitude >= topLatitude && cLeftLongitude <= leftLongitude && cRightLongitude >= rightLongitude
&& cBottomLatitude <= bottomLatitude; && cBottomLatitude <= bottomLatitude;
if((inside || fillFound) && toFill != null){ if((inside || fillFound) && toFill != null && Algoritms.objectEquals(filterId, cFilterId)){
for(Amenity a : cachedAmenities){ for(Amenity a : cachedAmenities){
LatLon location = a.getLocation(); LatLon location = a.getLocation();
if (location.getLatitude() <= topLatitude && location.getLongitude() >= leftLongitude && location.getLongitude() <= rightLongitude if (location.getLatitude() <= topLatitude && location.getLongitude() >= leftLongitude && location.getLongitude() <= rightLongitude
@ -116,8 +123,8 @@ public class AmenityIndexRepository {
} }
return inside; return inside;
} }
public boolean checkCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, List<Amenity> toFill){ public boolean checkCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, String filterId, List<Amenity> toFill){
return checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill, false); return checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, filterId, toFill, false);
} }
public void initialize(final IProgress progress, File file) { public void initialize(final IProgress progress, File file) {

View file

@ -7,6 +7,7 @@ import java.util.Map;
import com.osmand.data.Amenity; import com.osmand.data.Amenity;
import com.osmand.data.AmenityType; import com.osmand.data.AmenityType;
import com.osmand.data.index.IndexConstants.IndexPoiTable;
public class PoiFilter { public class PoiFilter {
@ -14,6 +15,8 @@ public class PoiFilter {
public static String USER_PREFIX = "user_"; public static String USER_PREFIX = "user_";
private Map<AmenityType, List<String>> acceptedTypes = new LinkedHashMap<AmenityType, List<String>>(); private Map<AmenityType, List<String>> acceptedTypes = new LinkedHashMap<AmenityType, List<String>>();
private String filterByName = null;
private String filterId; private String filterId;
private String name; private String name;
private final boolean isStandardFilter; private final boolean isStandardFilter;
@ -96,6 +99,56 @@ public class PoiFilter {
} }
} }
public String buildSqlWhereFilter(){
if(AmenityType.values().length == acceptedTypes.size()){
boolean wildcard = true;
for(AmenityType a : acceptedTypes.keySet()){
if(acceptedTypes.get(a) != null){
wildcard = false;
break;
}
}
if(wildcard){
return null;
}
}
if(acceptedTypes.size() == 0){
return "1 > 1";
}
StringBuilder b = new StringBuilder();
b.append("(");
boolean first = true;
for(AmenityType a : acceptedTypes.keySet()){
if(first){
first = false;
} else {
b.append(" OR ");
}
b.append("(");
b.append(IndexPoiTable.TYPE.name().toLowerCase()).append(" = '").append(AmenityType.valueToString(a)).append("'");
if(acceptedTypes.get(a) != null){
List<String> list = acceptedTypes.get(a);
b.append(" AND ");
b.append(IndexPoiTable.SUBTYPE.name().toLowerCase()).append(" IN (");
boolean bfirst = true;
for(String s : list){
if(bfirst){
bfirst = false;
} else {
b.append(", ");
}
b.append("'").append(s).append("'");
}
b.append(")");
}
b.append(")");
}
b.append(")");
return b.toString();
}
public void selectSubTypesToAccept(AmenityType t, List<String> accept){ public void selectSubTypesToAccept(AmenityType t, List<String> accept){
acceptedTypes.put(t, accept); acceptedTypes.put(t, accept);
} }
@ -104,6 +157,14 @@ public class PoiFilter {
return filterId; return filterId;
} }
public String getFilterByName() {
return filterByName;
}
public void setFilterByName(String filterByName) {
this.filterByName = filterByName;
}
public boolean isStandardFilter(){ public boolean isStandardFilter(){
return isStandardFilter; return isStandardFilter;
} }

View file

@ -28,7 +28,7 @@ public class PoiFiltersHelper {
return null; return null;
} }
private static List<PoiFilter> cacheUserDefinedFilters = new ArrayList<PoiFilter>(); private static List<PoiFilter> cacheUserDefinedFilters;
public static List<PoiFilter> getUserDefinedPoiFilters(Context ctx){ public static List<PoiFilter> getUserDefinedPoiFilters(Context ctx){
if(cacheUserDefinedFilters == null){ if(cacheUserDefinedFilters == null){
cacheUserDefinedFilters = new ArrayList<PoiFilter>(); cacheUserDefinedFilters = new ArrayList<PoiFilter>();
@ -37,7 +37,7 @@ public class PoiFiltersHelper {
return cacheUserDefinedFilters; return cacheUserDefinedFilters;
} }
private static List<PoiFilter> cacheOsmDefinedFilters = new ArrayList<PoiFilter>(); private static List<PoiFilter> cacheOsmDefinedFilters;
public static List<PoiFilter> getOsmDefinedPoiFilters(Context ctx){ public static List<PoiFilter> getOsmDefinedPoiFilters(Context ctx){
if(cacheOsmDefinedFilters == null){ if(cacheOsmDefinedFilters == null){
cacheOsmDefinedFilters = new ArrayList<PoiFilter>(); cacheOsmDefinedFilters = new ArrayList<PoiFilter>();

View file

@ -17,7 +17,6 @@ import android.graphics.BitmapFactory;
import android.os.Environment; import android.os.Environment;
import com.osmand.data.Amenity; import com.osmand.data.Amenity;
import com.osmand.data.AmenityType;
import com.osmand.data.index.IndexConstants; import com.osmand.data.index.IndexConstants;
import com.osmand.data.preparation.MapTileDownloader; import com.osmand.data.preparation.MapTileDownloader;
import com.osmand.data.preparation.MapTileDownloader.DownloadRequest; import com.osmand.data.preparation.MapTileDownloader.DownloadRequest;
@ -205,9 +204,8 @@ public class ResourceManager {
} }
} }
// //////////////////////////////////////////// Working with amenities // //////////////////////////////////////////// Working with amenities ////////////////////////////////////////////////
// //////////////////////////////////////////////// public List<Amenity> searchAmenities(PoiFilter filter, double latitude, double longitude, int zoom, int limit) {
public List<Amenity> searchAmenities(AmenityType type, double latitude, double longitude, int zoom, int limit) {
double tileNumberX = Math.floor(MapUtils.getTileNumberX(zoom, longitude)); double tileNumberX = Math.floor(MapUtils.getTileNumberX(zoom, longitude));
double tileNumberY = Math.floor(MapUtils.getTileNumberY(zoom, latitude)); double tileNumberY = Math.floor(MapUtils.getTileNumberY(zoom, latitude));
double topLatitude = MapUtils.getLatitudeFromTile(zoom, tileNumberY); double topLatitude = MapUtils.getLatitudeFromTile(zoom, tileNumberY);
@ -217,8 +215,8 @@ public class ResourceManager {
List<Amenity> amenities = new ArrayList<Amenity>(); List<Amenity> amenities = new ArrayList<Amenity>();
for (AmenityIndexRepository index : amenityRepositories) { for (AmenityIndexRepository index : amenityRepositories) {
if (index.checkContains(topLatitude, leftLongitude, bottomLatitude, rightLongitude)) { if (index.checkContains(topLatitude, leftLongitude, bottomLatitude, rightLongitude)) {
if (!index.checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, amenities)) { if (!index.checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, filter.getFilterId(), amenities)) {
index.searchAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, limit, type, amenities); index.searchAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, limit, filter, amenities);
} }
} }
} }
@ -226,12 +224,13 @@ public class ResourceManager {
return amenities; return amenities;
} }
public void searchAmenitiesAsync(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, List<Amenity> toFill){ public void searchAmenitiesAsync(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, PoiFilter filter, List<Amenity> toFill){
String filterId = filter == null ? null : filter.getFilterId();
for(AmenityIndexRepository index : amenityRepositories){ for(AmenityIndexRepository index : amenityRepositories){
if(index.checkContains(topLatitude, leftLongitude, bottomLatitude, rightLongitude)){ if(index.checkContains(topLatitude, leftLongitude, bottomLatitude, rightLongitude)){
if(!index.checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill, true)){ if(!index.checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, filterId, toFill, true)){
asyncLoadingTiles.requestToLoadAmenities( asyncLoadingTiles.requestToLoadAmenities(
new AmenityLoadRequest(index, topLatitude, leftLongitude, bottomLatitude, rightLongitude)); new AmenityLoadRequest(index, topLatitude, leftLongitude, bottomLatitude, rightLongitude, filter));
} }
} }
} }
@ -317,15 +316,17 @@ public class ResourceManager {
public final double bottomLatitude; public final double bottomLatitude;
public final double leftLongitude; public final double leftLongitude;
public final double rightLongitude; public final double rightLongitude;
public final PoiFilter filter;
public AmenityLoadRequest(AmenityIndexRepository repository, double topLatitude, double leftLongitude, public AmenityLoadRequest(AmenityIndexRepository repository, double topLatitude, double leftLongitude,
double bottomLatitude, double rightLongitude) { double bottomLatitude, double rightLongitude, PoiFilter filter) {
super(); super();
this.bottomLatitude = bottomLatitude; this.bottomLatitude = bottomLatitude;
this.leftLongitude = leftLongitude; this.leftLongitude = leftLongitude;
this.repository = repository; this.repository = repository;
this.rightLongitude = rightLongitude; this.rightLongitude = rightLongitude;
this.topLatitude = topLatitude; this.topLatitude = topLatitude;
this.filter = filter;
} }
@ -357,12 +358,12 @@ public class ResourceManager {
if(!amenityLoaded){ if(!amenityLoaded){
AmenityLoadRequest r = (AmenityLoadRequest) req; AmenityLoadRequest r = (AmenityLoadRequest) req;
r.repository.evaluateCachedAmenities(r.topLatitude, r.leftLongitude, r.repository.evaluateCachedAmenities(r.topLatitude, r.leftLongitude,
r.bottomLatitude, r.rightLongitude, null); r.bottomLatitude, r.rightLongitude, r.filter, null);
amenityLoaded = true; amenityLoaded = true;
} }
} }
} }
if(update){ if(update || amenityLoaded){
// use downloader callback // use downloader callback
for(IMapDownloaderCallback c : downloader.getDownloaderCallbacks()){ for(IMapDownloaderCallback c : downloader.getDownloaderCallbacks()){
c.tileDownloaded(null); c.tileDownloaded(null);

View file

@ -23,7 +23,7 @@ public class SearchActivity extends TabActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
TabHost host = getTabHost(); TabHost host = getTabHost();
host.addTab(host.newTabSpec("Search_POI").setIndicator("POI").setContent(new Intent(this, SearchPOIListActivity.class))); host.addTab(host.newTabSpec("Search_POI").setIndicator("POI").setContent(new Intent(this, SearchPoiFilterActivity.class)));
host.addTab(host.newTabSpec("Search_Address").setIndicator("Address").setContent(new Intent(this, SearchAddressActivity.class))); host.addTab(host.newTabSpec("Search_Address").setIndicator("Address").setContent(new Intent(this, SearchAddressActivity.class)));
host.addTab(host.newTabSpec("Search_Location").setIndicator("Location").setContent(new Intent(this, NavigatePointActivity.class))); host.addTab(host.newTabSpec("Search_Location").setIndicator("Location").setContent(new Intent(this, NavigatePointActivity.class)));
} }

View file

@ -21,13 +21,13 @@ import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.osmand.Algoritms;
import com.osmand.OsmandSettings; import com.osmand.OsmandSettings;
import com.osmand.PoiFilter;
import com.osmand.PoiFiltersHelper;
import com.osmand.R; import com.osmand.R;
import com.osmand.ResourceManager; import com.osmand.ResourceManager;
import com.osmand.activities.MapActivity; import com.osmand.activities.MapActivity;
import com.osmand.data.Amenity; import com.osmand.data.Amenity;
import com.osmand.data.AmenityType;
import com.osmand.osm.LatLon; import com.osmand.osm.LatLon;
import com.osmand.osm.MapUtils; import com.osmand.osm.MapUtils;
@ -37,7 +37,7 @@ import com.osmand.osm.MapUtils;
*/ */
public class SearchPOIActivity extends ListActivity { public class SearchPOIActivity extends ListActivity {
public static final String AMENITY_TYPE = "amenity_type"; public static final String AMENITY_FILTER = "amenity_filter";
private List<Amenity> amenityList; private List<Amenity> amenityList;
@ -47,7 +47,7 @@ public class SearchPOIActivity extends ListActivity {
private final static int limitOfClosest = 30; private final static int limitOfClosest = 30;
private int zoom = 13; private int zoom = 13;
private AmenityType amenityType; private PoiFilter filter;
private AmenityAdapter amenityAdapter; private AmenityAdapter amenityAdapter;
@ -65,7 +65,7 @@ public class SearchPOIActivity extends ListActivity {
if (zoom > finalZoom) { if (zoom > finalZoom) {
--zoom; --zoom;
} }
amenityList = resourceManager.searchAmenities(amenityType, lastKnownMapLocation.getLatitude(), lastKnownMapLocation amenityList = resourceManager.searchAmenities(filter, lastKnownMapLocation.getLatitude(), lastKnownMapLocation
.getLongitude(), zoom, -1); .getLongitude(), zoom, -1);
if (amenityList != null) { if (amenityList != null) {
MapUtils.sortListOfMapObject(amenityList, lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude()); MapUtils.sortListOfMapObject(amenityList, lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude());
@ -77,22 +77,18 @@ public class SearchPOIActivity extends ListActivity {
}); });
Bundle bundle = this.getIntent().getExtras(); Bundle bundle = this.getIntent().getExtras();
String anemity = bundle.getString(AMENITY_TYPE); String filterId = bundle.getString(AMENITY_FILTER);
if (anemity != null) { if (filterId != null) {
ResourceManager resourceManager = ResourceManager.getResourceManager(); ResourceManager resourceManager = ResourceManager.getResourceManager();
lastKnownMapLocation = OsmandSettings.getLastKnownMapLocation(this); lastKnownMapLocation = OsmandSettings.getLastKnownMapLocation(this);
amenityType = findAmenityType(anemity); filter = PoiFiltersHelper.getFilterById(this, filterId);
if (amenityType != null) { amenityList = resourceManager.searchAmenities(filter, lastKnownMapLocation.getLatitude(), lastKnownMapLocation
amenityList = resourceManager.searchAmenities(amenityType, lastKnownMapLocation.getLatitude(), lastKnownMapLocation
.getLongitude(), zoom, maxCount); .getLongitude(), zoom, maxCount);
} else {
amenityList = resourceManager.searchAmenities(amenityType, lastKnownMapLocation.getLatitude(), lastKnownMapLocation
.getLongitude(), zoom + 2, maxCount);
}
if (amenityList != null) { if (amenityList != null) {
MapUtils.sortListOfMapObject(amenityList, lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude()); MapUtils.sortListOfMapObject(amenityList, lastKnownMapLocation.getLatitude(), lastKnownMapLocation.getLongitude());
if(amenityType == null){ // TODO filter closest pois
if(filter.isStandardFilter()){
while (amenityList.size() > limitOfClosest) { while (amenityList.size() > limitOfClosest) {
amenityList.remove(amenityList.size() - 1); amenityList.remove(amenityList.size() - 1);
} }
@ -129,27 +125,16 @@ public class SearchPOIActivity extends ListActivity {
} }
} }
private AmenityType findAmenityType(String string) { class AmenityAdapter extends ArrayAdapter<Amenity> {
for (AmenityType type : AmenityType.values()) { AmenityAdapter(List<Amenity> list) {
if (string.equals(Algoritms.capitalizeFirstLetterAndLowercase(type.toString()))) { super(SearchPOIActivity.this, R.layout.searchpoi_list, list);
return type;
}
}
return null;
}
@SuppressWarnings("unchecked")
class AmenityAdapter extends ArrayAdapter {
AmenityAdapter(Object list) {
super(SearchPOIActivity.this, R.layout.searchpoi_list, (List<?>) list);
this.setNotifyOnChange(false); this.setNotifyOnChange(false);
} }
public void setNewModel(List<?> amenityList) { public void setNewModel(List<Amenity> amenityList) {
setNotifyOnChange(false); setNotifyOnChange(false);
((AmenityAdapter) getListAdapter()).clear(); ((AmenityAdapter) getListAdapter()).clear();
for (Object obj : amenityList) { for (Amenity obj : amenityList) {
this.add(obj); this.add(obj);
} }
this.notifyDataSetChanged(); this.notifyDataSetChanged();
@ -165,29 +150,20 @@ public class SearchPOIActivity extends ListActivity {
TextView label = (TextView) row.findViewById(R.id.poi_label); TextView label = (TextView) row.findViewById(R.id.poi_label);
TextView distanceLabel = (TextView) row.findViewById(R.id.poidistance_label); TextView distanceLabel = (TextView) row.findViewById(R.id.poidistance_label);
ImageView icon = (ImageView) row.findViewById(R.id.poi_icon); ImageView icon = (ImageView) row.findViewById(R.id.poi_icon);
Object model = getModel(position); Amenity amenity = getItem(position);
if (model instanceof Amenity) {
Amenity anemity = (Amenity) model;
if (anemity != null) {
LatLon lastKnownMapLocation = OsmandSettings.getLastKnownMapLocation(SearchPOIActivity.this); LatLon lastKnownMapLocation = OsmandSettings.getLastKnownMapLocation(SearchPOIActivity.this);
int dist = (int) (MapUtils.getDistance(anemity.getLocation(), lastKnownMapLocation.getLatitude(), lastKnownMapLocation int dist = (int) (MapUtils.getDistance(amenity.getLocation(), lastKnownMapLocation.getLatitude(), lastKnownMapLocation
.getLongitude())); .getLongitude()));
String str = anemity.getStringWithoutType(OsmandSettings.usingEnglishNames(SearchPOIActivity.this)); String str = amenity.getStringWithoutType(OsmandSettings.usingEnglishNames(SearchPOIActivity.this));
label.setText(str); label.setText(str);
if(anemity.getOpeningHours() != null) { if (amenity.getOpeningHours() != null) {
icon.setImageResource(R.drawable.poi); icon.setImageResource(R.drawable.poi);
} else { } else {
icon.setImageResource(R.drawable.closed_poi); icon.setImageResource(R.drawable.closed_poi);
} }
distanceLabel.setText(" " + dist + " m "); distanceLabel.setText(" " + dist + " m ");
}
}
return (row); return (row);
} }
private Object getModel(int position) {
return (((AmenityAdapter) getListAdapter()).getItem(position));
}
} }
} }

View file

@ -1,94 +0,0 @@
/**
*
*/
package com.osmand.activities.search;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.osmand.Algoritms;
import com.osmand.R;
import com.osmand.data.AmenityType;
/**
* @author Maxim Frolov
*
*/
public class SearchPOIListActivity extends ListActivity {
List<String> amenityList = new ArrayList<String>();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.searchpoilist);
createAmenityTypeList();
setListAdapter(new AmenityAdapter(amenityList));
}
private void createAmenityTypeList() {
amenityList.add(getResources().getString(R.string.Closest_Amenities));
for (AmenityType type : AmenityType.values()) {
amenityList.add(Algoritms.capitalizeFirstLetterAndLowercase(type.toString()));
}
}
public void onListItemClick(ListView parent, View v, int position, long id) {
AmenityType amenityType = findAmenityType(amenityList.get(position));
Bundle bundle = new Bundle();
Intent newIntent = new Intent(SearchPOIListActivity.this, SearchPOIActivity.class);
// folder selected
if (amenityType != null) {
bundle.putString(SearchPOIActivity.AMENITY_TYPE, amenityList.get(position));
} else {
bundle.putString(SearchPOIActivity.AMENITY_TYPE, "Closest_Amenities");
}
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
}
private AmenityType findAmenityType(String string) {
for (AmenityType type : AmenityType.values()) {
if (string.equals(Algoritms.capitalizeFirstLetterAndLowercase(type.toString()))) {
return type;
}
}
return null;
}
@SuppressWarnings("unchecked")
class AmenityAdapter extends ArrayAdapter {
AmenityAdapter(Object list) {
super(SearchPOIListActivity.this, R.layout.searchpoi_list, (List<?>) list);
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.searchpoifolder_list, parent, false);
TextView label = (TextView) row.findViewById(R.id.folder_label);
ImageView icon = (ImageView) row.findViewById(R.id.folder_icon);
Object model = getModel(position);
label.setText((String) model);
icon.setImageResource(R.drawable.folder);
return (row);
}
private Object getModel(int position) {
return (((AmenityAdapter) getListAdapter()).getItem(position));
}
}
}

View file

@ -0,0 +1,67 @@
/**
*
*/
package com.osmand.activities.search;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.osmand.PoiFilter;
import com.osmand.PoiFiltersHelper;
import com.osmand.R;
/**
* @author Maxim Frolov
*
*/
public class SearchPoiFilterActivity extends ListActivity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.searchpoilist);
List<PoiFilter> filters = PoiFiltersHelper.getOsmDefinedPoiFilters(this);
setListAdapter(new AmenityAdapter(filters));
}
public void onListItemClick(ListView parent, View v, int position, long id) {
PoiFilter filter = ((AmenityAdapter) getListAdapter()).getItem(position);
Bundle bundle = new Bundle();
Intent newIntent = new Intent(SearchPoiFilterActivity.this, SearchPOIActivity.class);
// folder selected
bundle.putString(SearchPOIActivity.AMENITY_FILTER, filter.getFilterId());
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
}
class AmenityAdapter extends ArrayAdapter<PoiFilter> {
AmenityAdapter(List<PoiFilter> list) {
super(SearchPoiFilterActivity.this, R.layout.searchpoi_list, list);
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.searchpoifolder_list, parent, false);
TextView label = (TextView) row.findViewById(R.id.folder_label);
ImageView icon = (ImageView) row.findViewById(R.id.folder_icon);
PoiFilter model = getItem(position);
label.setText(model.getName());
icon.setImageResource(R.drawable.folder);
return (row);
}
}
}

View file

@ -105,7 +105,7 @@ public class POIMapLayer implements OsmandMapLayer {
double rightLongitude = MapUtils.getLongitudeFromTile(view.getZoom(), tileRect.right); double rightLongitude = MapUtils.getLongitudeFromTile(view.getZoom(), tileRect.right);
objects.clear(); objects.clear();
resourceManager.searchAmenitiesAsync(topLatitude, leftLongitude, bottomLatitude, rightLongitude, objects); resourceManager.searchAmenitiesAsync(topLatitude, leftLongitude, bottomLatitude, rightLongitude, null, objects);
for (Amenity o : objects) { for (Amenity o : objects) {
int x = view.getMapXForPoint(o.getLocation().getLongitude()); int x = view.getMapXForPoint(o.getLocation().getLongitude());
int y = view.getMapYForPoint(o.getLocation().getLatitude()); int y = view.getMapYForPoint(o.getLocation().getLatitude());