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){
cachedAmenities.clear();
cTopLatitude = topLatitude + (topLatitude -bottomLatitude);
@ -78,12 +86,13 @@ public class AmenityIndexRepository {
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) {
return true;
}
if (cTopLatitude >= topLatitude && cLeftLongitude <= leftLongitude && cRightLongitude >= rightLongitude
&& cBottomLatitude <= bottomLatitude) {
boolean inside = cTopLatitude >= topLatitude && cLeftLongitude <= leftLongitude && cRightLongitude >= rightLongitude
&& cBottomLatitude <= bottomLatitude;
if((inside || fillFound) && toFill != null){
for(Amenity a : cachedAmenities){
LatLon location = a.getLocation();
if (location.getLatitude() <= topLatitude && location.getLongitude() >= leftLongitude && location.getLongitude() <= rightLongitude
@ -91,9 +100,11 @@ public class AmenityIndexRepository {
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) {

View file

@ -119,7 +119,7 @@ public class ResourceManager {
}
File en = new File(req.dirWithTiles, req.fileToLoad);
if (cacheOfImages.size() > maxImgCacheSize) {
onLowMemory();
clearTiles();
}
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){
for(AmenityIndexRepository index : amenityRepositories){
if(index.checkContains(topLatitude, leftLongitude, bottomLatitude, rightLongitude)){
if(!index.checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill)){
// TODO add request to another thread
index.evaluateCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill);
if(!index.checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill, true)){
asyncLoadingTiles.requestToLoadAmenities(
new AmenityLoadRequest(index, topLatitude, leftLongitude, bottomLatitude, rightLongitude));
}
}
}
}
////////////////////////////////////////////// Working with amenities ////////////////////////////////////////////////
/// On low memory method ///
public void onLowMemory() {
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());
// remove first images (as we think they are older)
for (int i = 0; i < list.size()/2; i ++) {
@ -219,15 +230,12 @@ public class ResourceManager {
bmp.recycle();
}
}
// TODO clear amenity indexes & addresses indexes
System.gc();
}
private static class TileLoadDownloadRequest extends DownloadRequest {
public final String fileToLoad;
@ -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 {
Stack<TileLoadDownloadRequest> requests = new Stack<TileLoadDownloadRequest>();
Stack<Object> requests = new Stack<Object>();
public AsyncLoadingThread(){
super("Async loading tiles");
super("Loader map objects (tiles, poi)");
}
@Override
@ -254,11 +283,22 @@ public class ResourceManager {
while(true){
try {
boolean update = false;
boolean amenityLoaded = false;
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) {
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){
// use downloader callback
@ -278,5 +318,8 @@ public class ResourceManager {
public void requestToLoadImage(TileLoadDownloadRequest req){
requests.push(req);
}
public void requestToLoadAmenities(AmenityLoadRequest req){
requests.push(req);
}
};
}