travel obf resource manager reader added

This commit is contained in:
simon 2020-12-09 14:55:35 +02:00
parent c949f98329
commit 16ae7b28c1
2 changed files with 132 additions and 112 deletions

View file

@ -19,8 +19,10 @@ import net.osmand.IndexConstants;
import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryIndexPart;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.BinaryMapIndexReader.SearchPoiTypeFilter;
import net.osmand.binary.BinaryMapPoiReaderAdapter;
import net.osmand.binary.CachedOsmandIndexes;
import net.osmand.data.Amenity;
import net.osmand.data.RotatedTileBox;
@ -75,7 +77,6 @@ import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import static net.osmand.IndexConstants.VOICE_INDEX_DIR;
/**
@ -83,7 +84,7 @@ import static net.osmand.IndexConstants.VOICE_INDEX_DIR;
* that could consume memory (especially with file resources).
* Such as indexes, tiles.
* Also it is responsible to create cache for that resources if they
* can't be loaded fully into memory & clear them on request.
* can't be loaded fully into memory & clear them on request.
*/
public class ResourceManager {
@ -94,7 +95,7 @@ public class ResourceManager {
protected static ResourceManager manager = null;
protected File dirWithTiles ;
protected File dirWithTiles;
private List<TilesCache> tilesCacheList = new ArrayList<>();
private BitmapTilesCache bitmapTilesCache;
@ -126,10 +127,11 @@ public class ResourceManager {
private List<BinaryMapIndexReader> readers = new ArrayList<>(BinaryMapReaderResourceType.values().length);
private boolean useForRouting;
private boolean useForPublicTransport;
public BinaryMapReaderResource(File f, BinaryMapIndexReader initialReader) {
this.filename = f;
this.initialReader = initialReader;
while(readers.size() < BinaryMapReaderResourceType.values().length) {
while (readers.size() < BinaryMapReaderResourceType.values().length) {
readers.add(null);
}
}
@ -207,8 +209,8 @@ public class ResourceManager {
private final Map<String, RegionAddressRepository> addressMap = new ConcurrentHashMap<String, RegionAddressRepository>();
protected final Map<String, AmenityIndexRepository> amenityRepositories = new ConcurrentHashMap<String, AmenityIndexRepository>();
// protected final Map<String, BinaryMapIndexReader> routingMapFiles = new ConcurrentHashMap<String, BinaryMapIndexReader>();
protected final Map<String, AmenityIndexRepository> amenityRepositories = new ConcurrentHashMap<String, AmenityIndexRepository>();
// protected final Map<String, BinaryMapIndexReader> routingMapFiles = new ConcurrentHashMap<String, BinaryMapIndexReader>();
protected final Map<String, BinaryMapReaderResource> transportRepositories = new ConcurrentHashMap<String, BinaryMapReaderResource>();
protected final Map<String, String> indexFileNames = new ConcurrentHashMap<String, String>();
@ -293,7 +295,7 @@ public class ResourceManager {
// ".nomedia" indicates there are no pictures and no music to list in this dir for the Gallery app
try {
context.getAppPath(".nomedia").createNewFile(); //$NON-NLS-1$
} catch( Exception e ) {
} catch (Exception e) {
}
for (TilesCache tilesCache : tilesCacheList) {
tilesCache.setDirWithTiles(dirWithTiles);
@ -323,7 +325,7 @@ public class ResourceManager {
return null;
}
public synchronized void tileDownloaded(DownloadRequest request){
public synchronized void tileDownloaded(DownloadRequest request) {
if (request instanceof TileLoadDownloadRequest) {
TileLoadDownloadRequest req = ((TileLoadDownloadRequest) request);
TilesCache cache = getTilesCache(req.tileSource);
@ -338,7 +340,7 @@ public class ResourceManager {
return cache != null && cache.tileExistOnFileSystem(file, map, x, y, zoom);
}
public void clearTileForMap(String file, ITileSource map, int x, int y, int zoom){
public void clearTileForMap(String file, ITileSource map, int x, int y, int zoom) {
TilesCache cache = getTilesCache(map);
if (cache != null) {
cache.getTileForMap(file, map, x, y, zoom, true, false, true, true);
@ -376,7 +378,7 @@ public class ResourceManager {
////////////////////////////////////////////// Working with indexes ////////////////////////////////////////////////
public List<String> reloadIndexesOnStart(AppInitializer progress, List<String> warnings){
public List<String> reloadIndexesOnStart(AppInitializer progress, List<String> warnings) {
close();
// check we have some assets to copy to sdcard
warnings.addAll(checkAssets(progress, false));
@ -429,7 +431,7 @@ public class ResourceManager {
return warnings;
}
public List<String> indexFontFiles(IProgress progress){
public List<String> indexFontFiles(IProgress progress) {
File file = context.getAppPath(IndexConstants.FONT_INDEX_DIR);
file.mkdirs();
List<String> warnings = new ArrayList<String>();
@ -476,7 +478,7 @@ public class ResourceManager {
public List<String> checkAssets(IProgress progress, boolean forceUpdate) {
String fv = Version.getFullVersion(context);
if(context.getAppInitializer().isAppVersionChanged()) {
if (context.getAppInitializer().isAppVersionChanged()) {
copyMissingJSAssets();
}
if (!fv.equalsIgnoreCase(context.getSettings().PREVIOUS_INSTALLED_VERSION.get()) || forceUpdate) {
@ -540,6 +542,7 @@ public class ResourceManager {
private final static String ASSET_COPY_MODE__overwriteOnlyIfExists = "overwriteOnlyIfExists";
private final static String ASSET_COPY_MODE__alwaysOverwriteOrCopy = "alwaysOverwriteOrCopy";
private final static String ASSET_COPY_MODE__copyOnlyIfDoesNotExist = "copyOnlyIfDoesNotExist";
private void unpackBundledAssets(AssetManager assetManager, File appDataDir, IProgress progress, boolean isFirstInstall) throws IOException, XmlPullParserException {
List<AssetEntry> assetEntries = DownloadOsmandIndexesHelper.getBundledAssets(assetManager);
for (AssetEntry asset : assetEntries) {
@ -581,7 +584,7 @@ public class ResourceManager {
}
public static void copyAssets(AssetManager assetManager, String assetName, File file) throws IOException {
if(file.exists()){
if (file.exists()) {
Algorithms.removeAllFiles(file);
}
file.getParentFile().mkdirs();
@ -593,9 +596,9 @@ public class ResourceManager {
}
private List<File> collectFiles(File dir, String ext, List<File> files) {
if(dir.exists() && dir.canRead()) {
if (dir.exists() && dir.canRead()) {
File[] lf = dir.listFiles();
if(lf == null || lf.length == 0) {
if (lf == null || lf.length == 0) {
return files;
}
for (File f : lf) {
@ -608,10 +611,9 @@ public class ResourceManager {
}
private void renameRoadsFiles(ArrayList<File> files, File roadsPath) {
Iterator<File> it = files.iterator();
while(it.hasNext()) {
while (it.hasNext()) {
File f = it.next();
if (f.getName().endsWith("-roads" + IndexConstants.BINARY_MAP_INDEX_EXT)) {
f.renameTo(new File(roadsPath, f.getName().replace("-roads" + IndexConstants.BINARY_MAP_INDEX_EXT,
@ -634,6 +636,7 @@ public class ResourceManager {
collectFiles(roadsPath, IndexConstants.BINARY_MAP_INDEX_EXT, files);
if (Version.isPaidVersion(context)) {
collectFiles(context.getAppPath(IndexConstants.WIKI_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files);
collectFiles(context.getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files);
}
if (OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) != null || InAppPurchaseHelper.isSubscribedToLiveUpdates(context)) {
collectFiles(context.getAppPath(IndexConstants.SRTM_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files);
@ -707,9 +710,9 @@ public class ResourceManager {
if (dateCreated == 0) {
dateCreated = f.lastModified();
}
if(f.getParentFile().getName().equals(liveDir.getName())) {
if (f.getParentFile().getName().equals(liveDir.getName())) {
boolean toUse = changesManager.index(f, dateCreated, mapReader);
if(!toUse) {
if (!toUse) {
try {
mapReader.close();
} catch (IOException e) {
@ -717,7 +720,7 @@ public class ResourceManager {
}
continue;
}
} else if(!wikiMap && !srtmMap) {
} else if (!wikiMap && !srtmMap) {
changesManager.indexMainMap(f, dateCreated);
}
indexFileNames.put(f.getName(), dateFormat.format(dateCreated)); //$NON-NLS-1$
@ -779,7 +782,7 @@ public class ResourceManager {
}
}
Iterator<Entry<PoiCategory, Map<String, PoiType>>> it = toAddPoiTypes.entrySet().iterator();
while(it.hasNext()) {
while (it.hasNext()) {
Entry<PoiCategory, Map<String, PoiType>> next = it.next();
PoiCategory category = next.getKey();
category.addExtraPoiTypes(next.getValue());
@ -799,7 +802,6 @@ public class ResourceManager {
}
public void initMapBoundariesCacheNative() {
File indCache = context.getAppPath(INDEXES_CACHE);
if (indCache.exists()) {
@ -826,7 +828,7 @@ public class ResourceManager {
}
public List<Amenity> searchAmenities(SearchPoiTypeFilter filter,
double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom, final ResultMatcher<Amenity> matcher) {
double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom, final ResultMatcher<Amenity> matcher) {
final List<Amenity> amenities = new ArrayList<Amenity>();
searchAmenitiesInProgress = true;
try {
@ -855,8 +857,8 @@ public class ResourceManager {
return amenities;
}
public List<Amenity> searchAmenitiesOnThePath(List<Location> locations, double radius, SearchPoiTypeFilter filter,
ResultMatcher<Amenity> matcher) {
public List<Amenity> searchAmenitiesOnThePath(List<Location> locations, double radius, SearchPoiTypeFilter filter,
ResultMatcher<Amenity> matcher) {
searchAmenitiesInProgress = true;
final List<Amenity> amenities = new ArrayList<Amenity>();
try {
@ -885,7 +887,7 @@ public class ResourceManager {
if (!repos.isEmpty()) {
for (AmenityIndexRepository r : repos) {
List<Amenity> res = r.searchAmenitiesOnThePath(locations, radius, filter, matcher);
if(res != null) {
if (res != null) {
amenities.addAll(res);
}
}
@ -899,10 +901,10 @@ public class ResourceManager {
}
public boolean containsAmenityRepositoryToSearch(boolean searchByName){
public boolean containsAmenityRepositoryToSearch(boolean searchByName) {
for (AmenityIndexRepository index : getAmenityRepositories()) {
if(searchByName){
if(index instanceof AmenityIndexRepositoryBinary){
if (searchByName) {
if (index instanceof AmenityIndexRepositoryBinary) {
return true;
}
} else {
@ -913,8 +915,8 @@ public class ResourceManager {
}
public List<Amenity> searchAmenitiesByName(String searchQuery,
double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude,
double lat, double lon, ResultMatcher<Amenity> matcher) {
double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude,
double lat, double lon, ResultMatcher<Amenity> matcher) {
List<Amenity> amenities = new ArrayList<Amenity>();
List<AmenityIndexRepositoryBinary> list = new ArrayList<AmenityIndexRepositoryBinary>();
int left = MapUtils.get31TileNumberX(leftLongitude);
@ -927,7 +929,7 @@ public class ResourceManager {
}
if (index instanceof AmenityIndexRepositoryBinary) {
if (index.checkContainsInt(top, left, bottom, right)) {
if(index.checkContains(lat, lon)){
if (index.checkContains(lat, lon)) {
list.add(0, (AmenityIndexRepositoryBinary) index);
} else {
list.add((AmenityIndexRepositoryBinary) index);
@ -973,11 +975,11 @@ public class ResourceManager {
////////////////////////////////////////////// Working with address ///////////////////////////////////////////
public RegionAddressRepository getRegionRepository(String name){
public RegionAddressRepository getRegionRepository(String name) {
return addressMap.get(name);
}
public Collection<RegionAddressRepository> getAddressRepositories(){
public Collection<RegionAddressRepository> getAddressRepositories() {
return addressMap.values();
}
@ -1013,7 +1015,7 @@ public class ResourceManager {
public List<TransportStop> searchTransportSync(double topLat, double leftLon, double bottomLat, double rightLon,
ResultMatcher<TransportStop> matcher) throws IOException {
ResultMatcher<TransportStop> matcher) throws IOException {
TransportStopsRouteReader readers =
new TransportStopsRouteReader(getTransportRepositories(topLat, leftLon, bottomLat, rightLon));
List<TransportStop> stops = new ArrayList<>();
@ -1030,7 +1032,7 @@ public class ResourceManager {
public List<TransportRoute> getRoutesForStop(TransportStop stop) {
List<TransportRoute> rts = stop.getRoutes();
if(rts != null) {
if (rts != null) {
return rts;
}
return Collections.emptyList();
@ -1041,12 +1043,12 @@ public class ResourceManager {
return renderer.updateMapIsNeeded(rotatedTileBox, drawSettings);
}
public void updateRendererMap(RotatedTileBox rotatedTileBox, OnMapLoadedListener mapLoadedListener){
public void updateRendererMap(RotatedTileBox rotatedTileBox, OnMapLoadedListener mapLoadedListener) {
renderer.interruptLoadingMap();
asyncLoadingThread.requestToLoadMap(new MapLoadRequest(rotatedTileBox, mapLoadedListener));
}
public void interruptRendering(){
public void interruptRendering() {
renderer.interruptLoadingMap();
}
@ -1067,12 +1069,12 @@ public class ResourceManager {
indexFileNames.remove(fileName);
renderer.closeConnection(fileName);
BinaryMapReaderResource resource = fileReaders.remove(fileName);
if(resource != null) {
if (resource != null) {
resource.close();
}
}
public synchronized void close(){
public synchronized void close() {
for (TilesCache tc : tilesCacheList) {
tc.close();
}
@ -1082,7 +1084,7 @@ public class ResourceManager {
transportRepositories.clear();
addressMap.clear();
amenityRepositories.clear();
for(BinaryMapReaderResource res : fileReaders.values()) {
for (BinaryMapReaderResource res : fileReaders.values()) {
res.close();
}
fileReaders.clear();
@ -1107,7 +1109,7 @@ public class ResourceManager {
Collection<BinaryMapReaderResource> fileReaders = getFileReaders();
List<BinaryMapIndexReader> readers = new ArrayList<>(fileReaders.size());
for (BinaryMapReaderResource r : fileReaders) {
if (r.isUseForPublicTransport()) {
if (r.isUseForPublicTransport()) {
BinaryMapIndexReader reader = r.getReader(BinaryMapReaderResourceType.TRANSPORT_ROUTING);
if (reader != null) {
readers.add(reader);
@ -1132,12 +1134,29 @@ public class ResourceManager {
return readers.toArray(new BinaryMapIndexReader[0]);
}
public BinaryMapIndexReader[] getTravelFiles() {
Collection<BinaryMapReaderResource> fileReaders = getFileReaders();
List<BinaryMapIndexReader> readers = new ArrayList<>(fileReaders.size());
for (BinaryMapReaderResource res : fileReaders) {
if (!res.filename.toString().toLowerCase().contains("wiki")) {
continue;
}
BinaryMapIndexReader index = res.getReader(BinaryMapReaderResourceType.POI);
for (BinaryIndexPart p : index.getIndexes()) {
if (p instanceof BinaryMapPoiReaderAdapter.PoiRegion) {
readers.add(index);
}
}
}
return readers.toArray(new BinaryMapIndexReader[0]);
}
public Map<String, String> getIndexFileNames() {
return new LinkedHashMap<String, String>(indexFileNames);
}
public boolean containsBasemap(){
public boolean containsBasemap() {
return !basemapFileNames.isEmpty();
}

View file

@ -85,23 +85,23 @@ public class TravelObfHelper implements TravelHelper {
* 2. check settings for default?
*/
public void initTravelBooks() {
List<File> files = getPossibleFiles();
BinaryMapIndexReader[] readers = application.getResourceManager().getTravelFiles();
String travelBook = application.getSettings().SELECTED_TRAVEL_BOOK.get();
existingTravelBooks.clear();
if (files != null && !files.isEmpty()) {
for (File f : files) {
if (readers != null) {
for (BinaryMapIndexReader reader : readers) {
File f = reader.getFile();
existingTravelBooks.add(f);
if (selectedTravelBook == null) {
selectedTravelBook = f;
} else if (Algorithms.objectEquals(travelBook, f.getName())) {
selectedTravelBook = f;
}
selectedTravelBook = reader.getFile();
}
selectedTravelBook = files.get(0);
} else {
selectedTravelBook = null;
}
}
/**
@ -147,6 +147,7 @@ public class TravelObfHelper implements TravelHelper {
@NonNull
@Override
public List<WikivoyageSearchResult> search(String searchQuery) {
List<WikivoyageSearchResult> res = new ArrayList<>();
CollatorStringMatcher matcher = new CollatorStringMatcher(searchQuery,
CollatorStringMatcher.StringMatcherMode.CHECK_STARTS_FROM_SPACE);