refactor SQLiteTileSource and FileUtils

This commit is contained in:
veliymolfar 2020-05-28 14:55:40 +03:00
parent 0e93d40666
commit fac931e801
4 changed files with 28 additions and 26 deletions

View file

@ -13,7 +13,6 @@ import androidx.appcompat.app.AlertDialog;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.SQLiteTileSource; import net.osmand.plus.SQLiteTileSource;
import net.osmand.plus.download.ui.LocalIndexesFragment;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.io.File; import java.io.File;
@ -25,7 +24,7 @@ public class FileUtils {
public static final Pattern ILLEGAL_FILE_NAME_CHARACTERS = Pattern.compile("[?:\"*|/<>]"); public static final Pattern ILLEGAL_FILE_NAME_CHARACTERS = Pattern.compile("[?:\"*|/<>]");
public static final Pattern ILLEGAL_PATH_NAME_CHARACTERS = Pattern.compile("[?:\"*|<>]"); public static final Pattern ILLEGAL_PATH_NAME_CHARACTERS = Pattern.compile("[?:\"*|<>]");
public static void renameFile(Activity a, final File f, final LocalIndexesFragment.RenameCallback callback) { public static void renameFile(Activity a, final File f, final RenameCallback callback) {
final WeakReference<Activity> weakActivity = new WeakReference<>(a); final WeakReference<Activity> weakActivity = new WeakReference<>(a);
AlertDialog.Builder b = new AlertDialog.Builder(a); AlertDialog.Builder b = new AlertDialog.Builder(a);
if (f.exists()) { if (f.exists()) {
@ -89,7 +88,7 @@ public class FileUtils {
} }
public static File renameSQLiteFile(OsmandApplication ctx, File source, String newName, public static File renameSQLiteFile(OsmandApplication ctx, File source, String newName,
LocalIndexesFragment.RenameCallback callback) { RenameCallback callback) {
File dest = checkRenamePossibility(ctx, source, newName, false); File dest = checkRenamePossibility(ctx, source, newName, false);
if (dest == null) { if (dest == null) {
return null; return null;
@ -116,7 +115,7 @@ public class FileUtils {
} }
public static File renameGpxFile(OsmandApplication ctx, File source, String newName, boolean dirAllowed, public static File renameGpxFile(OsmandApplication ctx, File source, String newName, boolean dirAllowed,
LocalIndexesFragment.RenameCallback callback) { RenameCallback callback) {
File dest = checkRenamePossibility(ctx, source, newName, dirAllowed); File dest = checkRenamePossibility(ctx, source, newName, dirAllowed);
if (dest == null) { if (dest == null) {
return null; return null;
@ -153,4 +152,8 @@ public class FileUtils {
} }
return dest; return dest;
} }
public interface RenameCallback {
void renamedTo(File file);
}
} }

View file

@ -126,7 +126,7 @@ public class SQLiteTileSource implements ITileSource {
} }
public void createDataBase() { public void createDataBase() {
db = ctx.getSQLiteAPI().getOrCreateDatabase( SQLiteConnection db = ctx.getSQLiteAPI().getOrCreateDatabase(
ctx.getAppPath(TILES_INDEX_DIR).getAbsolutePath() + "/" + name + SQLITE_EXT, true); ctx.getAppPath(TILES_INDEX_DIR).getAbsolutePath() + "/" + name + SQLITE_EXT, true);
db.execSQL("CREATE TABLE IF NOT EXISTS tiles (x int, y int, z int, s int, image blob, time long, PRIMARY KEY (x,y,z,s))"); db.execSQL("CREATE TABLE IF NOT EXISTS tiles (x int, y int, z int, s int, image blob, time long, PRIMARY KEY (x,y,z,s))");
@ -134,14 +134,13 @@ public class SQLiteTileSource implements ITileSource {
db.execSQL("CREATE TABLE IF NOT EXISTS info(tilenumbering,minzoom,maxzoom)"); db.execSQL("CREATE TABLE IF NOT EXISTS info(tilenumbering,minzoom,maxzoom)");
db.execSQL("INSERT INTO info (tilenumbering,minzoom,maxzoom) VALUES ('simple','" + minZoom + "','" + maxZoom + "');"); db.execSQL("INSERT INTO info (tilenumbering,minzoom,maxzoom) VALUES ('simple','" + minZoom + "','" + maxZoom + "');");
addInfoColumn(URL, urlTemplate); addInfoColumn(db, URL, urlTemplate);
addInfoColumn(RANDOMS, randoms); addInfoColumn(db, RANDOMS, randoms);
addInfoColumn(ELLIPSOID, isEllipsoid ? "1" : "0"); addInfoColumn(db, ELLIPSOID, isEllipsoid ? "1" : "0");
addInfoColumn(INVERTED_Y, invertedY ? "1" : "0"); addInfoColumn(db, INVERTED_Y, invertedY ? "1" : "0");
addInfoColumn(REFERER, referer); addInfoColumn(db, REFERER, referer);
addInfoColumn(TIME_COLUMN, timeSupported ? "yes" : "no"); addInfoColumn(db, TIME_COLUMN, timeSupported ? "yes" : "no");
addInfoColumn(EXPIRE_MINUTES, String.valueOf(getExpirationTimeMinutes())); addInfoColumn(db, EXPIRE_MINUTES, String.valueOf(getExpirationTimeMinutes()));
db.close(); db.close();
} }
@ -270,14 +269,14 @@ public class SQLiteTileSource implements ITileSource {
inversiveZoom = BIG_PLANET_TILE_NUMBERING.equalsIgnoreCase(cursor.getString(tnumbering)); inversiveZoom = BIG_PLANET_TILE_NUMBERING.equalsIgnoreCase(cursor.getString(tnumbering));
} else { } else {
inversiveZoom = true; inversiveZoom = true;
addInfoColumn(TILENUMBERING, BIG_PLANET_TILE_NUMBERING); addInfoColumn(db, TILENUMBERING, BIG_PLANET_TILE_NUMBERING);
} }
int timecolumn = list.indexOf(TIME_COLUMN); int timecolumn = list.indexOf(TIME_COLUMN);
if (timecolumn != -1) { if (timecolumn != -1) {
timeSupported = "yes".equalsIgnoreCase(cursor.getString(timecolumn)); timeSupported = "yes".equalsIgnoreCase(cursor.getString(timecolumn));
} else { } else {
timeSupported = hasTimeColumn(); timeSupported = hasTimeColumn(db);
addInfoColumn(TIME_COLUMN, timeSupported? "yes" : "no"); addInfoColumn(db, TIME_COLUMN, timeSupported ? "yes" : "no");
} }
int expireminutes = list.indexOf(EXPIRE_MINUTES); int expireminutes = list.indexOf(EXPIRE_MINUTES);
this.expirationTimeMillis = -1; this.expirationTimeMillis = -1;
@ -287,7 +286,7 @@ public class SQLiteTileSource implements ITileSource {
this.expirationTimeMillis = minutes * 60 * 1000l; this.expirationTimeMillis = minutes * 60 * 1000l;
} }
} else { } else {
addInfoColumn(EXPIRE_MINUTES, "0"); addInfoColumn(db, EXPIRE_MINUTES, "0");
} }
int tsColumn = list.indexOf(TILESIZE); int tsColumn = list.indexOf(TILESIZE);
this.tileSizeSpecified = tsColumn != -1; this.tileSizeSpecified = tsColumn != -1;
@ -338,7 +337,7 @@ public class SQLiteTileSource implements ITileSource {
} }
public void updateFromTileSourceTemplate(TileSourceTemplate r) { public void updateFromTileSourceTemplate(TileSourceTemplate r) {
db = getDatabase(); SQLiteConnection db = getDatabase();
if (!onlyReadonlyAvailable && db != null) { if (!onlyReadonlyAvailable && db != null) {
int maxZoom = r.getMaximumZoomSupported(); int maxZoom = r.getMaximumZoomSupported();
int minZoom = r.getMinimumZoomSupported(); int minZoom = r.getMinimumZoomSupported();
@ -363,9 +362,12 @@ public class SQLiteTileSource implements ITileSource {
db.execSQL("update info set " + EXPIRE_MINUTES + " = '" + r.getExpirationTimeMinutes() + "'"); db.execSQL("update info set " + EXPIRE_MINUTES + " = '" + r.getExpirationTimeMinutes() + "'");
} }
} }
if (db != null) {
db.close();
}
} }
private void addInfoColumn(String columnName, String value) { private void addInfoColumn(SQLiteConnection db, String columnName, String value) {
if(!onlyReadonlyAvailable) { if(!onlyReadonlyAvailable) {
try { try {
db.execSQL("alter table info add column " + columnName + " TEXT"); db.execSQL("alter table info add column " + columnName + " TEXT");
@ -376,7 +378,7 @@ public class SQLiteTileSource implements ITileSource {
} }
} }
private boolean hasTimeColumn() { private boolean hasTimeColumn(SQLiteConnection db) {
SQLiteCursor cursor; SQLiteCursor cursor;
cursor = db.rawQuery("SELECT * FROM tiles", null); cursor = db.rawQuery("SELECT * FROM tiles", null);
cursor.moveToFirst(); cursor.moveToFirst();
@ -476,7 +478,7 @@ public class SQLiteTileSource implements ITileSource {
} else if(!tileSizeSpecified && } else if(!tileSizeSpecified &&
tileSize != bmp.getWidth() && bmp.getWidth() > 0) { tileSize != bmp.getWidth() && bmp.getWidth() > 0) {
tileSize = bmp.getWidth(); tileSize = bmp.getWidth();
addInfoColumn("tilesize", tileSize+""); addInfoColumn(db, "tilesize", tileSize + "");
tileSizeSpecified = true; tileSizeSpecified = true;
} }
return bmp; return bmp;

View file

@ -36,6 +36,7 @@ import androidx.core.view.MenuItemCompat;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.Collator; import net.osmand.Collator;
import net.osmand.FileUtils; import net.osmand.FileUtils;
import net.osmand.FileUtils.RenameCallback;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.OsmAndCollator; import net.osmand.OsmAndCollator;
import net.osmand.map.ITileSource; import net.osmand.map.ITileSource;
@ -1206,8 +1207,4 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement
return (DownloadActivity) getActivity(); return (DownloadActivity) getActivity();
} }
public interface RenameCallback {
public void renamedTo(File file);
}
} }

View file

@ -47,6 +47,7 @@ import androidx.core.view.MenuItemCompat;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.FileUtils; import net.osmand.FileUtils;
import net.osmand.FileUtils.RenameCallback;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.GPXUtilities.GPXTrackAnalysis;
@ -76,7 +77,6 @@ import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.activities.TrackActivity; import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.base.OsmandExpandableListFragment; import net.osmand.plus.base.OsmandExpandableListFragment;
import net.osmand.plus.dialogs.DirectionsDialogs; import net.osmand.plus.dialogs.DirectionsDialogs;
import net.osmand.plus.download.ui.LocalIndexesFragment.RenameCallback;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType; import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType;
import net.osmand.plus.mapmarkers.CoordinateInputDialogFragment; import net.osmand.plus.mapmarkers.CoordinateInputDialogFragment;