From c938d9eda472da7dec07698962b62388bf15e3a6 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 27 May 2010 12:52:31 +0000 Subject: [PATCH] load amenities async git-svn-id: https://osmand.googlecode.com/svn/trunk@85 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8 --- .../com/osmand/AmenityIndexRepository.java | 21 ++++-- OsmAnd/src/com/osmand/ResourceManager.java | 67 +++++++++++++++---- 2 files changed, 71 insertions(+), 17 deletions(-) diff --git a/OsmAnd/src/com/osmand/AmenityIndexRepository.java b/OsmAnd/src/com/osmand/AmenityIndexRepository.java index 5ee1bc59d2..2142891064 100644 --- a/OsmAnd/src/com/osmand/AmenityIndexRepository.java +++ b/OsmAnd/src/com/osmand/AmenityIndexRepository.java @@ -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 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 toFill){ + public boolean checkCachedAmenities(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, List 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 toFill){ + return checkCachedAmenities(topLatitude, leftLongitude, bottomLatitude, rightLongitude, toFill, false); } public void initialize(final IProgress progress, File file) { diff --git a/OsmAnd/src/com/osmand/ResourceManager.java b/OsmAnd/src/com/osmand/ResourceManager.java index de60a0012a..c308793ac6 100644 --- a/OsmAnd/src/com/osmand/ResourceManager.java +++ b/OsmAnd/src/com/osmand/ResourceManager.java @@ -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 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 list = new ArrayList(cacheOfImages.keySet()); // remove first images (as we think they are older) for (int i = 0; i < list.size()/2; i ++) { @@ -219,10 +230,7 @@ public class ResourceManager { 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 { - Stack requests = new Stack(); + Stack requests = new Stack(); 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); + } }; }