Commit 793f7fb1 authored by Victor Mocelin's avatar Victor Mocelin
Browse files

Adicionado o método que pega a base do sql

TilesDbHelper agora verifica se a base existe e se não existir cria a partir do arquivo sql nos assests.
TilesProvider ao ser criado já chama o Helper para criar a base.
Atualizado dados de latitude e longitude no TilesManager.
parent 8e52fd37
...@@ -3,80 +3,160 @@ package ufpr.inf.pet.geoloc; ...@@ -3,80 +3,160 @@ package ufpr.inf.pet.geoloc;
import android.content.Context; import android.content.Context;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.database.Cursor; import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream;
import ufpr.inf.pet.geoloc.TileDbContract.FeedEntry; import ufpr.inf.pet.geoloc.TileDbContract.FeedEntry;
/** /**
* Created by vmocelin on 11/06/15. * Created by vmocelin on 11/06/15.
*/ */
public class TileDbHelper extends SQLiteOpenHelper { public class TileDbHelper extends SQLiteOpenHelper{
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Tiles.db";
//The Android's default system path of your application database.
//private String DB_PATH = Context.getFilesDir().getPath() + "/ufpr.inf.pet.geoloc/databases/";
private static String DB_PATH = "/data/data/ufpr.inf.pet.geoloc/databases/";
private static String DB_NAME = "tilesdb";
private SQLiteDatabase myDataBase;
private final Context myContext;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public TileDbHelper(Context context) { public TileDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
super(context, DB_NAME, null, 1);
this.myContext = context;
} }
public void onCreate(SQLiteDatabase db) {
createTables(db); /**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(!dbExist){
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
} }
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is /**
// to simply to discard the data and start over * Check if the database already exist to avoid re-copying the file each time you open the application.
db.execSQL(FeedEntry.getSqlDeleteEntries()); * @return true if it exists, false if it doesn't
onCreate(db); */
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
} }
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion); /**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} }
private void createTables(SQLiteDatabase db) { public void openDataBase() throws SQLException {
AssetManager manager = context.getAssets();
InputStream inputStream = null; //Open the database
BufferedReader reader = null; String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
try { }
inputStream = manager.open(FeedEntry.getDbPath() +
File.separator + FeedEntry.getDbFile());
reader = new BufferedReader(new InputStreamReader(inputStream)); @Override
public synchronized void close() {
StringBuilder stringBuilder = new StringBuilder(); if(myDataBase != null)
myDataBase.close();
String line = null; super.close();
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
String[] sqls = stringBuilder.toString().split(";"); }
for (String sql : sqls) { @Override
db.execSQL(sql); public void onCreate(SQLiteDatabase db) {
}
} catch (IOException e) { }
//throw e;
} finally { @Override
try { public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (inputStream != null) {
inputStream.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
//throw e;
}
}
} }
}
// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.
}
\ No newline at end of file
...@@ -8,10 +8,10 @@ import android.graphics.Rect; ...@@ -8,10 +8,10 @@ import android.graphics.Rect;
*/ */
public class TilesManager { public class TilesManager {
private static double raioTerra = 6378137; private static double raioTerra = 6378137;
private double minLatitude; private double minLatitude = 0;
private double maxLatitude; private double maxLatitude = 90;
private double minLongitude; private double minLongitude = -180;
private double maxLongitude; private double maxLongitude = 180;
private int maxZoom = 5; private int maxZoom = 5;
private int tamTile = 256; private int tamTile = 256;
private int viewWidth; private int viewWidth;
......
...@@ -4,6 +4,7 @@ import android.database.Cursor; ...@@ -4,6 +4,7 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Hashtable; import java.util.Hashtable;
import android.content.Context; import android.content.Context;
...@@ -28,6 +29,15 @@ public class TilesProvider { ...@@ -28,6 +29,15 @@ public class TilesProvider {
public TilesProvider(Context context) { public TilesProvider(Context context) {
tDbHelper = new TileDbHelper(context); tDbHelper = new TileDbHelper(context);
try {
tDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
} }
public void fetchTiles(Rect rect, int zoom){ public void fetchTiles(Rect rect, int zoom){
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment