Merge pull request #8436 from osmandapp/Fix_7752

Fix 7752 Add edit online sources.
This commit is contained in:
vshcherb 2020-02-11 17:36:34 +01:00 committed by GitHub
commit 4b403a00b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 25 deletions

View file

@ -25,10 +25,14 @@ import java.util.List;
public class SQLiteTileSource implements ITileSource { public class SQLiteTileSource implements ITileSource {
public static final String EXT = IndexConstants.SQLITE_EXT; public static final String EXT = IndexConstants.SQLITE_EXT;
private static final Log LOG = PlatformUtil.getLog(SQLiteTileSource.class); private static final Log LOG = PlatformUtil.getLog(SQLiteTileSource.class);
private static final String MAXZOOM_FIELD = "maxzoom";
private static final String MINZOOM_FIELD = "minzoom";
private static final String ELLIPSOID_FIELD = "ellipsoid";
private static final String URL_FIELD = "url";
private static final String EXPIREMINUTES_FIELD = "expireminutes";
private ITileSource base; private ITileSource base;
private String urlTemplate = null; private String urlTemplate = null;
private String name; private String name;
@ -163,7 +167,7 @@ public class SQLiteTileSource implements ITileSource {
if(cursor.moveToFirst()) { if(cursor.moveToFirst()) {
String[] columnNames = cursor.getColumnNames(); String[] columnNames = cursor.getColumnNames();
List<String> list = Arrays.asList(columnNames); List<String> list = Arrays.asList(columnNames);
int url = list.indexOf("url"); int url = list.indexOf(URL_FIELD);
if(url != -1) { if(url != -1) {
String template = cursor.getString(url); String template = cursor.getString(url);
if(!Algorithms.isEmpty(template)){ if(!Algorithms.isEmpty(template)){
@ -192,7 +196,7 @@ public class SQLiteTileSource implements ITileSource {
timeSupported = hasTimeColumn(); timeSupported = hasTimeColumn();
addInfoColumn("timecolumn", timeSupported? "yes" : "no"); addInfoColumn("timecolumn", timeSupported? "yes" : "no");
} }
int expireminutes = list.indexOf("expireminutes"); int expireminutes = list.indexOf(EXPIREMINUTES_FIELD);
this.expirationTimeMillis = -1; this.expirationTimeMillis = -1;
if(expireminutes != -1) { if(expireminutes != -1) {
int minutes = (int) cursor.getInt(expireminutes); int minutes = (int) cursor.getInt(expireminutes);
@ -200,14 +204,14 @@ public class SQLiteTileSource implements ITileSource {
this.expirationTimeMillis = minutes * 60 * 1000l; this.expirationTimeMillis = minutes * 60 * 1000l;
} }
} else { } else {
addInfoColumn("expireminutes", "0"); addInfoColumn(EXPIREMINUTES_FIELD, "0");
} }
int tsColumn = list.indexOf("tilesize"); int tsColumn = list.indexOf("tilesize");
this.tileSizeSpecified = tsColumn != -1; this.tileSizeSpecified = tsColumn != -1;
if(tileSizeSpecified) { if(tileSizeSpecified) {
this.tileSize = (int) cursor.getInt(tsColumn); this.tileSize = (int) cursor.getInt(tsColumn);
} }
int ellipsoid = list.indexOf("ellipsoid"); int ellipsoid = list.indexOf(ELLIPSOID_FIELD);
if(ellipsoid != -1) { if(ellipsoid != -1) {
int set = (int) cursor.getInt(ellipsoid); int set = (int) cursor.getInt(ellipsoid);
if(set == 1){ if(set == 1){
@ -225,15 +229,14 @@ public class SQLiteTileSource implements ITileSource {
if(randomsId != -1) { if(randomsId != -1) {
this.randoms = cursor.getString(randomsId); this.randoms = cursor.getString(randomsId);
this.randomsArray = TileSourceTemplate.buildRandomsArray(this.randoms); this.randomsArray = TileSourceTemplate.buildRandomsArray(this.randoms);
} }
//boolean inversiveInfoZoom = tnumbering != -1 && "BigPlanet".equals(cursor.getString(tnumbering)); //boolean inversiveInfoZoom = tnumbering != -1 && "BigPlanet".equals(cursor.getString(tnumbering));
boolean inversiveInfoZoom = inversiveZoom; boolean inversiveInfoZoom = inversiveZoom;
int mnz = list.indexOf("minzoom"); int mnz = list.indexOf(MINZOOM_FIELD);
if(mnz != -1) { if(mnz != -1) {
minZoom = (int) cursor.getInt(mnz); minZoom = (int) cursor.getInt(mnz);
} }
int mxz = list.indexOf("maxzoom"); int mxz = list.indexOf(MAXZOOM_FIELD);
if(mxz != -1) { if(mxz != -1) {
maxZoom = (int) cursor.getInt(mxz); maxZoom = (int) cursor.getInt(mxz);
} }
@ -251,10 +254,37 @@ public class SQLiteTileSource implements ITileSource {
return db; return db;
} }
public void updateFromTileSourceTemplate(TileSourceTemplate r) {
if (!onlyReadonlyAvailable) {
int maxZoom = r.getMaximumZoomSupported();
int minZoom = r.getMinimumZoomSupported();
if (inversiveZoom) {
int mnz = minZoom;
minZoom = 17 - maxZoom;
maxZoom = 17 - mnz;
}
if (getUrlTemplate() != null && !getUrlTemplate().equals(r.getUrlTemplate())) {
db.execSQL("update info set " + URL_FIELD + " = '" + r.getUrlTemplate() + "'");
}
if (r.getMinimumZoomSupported() != minZoom) {
db.execSQL("update info set " + MINZOOM_FIELD + " = '" + minZoom + "'");
}
if (r.getMaximumZoomSupported() != maxZoom) {
db.execSQL("update info set " + MAXZOOM_FIELD + " = '" + maxZoom + "'");
}
if (r.isEllipticYTile() != isEllipticYTile()) {
db.execSQL("update info set " + ELLIPSOID_FIELD + " = '" + (r.isEllipticYTile() ? 1 : 0) + "'");
}
if (r.getExpirationTimeMinutes() != getExpirationTimeMinutes()) {
db.execSQL("update info set " + EXPIREMINUTES_FIELD + " = '" + r.getExpirationTimeMinutes() + "'");
}
}
}
private void addInfoColumn(String columnName, String value) { private void addInfoColumn(String columnName, String value) {
if(!onlyReadonlyAvailable) { if (!onlyReadonlyAvailable) {
db.execSQL("alter table info add column "+columnName+" TEXT"); db.execSQL("alter table info add column " + columnName + " TEXT");
db.execSQL("update info set "+columnName+" = '"+value+"'"); db.execSQL("update info set " + columnName + " = '" + value + "'");
} }
} }
@ -517,7 +547,9 @@ public class SQLiteTileSource implements ITileSource {
return referer; return referer;
} }
public String getUrlTemplate() {
return urlTemplate;
}
} }

View file

@ -23,6 +23,7 @@ import net.osmand.map.TileSourceManager;
import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.ContextMenuAdapter.ItemClickListener; import net.osmand.plus.ContextMenuAdapter.ItemClickListener;
import net.osmand.plus.ContextMenuItem; import net.osmand.plus.ContextMenuItem;
import net.osmand.plus.SQLiteTileSource;
import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandPlugin;
@ -252,7 +253,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement
getDownloadActivity().reloadLocalIndexes(); getDownloadActivity().reloadLocalIndexes();
return true; return true;
} }
}, info.getName()); }, info.getFileName());
} else if (resId == R.string.local_index_mi_restore) { } else if (resId == R.string.local_index_mi_restore) {
new LocalIndexOperationTask(getDownloadActivity(), listAdapter, LocalIndexOperationTask.RESTORE_OPERATION).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, info); new LocalIndexOperationTask(getDownloadActivity(), listAdapter, LocalIndexOperationTask.RESTORE_OPERATION).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, info);
} else if (resId == R.string.shared_string_delete) { } else if (resId == R.string.shared_string_delete) {
@ -1237,7 +1238,9 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement
} }
}); });
if (info.getType() == LocalIndexType.TILES_DATA if (info.getType() == LocalIndexType.TILES_DATA
&& (info.getAttachedObject() instanceof TileSourceManager.TileSourceTemplate)) { && ((info.getAttachedObject() instanceof TileSourceManager.TileSourceTemplate)
|| ((info.getAttachedObject() instanceof SQLiteTileSource)
&& ((SQLiteTileSource) info.getAttachedObject()).couldBeDownloadedFromInternet()))) {
item = optionsMenu.getMenu().add(R.string.shared_string_edit) item = optionsMenu.getMenu().add(R.string.shared_string_edit)
.setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_edit_dark)); .setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_edit_dark));
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

