From a62cb50e131715f89580b49750a8eb30dde07b53 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 2 May 2011 21:25:00 +0200 Subject: [PATCH] Remove support for deprecated address/transport odb indexes --- OsmAnd/.classpath | 2 +- .../plus/DownloadOsmandIndexesHelper.java | 10 - .../plus/RegionAddressRepositoryOdb.java | 524 ------------------ .../src/net/osmand/plus/ResourceManager.java | 69 +-- .../plus/TransportIndexRepositoryOdb.java | 315 ----------- .../activities/DownloadIndexActivity.java | 39 +- 6 files changed, 5 insertions(+), 954 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/RegionAddressRepositoryOdb.java delete mode 100644 OsmAnd/src/net/osmand/plus/TransportIndexRepositoryOdb.java diff --git a/OsmAnd/.classpath b/OsmAnd/.classpath index 9d8c50188e..79924d7430 100644 --- a/OsmAnd/.classpath +++ b/OsmAnd/.classpath @@ -1,7 +1,7 @@ - + diff --git a/OsmAnd/src/net/osmand/plus/DownloadOsmandIndexesHelper.java b/OsmAnd/src/net/osmand/plus/DownloadOsmandIndexesHelper.java index c118922e42..c9f0bd023a 100644 --- a/OsmAnd/src/net/osmand/plus/DownloadOsmandIndexesHelper.java +++ b/OsmAnd/src/net/osmand/plus/DownloadOsmandIndexesHelper.java @@ -97,11 +97,6 @@ public class DownloadOsmandIndexesHelper { String s = ""; //$NON-NLS-1$ if (fileName.endsWith(IndexConstants.POI_INDEX_EXT) || fileName.endsWith(IndexConstants.POI_INDEX_EXT_ZIP)) { s = ctx.getString(R.string.poi); - } else if (fileName.endsWith(IndexConstants.ADDRESS_INDEX_EXT) || fileName.endsWith(IndexConstants.ADDRESS_INDEX_EXT_ZIP)) { - s = ctx.getString(R.string.address); - } else if (fileName.endsWith(IndexConstants.TRANSPORT_INDEX_EXT) - || fileName.endsWith(IndexConstants.TRANSPORT_INDEX_EXT_ZIP)) { - s = ctx.getString(R.string.transport); } else if (fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT) || fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)) { boolean f = true; @@ -148,11 +143,6 @@ public class DownloadOsmandIndexesHelper { public boolean isAccepted(){ if (fileName.endsWith(IndexConstants.POI_INDEX_EXT) || fileName.endsWith(IndexConstants.POI_INDEX_EXT_ZIP)) { return true; - } else if (fileName.endsWith(IndexConstants.ADDRESS_INDEX_EXT) || fileName.endsWith(IndexConstants.ADDRESS_INDEX_EXT_ZIP)) { - return true; - } else if (fileName.endsWith(IndexConstants.TRANSPORT_INDEX_EXT) - || fileName.endsWith(IndexConstants.TRANSPORT_INDEX_EXT_ZIP)) { - return true; } else if (fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT) || fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)) { return true; diff --git a/OsmAnd/src/net/osmand/plus/RegionAddressRepositoryOdb.java b/OsmAnd/src/net/osmand/plus/RegionAddressRepositoryOdb.java deleted file mode 100644 index e214319f97..0000000000 --- a/OsmAnd/src/net/osmand/plus/RegionAddressRepositoryOdb.java +++ /dev/null @@ -1,524 +0,0 @@ -package net.osmand.plus; - -import java.io.File; -import java.text.Collator; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.EnumSet; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; -import java.util.TreeSet; - -import net.osmand.Algoritms; -import net.osmand.IProgress; -import net.osmand.LogUtil; -import net.osmand.data.Building; -import net.osmand.data.City; -import net.osmand.data.MapObject; -import net.osmand.data.MapObjectComparator; -import net.osmand.data.PostCode; -import net.osmand.data.Street; -import net.osmand.data.City.CityType; -import net.osmand.data.index.IndexConstants; -import net.osmand.osm.LatLon; -import net.osmand.osm.Node; -import net.osmand.osm.Way; - -import org.apache.commons.logging.Log; - -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; - - -public class RegionAddressRepositoryOdb implements RegionAddressRepository { - private static final Log log = LogUtil.getLog(RegionAddressRepositoryOdb.class); - private SQLiteDatabase db; - private String name; - private LinkedHashMap cities = new LinkedHashMap(); - - private Map> cityTypes = new HashMap>(); - private Map postCodes = new TreeMap(Collator.getInstance()); - - private boolean useEnglishNames = false; - - - public boolean initialize(final IProgress progress, File file) { - long start = System.currentTimeMillis(); - if(db != null){ - // close previous db - db.close(); - } - db = SQLiteDatabase.openOrCreateDatabase(file, null); - // add * as old format - name = file.getName().substring(0, file.getName().indexOf('.'))+" *"; //$NON-NLS-1$ - if(db.getVersion() != IndexConstants.ADDRESS_TABLE_VERSION){ - db.close(); - db = null; - return false; - } - - if (log.isDebugEnabled()) { - log.debug("Initializing address db " + file.getAbsolutePath() + " " + (System.currentTimeMillis() - start) + " ms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - } - return true; - } - - public void close(){ - clearCities(); - if(db != null){ - db.close(); - } - } - - public String getName() { - return name; - } - - public void clearCities(){ - cities.clear(); - cityTypes.clear(); - postCodes.clear(); - } - - public boolean areCitiesPreloaded(){ - return !cities.isEmpty(); - } - public boolean arePostcodesPreloaded(){ - return !postCodes.isEmpty(); - } - - public PostCode getPostcode(String name){ - if(name == null){ - return null; - } - preloadPostcodes(); - return postCodes.get(name.toUpperCase()); - } - - public City getCityById(Long id){ - if(id == -1){ - // do not preload cities for that case - return null; - } - preloadCities(); - return cities.get(id); - } - - - public Street getStreetByName(MapObject city, String name) { - preloadStreets(city); - if (city instanceof City) { - return ((City) city).getStreet(name); - } else { - return ((PostCode) city).getStreet(name); - } - } - - public Building getBuildingByName(Street street, String name){ - if(street.getBuildings().isEmpty()){ - preloadBuildings(street); - } - for(Building b : street.getBuildings()){ - String bName = useEnglishNames ? b.getEnName() : b.getName(); - if(bName.equals(name)){ - return b; - } - } - return null; - } - - public boolean useEnglishNames(){ - return useEnglishNames; - } - - public void setUseEnglishNames(boolean useEnglishNames) { - this.useEnglishNames = useEnglishNames; - // sort streets - for (City c : cities.values()) { - if (!c.isEmptyWithStreets()) { - ArrayList list = new ArrayList(c.getStreets()); - c.removeAllStreets(); - for (Street s : list) { - c.registerStreet(s, useEnglishNames); - } - } - } - // sort cities - ArrayList list = new ArrayList(cities.values()); - Collections.sort(list, new MapObjectComparator(useEnglishNames)); - cities.clear(); - cityTypes.clear(); - for(City c : list){ - addCityToPreloadedList(c); - } - } - - public void fillWithSuggestedBuildings(PostCode postcode, Street street, String name, List buildingsToFill){ - preloadBuildings(street); - name = name.toLowerCase(); - int ind = 0; - boolean empty = name.length() == 0; - if(empty && postcode == null){ - buildingsToFill.addAll(street.getBuildings()); - return; - } - for (Building building : street.getBuildings()) { - if(postcode != null && !postcode.getName().equals(building.getPostcode())){ - continue; - } else if(empty){ - buildingsToFill.add(building); - continue; - } - String bName = useEnglishNames ? building.getEnName() : building.getName(); - String lowerCase = bName.toLowerCase(); - if (lowerCase.startsWith(name)) { - buildingsToFill.add(ind, building); - ind++; - } else if (lowerCase.contains(name)) { - buildingsToFill.add(building); - } - } - } - - public void fillWithSuggestedStreetsIntersectStreets(City city, Street st, List streetsToFill) { - if (st != null) { - Set strIds = new TreeSet(); - log.debug("Start loading instersection streets for " + city.getName()); //$NON-NLS-1$ - Cursor query = db.rawQuery("SELECT B.STREET FROM street_node A JOIN street_node B ON A.ID = B.ID WHERE A.STREET = ?", //$NON-NLS-1$ - new String[] { st.getId() + "" }); //$NON-NLS-1$ - if (query.moveToFirst()) { - do { - if (st.getId() != query.getLong(0)) { - strIds.add(query.getLong(0)); - } - } while (query.moveToNext()); - } - query.close(); - for (Street s : city.getStreets()) { - if (strIds.contains(s.getId())) { - streetsToFill.add(s); - } - } - log.debug("Loaded " + strIds.size() + " streets"); //$NON-NLS-1$ //$NON-NLS-2$ - preloadWayNodes(st); - } - } - - - public void fillWithSuggestedStreets(MapObject o, String name, List streetsToFill){ - assert o instanceof PostCode || o instanceof City; - City city = (City) (o instanceof City ? o : null); - PostCode post = (PostCode) (o instanceof PostCode ? o : null); - preloadStreets(o); - name = name.toLowerCase(); - - Collection streets = post == null ? city.getStreets() : post.getStreets() ; - int ind = 0; - if(name.length() == 0){ - streetsToFill.addAll(streets); - return; - } - ind = 0; - for (Street s : streets) { - String sName = useEnglishNames ? s.getEnName() : s.getName(); - String lowerCase = sName.toLowerCase(); - if (lowerCase.startsWith(name)) { - streetsToFill.add(ind, s); - ind++; - } else if (lowerCase.contains(name)) { - streetsToFill.add(s); - } - } - } - - public void fillWithSuggestedCities(String name, List citiesToFill, LatLon currentLocation){ - preloadCities(); - // essentially index is created that cities towns are first in cities map - int ind = 0; - if (name.length() >= 2 && - Character.isDigit(name.charAt(0)) && - Character.isDigit(name.charAt(1))) { - preloadPostcodes(); - // also try to identify postcodes - String uName = name.toUpperCase(); - for (String code : postCodes.keySet()) { - if (code.startsWith(uName)) { - citiesToFill.add(ind++, postCodes.get(code)); - } else if(code.contains(uName)){ - citiesToFill.add(postCodes.get(code)); - } - } - - } - if(name.length() < 3){ - EnumSet set = EnumSet.of(CityType.CITY, CityType.TOWN); - for(CityType t : set){ - List list = cityTypes.get(t); - if(list == null){ - continue; - } - if(name.length() == 0){ - citiesToFill.addAll(list); - } else { - name = name.toLowerCase(); - for (City c : list) { - String cName = useEnglishNames ? c.getEnName() : c.getName(); - String lowerCase = cName.toLowerCase(); - if(lowerCase.startsWith(name)){ - citiesToFill.add(c); - } - } - } - } - } else { - name = name.toLowerCase(); - Collection src = cities.values(); - for (City c : src) { - String cName = useEnglishNames ? c.getEnName() : c.getName(); - String lowerCase = cName.toLowerCase(); - if (lowerCase.startsWith(name)) { - citiesToFill.add(ind, c); - ind++; - } else if (lowerCase.contains(name)) { - citiesToFill.add(c); - } - } - int initialsize = citiesToFill.size(); - log.debug("Start loading cities for " +getName() + " filter " + name); //$NON-NLS-1$ //$NON-NLS-2$ - // lower function in SQLite requires ICU extension - name = Algoritms.capitalizeFirstLetterAndLowercase(name); - int i = name.indexOf('\''); - if(i != -1){ - // SQL quotation - name = name.replace("'", "''"); //$NON-NLS-1$ //$NON-NLS-2$ - } - StringBuilder where = new StringBuilder(80); - where. - append("city_type not in ("). //$NON-NLS-1$ - append('\'').append(CityType.valueToString(CityType.CITY)).append('\'').append(", "). //$NON-NLS-1$ - append('\'').append(CityType.valueToString(CityType.TOWN)).append('\'').append(") and "). //$NON-NLS-1$ - append(useEnglishNames ? "name_en" : "name").append(" LIKE '"+name+"%'"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ - Cursor query = db.query("city", new String[]{"id","latitude", "longitude", "name", "name_en", "city_type"}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ - where.toString(), null, null, null, null); - if (query.moveToFirst()) { - List hamlets = new ArrayList(); - do { - hamlets.add(parseCityFromCursor(query)); - } while (query.moveToNext()); - Collections.sort(hamlets, new MapObjectNameDistanceComparator(useEnglishNames, currentLocation)); - citiesToFill.addAll(hamlets); - } - query.close(); - - - - log.debug("Loaded citites " + (citiesToFill.size() - initialsize)); //$NON-NLS-1$ - } - } - - public void preloadWayNodes(Street street){ - if(street.getWayNodes().isEmpty()){ - Cursor query = db.query("street_node", new String[]{"id", "latitude", "longitude", "street", "way"}, "? = street", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ - new String[] { street.getId() + "" }, null, null, null); //$NON-NLS-1$ - log.debug("Start loading waynodes for " + street.getName()); //$NON-NLS-1$ - Map ways = new LinkedHashMap(); - if (query.moveToFirst()) { - do { - Node n = new Node(query.getDouble(1), - query.getDouble(2), - query.getLong(0)); - long way = query.getLong(4); - if(!ways.containsKey(way)){ - ways.put(way, new Way(way)); - } - ways.get(way).addNode(n); - } while (query.moveToNext()); - } - query.close(); - for(Way w : ways.values()){ - street.getWayNodes().add(w); - } - log.debug("Loaded " + ways.size() + " ways"); //$NON-NLS-1$ //$NON-NLS-2$ - } - - } - - public void preloadPostcodes() { - if (postCodes.isEmpty()) { - // check if it possible to load postcodes - Cursor query = db.query(true, "building", new String[] { "postcode" }, null, //$NON-NLS-1$//$NON-NLS-2$ - null, null, null, null, null); - log.debug("Start loading postcodes for "); //$NON-NLS-1$ - if (query.moveToFirst()) { - do { - String postcode = query.getString(0); - if (postcode != null) { - postCodes.put(postcode, new PostCode(postcode)); - } - } while (query.moveToNext()); - } - query.close(); - log.debug("Loaded " + postCodes.size() + " postcodes "); //$NON-NLS-1$ //$NON-NLS-2$ - } - } - - public void preloadBuildings(Street street){ - if (street.getBuildings().isEmpty()) { - Cursor query = db.query("building", //$NON-NLS-1$ - new String[]{"id","latitude", "longitude", "name", "name_en", "street", "postcode"} //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ - , "? = street", //$NON-NLS-1$ - new String[] { street.getId() + "" }, null, null, null); //$NON-NLS-1$ - log.debug("Start loading buildings for " + street.getName()); //$NON-NLS-1$ - if (query.moveToFirst()) { - do { - Building building = new Building(); - building.setId(query.getLong(0)); - building.setLocation(query.getDouble(1), query.getDouble(2)); - building.setName(query.getString(3)); - building.setEnName(query.getString(4)); - building.setPostcode(query.getString(6)); - street.registerBuilding(building); - } while (query.moveToNext()); - street.sortBuildings(); - } - query.close(); - log.debug("Loaded " + street.getBuildings().size() + " buildings"); //$NON-NLS-1$ //$NON-NLS-2$ - } - } - - public void preloadStreets(MapObject o){ - assert o instanceof PostCode || o instanceof City; - City city = (City) (o instanceof City ? o : null); - PostCode post = (PostCode) (o instanceof PostCode ? o : null); - - if (city != null && city.isEmptyWithStreets()) { - log.debug("Start loading streets for " + city.getName()); //$NON-NLS-1$ - Cursor query = db.query("street", new String[]{"id","latitude", "longitude", "name", "name_en", "city"}, "? = city", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ - new String[] { city.getId() + "" }, null, null, null); //$NON-NLS-1$ - if (query.moveToFirst()) { - do { - Street street = new Street(city); - street.setId(query.getLong(0)); - street.setLocation(query.getDouble(1), query.getDouble(2)); - street.setName(query.getString(3)); - street.setEnName(query.getString(4)); - city.registerStreet(street, useEnglishNames); - } while (query.moveToNext()); - } - query.close(); - log.debug("Loaded " + city.getStreets().size() + " streets"); //$NON-NLS-1$ //$NON-NLS-2$ - } else if(post != null && post.isEmptyWithStreets()){ - log.debug("Start loading streets for " + post.getName()); //$NON-NLS-1$ - Cursor query = db.rawQuery("SELECT B.CITY, B.ID,B.LATITUDE, B.LONGITUDE, B.NAME, B.NAME_EN FROM building A JOIN street B ON A.street = B.ID WHERE A.postcode = ?", //$NON-NLS-1$ - new String[] { post.getName() + "" }); //$NON-NLS-1$ - if (query.moveToFirst()) { - do { - city = getCityById(query.getLong(0)); - Street street = null; - if(city != null){ - preloadStreets(city); - street = city.getStreet(useEnglishNames ? query.getString(5) : query.getString(4)); - } - if(street == null){ - street = new Street(city); - street.setId(query.getLong(1)); - street.setLocation(query.getDouble(2), query.getDouble(3)); - street.setName(query.getString(4)); - street.setEnName(query.getString(5)); - if(city != null){ - city.registerStreet(street, useEnglishNames); - } - } - post.registerStreet(street, useEnglishNames); - } while (query.moveToNext()); - } - query.close(); - log.debug("Loaded " +post.getStreets().size() + " streets"); //$NON-NLS-1$ //$NON-NLS-2$ - } - } - - public void addCityToPreloadedList(City city){ - cities.put(city.getId(), city); - - if(!cityTypes.containsKey(city.getType())){ - cityTypes.put(city.getType(), new ArrayList()); - } - cityTypes.get(city.getType()).add(city); - } - - protected City parseCityFromCursor(Cursor query){ - CityType type = CityType.valueFromString(query.getString(5)); - if (type != null) { - City city = new City(type); - city.setId(query.getLong(0)); - city.setLocation(query.getDouble(1), query.getDouble(2)); - city.setName(query.getString(3)); - city.setEnName(query.getString(4)); - return city; - } - return null; - } - - public void preloadCities(){ - if (cities.isEmpty()) { - log.debug("Start loading cities for " +getName()); //$NON-NLS-1$ - StringBuilder where = new StringBuilder(); - where.append("city_type="). //$NON-NLS-1$ - append('\'').append(CityType.valueToString(CityType.CITY)).append('\'').append(" or "). //$NON-NLS-1$ - append("city_type="). //$NON-NLS-1$ - append('\'').append(CityType.valueToString(CityType.TOWN)).append('\''); - Cursor query = db.query("city", new String[]{"id","latitude", "longitude", "name", "name_en", "city_type"}, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ - where.toString(), null, null, null, null); - if(query.moveToFirst()){ - do { - City city = parseCityFromCursor(query); - if (city != null) { - cities.put(city.getId(), city); - - if(!cityTypes.containsKey(city.getType())){ - cityTypes.put(city.getType(), new ArrayList()); - } - cityTypes.get(city.getType()).add(city); - } - - } while(query.moveToNext()); - } - log.debug("Loaded " + cities.size() + " cities"); //$NON-NLS-1$ //$NON-NLS-2$ - query.close(); - } - } - - @Override - public boolean isMapRepository() { - return false; - } - - @Override - public void clearCache() { - clearCities(); - - } - - @Override - public LatLon findStreetIntersection(Street street, Street street2) { - preloadWayNodes(street2); - preloadWayNodes(street); - for(Way w : street2.getWayNodes()){ - for(Way w2 : street.getWayNodes()){ - for(Node n : w.getNodes()){ - for(Node n2 : w2.getNodes()){ - if(n.getId() == n2.getId()){ - return n.getLatLon(); - } - } - } - } - } - return null; - } -} diff --git a/OsmAnd/src/net/osmand/plus/ResourceManager.java b/OsmAnd/src/net/osmand/plus/ResourceManager.java index 58a909bc4f..62aef122ff 100644 --- a/OsmAnd/src/net/osmand/plus/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/ResourceManager.java @@ -44,7 +44,7 @@ import android.graphics.BitmapFactory; * 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. - * + *SQLITE */ public class ResourceManager { @@ -52,8 +52,6 @@ public class ResourceManager { public static final String POI_PATH = APP_DIR + IndexConstants.POI_INDEX_DIR; public static final String VOICE_PATH = APP_DIR + IndexConstants.VOICE_INDEX_DIR; public static final String MAPS_PATH = APP_DIR; - public static final String ADDRESS_PATH = APP_DIR + IndexConstants.ADDRESS_INDEX_DIR; - public static final String TRANSPORT_PATH = APP_DIR + IndexConstants.TRANSPORT_INDEX_DIR; public static final String TILES_PATH = APP_DIR+"tiles/"; //$NON-NLS-1$ public static final String TEMP_SOURCE_TO_LOAD = "temp"; //$NON-NLS-1$ public static final String VECTOR_MAP = "#vector_map"; //$NON-NLS-1$ @@ -348,8 +346,6 @@ public class ResourceManager { // indexingImageTiles(progress); List warnings = new ArrayList(); warnings.addAll(indexingPoi(progress)); - warnings.addAll(indexingAddresses(progress)); - warnings.addAll(indexingTransport(progress)); warnings.addAll(indexingMaps(progress)); return warnings; } @@ -472,69 +468,6 @@ public class ResourceManager { } } - - public List indexingAddresses(final IProgress progress){ - File file = OsmandSettings.extendOsmandPath(context, ADDRESS_PATH); - List warnings = new ArrayList(); - closeAddresses(); - if (file.exists() && file.canRead()) { - for (File f : file.listFiles()) { - indexingAddress(progress, warnings, f); - } - } - return warnings; - } - - public void indexingAddress(final IProgress progress, List warnings, File f) { - if (f.getName().endsWith(IndexConstants.ADDRESS_INDEX_EXT)) { - RegionAddressRepositoryOdb repository = new RegionAddressRepositoryOdb(); - progress.startTask(context.getString(R.string.indexing_address) + " " + f.getName(), -1); //$NON-NLS-1$ - try { - boolean initialized = repository.initialize(progress, f); - if (initialized) { - addressMap.put(repository.getName(), repository); - indexFileNames.put(f.getName(), MessageFormat.format("{0,date,dd.MM.yyyy}", new Date(f.lastModified()))); //$NON-NLS-1$ - } else { - warnings.add(MessageFormat.format(context.getString(R.string.version_index_is_not_supported), f.getName())); //$NON-NLS-1$ - } - } catch (SQLiteException e) { - log.error("Exception reading " + f.getAbsolutePath(), e); //$NON-NLS-1$ - warnings.add(MessageFormat.format(context.getString(R.string.version_index_is_not_supported), f.getName())); //$NON-NLS-1$ - } - } - } - - - public List indexingTransport(final IProgress progress){ - File file = OsmandSettings.extendOsmandPath(context, TRANSPORT_PATH); - List warnings = new ArrayList(); - closeTransport(); - if (file.exists() && file.canRead()) { - for (File f : file.listFiles()) { - indexingTransport(progress, warnings, f); - } - } - return warnings; - } - - public void indexingTransport(final IProgress progress, List warnings, File f) { - if (f.getName().endsWith(IndexConstants.TRANSPORT_INDEX_EXT)) { - TransportIndexRepositoryOdb repository = new TransportIndexRepositoryOdb(); - progress.startTask(context.getString(R.string.indexing_transport) + " " + f.getName(), -1); //$NON-NLS-1$ - try { - boolean initialized = repository.initialize(progress, f); - if (initialized) { - transportRepositories.add(repository); - indexFileNames.put(f.getName(), MessageFormat.format("{0,date,dd.MM.yyyy}", new Date(f.lastModified()))); //$NON-NLS-1$ - } else { - warnings.add(MessageFormat.format(context.getString(R.string.version_index_is_not_supported), f.getName())); //$NON-NLS-1$ - } - } catch (SQLiteException e) { - log.error("Exception reading " + f.getAbsolutePath(), e); //$NON-NLS-1$ - warnings.add(MessageFormat.format(context.getString(R.string.version_index_is_not_supported), f.getName())); //$NON-NLS-1$ - } - } - } ////////////////////////////////////////////// Working with amenities //////////////////////////////////////////////// public List searchAmenityRepositories(double latitude, double longitude) { diff --git a/OsmAnd/src/net/osmand/plus/TransportIndexRepositoryOdb.java b/OsmAnd/src/net/osmand/plus/TransportIndexRepositoryOdb.java deleted file mode 100644 index 779aa0cc46..0000000000 --- a/OsmAnd/src/net/osmand/plus/TransportIndexRepositoryOdb.java +++ /dev/null @@ -1,315 +0,0 @@ -package net.osmand.plus; - -import java.io.File; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import net.osmand.IProgress; -import net.osmand.LogUtil; -import net.osmand.data.TransportRoute; -import net.osmand.data.TransportStop; -import net.osmand.data.index.IndexConstants; -import net.osmand.osm.LatLon; -import net.osmand.osm.MapUtils; - -import org.apache.commons.logging.Log; - -import android.database.Cursor; - -public class TransportIndexRepositoryOdb extends BaseLocationIndexRepository implements TransportIndexRepository { - private static final Log log = LogUtil.getLog(TransportIndexRepositoryOdb.class); - - private final static String TRANSPORT_STOP_TABLE = IndexConstants.TRANSPORT_STOP_TABLE; - private final static String TRANSPORT_ROUTE_STOP_TABLE = IndexConstants.TRANSPORT_ROUTE_STOP_TABLE; - private final static String TRANSPORT_ROUTE_TABLE = IndexConstants.TRANSPORT_ROUTE_TABLE; - - public boolean initialize(final IProgress progress, File file) { - return super.initialize(progress, file, IndexConstants.TRANSPORT_TABLE_VERSION, TRANSPORT_STOP_TABLE, false); - } - - private final String[] columns = new String[]{"id", "latitude", "longitude", "name", "name_en"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ - public List searchTransportStops(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int limit, List stops){ - long now = System.currentTimeMillis(); - String squery = "? < latitude AND latitude < ? AND ? < longitude AND longitude < ?"; //$NON-NLS-1$ - - if(limit != -1){ - squery += " ORDER BY RANDOM() LIMIT " +limit; //$NON-NLS-1$ - } - Cursor query = db.query(TRANSPORT_STOP_TABLE, columns, squery, - new String[]{Double.toString(bottomLatitude), - Double.toString(topLatitude), Double.toString(leftLongitude), Double.toString(rightLongitude)}, null, null, null); - if(query.moveToFirst()){ - do { - TransportStop st = new TransportStop(); - st.setId(query.getLong(0)); - st.setLocation(query.getDouble(1), - query.getDouble(2)); - st.setName(query.getString(3 )); - st.setEnName(query.getString(4)); - stops.add(st); - if(limit != -1 && stops.size() >= limit){ - break; - } - } while(query.moveToNext()); - } - query.close(); - - if (log.isDebugEnabled()) { - log.debug(String.format("Search for %s done in %s ms found %s.", //$NON-NLS-1$ - topLatitude + " " + leftLongitude, System.currentTimeMillis() - now, stops.size())); //$NON-NLS-1$ - } - return stops; - } - - - - private static String cacheSQLRouteDescriptions = null; - /** - * - * @param stop - * @param format {0} - ref, {1} - type, {2} - name, {3} - name_en - * @return - */ - public List getRouteDescriptionsForStop(TransportStop stop, String format) { - long now = System.currentTimeMillis(); - List res = new ArrayList(); - MessageFormat f = new MessageFormat(format); - - if (cacheSQLRouteDescriptions == null) { - StringBuilder sql = new StringBuilder(200); - sql.append("SELECT DISTINCT ref, type, name, name_en FROM ").append(TRANSPORT_ROUTE_TABLE).append(" JOIN ").append(TRANSPORT_ROUTE_STOP_TABLE); //$NON-NLS-1$ //$NON-NLS-2$ - sql.append(" ON transport_route.id = transport_route_stop.route WHERE transport_route_stop.stop = ?"); //$NON-NLS-1$ - cacheSQLRouteDescriptions = sql.toString(); - } - Cursor query = db.rawQuery(cacheSQLRouteDescriptions, new String[] { stop.getId() + "" }); //$NON-NLS-1$ - if (query.moveToFirst()) { - do { - res.add(f.format(new String[] { query.getString(0), query.getString(1), query.getString(2), query.getString(3) })); - } while (query.moveToNext()); - } - query.close(); - - if (log.isDebugEnabled()) { - log.debug(String.format("Search for stop %s done in %s ms found %s.", //$NON-NLS-1$ - stop.getId() + "", System.currentTimeMillis() - now, res.size())); //$NON-NLS-1$ - } - return res; - } - - - public void evaluateCachedTransportStops(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude, int zoom, int limit, List toFill){ - cTopLatitude = topLatitude + (topLatitude -bottomLatitude); - cBottomLatitude = bottomLatitude - (topLatitude -bottomLatitude); - cLeftLongitude = leftLongitude - (rightLongitude - leftLongitude); - cRightLongitude = rightLongitude + (rightLongitude - leftLongitude); - cZoom = zoom; - // first of all put all entities in temp list in order to not freeze other read threads - ArrayList tempList = new ArrayList(); - searchTransportStops(cTopLatitude, cLeftLongitude, cBottomLatitude, cRightLongitude, limit, tempList); - synchronized (this) { - cachedObjects.clear(); - cachedObjects.addAll(tempList); - } - - checkCachedObjects(topLatitude, leftLongitude, bottomLatitude, rightLongitude, cZoom, toFill); - } - - - private static String cacheSQLRoutes = null; - public List searchTransportRouteStops(double latitude, double longitude, LatLon locationToGo, int zoom) { - long now = System.currentTimeMillis(); - LatLon loc = new LatLon(latitude, longitude); - double tileNumberX = MapUtils.getTileNumberX(zoom, longitude); - double tileNumberY = MapUtils.getTileNumberY(zoom, latitude); - double topLatitude = MapUtils.getLatitudeFromTile(zoom, tileNumberY - 0.5); - double bottomLatitude = MapUtils.getLatitudeFromTile(zoom, tileNumberY + 0.5); - double leftLongitude = MapUtils.getLongitudeFromTile(zoom, tileNumberX - 0.5); - double rightLongitude = MapUtils.getLongitudeFromTile(zoom, tileNumberX + 0.5); - if(cacheSQLRoutes == null){ - StringBuilder sql = new StringBuilder(200); - sql.append("SELECT R.id, R.dist, R.name, R.name_en, R.ref, R.operator, R.type, "); //$NON-NLS-1$ - sql.append("T.id, T.name, T.name_en, T.latitude, T.longitude, TR.direction "); //$NON-NLS-1$ - sql.append(" FROM ").append(TRANSPORT_STOP_TABLE).append(" T "); //$NON-NLS-1$ //$NON-NLS-2$ - // join with stops table - sql.append(" JOIN ").append(TRANSPORT_ROUTE_STOP_TABLE).append(" TR "); //$NON-NLS-1$ //$NON-NLS-2$ - sql.append(" ON T.id = TR.stop "); //$NON-NLS-1$ - // join with route table - sql.append(" JOIN ").append(TRANSPORT_ROUTE_TABLE).append(" R "); //$NON-NLS-1$ //$NON-NLS-2$ - sql.append(" ON R.id = TR.route "); //$NON-NLS-1$ - sql.append(" WHERE ").append("? < latitude AND latitude < ? AND ? < longitude AND longitude < ?"); //$NON-NLS-1$ //$NON-NLS-2$ - cacheSQLRoutes = sql.toString(); - } - Cursor query = db.rawQuery(cacheSQLRoutes, - new String[] {bottomLatitude + "" , topLatitude + "" , leftLongitude + "" , rightLongitude + "" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ - - Map registeredRoutes = new LinkedHashMap(); - if (query.moveToFirst()) { - do { - TransportRoute route = new TransportRoute(); - route.setId(query.getLong(0)); - route.setDistance(query.getInt(1)); - route.setName(query.getString(2)); - route.setEnName(query.getString(3)); - route.setRef(query.getString(4)); - route.setOperator(query.getString(5)); - route.setType(query.getString(6)); - TransportStop s = new TransportStop(); - s.setId(query.getLong(7)); - s.setName(query.getString(8)); - s.setEnName(query.getString(9)); - s.setLocation(query.getDouble(10), query.getDouble(11)); - boolean direction = query.getInt(12) > 0; - long idToPut = route.getId() << 1 + (direction ? 1 : 0); - if(registeredRoutes.containsKey(idToPut)){ - TransportStop st = registeredRoutes.get(idToPut).getStart(); - if(MapUtils.getDistance(loc, st.getLocation()) < MapUtils.getDistance(loc, s.getLocation())){ - continue; - } - } - RouteInfoLocation r = new RouteInfoLocation(); - r.setRoute(route); - r.setStart(s); - r.setDirection(direction); - registeredRoutes.put(idToPut, r); - - - } while (query.moveToNext()); - } - query.close(); - - if (log.isDebugEnabled()) { - log.debug(String.format("Search for routes done in %s ms found %s.", //$NON-NLS-1$ - System.currentTimeMillis() - now, registeredRoutes.size())); - } - - List list = preloadRouteStopsAndCalculateDistance(loc, locationToGo, registeredRoutes); - return list; - - } - - @Override - public boolean acceptTransportStop(TransportStop stop) { - return checkContains(stop.getLocation().getLatitude(), stop.getLocation().getLongitude()); - } - - protected List preloadRouteStopsAndCalculateDistance(final LatLon loc, LatLon locationToGo, - Map registeredRoutes) { - if(registeredRoutes.isEmpty()){ - return Collections.emptyList(); - } - long now = System.currentTimeMillis(); - StringBuilder sql = new StringBuilder(200); - sql.append("SELECT T.id, T.latitude, T.longitude, T.name, T.name_en, "); //$NON-NLS-1$ - sql.append(" TR.route, TR.direction " ); //$NON-NLS-1$ - sql.append(" FROM ").append(TRANSPORT_STOP_TABLE).append(" T "); //$NON-NLS-1$ //$NON-NLS-2$ - // join with stops table - sql.append(" JOIN ").append(TRANSPORT_ROUTE_STOP_TABLE).append(" TR "); //$NON-NLS-1$ //$NON-NLS-2$ - sql.append(" ON T.id = TR.stop "); //$NON-NLS-1$ - - sql.append(" WHERE "); //$NON-NLS-1$ - boolean f = true; - for (RouteInfoLocation il : registeredRoutes.values()) { - if (f) { - f = false; - } else { - sql.append(" OR "); //$NON-NLS-1$ - } - sql.append("(TR.route"); //$NON-NLS-1$ - sql.append(" = ").append(il.getRoute().getId()); //$NON-NLS-1$ - sql.append(" AND TR.direction"); //$NON-NLS-1$ - sql.append(" = ").append(il.getDirection() ? 1 : 0); //$NON-NLS-1$ - sql.append(")"); //$NON-NLS-1$ - } - sql.append(" ORDER BY TR.ord asc"); //$NON-NLS-1$ - - - Map distanceToLoc = new LinkedHashMap(); - - Cursor query = db.rawQuery(sql.toString(), new String[] {}); - if (query.moveToFirst()) { - // load only part of the route - do { - TransportStop st = null; - - long routeId = query.getLong(5); - int direction = query.getInt(6); - long id = routeId << 1 + direction; - boolean found = distanceToLoc.containsKey(id); - RouteInfoLocation i = registeredRoutes.get(id); - if (found) { - st = new TransportStop(); - st.setId(query.getLong(0)); - st.setLocation(query.getDouble(1), query.getDouble(2)); - st.setName(query.getString(3)); - st.setEnName(query.getString(4)); - } else if (query.getLong(0) == i.getStart().getId()) { - st = i.getStart(); - found = true; - distanceToLoc.put(id, st); - } - - if (found) { - if (locationToGo != null) { - double d = MapUtils.getDistance(locationToGo, st.getLocation()); - double dbase = MapUtils.getDistance(locationToGo, distanceToLoc.get(id).getLocation()); - if (d < dbase) { - distanceToLoc.put(id, st); - } - } - if (i.getDirection()) { - i.getRoute().getForwardStops().add(st); - } else { - i.getRoute().getBackwardStops().add(st); - } - } - - } while (query.moveToNext()); - query.close(); - - } - - if (locationToGo != null) { - for (Long l : registeredRoutes.keySet()) { - Integer dist = (int) MapUtils.getDistance(locationToGo, distanceToLoc.get(l).getLocation()); - if (dist != null) { - registeredRoutes.get(l).setDistToLocation(dist); - } - registeredRoutes.get(l).setStop(distanceToLoc.get(l)); - } - } - - ArrayList listRoutes = new ArrayList(registeredRoutes.values()); - if (log.isDebugEnabled()) { - log.debug(String.format("Loading routes done in %s ms for %s routes.", //$NON-NLS-1$ - System.currentTimeMillis() - now, listRoutes.size())); - } - - if (locationToGo != null) { - Collections.sort(listRoutes, new Comparator() { - @Override - public int compare(RouteInfoLocation object1, RouteInfoLocation object2) { - int x = (int) (MapUtils.getDistance(loc, object1.getStart().getLocation()) + object1.getDistToLocation()); - int y = (int) (MapUtils.getDistance(loc, object2.getStart().getLocation()) + object2.getDistToLocation()); - return x - y; - } - - }); - } else { - Collections.sort(listRoutes, new Comparator() { - @Override - public int compare(RouteInfoLocation object1, RouteInfoLocation object2) { - return Double.compare(MapUtils.getDistance(loc, object1.getStart().getLocation()), MapUtils.getDistance(loc, object2 - .getStart().getLocation())); - } - - }); - } - return listRoutes; - } - -} diff --git a/OsmAnd/src/net/osmand/plus/activities/DownloadIndexActivity.java b/OsmAnd/src/net/osmand/plus/activities/DownloadIndexActivity.java index 0c9d635b57..ce9ebe409f 100644 --- a/OsmAnd/src/net/osmand/plus/activities/DownloadIndexActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/DownloadIndexActivity.java @@ -1,17 +1,11 @@ package net.osmand.plus.activities; -import static net.osmand.data.index.IndexConstants.ADDRESS_INDEX_EXT; -import static net.osmand.data.index.IndexConstants.ADDRESS_INDEX_EXT_ZIP; -import static net.osmand.data.index.IndexConstants.ADDRESS_TABLE_VERSION; import static net.osmand.data.index.IndexConstants.BINARY_MAP_INDEX_EXT; import static net.osmand.data.index.IndexConstants.BINARY_MAP_INDEX_EXT_ZIP; import static net.osmand.data.index.IndexConstants.BINARY_MAP_VERSION; import static net.osmand.data.index.IndexConstants.POI_INDEX_EXT; import static net.osmand.data.index.IndexConstants.POI_INDEX_EXT_ZIP; import static net.osmand.data.index.IndexConstants.POI_TABLE_VERSION; -import static net.osmand.data.index.IndexConstants.TRANSPORT_INDEX_EXT; -import static net.osmand.data.index.IndexConstants.TRANSPORT_INDEX_EXT_ZIP; -import static net.osmand.data.index.IndexConstants.TRANSPORT_TABLE_VERSION; import static net.osmand.data.index.IndexConstants.VOICE_INDEX_EXT_ZIP; import static net.osmand.data.index.IndexConstants.VOICE_VERSION; @@ -312,13 +306,10 @@ public class DownloadIndexActivity extends ListActivity { } - private List listAlreadyDownloadedWithAlternatives() - { + private List listAlreadyDownloadedWithAlternatives() { List files = new ArrayList(); File externalStorageDirectory = OsmandSettings.getExternalStorageDirectory(getApplicationContext()); - files.addAll(listWithAlternatives(new File(externalStorageDirectory, ResourceManager.ADDRESS_PATH),ADDRESS_INDEX_EXT,ADDRESS_INDEX_EXT_ZIP,ADDRESS_TABLE_VERSION)); files.addAll(listWithAlternatives(new File(externalStorageDirectory, ResourceManager.POI_PATH),POI_INDEX_EXT,POI_INDEX_EXT_ZIP,POI_TABLE_VERSION)); - files.addAll(listWithAlternatives(new File(externalStorageDirectory, ResourceManager.TRANSPORT_PATH),TRANSPORT_INDEX_EXT,TRANSPORT_INDEX_EXT_ZIP,TRANSPORT_TABLE_VERSION)); files.addAll(listWithAlternatives(new File(externalStorageDirectory, ResourceManager.APP_DIR),BINARY_MAP_INDEX_EXT,BINARY_MAP_INDEX_EXT_ZIP,BINARY_MAP_VERSION)); files.addAll(listWithAlternatives(new File(externalStorageDirectory, ResourceManager.VOICE_PATH),"",VOICE_INDEX_EXT_ZIP, VOICE_VERSION)); return files; @@ -353,15 +344,7 @@ public class DownloadIndexActivity extends ListActivity { boolean unzipDir = false; File externalStorageDirectory = OsmandSettings.getExternalStorageDirectory(getApplicationContext()); - if(fileName.endsWith(ADDRESS_INDEX_EXT)){ - parent = new File(externalStorageDirectory, ResourceManager.ADDRESS_PATH); - toSavePostfix = ADDRESS_INDEX_EXT; - toCheckPostfix = ADDRESS_INDEX_EXT; - } else if(fileName.endsWith(IndexConstants.ADDRESS_INDEX_EXT_ZIP)){ - parent = new File(externalStorageDirectory, ResourceManager.ADDRESS_PATH); - toSavePostfix = ADDRESS_INDEX_EXT_ZIP; - toCheckPostfix = ADDRESS_INDEX_EXT; - } else if(fileName.endsWith(IndexConstants.POI_INDEX_EXT)){ + if(fileName.endsWith(IndexConstants.POI_INDEX_EXT)){ parent = new File(externalStorageDirectory, ResourceManager.POI_PATH); toSavePostfix = POI_INDEX_EXT; toCheckPostfix = POI_INDEX_EXT; @@ -369,14 +352,6 @@ public class DownloadIndexActivity extends ListActivity { parent = new File(externalStorageDirectory, ResourceManager.POI_PATH); toSavePostfix = POI_INDEX_EXT_ZIP; toCheckPostfix = POI_INDEX_EXT; - } else if(fileName.endsWith(IndexConstants.TRANSPORT_INDEX_EXT)){ - parent = new File(externalStorageDirectory, ResourceManager.TRANSPORT_PATH); - toSavePostfix = TRANSPORT_INDEX_EXT; - toCheckPostfix = TRANSPORT_INDEX_EXT; - } else if(fileName.endsWith(IndexConstants.TRANSPORT_INDEX_EXT_ZIP)){ - parent = new File(externalStorageDirectory, ResourceManager.TRANSPORT_PATH); - toSavePostfix = TRANSPORT_INDEX_EXT_ZIP; - toCheckPostfix = TRANSPORT_INDEX_EXT; } else if(fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)){ parent = new File(externalStorageDirectory, ResourceManager.APP_DIR); toSavePostfix = BINARY_MAP_INDEX_EXT; @@ -664,12 +639,8 @@ public class DownloadIndexActivity extends ListActivity { if(dateModified != null){ toIndex.setLastModified(dateModified); } - if (toIndex.getName().endsWith(IndexConstants.ADDRESS_INDEX_EXT)) { - manager.indexingAddress(progress, warnings, toIndex); - } else if (toIndex.getName().endsWith(IndexConstants.POI_INDEX_EXT)) { + if (toIndex.getName().endsWith(IndexConstants.POI_INDEX_EXT)) { manager.indexingPoi(progress, warnings, toIndex); - } else if (toIndex.getName().endsWith(IndexConstants.TRANSPORT_INDEX_EXT)) { - manager.indexingTransport(progress, warnings, toIndex); } else if (toIndex.getName().endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) { warnings.addAll(manager.indexingMaps(progress)); } else if (toIndex.getName().endsWith(IndexConstants.VOICE_INDEX_EXT_ZIP)) { @@ -718,10 +689,6 @@ public class DownloadIndexActivity extends ListActivity { String s; if(name.endsWith(IndexConstants.POI_INDEX_EXT) || name.endsWith(IndexConstants.POI_INDEX_EXT_ZIP)){ s = IndexConstants.POI_INDEX_EXT; - } else if(name.endsWith(IndexConstants.ADDRESS_INDEX_EXT) || name.endsWith(IndexConstants.ADDRESS_INDEX_EXT_ZIP)){ - s = IndexConstants.ADDRESS_INDEX_EXT; - } else if(name.endsWith(IndexConstants.TRANSPORT_INDEX_EXT) || name.endsWith(IndexConstants.TRANSPORT_INDEX_EXT_ZIP)){ - s = IndexConstants.TRANSPORT_INDEX_EXT; } else if(name.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT) || name.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)){ s = IndexConstants.BINARY_MAP_INDEX_EXT; } else {