refactor / change to ITileSource

This commit is contained in:
veliymolfar 2020-02-06 19:20:26 +02:00
parent ab77ea143c
commit af5efd12b7
4 changed files with 77 additions and 47 deletions

View file

@ -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();

View file

@ -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;

View file

@ -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<SettingsItem> 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<QuickAction> 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<PoiUIFilter> 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<TileSourceManager.TileSourceTemplate> mapSources;
private OsmandApplication app;
public MapSourcesSettingsItem(OsmandSettings settings, List<TileSourceManager.TileSourceTemplate> mapSources) {
super(SettingsItemType.MAP_SOURCES, settings);
private List<ITileSource> mapSources;
public MapSourcesSettingsItem(OsmandApplication app, List<ITileSource> 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<TileSourceManager.TileSourceTemplate> getMapSources() {
public List<ITileSource> 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<TileSourceManager.TileSourceTemplate> 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<SettingsItem> 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<SettingsItem> 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<SettingsItem> items) {
if (isCollecting) {
if (collectOnly) {
listener.onSettingsImportFinished(true, false, items);
} else {
this.items = items;

View file

@ -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<TileSourceManager.TileSourceTemplate> tileSourceTemplates = new ArrayList<>();
final LinkedHashMap<String, String> tileSourceEntries = new LinkedHashMap<>(app.getSettings().getTileSourceEntries(false));
List<ITileSource> iTileSources = new ArrayList<>();
final LinkedHashMap<String, String> tileSourceEntries = new LinkedHashMap<>(app.getSettings().getTileSourceEntries(true));
for (Map.Entry<String, String> 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<SettingsHelper.SettingsItem> settingsItems = new ArrayList<>();
List<QuickAction> quickActions = new ArrayList<>();
List<PoiUIFilter> poiUIFilters = new ArrayList<>();
List<TileSourceManager.TileSourceTemplate> tileSourceTemplates = new ArrayList<>();
List<ITileSource> 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<AdditionalDataWrapper> dataList = new ArrayList<>();
List<QuickAction> quickActions = new ArrayList<>();
List<PoiUIFilter> poiUIFilters = new ArrayList<>();
List<TileSourceManager.TileSourceTemplate> tileSourceTemplates = new ArrayList<>();
List<ITileSource> tileSourceTemplates = new ArrayList<>();
List<File> routingFilesList = new ArrayList<>();
List<File> 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;