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;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import ufpr.inf.pet.geoloc.TileDbContract.FeedEntry;
/**
* Created by vmocelin on 11/06/15.
*/
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";
public class TileDbHelper extends SQLiteOpenHelper{
//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) {
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
db.execSQL(FeedEntry.getSqlDeleteEntries());
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
private void createTables(SQLiteDatabase db) {
AssetManager manager = context.getAssets();
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
InputStream inputStream = null;
BufferedReader reader = null;
SQLiteDatabase checkDB = null;
try {
inputStream = manager.open(FeedEntry.getDbPath() +
File.separator + FeedEntry.getDbFile());
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
reader = new BufferedReader(new InputStreamReader(inputStream));
}catch(SQLiteException e){
StringBuilder stringBuilder = new StringBuilder();
//database does't exist yet.
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
String[] sqls = stringBuilder.toString().split(";");
if(checkDB != null){
checkDB.close();
for (String sql : sqls) {
db.execSQL(sql);
}
} catch (IOException e) {
//throw e;
} finally {
try {
if (inputStream != null) {
inputStream.close();
return checkDB != null ? true : false;
}
if (reader != null) {
reader.close();
/**
* 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);
}
} catch (IOException e) {
//throw e;
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// 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;
*/
public class TilesManager {
private static double raioTerra = 6378137;
private double minLatitude;
private double maxLatitude;
private double minLongitude;
private double maxLongitude;
private double minLatitude = 0;
private double maxLatitude = 90;
private double minLongitude = -180;
private double maxLongitude = 180;
private int maxZoom = 5;
private int tamTile = 256;
private int viewWidth;
......
......@@ -4,6 +4,7 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import android.content.Context;
......@@ -28,6 +29,15 @@ public class TilesProvider {
public TilesProvider(Context 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){
......
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