From af5efd12b7c248d11df228e8b81aab48e36c0962 Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Thu, 6 Feb 2020 19:20:26 +0200 Subject: [PATCH] refactor / change to ITileSource --- .../main/java/net/osmand/map/ITileSource.java | 2 + .../src/net/osmand/plus/SQLiteTileSource.java | 5 ++ .../src/net/osmand/plus/SettingsHelper.java | 87 +++++++++++-------- .../ExportImportProfileBottomSheet.java | 30 ++++--- 4 files changed, 77 insertions(+), 47 deletions(-) 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 b33c232c29..6d91b00545 100644 --- a/OsmAnd-java/src/main/java/net/osmand/map/ITileSource.java +++ b/OsmAnd-java/src/main/java/net/osmand/map/ITileSource.java @@ -12,6 +12,8 @@ public interface ITileSource { public String getUrlToLoad(int x, int y, int zoom); + public String getUrlTemplate(); + public byte[] getBytes(int x, int y, int zoom, String dirWithTiles) throws IOException; public int getMinimumZoomSupported(); diff --git a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java index b6ca8f5527..a40019666d 100644 --- a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java +++ b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java @@ -117,6 +117,11 @@ public class SQLiteTileSource implements ITileSource { return TileSourceTemplate.buildUrlToLoad(urlTemplate, randomsArray, x, y, zoom); } + @Override + public String getUrlTemplate() { + return this.urlTemplate; + } + @Override public int hashCode() { final int prime = 31; diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index 90e978309d..4f9e016f3e 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -13,6 +13,7 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import net.osmand.PlatformUtil; +import net.osmand.map.ITileSource; import net.osmand.map.TileSourceManager; import net.osmand.osm.MapPoiTypes; import net.osmand.osm.PoiCategory; @@ -101,10 +102,6 @@ public class SettingsHelper { void onSettingsExportFinished(@NonNull File file, boolean succeed); } - public interface SettingsPreImportListener { - void onSettingsPreImported(boolean isSuccessful, List items); - } - public SettingsHelper(OsmandApplication app) { this.app = app; } @@ -172,7 +169,7 @@ public class SettingsHelper { @NonNull public abstract String getFileName(); - public Boolean shouldReadInCollecting() { + public Boolean shouldReadOnCollecting() { return false; } @@ -740,9 +737,10 @@ public class SettingsHelper { this.quickActions = quickActions; } - public QuickActionSettingsItem(@NonNull OsmandSettings settings, + public QuickActionSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject jsonObject) throws JSONException { - super(SettingsItemType.QUICK_ACTION, settings, jsonObject); + super(SettingsItemType.QUICK_ACTION, app.getSettings(), jsonObject); + this.app = app; } public List getQuickActions() { @@ -768,7 +766,7 @@ public class SettingsHelper { } @Override - public Boolean shouldReadInCollecting() { + public Boolean shouldReadOnCollecting() { return true; } @@ -882,7 +880,6 @@ public class SettingsHelper { public PoiUiFilterSettingsItem(OsmandApplication app, JSONObject jsonObject) throws JSONException { super(SettingsItemType.POI_UI_FILTERS, app.getSettings(), jsonObject); this.app = app; - readFromJson(jsonObject); } public List getPoiUIFilters() { @@ -912,7 +909,7 @@ public class SettingsHelper { } @Override - public Boolean shouldReadInCollecting() { + public Boolean shouldReadOnCollecting() { return true; } @@ -1004,28 +1001,32 @@ public class SettingsHelper { public static class MapSourcesSettingsItem extends OsmandSettingsItem { - private List mapSources; + private OsmandApplication app; - public MapSourcesSettingsItem(OsmandSettings settings, List mapSources) { - super(SettingsItemType.MAP_SOURCES, settings); + private List mapSources; + + public MapSourcesSettingsItem(OsmandApplication app, List mapSources) { + super(SettingsItemType.MAP_SOURCES, app.getSettings()); + this.app = app; this.mapSources = mapSources; } - public MapSourcesSettingsItem(OsmandSettings settings, JSONObject jsonObject) throws JSONException { - super(SettingsItemType.MAP_SOURCES, settings, jsonObject); + public MapSourcesSettingsItem(OsmandApplication app, JSONObject jsonObject) throws JSONException { + super(SettingsItemType.MAP_SOURCES, app.getSettings(), jsonObject); + this.app = app; } - public List getMapSources() { + public List getMapSources() { return this.mapSources; } @Override public void apply() { if (!mapSources.isEmpty()) { - for (TileSourceManager.TileSourceTemplate template : mapSources) { - boolean writed = getSettings().installTileSource(template); - LOG.info("Temaplate _" + writed); + for (ITileSource template : mapSources) { +// getSettings().installTileSource(template); } + } } @@ -1042,7 +1043,7 @@ public class SettingsHelper { } @Override - public Boolean shouldReadInCollecting() { + public Boolean shouldReadOnCollecting() { return true; } @@ -1081,10 +1082,16 @@ public class SettingsHelper { try { mapSources = new ArrayList<>(); json = new JSONObject(jsonStr); - String items = json.getString("items"); - TileSourceManager manager = new TileSourceManager(); - List templates = manager.parseTemplatesList(items); - mapSources.addAll(templates); + 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"); + + +// ITileSource template +// mapSources.add(); + } } catch (JSONException e) { throw new IllegalArgumentException("Json parse error", e); } @@ -1098,9 +1105,19 @@ public class SettingsHelper { return new OsmandSettingsItemWriter(this, getSettings()) { @Override protected void writePreferenceToJson(@NonNull OsmandPreference preference, @NonNull JSONObject json) throws JSONException { - TileSourceManager manager = new TileSourceManager(); + JSONArray items = new JSONArray(); if (!mapSources.isEmpty()) { - json.put("items", manager.templatesListToString(mapSources)); + for (ITileSource template : mapSources) { + JSONObject jsonObject = new JSONObject(); + 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("expire", template.getExpirationTimeMillis()); + items.put(jsonObject); + } + json.put("items", items); } } }; @@ -1164,13 +1181,13 @@ public class SettingsHelper { item = new FileSettingsItem(app, json); break; case QUICK_ACTION: - item = new QuickActionSettingsItem(settings, json); + item = new QuickActionSettingsItem(app, json); break; case POI_UI_FILTERS: item = new PoiUiFilterSettingsItem(app, json); break; case MAP_SOURCES: - item = new MapSourcesSettingsItem(settings, json); + item = new MapSourcesSettingsItem(app, json); break; } return item; @@ -1286,8 +1303,8 @@ public class SettingsHelper { while ((entry = zis.getNextEntry()) != null) { String fileName = entry.getName(); SettingsItem item = itemsFactory.getItemByFileName(fileName); - if (item != null && collecting && item.shouldReadInCollecting() - || item != null && !collecting && !item.shouldReadInCollecting()) { + if (item != null && collecting && item.shouldReadOnCollecting() + || item != null && !collecting && !item.shouldReadOnCollecting()) { try { item.getReader().readFromStream(ois); } catch (IllegalArgumentException e) { @@ -1318,7 +1335,7 @@ public class SettingsHelper { private File file; private String latestChanges; private int version; - private Boolean isCollecting; + private boolean collectOnly; private SettingsImportListener listener; private SettingsImporter importer; @@ -1333,7 +1350,7 @@ public class SettingsHelper { this.latestChanges = latestChanges; this.version = version; importer = new SettingsImporter(app); - isCollecting = true; + collectOnly = true; } ImportAsyncTask(@NonNull File settingsFile, @NonNull List items, String latestChanges, int version, @Nullable SettingsImportListener listener) { @@ -1343,7 +1360,7 @@ public class SettingsHelper { this.latestChanges = latestChanges; this.version = version; importer = new SettingsImporter(app); - isCollecting = false; + collectOnly = false; } @Override @@ -1358,7 +1375,7 @@ public class SettingsHelper { @Override protected List doInBackground(Void... voids) { - if (isCollecting) { + if (collectOnly) { try { return importer.collectItems(file); } catch (IllegalArgumentException e) { @@ -1374,7 +1391,7 @@ public class SettingsHelper { @Override protected void onPostExecute(List items) { - if (isCollecting) { + if (collectOnly) { listener.onSettingsImportFinished(true, false, items); } else { this.items = items; diff --git a/OsmAnd/src/net/osmand/plus/profiles/ExportImportProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/profiles/ExportImportProfileBottomSheet.java index e239ac2a3e..eea5562b21 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/ExportImportProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/profiles/ExportImportProfileBottomSheet.java @@ -13,7 +13,6 @@ import android.support.v4.app.FragmentManager; import android.support.v4.content.ContextCompat; import android.support.v4.widget.CompoundButtonCompat; import android.support.v7.widget.SwitchCompat; -import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -26,10 +25,12 @@ import android.widget.Toast; import net.osmand.AndroidUtils; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; +import net.osmand.map.ITileSource; import net.osmand.map.TileSourceManager; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.SQLiteTileSource; import net.osmand.plus.SettingsHelper; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; @@ -231,21 +232,26 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet { )); } - List tileSourceTemplates = new ArrayList<>(); - final LinkedHashMap tileSourceEntries = new LinkedHashMap<>(app.getSettings().getTileSourceEntries(false)); + List iTileSources = new ArrayList<>(); + final LinkedHashMap tileSourceEntries = new LinkedHashMap<>(app.getSettings().getTileSourceEntries(true)); for (Map.Entry entry : tileSourceEntries.entrySet()) { File f = app.getAppPath(IndexConstants.TILES_INDEX_DIR + entry.getKey()); if (f != null) { - TileSourceManager.TileSourceTemplate template = TileSourceManager.createTileSourceTemplate(f); - if (template != null) { - tileSourceTemplates.add(template); + ITileSource template; + if (f.getName().endsWith(SQLiteTileSource.EXT)) { + template = new SQLiteTileSource(app, f, TileSourceManager.getKnownSourceTemplates()); + } else { + template = TileSourceManager.createTileSourceTemplate(f); + } + if (template != null && template.getUrlTemplate() != null) { + iTileSources.add(template); } } } - if (!tileSourceTemplates.isEmpty()) { + if (!iTileSources.isEmpty()) { dataList.add(new AdditionalDataWrapper( AdditionalDataWrapper.Type.MAP_SOURCES, - tileSourceTemplates + iTileSources )); } @@ -284,7 +290,7 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet { List settingsItems = new ArrayList<>(); List quickActions = new ArrayList<>(); List poiUIFilters = new ArrayList<>(); - List tileSourceTemplates = new ArrayList<>(); + List tileSourceTemplates = new ArrayList<>(); for (Object object : dataToOperate) { if (object instanceof QuickAction) { quickActions.add((QuickAction) object); @@ -303,7 +309,7 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet { settingsItems.add(new SettingsHelper.PoiUiFilterSettingsItem(app, poiUIFilters)); } if (!tileSourceTemplates.isEmpty()) { - settingsItems.add(new SettingsHelper.MapSourcesSettingsItem(app.getSettings(), tileSourceTemplates)); + settingsItems.add(new SettingsHelper.MapSourcesSettingsItem(app, tileSourceTemplates)); } return settingsItems; } @@ -326,7 +332,7 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet { List dataList = new ArrayList<>(); List quickActions = new ArrayList<>(); List poiUIFilters = new ArrayList<>(); - List tileSourceTemplates = new ArrayList<>(); + List tileSourceTemplates = new ArrayList<>(); List routingFilesList = new ArrayList<>(); List renderFilesList = new ArrayList<>(); @@ -634,7 +640,7 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet { icon.setImageDrawable(app.getUIUtilities().getIcon(iconRes != 0 ? iconRes : R.drawable.ic_person, profileColor)); break; case MAP_SOURCES: - title.setText(((TileSourceManager.TileSourceTemplate) currentItem).getName()); + title.setText(((ITileSource) currentItem).getName()); icon.setVisibility(View.INVISIBLE); icon.setImageResource(R.drawable.ic_action_info_dark); break;