Refactor java core
This commit is contained in:
parent
03018fd6c0
commit
6a0b93ea63
4 changed files with 75 additions and 22 deletions
|
@ -12,13 +12,12 @@ import net.osmand.LogUtil;
|
||||||
import net.osmand.data.IndexConstants;
|
import net.osmand.data.IndexConstants;
|
||||||
import net.osmand.map.ITileSource;
|
import net.osmand.map.ITileSource;
|
||||||
import net.osmand.map.TileSourceManager.TileSourceTemplate;
|
import net.osmand.map.TileSourceManager.TileSourceTemplate;
|
||||||
|
import net.osmand.plus.api.SQLiteAPI.SQLiteConnection;
|
||||||
|
import net.osmand.plus.api.SQLiteAPI.SQLiteCursor;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
import android.database.sqlite.SQLiteDiskIOException;
|
import android.database.sqlite.SQLiteDiskIOException;
|
||||||
import android.database.sqlite.SQLiteStatement;
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.Bitmap.Config;
|
import android.graphics.Bitmap.Config;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
|
@ -37,7 +36,7 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
private ITileSource base;
|
private ITileSource base;
|
||||||
private String urlTemplate = null;
|
private String urlTemplate = null;
|
||||||
private String name;
|
private String name;
|
||||||
private SQLiteDatabase db;
|
private SQLiteConnection db;
|
||||||
private final File file;
|
private final File file;
|
||||||
private int minZoom = 1;
|
private int minZoom = 1;
|
||||||
private int maxZoom = 17;
|
private int maxZoom = 17;
|
||||||
|
@ -46,8 +45,10 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
final int margin = 1;
|
final int margin = 1;
|
||||||
final int tileSize = 256;
|
final int tileSize = 256;
|
||||||
final int minScaledSize = 8;
|
final int minScaledSize = 8;
|
||||||
|
private ClientContext ctx;
|
||||||
|
|
||||||
public SQLiteTileSource(File f, List<TileSourceTemplate> toFindUrl){
|
public SQLiteTileSource(ClientContext ctx, File f, List<TileSourceTemplate> toFindUrl){
|
||||||
|
this.ctx = ctx;
|
||||||
this.file = f;
|
this.file = f;
|
||||||
int i = f.getName().lastIndexOf('.');
|
int i = f.getName().lastIndexOf('.');
|
||||||
name = f.getName().substring(0, i);
|
name = f.getName().substring(0, i);
|
||||||
|
@ -101,7 +102,7 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
public String getUrlToLoad(int x, int y, int zoom) {
|
public String getUrlToLoad(int x, int y, int zoom) {
|
||||||
if (zoom > baseZoom)
|
if (zoom > baseZoom)
|
||||||
return null;
|
return null;
|
||||||
SQLiteDatabase db = getDatabase();
|
SQLiteConnection db = getDatabase();
|
||||||
if(db == null || db.isReadOnly() || urlTemplate == null){
|
if(db == null || db.isReadOnly() || urlTemplate == null){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -139,9 +140,9 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private SQLiteDatabase getDatabase(){
|
private SQLiteConnection getDatabase(){
|
||||||
if(db == null && file.exists()){
|
if((db == null || db.isClosed()) && file.exists() ){
|
||||||
db = SQLiteDatabase.openDatabase(file.getAbsolutePath(), null, 0);
|
db = ctx.getSQLiteAPI().openByAbsolutePath(file.getAbsolutePath(), false);
|
||||||
try {
|
try {
|
||||||
String template = db.compileStatement("SELECT url FROM info").simpleQueryForString(); //$NON-NLS-1$
|
String template = db.compileStatement("SELECT url FROM info").simpleQueryForString(); //$NON-NLS-1$
|
||||||
if(!Algoritms.isEmpty(template)){
|
if(!Algoritms.isEmpty(template)){
|
||||||
|
@ -168,13 +169,13 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean exists(int x, int y, int zoom, boolean exact) {
|
public boolean exists(int x, int y, int zoom, boolean exact) {
|
||||||
SQLiteDatabase db = getDatabase();
|
SQLiteConnection db = getDatabase();
|
||||||
if(db == null){
|
if(db == null){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
long time = System.currentTimeMillis();
|
long time = System.currentTimeMillis();
|
||||||
if (exact || zoom <= baseZoom) {
|
if (exact || zoom <= baseZoom) {
|
||||||
Cursor cursor = db.rawQuery("SELECT 1 FROM tiles WHERE x = ? AND y = ? AND z = ?", new String[] {x+"", y+"",(17 - zoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
|
SQLiteCursor cursor = db.rawQuery("SELECT 1 FROM tiles WHERE x = ? AND y = ? AND z = ?", new String[] {x+"", y+"",(17 - zoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
|
||||||
try {
|
try {
|
||||||
boolean e = cursor.moveToFirst();
|
boolean e = cursor.moveToFirst();
|
||||||
cursor.close();
|
cursor.close();
|
||||||
|
@ -189,7 +190,7 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
int n = zoom - baseZoom;
|
int n = zoom - baseZoom;
|
||||||
int base_xtile = x >> n;
|
int base_xtile = x >> n;
|
||||||
int base_ytile = y >> n;
|
int base_ytile = y >> n;
|
||||||
Cursor cursor = db.rawQuery("SELECT 1 FROM tiles WHERE x = ? AND y = ? AND z = ?", new String[] {base_xtile+"", base_ytile+"",(17 - baseZoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
|
SQLiteCursor cursor = db.rawQuery("SELECT 1 FROM tiles WHERE x = ? AND y = ? AND z = ?", new String[] {base_xtile+"", base_ytile+"",(17 - baseZoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
|
||||||
try {
|
try {
|
||||||
boolean e = cursor.moveToFirst();
|
boolean e = cursor.moveToFirst();
|
||||||
cursor.close();
|
cursor.close();
|
||||||
|
@ -204,7 +205,7 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLocked() {
|
public boolean isLocked() {
|
||||||
SQLiteDatabase db = getDatabase();
|
SQLiteConnection db = getDatabase();
|
||||||
if(db == null){
|
if(db == null){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -216,7 +217,7 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
// based on its neighbor. This is needed to have a nice bilinear resampling
|
// based on its neighbor. This is needed to have a nice bilinear resampling
|
||||||
// on tile edges. Margin of 1 is enough for bilinear resampling.
|
// on tile edges. Margin of 1 is enough for bilinear resampling.
|
||||||
|
|
||||||
SQLiteDatabase db = getDatabase();
|
SQLiteConnection db = getDatabase();
|
||||||
if(db == null){
|
if(db == null){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -232,7 +233,7 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
|
|
||||||
int xOff, yOff, w, h;
|
int xOff, yOff, w, h;
|
||||||
int dstx, dsty;
|
int dstx, dsty;
|
||||||
Cursor cursor = db.rawQuery(
|
SQLiteCursor cursor = db.rawQuery(
|
||||||
"SELECT image FROM tiles WHERE x = ? AND y = ? AND z = ?",
|
"SELECT image FROM tiles WHERE x = ? AND y = ? AND z = ?",
|
||||||
new String[] {(x + dx) + "", (y + dy) + "", (17 - zoom) + ""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
|
new String[] {(x + dx) + "", (y + dy) + "", (17 - zoom) + ""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
|
||||||
byte[] blob = null;
|
byte[] blob = null;
|
||||||
|
@ -262,13 +263,13 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap getImage(int x, int y, int zoom) {
|
public Bitmap getImage(int x, int y, int zoom) {
|
||||||
SQLiteDatabase db = getDatabase();
|
SQLiteConnection db = getDatabase();
|
||||||
if(db == null){
|
if(db == null){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (zoom <= baseZoom) {
|
if (zoom <= baseZoom) {
|
||||||
// return the normal tile if exists
|
// return the normal tile if exists
|
||||||
Cursor cursor = db.rawQuery("SELECT image FROM tiles WHERE x = ? AND y = ? AND z = ?", new String[] {x+"", y+"",(17 - zoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
|
SQLiteCursor cursor = db.rawQuery("SELECT image FROM tiles WHERE x = ? AND y = ? AND z = ?", new String[] {x+"", y+"",(17 - zoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
|
||||||
byte[] blob = null;
|
byte[] blob = null;
|
||||||
if(cursor.moveToFirst()) {
|
if(cursor.moveToFirst()) {
|
||||||
blob = cursor.getBlob(0);
|
blob = cursor.getBlob(0);
|
||||||
|
@ -325,7 +326,7 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteImage(int x, int y, int zoom) {
|
public void deleteImage(int x, int y, int zoom) {
|
||||||
SQLiteDatabase db = getDatabase();
|
SQLiteConnection db = getDatabase();
|
||||||
if(db == null || db.isReadOnly()){
|
if(db == null || db.isReadOnly()){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -339,7 +340,7 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
* let all writing attempts to wait outside of this method
|
* let all writing attempts to wait outside of this method
|
||||||
*/
|
*/
|
||||||
public synchronized void insertImage(int x, int y, int zoom, File fileToSave) throws IOException {
|
public synchronized void insertImage(int x, int y, int zoom, File fileToSave) throws IOException {
|
||||||
SQLiteDatabase db = getDatabase();
|
SQLiteConnection db = getDatabase();
|
||||||
if (db == null || db.isReadOnly()) {
|
if (db == null || db.isReadOnly()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -354,7 +355,7 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
buf.put(b, 0, i);
|
buf.put(b, 0, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
SQLiteStatement statement = db.compileStatement("INSERT INTO tiles VALUES(?, ?, ?, ?, ?)"); //$NON-NLS-1$
|
net.osmand.plus.api.SQLiteAPI.SQLiteStatement statement = db.compileStatement("INSERT INTO tiles VALUES(?, ?, ?, ?, ?)"); //$NON-NLS-1$
|
||||||
statement.bindLong(1, x);
|
statement.bindLong(1, x);
|
||||||
statement.bindLong(2, y);
|
statement.bindLong(2, y);
|
||||||
statement.bindLong(3, 17 - zoom);
|
statement.bindLong(3, 17 - zoom);
|
||||||
|
@ -362,6 +363,7 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
statement.bindBlob(5, buf.array());
|
statement.bindBlob(5, buf.array());
|
||||||
statement.execute();
|
statement.execute();
|
||||||
statement.close();
|
statement.close();
|
||||||
|
is.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -90,7 +90,7 @@ public class LocalIndexHelper {
|
||||||
template.getUrlTemplate() != null, zooms.toString());
|
template.getUrlTemplate() != null, zooms.toString());
|
||||||
info.setDescription(descr);
|
info.setDescription(descr);
|
||||||
} else if(f.isFile() && f.getName().endsWith(SQLiteTileSource.EXT)){
|
} else if(f.isFile() && f.getName().endsWith(SQLiteTileSource.EXT)){
|
||||||
SQLiteTileSource template = new SQLiteTileSource(f, TileSourceManager.getKnownSourceTemplates());
|
SQLiteTileSource template = new SQLiteTileSource(app, f, TileSourceManager.getKnownSourceTemplates());
|
||||||
// Set<Integer> zooms = new TreeSet<Integer>();
|
// Set<Integer> zooms = new TreeSet<Integer>();
|
||||||
// for(int i=1; i<22; i++){
|
// for(int i=1; i<22; i++){
|
||||||
// if(template.exists(i)){
|
// if(template.exists(i)){
|
||||||
|
|
|
@ -36,7 +36,7 @@ public class InternalToDoAPIImpl implements InternalToDoAPI {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ITileSource newSqliteTileSource(File dir, List<TileSourceTemplate> knownTemplates) {
|
public ITileSource newSqliteTileSource(File dir, List<TileSourceTemplate> knownTemplates) {
|
||||||
return new SQLiteTileSource(dir, knownTemplates);
|
return new SQLiteTileSource(app, dir, knownTemplates);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -85,6 +85,11 @@ public class SQLiteAPIImpl implements SQLiteAPI {
|
||||||
public long getInt(int ind) {
|
public long getInt(int ind) {
|
||||||
return c.getInt(ind);
|
return c.getInt(ind);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] getBlob(int ind) {
|
||||||
|
return c.getBlob(ind);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,6 +132,26 @@ public class SQLiteAPIImpl implements SQLiteAPI {
|
||||||
public void bindNull(int i) {
|
public void bindNull(int i) {
|
||||||
st.bindNull(i);
|
st.bindNull(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long simpleQueryForLong() {
|
||||||
|
return st.simpleQueryForLong();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String simpleQueryForString() {
|
||||||
|
return st.simpleQueryForString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void bindLong(int i, long val) {
|
||||||
|
st.bindLong(i, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void bindBlob(int i, byte[] val) {
|
||||||
|
st.bindBlob(i, val);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,5 +160,31 @@ public class SQLiteAPIImpl implements SQLiteAPI {
|
||||||
ds.setVersion(newVersion);
|
ds.setVersion(newVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isReadOnly() {
|
||||||
|
return ds.isReadOnly();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isDbLockedByOtherThreads() {
|
||||||
|
return ds.isDbLockedByOtherThreads();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isClosed() {
|
||||||
|
return !ds.isOpen();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SQLiteConnection openByAbsolutePath(String path, boolean readOnly) {
|
||||||
|
android.database.sqlite.SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null,
|
||||||
|
readOnly? SQLiteDatabase.OPEN_READONLY : SQLiteDatabase.OPEN_READWRITE);
|
||||||
|
if(db == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new SQLiteDatabaseWrapper(db) ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue