diff --git a/OsmAnd-java/src/main/java/net/osmand/map/ITileSource.java b/OsmAnd-java/src/main/java/net/osmand/map/ITileSource.java index 6d91b00545..503467b93b 100644 --- a/OsmAnd-java/src/main/java/net/osmand/map/ITileSource.java +++ b/OsmAnd-java/src/main/java/net/osmand/map/ITileSource.java @@ -34,4 +34,15 @@ public interface ITileSource { public void deleteTiles(String path); + public int getAvgSize(); + + public String getRule(); + + public String getRandoms(); + + public boolean isInvertedYTile(); + + public boolean isTimeSupported(); + + public boolean getInversiveZoom(); } diff --git a/OsmAnd-java/src/main/java/net/osmand/map/TileSourceManager.java b/OsmAnd-java/src/main/java/net/osmand/map/TileSourceManager.java index 5fd510db61..38e8307abb 100644 --- a/OsmAnd-java/src/main/java/net/osmand/map/TileSourceManager.java +++ b/OsmAnd-java/src/main/java/net/osmand/map/TileSourceManager.java @@ -178,6 +178,16 @@ public class TileSourceManager { return invertedYTile; } + @Override + public boolean isTimeSupported() { + return expirationTimeMillis != -1; + } + + @Override + public boolean getInversiveZoom() { + return false; + } + public void setInvertedYTile(boolean invertedYTile) { this.invertedYTile = invertedYTile; } @@ -414,6 +424,11 @@ public class TileSourceManager { } } } + + @Override + public int getAvgSize() { + return this.avgSize; + } } private static Map readMetaInfoFile(File dir) { diff --git a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java index a40019666d..c3b84f35f3 100644 --- a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java +++ b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java @@ -1,5 +1,6 @@ package net.osmand.plus; +import android.content.ContentValues; import android.database.sqlite.SQLiteDiskIOException; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -22,6 +23,10 @@ import java.nio.ByteBuffer; import java.util.Arrays; import java.util.List; +import static net.osmand.IndexConstants.APP_DIR; +import static net.osmand.IndexConstants.SQLITE_EXT; +import static net.osmand.IndexConstants.TILES_INDEX_DIR; + public class SQLiteTileSource implements ITileSource { @@ -33,7 +38,7 @@ public class SQLiteTileSource implements ITileSource { private String urlTemplate = null; private String name; private SQLiteConnection db = null; - private final File file; + private File file = null; private int minZoom = 1; private int maxZoom = 17; private boolean inversiveZoom = true; // BigPlanet @@ -51,6 +56,14 @@ public class SQLiteTileSource implements ITileSource { private OsmandApplication ctx; private boolean onlyReadonlyAvailable = false; + private static final String TILES_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS tiles (" + + "x, " + + "y, " + + "z, " + + "s, " + + "image, " + + "time" + + ")"; public SQLiteTileSource(OsmandApplication ctx, File f, List toFindUrl){ this.ctx = ctx; @@ -72,6 +85,49 @@ public class SQLiteTileSource implements ITileSource { } } + + public SQLiteTileSource(OsmandApplication ctx, String name, int minZoom, int maxZoom, String urlTemplate, + String randoms, boolean isEllipsoid, boolean invertedY, String referer, + boolean timeSupported, long expirationTimeMillis, boolean inversiveZoom) { + this.ctx = ctx; + this.name = name; + this.urlTemplate = urlTemplate; + this.maxZoom = maxZoom; + this.minZoom = minZoom; + this.isEllipsoid = isEllipsoid; + this.expirationTimeMillis = expirationTimeMillis; + this.randoms = randoms; + this.referer = referer; + this.invertedY = invertedY; + this.timeSupported = timeSupported; + this.inversiveZoom = inversiveZoom; + } + + public void createDataBase() { + db = ctx.getSQLiteAPI().getOrCreateDatabase( + ctx.getAppPath(TILES_INDEX_DIR).getAbsolutePath() + "/" + name + SQLITE_EXT, true); + db.execSQL("CREATE TABLE IF NOT EXISTS info (" + + "url " + + ");"); + db.execSQL("INSERT INTO info (url) VALUES ('" + urlTemplate + "');"); + +// db.execSQL("INSERT INTO info VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", +// new Object[]{minZoom, maxZoom, urlTemplate, randoms, isEllipsoid ? 1 : 0, +// invertedY ? 1 : 0, referer, timeSupported ? "yes" : "no", timeSupported ? "yes" : "no", +// getExpirationTimeMinutes(), inversiveZoom ? "BigPlanet" : ""}); + + addInfoColumn("minzoom", String.valueOf(minZoom)); + addInfoColumn("maxzoom", String.valueOf(maxZoom)); + addInfoColumn("randoms", randoms); + addInfoColumn("ellipsoid", isEllipsoid ? "1" : "0"); + addInfoColumn("inverted_y", invertedY ? "1" : "0"); + addInfoColumn("referer", referer); + addInfoColumn("timesupported", timeSupported ? "yes" : "no"); + addInfoColumn("expireminutes", String.valueOf(getExpirationTimeMinutes())); + + db.execSQL(TILES_TABLE_CREATE); + db.close(); + } @Override public int getBitDensity() { @@ -119,7 +175,16 @@ public class SQLiteTileSource implements ITileSource { @Override public String getUrlTemplate() { - return this.urlTemplate; + if (this.urlTemplate != null) { + return this.urlTemplate; + } else { + SQLiteConnection db = getDatabase(); + if (db == null || db.isReadOnly() || urlTemplate == null) { + return null; + } else { + return this.urlTemplate; + } + } } @Override @@ -434,7 +499,37 @@ public class SQLiteTileSource implements ITileSource { } db.execSQL("DELETE FROM tiles"); } - + + @Override + public int getAvgSize() { + return base != null ? base.getAvgSize() : -1; + } + + @Override + public String getRule() { + return this.rule; + } + + @Override + public String getRandoms() { + return this.randoms; + } + + @Override + public boolean isInvertedYTile() { + return this.invertedY; + } + + @Override + public boolean isTimeSupported() { + return this.timeSupported; + } + + @Override + public boolean getInversiveZoom() { + return this.inversiveZoom; + } + /** * Makes method synchronized to give a little more time for get methods and * let all writing attempts to wait outside of this method diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index 4f9e016f3e..2ebb252685 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -59,6 +59,7 @@ import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import static net.osmand.IndexConstants.OSMAND_SETTINGS_FILE_EXT; +import static net.osmand.IndexConstants.TILES_INDEX_DIR; /* Usage: @@ -1024,9 +1025,12 @@ public class SettingsHelper { public void apply() { if (!mapSources.isEmpty()) { for (ITileSource template : mapSources) { -// getSettings().installTileSource(template); + if (template instanceof TileSourceManager.TileSourceTemplate) { + getSettings().installTileSource((TileSourceManager.TileSourceTemplate) template); + } else { + ((SQLiteTileSource) template).createDataBase(); + } } - } } @@ -1085,12 +1089,32 @@ public class SettingsHelper { JSONArray items = json.getJSONArray("items"); for (int i = 0; i < items.length(); i++) { JSONObject object = items.getJSONObject(i); - String name = object.getString("name"); -// String url = object.getString("url"); + boolean sql = object.optBoolean("sql"); + String name = object.optString("name"); + int minZoom = object.optInt("minZoom", 1); + int maxZoom = object.optInt("maxZoom", 17); + String url = object.optString("url"); + String randoms = object.optString("randoms"); + boolean ellipsoid = object.optBoolean("ellipsoid", false); + boolean invertedY = object.optBoolean("inverted_y", false); + String referer = object.optString("referer"); + boolean timesupported = object.optBoolean("timesupported", false); + long expire = object.optLong("expire"); + boolean inversiveZoom = object.optBoolean("inversiveZoom", false); + String ext = object.optString("ext"); + int tileSize = object.optInt("tileSize"); + int bitDensity = object.optInt("bitDensity"); + int avgSize = object.optInt("avgSize"); + String rule = object.optString("rule"); -// ITileSource template -// mapSources.add(); + ITileSource template; + if (!sql) { + template = new TileSourceManager.TileSourceTemplate(name, url, ext, maxZoom, minZoom, tileSize, bitDensity, avgSize); + } else { + template = new SQLiteTileSource(app, name, minZoom, maxZoom, url, randoms, ellipsoid, invertedY, referer, timesupported, expire, inversiveZoom); + } + mapSources.add(template); } } catch (JSONException e) { throw new IllegalArgumentException("Json parse error", e); @@ -1109,12 +1133,26 @@ public class SettingsHelper { if (!mapSources.isEmpty()) { for (ITileSource template : mapSources) { JSONObject jsonObject = new JSONObject(); + boolean sql = template instanceof SQLiteTileSource; + jsonObject.put("sql", sql); jsonObject.put("name", template.getName()); - jsonObject.put("url", template.getUrlTemplate()); - jsonObject.put("ext", template.getUrlTemplate()); jsonObject.put("minZoom", template.getMinimumZoomSupported()); jsonObject.put("maxZoom", template.getMaximumZoomSupported()); + jsonObject.put("url", template.getUrlTemplate()); + jsonObject.put("randoms", template.getRandoms()); + jsonObject.put("ellipsoid", template.isEllipticYTile()); + jsonObject.put("inverted_y", template.isInvertedYTile()); + jsonObject.put("referer", template.getReferer()); + jsonObject.put("timesupported", template.isTimeSupported()); jsonObject.put("expire", template.getExpirationTimeMillis()); + jsonObject.put("inversiveZoom", template.getInversiveZoom()); + + jsonObject.put("ext", template.getTileFormat()); + jsonObject.put("tileSize", template.getTileSize()); + jsonObject.put("bitDensity", template.getBitDensity()); + jsonObject.put("avgSize", template.getAvgSize()); + jsonObject.put("rule", template.getRule()); + items.put(jsonObject); } json.put("items", items); diff --git a/OsmAnd/src/net/osmand/plus/profiles/ExportImportProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/profiles/ExportImportProfileBottomSheet.java index eea5562b21..710c649586 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/ExportImportProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/profiles/ExportImportProfileBottomSheet.java @@ -296,8 +296,9 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet { quickActions.add((QuickAction) object); } else if (object instanceof PoiUIFilter) { poiUIFilters.add((PoiUIFilter) object); - } else if (object instanceof TileSourceManager.TileSourceTemplate) { - tileSourceTemplates.add((TileSourceManager.TileSourceTemplate) object); + } else if (object instanceof TileSourceManager.TileSourceTemplate + || object instanceof SQLiteTileSource) { + tileSourceTemplates.add((ITileSource) object); } else if (object instanceof File) { settingsItems.add(new SettingsHelper.FileSettingsItem(app, (File) object)); }