load amenities async

git-svn-id: https://osmand.googlecode.com/svn/trunk@85 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-05-27 12:52:31 +00:00
parent 4b7408da3f
commit c938d9eda4
2 changed files with 71 additions and 17 deletions

View file

@ -68,6 +68,14 @@ public class AmenityIndexRepository {
} }
public void clearCache(){
cachedAmenities.clear();
cTopLatitude = 0;
cBottomLatitude = 0;
cRightLongitude = 0;
cLeftLongitude = 0;
}
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, List<Amenity> toFill){
cachedAmenities.clear(); cachedAmenities.clear();
cTopLatitude = topLatitude + (topLatitude -bottomLatitude); cTopLatitude = topLatitude + (topLatitude -bottomLatitude);
@ -78,12 +86,13 @@ public class AmenityIndexRepository {
checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill); checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill);
} }
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, List<Amenity> toFill, boolean fillFound){
if (db == null) { if (db == null) {
return true; return true;
} }
if (cTopLatitude >= topLatitude && cLeftLongitude <= leftLongitude && cRightLongitude >= rightLongitude boolean inside = cTopLatitude >= topLatitude && cLeftLongitude <= leftLongitude && cRightLongitude >= rightLongitude
&& cBottomLatitude <= bottomLatitude) { && cBottomLatitude <= bottomLatitude;
if((inside || fillFound) && toFill != null){
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
@ -91,9 +100,11 @@ public class AmenityIndexRepository {
toFill.add(a); toFill.add(a);
} }
} }
return true;
} }
return false; return inside;
}
public boolean checkCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, List<Amenity> toFill){
return checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill, false);
} }
public void initialize(final IProgress progress, File file) { public void initialize(final IProgress progress, File file) {

View file

@ -119,7 +119,7 @@ public class ResourceManager {
} }
File en = new File(req.dirWithTiles, req.fileToLoad); File en = new File(req.dirWithTiles, req.fileToLoad);
if (cacheOfImages.size() > maxImgCacheSize) { if (cacheOfImages.size() > maxImgCacheSize) {
onLowMemory(); clearTiles();
} }
if (!downloader.isFileCurrentlyDownloaded(en) && req.dirWithTiles.canRead()) { if (!downloader.isFileCurrentlyDownloaded(en) && req.dirWithTiles.canRead()) {
@ -198,19 +198,30 @@ public class ResourceManager {
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, List<Amenity> toFill){
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)){ if(!index.checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill, true)){
// TODO add request to another thread asyncLoadingTiles.requestToLoadAmenities(
index.evaluateCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill); new AmenityLoadRequest(index, topLatitude, leftLongitude, bottomLatitude, rightLongitude));
} }
} }
} }
} }
////////////////////////////////////////////// Working with amenities ////////////////////////////////////////////////
/// On low memory method /// /// On low memory method ///
public void onLowMemory() { public void onLowMemory() {
log.info("On low memory : cleaning tiles - size = " + cacheOfImages.size()); log.info("On low memory : cleaning tiles - size = " + cacheOfImages.size());
clearTiles();
for(AmenityIndexRepository r : amenityRepositories){
r.clearCache();
}
// TODO clear addresses indexes
System.gc();
}
protected void clearTiles(){
ArrayList<String> list = new ArrayList<String>(cacheOfImages.keySet()); ArrayList<String> list = new ArrayList<String>(cacheOfImages.keySet());
// remove first images (as we think they are older) // remove first images (as we think they are older)
for (int i = 0; i < list.size()/2; i ++) { for (int i = 0; i < list.size()/2; i ++) {
@ -219,10 +230,7 @@ public class ResourceManager {
bmp.recycle(); bmp.recycle();
} }
} }
// TODO clear amenity indexes & addresses indexes }
System.gc();
}
@ -241,12 +249,33 @@ public class ResourceManager {
} }
} }
private static class AmenityLoadRequest {
public final AmenityIndexRepository repository;
public final double topLatitude;
public final double bottomLatitude;
public final double leftLongitude;
public final double rightLongitude;
public AmenityLoadRequest(AmenityIndexRepository repository, double topLatitude, double leftLongitude,
double bottomLatitude, double rightLongitude) {
super();
this.bottomLatitude = bottomLatitude;
this.leftLongitude = leftLongitude;
this.repository = repository;
this.rightLongitude = rightLongitude;
this.topLatitude = topLatitude;
}
}
public class AsyncLoadingThread extends Thread { public class AsyncLoadingThread extends Thread {
Stack<TileLoadDownloadRequest> requests = new Stack<TileLoadDownloadRequest>(); Stack<Object> requests = new Stack<Object>();
public AsyncLoadingThread(){ public AsyncLoadingThread(){
super("Async loading tiles"); super("Loader map objects (tiles, poi)");
} }
@Override @Override
@ -254,11 +283,22 @@ public class ResourceManager {
while(true){ while(true){
try { try {
boolean update = false; boolean update = false;
boolean amenityLoaded = false;
while(!requests.isEmpty()){ while(!requests.isEmpty()){
TileLoadDownloadRequest r = requests.pop(); Object req = requests.pop();
if(req instanceof TileLoadDownloadRequest){
TileLoadDownloadRequest r = (TileLoadDownloadRequest) req;
if(cacheOfImages.get(r.fileToLoad) == null) { if(cacheOfImages.get(r.fileToLoad) == null) {
update |= getRequestedImageTile(r) != null; update |= getRequestedImageTile(r) != null;
} }
} else if(req instanceof AmenityLoadRequest){
if(!amenityLoaded){
AmenityLoadRequest r = (AmenityLoadRequest) req;
r.repository.evaluateCachedAmenities(r.topLatitude, r.leftLongitude,
r.bottomLatitude, r.rightLongitude, null);
amenityLoaded = true;
}
}
} }
if(update){ if(update){
// use downloader callback // use downloader callback
@ -278,5 +318,8 @@ public class ResourceManager {
public void requestToLoadImage(TileLoadDownloadRequest req){ public void requestToLoadImage(TileLoadDownloadRequest req){
requests.push(req); requests.push(req);
} }
public void requestToLoadAmenities(AmenityLoadRequest req){
requests.push(req);
}
}; };
} }