GeolocLocationListener está sendo usado/não deixa usar sem gps

parent 61a67d0c
package ufpr.inf.pet.geoloc;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
......@@ -14,26 +17,21 @@ public class GelocLocationListener implements LocationListener {
private boolean upateMapView = true;
private MapView mapView;
private String provider;
private FragmentManager fragmentManager;
private boolean providerEnabled = false;
public GelocLocationListener(MapActivity mapActivity, MapView mapView) {
public GelocLocationListener(MapView mapView,FragmentManager fragmentManager) {
this.mapView = mapView;
this.fragmentManager = fragmentManager;
}
@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(!upateMapView && location == null); //Só atualiza os textos se não estiver pausado e o location tiver uma posição
Log.d(MainActivity.TAG, "Latitude: " + String.valueOf(lat));
Log.d(MainActivity.TAG, "Longitude: " + String.valueOf(lng));
Log.d(MainActivity.TAG, "Altitude: " + String.valueOf(alt));
this.mapView.setGpsLocation(location);
}
@Override
......@@ -43,13 +41,32 @@ 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;
......@@ -57,4 +74,11 @@ public class GelocLocationListener implements LocationListener {
public void setMapView(MapView mapView) {
this.mapView = mapView;
}
public boolean isProviderEnabled() {
return providerEnabled;
}
public void setProviderEnabled(boolean providerEnabled) {
this.providerEnabled = providerEnabled;
}
}
......@@ -32,7 +32,7 @@ import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends ActionBarActivity implements LocationListener {
public class MainActivity extends ActionBarActivity /*implements LocationListener*/ {
public static final String TAG = "geoloc";
......@@ -52,7 +52,7 @@ public class MainActivity extends ActionBarActivity implements LocationListener
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
//não está pausado (em segundo plano)
pausado = false;
......@@ -93,6 +93,7 @@ public class MainActivity extends ActionBarActivity implements LocationListener
Log.d(TAG, rect.toString());
tprovider.fetchTiles(rect, zoom);
imageView.setImageBitmap(tprovider.tiles.get("0:0").getImg());
*/
}
@Override
......@@ -118,7 +119,7 @@ public class MainActivity extends ActionBarActivity implements LocationListener
}
/* Request updates at startup */
@Override
/*@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
......@@ -128,6 +129,7 @@ public class MainActivity extends ActionBarActivity implements LocationListener
}
/* Remove the locationlistener updates when Activity is paused */
/*
@Override
protected void onPause() {
super.onPause();
......@@ -167,9 +169,10 @@ public class MainActivity extends ActionBarActivity implements LocationListener
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);
}
}
......@@ -11,6 +11,8 @@ 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 {
......@@ -32,48 +34,56 @@ public class MapActivity extends ActionBarActivity {
@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.locationListener = new GelocLocationListener(this,mapView);
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
boolean isGPSEnabled = false;
do{
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
DialogFragment dialog = new EnableGpsDialogFragment();
dialog.show(getFragmentManager(), "habilitar gps");
//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();
}
while(!isGPSEnabled);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location lastLocation = locationManager.getLastKnownLocation(provider);
if(lastLocation != null){
Log.d(MainActivity.TAG, "Provedor " + provider + " foi selecionado.");
}else{
Log.d(MainActivity.TAG, "Sem localização!");
}
//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) {
......
......@@ -5,6 +5,7 @@ import android.graphics.Canvas;
import android.graphics.Paint;
import android.location.Location;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**
......@@ -20,13 +21,27 @@ public class MapView extends View {
private PointD seekLocation;
private Location gpsLocation;
public MapView(Context context, AttributeSet attrs) {
super(context, attrs);
public MapView(Context context) {
super(context);
this.setBackgroundColor(getResources().getColor(android.R.color.black));
}
public void setGpsLocation(Location location){
this.gpsLocation=location;
float lat;
float lng;
float alt;
do {
lat = (float) (location.getLatitude());
lng = (float) (location.getLongitude());
alt = (float) (location.getAltitude());
}while(location == null); //Só atualiza os textos se não estiver pausado e o location tiver uma posição
Log.d(MainActivity.TAG, "Latitude: " + String.valueOf(lat));
Log.d(MainActivity.TAG, "Longitude: " + String.valueOf(lng));
Log.d(MainActivity.TAG, "Altitude: " + String.valueOf(alt));
}
public void drawTiles(Canvas canvas,int pointOffset){
......
......@@ -4,9 +4,8 @@
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="ufpr.inf.pet.geoloc.MapActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
tools:context="ufpr.inf.pet.geoloc.MapActivity"
android:id="@+id/layoutMapActivity"
android:focusableInTouchMode="false">
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
</LinearLayout>
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