Commit 0bb825af authored by vmocelin's avatar vmocelin
Browse files

Arrumado criação da base a partir do arquivo

TileDbHelper agora pega o PATH por um método, não é estático.
TilesProvider não iniciava a hash tiles, nem fechava o cursor.
TilesProvider agora pega o banco a partir do TileDbHelper.
Adicionado uma chamada na main para fazer um fetch direto e colocar na tela uma imagem.
Adicionado alguns logs para debug.
TileDbContract se mostrou inútil e foi kickado :)
parent 793f7fb1
......@@ -3,6 +3,7 @@ package ufpr.inf.pet.geoloc;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
......@@ -12,9 +13,11 @@ import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
......@@ -42,6 +45,7 @@ public class MainActivity extends ActionBarActivity implements LocationListener
private TextView altitudeText;
private String provider;
private boolean pausado;
private int zoom = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -55,6 +59,7 @@ public class MainActivity extends ActionBarActivity implements LocationListener
latitudeText = (TextView) findViewById(R.id.latitude_label);
longitudeText = (TextView) findViewById(R.id.longitude_label);
altitudeText = (TextView) findViewById(R.id.altitude_label);
ImageView imageView = (ImageView) findViewById(R.id.tile_image);
//Construímos o locationManager e pegamos o serviço em execução
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
......@@ -77,6 +82,16 @@ public class MainActivity extends ActionBarActivity implements LocationListener
longitudeText.setText(R.string.no_location_detected);
altitudeText.setText(R.string.no_location_detected);
}
TilesProvider tprovider = new TilesProvider(getApplicationContext());
Log.d(TAG, "Criou a base");
//DisplayMetrics metrics = new DisplayMetrics();
//getWindowManager().getDefaultDisplay().getMetrics(metrics);
//int width = metrics.widthPixels;
//int height = metrics.heightPixels;
Rect rect = new Rect(0, 0, 1, 1);
Log.d(TAG, rect.toString());
tprovider.fetchTiles(rect, zoom);
imageView.setImageBitmap(tprovider.tiles.get("0:0").getImg());
}
@Override
......
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.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
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.
......@@ -24,8 +19,7 @@ import ufpr.inf.pet.geoloc.TileDbContract.FeedEntry;
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 String DB_PATH;
private static String DB_NAME = "tilesdb";
......@@ -35,12 +29,12 @@ public class TileDbHelper extends SQLiteOpenHelper{
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
* Takes and keeps a reference of the passed context in order to access to the application assets and resources
*/
public TileDbHelper(Context context) {
super(context, DB_NAME, null, 1);
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
this.myContext = context;
}
......@@ -48,11 +42,11 @@ public class TileDbHelper extends SQLiteOpenHelper{
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
Log.d(MainActivity.TAG, "checando base de dados");
boolean dbExist = checkDataBase();
if(!dbExist){
Log.d(MainActivity.TAG, "base de dados nao existe, vai ser copiada");
//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();
......@@ -67,7 +61,8 @@ public class TileDbHelper extends SQLiteOpenHelper{
}
}
else
Log.d(MainActivity.TAG, "base de dados ja existe, não faz nada");
}
/**
......@@ -94,7 +89,7 @@ public class TileDbHelper extends SQLiteOpenHelper{
}
return checkDB != null ? true : false;
return checkDB != null;
}
/**
......@@ -132,7 +127,6 @@ public class TileDbHelper extends SQLiteOpenHelper{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
......
......@@ -12,6 +12,7 @@ import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.util.Log;
import ufpr.inf.pet.geoloc.TileDbContract.FeedEntry;
......@@ -21,7 +22,7 @@ import ufpr.inf.pet.geoloc.TileDbContract.FeedEntry;
public class TilesProvider {
public SQLiteDatabase tilesDb;
public Hashtable<String, Tile> tiles;
TileDbHelper tDbHelper;
private TileDbHelper tDbHelper;
public TilesProvider(String db) {
tilesDb = SQLiteDatabase.openDatabase(db, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
......@@ -29,6 +30,7 @@ public class TilesProvider {
public TilesProvider(Context context) {
tDbHelper = new TileDbHelper(context);
tiles = new Hashtable<>();
try {
tDbHelper.createDataBase();
......@@ -43,12 +45,16 @@ public class TilesProvider {
public void fetchTiles(Rect rect, int zoom){
String query = "SELECT tilex,tiley,image FROM tiles WHERE " +
"tilex >= " + rect.left + " AND tilex <= " + rect.right +
" AND tiley >=" + rect.top + " AND tiley <= " + rect.bottom + " AND zoom = " + zoom;
" AND tiley >= " + rect.top + " AND tiley <= " + rect.bottom + " AND zoom = " + zoom;
tDbHelper.openDataBase();
tDbHelper.close();
tilesDb = tDbHelper.getReadableDatabase();
Cursor cursor;
cursor = tilesDb.rawQuery(query, null);
Hashtable<String, Tile> temp = new Hashtable<String, Tile>();
Hashtable<String, Tile> temp = new Hashtable<>();
if(cursor.moveToFirst()) {
do {
......@@ -66,41 +72,11 @@ public class TilesProvider {
} while(cursor.moveToNext());
}
cursor.close();
tiles.clear();
tiles = temp;
}
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;
}
......
......@@ -30,4 +30,12 @@
android:layout_below="@+id/longitude_label"
android:layout_alignLeft="@+id/latitude_label"
android:layout_alignStart="@+id/latitude_label" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tile_image"
android:layout_below="@+id/altitude_label"
android:layout_centerHorizontal="true"
android:layout_marginTop="113dp" />
</RelativeLayout>
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