Commit c5f9da02 authored by Victor Mocelin's avatar Victor Mocelin
Browse files

Adicionada as classes do BD

parent 7772cbcb
package ufpr.inf.pet.geoloc;
import android.os.Environment;
import android.provider.BaseColumns;
/**
* Created by vmocelin on 11/06/15.
*/
public final class TileDbContract {
// To prevent someone from accidentally instantiating the contract class,
// give it an empty constructor.
public TileDbContract() {}
/* Inner class that defines the table contents */
public static abstract class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "tilesdb";
public static final String COLUMN_NAME_TITLEX = "tilex";
public static final String COLUMN_NAME_TITLEY = "tiley";
public static final String COLUMN_NAME_ZOOM = "zoom";
public static final String COLUMN_NAME_IMAGE = "image";
private static final String DB_PATH = String.valueOf(Environment.getDataDirectory()) + "/data/Geoloc/databases/" + TABLE_NAME;
private static final String DB_FILE = "tiles.sql";
private static final String INT_TYPE = " INT";
private static final String BLOB_TYPE = " BLOB";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
FeedEntry._ID + " INTEGER PRIMARY KEY," +
FeedEntry.COLUMN_NAME_TITLEX + INT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_TITLEY + INT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_ZOOM + INT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_IMAGE + BLOB_TYPE + COMMA_SEP +
" )";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
public static String getSqlCreateEntries() {
return SQL_CREATE_ENTRIES;
}
public static String getSqlDeleteEntries() {
return SQL_DELETE_ENTRIES;
}
public static String getDbPath() {
return DB_PATH;
}
public static String getDbFile() {
return DB_FILE;
}
}
}
package ufpr.inf.pet.geoloc;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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 TileDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
createTables(db);
}
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();
InputStream inputStream = null;
BufferedReader reader = null;
try {
inputStream = manager.open(FeedEntry.getDbPath() +
File.separator + FeedEntry.getDbFile());
reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
String[] sqls = stringBuilder.toString().split(";");
for (String sql : sqls) {
db.execSQL(sql);
}
} catch (IOException e) {
//throw e;
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (reader != null) {
reader.close();
}
} catch (IOException e) {
//throw e;
}
}
}
}
package ufpr.inf.pet.geoloc;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Hashtable;
import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import ufpr.inf.pet.geoloc.TileDbContract.FeedEntry;
/**
* Created by vmocelin on 10/06/15.
*/
public class TileProvider{
public SQLiteDatabase tilesDb;
public Hashtable<String, Tile> tiles;
TileDbHelper tDbHelper;
public TileProvider(Context context) {
tDbHelper = new TileDbHelper(context);
}
public void fetchTiles(){
}
public Hashtable<String, Tile> select(String[] selectTile) throws Exception {
tiles = null;
Cursor cursor = null;
SQLiteDatabase sqlLite = tDbHelper.getReadableDatabase();
String where = "TILEX = ? AND TILEY = ? AND ZOOM = ?";
String[] colunas = new String[] {FeedEntry.COLUMN_NAME_TITLEX,FeedEntry.COLUMN_NAME_TITLEY, FeedEntry.COLUMN_NAME_ZOOM, FeedEntry.COLUMN_NAME_IMAGE};
cursor = sqlLite.query(FeedEntry.TABLE_NAME, colunas, where, selectTile, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
Tile tile = new Tile();
Integer zoom;
String string;
tile.setX(cursor.getInt(cursor.getColumnIndex(FeedEntry.COLUMN_NAME_TITLEX)));
tile.setY(cursor.getInt(cursor.getColumnIndex(FeedEntry.COLUMN_NAME_TITLEY)));
zoom = cursor.getInt(cursor.getColumnIndex(FeedEntry.COLUMN_NAME_ZOOM));
ByteArrayInputStream inputStream = new ByteArrayInputStream(cursor.getBlob(cursor.getColumnIndex(FeedEntry.COLUMN_NAME_IMAGE)));
tile.setImg(BitmapFactory.decodeStream(inputStream));
string = String.valueOf(tile.getX()) + ":" + String.valueOf(tile.getY());
tiles.put(string, tile);
}
if (cursor != null)
cursor.close();
return tiles;
}
public Hashtable<String, Tile> getTiles() {
return tiles;
}
}
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