View file

@ -36,6 +36,7 @@ import net.osmand.plus.OsmandSettings;
import net.osmand.plus.OsmandSettings.CommonPreference; import net.osmand.plus.OsmandSettings.CommonPreference;
import net.osmand.plus.OsmandSettings.LayerTransparencySeekbarMode; import net.osmand.plus.OsmandSettings.LayerTransparencySeekbarMode;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.SQLiteTileSource;
import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities;
import net.osmand.plus.Version; import net.osmand.plus.Version;
import net.osmand.plus.activities.DownloadTilesDialog; import net.osmand.plus.activities.DownloadTilesDialog;
@ -478,10 +479,11 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
public static void defineNewEditLayer(final Activity activity, final ResultMatcher<TileSourceTemplate> resultMatcher, String editedLayerName) { public static void defineNewEditLayer(final Activity activity, final ResultMatcher<TileSourceTemplate> resultMatcher, final String editedLayerName) {
final OsmandApplication app = (OsmandApplication) activity.getApplication(); final OsmandApplication app = (OsmandApplication) activity.getApplication();
final OsmandSettings settings = app.getSettings(); final OsmandSettings settings = app.getSettings();
final Map<String, String> entriesMap = settings.getTileSourceEntries(false); final Map<String, String> entriesMap = settings.getTileSourceEntries(true);
final SQLiteTileSource[] sqLiteTileSource = new SQLiteTileSource[1];
boolean nightMode = isNightMode(activity, app); boolean nightMode = isNightMode(activity, app);
final int dp8 = AndroidUtils.dpToPx(app, 8f); final int dp8 = AndroidUtils.dpToPx(app, 8f);
int textColorPrimary = ContextCompat.getColor(app, nightMode ? R.color.text_color_primary_dark : R.color.text_color_primary_light); int textColorPrimary = ContextCompat.getColor(app, nightMode ? R.color.text_color_primary_dark : R.color.text_color_primary_light);
@ -512,10 +514,25 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
); );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
existing.setAdapter(adapter); existing.setAdapter(adapter);
TileSourceTemplate template;
if (editedLayerName != null) { if (editedLayerName != null) {
File f = ((OsmandApplication) activity.getApplication()).getAppPath( if (!editedLayerName.endsWith(IndexConstants.SQLITE_EXT)) {
IndexConstants.TILES_INDEX_DIR + editedLayerName); File f = ((OsmandApplication) activity.getApplication()).getAppPath(
TileSourceTemplate template = TileSourceManager.createTileSourceTemplate(f); IndexConstants.TILES_INDEX_DIR + editedLayerName);
template = TileSourceManager.createTileSourceTemplate(f);
} else {
List<TileSourceTemplate> knownTemplates = TileSourceManager.getKnownSourceTemplates();
File tPath = app.getAppPath(IndexConstants.TILES_INDEX_DIR);
File dir = new File(tPath, editedLayerName);
sqLiteTileSource[0] = new SQLiteTileSource(app, dir, knownTemplates);
sqLiteTileSource[0].couldBeDownloadedFromInternet();
template = new TileSourceManager.TileSourceTemplate(sqLiteTileSource[0].getName(),
sqLiteTileSource[0].getUrlTemplate(), "png", sqLiteTileSource[0].getMaximumZoomSupported(),
sqLiteTileSource[0].getMinimumZoomSupported(), sqLiteTileSource[0].getTileSize(),
sqLiteTileSource[0].getBitDensity(), 32000);
template.setExpirationTimeMinutes(sqLiteTileSource[0].getExpirationTimeMinutes());
template.setEllipticYTile(sqLiteTileSource[0].isEllipticYTile());
}
if (template != null) { if (template != null) {
result[0] = template.copy(); result[0] = template.copy();
updateTileSourceEditView(result[0], name, urlToLoad, minZoom, maxZoom, expire, elliptic); updateTileSourceEditView(result[0], name, urlToLoad, minZoom, maxZoom, expire, elliptic);
@ -558,11 +575,15 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
r.setEllipticYTile(elliptic.isChecked()); r.setEllipticYTile(elliptic.isChecked());
r.setUrlToLoad(urlToLoad.getText().toString().equals("") ? null : urlToLoad.getText().toString().replace("{$x}", "{1}") r.setUrlToLoad(urlToLoad.getText().toString().equals("") ? null : urlToLoad.getText().toString().replace("{$x}", "{1}")
.replace("{$y}", "{2}").replace("{$z}", "{0}")); .replace("{$y}", "{2}").replace("{$z}", "{0}"));
if (r.getName().length() > 0) { if (sqLiteTileSource[0] != null) {
if (settings.installTileSource(r)) { sqLiteTileSource[0].updateFromTileSourceTemplate(r);
Toast.makeText(activity, activity.getString(R.string.edit_tilesource_successfully, r.getName()), } else {
Toast.LENGTH_SHORT).show(); if (r.getName().length() > 0) {
resultMatcher.publish(r); if (settings.installTileSource(r)) {
Toast.makeText(activity, activity.getString(R.string.edit_tilesource_successfully, r.getName()),
Toast.LENGTH_SHORT).show();
resultMatcher.publish(r);
}
} }
} }
} catch (RuntimeException e) { } catch (RuntimeException e) {