Commit 597590cc authored by Vytor Calixto's avatar Vytor Calixto 👾
Browse files

Alteradas classes para a visualização

QUASE TUDO funciona. Está dando um erro ao abrir a base de dados.

Creio que corrigindo isso o app mostre as imagens e aí tem que calibrar o software
parent 8d3b33de
......@@ -21,10 +21,6 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MapActivity"
android:label="@string/title_activity_map" >
</activity>
</application>
</manifest>
......@@ -16,22 +16,29 @@ import android.view.View;
public class GelocLocationListener implements LocationListener {
private boolean upateMapView = true;
private boolean stopped;
private MapView mapView;
private String provider;
private FragmentManager fragmentManager;
private boolean providerEnabled = false;
public GelocLocationListener(MapView mapView,FragmentManager fragmentManager) {
public GelocLocationListener(MapView mapView) {
this.mapView = mapView;
this.fragmentManager = fragmentManager;
}
public void stop() {
stopped = true;
mapView = null;
}
@Override
public void onLocationChanged(Location location) {
this.mapView.setGpsLocation(location);
if(!stopped && location != null) {
this.mapView.setGpsLocation(location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getAccuracy());
this.mapView.postInvalidate();
}
}
@Override
......@@ -41,44 +48,11 @@ public class GelocLocationListener implements LocationListener {
@Override
public void onProviderEnabled(String provider) {
if(provider.equals("gps"))
{
providerEnabled = true;
}
Log.d(MainActivity.TAG, "Novo provedor habilitado: " + provider);
}
@Override
public void onProviderDisabled(String provider) {
if(provider.equals("gps"))
{
providerEnabled = false;
//app can be at the forground
try {
askEnableProvider();
}catch (Exception e){
}
}
Log.d(MainActivity.TAG, "Provedor desabilitado: " + provider);
}
public void askEnableProvider(){
DialogFragment dialog = new EnableGpsDialogFragment();
dialog.show(this.fragmentManager, "habilitar gps");
}
public void setUpateMapView(boolean upateMapView) {
this.upateMapView = upateMapView;
}
public void setMapView(MapView mapView) {
this.mapView = mapView;
}
public boolean isProviderEnabled() {
return providerEnabled;
}
public void setProviderEnabled(boolean providerEnabled) {
this.providerEnabled = providerEnabled;
}
}
package ufpr.inf.pet.geoloc;
import android.app.Activity;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.location.LocationServices;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends Activity {
public static String TAG = "GEOLOC:";
public class MainActivity extends ActionBarActivity /*implements LocationListener*/ {
// Constant strings used in onSaveInstanceState, onRestoreInstanceState
private final class Save
{
public final static String GPS_LON = "gpsLon";
public final static String GPS_LAT = "gpsLAT";
public final static String GPS_ALT = "gpsALT";
public final static String GPS_ACC = "gpsAcc";
}
public static final String TAG = "geoloc";
// Constant strings to save settings in SharedPreferences
// Also used for restoring settings
private final class Pref
{
public final static String SEEK_LON = "seek_lon";
public final static String SEEK_LAT = "seek_lat";
public final static String ZOOM = "zoom";
}
/**
* Última localização obtida
*/
private Location lastLocation;
private LocationManager locationManager;
private TextView latitudeText;
private TextView longitudeText;
private TextView altitudeText;
private String provider;
private boolean pausado;
private int zoom = 1;
private MapView mapView;
private GelocLocationListener locationListener;
private TilesProvider tilesProvider;
private Location savedGpsLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
//não está pausado (em segundo plano)
pausado = false;
//Pegamos a referência as TextViews que vamos modificar
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);
//Se o GPS não estiver habilitado alertamos o usuário com uma dialog para que ele habilite
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!isGPSEnabled){
DialogFragment dialog = new EnableGpsDialogFragment();
dialog.show(getFragmentManager(), "habilitar gps");
}
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
lastLocation = locationManager.getLastKnownLocation(provider);
private void initViews(){
tilesProvider = new TilesProvider(getApplicationContext());
Display display = getWindowManager().getDefaultDisplay();
mapView = new MapView(this, display.getWidth(), display.getHeight(), tilesProvider);
if(lastLocation != null){
Log.d(TAG, "Provedor " + provider + " foi selecionado.");
}else{
latitudeText.setText(R.string.no_location_detected);
longitudeText.setText(R.string.no_location_detected);
altitudeText.setText(R.string.no_location_detected);
if(savedGpsLocation != null) {
mapView.setGpsLocation(savedGpsLocation);
}
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
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
mapView.refresh();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
public boolean onKeyDown(int keyCode, KeyEvent event)
{
// Zooming
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_Z)
{
mapView.zoomIn();
return true;
}
// Zooming
else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_X)
{
mapView.zoomOut();
return true;
}
// Enable auto follow
if (keyCode == KeyEvent.KEYCODE_H || keyCode == KeyEvent.KEYCODE_FOCUS)
{
mapView.followMarker();
return true;
}
// Simula estar no PET Computação, para testes
else if (keyCode == KeyEvent.KEYCODE_M || keyCode == KeyEvent.KEYCODE_MENU)
{
mapView.setGpsLocation(-25.45072, -49.232287, 1000, 182);
mapView.invalidate();
return false;
}
return super.onOptionsItemSelected(item);
return super.onKeyDown(keyCode, event);
}
/* Request updates at startup */
/*@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
// Called manually to restore settings from SharedPreferences
void restoreMapViewSettings()
{
SharedPreferences pref = getSharedPreferences("View_Settings", MODE_PRIVATE);
double lon, lat;
int zoom;
lon = Double.parseDouble(pref.getString(Pref.SEEK_LON, "0"));
lat = Double.parseDouble(pref.getString(Pref.SEEK_LAT, "0"));
zoom = pref.getInt(Pref.ZOOM, 0);
//não está pausado (em segundo plano)
pausado = false;
mapView.setSeekLocation(lon, lat);
mapView.setZoom(zoom);
mapView.refresh();
}
/* Remove the locationlistener updates when Activity is paused */
/*
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
// Called manually to save settings in SharedPreferences
void saveMapViewSettings()
{
SharedPreferences.Editor editor = getSharedPreferences("View_Settings", MODE_PRIVATE).edit();
//foi pausado (está em segundo plano)
pausado = true;
PointD seekLocation = mapView.getSeekLocation();
editor.putString(Pref.SEEK_LON, Double.toString(seekLocation.getX()));
editor.putString(Pref.SEEK_LAT, Double.toString(seekLocation.getY()));
editor.putInt(Pref.ZOOM, mapView.getZoom());
editor.commit();
}
@Override
public void onLocationChanged(Location location) {
float lat;
float lng;
float alt;
do {
lat = (float) (location.getLatitude());
lng = (float) (location.getLongitude());
alt = (float) (location.getAltitude());
}while(!pausado && location == null); //Só atualiza os textos se não estiver pausado e o location tiver uma posição
latitudeText.setText("Latitude: " + String.valueOf(lat));
longitudeText.setText("Longitude: " + String.valueOf(lng));
altitudeText.setText("Altitude: " + String.valueOf(alt));
protected void onPause() {
// Save settings before leaving
saveMapViewSettings();
// Mainly releases the MapView pointer inside the listener
locationListener.stop();
// Unregistering our listener
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.removeUpdates(locationListener);
// Closes the source of the tiles (Database in our case)
tilesProvider.close();
// Clears the tiles held in the tilesProvider
tilesProvider.clear();
// Release mapView pointer
mapView = null;
super.onPause();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
protected void onSaveInstanceState(Bundle outState)
{
if (mapView.getGpsLocation() != null)
{
outState.putDouble(Save.GPS_LON, mapView.getGpsLocation().getLongitude());
outState.putDouble(Save.GPS_LAT, mapView.getGpsLocation().getLatitude());
outState.putDouble(Save.GPS_ALT, mapView.getGpsLocation().getAltitude());
outState.putFloat(Save.GPS_ACC, mapView.getGpsLocation().getAccuracy());
}
super.onSaveInstanceState(outState);
}
@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "Novo provedor habilitado: " + provider);
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
double gpsLon, gpsLat, gpsAlt;
float gpsAcc;
gpsLon = savedInstanceState.getDouble(Save.GPS_LON, 999);
gpsLat = savedInstanceState.getDouble(Save.GPS_LAT, 999);
gpsAlt = savedInstanceState.getDouble(Save.GPS_ALT, 999);
gpsAcc = savedInstanceState.getFloat(Save.GPS_ACC, 999);
if (gpsLon != 999 && gpsLat != 999 && gpsAlt != 999 && gpsAcc != 999)
{
savedGpsLocation = new Location(LocationManager.GPS_PROVIDER);
savedGpsLocation.setLongitude(gpsLon);
savedGpsLocation.setLatitude(gpsLat);
savedGpsLocation.setAltitude(gpsAlt);
savedGpsLocation.setAccuracy(gpsAcc);
}
super.onRestoreInstanceState(savedInstanceState);
}
@Override
public void onProviderDisabled(String provider) {
Log.d(TAG, "Provedor desabilitado: " + provider);
}
*/
public void openMap(View view){
Intent mapAcitivityIntent = new Intent(this,MapActivity.class);
startActivity(mapAcitivityIntent);
protected void onResume() {
initViews();
restoreMapViewSettings();
locationListener = new GelocLocationListener(mapView);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
setContentView(mapView);
super.onResume();
}
}
package ufpr.inf.pet.geoloc;
import android.app.DialogFragment;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
public class MapActivity extends ActionBarActivity {
private MapView mapView;
private LocationManager locationManager;
private GelocLocationListener locationListener;
private String provider;
private void init(){
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
// if location provider
if(!locationListener.isProviderEnabled()){
locationListener.askEnableProvider();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
this.mapView = new MapView(this);
/*
add mapView to the layout
*/
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layoutMapActivity);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layout.addView(this.mapView,params);
this.locationListener = new GelocLocationListener(mapView,getFragmentManager());
this.locationManager = (LocationManager) this.getSystemService(this.LOCATION_SERVICE);
//Verify if the gps is enable, if not ask(s?) for the user enable it
//TODO: Ask for enable gps every time that the user go back
this.locationListener.setProviderEnabled(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER));
if(!this.locationListener.isProviderEnabled()){
this.locationListener.askEnableProvider();
}
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
//TODO: See if the time and the distance selected are the best
locationManager.requestLocationUpdates(provider, 400, 1, locationListener);
}
//TODO: See if the menu is needed
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_map, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
......@@ -2,53 +2,269 @@ package ufpr.inf.pet.geoloc;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.location.Location;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import java.util.Collection;
/**
* Created by pet on 23/09/15.
*/
public class MapView extends View {
private Context context;
private int viewWidth;
private int viewHeight;
private TilesProvider tilesProvider;
private TilesManager tilesManager;
private Paint fontPaint;
private Paint circlePaint;
private PointD seekLocation;
private Location gpsLocation;
private Paint circlePaint = new Paint();
private Paint bitmapPaint = new Paint();
private PointD seekLocation = new PointD(0,0);
private Location gpsLocation = null;
private boolean autoFollow;
private PointD lastTouchPos = new PointD(-1, -1);
public MapView(Context context) {
public MapView(Context context, int viewWidth, int viewHeight, TilesProvider tilesProvider) {
super(context);
this.setBackgroundColor(getResources().getColor(android.R.color.black));
this.context = context;
this.tilesProvider = tilesProvider;
this.viewHeight = viewHeight;
this.viewWidth = viewWidth;
tilesManager = new TilesManager(256, viewWidth, viewHeight);
initPaints();
fetchTiles();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// Define a largura e altura que foram passadas no contrutor como a dimensão da view
setMeasuredDimension(viewWidth, viewHeight);
}
private void initPaints() {
fontPaint = new Paint();
fontPaint.setColor(Color.DKGRAY);
fontPaint.setShadowLayer(1, 1, 1, Color.BLACK);
fontPaint.setTextSize(20);
public void setGpsLocation(Location location){